xref: /openbmc/u-boot/fs/ubifs/misc.h (revision 1d2c0506)
1 /*
2  * This file is part of UBIFS.
3  *
4  * Copyright (C) 2006-2008 Nokia Corporation
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  *
8  * Authors: Artem Bityutskiy (Битюцкий Артём)
9  *          Adrian Hunter
10  */
11 
12 /*
13  * This file contains miscellaneous helper functions.
14  */
15 
16 #ifndef __UBIFS_MISC_H__
17 #define __UBIFS_MISC_H__
18 
19 /**
20  * ubifs_zn_dirty - check if znode is dirty.
21  * @znode: znode to check
22  *
23  * This helper function returns %1 if @znode is dirty and %0 otherwise.
24  */
25 static inline int ubifs_zn_dirty(const struct ubifs_znode *znode)
26 {
27 	return !!test_bit(DIRTY_ZNODE, &znode->flags);
28 }
29 
30 /**
31  * ubifs_zn_obsolete - check if znode is obsolete.
32  * @znode: znode to check
33  *
34  * This helper function returns %1 if @znode is obsolete and %0 otherwise.
35  */
36 static inline int ubifs_zn_obsolete(const struct ubifs_znode *znode)
37 {
38 	return !!test_bit(OBSOLETE_ZNODE, &znode->flags);
39 }
40 
41 /**
42  * ubifs_zn_cow - check if znode has to be copied on write.
43  * @znode: znode to check
44  *
45  * This helper function returns %1 if @znode is has COW flag set and %0
46  * otherwise.
47  */
48 static inline int ubifs_zn_cow(const struct ubifs_znode *znode)
49 {
50 	return !!test_bit(COW_ZNODE, &znode->flags);
51 }
52 
53 /**
54  * ubifs_wake_up_bgt - wake up background thread.
55  * @c: UBIFS file-system description object
56  */
57 static inline void ubifs_wake_up_bgt(struct ubifs_info *c)
58 {
59 	if (c->bgt && !c->need_bgt) {
60 		c->need_bgt = 1;
61 		wake_up_process(c->bgt);
62 	}
63 }
64 
65 /**
66  * ubifs_tnc_find_child - find next child in znode.
67  * @znode: znode to search at
68  * @start: the zbranch index to start at
69  *
70  * This helper function looks for znode child starting at index @start. Returns
71  * the child or %NULL if no children were found.
72  */
73 static inline struct ubifs_znode *
74 ubifs_tnc_find_child(struct ubifs_znode *znode, int start)
75 {
76 	while (start < znode->child_cnt) {
77 		if (znode->zbranch[start].znode)
78 			return znode->zbranch[start].znode;
79 		start += 1;
80 	}
81 
82 	return NULL;
83 }
84 
85 /**
86  * ubifs_inode - get UBIFS inode information by VFS 'struct inode' object.
87  * @inode: the VFS 'struct inode' pointer
88  */
89 static inline struct ubifs_inode *ubifs_inode(const struct inode *inode)
90 {
91 	return container_of(inode, struct ubifs_inode, vfs_inode);
92 }
93 
94 /**
95  * ubifs_compr_present - check if compressor was compiled in.
96  * @compr_type: compressor type to check
97  *
98  * This function returns %1 of compressor of type @compr_type is present, and
99  * %0 if not.
100  */
101 static inline int ubifs_compr_present(int compr_type)
102 {
103 	ubifs_assert(compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
104 	return !!ubifs_compressors[compr_type]->capi_name;
105 }
106 
107 /**
108  * ubifs_compr_name - get compressor name string by its type.
109  * @compr_type: compressor type
110  *
111  * This function returns compressor type string.
112  */
113 static inline const char *ubifs_compr_name(int compr_type)
114 {
115 	ubifs_assert(compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
116 	return ubifs_compressors[compr_type]->name;
117 }
118 
119 /**
120  * ubifs_wbuf_sync - synchronize write-buffer.
121  * @wbuf: write-buffer to synchronize
122  *
123  * This is the same as as 'ubifs_wbuf_sync_nolock()' but it does not assume
124  * that the write-buffer is already locked.
125  */
126 static inline int ubifs_wbuf_sync(struct ubifs_wbuf *wbuf)
127 {
128 	int err;
129 
130 	mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
131 	err = ubifs_wbuf_sync_nolock(wbuf);
132 	mutex_unlock(&wbuf->io_mutex);
133 	return err;
134 }
135 
136 #ifndef __UBOOT__
137 /**
138  * ubifs_encode_dev - encode device node IDs.
139  * @dev: UBIFS device node information
140  * @rdev: device IDs to encode
141  *
142  * This is a helper function which encodes major/minor numbers of a device node
143  * into UBIFS device node description. We use standard Linux "new" and "huge"
144  * encodings.
145  */
146 static inline int ubifs_encode_dev(union ubifs_dev_desc *dev, dev_t rdev)
147 {
148 	if (new_valid_dev(rdev)) {
149 		dev->new = cpu_to_le32(new_encode_dev(rdev));
150 		return sizeof(dev->new);
151 	} else {
152 		dev->huge = cpu_to_le64(huge_encode_dev(rdev));
153 		return sizeof(dev->huge);
154 	}
155 }
156 #endif
157 
158 /**
159  * ubifs_add_dirt - add dirty space to LEB properties.
160  * @c: the UBIFS file-system description object
161  * @lnum: LEB to add dirty space for
162  * @dirty: dirty space to add
163  *
164  * This is a helper function which increased amount of dirty LEB space. Returns
165  * zero in case of success and a negative error code in case of failure.
166  */
167 static inline int ubifs_add_dirt(struct ubifs_info *c, int lnum, int dirty)
168 {
169 	return ubifs_update_one_lp(c, lnum, LPROPS_NC, dirty, 0, 0);
170 }
171 
172 /**
173  * ubifs_return_leb - return LEB to lprops.
174  * @c: the UBIFS file-system description object
175  * @lnum: LEB to return
176  *
177  * This helper function cleans the "taken" flag of a logical eraseblock in the
178  * lprops. Returns zero in case of success and a negative error code in case of
179  * failure.
180  */
181 static inline int ubifs_return_leb(struct ubifs_info *c, int lnum)
182 {
183 	return ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
184 				   LPROPS_TAKEN, 0);
185 }
186 
187 /**
188  * ubifs_idx_node_sz - return index node size.
189  * @c: the UBIFS file-system description object
190  * @child_cnt: number of children of this index node
191  */
192 static inline int ubifs_idx_node_sz(const struct ubifs_info *c, int child_cnt)
193 {
194 	return UBIFS_IDX_NODE_SZ + (UBIFS_BRANCH_SZ + c->key_len) * child_cnt;
195 }
196 
197 /**
198  * ubifs_idx_branch - return pointer to an index branch.
199  * @c: the UBIFS file-system description object
200  * @idx: index node
201  * @bnum: branch number
202  */
203 static inline
204 struct ubifs_branch *ubifs_idx_branch(const struct ubifs_info *c,
205 				      const struct ubifs_idx_node *idx,
206 				      int bnum)
207 {
208 	return (struct ubifs_branch *)((void *)idx->branches +
209 				       (UBIFS_BRANCH_SZ + c->key_len) * bnum);
210 }
211 
212 /**
213  * ubifs_idx_key - return pointer to an index key.
214  * @c: the UBIFS file-system description object
215  * @idx: index node
216  */
217 static inline void *ubifs_idx_key(const struct ubifs_info *c,
218 				  const struct ubifs_idx_node *idx)
219 {
220 #ifndef __UBOOT__
221 	return (void *)((struct ubifs_branch *)idx->branches)->key;
222 #else
223 	struct ubifs_branch *tmp;
224 
225 	tmp = (struct ubifs_branch *)idx->branches;
226 	return (void *)tmp->key;
227 #endif
228 }
229 
230 /**
231  * ubifs_current_time - round current time to time granularity.
232  * @inode: inode
233  */
234 static inline struct timespec ubifs_current_time(struct inode *inode)
235 {
236 	return (inode->i_sb->s_time_gran < NSEC_PER_SEC) ?
237 		current_fs_time(inode->i_sb) : CURRENT_TIME_SEC;
238 }
239 
240 /**
241  * ubifs_tnc_lookup - look up a file-system node.
242  * @c: UBIFS file-system description object
243  * @key: node key to lookup
244  * @node: the node is returned here
245  *
246  * This function look up and reads node with key @key. The caller has to make
247  * sure the @node buffer is large enough to fit the node. Returns zero in case
248  * of success, %-ENOENT if the node was not found, and a negative error code in
249  * case of failure.
250  */
251 static inline int ubifs_tnc_lookup(struct ubifs_info *c,
252 				   const union ubifs_key *key, void *node)
253 {
254 	return ubifs_tnc_locate(c, key, node, NULL, NULL);
255 }
256 
257 /**
258  * ubifs_get_lprops - get reference to LEB properties.
259  * @c: the UBIFS file-system description object
260  *
261  * This function locks lprops. Lprops have to be unlocked by
262  * 'ubifs_release_lprops()'.
263  */
264 static inline void ubifs_get_lprops(struct ubifs_info *c)
265 {
266 	mutex_lock(&c->lp_mutex);
267 }
268 
269 /**
270  * ubifs_release_lprops - release lprops lock.
271  * @c: the UBIFS file-system description object
272  *
273  * This function has to be called after each 'ubifs_get_lprops()' call to
274  * unlock lprops.
275  */
276 static inline void ubifs_release_lprops(struct ubifs_info *c)
277 {
278 	ubifs_assert(mutex_is_locked(&c->lp_mutex));
279 	ubifs_assert(c->lst.empty_lebs >= 0 &&
280 		     c->lst.empty_lebs <= c->main_lebs);
281 	mutex_unlock(&c->lp_mutex);
282 }
283 
284 /**
285  * ubifs_next_log_lnum - switch to the next log LEB.
286  * @c: UBIFS file-system description object
287  * @lnum: current log LEB
288  *
289  * This helper function returns the log LEB number which goes next after LEB
290  * 'lnum'.
291  */
292 static inline int ubifs_next_log_lnum(const struct ubifs_info *c, int lnum)
293 {
294 	lnum += 1;
295 	if (lnum > c->log_last)
296 		lnum = UBIFS_LOG_LNUM;
297 
298 	return lnum;
299 }
300 
301 #endif /* __UBIFS_MISC_H__ */
302