Line data Source code
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 : */
24 : static inline int ubifs_zn_dirty(const struct ubifs_znode *znode)
25 : {
26 2289276752 : 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 : */
35 : static inline int ubifs_zn_obsolete(const struct ubifs_znode *znode)
36 : {
37 5456864 : 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 : */
47 : static inline int ubifs_zn_cow(const struct ubifs_znode *znode)
48 : {
49 83917976 : return !!test_bit(COW_ZNODE, &znode->flags);
50 : }
51 :
52 : /**
53 : * ubifs_tnc_find_child - find next child in znode.
54 : * @znode: znode to search at
55 : * @start: the zbranch index to start at
56 : *
57 : * This helper function looks for znode child starting at index @start. Returns
58 : * the child or %NULL if no children were found.
59 : */
60 : static inline struct ubifs_znode *
61 : ubifs_tnc_find_child(struct ubifs_znode *znode, int start)
62 : {
63 678955413 : while (start < znode->child_cnt) {
64 567170501 : if (znode->zbranch[start].znode)
65 : return znode->zbranch[start].znode;
66 9444 : start += 1;
67 : }
68 :
69 : return NULL;
70 : }
71 :
72 : /**
73 : * ubifs_inode - get UBIFS inode information by VFS 'struct inode' object.
74 : * @inode: the VFS 'struct inode' pointer
75 : */
76 : static inline struct ubifs_inode *ubifs_inode(const struct inode *inode)
77 : {
78 30 : return container_of(inode, struct ubifs_inode, vfs_inode);
79 : }
80 :
81 : /**
82 : * ubifs_wbuf_sync - synchronize write-buffer.
83 : * @wbuf: write-buffer to synchronize
84 : *
85 : * This is the same as 'ubifs_wbuf_sync_nolock()' but it does not assume
86 : * that the write-buffer is already locked.
87 : */
88 10383 : static inline int ubifs_wbuf_sync(struct ubifs_wbuf *wbuf)
89 : {
90 : int err;
91 :
92 10383 : mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
93 10383 : err = ubifs_wbuf_sync_nolock(wbuf);
94 10383 : mutex_unlock(&wbuf->io_mutex);
95 10383 : return err;
96 : }
97 :
98 : /**
99 : * ubifs_add_dirt - add dirty space to LEB properties.
100 : * @c: the UBIFS file-system description object
101 : * @lnum: LEB to add dirty space for
102 : * @dirty: dirty space to add
103 : *
104 : * This is a helper function which increased amount of dirty LEB space. Returns
105 : * zero in case of success and a negative error code in case of failure.
106 : */
107 : static inline int ubifs_add_dirt(struct ubifs_info *c, int lnum, int dirty)
108 : {
109 5824025 : return ubifs_update_one_lp(c, lnum, LPROPS_NC, dirty, 0, 0);
110 : }
111 :
112 : /**
113 : * ubifs_return_leb - return LEB to lprops.
114 : * @c: the UBIFS file-system description object
115 : * @lnum: LEB to return
116 : *
117 : * This helper function cleans the "taken" flag of a logical eraseblock in the
118 : * lprops. Returns zero in case of success and a negative error code in case of
119 : * failure.
120 : */
121 : static inline int ubifs_return_leb(struct ubifs_info *c, int lnum)
122 : {
123 1443 : return ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
124 : LPROPS_TAKEN, 0);
125 : }
126 :
127 : /**
128 : * ubifs_idx_node_sz - return index node size.
129 : * @c: the UBIFS file-system description object
130 : * @child_cnt: number of children of this index node
131 : */
132 : static inline int ubifs_idx_node_sz(const struct ubifs_info *c, int child_cnt)
133 : {
134 582954138 : return UBIFS_IDX_NODE_SZ + (UBIFS_BRANCH_SZ + c->key_len + c->hash_len)
135 : * child_cnt;
136 : }
137 :
138 : /**
139 : * ubifs_idx_branch - return pointer to an index branch.
140 : * @c: the UBIFS file-system description object
141 : * @idx: index node
142 : * @bnum: branch number
143 : */
144 : static inline
145 : struct ubifs_branch *ubifs_idx_branch(const struct ubifs_info *c,
146 : const struct ubifs_idx_node *idx,
147 : int bnum)
148 : {
149 6364448610 : return (struct ubifs_branch *)((void *)idx->branches +
150 3182224305 : (UBIFS_BRANCH_SZ + c->key_len + c->hash_len) * bnum);
151 : }
152 :
153 : /**
154 : * ubifs_idx_key - return pointer to an index key.
155 : * @c: the UBIFS file-system description object
156 : * @idx: index node
157 : */
158 : static inline void *ubifs_idx_key(__unused const struct ubifs_info *c,
159 : const struct ubifs_idx_node *idx)
160 : {
161 : return (void *)((struct ubifs_branch *)idx->branches)->key;
162 : }
163 :
164 : /**
165 : * ubifs_tnc_lookup - look up a file-system node.
166 : * @c: UBIFS file-system description object
167 : * @key: node key to lookup
168 : * @node: the node is returned here
169 : *
170 : * This function look up and reads node with key @key. The caller has to make
171 : * sure the @node buffer is large enough to fit the node. Returns zero in case
172 : * of success, %-ENOENT if the node was not found, and a negative error code in
173 : * case of failure.
174 : */
175 : static inline int ubifs_tnc_lookup(struct ubifs_info *c,
176 : const union ubifs_key *key, void *node)
177 : {
178 82634 : return ubifs_tnc_locate(c, key, node, NULL, NULL);
179 : }
180 :
181 : /**
182 : * ubifs_get_lprops - get reference to LEB properties.
183 : * @c: the UBIFS file-system description object
184 : *
185 : * This function locks lprops. Lprops have to be unlocked by
186 : * 'ubifs_release_lprops()'.
187 : */
188 : static inline void ubifs_get_lprops(struct ubifs_info *c)
189 : {
190 5793825 : mutex_lock(&c->lp_mutex);
191 : }
192 :
193 : /**
194 : * ubifs_release_lprops - release lprops lock.
195 : * @c: the UBIFS file-system description object
196 : *
197 : * This function has to be called after each 'ubifs_get_lprops()' call to
198 : * unlock lprops.
199 : */
200 5793825 : static inline void ubifs_release_lprops(struct ubifs_info *c)
201 : {
202 5793825 : ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
203 5793825 : ubifs_assert(c, c->lst.empty_lebs >= 0 &&
204 : c->lst.empty_lebs <= c->main_lebs);
205 5793825 : mutex_unlock(&c->lp_mutex);
206 5793825 : }
207 :
208 : /**
209 : * ubifs_next_log_lnum - switch to the next log LEB.
210 : * @c: UBIFS file-system description object
211 : * @lnum: current log LEB
212 : *
213 : * This helper function returns the log LEB number which goes next after LEB
214 : * 'lnum'.
215 : */
216 : static inline int ubifs_next_log_lnum(const struct ubifs_info *c, int lnum)
217 : {
218 10284 : lnum += 1;
219 10284 : if (lnum > c->log_last)
220 2088 : lnum = UBIFS_LOG_LNUM;
221 :
222 : return lnum;
223 : }
224 :
225 : #endif /* __UBIFS_MISC_H__ */
|