xref: /openbmc/linux/fs/ubifs/misc.h (revision 9ca2d732)
11e51764aSArtem Bityutskiy /*
21e51764aSArtem Bityutskiy  * This file is part of UBIFS.
31e51764aSArtem Bityutskiy  *
41e51764aSArtem Bityutskiy  * Copyright (C) 2006-2008 Nokia Corporation
51e51764aSArtem Bityutskiy  *
61e51764aSArtem Bityutskiy  * This program is free software; you can redistribute it and/or modify it
71e51764aSArtem Bityutskiy  * under the terms of the GNU General Public License version 2 as published by
81e51764aSArtem Bityutskiy  * the Free Software Foundation.
91e51764aSArtem Bityutskiy  *
101e51764aSArtem Bityutskiy  * This program is distributed in the hope that it will be useful, but WITHOUT
111e51764aSArtem Bityutskiy  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
121e51764aSArtem Bityutskiy  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
131e51764aSArtem Bityutskiy  * more details.
141e51764aSArtem Bityutskiy  *
151e51764aSArtem Bityutskiy  * You should have received a copy of the GNU General Public License along with
161e51764aSArtem Bityutskiy  * this program; if not, write to the Free Software Foundation, Inc., 51
171e51764aSArtem Bityutskiy  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
181e51764aSArtem Bityutskiy  *
191e51764aSArtem Bityutskiy  * Authors: Artem Bityutskiy (Битюцкий Артём)
201e51764aSArtem Bityutskiy  *          Adrian Hunter
211e51764aSArtem Bityutskiy  */
221e51764aSArtem Bityutskiy 
231e51764aSArtem Bityutskiy /*
241e51764aSArtem Bityutskiy  * This file contains miscellaneous helper functions.
251e51764aSArtem Bityutskiy  */
261e51764aSArtem Bityutskiy 
271e51764aSArtem Bityutskiy #ifndef __UBIFS_MISC_H__
281e51764aSArtem Bityutskiy #define __UBIFS_MISC_H__
291e51764aSArtem Bityutskiy 
301e51764aSArtem Bityutskiy /**
311e51764aSArtem Bityutskiy  * ubifs_zn_dirty - check if znode is dirty.
321e51764aSArtem Bityutskiy  * @znode: znode to check
331e51764aSArtem Bityutskiy  *
341e51764aSArtem Bityutskiy  * This helper function returns %1 if @znode is dirty and %0 otherwise.
351e51764aSArtem Bityutskiy  */
361e51764aSArtem Bityutskiy static inline int ubifs_zn_dirty(const struct ubifs_znode *znode)
371e51764aSArtem Bityutskiy {
381e51764aSArtem Bityutskiy 	return !!test_bit(DIRTY_ZNODE, &znode->flags);
391e51764aSArtem Bityutskiy }
401e51764aSArtem Bityutskiy 
411e51764aSArtem Bityutskiy /**
42f42eed7cSArtem Bityutskiy  * ubifs_zn_obsolete - check if znode is obsolete.
43f42eed7cSArtem Bityutskiy  * @znode: znode to check
44f42eed7cSArtem Bityutskiy  *
45f42eed7cSArtem Bityutskiy  * This helper function returns %1 if @znode is obsolete and %0 otherwise.
46f42eed7cSArtem Bityutskiy  */
47f42eed7cSArtem Bityutskiy static inline int ubifs_zn_obsolete(const struct ubifs_znode *znode)
48f42eed7cSArtem Bityutskiy {
49f42eed7cSArtem Bityutskiy 	return !!test_bit(OBSOLETE_ZNODE, &znode->flags);
50f42eed7cSArtem Bityutskiy }
51f42eed7cSArtem Bityutskiy 
52f42eed7cSArtem Bityutskiy /**
53f42eed7cSArtem Bityutskiy  * ubifs_zn_cow - check if znode has to be copied on write.
54f42eed7cSArtem Bityutskiy  * @znode: znode to check
55f42eed7cSArtem Bityutskiy  *
56f42eed7cSArtem Bityutskiy  * This helper function returns %1 if @znode is has COW flag set and %0
57f42eed7cSArtem Bityutskiy  * otherwise.
58f42eed7cSArtem Bityutskiy  */
59f42eed7cSArtem Bityutskiy static inline int ubifs_zn_cow(const struct ubifs_znode *znode)
60f42eed7cSArtem Bityutskiy {
61f42eed7cSArtem Bityutskiy 	return !!test_bit(COW_ZNODE, &znode->flags);
62f42eed7cSArtem Bityutskiy }
63f42eed7cSArtem Bityutskiy 
64f42eed7cSArtem Bityutskiy /**
651e51764aSArtem Bityutskiy  * ubifs_wake_up_bgt - wake up background thread.
661e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
671e51764aSArtem Bityutskiy  */
681e51764aSArtem Bityutskiy static inline void ubifs_wake_up_bgt(struct ubifs_info *c)
691e51764aSArtem Bityutskiy {
701e51764aSArtem Bityutskiy 	if (c->bgt && !c->need_bgt) {
711e51764aSArtem Bityutskiy 		c->need_bgt = 1;
721e51764aSArtem Bityutskiy 		wake_up_process(c->bgt);
731e51764aSArtem Bityutskiy 	}
741e51764aSArtem Bityutskiy }
751e51764aSArtem Bityutskiy 
761e51764aSArtem Bityutskiy /**
771e51764aSArtem Bityutskiy  * ubifs_tnc_find_child - find next child in znode.
781e51764aSArtem Bityutskiy  * @znode: znode to search at
791e51764aSArtem Bityutskiy  * @start: the zbranch index to start at
801e51764aSArtem Bityutskiy  *
811e51764aSArtem Bityutskiy  * This helper function looks for znode child starting at index @start. Returns
821e51764aSArtem Bityutskiy  * the child or %NULL if no children were found.
831e51764aSArtem Bityutskiy  */
841e51764aSArtem Bityutskiy static inline struct ubifs_znode *
851e51764aSArtem Bityutskiy ubifs_tnc_find_child(struct ubifs_znode *znode, int start)
861e51764aSArtem Bityutskiy {
871e51764aSArtem Bityutskiy 	while (start < znode->child_cnt) {
881e51764aSArtem Bityutskiy 		if (znode->zbranch[start].znode)
891e51764aSArtem Bityutskiy 			return znode->zbranch[start].znode;
901e51764aSArtem Bityutskiy 		start += 1;
911e51764aSArtem Bityutskiy 	}
921e51764aSArtem Bityutskiy 
931e51764aSArtem Bityutskiy 	return NULL;
941e51764aSArtem Bityutskiy }
951e51764aSArtem Bityutskiy 
961e51764aSArtem Bityutskiy /**
971e51764aSArtem Bityutskiy  * ubifs_inode - get UBIFS inode information by VFS 'struct inode' object.
981e51764aSArtem Bityutskiy  * @inode: the VFS 'struct inode' pointer
991e51764aSArtem Bityutskiy  */
1001e51764aSArtem Bityutskiy static inline struct ubifs_inode *ubifs_inode(const struct inode *inode)
1011e51764aSArtem Bityutskiy {
1021e51764aSArtem Bityutskiy 	return container_of(inode, struct ubifs_inode, vfs_inode);
1031e51764aSArtem Bityutskiy }
1041e51764aSArtem Bityutskiy 
1051e51764aSArtem Bityutskiy /**
1061e51764aSArtem Bityutskiy  * ubifs_compr_present - check if compressor was compiled in.
1071e51764aSArtem Bityutskiy  * @compr_type: compressor type to check
1086eb61d58SRichard Weinberger  * @c: the UBIFS file-system description object
1091e51764aSArtem Bityutskiy  *
1101e51764aSArtem Bityutskiy  * This function returns %1 of compressor of type @compr_type is present, and
1111e51764aSArtem Bityutskiy  * %0 if not.
1121e51764aSArtem Bityutskiy  */
1136eb61d58SRichard Weinberger static inline int ubifs_compr_present(struct ubifs_info *c, int compr_type)
1141e51764aSArtem Bityutskiy {
1156eb61d58SRichard Weinberger 	ubifs_assert(c, compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
1161e51764aSArtem Bityutskiy 	return !!ubifs_compressors[compr_type]->capi_name;
1171e51764aSArtem Bityutskiy }
1181e51764aSArtem Bityutskiy 
1191e51764aSArtem Bityutskiy /**
1201e51764aSArtem Bityutskiy  * ubifs_compr_name - get compressor name string by its type.
1211e51764aSArtem Bityutskiy  * @compr_type: compressor type
1226eb61d58SRichard Weinberger  * @c: the UBIFS file-system description object
1231e51764aSArtem Bityutskiy  *
1241e51764aSArtem Bityutskiy  * This function returns compressor type string.
1251e51764aSArtem Bityutskiy  */
1266eb61d58SRichard Weinberger static inline const char *ubifs_compr_name(struct ubifs_info *c, int compr_type)
1271e51764aSArtem Bityutskiy {
1286eb61d58SRichard Weinberger 	ubifs_assert(c, compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
1291e51764aSArtem Bityutskiy 	return ubifs_compressors[compr_type]->name;
1301e51764aSArtem Bityutskiy }
1311e51764aSArtem Bityutskiy 
1321e51764aSArtem Bityutskiy /**
1331e51764aSArtem Bityutskiy  * ubifs_wbuf_sync - synchronize write-buffer.
1341e51764aSArtem Bityutskiy  * @wbuf: write-buffer to synchronize
1351e51764aSArtem Bityutskiy  *
1361e51764aSArtem Bityutskiy  * This is the same as as 'ubifs_wbuf_sync_nolock()' but it does not assume
1371e51764aSArtem Bityutskiy  * that the write-buffer is already locked.
1381e51764aSArtem Bityutskiy  */
1391e51764aSArtem Bityutskiy static inline int ubifs_wbuf_sync(struct ubifs_wbuf *wbuf)
1401e51764aSArtem Bityutskiy {
1411e51764aSArtem Bityutskiy 	int err;
1421e51764aSArtem Bityutskiy 
1431e51764aSArtem Bityutskiy 	mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1441e51764aSArtem Bityutskiy 	err = ubifs_wbuf_sync_nolock(wbuf);
1451e51764aSArtem Bityutskiy 	mutex_unlock(&wbuf->io_mutex);
1461e51764aSArtem Bityutskiy 	return err;
1471e51764aSArtem Bityutskiy }
1481e51764aSArtem Bityutskiy 
1491e51764aSArtem Bityutskiy /**
1501e51764aSArtem Bityutskiy  * ubifs_encode_dev - encode device node IDs.
1511e51764aSArtem Bityutskiy  * @dev: UBIFS device node information
1521e51764aSArtem Bityutskiy  * @rdev: device IDs to encode
1531e51764aSArtem Bityutskiy  *
1541e51764aSArtem Bityutskiy  * This is a helper function which encodes major/minor numbers of a device node
1551e51764aSArtem Bityutskiy  * into UBIFS device node description. We use standard Linux "new" and "huge"
1561e51764aSArtem Bityutskiy  * encodings.
1571e51764aSArtem Bityutskiy  */
1581e51764aSArtem Bityutskiy static inline int ubifs_encode_dev(union ubifs_dev_desc *dev, dev_t rdev)
1591e51764aSArtem Bityutskiy {
1601e51764aSArtem Bityutskiy 	dev->new = cpu_to_le32(new_encode_dev(rdev));
1611e51764aSArtem Bityutskiy 	return sizeof(dev->new);
1621e51764aSArtem Bityutskiy }
1631e51764aSArtem Bityutskiy 
1641e51764aSArtem Bityutskiy /**
1651e51764aSArtem Bityutskiy  * ubifs_add_dirt - add dirty space to LEB properties.
1661e51764aSArtem Bityutskiy  * @c: the UBIFS file-system description object
1671e51764aSArtem Bityutskiy  * @lnum: LEB to add dirty space for
1681e51764aSArtem Bityutskiy  * @dirty: dirty space to add
1691e51764aSArtem Bityutskiy  *
1701e51764aSArtem Bityutskiy  * This is a helper function which increased amount of dirty LEB space. Returns
1711e51764aSArtem Bityutskiy  * zero in case of success and a negative error code in case of failure.
1721e51764aSArtem Bityutskiy  */
1731e51764aSArtem Bityutskiy static inline int ubifs_add_dirt(struct ubifs_info *c, int lnum, int dirty)
1741e51764aSArtem Bityutskiy {
1751e51764aSArtem Bityutskiy 	return ubifs_update_one_lp(c, lnum, LPROPS_NC, dirty, 0, 0);
1761e51764aSArtem Bityutskiy }
1771e51764aSArtem Bityutskiy 
1781e51764aSArtem Bityutskiy /**
1791e51764aSArtem Bityutskiy  * ubifs_return_leb - return LEB to lprops.
1801e51764aSArtem Bityutskiy  * @c: the UBIFS file-system description object
1811e51764aSArtem Bityutskiy  * @lnum: LEB to return
1821e51764aSArtem Bityutskiy  *
1831e51764aSArtem Bityutskiy  * This helper function cleans the "taken" flag of a logical eraseblock in the
1841e51764aSArtem Bityutskiy  * lprops. Returns zero in case of success and a negative error code in case of
1851e51764aSArtem Bityutskiy  * failure.
1861e51764aSArtem Bityutskiy  */
1871e51764aSArtem Bityutskiy static inline int ubifs_return_leb(struct ubifs_info *c, int lnum)
1881e51764aSArtem Bityutskiy {
1891e51764aSArtem Bityutskiy 	return ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
1901e51764aSArtem Bityutskiy 				   LPROPS_TAKEN, 0);
1911e51764aSArtem Bityutskiy }
1921e51764aSArtem Bityutskiy 
1931e51764aSArtem Bityutskiy /**
1941e51764aSArtem Bityutskiy  * ubifs_idx_node_sz - return index node size.
1951e51764aSArtem Bityutskiy  * @c: the UBIFS file-system description object
1961e51764aSArtem Bityutskiy  * @child_cnt: number of children of this index node
1971e51764aSArtem Bityutskiy  */
1981e51764aSArtem Bityutskiy static inline int ubifs_idx_node_sz(const struct ubifs_info *c, int child_cnt)
1991e51764aSArtem Bityutskiy {
20016a26b20SSascha Hauer 	return UBIFS_IDX_NODE_SZ + (UBIFS_BRANCH_SZ + c->key_len + c->hash_len)
20116a26b20SSascha Hauer 				   * child_cnt;
2021e51764aSArtem Bityutskiy }
2031e51764aSArtem Bityutskiy 
2041e51764aSArtem Bityutskiy /**
2051e51764aSArtem Bityutskiy  * ubifs_idx_branch - return pointer to an index branch.
2061e51764aSArtem Bityutskiy  * @c: the UBIFS file-system description object
2071e51764aSArtem Bityutskiy  * @idx: index node
2081e51764aSArtem Bityutskiy  * @bnum: branch number
2091e51764aSArtem Bityutskiy  */
2101e51764aSArtem Bityutskiy static inline
2111e51764aSArtem Bityutskiy struct ubifs_branch *ubifs_idx_branch(const struct ubifs_info *c,
2121e51764aSArtem Bityutskiy 				      const struct ubifs_idx_node *idx,
2131e51764aSArtem Bityutskiy 				      int bnum)
2141e51764aSArtem Bityutskiy {
2151e51764aSArtem Bityutskiy 	return (struct ubifs_branch *)((void *)idx->branches +
21616a26b20SSascha Hauer 			(UBIFS_BRANCH_SZ + c->key_len + c->hash_len) * bnum);
2171e51764aSArtem Bityutskiy }
2181e51764aSArtem Bityutskiy 
2191e51764aSArtem Bityutskiy /**
2201e51764aSArtem Bityutskiy  * ubifs_idx_key - return pointer to an index key.
2211e51764aSArtem Bityutskiy  * @c: the UBIFS file-system description object
2221e51764aSArtem Bityutskiy  * @idx: index node
2231e51764aSArtem Bityutskiy  */
2241e51764aSArtem Bityutskiy static inline void *ubifs_idx_key(const struct ubifs_info *c,
2251e51764aSArtem Bityutskiy 				  const struct ubifs_idx_node *idx)
2261e51764aSArtem Bityutskiy {
2271e51764aSArtem Bityutskiy 	return (void *)((struct ubifs_branch *)idx->branches)->key;
2281e51764aSArtem Bityutskiy }
2291e51764aSArtem Bityutskiy 
2301e51764aSArtem Bityutskiy /**
231601c0bc4SAdrian Hunter  * ubifs_tnc_lookup - look up a file-system node.
232601c0bc4SAdrian Hunter  * @c: UBIFS file-system description object
233601c0bc4SAdrian Hunter  * @key: node key to lookup
234601c0bc4SAdrian Hunter  * @node: the node is returned here
235601c0bc4SAdrian Hunter  *
236601c0bc4SAdrian Hunter  * This function look up and reads node with key @key. The caller has to make
237601c0bc4SAdrian Hunter  * sure the @node buffer is large enough to fit the node. Returns zero in case
238601c0bc4SAdrian Hunter  * of success, %-ENOENT if the node was not found, and a negative error code in
239601c0bc4SAdrian Hunter  * case of failure.
240601c0bc4SAdrian Hunter  */
241601c0bc4SAdrian Hunter static inline int ubifs_tnc_lookup(struct ubifs_info *c,
242601c0bc4SAdrian Hunter 				   const union ubifs_key *key, void *node)
243601c0bc4SAdrian Hunter {
244601c0bc4SAdrian Hunter 	return ubifs_tnc_locate(c, key, node, NULL, NULL);
245601c0bc4SAdrian Hunter }
246601c0bc4SAdrian Hunter 
247746103acSArtem Bityutskiy /**
248746103acSArtem Bityutskiy  * ubifs_get_lprops - get reference to LEB properties.
249746103acSArtem Bityutskiy  * @c: the UBIFS file-system description object
250746103acSArtem Bityutskiy  *
251746103acSArtem Bityutskiy  * This function locks lprops. Lprops have to be unlocked by
252746103acSArtem Bityutskiy  * 'ubifs_release_lprops()'.
253746103acSArtem Bityutskiy  */
254746103acSArtem Bityutskiy static inline void ubifs_get_lprops(struct ubifs_info *c)
255746103acSArtem Bityutskiy {
256746103acSArtem Bityutskiy 	mutex_lock(&c->lp_mutex);
257746103acSArtem Bityutskiy }
258746103acSArtem Bityutskiy 
259746103acSArtem Bityutskiy /**
260746103acSArtem Bityutskiy  * ubifs_release_lprops - release lprops lock.
261746103acSArtem Bityutskiy  * @c: the UBIFS file-system description object
262746103acSArtem Bityutskiy  *
263746103acSArtem Bityutskiy  * This function has to be called after each 'ubifs_get_lprops()' call to
264746103acSArtem Bityutskiy  * unlock lprops.
265746103acSArtem Bityutskiy  */
266746103acSArtem Bityutskiy static inline void ubifs_release_lprops(struct ubifs_info *c)
267746103acSArtem Bityutskiy {
2686eb61d58SRichard Weinberger 	ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
2696eb61d58SRichard Weinberger 	ubifs_assert(c, c->lst.empty_lebs >= 0 &&
270746103acSArtem Bityutskiy 		     c->lst.empty_lebs <= c->main_lebs);
271746103acSArtem Bityutskiy 	mutex_unlock(&c->lp_mutex);
272746103acSArtem Bityutskiy }
273746103acSArtem Bityutskiy 
274e11602eaSArtem Bityutskiy /**
275e11602eaSArtem Bityutskiy  * ubifs_next_log_lnum - switch to the next log LEB.
276e11602eaSArtem Bityutskiy  * @c: UBIFS file-system description object
277e11602eaSArtem Bityutskiy  * @lnum: current log LEB
278e11602eaSArtem Bityutskiy  *
279e11602eaSArtem Bityutskiy  * This helper function returns the log LEB number which goes next after LEB
280e11602eaSArtem Bityutskiy  * 'lnum'.
281e11602eaSArtem Bityutskiy  */
282e11602eaSArtem Bityutskiy static inline int ubifs_next_log_lnum(const struct ubifs_info *c, int lnum)
283e11602eaSArtem Bityutskiy {
284e11602eaSArtem Bityutskiy 	lnum += 1;
285e11602eaSArtem Bityutskiy 	if (lnum > c->log_last)
286e11602eaSArtem Bityutskiy 		lnum = UBIFS_LOG_LNUM;
287e11602eaSArtem Bityutskiy 
288e11602eaSArtem Bityutskiy 	return lnum;
289e11602eaSArtem Bityutskiy }
290e11602eaSArtem Bityutskiy 
2919ca2d732SRichard Weinberger static inline int ubifs_xattr_max_cnt(struct ubifs_info *c)
2929ca2d732SRichard Weinberger {
2939ca2d732SRichard Weinberger 	int max_xattrs = (c->leb_size / 2) / UBIFS_INO_NODE_SZ;
2949ca2d732SRichard Weinberger 
2959ca2d732SRichard Weinberger 	ubifs_assert(c, max_xattrs < c->max_orphans);
2969ca2d732SRichard Weinberger 	return max_xattrs;
2979ca2d732SRichard Weinberger }
2989ca2d732SRichard Weinberger 
299c38c5a7fSRichard Weinberger const char *ubifs_assert_action_name(struct ubifs_info *c);
300c38c5a7fSRichard Weinberger 
3011e51764aSArtem Bityutskiy #endif /* __UBIFS_MISC_H__ */
302