resize.c 15 KB

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