misc.h 7.8 KB

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