misc.h 7.8 KB

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