1 /****************************************************************************
2 * drivers/bch/bchlib_cache.c
3 *
4 * Copyright (C) 2008-2009, 2014, 2016 Gregory Nutt. All rights reserved.
5 * Author: Gregory Nutt <gnutt@nuttx.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name NuttX nor the names of its contributors may be
18 * used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ****************************************************************************/
35
36 /****************************************************************************
37 * Included Files
38 ****************************************************************************/
39 #include <sys/types.h>
40 #include <stdbool.h>
41 #include <errno.h>
42 #include <assert.h>
43 #include "bch.h"
44
45 /****************************************************************************
46 * Private Functions
47 ****************************************************************************/
48
49 /****************************************************************************
50 * Public Functions
51 ****************************************************************************/
52
53 /****************************************************************************
54 * Name: bchlib_flushsector
55 *
56 * Description:
57 * Flush the current contents of the sector buffer (if dirty)
58 *
59 * Assumptions:
60 * Caller must assume mutual exclusion
61 *
62 ****************************************************************************/
63
bchlib_flushsector(struct bchlib_s * bch)64 int bchlib_flushsector(struct bchlib_s *bch)
65 {
66 int ret = OK;
67
68 /* Check if the sector has been modified and is out of synch with the
69 * media.
70 */
71
72 if (bch->dirty)
73 {
74 /* Write the sector to the media */
75
76 ret = los_disk_write(bch->disk->disk_id, (const void *)bch->buffer, bch->sector, 1);
77 if (ret < 0)
78 {
79 PRINTK("bchlib_flushsector Write failed: %d\n", ret);
80 return ret;
81 }
82
83 /* The sector is now in sync with the media */
84
85 bch->dirty = false;
86 }
87
88 return ret;
89 }
90
91 /****************************************************************************
92 * Name: bchlib_readsector
93 *
94 * Description:
95 * Flush the current contents of the sector buffer (if dirty)
96 *
97 * Assumptions:
98 * Caller must assume mutual exclusion
99 *
100 ****************************************************************************/
101
bchlib_readsector(struct bchlib_s * bch,unsigned long long sector)102 int bchlib_readsector(struct bchlib_s *bch, unsigned long long sector)
103 {
104 int ret = OK;
105
106 if (bch->sector != sector)
107 {
108 ret = bchlib_flushsector(bch);
109 if (ret < 0)
110 {
111 return ret;
112 }
113
114 bch->sector = (unsigned long long)-1;
115 /* useRead is set TRUE, it'll use read block for not reading large data */
116 ret = los_disk_read(bch->disk->disk_id, (void *)bch->buffer, sector, 1, TRUE);
117 if (ret < 0)
118 {
119 PRINTK("Read failed: %d\n", ret);
120 return ret;
121 }
122 bch->sector = sector;
123 }
124 return ret;
125 }
126
127