diff options
Diffstat (limited to 'fs/ubifs/misc.h')
-rw-r--r-- | fs/ubifs/misc.h | 342 |
1 files changed, 342 insertions, 0 deletions
diff --git a/fs/ubifs/misc.h b/fs/ubifs/misc.h new file mode 100644 index 000000000000..4beccfc256d2 --- /dev/null +++ b/fs/ubifs/misc.h | |||
@@ -0,0 +1,342 @@ | |||
1 | /* | ||
2 | * This file is part of UBIFS. | ||
3 | * | ||
4 | * Copyright (C) 2006-2008 Nokia Corporation | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify it | ||
7 | * under the terms of the GNU General Public License version 2 as published by | ||
8 | * the Free Software Foundation. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
13 | * more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License along with | ||
16 | * this program; if not, write to the Free Software Foundation, Inc., 51 | ||
17 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | * | ||
19 | * Authors: Artem Bityutskiy (Битюцкий Артём) | ||
20 | * Adrian Hunter | ||
21 | */ | ||
22 | |||
23 | /* | ||
24 | * This file contains miscellaneous helper functions. | ||
25 | */ | ||
26 | |||
27 | #ifndef __UBIFS_MISC_H__ | ||
28 | #define __UBIFS_MISC_H__ | ||
29 | |||
30 | /** | ||
31 | * ubifs_zn_dirty - check if znode is dirty. | ||
32 | * @znode: znode to check | ||
33 | * | ||
34 | * This helper function returns %1 if @znode is dirty and %0 otherwise. | ||
35 | */ | ||
36 | static inline int ubifs_zn_dirty(const struct ubifs_znode *znode) | ||
37 | { | ||
38 | return !!test_bit(DIRTY_ZNODE, &znode->flags); | ||
39 | } | ||
40 | |||
41 | /** | ||
42 | * ubifs_wake_up_bgt - wake up background thread. | ||
43 | * @c: UBIFS file-system description object | ||
44 | */ | ||
45 | static inline void ubifs_wake_up_bgt(struct ubifs_info *c) | ||
46 | { | ||
47 | if (c->bgt && !c->need_bgt) { | ||
48 | c->need_bgt = 1; | ||
49 | wake_up_process(c->bgt); | ||
50 | } | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * ubifs_tnc_find_child - find next child in znode. | ||
55 | * @znode: znode to search at | ||
56 | * @start: the zbranch index to start at | ||
57 | * | ||
58 | * This helper function looks for znode child starting at index @start. Returns | ||
59 | * the child or %NULL if no children were found. | ||
60 | */ | ||
61 | static inline struct ubifs_znode * | ||
62 | ubifs_tnc_find_child(struct ubifs_znode *znode, int start) | ||
63 | { | ||
64 | while (start < znode->child_cnt) { | ||
65 | if (znode->zbranch[start].znode) | ||
66 | return znode->zbranch[start].znode; | ||
67 | start += 1; | ||
68 | } | ||
69 | |||
70 | return NULL; | ||
71 | } | ||
72 | |||
73 | /** | ||
74 | * ubifs_inode - get UBIFS inode information by VFS 'struct inode' object. | ||
75 | * @inode: the VFS 'struct inode' pointer | ||
76 | */ | ||
77 | static inline struct ubifs_inode *ubifs_inode(const struct inode *inode) | ||
78 | { | ||
79 | return container_of(inode, struct ubifs_inode, vfs_inode); | ||
80 | } | ||
81 | |||
82 | /** | ||
83 | * ubifs_ro_mode - switch UBIFS to read read-only mode. | ||
84 | * @c: UBIFS file-system description object | ||
85 | * @err: error code which is the reason of switching to R/O mode | ||
86 | */ | ||
87 | static inline void ubifs_ro_mode(struct ubifs_info *c, int err) | ||
88 | { | ||
89 | if (!c->ro_media) { | ||
90 | c->ro_media = 1; | ||
91 | ubifs_warn("switched to read-only mode, error %d", err); | ||
92 | dbg_dump_stack(); | ||
93 | } | ||
94 | } | ||
95 | |||
96 | /** | ||
97 | * ubifs_compr_present - check if compressor was compiled in. | ||
98 | * @compr_type: compressor type to check | ||
99 | * | ||
100 | * This function returns %1 of compressor of type @compr_type is present, and | ||
101 | * %0 if not. | ||
102 | */ | ||
103 | static inline int ubifs_compr_present(int compr_type) | ||
104 | { | ||
105 | ubifs_assert(compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT); | ||
106 | return !!ubifs_compressors[compr_type]->capi_name; | ||
107 | } | ||
108 | |||
109 | /** | ||
110 | * ubifs_compr_name - get compressor name string by its type. | ||
111 | * @compr_type: compressor type | ||
112 | * | ||
113 | * This function returns compressor type string. | ||
114 | */ | ||
115 | static inline const char *ubifs_compr_name(int compr_type) | ||
116 | { | ||
117 | ubifs_assert(compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT); | ||
118 | return ubifs_compressors[compr_type]->name; | ||
119 | } | ||
120 | |||
121 | /** | ||
122 | * ubifs_wbuf_sync - synchronize write-buffer. | ||
123 | * @wbuf: write-buffer to synchronize | ||
124 | * | ||
125 | * This is the same as as 'ubifs_wbuf_sync_nolock()' but it does not assume | ||
126 | * that the write-buffer is already locked. | ||
127 | */ | ||
128 | static inline int ubifs_wbuf_sync(struct ubifs_wbuf *wbuf) | ||
129 | { | ||
130 | int err; | ||
131 | |||
132 | mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); | ||
133 | err = ubifs_wbuf_sync_nolock(wbuf); | ||
134 | mutex_unlock(&wbuf->io_mutex); | ||
135 | return err; | ||
136 | } | ||
137 | |||
138 | /** | ||
139 | * ubifs_leb_unmap - unmap an LEB. | ||
140 | * @c: UBIFS file-system description object | ||
141 | * @lnum: LEB number to unmap | ||
142 | * | ||
143 | * This function returns %0 on success and a negative error code on failure. | ||
144 | */ | ||
145 | static inline int ubifs_leb_unmap(const struct ubifs_info *c, int lnum) | ||
146 | { | ||
147 | int err; | ||
148 | |||
149 | if (c->ro_media) | ||
150 | return -EROFS; | ||
151 | err = ubi_leb_unmap(c->ubi, lnum); | ||
152 | if (err) { | ||
153 | ubifs_err("unmap LEB %d failed, error %d", lnum, err); | ||
154 | return err; | ||
155 | } | ||
156 | |||
157 | return 0; | ||
158 | } | ||
159 | |||
160 | /** | ||
161 | * ubifs_leb_write - write to a LEB. | ||
162 | * @c: UBIFS file-system description object | ||
163 | * @lnum: LEB number to write | ||
164 | * @buf: buffer to write from | ||
165 | * @offs: offset within LEB to write to | ||
166 | * @len: length to write | ||
167 | * @dtype: data type | ||
168 | * | ||
169 | * This function returns %0 on success and a negative error code on failure. | ||
170 | */ | ||
171 | static inline int ubifs_leb_write(const struct ubifs_info *c, int lnum, | ||
172 | const void *buf, int offs, int len, int dtype) | ||
173 | { | ||
174 | int err; | ||
175 | |||
176 | if (c->ro_media) | ||
177 | return -EROFS; | ||
178 | err = ubi_leb_write(c->ubi, lnum, buf, offs, len, dtype); | ||
179 | if (err) { | ||
180 | ubifs_err("writing %d bytes at %d:%d, error %d", | ||
181 | len, lnum, offs, err); | ||
182 | return err; | ||
183 | } | ||
184 | |||
185 | return 0; | ||
186 | } | ||
187 | |||
188 | /** | ||
189 | * ubifs_leb_change - atomic LEB change. | ||
190 | * @c: UBIFS file-system description object | ||
191 | * @lnum: LEB number to write | ||
192 | * @buf: buffer to write from | ||
193 | * @len: length to write | ||
194 | * @dtype: data type | ||
195 | * | ||
196 | * This function returns %0 on success and a negative error code on failure. | ||
197 | */ | ||
198 | static inline int ubifs_leb_change(const struct ubifs_info *c, int lnum, | ||
199 | const void *buf, int len, int dtype) | ||
200 | { | ||
201 | int err; | ||
202 | |||
203 | if (c->ro_media) | ||
204 | return -EROFS; | ||
205 | err = ubi_leb_change(c->ubi, lnum, buf, len, dtype); | ||
206 | if (err) { | ||
207 | ubifs_err("changing %d bytes in LEB %d, error %d", | ||
208 | len, lnum, err); | ||
209 | return err; | ||
210 | } | ||
211 | |||
212 | return 0; | ||
213 | } | ||
214 | |||
215 | /** | ||
216 | * ubifs_encode_dev - encode device node IDs. | ||
217 | * @dev: UBIFS device node information | ||
218 | * @rdev: device IDs to encode | ||
219 | * | ||
220 | * This is a helper function which encodes major/minor numbers of a device node | ||
221 | * into UBIFS device node description. We use standard Linux "new" and "huge" | ||
222 | * encodings. | ||
223 | */ | ||
224 | static inline int ubifs_encode_dev(union ubifs_dev_desc *dev, dev_t rdev) | ||
225 | { | ||
226 | if (new_valid_dev(rdev)) { | ||
227 | dev->new = cpu_to_le32(new_encode_dev(rdev)); | ||
228 | return sizeof(dev->new); | ||
229 | } else { | ||
230 | dev->huge = cpu_to_le64(huge_encode_dev(rdev)); | ||
231 | return sizeof(dev->huge); | ||
232 | } | ||
233 | } | ||
234 | |||
235 | /** | ||
236 | * ubifs_add_dirt - add dirty space to LEB properties. | ||
237 | * @c: the UBIFS file-system description object | ||
238 | * @lnum: LEB to add dirty space for | ||
239 | * @dirty: dirty space to add | ||
240 | * | ||
241 | * This is a helper function which increased amount of dirty LEB space. Returns | ||
242 | * zero in case of success and a negative error code in case of failure. | ||
243 | */ | ||
244 | static inline int ubifs_add_dirt(struct ubifs_info *c, int lnum, int dirty) | ||
245 | { | ||
246 | return ubifs_update_one_lp(c, lnum, LPROPS_NC, dirty, 0, 0); | ||
247 | } | ||
248 | |||
249 | /** | ||
250 | * ubifs_return_leb - return LEB to lprops. | ||
251 | * @c: the UBIFS file-system description object | ||
252 | * @lnum: LEB to return | ||
253 | * | ||
254 | * This helper function cleans the "taken" flag of a logical eraseblock in the | ||
255 | * lprops. Returns zero in case of success and a negative error code in case of | ||
256 | * failure. | ||
257 | */ | ||
258 | static inline int ubifs_return_leb(struct ubifs_info *c, int lnum) | ||
259 | { | ||
260 | return ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0, | ||
261 | LPROPS_TAKEN, 0); | ||
262 | } | ||
263 | |||
264 | /** | ||
265 | * ubifs_idx_node_sz - return index node size. | ||
266 | * @c: the UBIFS file-system description object | ||
267 | * @child_cnt: number of children of this index node | ||
268 | */ | ||
269 | static inline int ubifs_idx_node_sz(const struct ubifs_info *c, int child_cnt) | ||
270 | { | ||
271 | return UBIFS_IDX_NODE_SZ + (UBIFS_BRANCH_SZ + c->key_len) * child_cnt; | ||
272 | } | ||
273 | |||
274 | /** | ||
275 | * ubifs_idx_branch - return pointer to an index branch. | ||
276 | * @c: the UBIFS file-system description object | ||
277 | * @idx: index node | ||
278 | * @bnum: branch number | ||
279 | */ | ||
280 | static inline | ||
281 | struct ubifs_branch *ubifs_idx_branch(const struct ubifs_info *c, | ||
282 | const struct ubifs_idx_node *idx, | ||
283 | int bnum) | ||
284 | { | ||
285 | return (struct ubifs_branch *)((void *)idx->branches + | ||
286 | (UBIFS_BRANCH_SZ + c->key_len) * bnum); | ||
287 | } | ||
288 | |||
289 | /** | ||
290 | * ubifs_idx_key - return pointer to an index key. | ||
291 | * @c: the UBIFS file-system description object | ||
292 | * @idx: index node | ||
293 | */ | ||
294 | static inline void *ubifs_idx_key(const struct ubifs_info *c, | ||
295 | const struct ubifs_idx_node *idx) | ||
296 | { | ||
297 | return (void *)((struct ubifs_branch *)idx->branches)->key; | ||
298 | } | ||
299 | |||
300 | /** | ||
301 | * ubifs_reported_space - calculate reported free space. | ||
302 | * @c: the UBIFS file-system description object | ||
303 | * @free: amount of free space | ||
304 | * | ||
305 | * This function calculates amount of free space which will be reported to | ||
306 | * user-space. User-space application tend to expect that if the file-system | ||
307 | * (e.g., via the 'statfs()' call) reports that it has N bytes available, they | ||
308 | * are able to write a file of size N. UBIFS attaches node headers to each data | ||
309 | * node and it has to write indexind nodes as well. This introduces additional | ||
310 | * overhead, and UBIFS it has to report sligtly less free space to meet the | ||
311 | * above expectetion. | ||
312 | * | ||
313 | * This function assumes free space is made up of uncompressed data nodes and | ||
314 | * full index nodes (one per data node, doubled because we always allow enough | ||
315 | * space to write the index twice). | ||
316 | * | ||
317 | * Note, the calculation is pessimistic, which means that most of the time | ||
318 | * UBIFS reports less space than it actually has. | ||
319 | */ | ||
320 | static inline long long ubifs_reported_space(const struct ubifs_info *c, | ||
321 | uint64_t free) | ||
322 | { | ||
323 | int divisor, factor; | ||
324 | |||
325 | divisor = UBIFS_MAX_DATA_NODE_SZ + (c->max_idx_node_sz << 1); | ||
326 | factor = UBIFS_MAX_DATA_NODE_SZ - UBIFS_DATA_NODE_SZ; | ||
327 | do_div(free, divisor); | ||
328 | |||
329 | return free * factor; | ||
330 | } | ||
331 | |||
332 | /** | ||
333 | * ubifs_current_time - round current time to time granularity. | ||
334 | * @inode: inode | ||
335 | */ | ||
336 | static inline struct timespec ubifs_current_time(struct inode *inode) | ||
337 | { | ||
338 | return (inode->i_sb->s_time_gran < NSEC_PER_SEC) ? | ||
339 | current_fs_time(inode->i_sb) : CURRENT_TIME_SEC; | ||
340 | } | ||
341 | |||
342 | #endif /* __UBIFS_MISC_H__ */ | ||