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