misc.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Artem Bityutskiy (Битюцкий Артём)
  20. * Adrian Hunter
  21. */
  22. /*
  23. * This file contains miscellaneous helper functions.
  24. */
  25. #ifndef __UBIFS_MISC_H__
  26. #define __UBIFS_MISC_H__
  27. /**
  28. * ubifs_zn_dirty - check if znode is dirty.
  29. * @znode: znode to check
  30. *
  31. * This helper function returns %1 if @znode is dirty and %0 otherwise.
  32. */
  33. static inline int ubifs_zn_dirty(const struct ubifs_znode *znode)
  34. {
  35. return !!test_bit(DIRTY_ZNODE, &znode->flags);
  36. }
  37. /**
  38. * ubifs_wake_up_bgt - wake up background thread.
  39. * @c: UBIFS file-system description object
  40. */
  41. static inline void ubifs_wake_up_bgt(struct ubifs_info *c)
  42. {
  43. if (c->bgt && !c->need_bgt) {
  44. c->need_bgt = 1;
  45. wake_up_process(c->bgt);
  46. }
  47. }
  48. /**
  49. * ubifs_tnc_find_child - find next child in znode.
  50. * @znode: znode to search at
  51. * @start: the zbranch index to start at
  52. *
  53. * This helper function looks for znode child starting at index @start. Returns
  54. * the child or %NULL if no children were found.
  55. */
  56. static inline struct ubifs_znode *
  57. ubifs_tnc_find_child(struct ubifs_znode *znode, int start)
  58. {
  59. while (start < znode->child_cnt) {
  60. if (znode->zbranch[start].znode)
  61. return znode->zbranch[start].znode;
  62. start += 1;
  63. }
  64. return NULL;
  65. }
  66. /**
  67. * ubifs_inode - get UBIFS inode information by VFS 'struct inode' object.
  68. * @inode: the VFS 'struct inode' pointer
  69. */
  70. static inline struct ubifs_inode *ubifs_inode(const struct inode *inode)
  71. {
  72. return container_of(inode, struct ubifs_inode, vfs_inode);
  73. }
  74. /**
  75. * ubifs_ro_mode - switch UBIFS to read read-only mode.
  76. * @c: UBIFS file-system description object
  77. * @err: error code which is the reason of switching to R/O mode
  78. */
  79. static inline void ubifs_ro_mode(struct ubifs_info *c, int err)
  80. {
  81. if (!c->ro_media) {
  82. c->ro_media = 1;
  83. ubifs_warn("switched to read-only mode, error %d", err);
  84. dbg_dump_stack();
  85. }
  86. }
  87. /**
  88. * ubifs_compr_present - check if compressor was compiled in.
  89. * @compr_type: compressor type to check
  90. *
  91. * This function returns %1 of compressor of type @compr_type is present, and
  92. * %0 if not.
  93. */
  94. static inline int ubifs_compr_present(int compr_type)
  95. {
  96. ubifs_assert(compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
  97. return !!ubifs_compressors[compr_type]->capi_name;
  98. }
  99. /**
  100. * ubifs_compr_name - get compressor name string by its type.
  101. * @compr_type: compressor type
  102. *
  103. * This function returns compressor type string.
  104. */
  105. static inline const char *ubifs_compr_name(int compr_type)
  106. {
  107. ubifs_assert(compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
  108. return ubifs_compressors[compr_type]->name;
  109. }
  110. /**
  111. * ubifs_wbuf_sync - synchronize write-buffer.
  112. * @wbuf: write-buffer to synchronize
  113. *
  114. * This is the same as as 'ubifs_wbuf_sync_nolock()' but it does not assume
  115. * that the write-buffer is already locked.
  116. */
  117. static inline int ubifs_wbuf_sync(struct ubifs_wbuf *wbuf)
  118. {
  119. int err;
  120. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  121. err = ubifs_wbuf_sync_nolock(wbuf);
  122. mutex_unlock(&wbuf->io_mutex);
  123. return err;
  124. }
  125. /**
  126. * ubifs_leb_unmap - unmap an LEB.
  127. * @c: UBIFS file-system description object
  128. * @lnum: LEB number to unmap
  129. *
  130. * This function returns %0 on success and a negative error code on failure.
  131. */
  132. static inline int ubifs_leb_unmap(const struct ubifs_info *c, int lnum)
  133. {
  134. int err;
  135. if (c->ro_media)
  136. return -EROFS;
  137. err = ubi_leb_unmap(c->ubi, lnum);
  138. if (err) {
  139. ubifs_err("unmap LEB %d failed, error %d", lnum, err);
  140. return err;
  141. }
  142. return 0;
  143. }
  144. /**
  145. * ubifs_leb_write - write to a LEB.
  146. * @c: UBIFS file-system description object
  147. * @lnum: LEB number to write
  148. * @buf: buffer to write from
  149. * @offs: offset within LEB to write to
  150. * @len: length to write
  151. * @dtype: data type
  152. *
  153. * This function returns %0 on success and a negative error code on failure.
  154. */
  155. static inline int ubifs_leb_write(const struct ubifs_info *c, int lnum,
  156. const void *buf, int offs, int len, int dtype)
  157. {
  158. int err;
  159. if (c->ro_media)
  160. return -EROFS;
  161. err = ubi_leb_write(c->ubi, lnum, buf, offs, len, dtype);
  162. if (err) {
  163. ubifs_err("writing %d bytes at %d:%d, error %d",
  164. len, lnum, offs, err);
  165. return err;
  166. }
  167. return 0;
  168. }
  169. /**
  170. * ubifs_leb_change - atomic LEB change.
  171. * @c: UBIFS file-system description object
  172. * @lnum: LEB number to write
  173. * @buf: buffer to write from
  174. * @len: length to write
  175. * @dtype: data type
  176. *
  177. * This function returns %0 on success and a negative error code on failure.
  178. */
  179. static inline int ubifs_leb_change(const struct ubifs_info *c, int lnum,
  180. const void *buf, int len, int dtype)
  181. {
  182. int err;
  183. if (c->ro_media)
  184. return -EROFS;
  185. err = ubi_leb_change(c->ubi, lnum, buf, len, dtype);
  186. if (err) {
  187. ubifs_err("changing %d bytes in LEB %d, error %d",
  188. len, lnum, err);
  189. return err;
  190. }
  191. return 0;
  192. }
  193. /**
  194. * ubifs_encode_dev - encode device node IDs.
  195. * @dev: UBIFS device node information
  196. * @rdev: device IDs to encode
  197. *
  198. * This is a helper function which encodes major/minor numbers of a device node
  199. * into UBIFS device node description. We use standard Linux "new" and "huge"
  200. * encodings.
  201. */
  202. static inline int ubifs_encode_dev(union ubifs_dev_desc *dev, dev_t rdev)
  203. {
  204. if (new_valid_dev(rdev)) {
  205. dev->new = cpu_to_le32(new_encode_dev(rdev));
  206. return sizeof(dev->new);
  207. } else {
  208. dev->huge = cpu_to_le64(huge_encode_dev(rdev));
  209. return sizeof(dev->huge);
  210. }
  211. }
  212. /**
  213. * ubifs_add_dirt - add dirty space to LEB properties.
  214. * @c: the UBIFS file-system description object
  215. * @lnum: LEB to add dirty space for
  216. * @dirty: dirty space to add
  217. *
  218. * This is a helper function which increased amount of dirty LEB space. Returns
  219. * zero in case of success and a negative error code in case of failure.
  220. */
  221. static inline int ubifs_add_dirt(struct ubifs_info *c, int lnum, int dirty)
  222. {
  223. return ubifs_update_one_lp(c, lnum, LPROPS_NC, dirty, 0, 0);
  224. }
  225. /**
  226. * ubifs_return_leb - return LEB to lprops.
  227. * @c: the UBIFS file-system description object
  228. * @lnum: LEB to return
  229. *
  230. * This helper function cleans the "taken" flag of a logical eraseblock in the
  231. * lprops. Returns zero in case of success and a negative error code in case of
  232. * failure.
  233. */
  234. static inline int ubifs_return_leb(struct ubifs_info *c, int lnum)
  235. {
  236. return ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
  237. LPROPS_TAKEN, 0);
  238. }
  239. /**
  240. * ubifs_idx_node_sz - return index node size.
  241. * @c: the UBIFS file-system description object
  242. * @child_cnt: number of children of this index node
  243. */
  244. static inline int ubifs_idx_node_sz(const struct ubifs_info *c, int child_cnt)
  245. {
  246. return UBIFS_IDX_NODE_SZ + (UBIFS_BRANCH_SZ + c->key_len) * child_cnt;
  247. }
  248. /**
  249. * ubifs_idx_branch - return pointer to an index branch.
  250. * @c: the UBIFS file-system description object
  251. * @idx: index node
  252. * @bnum: branch number
  253. */
  254. static inline
  255. struct ubifs_branch *ubifs_idx_branch(const struct ubifs_info *c,
  256. const struct ubifs_idx_node *idx,
  257. int bnum)
  258. {
  259. return (struct ubifs_branch *)((void *)idx->branches +
  260. (UBIFS_BRANCH_SZ + c->key_len) * bnum);
  261. }
  262. /**
  263. * ubifs_idx_key - return pointer to an index key.
  264. * @c: the UBIFS file-system description object
  265. * @idx: index node
  266. */
  267. static inline void *ubifs_idx_key(const struct ubifs_info *c,
  268. const struct ubifs_idx_node *idx)
  269. {
  270. return (void *)((struct ubifs_branch *)idx->branches)->key;
  271. }
  272. /**
  273. * ubifs_reported_space - calculate reported free space.
  274. * @c: the UBIFS file-system description object
  275. * @free: amount of free space
  276. *
  277. * This function calculates amount of free space which will be reported to
  278. * user-space. User-space application tend to expect that if the file-system
  279. * (e.g., via the 'statfs()' call) reports that it has N bytes available, they
  280. * are able to write a file of size N. UBIFS attaches node headers to each data
  281. * node and it has to write indexind nodes as well. This introduces additional
  282. * overhead, and UBIFS it has to report sligtly less free space to meet the
  283. * above expectetion.
  284. *
  285. * This function assumes free space is made up of uncompressed data nodes and
  286. * full index nodes (one per data node, doubled because we always allow enough
  287. * space to write the index twice).
  288. *
  289. * Note, the calculation is pessimistic, which means that most of the time
  290. * UBIFS reports less space than it actually has.
  291. */
  292. static inline long long ubifs_reported_space(const struct ubifs_info *c,
  293. uint64_t free)
  294. {
  295. int divisor, factor;
  296. divisor = UBIFS_MAX_DATA_NODE_SZ + (c->max_idx_node_sz << 1);
  297. factor = UBIFS_MAX_DATA_NODE_SZ - UBIFS_DATA_NODE_SZ;
  298. do_div(free, divisor);
  299. return free * factor;
  300. }
  301. /**
  302. * ubifs_current_time - round current time to time granularity.
  303. * @inode: inode
  304. */
  305. static inline struct timespec ubifs_current_time(struct inode *inode)
  306. {
  307. return (inode->i_sb->s_time_gran < NSEC_PER_SEC) ?
  308. current_fs_time(inode->i_sb) : CURRENT_TIME_SEC;
  309. }
  310. #endif /* __UBIFS_MISC_H__ */