xref: /openbmc/linux/fs/ubifs/misc.h (revision fcf44196)
12b27bdccSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
21e51764aSArtem Bityutskiy /*
31e51764aSArtem Bityutskiy  * This file is part of UBIFS.
41e51764aSArtem Bityutskiy  *
51e51764aSArtem Bityutskiy  * Copyright (C) 2006-2008 Nokia Corporation
61e51764aSArtem Bityutskiy  *
71e51764aSArtem Bityutskiy  * Authors: Artem Bityutskiy (Битюцкий Артём)
81e51764aSArtem Bityutskiy  *          Adrian Hunter
91e51764aSArtem Bityutskiy  */
101e51764aSArtem Bityutskiy 
111e51764aSArtem Bityutskiy /*
121e51764aSArtem Bityutskiy  * This file contains miscellaneous helper functions.
131e51764aSArtem Bityutskiy  */
141e51764aSArtem Bityutskiy 
151e51764aSArtem Bityutskiy #ifndef __UBIFS_MISC_H__
161e51764aSArtem Bityutskiy #define __UBIFS_MISC_H__
171e51764aSArtem Bityutskiy 
181e51764aSArtem Bityutskiy /**
191e51764aSArtem Bityutskiy  * ubifs_zn_dirty - check if znode is dirty.
201e51764aSArtem Bityutskiy  * @znode: znode to check
211e51764aSArtem Bityutskiy  *
221e51764aSArtem Bityutskiy  * This helper function returns %1 if @znode is dirty and %0 otherwise.
231e51764aSArtem Bityutskiy  */
ubifs_zn_dirty(const struct ubifs_znode * znode)241e51764aSArtem Bityutskiy static inline int ubifs_zn_dirty(const struct ubifs_znode *znode)
251e51764aSArtem Bityutskiy {
261e51764aSArtem Bityutskiy 	return !!test_bit(DIRTY_ZNODE, &znode->flags);
271e51764aSArtem Bityutskiy }
281e51764aSArtem Bityutskiy 
291e51764aSArtem Bityutskiy /**
30f42eed7cSArtem Bityutskiy  * ubifs_zn_obsolete - check if znode is obsolete.
31f42eed7cSArtem Bityutskiy  * @znode: znode to check
32f42eed7cSArtem Bityutskiy  *
33f42eed7cSArtem Bityutskiy  * This helper function returns %1 if @znode is obsolete and %0 otherwise.
34f42eed7cSArtem Bityutskiy  */
ubifs_zn_obsolete(const struct ubifs_znode * znode)35f42eed7cSArtem Bityutskiy static inline int ubifs_zn_obsolete(const struct ubifs_znode *znode)
36f42eed7cSArtem Bityutskiy {
37f42eed7cSArtem Bityutskiy 	return !!test_bit(OBSOLETE_ZNODE, &znode->flags);
38f42eed7cSArtem Bityutskiy }
39f42eed7cSArtem Bityutskiy 
40f42eed7cSArtem Bityutskiy /**
41f42eed7cSArtem Bityutskiy  * ubifs_zn_cow - check if znode has to be copied on write.
42f42eed7cSArtem Bityutskiy  * @znode: znode to check
43f42eed7cSArtem Bityutskiy  *
44f42eed7cSArtem Bityutskiy  * This helper function returns %1 if @znode is has COW flag set and %0
45f42eed7cSArtem Bityutskiy  * otherwise.
46f42eed7cSArtem Bityutskiy  */
ubifs_zn_cow(const struct ubifs_znode * znode)47f42eed7cSArtem Bityutskiy static inline int ubifs_zn_cow(const struct ubifs_znode *znode)
48f42eed7cSArtem Bityutskiy {
49f42eed7cSArtem Bityutskiy 	return !!test_bit(COW_ZNODE, &znode->flags);
50f42eed7cSArtem Bityutskiy }
51f42eed7cSArtem Bityutskiy 
52f42eed7cSArtem Bityutskiy /**
531e51764aSArtem Bityutskiy  * ubifs_wake_up_bgt - wake up background thread.
541e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
551e51764aSArtem Bityutskiy  */
ubifs_wake_up_bgt(struct ubifs_info * c)561e51764aSArtem Bityutskiy static inline void ubifs_wake_up_bgt(struct ubifs_info *c)
571e51764aSArtem Bityutskiy {
581e51764aSArtem Bityutskiy 	if (c->bgt && !c->need_bgt) {
591e51764aSArtem Bityutskiy 		c->need_bgt = 1;
601e51764aSArtem Bityutskiy 		wake_up_process(c->bgt);
611e51764aSArtem Bityutskiy 	}
621e51764aSArtem Bityutskiy }
631e51764aSArtem Bityutskiy 
641e51764aSArtem Bityutskiy /**
651e51764aSArtem Bityutskiy  * ubifs_tnc_find_child - find next child in znode.
661e51764aSArtem Bityutskiy  * @znode: znode to search at
671e51764aSArtem Bityutskiy  * @start: the zbranch index to start at
681e51764aSArtem Bityutskiy  *
691e51764aSArtem Bityutskiy  * This helper function looks for znode child starting at index @start. Returns
701e51764aSArtem Bityutskiy  * the child or %NULL if no children were found.
711e51764aSArtem Bityutskiy  */
721e51764aSArtem Bityutskiy static inline struct ubifs_znode *
ubifs_tnc_find_child(struct ubifs_znode * znode,int start)731e51764aSArtem Bityutskiy ubifs_tnc_find_child(struct ubifs_znode *znode, int start)
741e51764aSArtem Bityutskiy {
751e51764aSArtem Bityutskiy 	while (start < znode->child_cnt) {
761e51764aSArtem Bityutskiy 		if (znode->zbranch[start].znode)
771e51764aSArtem Bityutskiy 			return znode->zbranch[start].znode;
781e51764aSArtem Bityutskiy 		start += 1;
791e51764aSArtem Bityutskiy 	}
801e51764aSArtem Bityutskiy 
811e51764aSArtem Bityutskiy 	return NULL;
821e51764aSArtem Bityutskiy }
831e51764aSArtem Bityutskiy 
841e51764aSArtem Bityutskiy /**
851e51764aSArtem Bityutskiy  * ubifs_inode - get UBIFS inode information by VFS 'struct inode' object.
861e51764aSArtem Bityutskiy  * @inode: the VFS 'struct inode' pointer
871e51764aSArtem Bityutskiy  */
ubifs_inode(const struct inode * inode)881e51764aSArtem Bityutskiy static inline struct ubifs_inode *ubifs_inode(const struct inode *inode)
891e51764aSArtem Bityutskiy {
901e51764aSArtem Bityutskiy 	return container_of(inode, struct ubifs_inode, vfs_inode);
911e51764aSArtem Bityutskiy }
921e51764aSArtem Bityutskiy 
931e51764aSArtem Bityutskiy /**
941e51764aSArtem Bityutskiy  * ubifs_compr_present - check if compressor was compiled in.
951e51764aSArtem Bityutskiy  * @compr_type: compressor type to check
966eb61d58SRichard Weinberger  * @c: the UBIFS file-system description object
971e51764aSArtem Bityutskiy  *
981e51764aSArtem Bityutskiy  * This function returns %1 of compressor of type @compr_type is present, and
991e51764aSArtem Bityutskiy  * %0 if not.
1001e51764aSArtem Bityutskiy  */
ubifs_compr_present(struct ubifs_info * c,int compr_type)1016eb61d58SRichard Weinberger static inline int ubifs_compr_present(struct ubifs_info *c, int compr_type)
1021e51764aSArtem Bityutskiy {
1036eb61d58SRichard Weinberger 	ubifs_assert(c, compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
1041e51764aSArtem Bityutskiy 	return !!ubifs_compressors[compr_type]->capi_name;
1051e51764aSArtem Bityutskiy }
1061e51764aSArtem Bityutskiy 
1071e51764aSArtem Bityutskiy /**
1081e51764aSArtem Bityutskiy  * ubifs_compr_name - get compressor name string by its type.
1091e51764aSArtem Bityutskiy  * @compr_type: compressor type
1106eb61d58SRichard Weinberger  * @c: the UBIFS file-system description object
1111e51764aSArtem Bityutskiy  *
1121e51764aSArtem Bityutskiy  * This function returns compressor type string.
1131e51764aSArtem Bityutskiy  */
ubifs_compr_name(struct ubifs_info * c,int compr_type)1146eb61d58SRichard Weinberger static inline const char *ubifs_compr_name(struct ubifs_info *c, int compr_type)
1151e51764aSArtem Bityutskiy {
1166eb61d58SRichard Weinberger 	ubifs_assert(c, compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
1171e51764aSArtem Bityutskiy 	return ubifs_compressors[compr_type]->name;
1181e51764aSArtem Bityutskiy }
1191e51764aSArtem Bityutskiy 
1201e51764aSArtem Bityutskiy /**
1211e51764aSArtem Bityutskiy  * ubifs_wbuf_sync - synchronize write-buffer.
1221e51764aSArtem Bityutskiy  * @wbuf: write-buffer to synchronize
1231e51764aSArtem Bityutskiy  *
124fcf44196SRandy Dunlap  * This is the same as 'ubifs_wbuf_sync_nolock()' but it does not assume
1251e51764aSArtem Bityutskiy  * that the write-buffer is already locked.
1261e51764aSArtem Bityutskiy  */
ubifs_wbuf_sync(struct ubifs_wbuf * wbuf)1271e51764aSArtem Bityutskiy static inline int ubifs_wbuf_sync(struct ubifs_wbuf *wbuf)
1281e51764aSArtem Bityutskiy {
1291e51764aSArtem Bityutskiy 	int err;
1301e51764aSArtem Bityutskiy 
1311e51764aSArtem Bityutskiy 	mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1321e51764aSArtem Bityutskiy 	err = ubifs_wbuf_sync_nolock(wbuf);
1331e51764aSArtem Bityutskiy 	mutex_unlock(&wbuf->io_mutex);
1341e51764aSArtem Bityutskiy 	return err;
1351e51764aSArtem Bityutskiy }
1361e51764aSArtem Bityutskiy 
1371e51764aSArtem Bityutskiy /**
1381e51764aSArtem Bityutskiy  * ubifs_encode_dev - encode device node IDs.
1391e51764aSArtem Bityutskiy  * @dev: UBIFS device node information
1401e51764aSArtem Bityutskiy  * @rdev: device IDs to encode
1411e51764aSArtem Bityutskiy  *
1421e51764aSArtem Bityutskiy  * This is a helper function which encodes major/minor numbers of a device node
1431e51764aSArtem Bityutskiy  * into UBIFS device node description. We use standard Linux "new" and "huge"
1441e51764aSArtem Bityutskiy  * encodings.
1451e51764aSArtem Bityutskiy  */
ubifs_encode_dev(union ubifs_dev_desc * dev,dev_t rdev)1461e51764aSArtem Bityutskiy static inline int ubifs_encode_dev(union ubifs_dev_desc *dev, dev_t rdev)
1471e51764aSArtem Bityutskiy {
1481e51764aSArtem Bityutskiy 	dev->new = cpu_to_le32(new_encode_dev(rdev));
1491e51764aSArtem Bityutskiy 	return sizeof(dev->new);
1501e51764aSArtem Bityutskiy }
1511e51764aSArtem Bityutskiy 
1521e51764aSArtem Bityutskiy /**
1531e51764aSArtem Bityutskiy  * ubifs_add_dirt - add dirty space to LEB properties.
1541e51764aSArtem Bityutskiy  * @c: the UBIFS file-system description object
1551e51764aSArtem Bityutskiy  * @lnum: LEB to add dirty space for
1561e51764aSArtem Bityutskiy  * @dirty: dirty space to add
1571e51764aSArtem Bityutskiy  *
1581e51764aSArtem Bityutskiy  * This is a helper function which increased amount of dirty LEB space. Returns
1591e51764aSArtem Bityutskiy  * zero in case of success and a negative error code in case of failure.
1601e51764aSArtem Bityutskiy  */
ubifs_add_dirt(struct ubifs_info * c,int lnum,int dirty)1611e51764aSArtem Bityutskiy static inline int ubifs_add_dirt(struct ubifs_info *c, int lnum, int dirty)
1621e51764aSArtem Bityutskiy {
1631e51764aSArtem Bityutskiy 	return ubifs_update_one_lp(c, lnum, LPROPS_NC, dirty, 0, 0);
1641e51764aSArtem Bityutskiy }
1651e51764aSArtem Bityutskiy 
1661e51764aSArtem Bityutskiy /**
1671e51764aSArtem Bityutskiy  * ubifs_return_leb - return LEB to lprops.
1681e51764aSArtem Bityutskiy  * @c: the UBIFS file-system description object
1691e51764aSArtem Bityutskiy  * @lnum: LEB to return
1701e51764aSArtem Bityutskiy  *
1711e51764aSArtem Bityutskiy  * This helper function cleans the "taken" flag of a logical eraseblock in the
1721e51764aSArtem Bityutskiy  * lprops. Returns zero in case of success and a negative error code in case of
1731e51764aSArtem Bityutskiy  * failure.
1741e51764aSArtem Bityutskiy  */
ubifs_return_leb(struct ubifs_info * c,int lnum)1751e51764aSArtem Bityutskiy static inline int ubifs_return_leb(struct ubifs_info *c, int lnum)
1761e51764aSArtem Bityutskiy {
1771e51764aSArtem Bityutskiy 	return ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
1781e51764aSArtem Bityutskiy 				   LPROPS_TAKEN, 0);
1791e51764aSArtem Bityutskiy }
1801e51764aSArtem Bityutskiy 
1811e51764aSArtem Bityutskiy /**
1821e51764aSArtem Bityutskiy  * ubifs_idx_node_sz - return index node size.
1831e51764aSArtem Bityutskiy  * @c: the UBIFS file-system description object
1841e51764aSArtem Bityutskiy  * @child_cnt: number of children of this index node
1851e51764aSArtem Bityutskiy  */
ubifs_idx_node_sz(const struct ubifs_info * c,int child_cnt)1861e51764aSArtem Bityutskiy static inline int ubifs_idx_node_sz(const struct ubifs_info *c, int child_cnt)
1871e51764aSArtem Bityutskiy {
18816a26b20SSascha Hauer 	return UBIFS_IDX_NODE_SZ + (UBIFS_BRANCH_SZ + c->key_len + c->hash_len)
18916a26b20SSascha Hauer 				   * child_cnt;
1901e51764aSArtem Bityutskiy }
1911e51764aSArtem Bityutskiy 
1921e51764aSArtem Bityutskiy /**
1931e51764aSArtem Bityutskiy  * ubifs_idx_branch - return pointer to an index branch.
1941e51764aSArtem Bityutskiy  * @c: the UBIFS file-system description object
1951e51764aSArtem Bityutskiy  * @idx: index node
1961e51764aSArtem Bityutskiy  * @bnum: branch number
1971e51764aSArtem Bityutskiy  */
1981e51764aSArtem Bityutskiy static inline
ubifs_idx_branch(const struct ubifs_info * c,const struct ubifs_idx_node * idx,int bnum)1991e51764aSArtem Bityutskiy struct ubifs_branch *ubifs_idx_branch(const struct ubifs_info *c,
2001e51764aSArtem Bityutskiy 				      const struct ubifs_idx_node *idx,
2011e51764aSArtem Bityutskiy 				      int bnum)
2021e51764aSArtem Bityutskiy {
2031e51764aSArtem Bityutskiy 	return (struct ubifs_branch *)((void *)idx->branches +
20416a26b20SSascha Hauer 			(UBIFS_BRANCH_SZ + c->key_len + c->hash_len) * bnum);
2051e51764aSArtem Bityutskiy }
2061e51764aSArtem Bityutskiy 
2071e51764aSArtem Bityutskiy /**
2081e51764aSArtem Bityutskiy  * ubifs_idx_key - return pointer to an index key.
2091e51764aSArtem Bityutskiy  * @c: the UBIFS file-system description object
2101e51764aSArtem Bityutskiy  * @idx: index node
2111e51764aSArtem Bityutskiy  */
ubifs_idx_key(const struct ubifs_info * c,const struct ubifs_idx_node * idx)2121e51764aSArtem Bityutskiy static inline void *ubifs_idx_key(const struct ubifs_info *c,
2131e51764aSArtem Bityutskiy 				  const struct ubifs_idx_node *idx)
2141e51764aSArtem Bityutskiy {
2151e51764aSArtem Bityutskiy 	return (void *)((struct ubifs_branch *)idx->branches)->key;
2161e51764aSArtem Bityutskiy }
2171e51764aSArtem Bityutskiy 
2181e51764aSArtem Bityutskiy /**
219601c0bc4SAdrian Hunter  * ubifs_tnc_lookup - look up a file-system node.
220601c0bc4SAdrian Hunter  * @c: UBIFS file-system description object
221601c0bc4SAdrian Hunter  * @key: node key to lookup
222601c0bc4SAdrian Hunter  * @node: the node is returned here
223601c0bc4SAdrian Hunter  *
224601c0bc4SAdrian Hunter  * This function look up and reads node with key @key. The caller has to make
225601c0bc4SAdrian Hunter  * sure the @node buffer is large enough to fit the node. Returns zero in case
226601c0bc4SAdrian Hunter  * of success, %-ENOENT if the node was not found, and a negative error code in
227601c0bc4SAdrian Hunter  * case of failure.
228601c0bc4SAdrian Hunter  */
ubifs_tnc_lookup(struct ubifs_info * c,const union ubifs_key * key,void * node)229601c0bc4SAdrian Hunter static inline int ubifs_tnc_lookup(struct ubifs_info *c,
230601c0bc4SAdrian Hunter 				   const union ubifs_key *key, void *node)
231601c0bc4SAdrian Hunter {
232601c0bc4SAdrian Hunter 	return ubifs_tnc_locate(c, key, node, NULL, NULL);
233601c0bc4SAdrian Hunter }
234601c0bc4SAdrian Hunter 
235746103acSArtem Bityutskiy /**
236746103acSArtem Bityutskiy  * ubifs_get_lprops - get reference to LEB properties.
237746103acSArtem Bityutskiy  * @c: the UBIFS file-system description object
238746103acSArtem Bityutskiy  *
239746103acSArtem Bityutskiy  * This function locks lprops. Lprops have to be unlocked by
240746103acSArtem Bityutskiy  * 'ubifs_release_lprops()'.
241746103acSArtem Bityutskiy  */
ubifs_get_lprops(struct ubifs_info * c)242746103acSArtem Bityutskiy static inline void ubifs_get_lprops(struct ubifs_info *c)
243746103acSArtem Bityutskiy {
244746103acSArtem Bityutskiy 	mutex_lock(&c->lp_mutex);
245746103acSArtem Bityutskiy }
246746103acSArtem Bityutskiy 
247746103acSArtem Bityutskiy /**
248746103acSArtem Bityutskiy  * ubifs_release_lprops - release lprops lock.
249746103acSArtem Bityutskiy  * @c: the UBIFS file-system description object
250746103acSArtem Bityutskiy  *
251746103acSArtem Bityutskiy  * This function has to be called after each 'ubifs_get_lprops()' call to
252746103acSArtem Bityutskiy  * unlock lprops.
253746103acSArtem Bityutskiy  */
ubifs_release_lprops(struct ubifs_info * c)254746103acSArtem Bityutskiy static inline void ubifs_release_lprops(struct ubifs_info *c)
255746103acSArtem Bityutskiy {
2566eb61d58SRichard Weinberger 	ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
2576eb61d58SRichard Weinberger 	ubifs_assert(c, c->lst.empty_lebs >= 0 &&
258746103acSArtem Bityutskiy 		     c->lst.empty_lebs <= c->main_lebs);
259746103acSArtem Bityutskiy 	mutex_unlock(&c->lp_mutex);
260746103acSArtem Bityutskiy }
261746103acSArtem Bityutskiy 
262e11602eaSArtem Bityutskiy /**
263e11602eaSArtem Bityutskiy  * ubifs_next_log_lnum - switch to the next log LEB.
264e11602eaSArtem Bityutskiy  * @c: UBIFS file-system description object
265e11602eaSArtem Bityutskiy  * @lnum: current log LEB
266e11602eaSArtem Bityutskiy  *
267e11602eaSArtem Bityutskiy  * This helper function returns the log LEB number which goes next after LEB
268e11602eaSArtem Bityutskiy  * 'lnum'.
269e11602eaSArtem Bityutskiy  */
ubifs_next_log_lnum(const struct ubifs_info * c,int lnum)270e11602eaSArtem Bityutskiy static inline int ubifs_next_log_lnum(const struct ubifs_info *c, int lnum)
271e11602eaSArtem Bityutskiy {
272e11602eaSArtem Bityutskiy 	lnum += 1;
273e11602eaSArtem Bityutskiy 	if (lnum > c->log_last)
274e11602eaSArtem Bityutskiy 		lnum = UBIFS_LOG_LNUM;
275e11602eaSArtem Bityutskiy 
276e11602eaSArtem Bityutskiy 	return lnum;
277e11602eaSArtem Bityutskiy }
278e11602eaSArtem Bityutskiy 
ubifs_xattr_max_cnt(struct ubifs_info * c)2799ca2d732SRichard Weinberger static inline int ubifs_xattr_max_cnt(struct ubifs_info *c)
2809ca2d732SRichard Weinberger {
2819ca2d732SRichard Weinberger 	int max_xattrs = (c->leb_size / 2) / UBIFS_INO_NODE_SZ;
2829ca2d732SRichard Weinberger 
2839ca2d732SRichard Weinberger 	ubifs_assert(c, max_xattrs < c->max_orphans);
2849ca2d732SRichard Weinberger 	return max_xattrs;
2859ca2d732SRichard Weinberger }
2869ca2d732SRichard Weinberger 
287c38c5a7fSRichard Weinberger const char *ubifs_assert_action_name(struct ubifs_info *c);
288c38c5a7fSRichard Weinberger 
2891e51764aSArtem Bityutskiy #endif /* __UBIFS_MISC_H__ */
290