resize.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. if (sbi->mntflag & JFS_INLINELOG) {
  193. /*
  194. * deactivate old inline log
  195. */
  196. lmLogShutdown(log);
  197. /*
  198. * mark on-disk super block for fs in transition;
  199. *
  200. * update on-disk superblock for the new space configuration
  201. * of inline log space and fsck work space descriptors:
  202. * N.B. FS descriptor is NOT updated;
  203. *
  204. * crash recovery:
  205. * logredo(): if FM_EXTENDFS, return to fsck() for cleanup;
  206. * fsck(): if FM_EXTENDFS, reformat inline log and fsck
  207. * workspace from superblock inline log descriptor and fsck
  208. * workspace descriptor;
  209. */
  210. /* read in superblock */
  211. if ((rc = readSuper(sb, &bh)))
  212. goto error_out;
  213. j_sb = (struct jfs_superblock *)bh->b_data;
  214. /* mark extendfs() in progress */
  215. j_sb->s_state |= cpu_to_le32(FM_EXTENDFS);
  216. j_sb->s_xsize = cpu_to_le64(newFSSize);
  217. PXDaddress(&j_sb->s_xfsckpxd, newFSCKAddress);
  218. PXDlength(&j_sb->s_xfsckpxd, newFSCKSize);
  219. PXDaddress(&j_sb->s_xlogpxd, newLogAddress);
  220. PXDlength(&j_sb->s_xlogpxd, newLogSize);
  221. /* synchronously update superblock */
  222. mark_buffer_dirty(bh);
  223. sync_dirty_buffer(bh);
  224. brelse(bh);
  225. /*
  226. * format new inline log synchronously;
  227. *
  228. * crash recovery: if log move in progress,
  229. * reformat log and exit success;
  230. */
  231. if (!log_formatted)
  232. if ((rc = lmLogFormat(log, newLogAddress, newLogSize)))
  233. goto error_out;
  234. /*
  235. * activate new log
  236. */
  237. log->base = newLogAddress;
  238. log->size = newLogSize >> (L2LOGPSIZE - sb->s_blocksize_bits);
  239. if ((rc = lmLogInit(log)))
  240. goto error_out;
  241. }
  242. /*
  243. * extend block allocation map
  244. * ---------------------------
  245. *
  246. * extendfs() for new extension, retry after crash recovery;
  247. *
  248. * note: both logredo() and fsck() rebuild map from
  249. * the bitmap and configuration parameter from superblock
  250. * (disregarding all other control information in the map);
  251. *
  252. * superblock:
  253. * s_size: aggregate size in physical blocks;
  254. */
  255. /*
  256. * compute the new block allocation map configuration
  257. *
  258. * map dinode:
  259. * di_size: map file size in byte;
  260. * di_nblocks: number of blocks allocated for map file;
  261. * di_mapsize: number of blocks in aggregate (covered by map);
  262. * map control page:
  263. * db_mapsize: number of blocks in aggregate (covered by map);
  264. */
  265. newMapSize = newFSSize;
  266. /* number of data pages of new bmap file:
  267. * roundup new size to full dmap page boundary and
  268. * add 1 extra dmap page for next extendfs()
  269. */
  270. t64 = (newMapSize - 1) + BPERDMAP;
  271. newNpages = BLKTODMAPN(t64) + 1;
  272. /*
  273. * extend map from current map (WITHOUT growing mapfile)
  274. *
  275. * map new extension with unmapped part of the last partial
  276. * dmap page, if applicable, and extra page(s) allocated
  277. * at end of bmap by mkfs() or previous extendfs();
  278. */
  279. extendBmap:
  280. /* compute number of blocks requested to extend */
  281. mapSize = bmp->db_mapsize;
  282. XAddress = mapSize; /* eXtension Address */
  283. XSize = newMapSize - mapSize; /* eXtension Size */
  284. old_agsize = bmp->db_agsize; /* We need to know if this changes */
  285. /* compute number of blocks that can be extended by current mapfile */
  286. t64 = dbMapFileSizeToMapSize(ipbmap);
  287. if (mapSize > t64) {
  288. printk(KERN_ERR "jfs_extendfs: mapSize (0x%Lx) > t64 (0x%Lx)\n",
  289. (long long) mapSize, (long long) t64);
  290. rc = -EIO;
  291. goto error_out;
  292. }
  293. nblocks = min(t64 - mapSize, XSize);
  294. /*
  295. * update map pages for new extension:
  296. *
  297. * update/init dmap and bubble up the control hierarchy
  298. * incrementally fold up dmaps into upper levels;
  299. * update bmap control page;
  300. */
  301. if ((rc = dbExtendFS(ipbmap, XAddress, nblocks)))
  302. goto error_out;
  303. /*
  304. * the map now has extended to cover additional nblocks:
  305. * dn_mapsize = oldMapsize + nblocks;
  306. */
  307. /* ipbmap->i_mapsize += nblocks; */
  308. XSize -= nblocks;
  309. /*
  310. * grow map file to cover remaining extension
  311. * and/or one extra dmap page for next extendfs();
  312. *
  313. * allocate new map pages and its backing blocks, and
  314. * update map file xtree
  315. */
  316. /* compute number of data pages of current bmap file */
  317. nPages = ipbmap->i_size >> L2PSIZE;
  318. /* need to grow map file ? */
  319. if (nPages == newNpages)
  320. goto finalizeBmap;
  321. /*
  322. * grow bmap file for the new map pages required:
  323. *
  324. * allocate growth at the start of newly extended region;
  325. * bmap file only grows sequentially, i.e., both data pages
  326. * and possibly xtree index pages may grow in append mode,
  327. * s.t. logredo() can reconstruct pre-extension state
  328. * by washing away bmap file of pages outside s_size boundary;
  329. */
  330. /*
  331. * journal map file growth as if a regular file growth:
  332. * (note: bmap is created with di_mode = IFJOURNAL|IFREG);
  333. *
  334. * journaling of bmap file growth is not required since
  335. * logredo() do/can not use log records of bmap file growth
  336. * but it provides careful write semantics, pmap update, etc.;
  337. */
  338. /* synchronous write of data pages: bmap data pages are
  339. * cached in meta-data cache, and not written out
  340. * by txCommit();
  341. */
  342. filemap_fdatawait(ipbmap->i_mapping);
  343. filemap_fdatawrite(ipbmap->i_mapping);
  344. filemap_fdatawait(ipbmap->i_mapping);
  345. diWriteSpecial(ipbmap, 0);
  346. newPage = nPages; /* first new page number */
  347. xoff = newPage << sbi->l2nbperpage;
  348. xlen = (newNpages - nPages) << sbi->l2nbperpage;
  349. xlen = min(xlen, (int) nblocks) & ~(sbi->nbperpage - 1);
  350. xaddr = XAddress;
  351. tid = txBegin(sb, COMMIT_FORCE);
  352. if ((rc = xtAppend(tid, ipbmap, 0, xoff, nblocks, &xlen, &xaddr, 0))) {
  353. txEnd(tid);
  354. goto error_out;
  355. }
  356. /* update bmap file size */
  357. ipbmap->i_size += xlen << sbi->l2bsize;
  358. inode_add_bytes(ipbmap, xlen << sbi->l2bsize);
  359. iplist[0] = ipbmap;
  360. rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
  361. txEnd(tid);
  362. if (rc)
  363. goto error_out;
  364. /*
  365. * map file has been grown now to cover extension to further out;
  366. * di_size = new map file size;
  367. *
  368. * if huge extension, the previous extension based on previous
  369. * map file size may not have been sufficient to cover whole extension
  370. * (it could have been used up for new map pages),
  371. * but the newly grown map file now covers lot bigger new free space
  372. * available for further extension of map;
  373. */
  374. /* any more blocks to extend ? */
  375. if (XSize)
  376. goto extendBmap;
  377. finalizeBmap:
  378. /* finalize bmap */
  379. dbFinalizeBmap(ipbmap);
  380. /*
  381. * update inode allocation map
  382. * ---------------------------
  383. *
  384. * move iag lists from old to new iag;
  385. * agstart field is not updated for logredo() to reconstruct
  386. * iag lists if system crash occurs.
  387. * (computation of ag number from agstart based on agsize
  388. * will correctly identify the new ag);
  389. */
  390. /* if new AG size the same as old AG size, done! */
  391. if (bmp->db_agsize != old_agsize) {
  392. if ((rc = diExtendFS(ipimap, ipbmap)))
  393. goto error_out;
  394. /* finalize imap */
  395. if ((rc = diSync(ipimap)))
  396. goto error_out;
  397. }
  398. /*
  399. * finalize
  400. * --------
  401. *
  402. * extension is committed when on-disk super block is
  403. * updated with new descriptors: logredo will recover
  404. * crash before it to pre-extension state;
  405. */
  406. /* sync log to skip log replay of bmap file growth transaction; */
  407. /* lmLogSync(log, 1); */
  408. /*
  409. * synchronous write bmap global control page;
  410. * for crash before completion of write
  411. * logredo() will recover to pre-extendfs state;
  412. * for crash after completion of write,
  413. * logredo() will recover post-extendfs state;
  414. */
  415. if ((rc = dbSync(ipbmap)))
  416. goto error_out;
  417. /*
  418. * copy primary bmap inode to secondary bmap inode
  419. */
  420. ipbmap2 = diReadSpecial(sb, BMAP_I, 1);
  421. if (ipbmap2 == NULL) {
  422. printk(KERN_ERR "jfs_extendfs: diReadSpecial(bmap) failed\n");
  423. goto error_out;
  424. }
  425. memcpy(&JFS_IP(ipbmap2)->i_xtroot, &JFS_IP(ipbmap)->i_xtroot, 288);
  426. ipbmap2->i_size = ipbmap->i_size;
  427. ipbmap2->i_blocks = ipbmap->i_blocks;
  428. diWriteSpecial(ipbmap2, 1);
  429. diFreeSpecial(ipbmap2);
  430. /*
  431. * update superblock
  432. */
  433. if ((rc = readSuper(sb, &bh)))
  434. goto error_out;
  435. j_sb = (struct jfs_superblock *)bh->b_data;
  436. /* mark extendfs() completion */
  437. j_sb->s_state &= cpu_to_le32(~FM_EXTENDFS);
  438. j_sb->s_size = cpu_to_le64(bmp->db_mapsize <<
  439. le16_to_cpu(j_sb->s_l2bfactor));
  440. j_sb->s_agsize = cpu_to_le32(bmp->db_agsize);
  441. /* update inline log space descriptor */
  442. if (sbi->mntflag & JFS_INLINELOG) {
  443. PXDaddress(&(j_sb->s_logpxd), newLogAddress);
  444. PXDlength(&(j_sb->s_logpxd), newLogSize);
  445. }
  446. /* record log's mount serial number */
  447. j_sb->s_logserial = cpu_to_le32(log->serial);
  448. /* update fsck work space descriptor */
  449. PXDaddress(&(j_sb->s_fsckpxd), newFSCKAddress);
  450. PXDlength(&(j_sb->s_fsckpxd), newFSCKSize);
  451. j_sb->s_fscklog = 1;
  452. /* sb->s_fsckloglen remains the same */
  453. /* Update secondary superblock */
  454. bh2 = sb_bread(sb, SUPER2_OFF >> sb->s_blocksize_bits);
  455. if (bh2) {
  456. j_sb2 = (struct jfs_superblock *)bh2->b_data;
  457. memcpy(j_sb2, j_sb, sizeof (struct jfs_superblock));
  458. mark_buffer_dirty(bh);
  459. sync_dirty_buffer(bh2);
  460. brelse(bh2);
  461. }
  462. /* write primary superblock */
  463. mark_buffer_dirty(bh);
  464. sync_dirty_buffer(bh);
  465. brelse(bh);
  466. goto resume;
  467. error_out:
  468. jfs_error(sb, "jfs_extendfs");
  469. resume:
  470. /*
  471. * resume file system transactions
  472. */
  473. txResume(sb);
  474. out:
  475. return rc;
  476. }