resize.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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/buffer_head.h>
  7. #include <linux/quotaops.h>
  8. #include <linux/blkdev.h>
  9. #include "jfs_incore.h"
  10. #include "jfs_filsys.h"
  11. #include "jfs_metapage.h"
  12. #include "jfs_dinode.h"
  13. #include "jfs_imap.h"
  14. #include "jfs_dmap.h"
  15. #include "jfs_superblock.h"
  16. #include "jfs_txnmgr.h"
  17. #include "jfs_debug.h"
  18. #define BITSPERPAGE (PSIZE << 3)
  19. #define L2MEGABYTE 20
  20. #define MEGABYTE (1 << L2MEGABYTE)
  21. #define MEGABYTE32 (MEGABYTE << 5)
  22. /* convert block number to bmap file page number */
  23. #define BLKTODMAPN(b)\
  24. (((b) >> 13) + ((b) >> 23) + ((b) >> 33) + 3 + 1)
  25. /*
  26. * jfs_extendfs()
  27. *
  28. * function: extend file system;
  29. *
  30. * |-------------------------------|----------|----------|
  31. * file system space fsck inline log
  32. * workspace space
  33. *
  34. * input:
  35. * new LVSize: in LV blocks (required)
  36. * new LogSize: in LV blocks (optional)
  37. * new FSSize: in LV blocks (optional)
  38. *
  39. * new configuration:
  40. * 1. set new LogSize as specified or default from new LVSize;
  41. * 2. compute new FSCKSize from new LVSize;
  42. * 3. set new FSSize as MIN(FSSize, LVSize-(LogSize+FSCKSize)) where
  43. * assert(new FSSize >= old FSSize),
  44. * i.e., file system must not be shrunk;
  45. */
  46. int jfs_extendfs(struct super_block *sb, s64 newLVSize, int newLogSize)
  47. {
  48. int rc = 0;
  49. struct jfs_sb_info *sbi = JFS_SBI(sb);
  50. struct inode *ipbmap = sbi->ipbmap;
  51. struct inode *ipbmap2;
  52. struct inode *ipimap = sbi->ipimap;
  53. struct jfs_log *log = sbi->log;
  54. struct bmap *bmp = sbi->bmap;
  55. s64 newLogAddress, newFSCKAddress;
  56. int newFSCKSize;
  57. s64 newMapSize = 0, mapSize;
  58. s64 XAddress, XSize, nblocks, xoff, xaddr, t64;
  59. s64 oldLVSize;
  60. s64 newFSSize;
  61. s64 VolumeSize;
  62. int newNpages = 0, nPages, newPage, xlen, t32;
  63. int tid;
  64. int log_formatted = 0;
  65. struct inode *iplist[1];
  66. struct jfs_superblock *j_sb, *j_sb2;
  67. s64 old_agsize;
  68. int agsizechanged = 0;
  69. struct buffer_head *bh, *bh2;
  70. /* If the volume hasn't grown, get out now */
  71. if (sbi->mntflag & JFS_INLINELOG)
  72. oldLVSize = addressPXD(&sbi->logpxd) + lengthPXD(&sbi->logpxd);
  73. else
  74. oldLVSize = addressPXD(&sbi->fsckpxd) +
  75. lengthPXD(&sbi->fsckpxd);
  76. if (oldLVSize >= newLVSize) {
  77. printk(KERN_WARNING
  78. "jfs_extendfs: volume hasn't grown, returning\n");
  79. goto out;
  80. }
  81. VolumeSize = sb_bdev_nr_blocks(sb);
  82. if (VolumeSize) {
  83. if (newLVSize > VolumeSize) {
  84. printk(KERN_WARNING "jfs_extendfs: invalid size\n");
  85. rc = -EINVAL;
  86. goto out;
  87. }
  88. } else {
  89. /* check the device */
  90. bh = sb_bread(sb, newLVSize - 1);
  91. if (!bh) {
  92. printk(KERN_WARNING "jfs_extendfs: invalid size\n");
  93. rc = -EINVAL;
  94. goto out;
  95. }
  96. bforget(bh);
  97. }
  98. /* Can't extend write-protected drive */
  99. if (isReadOnly(ipbmap)) {
  100. printk(KERN_WARNING "jfs_extendfs: read-only file system\n");
  101. rc = -EROFS;
  102. goto out;
  103. }
  104. /*
  105. * reconfigure LV spaces
  106. * ---------------------
  107. *
  108. * validate new size, or, if not specified, determine new size
  109. */
  110. /*
  111. * reconfigure inline log space:
  112. */
  113. if ((sbi->mntflag & JFS_INLINELOG)) {
  114. if (newLogSize == 0) {
  115. /*
  116. * no size specified: default to 1/256 of aggregate
  117. * size; rounded up to a megabyte boundary;
  118. */
  119. newLogSize = newLVSize >> 8;
  120. t32 = (1 << (20 - sbi->l2bsize)) - 1;
  121. newLogSize = (newLogSize + t32) & ~t32;
  122. newLogSize =
  123. min(newLogSize, MEGABYTE32 >> sbi->l2bsize);
  124. } else {
  125. /*
  126. * convert the newLogSize to fs blocks.
  127. *
  128. * Since this is given in megabytes, it will always be
  129. * an even number of pages.
  130. */
  131. newLogSize = (newLogSize * MEGABYTE) >> sbi->l2bsize;
  132. }
  133. } else
  134. newLogSize = 0;
  135. newLogAddress = newLVSize - newLogSize;
  136. /*
  137. * reconfigure fsck work space:
  138. *
  139. * configure it to the end of the logical volume regardless of
  140. * whether file system extends to the end of the aggregate;
  141. * Need enough 4k pages to cover:
  142. * - 1 bit per block in aggregate rounded up to BPERDMAP boundary
  143. * - 1 extra page to handle control page and intermediate level pages
  144. * - 50 extra pages for the chkdsk service log
  145. */
  146. t64 = ((newLVSize - newLogSize + BPERDMAP - 1) >> L2BPERDMAP)
  147. << L2BPERDMAP;
  148. t32 = DIV_ROUND_UP(t64, BITSPERPAGE) + 1 + 50;
  149. newFSCKSize = t32 << sbi->l2nbperpage;
  150. newFSCKAddress = newLogAddress - newFSCKSize;
  151. /*
  152. * compute new file system space;
  153. */
  154. newFSSize = newLVSize - newLogSize - newFSCKSize;
  155. /* file system cannot be shrunk */
  156. if (newFSSize < bmp->db_mapsize) {
  157. rc = -EINVAL;
  158. goto out;
  159. }
  160. /*
  161. * If we're expanding enough that the inline log does not overlap
  162. * the old one, we can format the new log before we quiesce the
  163. * filesystem.
  164. */
  165. if ((sbi->mntflag & JFS_INLINELOG) && (newLogAddress > oldLVSize)) {
  166. if ((rc = lmLogFormat(log, newLogAddress, newLogSize)))
  167. goto out;
  168. log_formatted = 1;
  169. }
  170. /*
  171. * quiesce file system
  172. *
  173. * (prepare to move the inline log and to prevent map update)
  174. *
  175. * block any new transactions and wait for completion of
  176. * all wip transactions and flush modified pages s.t.
  177. * on-disk file system is in consistent state and
  178. * log is not required for recovery.
  179. */
  180. txQuiesce(sb);
  181. /* Reset size of direct inode */
  182. sbi->direct_inode->i_size = bdev_nr_bytes(sb->s_bdev);
  183. if (sbi->mntflag & JFS_INLINELOG) {
  184. /*
  185. * deactivate old inline log
  186. */
  187. lmLogShutdown(log);
  188. /*
  189. * mark on-disk super block for fs in transition;
  190. *
  191. * update on-disk superblock for the new space configuration
  192. * of inline log space and fsck work space descriptors:
  193. * N.B. FS descriptor is NOT updated;
  194. *
  195. * crash recovery:
  196. * logredo(): if FM_EXTENDFS, return to fsck() for cleanup;
  197. * fsck(): if FM_EXTENDFS, reformat inline log and fsck
  198. * workspace from superblock inline log descriptor and fsck
  199. * workspace descriptor;
  200. */
  201. /* read in superblock */
  202. if ((rc = readSuper(sb, &bh)))
  203. goto error_out;
  204. j_sb = (struct jfs_superblock *)bh->b_data;
  205. /* mark extendfs() in progress */
  206. j_sb->s_state |= cpu_to_le32(FM_EXTENDFS);
  207. j_sb->s_xsize = cpu_to_le64(newFSSize);
  208. PXDaddress(&j_sb->s_xfsckpxd, newFSCKAddress);
  209. PXDlength(&j_sb->s_xfsckpxd, newFSCKSize);
  210. PXDaddress(&j_sb->s_xlogpxd, newLogAddress);
  211. PXDlength(&j_sb->s_xlogpxd, newLogSize);
  212. /* synchronously update superblock */
  213. mark_buffer_dirty(bh);
  214. sync_dirty_buffer(bh);
  215. brelse(bh);
  216. /*
  217. * format new inline log synchronously;
  218. *
  219. * crash recovery: if log move in progress,
  220. * reformat log and exit success;
  221. */
  222. if (!log_formatted)
  223. if ((rc = lmLogFormat(log, newLogAddress, newLogSize)))
  224. goto error_out;
  225. /*
  226. * activate new log
  227. */
  228. log->base = newLogAddress;
  229. log->size = newLogSize >> (L2LOGPSIZE - sb->s_blocksize_bits);
  230. if ((rc = lmLogInit(log)))
  231. goto error_out;
  232. }
  233. /*
  234. * extend block allocation map
  235. * ---------------------------
  236. *
  237. * extendfs() for new extension, retry after crash recovery;
  238. *
  239. * note: both logredo() and fsck() rebuild map from
  240. * the bitmap and configuration parameter from superblock
  241. * (disregarding all other control information in the map);
  242. *
  243. * superblock:
  244. * s_size: aggregate size in physical blocks;
  245. */
  246. /*
  247. * compute the new block allocation map configuration
  248. *
  249. * map dinode:
  250. * di_size: map file size in byte;
  251. * di_nblocks: number of blocks allocated for map file;
  252. * di_mapsize: number of blocks in aggregate (covered by map);
  253. * map control page:
  254. * db_mapsize: number of blocks in aggregate (covered by map);
  255. */
  256. newMapSize = newFSSize;
  257. /* number of data pages of new bmap file:
  258. * roundup new size to full dmap page boundary and
  259. * add 1 extra dmap page for next extendfs()
  260. */
  261. t64 = (newMapSize - 1) + BPERDMAP;
  262. newNpages = BLKTODMAPN(t64) + 1;
  263. /*
  264. * extend map from current map (WITHOUT growing mapfile)
  265. *
  266. * map new extension with unmapped part of the last partial
  267. * dmap page, if applicable, and extra page(s) allocated
  268. * at end of bmap by mkfs() or previous extendfs();
  269. */
  270. extendBmap:
  271. /* compute number of blocks requested to extend */
  272. mapSize = bmp->db_mapsize;
  273. XAddress = mapSize; /* eXtension Address */
  274. XSize = newMapSize - mapSize; /* eXtension Size */
  275. old_agsize = bmp->db_agsize; /* We need to know if this changes */
  276. /* compute number of blocks that can be extended by current mapfile */
  277. t64 = dbMapFileSizeToMapSize(ipbmap);
  278. if (mapSize > t64) {
  279. printk(KERN_ERR "jfs_extendfs: mapSize (0x%Lx) > t64 (0x%Lx)\n",
  280. (long long) mapSize, (long long) t64);
  281. rc = -EIO;
  282. goto error_out;
  283. }
  284. nblocks = min(t64 - mapSize, XSize);
  285. /*
  286. * update map pages for new extension:
  287. *
  288. * update/init dmap and bubble up the control hierarchy
  289. * incrementally fold up dmaps into upper levels;
  290. * update bmap control page;
  291. */
  292. if ((rc = dbExtendFS(ipbmap, XAddress, nblocks)))
  293. goto error_out;
  294. agsizechanged |= (bmp->db_agsize != old_agsize);
  295. /*
  296. * the map now has extended to cover additional nblocks:
  297. * dn_mapsize = oldMapsize + nblocks;
  298. */
  299. /* ipbmap->i_mapsize += nblocks; */
  300. XSize -= nblocks;
  301. /*
  302. * grow map file to cover remaining extension
  303. * and/or one extra dmap page for next extendfs();
  304. *
  305. * allocate new map pages and its backing blocks, and
  306. * update map file xtree
  307. */
  308. /* compute number of data pages of current bmap file */
  309. nPages = ipbmap->i_size >> L2PSIZE;
  310. /* need to grow map file ? */
  311. if (nPages == newNpages)
  312. goto finalizeBmap;
  313. /*
  314. * grow bmap file for the new map pages required:
  315. *
  316. * allocate growth at the start of newly extended region;
  317. * bmap file only grows sequentially, i.e., both data pages
  318. * and possibly xtree index pages may grow in append mode,
  319. * s.t. logredo() can reconstruct pre-extension state
  320. * by washing away bmap file of pages outside s_size boundary;
  321. */
  322. /*
  323. * journal map file growth as if a regular file growth:
  324. * (note: bmap is created with di_mode = IFJOURNAL|IFREG);
  325. *
  326. * journaling of bmap file growth is not required since
  327. * logredo() do/can not use log records of bmap file growth
  328. * but it provides careful write semantics, pmap update, etc.;
  329. */
  330. /* synchronous write of data pages: bmap data pages are
  331. * cached in meta-data cache, and not written out
  332. * by txCommit();
  333. */
  334. rc = filemap_fdatawait(ipbmap->i_mapping);
  335. if (rc)
  336. goto error_out;
  337. rc = filemap_write_and_wait(ipbmap->i_mapping);
  338. if (rc)
  339. goto error_out;
  340. diWriteSpecial(ipbmap, 0);
  341. newPage = nPages; /* first new page number */
  342. xoff = newPage << sbi->l2nbperpage;
  343. xlen = (newNpages - nPages) << sbi->l2nbperpage;
  344. xlen = min(xlen, (int) nblocks) & ~(sbi->nbperpage - 1);
  345. xaddr = XAddress;
  346. tid = txBegin(sb, COMMIT_FORCE);
  347. if ((rc = xtAppend(tid, ipbmap, 0, xoff, nblocks, &xlen, &xaddr, 0))) {
  348. txEnd(tid);
  349. goto error_out;
  350. }
  351. /* update bmap file size */
  352. ipbmap->i_size += xlen << sbi->l2bsize;
  353. inode_add_bytes(ipbmap, xlen << sbi->l2bsize);
  354. iplist[0] = ipbmap;
  355. rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
  356. txEnd(tid);
  357. if (rc)
  358. goto error_out;
  359. /*
  360. * map file has been grown now to cover extension to further out;
  361. * di_size = new map file size;
  362. *
  363. * if huge extension, the previous extension based on previous
  364. * map file size may not have been sufficient to cover whole extension
  365. * (it could have been used up for new map pages),
  366. * but the newly grown map file now covers lot bigger new free space
  367. * available for further extension of map;
  368. */
  369. /* any more blocks to extend ? */
  370. if (XSize)
  371. goto extendBmap;
  372. finalizeBmap:
  373. /* finalize bmap */
  374. dbFinalizeBmap(ipbmap);
  375. /*
  376. * update inode allocation map
  377. * ---------------------------
  378. *
  379. * move iag lists from old to new iag;
  380. * agstart field is not updated for logredo() to reconstruct
  381. * iag lists if system crash occurs.
  382. * (computation of ag number from agstart based on agsize
  383. * will correctly identify the new ag);
  384. */
  385. /* if new AG size the same as old AG size, done! */
  386. if (agsizechanged) {
  387. if ((rc = diExtendFS(ipimap, ipbmap)))
  388. goto error_out;
  389. /* finalize imap */
  390. if ((rc = diSync(ipimap)))
  391. goto error_out;
  392. }
  393. /*
  394. * finalize
  395. * --------
  396. *
  397. * extension is committed when on-disk super block is
  398. * updated with new descriptors: logredo will recover
  399. * crash before it to pre-extension state;
  400. */
  401. /* sync log to skip log replay of bmap file growth transaction; */
  402. /* lmLogSync(log, 1); */
  403. /*
  404. * synchronous write bmap global control page;
  405. * for crash before completion of write
  406. * logredo() will recover to pre-extendfs state;
  407. * for crash after completion of write,
  408. * logredo() will recover post-extendfs state;
  409. */
  410. if ((rc = dbSync(ipbmap)))
  411. goto error_out;
  412. /*
  413. * copy primary bmap inode to secondary bmap inode
  414. */
  415. ipbmap2 = diReadSpecial(sb, BMAP_I, 1);
  416. if (ipbmap2 == NULL) {
  417. printk(KERN_ERR "jfs_extendfs: diReadSpecial(bmap) failed\n");
  418. goto error_out;
  419. }
  420. memcpy(&JFS_IP(ipbmap2)->i_xtroot, &JFS_IP(ipbmap)->i_xtroot, 288);
  421. ipbmap2->i_size = ipbmap->i_size;
  422. ipbmap2->i_blocks = ipbmap->i_blocks;
  423. diWriteSpecial(ipbmap2, 1);
  424. diFreeSpecial(ipbmap2);
  425. /*
  426. * update superblock
  427. */
  428. if ((rc = readSuper(sb, &bh)))
  429. goto error_out;
  430. j_sb = (struct jfs_superblock *)bh->b_data;
  431. /* mark extendfs() completion */
  432. j_sb->s_state &= cpu_to_le32(~FM_EXTENDFS);
  433. j_sb->s_size = cpu_to_le64(bmp->db_mapsize <<
  434. le16_to_cpu(j_sb->s_l2bfactor));
  435. j_sb->s_agsize = cpu_to_le32(bmp->db_agsize);
  436. /* update inline log space descriptor */
  437. if (sbi->mntflag & JFS_INLINELOG) {
  438. PXDaddress(&(j_sb->s_logpxd), newLogAddress);
  439. PXDlength(&(j_sb->s_logpxd), newLogSize);
  440. }
  441. /* record log's mount serial number */
  442. j_sb->s_logserial = cpu_to_le32(log->serial);
  443. /* update fsck work space descriptor */
  444. PXDaddress(&(j_sb->s_fsckpxd), newFSCKAddress);
  445. PXDlength(&(j_sb->s_fsckpxd), newFSCKSize);
  446. j_sb->s_fscklog = 1;
  447. /* sb->s_fsckloglen remains the same */
  448. /* Update secondary superblock */
  449. bh2 = sb_bread(sb, SUPER2_OFF >> sb->s_blocksize_bits);
  450. if (bh2) {
  451. j_sb2 = (struct jfs_superblock *)bh2->b_data;
  452. memcpy(j_sb2, j_sb, sizeof (struct jfs_superblock));
  453. mark_buffer_dirty(bh);
  454. sync_dirty_buffer(bh2);
  455. brelse(bh2);
  456. }
  457. /* write primary superblock */
  458. mark_buffer_dirty(bh);
  459. sync_dirty_buffer(bh);
  460. brelse(bh);
  461. goto resume;
  462. error_out:
  463. jfs_error(sb, "\n");
  464. resume:
  465. /*
  466. * resume file system transactions
  467. */
  468. txResume(sb);
  469. out:
  470. return rc;
  471. }