Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * This file is part of UBIFS.
0004  *
0005  * Copyright (C) 2006-2008 Nokia Corporation
0006  *
0007  * Authors: Artem Bityutskiy (Битюцкий Артём)
0008  *          Adrian Hunter
0009  */
0010 
0011 /*
0012  * This file contains miscellaneous helper functions.
0013  */
0014 
0015 #ifndef __UBIFS_MISC_H__
0016 #define __UBIFS_MISC_H__
0017 
0018 /**
0019  * ubifs_zn_dirty - check if znode is dirty.
0020  * @znode: znode to check
0021  *
0022  * This helper function returns %1 if @znode is dirty and %0 otherwise.
0023  */
0024 static inline int ubifs_zn_dirty(const struct ubifs_znode *znode)
0025 {
0026     return !!test_bit(DIRTY_ZNODE, &znode->flags);
0027 }
0028 
0029 /**
0030  * ubifs_zn_obsolete - check if znode is obsolete.
0031  * @znode: znode to check
0032  *
0033  * This helper function returns %1 if @znode is obsolete and %0 otherwise.
0034  */
0035 static inline int ubifs_zn_obsolete(const struct ubifs_znode *znode)
0036 {
0037     return !!test_bit(OBSOLETE_ZNODE, &znode->flags);
0038 }
0039 
0040 /**
0041  * ubifs_zn_cow - check if znode has to be copied on write.
0042  * @znode: znode to check
0043  *
0044  * This helper function returns %1 if @znode is has COW flag set and %0
0045  * otherwise.
0046  */
0047 static inline int ubifs_zn_cow(const struct ubifs_znode *znode)
0048 {
0049     return !!test_bit(COW_ZNODE, &znode->flags);
0050 }
0051 
0052 /**
0053  * ubifs_wake_up_bgt - wake up background thread.
0054  * @c: UBIFS file-system description object
0055  */
0056 static inline void ubifs_wake_up_bgt(struct ubifs_info *c)
0057 {
0058     if (c->bgt && !c->need_bgt) {
0059         c->need_bgt = 1;
0060         wake_up_process(c->bgt);
0061     }
0062 }
0063 
0064 /**
0065  * ubifs_tnc_find_child - find next child in znode.
0066  * @znode: znode to search at
0067  * @start: the zbranch index to start at
0068  *
0069  * This helper function looks for znode child starting at index @start. Returns
0070  * the child or %NULL if no children were found.
0071  */
0072 static inline struct ubifs_znode *
0073 ubifs_tnc_find_child(struct ubifs_znode *znode, int start)
0074 {
0075     while (start < znode->child_cnt) {
0076         if (znode->zbranch[start].znode)
0077             return znode->zbranch[start].znode;
0078         start += 1;
0079     }
0080 
0081     return NULL;
0082 }
0083 
0084 /**
0085  * ubifs_inode - get UBIFS inode information by VFS 'struct inode' object.
0086  * @inode: the VFS 'struct inode' pointer
0087  */
0088 static inline struct ubifs_inode *ubifs_inode(const struct inode *inode)
0089 {
0090     return container_of(inode, struct ubifs_inode, vfs_inode);
0091 }
0092 
0093 /**
0094  * ubifs_compr_present - check if compressor was compiled in.
0095  * @compr_type: compressor type to check
0096  * @c: the UBIFS file-system description object
0097  *
0098  * This function returns %1 of compressor of type @compr_type is present, and
0099  * %0 if not.
0100  */
0101 static inline int ubifs_compr_present(struct ubifs_info *c, int compr_type)
0102 {
0103     ubifs_assert(c, compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
0104     return !!ubifs_compressors[compr_type]->capi_name;
0105 }
0106 
0107 /**
0108  * ubifs_compr_name - get compressor name string by its type.
0109  * @compr_type: compressor type
0110  * @c: the UBIFS file-system description object
0111  *
0112  * This function returns compressor type string.
0113  */
0114 static inline const char *ubifs_compr_name(struct ubifs_info *c, int compr_type)
0115 {
0116     ubifs_assert(c, compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
0117     return ubifs_compressors[compr_type]->name;
0118 }
0119 
0120 /**
0121  * ubifs_wbuf_sync - synchronize write-buffer.
0122  * @wbuf: write-buffer to synchronize
0123  *
0124  * This is the same as 'ubifs_wbuf_sync_nolock()' but it does not assume
0125  * that the write-buffer is already locked.
0126  */
0127 static inline int ubifs_wbuf_sync(struct ubifs_wbuf *wbuf)
0128 {
0129     int err;
0130 
0131     mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
0132     err = ubifs_wbuf_sync_nolock(wbuf);
0133     mutex_unlock(&wbuf->io_mutex);
0134     return err;
0135 }
0136 
0137 /**
0138  * ubifs_encode_dev - encode device node IDs.
0139  * @dev: UBIFS device node information
0140  * @rdev: device IDs to encode
0141  *
0142  * This is a helper function which encodes major/minor numbers of a device node
0143  * into UBIFS device node description. We use standard Linux "new" and "huge"
0144  * encodings.
0145  */
0146 static inline int ubifs_encode_dev(union ubifs_dev_desc *dev, dev_t rdev)
0147 {
0148     dev->new = cpu_to_le32(new_encode_dev(rdev));
0149     return sizeof(dev->new);
0150 }
0151 
0152 /**
0153  * ubifs_add_dirt - add dirty space to LEB properties.
0154  * @c: the UBIFS file-system description object
0155  * @lnum: LEB to add dirty space for
0156  * @dirty: dirty space to add
0157  *
0158  * This is a helper function which increased amount of dirty LEB space. Returns
0159  * zero in case of success and a negative error code in case of failure.
0160  */
0161 static inline int ubifs_add_dirt(struct ubifs_info *c, int lnum, int dirty)
0162 {
0163     return ubifs_update_one_lp(c, lnum, LPROPS_NC, dirty, 0, 0);
0164 }
0165 
0166 /**
0167  * ubifs_return_leb - return LEB to lprops.
0168  * @c: the UBIFS file-system description object
0169  * @lnum: LEB to return
0170  *
0171  * This helper function cleans the "taken" flag of a logical eraseblock in the
0172  * lprops. Returns zero in case of success and a negative error code in case of
0173  * failure.
0174  */
0175 static inline int ubifs_return_leb(struct ubifs_info *c, int lnum)
0176 {
0177     return ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
0178                    LPROPS_TAKEN, 0);
0179 }
0180 
0181 /**
0182  * ubifs_idx_node_sz - return index node size.
0183  * @c: the UBIFS file-system description object
0184  * @child_cnt: number of children of this index node
0185  */
0186 static inline int ubifs_idx_node_sz(const struct ubifs_info *c, int child_cnt)
0187 {
0188     return UBIFS_IDX_NODE_SZ + (UBIFS_BRANCH_SZ + c->key_len + c->hash_len)
0189                    * child_cnt;
0190 }
0191 
0192 /**
0193  * ubifs_idx_branch - return pointer to an index branch.
0194  * @c: the UBIFS file-system description object
0195  * @idx: index node
0196  * @bnum: branch number
0197  */
0198 static inline
0199 struct ubifs_branch *ubifs_idx_branch(const struct ubifs_info *c,
0200                       const struct ubifs_idx_node *idx,
0201                       int bnum)
0202 {
0203     return (struct ubifs_branch *)((void *)idx->branches +
0204             (UBIFS_BRANCH_SZ + c->key_len + c->hash_len) * bnum);
0205 }
0206 
0207 /**
0208  * ubifs_idx_key - return pointer to an index key.
0209  * @c: the UBIFS file-system description object
0210  * @idx: index node
0211  */
0212 static inline void *ubifs_idx_key(const struct ubifs_info *c,
0213                   const struct ubifs_idx_node *idx)
0214 {
0215     return (void *)((struct ubifs_branch *)idx->branches)->key;
0216 }
0217 
0218 /**
0219  * ubifs_tnc_lookup - look up a file-system node.
0220  * @c: UBIFS file-system description object
0221  * @key: node key to lookup
0222  * @node: the node is returned here
0223  *
0224  * This function look up and reads node with key @key. The caller has to make
0225  * sure the @node buffer is large enough to fit the node. Returns zero in case
0226  * of success, %-ENOENT if the node was not found, and a negative error code in
0227  * case of failure.
0228  */
0229 static inline int ubifs_tnc_lookup(struct ubifs_info *c,
0230                    const union ubifs_key *key, void *node)
0231 {
0232     return ubifs_tnc_locate(c, key, node, NULL, NULL);
0233 }
0234 
0235 /**
0236  * ubifs_get_lprops - get reference to LEB properties.
0237  * @c: the UBIFS file-system description object
0238  *
0239  * This function locks lprops. Lprops have to be unlocked by
0240  * 'ubifs_release_lprops()'.
0241  */
0242 static inline void ubifs_get_lprops(struct ubifs_info *c)
0243 {
0244     mutex_lock(&c->lp_mutex);
0245 }
0246 
0247 /**
0248  * ubifs_release_lprops - release lprops lock.
0249  * @c: the UBIFS file-system description object
0250  *
0251  * This function has to be called after each 'ubifs_get_lprops()' call to
0252  * unlock lprops.
0253  */
0254 static inline void ubifs_release_lprops(struct ubifs_info *c)
0255 {
0256     ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
0257     ubifs_assert(c, c->lst.empty_lebs >= 0 &&
0258              c->lst.empty_lebs <= c->main_lebs);
0259     mutex_unlock(&c->lp_mutex);
0260 }
0261 
0262 /**
0263  * ubifs_next_log_lnum - switch to the next log LEB.
0264  * @c: UBIFS file-system description object
0265  * @lnum: current log LEB
0266  *
0267  * This helper function returns the log LEB number which goes next after LEB
0268  * 'lnum'.
0269  */
0270 static inline int ubifs_next_log_lnum(const struct ubifs_info *c, int lnum)
0271 {
0272     lnum += 1;
0273     if (lnum > c->log_last)
0274         lnum = UBIFS_LOG_LNUM;
0275 
0276     return lnum;
0277 }
0278 
0279 static inline int ubifs_xattr_max_cnt(struct ubifs_info *c)
0280 {
0281     int max_xattrs = (c->leb_size / 2) / UBIFS_INO_NODE_SZ;
0282 
0283     ubifs_assert(c, max_xattrs < c->max_orphans);
0284     return max_xattrs;
0285 }
0286 
0287 const char *ubifs_assert_action_name(struct ubifs_info *c);
0288 
0289 #endif /* __UBIFS_MISC_H__ */