blob: 160cd909e9579f486804cd293ba1d414796e3e70 [file] [log] [blame]
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001/*
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 */
36static inline int ubifs_zn_dirty(const struct ubifs_znode *znode)
37{
38 return !!test_bit(DIRTY_ZNODE, &znode->flags);
39}
40
41/**
Artem Bityutskiyf42eed72011-05-30 14:45:30 +030042 * ubifs_zn_obsolete - check if znode is obsolete.
43 * @znode: znode to check
44 *
45 * This helper function returns %1 if @znode is obsolete and %0 otherwise.
46 */
47static inline int ubifs_zn_obsolete(const struct ubifs_znode *znode)
48{
49 return !!test_bit(OBSOLETE_ZNODE, &znode->flags);
50}
51
52/**
53 * ubifs_zn_cow - check if znode has to be copied on write.
54 * @znode: znode to check
55 *
56 * This helper function returns %1 if @znode is has COW flag set and %0
57 * otherwise.
58 */
59static inline int ubifs_zn_cow(const struct ubifs_znode *znode)
60{
61 return !!test_bit(COW_ZNODE, &znode->flags);
62}
63
64/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +030065 * ubifs_wake_up_bgt - wake up background thread.
66 * @c: UBIFS file-system description object
67 */
68static inline void ubifs_wake_up_bgt(struct ubifs_info *c)
69{
70 if (c->bgt && !c->need_bgt) {
71 c->need_bgt = 1;
72 wake_up_process(c->bgt);
73 }
74}
75
76/**
77 * ubifs_tnc_find_child - find next child in znode.
78 * @znode: znode to search at
79 * @start: the zbranch index to start at
80 *
81 * This helper function looks for znode child starting at index @start. Returns
82 * the child or %NULL if no children were found.
83 */
84static inline struct ubifs_znode *
85ubifs_tnc_find_child(struct ubifs_znode *znode, int start)
86{
87 while (start < znode->child_cnt) {
88 if (znode->zbranch[start].znode)
89 return znode->zbranch[start].znode;
90 start += 1;
91 }
92
93 return NULL;
94}
95
96/**
97 * ubifs_inode - get UBIFS inode information by VFS 'struct inode' object.
98 * @inode: the VFS 'struct inode' pointer
99 */
100static inline struct ubifs_inode *ubifs_inode(const struct inode *inode)
101{
102 return container_of(inode, struct ubifs_inode, vfs_inode);
103}
104
105/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300106 * ubifs_compr_present - check if compressor was compiled in.
107 * @compr_type: compressor type to check
108 *
109 * This function returns %1 of compressor of type @compr_type is present, and
110 * %0 if not.
111 */
112static inline int ubifs_compr_present(int compr_type)
113{
114 ubifs_assert(compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
115 return !!ubifs_compressors[compr_type]->capi_name;
116}
117
118/**
119 * ubifs_compr_name - get compressor name string by its type.
120 * @compr_type: compressor type
121 *
122 * This function returns compressor type string.
123 */
124static inline const char *ubifs_compr_name(int compr_type)
125{
126 ubifs_assert(compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
127 return ubifs_compressors[compr_type]->name;
128}
129
130/**
131 * ubifs_wbuf_sync - synchronize write-buffer.
132 * @wbuf: write-buffer to synchronize
133 *
134 * This is the same as as 'ubifs_wbuf_sync_nolock()' but it does not assume
135 * that the write-buffer is already locked.
136 */
137static inline int ubifs_wbuf_sync(struct ubifs_wbuf *wbuf)
138{
139 int err;
140
141 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
142 err = ubifs_wbuf_sync_nolock(wbuf);
143 mutex_unlock(&wbuf->io_mutex);
144 return err;
145}
146
147/**
148 * ubifs_leb_unmap - unmap an LEB.
149 * @c: UBIFS file-system description object
150 * @lnum: LEB number to unmap
151 *
152 * This function returns %0 on success and a negative error code on failure.
153 */
154static inline int ubifs_leb_unmap(const struct ubifs_info *c, int lnum)
155{
156 int err;
157
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300158 ubifs_assert(!c->ro_media && !c->ro_mount);
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300159 if (c->ro_error)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300160 return -EROFS;
161 err = ubi_leb_unmap(c->ubi, lnum);
162 if (err) {
163 ubifs_err("unmap LEB %d failed, error %d", lnum, err);
164 return err;
165 }
166
167 return 0;
168}
169
170/**
171 * ubifs_leb_write - write to a LEB.
172 * @c: UBIFS file-system description object
173 * @lnum: LEB number to write
174 * @buf: buffer to write from
175 * @offs: offset within LEB to write to
176 * @len: length to write
177 * @dtype: data type
178 *
179 * This function returns %0 on success and a negative error code on failure.
180 */
181static inline int ubifs_leb_write(const struct ubifs_info *c, int lnum,
182 const void *buf, int offs, int len, int dtype)
183{
184 int err;
185
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300186 ubifs_assert(!c->ro_media && !c->ro_mount);
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300187 if (c->ro_error)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300188 return -EROFS;
189 err = ubi_leb_write(c->ubi, lnum, buf, offs, len, dtype);
190 if (err) {
191 ubifs_err("writing %d bytes at %d:%d, error %d",
192 len, lnum, offs, err);
193 return err;
194 }
195
196 return 0;
197}
198
199/**
200 * ubifs_leb_change - atomic LEB change.
201 * @c: UBIFS file-system description object
202 * @lnum: LEB number to write
203 * @buf: buffer to write from
204 * @len: length to write
205 * @dtype: data type
206 *
207 * This function returns %0 on success and a negative error code on failure.
208 */
209static inline int ubifs_leb_change(const struct ubifs_info *c, int lnum,
210 const void *buf, int len, int dtype)
211{
212 int err;
213
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300214 ubifs_assert(!c->ro_media && !c->ro_mount);
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300215 if (c->ro_error)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300216 return -EROFS;
217 err = ubi_leb_change(c->ubi, lnum, buf, len, dtype);
218 if (err) {
219 ubifs_err("changing %d bytes in LEB %d, error %d",
220 len, lnum, err);
221 return err;
222 }
223
224 return 0;
225}
226
227/**
228 * ubifs_encode_dev - encode device node IDs.
229 * @dev: UBIFS device node information
230 * @rdev: device IDs to encode
231 *
232 * This is a helper function which encodes major/minor numbers of a device node
233 * into UBIFS device node description. We use standard Linux "new" and "huge"
234 * encodings.
235 */
236static inline int ubifs_encode_dev(union ubifs_dev_desc *dev, dev_t rdev)
237{
238 if (new_valid_dev(rdev)) {
239 dev->new = cpu_to_le32(new_encode_dev(rdev));
240 return sizeof(dev->new);
241 } else {
242 dev->huge = cpu_to_le64(huge_encode_dev(rdev));
243 return sizeof(dev->huge);
244 }
245}
246
247/**
248 * ubifs_add_dirt - add dirty space to LEB properties.
249 * @c: the UBIFS file-system description object
250 * @lnum: LEB to add dirty space for
251 * @dirty: dirty space to add
252 *
253 * This is a helper function which increased amount of dirty LEB space. Returns
254 * zero in case of success and a negative error code in case of failure.
255 */
256static inline int ubifs_add_dirt(struct ubifs_info *c, int lnum, int dirty)
257{
258 return ubifs_update_one_lp(c, lnum, LPROPS_NC, dirty, 0, 0);
259}
260
261/**
262 * ubifs_return_leb - return LEB to lprops.
263 * @c: the UBIFS file-system description object
264 * @lnum: LEB to return
265 *
266 * This helper function cleans the "taken" flag of a logical eraseblock in the
267 * lprops. Returns zero in case of success and a negative error code in case of
268 * failure.
269 */
270static inline int ubifs_return_leb(struct ubifs_info *c, int lnum)
271{
272 return ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
273 LPROPS_TAKEN, 0);
274}
275
276/**
277 * ubifs_idx_node_sz - return index node size.
278 * @c: the UBIFS file-system description object
279 * @child_cnt: number of children of this index node
280 */
281static inline int ubifs_idx_node_sz(const struct ubifs_info *c, int child_cnt)
282{
283 return UBIFS_IDX_NODE_SZ + (UBIFS_BRANCH_SZ + c->key_len) * child_cnt;
284}
285
286/**
287 * ubifs_idx_branch - return pointer to an index branch.
288 * @c: the UBIFS file-system description object
289 * @idx: index node
290 * @bnum: branch number
291 */
292static inline
293struct ubifs_branch *ubifs_idx_branch(const struct ubifs_info *c,
294 const struct ubifs_idx_node *idx,
295 int bnum)
296{
297 return (struct ubifs_branch *)((void *)idx->branches +
298 (UBIFS_BRANCH_SZ + c->key_len) * bnum);
299}
300
301/**
302 * ubifs_idx_key - return pointer to an index key.
303 * @c: the UBIFS file-system description object
304 * @idx: index node
305 */
306static inline void *ubifs_idx_key(const struct ubifs_info *c,
307 const struct ubifs_idx_node *idx)
308{
309 return (void *)((struct ubifs_branch *)idx->branches)->key;
310}
311
312/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300313 * ubifs_current_time - round current time to time granularity.
314 * @inode: inode
315 */
316static inline struct timespec ubifs_current_time(struct inode *inode)
317{
318 return (inode->i_sb->s_time_gran < NSEC_PER_SEC) ?
319 current_fs_time(inode->i_sb) : CURRENT_TIME_SEC;
320}
321
Adrian Hunter601c0bc2008-08-22 14:23:35 +0300322/**
323 * ubifs_tnc_lookup - look up a file-system node.
324 * @c: UBIFS file-system description object
325 * @key: node key to lookup
326 * @node: the node is returned here
327 *
328 * This function look up and reads node with key @key. The caller has to make
329 * sure the @node buffer is large enough to fit the node. Returns zero in case
330 * of success, %-ENOENT if the node was not found, and a negative error code in
331 * case of failure.
332 */
333static inline int ubifs_tnc_lookup(struct ubifs_info *c,
334 const union ubifs_key *key, void *node)
335{
336 return ubifs_tnc_locate(c, key, node, NULL, NULL);
337}
338
Artem Bityutskiy746103a2008-08-27 12:50:57 +0300339/**
340 * ubifs_get_lprops - get reference to LEB properties.
341 * @c: the UBIFS file-system description object
342 *
343 * This function locks lprops. Lprops have to be unlocked by
344 * 'ubifs_release_lprops()'.
345 */
346static inline void ubifs_get_lprops(struct ubifs_info *c)
347{
348 mutex_lock(&c->lp_mutex);
349}
350
351/**
352 * ubifs_release_lprops - release lprops lock.
353 * @c: the UBIFS file-system description object
354 *
355 * This function has to be called after each 'ubifs_get_lprops()' call to
356 * unlock lprops.
357 */
358static inline void ubifs_release_lprops(struct ubifs_info *c)
359{
360 ubifs_assert(mutex_is_locked(&c->lp_mutex));
361 ubifs_assert(c->lst.empty_lebs >= 0 &&
362 c->lst.empty_lebs <= c->main_lebs);
363 mutex_unlock(&c->lp_mutex);
364}
365
Artem Bityutskiye11602e2011-05-06 17:52:32 +0300366/**
367 * ubifs_next_log_lnum - switch to the next log LEB.
368 * @c: UBIFS file-system description object
369 * @lnum: current log LEB
370 *
371 * This helper function returns the log LEB number which goes next after LEB
372 * 'lnum'.
373 */
374static inline int ubifs_next_log_lnum(const struct ubifs_info *c, int lnum)
375{
376 lnum += 1;
377 if (lnum > c->log_last)
378 lnum = UBIFS_LOG_LNUM;
379
380 return lnum;
381}
382
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300383#endif /* __UBIFS_MISC_H__ */