resize.c 15 KB

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