1 /****************************************************************************
2 * drivers/bch/bchlib_cache.c
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership. The
7 * ASF licenses this file to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance with the
9 * License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations
17 * under the License.
18 *
19 ****************************************************************************/
20
21 /****************************************************************************
22 * Included Files
23 ****************************************************************************/
24 #include <sys/types.h>
25 #include <stdbool.h>
26 #include <errno.h>
27 #include <assert.h>
28 #include "bch.h"
29
30 /****************************************************************************
31 * Private Functions
32 ****************************************************************************/
33
34 /****************************************************************************
35 * Public Functions
36 ****************************************************************************/
37
38 /****************************************************************************
39 * Name: bchlib_flushsector
40 *
41 * Description:
42 * Flush the current contents of the sector buffer (if dirty)
43 *
44 * Assumptions:
45 * Caller must assume mutual exclusion
46 *
47 ****************************************************************************/
48
bchlib_flushsector(struct bchlib_s * bch)49 int bchlib_flushsector(struct bchlib_s *bch)
50 {
51 int ret = OK;
52
53 /* Check if the sector has been modified and is out of synch with the
54 * media.
55 */
56
57 if (bch->dirty)
58 {
59 /* Write the sector to the media */
60
61 ret = los_disk_write(bch->disk->disk_id, (const void *)bch->buffer, bch->sector, 1);
62 if (ret < 0)
63 {
64 PRINTK("bchlib_flushsector Write failed: %d\n", ret);
65 return ret;
66 }
67
68 /* The sector is now in sync with the media */
69
70 bch->dirty = false;
71 }
72
73 return ret;
74 }
75
76 /****************************************************************************
77 * Name: bchlib_readsector
78 *
79 * Description:
80 * Flush the current contents of the sector buffer (if dirty)
81 *
82 * Assumptions:
83 * Caller must assume mutual exclusion
84 *
85 ****************************************************************************/
86
bchlib_readsector(struct bchlib_s * bch,unsigned long long sector)87 int bchlib_readsector(struct bchlib_s *bch, unsigned long long sector)
88 {
89 int ret = OK;
90
91 if (bch->sector != sector)
92 {
93 ret = bchlib_flushsector(bch);
94 if (ret < 0)
95 {
96 return ret;
97 }
98
99 bch->sector = (unsigned long long)-1;
100 /* useRead is set TRUE, it'll use read block for not reading large data */
101 ret = los_disk_read(bch->disk->disk_id, (void *)bch->buffer, sector, 1, TRUE);
102 if (ret < 0)
103 {
104 PRINTK("Read failed: %d\n", ret);
105 return ret;
106 }
107 bch->sector = sector;
108 }
109 return ret;
110 }
111
112