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