jfs_extent.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) International Business Machines Corp., 2000-2004
  4. */
  5. #include <linux/fs.h>
  6. #include <linux/quotaops.h>
  7. #include "jfs_incore.h"
  8. #include "jfs_inode.h"
  9. #include "jfs_superblock.h"
  10. #include "jfs_dmap.h"
  11. #include "jfs_extent.h"
  12. #include "jfs_debug.h"
  13. /*
  14. * forward references
  15. */
  16. static int extBalloc(struct inode *, s64, s64 *, s64 *);
  17. static s64 extRoundDown(s64 nb);
  18. #define DPD(a) (printk("(a): %d\n",(a)))
  19. #define DPC(a) (printk("(a): %c\n",(a)))
  20. #define DPL1(a) \
  21. { \
  22. if ((a) >> 32) \
  23. printk("(a): %x%08x ",(a)); \
  24. else \
  25. printk("(a): %x ",(a) << 32); \
  26. }
  27. #define DPL(a) \
  28. { \
  29. if ((a) >> 32) \
  30. printk("(a): %x%08x\n",(a)); \
  31. else \
  32. printk("(a): %x\n",(a) << 32); \
  33. }
  34. #define DPD1(a) (printk("(a): %d ",(a)))
  35. #define DPX(a) (printk("(a): %08x\n",(a)))
  36. #define DPX1(a) (printk("(a): %08x ",(a)))
  37. #define DPS(a) (printk("%s\n",(a)))
  38. #define DPE(a) (printk("\nENTERING: %s\n",(a)))
  39. #define DPE1(a) (printk("\nENTERING: %s",(a)))
  40. #define DPS1(a) (printk(" %s ",(a)))
  41. /*
  42. * NAME: extAlloc()
  43. *
  44. * FUNCTION: allocate an extent for a specified page range within a
  45. * file.
  46. *
  47. * PARAMETERS:
  48. * ip - the inode of the file.
  49. * xlen - requested extent length.
  50. * pno - the starting page number with the file.
  51. * xp - pointer to an xad. on entry, xad describes an
  52. * extent that is used as an allocation hint if the
  53. * xaddr of the xad is non-zero. on successful exit,
  54. * the xad describes the newly allocated extent.
  55. * abnr - bool indicating whether the newly allocated extent
  56. * should be marked as allocated but not recorded.
  57. *
  58. * RETURN VALUES:
  59. * 0 - success
  60. * -EIO - i/o error.
  61. * -ENOSPC - insufficient disk resources.
  62. */
  63. int
  64. extAlloc(struct inode *ip, s64 xlen, s64 pno, xad_t * xp, bool abnr)
  65. {
  66. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  67. s64 nxlen, nxaddr, xoff, hint, xaddr = 0;
  68. int rc;
  69. int xflag;
  70. /* This blocks if we are low on resources */
  71. txBeginAnon(ip->i_sb);
  72. /* Avoid race with jfs_commit_inode() */
  73. mutex_lock(&JFS_IP(ip)->commit_mutex);
  74. /* validate extent length */
  75. if (xlen > MAXXLEN)
  76. xlen = MAXXLEN;
  77. /* get the page's starting extent offset */
  78. xoff = pno << sbi->l2nbperpage;
  79. /* check if an allocation hint was provided */
  80. if ((hint = addressXAD(xp))) {
  81. /* get the size of the extent described by the hint */
  82. nxlen = lengthXAD(xp);
  83. /* check if the hint is for the portion of the file
  84. * immediately previous to the current allocation
  85. * request and if hint extent has the same abnr
  86. * value as the current request. if so, we can
  87. * extend the hint extent to include the current
  88. * extent if we can allocate the blocks immediately
  89. * following the hint extent.
  90. */
  91. if (offsetXAD(xp) + nxlen == xoff &&
  92. abnr == ((xp->flag & XAD_NOTRECORDED) ? true : false))
  93. xaddr = hint + nxlen;
  94. /* adjust the hint to the last block of the extent */
  95. hint += (nxlen - 1);
  96. }
  97. /* allocate the disk blocks for the extent. initially, extBalloc()
  98. * will try to allocate disk blocks for the requested size (xlen).
  99. * if this fails (xlen contiguous free blocks not available), it'll
  100. * try to allocate a smaller number of blocks (producing a smaller
  101. * extent), with this smaller number of blocks consisting of the
  102. * requested number of blocks rounded down to the next smaller
  103. * power of 2 number (i.e. 16 -> 8). it'll continue to round down
  104. * and retry the allocation until the number of blocks to allocate
  105. * is smaller than the number of blocks per page.
  106. */
  107. nxlen = xlen;
  108. if ((rc = extBalloc(ip, hint ? hint : INOHINT(ip), &nxlen, &nxaddr))) {
  109. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  110. return (rc);
  111. }
  112. /* Allocate blocks to quota. */
  113. rc = dquot_alloc_block(ip, nxlen);
  114. if (rc) {
  115. dbFree(ip, nxaddr, (s64) nxlen);
  116. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  117. return rc;
  118. }
  119. /* determine the value of the extent flag */
  120. xflag = abnr ? XAD_NOTRECORDED : 0;
  121. /* if we can extend the hint extent to cover the current request,
  122. * extend it. otherwise, insert a new extent to
  123. * cover the current request.
  124. */
  125. if (xaddr && xaddr == nxaddr)
  126. rc = xtExtend(0, ip, xoff, (int) nxlen, 0);
  127. else
  128. rc = xtInsert(0, ip, xflag, xoff, (int) nxlen, &nxaddr, 0);
  129. /* if the extend or insert failed,
  130. * free the newly allocated blocks and return the error.
  131. */
  132. if (rc) {
  133. dbFree(ip, nxaddr, nxlen);
  134. dquot_free_block(ip, nxlen);
  135. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  136. return (rc);
  137. }
  138. /* set the results of the extent allocation */
  139. XADaddress(xp, nxaddr);
  140. XADlength(xp, nxlen);
  141. XADoffset(xp, xoff);
  142. xp->flag = xflag;
  143. mark_inode_dirty(ip);
  144. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  145. /*
  146. * COMMIT_SyncList flags an anonymous tlock on page that is on
  147. * sync list.
  148. * We need to commit the inode to get the page written to the disk.
  149. */
  150. if (test_and_clear_cflag(COMMIT_Synclist,ip))
  151. jfs_commit_inode(ip, 0);
  152. return (0);
  153. }
  154. /*
  155. * NAME: extHint()
  156. *
  157. * FUNCTION: produce an extent allocation hint for a file offset.
  158. *
  159. * PARAMETERS:
  160. * ip - the inode of the file.
  161. * offset - file offset for which the hint is needed.
  162. * xp - pointer to the xad that is to be filled in with
  163. * the hint.
  164. *
  165. * RETURN VALUES:
  166. * 0 - success
  167. * -EIO - i/o error.
  168. */
  169. int extHint(struct inode *ip, s64 offset, xad_t * xp)
  170. {
  171. struct super_block *sb = ip->i_sb;
  172. int nbperpage = JFS_SBI(sb)->nbperpage;
  173. s64 prev;
  174. int rc = 0;
  175. s64 xaddr;
  176. int xlen;
  177. int xflag;
  178. /* init the hint as "no hint provided" */
  179. XADaddress(xp, 0);
  180. /* determine the starting extent offset of the page previous
  181. * to the page containing the offset.
  182. */
  183. prev = ((offset & ~POFFSET) >> JFS_SBI(sb)->l2bsize) - nbperpage;
  184. /* if the offset is in the first page of the file, no hint provided.
  185. */
  186. if (prev < 0)
  187. goto out;
  188. rc = xtLookup(ip, prev, nbperpage, &xflag, &xaddr, &xlen, 0);
  189. if ((rc == 0) && xlen) {
  190. if (xlen != nbperpage) {
  191. jfs_error(ip->i_sb, "corrupt xtree\n");
  192. rc = -EIO;
  193. }
  194. XADaddress(xp, xaddr);
  195. XADlength(xp, xlen);
  196. XADoffset(xp, prev);
  197. /*
  198. * only preserve the abnr flag within the xad flags
  199. * of the returned hint.
  200. */
  201. xp->flag = xflag & XAD_NOTRECORDED;
  202. } else
  203. rc = 0;
  204. out:
  205. return (rc);
  206. }
  207. /*
  208. * NAME: extRecord()
  209. *
  210. * FUNCTION: change a page with a file from not recorded to recorded.
  211. *
  212. * PARAMETERS:
  213. * ip - inode of the file.
  214. * cp - cbuf of the file page.
  215. *
  216. * RETURN VALUES:
  217. * 0 - success
  218. * -EIO - i/o error.
  219. * -ENOSPC - insufficient disk resources.
  220. */
  221. int extRecord(struct inode *ip, xad_t * xp)
  222. {
  223. int rc;
  224. txBeginAnon(ip->i_sb);
  225. mutex_lock(&JFS_IP(ip)->commit_mutex);
  226. /* update the extent */
  227. rc = xtUpdate(0, ip, xp);
  228. mutex_unlock(&JFS_IP(ip)->commit_mutex);
  229. return rc;
  230. }
  231. /*
  232. * NAME: extBalloc()
  233. *
  234. * FUNCTION: allocate disk blocks to form an extent.
  235. *
  236. * initially, we will try to allocate disk blocks for the
  237. * requested size (nblocks). if this fails (nblocks
  238. * contiguous free blocks not available), we'll try to allocate
  239. * a smaller number of blocks (producing a smaller extent), with
  240. * this smaller number of blocks consisting of the requested
  241. * number of blocks rounded down to the next smaller power of 2
  242. * number (i.e. 16 -> 8). we'll continue to round down and
  243. * retry the allocation until the number of blocks to allocate
  244. * is smaller than the number of blocks per page.
  245. *
  246. * PARAMETERS:
  247. * ip - the inode of the file.
  248. * hint - disk block number to be used as an allocation hint.
  249. * *nblocks - pointer to an s64 value. on entry, this value specifies
  250. * the desired number of block to be allocated. on successful
  251. * exit, this value is set to the number of blocks actually
  252. * allocated.
  253. * blkno - pointer to a block address that is filled in on successful
  254. * return with the starting block number of the newly
  255. * allocated block range.
  256. *
  257. * RETURN VALUES:
  258. * 0 - success
  259. * -EIO - i/o error.
  260. * -ENOSPC - insufficient disk resources.
  261. */
  262. static int
  263. extBalloc(struct inode *ip, s64 hint, s64 * nblocks, s64 * blkno)
  264. {
  265. struct jfs_inode_info *ji = JFS_IP(ip);
  266. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  267. s64 nb, nblks, daddr, max;
  268. int rc, nbperpage = sbi->nbperpage;
  269. struct bmap *bmp = sbi->bmap;
  270. int ag;
  271. /* get the number of blocks to initially attempt to allocate.
  272. * we'll first try the number of blocks requested unless this
  273. * number is greater than the maximum number of contiguous free
  274. * blocks in the map. in that case, we'll start off with the
  275. * maximum free.
  276. */
  277. /* give up if no space left */
  278. if (bmp->db_maxfreebud == -1)
  279. return -ENOSPC;
  280. max = (s64) 1 << bmp->db_maxfreebud;
  281. if (*nblocks >= max && *nblocks > nbperpage)
  282. nb = nblks = (max > nbperpage) ? max : nbperpage;
  283. else
  284. nb = nblks = *nblocks;
  285. /* try to allocate blocks */
  286. while ((rc = dbAlloc(ip, hint, nb, &daddr)) != 0) {
  287. /* if something other than an out of space error,
  288. * stop and return this error.
  289. */
  290. if (rc != -ENOSPC)
  291. return (rc);
  292. /* decrease the allocation request size */
  293. nb = min(nblks, extRoundDown(nb));
  294. /* give up if we cannot cover a page */
  295. if (nb < nbperpage)
  296. return (rc);
  297. }
  298. *nblocks = nb;
  299. *blkno = daddr;
  300. if (S_ISREG(ip->i_mode) && (ji->fileset == FILESYSTEM_I)) {
  301. ag = BLKTOAG(daddr, sbi);
  302. spin_lock_irq(&ji->ag_lock);
  303. if (ji->active_ag == -1) {
  304. atomic_inc(&bmp->db_active[ag]);
  305. ji->active_ag = ag;
  306. } else if (ji->active_ag != ag) {
  307. atomic_dec(&bmp->db_active[ji->active_ag]);
  308. atomic_inc(&bmp->db_active[ag]);
  309. ji->active_ag = ag;
  310. }
  311. spin_unlock_irq(&ji->ag_lock);
  312. }
  313. return (0);
  314. }
  315. /*
  316. * NAME: extRoundDown()
  317. *
  318. * FUNCTION: round down a specified number of blocks to the next
  319. * smallest power of 2 number.
  320. *
  321. * PARAMETERS:
  322. * nb - the inode of the file.
  323. *
  324. * RETURN VALUES:
  325. * next smallest power of 2 number.
  326. */
  327. static s64 extRoundDown(s64 nb)
  328. {
  329. int i;
  330. u64 m, k;
  331. for (i = 0, m = (u64) 1 << 63; i < 64; i++, m >>= 1) {
  332. if (m & nb)
  333. break;
  334. }
  335. i = 63 - i;
  336. k = (u64) 1 << i;
  337. k = ((k - 1) & nb) ? k : k >> 1;
  338. return (k);
  339. }