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