jfs_extent.c 10 KB

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