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  */
ubifs_zn_dirty(const struct ubifs_znode * znode)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_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  */
ubifs_zn_obsolete(const struct ubifs_znode * znode)47 static 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  */
ubifs_zn_cow(const struct ubifs_znode * znode)59 static inline int ubifs_zn_cow(const struct ubifs_znode *znode)
60 {
61 	return !!test_bit(COW_ZNODE, &znode->flags);
62 }
63 
64 /**
65  * ubifs_wake_up_bgt - wake up background thread.
66  * @c: UBIFS file-system description object
67  */
ubifs_wake_up_bgt(struct ubifs_info * c)68 static 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  */
84 static inline struct ubifs_znode *
ubifs_tnc_find_child(struct ubifs_znode * znode,int start)85 ubifs_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  */
ubifs_inode(const struct inode * inode)100 static inline struct ubifs_inode *ubifs_inode(const struct inode *inode)
101 {
102 	return container_of(inode, struct ubifs_inode, vfs_inode);
103 }
104 
105 /**
106  * ubifs_compr_present - check if compressor was compiled in.
107  * @compr_type: compressor type to check
108  * @c: the UBIFS file-system description object
109  *
110  * This function returns %1 of compressor of type @compr_type is present, and
111  * %0 if not.
112  */
ubifs_compr_present(struct ubifs_info * c,int compr_type)113 static inline int ubifs_compr_present(struct ubifs_info *c, int compr_type)
114 {
115 	ubifs_assert(c, compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
116 	return !!ubifs_compressors[compr_type]->capi_name;
117 }
118 
119 /**
120  * ubifs_compr_name - get compressor name string by its type.
121  * @compr_type: compressor type
122  * @c: the UBIFS file-system description object
123  *
124  * This function returns compressor type string.
125  */
ubifs_compr_name(struct ubifs_info * c,int compr_type)126 static inline const char *ubifs_compr_name(struct ubifs_info *c, int compr_type)
127 {
128 	ubifs_assert(c, compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
129 	return ubifs_compressors[compr_type]->name;
130 }
131 
132 /**
133  * ubifs_wbuf_sync - synchronize write-buffer.
134  * @wbuf: write-buffer to synchronize
135  *
136  * This is the same as as 'ubifs_wbuf_sync_nolock()' but it does not assume
137  * that the write-buffer is already locked.
138  */
ubifs_wbuf_sync(struct ubifs_wbuf * wbuf)139 static inline int ubifs_wbuf_sync(struct ubifs_wbuf *wbuf)
140 {
141 	int err;
142 
143 	mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
144 	err = ubifs_wbuf_sync_nolock(wbuf);
145 	mutex_unlock(&wbuf->io_mutex);
146 	return err;
147 }
148 
149 /**
150  * ubifs_encode_dev - encode device node IDs.
151  * @dev: UBIFS device node information
152  * @rdev: device IDs to encode
153  *
154  * This is a helper function which encodes major/minor numbers of a device node
155  * into UBIFS device node description. We use standard Linux "new" and "huge"
156  * encodings.
157  */
ubifs_encode_dev(union ubifs_dev_desc * dev,dev_t rdev)158 static inline int ubifs_encode_dev(union ubifs_dev_desc *dev, dev_t rdev)
159 {
160 	dev->new = cpu_to_le32(new_encode_dev(rdev));
161 	return sizeof(dev->new);
162 }
163 
164 /**
165  * ubifs_add_dirt - add dirty space to LEB properties.
166  * @c: the UBIFS file-system description object
167  * @lnum: LEB to add dirty space for
168  * @dirty: dirty space to add
169  *
170  * This is a helper function which increased amount of dirty LEB space. Returns
171  * zero in case of success and a negative error code in case of failure.
172  */
ubifs_add_dirt(struct ubifs_info * c,int lnum,int dirty)173 static inline int ubifs_add_dirt(struct ubifs_info *c, int lnum, int dirty)
174 {
175 	return ubifs_update_one_lp(c, lnum, LPROPS_NC, dirty, 0, 0);
176 }
177 
178 /**
179  * ubifs_return_leb - return LEB to lprops.
180  * @c: the UBIFS file-system description object
181  * @lnum: LEB to return
182  *
183  * This helper function cleans the "taken" flag of a logical eraseblock in the
184  * lprops. Returns zero in case of success and a negative error code in case of
185  * failure.
186  */
ubifs_return_leb(struct ubifs_info * c,int lnum)187 static inline int ubifs_return_leb(struct ubifs_info *c, int lnum)
188 {
189 	return ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
190 				   LPROPS_TAKEN, 0);
191 }
192 
193 /**
194  * ubifs_idx_node_sz - return index node size.
195  * @c: the UBIFS file-system description object
196  * @child_cnt: number of children of this index node
197  */
ubifs_idx_node_sz(const struct ubifs_info * c,int child_cnt)198 static inline int ubifs_idx_node_sz(const struct ubifs_info *c, int child_cnt)
199 {
200 	return UBIFS_IDX_NODE_SZ + (UBIFS_BRANCH_SZ + c->key_len) * child_cnt;
201 }
202 
203 /**
204  * ubifs_idx_branch - return pointer to an index branch.
205  * @c: the UBIFS file-system description object
206  * @idx: index node
207  * @bnum: branch number
208  */
209 static inline
ubifs_idx_branch(const struct ubifs_info * c,const struct ubifs_idx_node * idx,int bnum)210 struct ubifs_branch *ubifs_idx_branch(const struct ubifs_info *c,
211 				      const struct ubifs_idx_node *idx,
212 				      int bnum)
213 {
214 	return (struct ubifs_branch *)((void *)idx->branches +
215 				       (UBIFS_BRANCH_SZ + c->key_len) * bnum);
216 }
217 
218 /**
219  * ubifs_idx_key - return pointer to an index key.
220  * @c: the UBIFS file-system description object
221  * @idx: index node
222  */
ubifs_idx_key(const struct ubifs_info * c,const struct ubifs_idx_node * idx)223 static inline void *ubifs_idx_key(const struct ubifs_info *c,
224 				  const struct ubifs_idx_node *idx)
225 {
226 	return (void *)((struct ubifs_branch *)idx->branches)->key;
227 }
228 
229 /**
230  * ubifs_tnc_lookup - look up a file-system node.
231  * @c: UBIFS file-system description object
232  * @key: node key to lookup
233  * @node: the node is returned here
234  *
235  * This function look up and reads node with key @key. The caller has to make
236  * sure the @node buffer is large enough to fit the node. Returns zero in case
237  * of success, %-ENOENT if the node was not found, and a negative error code in
238  * case of failure.
239  */
ubifs_tnc_lookup(struct ubifs_info * c,const union ubifs_key * key,void * node)240 static inline int ubifs_tnc_lookup(struct ubifs_info *c,
241 				   const union ubifs_key *key, void *node)
242 {
243 	return ubifs_tnc_locate(c, key, node, NULL, NULL);
244 }
245 
246 /**
247  * ubifs_get_lprops - get reference to LEB properties.
248  * @c: the UBIFS file-system description object
249  *
250  * This function locks lprops. Lprops have to be unlocked by
251  * 'ubifs_release_lprops()'.
252  */
ubifs_get_lprops(struct ubifs_info * c)253 static inline void ubifs_get_lprops(struct ubifs_info *c)
254 {
255 	mutex_lock(&c->lp_mutex);
256 }
257 
258 /**
259  * ubifs_release_lprops - release lprops lock.
260  * @c: the UBIFS file-system description object
261  *
262  * This function has to be called after each 'ubifs_get_lprops()' call to
263  * unlock lprops.
264  */
ubifs_release_lprops(struct ubifs_info * c)265 static inline void ubifs_release_lprops(struct ubifs_info *c)
266 {
267 	ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
268 	ubifs_assert(c, c->lst.empty_lebs >= 0 &&
269 		     c->lst.empty_lebs <= c->main_lebs);
270 	mutex_unlock(&c->lp_mutex);
271 }
272 
273 /**
274  * ubifs_next_log_lnum - switch to the next log LEB.
275  * @c: UBIFS file-system description object
276  * @lnum: current log LEB
277  *
278  * This helper function returns the log LEB number which goes next after LEB
279  * 'lnum'.
280  */
ubifs_next_log_lnum(const struct ubifs_info * c,int lnum)281 static inline int ubifs_next_log_lnum(const struct ubifs_info *c, int lnum)
282 {
283 	lnum += 1;
284 	if (lnum > c->log_last)
285 		lnum = UBIFS_LOG_LNUM;
286 
287 	return lnum;
288 }
289 
290 const char *ubifs_assert_action_name(struct ubifs_info *c);
291 
292 #endif /* __UBIFS_MISC_H__ */
293