Merge with 2.5.75.
[linux-2.6/linux-mips.git] / fs / jfs / resize.c
blobba7cb585f545dcedcf5e9508157a9f3cc2340a91
1 /*
2 * Copyright (c) International Business Machines Corp., 2000-2003
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.
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
19 #include <linux/fs.h>
20 #include <linux/buffer_head.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"
31 #define BITSPERPAGE (PSIZE << 3)
32 #define L2MEGABYTE 20
33 #define MEGABYTE (1 << L2MEGABYTE)
34 #define MEGABYTE32 (MEGABYTE << 5)
36 /* convert block number to bmap file page number */
37 #define BLKTODMAPN(b)\
38 (((b) >> 13) + ((b) >> 23) + ((b) >> 33) + 3 + 1)
41 * jfs_extendfs()
43 * function: extend file system;
45 * |-------------------------------|----------|----------|
46 * file system space fsck inline log
47 * workspace space
49 * input:
50 * new LVSize: in LV blocks (required)
51 * new LogSize: in LV blocks (optional)
52 * new FSSize: in LV blocks (optional)
54 * new configuration:
55 * 1. set new LogSize as specified or default from new LVSize;
56 * 2. compute new FSCKSize from new LVSize;
57 * 3. set new FSSize as MIN(FSSize, LVSize-(LogSize+FSCKSize)) where
58 * assert(new FSSize >= old FSSize),
59 * i.e., file system must not be shrinked;
61 int jfs_extendfs(struct super_block *sb, s64 newLVSize, int newLogSize)
63 int rc = 0;
64 struct jfs_sb_info *sbi = JFS_SBI(sb);
65 struct inode *ipbmap = sbi->ipbmap;
66 struct inode *ipbmap2;
67 struct inode *ipimap = sbi->ipimap;
68 struct jfs_log *log = sbi->log;
69 struct bmap *bmp = sbi->bmap;
70 s64 newLogAddress, newFSCKAddress;
71 int newFSCKSize;
72 s64 newMapSize = 0, mapSize;
73 s64 XAddress, XSize, nblocks, xoff, xaddr, t64;
74 s64 oldLVSize;
75 s64 newFSSize;
76 s64 VolumeSize;
77 int newNpages = 0, nPages, newPage, xlen, t32;
78 int tid;
79 int log_formatted = 0;
80 struct inode *iplist[1];
81 struct jfs_superblock *j_sb, *j_sb2;
82 uint old_agsize;
83 struct buffer_head *bh, *bh2;
85 /* If the volume hasn't grown, get out now */
87 if (sbi->mntflag & JFS_INLINELOG)
88 oldLVSize = addressPXD(&sbi->logpxd) + lengthPXD(&sbi->logpxd);
89 else
90 oldLVSize = addressPXD(&sbi->fsckpxd) +
91 lengthPXD(&sbi->fsckpxd);
93 if (oldLVSize >= newLVSize) {
94 printk(KERN_WARNING
95 "jfs_extendfs: volume hasn't grown, returning\n");
96 goto out;
99 VolumeSize = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
101 if (VolumeSize) {
102 if (newLVSize > VolumeSize) {
103 printk(KERN_WARNING "jfs_extendfs: invalid size\n");
104 rc = -EINVAL;
105 goto out;
107 } else {
108 /* check the device */
109 bh = sb_bread(sb, newLVSize - 1);
110 if (!bh) {
111 printk(KERN_WARNING "jfs_extendfs: invalid size\n");
112 rc = -EINVAL;
113 goto out;
115 bforget(bh);
118 /* Can't extend write-protected drive */
120 if (isReadOnly(ipbmap)) {
121 printk(KERN_WARNING "jfs_extendfs: read-only file system\n");
122 rc = -EROFS;
123 goto out;
127 * reconfigure LV spaces
128 * ---------------------
130 * validate new size, or, if not specified, determine new size
134 * reconfigure inline log space:
136 if ((sbi->mntflag & JFS_INLINELOG)) {
137 if (newLogSize == 0) {
139 * no size specified: default to 1/256 of aggregate
140 * size; rounded up to a megabyte boundary;
142 newLogSize = newLVSize >> 8;
143 t32 = (1 << (20 - sbi->l2bsize)) - 1;
144 newLogSize = (newLogSize + t32) & ~t32;
145 newLogSize =
146 min(newLogSize, MEGABYTE32 >> sbi->l2bsize);
147 } else {
149 * convert the newLogSize to fs blocks.
151 * Since this is given in megabytes, it will always be
152 * an even number of pages.
154 newLogSize = (newLogSize * MEGABYTE) >> sbi->l2bsize;
157 } else
158 newLogSize = 0;
160 newLogAddress = newLVSize - newLogSize;
163 * reconfigure fsck work space:
165 * configure it to the end of the logical volume regardless of
166 * whether file system extends to the end of the aggregate;
167 * Need enough 4k pages to cover:
168 * - 1 bit per block in aggregate rounded up to BPERDMAP boundary
169 * - 1 extra page to handle control page and intermediate level pages
170 * - 50 extra pages for the chkdsk service log
172 t64 = ((newLVSize - newLogSize + BPERDMAP - 1) >> L2BPERDMAP)
173 << L2BPERDMAP;
174 t32 = ((t64 + (BITSPERPAGE - 1)) / BITSPERPAGE) + 1 + 50;
175 newFSCKSize = t32 << sbi->l2nbperpage;
176 newFSCKAddress = newLogAddress - newFSCKSize;
179 * compute new file system space;
181 newFSSize = newLVSize - newLogSize - newFSCKSize;
183 /* file system cannot be shrinked */
184 if (newFSSize < bmp->db_mapsize) {
185 rc = -EINVAL;
186 goto out;
190 * If we're expanding enough that the inline log does not overlap
191 * the old one, we can format the new log before we quiesce the
192 * filesystem.
194 if ((sbi->mntflag & JFS_INLINELOG) && (newLogAddress > oldLVSize)) {
195 if ((rc = lmLogFormat(log, newLogAddress, newLogSize)))
196 goto out;
197 log_formatted = 1;
200 * quiesce file system
202 * (prepare to move the inline log and to prevent map update)
204 * block any new transactions and wait for completion of
205 * all wip transactions and flush modified pages s.t.
206 * on-disk file system is in consistent state and
207 * log is not required for recovery.
209 txQuiesce(sb);
211 if (sbi->mntflag & JFS_INLINELOG) {
213 * deactivate old inline log
215 lmLogShutdown(log);
218 * mark on-disk super block for fs in transition;
220 * update on-disk superblock for the new space configuration
221 * of inline log space and fsck work space descriptors:
222 * N.B. FS descriptor is NOT updated;
224 * crash recovery:
225 * logredo(): if FM_EXTENDFS, return to fsck() for cleanup;
226 * fsck(): if FM_EXTENDFS, reformat inline log and fsck
227 * workspace from superblock inline log descriptor and fsck
228 * workspace descriptor;
231 /* read in superblock */
232 if ((rc = readSuper(sb, &bh)))
233 goto error_out;
234 j_sb = (struct jfs_superblock *)bh->b_data;
236 /* mark extendfs() in progress */
237 j_sb->s_state |= cpu_to_le32(FM_EXTENDFS);
238 j_sb->s_xsize = cpu_to_le64(newFSSize);
239 PXDaddress(&j_sb->s_xfsckpxd, newFSCKAddress);
240 PXDlength(&j_sb->s_xfsckpxd, newFSCKSize);
241 PXDaddress(&j_sb->s_xlogpxd, newLogAddress);
242 PXDlength(&j_sb->s_xlogpxd, newLogSize);
244 /* synchronously update superblock */
245 mark_buffer_dirty(bh);
246 sync_dirty_buffer(bh);
247 brelse(bh);
250 * format new inline log synchronously;
252 * crash recovery: if log move in progress,
253 * reformat log and exit success;
255 if (!log_formatted)
256 if ((rc = lmLogFormat(log, newLogAddress, newLogSize)))
257 goto error_out;
260 * activate new log
262 log->base = newLogAddress;
263 log->size = newLogSize >> (L2LOGPSIZE - sb->s_blocksize_bits);
264 if ((rc = lmLogInit(log)))
265 goto error_out;
269 * extend block allocation map
270 * ---------------------------
272 * extendfs() for new extension, retry after crash recovery;
274 * note: both logredo() and fsck() rebuild map from
275 * the bitmap and configuration parameter from superblock
276 * (disregarding all other control information in the map);
278 * superblock:
279 * s_size: aggregate size in physical blocks;
282 * compute the new block allocation map configuration
284 * map dinode:
285 * di_size: map file size in byte;
286 * di_nblocks: number of blocks allocated for map file;
287 * di_mapsize: number of blocks in aggregate (covered by map);
288 * map control page:
289 * db_mapsize: number of blocks in aggregate (covered by map);
291 newMapSize = newFSSize;
292 /* number of data pages of new bmap file:
293 * roundup new size to full dmap page boundary and
294 * add 1 extra dmap page for next extendfs()
296 t64 = (newMapSize - 1) + BPERDMAP;
297 newNpages = BLKTODMAPN(t64) + 1;
300 * extend map from current map (WITHOUT growing mapfile)
302 * map new extension with unmapped part of the last partial
303 * dmap page, if applicable, and extra page(s) allocated
304 * at end of bmap by mkfs() or previous extendfs();
306 extendBmap:
307 /* compute number of blocks requested to extend */
308 mapSize = bmp->db_mapsize;
309 XAddress = mapSize; /* eXtension Address */
310 XSize = newMapSize - mapSize; /* eXtension Size */
311 old_agsize = bmp->db_agsize; /* We need to know if this changes */
313 /* compute number of blocks that can be extended by current mapfile */
314 t64 = dbMapFileSizeToMapSize(ipbmap);
315 if (mapSize > t64) {
316 printk(KERN_ERR "jfs_extendfs: mapSize (0x%Lx) > t64 (0x%Lx)\n",
317 (long long) mapSize, (long long) t64);
318 rc = -EIO;
319 goto error_out;
321 nblocks = min(t64 - mapSize, XSize);
324 * update map pages for new extension:
326 * update/init dmap and bubble up the control hierarchy
327 * incrementally fold up dmaps into upper levels;
328 * update bmap control page;
330 if ((rc = dbExtendFS(ipbmap, XAddress, nblocks)))
331 goto error_out;
333 * the map now has extended to cover additional nblocks:
334 * dn_mapsize = oldMapsize + nblocks;
336 /* ipbmap->i_mapsize += nblocks; */
337 XSize -= nblocks;
340 * grow map file to cover remaining extension
341 * and/or one extra dmap page for next extendfs();
343 * allocate new map pages and its backing blocks, and
344 * update map file xtree
346 /* compute number of data pages of current bmap file */
347 nPages = ipbmap->i_size >> L2PSIZE;
349 /* need to grow map file ? */
350 if (nPages == newNpages)
351 goto finalizeBmap;
354 * grow bmap file for the new map pages required:
356 * allocate growth at the start of newly extended region;
357 * bmap file only grows sequentially, i.e., both data pages
358 * and possibly xtree index pages may grow in append mode,
359 * s.t. logredo() can reconstruct pre-extension state
360 * by washing away bmap file of pages outside s_size boundary;
363 * journal map file growth as if a regular file growth:
364 * (note: bmap is created with di_mode = IFJOURNAL|IFREG);
366 * journaling of bmap file growth is not required since
367 * logredo() do/can not use log records of bmap file growth
368 * but it provides careful write semantics, pmap update, etc.;
370 /* synchronous write of data pages: bmap data pages are
371 * cached in meta-data cache, and not written out
372 * by txCommit();
374 filemap_fdatawait(ipbmap->i_mapping);
375 filemap_fdatawrite(ipbmap->i_mapping);
376 filemap_fdatawait(ipbmap->i_mapping);
377 diWriteSpecial(ipbmap, 0);
379 newPage = nPages; /* first new page number */
380 xoff = newPage << sbi->l2nbperpage;
381 xlen = (newNpages - nPages) << sbi->l2nbperpage;
382 xlen = min(xlen, (int) nblocks) & ~(sbi->nbperpage - 1);
383 xaddr = XAddress;
385 tid = txBegin(sb, COMMIT_FORCE);
387 if ((rc = xtAppend(tid, ipbmap, 0, xoff, nblocks, &xlen, &xaddr, 0))) {
388 txEnd(tid);
389 goto error_out;
391 /* update bmap file size */
392 ipbmap->i_size += xlen << sbi->l2bsize;
393 ipbmap->i_blocks += LBLK2PBLK(sb, xlen);
395 iplist[0] = ipbmap;
396 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
398 txEnd(tid);
400 if (rc)
401 goto error_out;
404 * map file has been grown now to cover extension to further out;
405 * di_size = new map file size;
407 * if huge extension, the previous extension based on previous
408 * map file size may not have been sufficient to cover whole extension
409 * (it could have been used up for new map pages),
410 * but the newly grown map file now covers lot bigger new free space
411 * available for further extension of map;
413 /* any more blocks to extend ? */
414 if (XSize)
415 goto extendBmap;
417 finalizeBmap:
418 /* finalize bmap */
419 dbFinalizeBmap(ipbmap);
422 * update inode allocation map
423 * ---------------------------
425 * move iag lists from old to new iag;
426 * agstart field is not updated for logredo() to reconstruct
427 * iag lists if system crash occurs.
428 * (computation of ag number from agstart based on agsize
429 * will correctly identify the new ag);
431 /* if new AG size the same as old AG size, done! */
432 if (bmp->db_agsize != old_agsize) {
433 if ((rc = diExtendFS(ipimap, ipbmap)))
434 goto error_out;
436 /* finalize imap */
437 if ((rc = diSync(ipimap)))
438 goto error_out;
442 * finalize
443 * --------
445 * extension is committed when on-disk super block is
446 * updated with new descriptors: logredo will recover
447 * crash before it to pre-extension state;
450 /* sync log to skip log replay of bmap file growth transaction; */
451 /* lmLogSync(log, 1); */
454 * synchronous write bmap global control page;
455 * for crash before completion of write
456 * logredo() will recover to pre-extendfs state;
457 * for crash after completion of write,
458 * logredo() will recover post-extendfs state;
460 if ((rc = dbSync(ipbmap)))
461 goto error_out;
464 * copy primary bmap inode to secondary bmap inode
467 ipbmap2 = diReadSpecial(sb, BMAP_I, 1);
468 if (ipbmap2 == NULL) {
469 printk(KERN_ERR "jfs_extendfs: diReadSpecial(bmap) failed\n");
470 goto error_out;
472 memcpy(&JFS_IP(ipbmap2)->i_xtroot, &JFS_IP(ipbmap)->i_xtroot, 288);
473 ipbmap2->i_size = ipbmap->i_size;
474 ipbmap2->i_blocks = ipbmap->i_blocks;
476 diWriteSpecial(ipbmap2, 1);
477 diFreeSpecial(ipbmap2);
480 * update superblock
482 if ((rc = readSuper(sb, &bh)))
483 goto error_out;
484 j_sb = (struct jfs_superblock *)bh->b_data;
486 /* mark extendfs() completion */
487 j_sb->s_state &= cpu_to_le32(~FM_EXTENDFS);
488 j_sb->s_size = cpu_to_le64(bmp->db_mapsize <<
489 le16_to_cpu(j_sb->s_l2bfactor));
490 j_sb->s_agsize = cpu_to_le32(bmp->db_agsize);
492 /* update inline log space descriptor */
493 if (sbi->mntflag & JFS_INLINELOG) {
494 PXDaddress(&(j_sb->s_logpxd), newLogAddress);
495 PXDlength(&(j_sb->s_logpxd), newLogSize);
498 /* record log's mount serial number */
499 j_sb->s_logserial = cpu_to_le32(log->serial);
501 /* update fsck work space descriptor */
502 PXDaddress(&(j_sb->s_fsckpxd), newFSCKAddress);
503 PXDlength(&(j_sb->s_fsckpxd), newFSCKSize);
504 j_sb->s_fscklog = 1;
505 /* sb->s_fsckloglen remains the same */
507 /* Update secondary superblock */
508 bh2 = sb_bread(sb, SUPER2_OFF >> sb->s_blocksize_bits);
509 if (bh2) {
510 j_sb2 = (struct jfs_superblock *)bh2->b_data;
511 memcpy(j_sb2, j_sb, sizeof (struct jfs_superblock));
513 mark_buffer_dirty(bh);
514 sync_dirty_buffer(bh2);
515 brelse(bh2);
518 /* write primary superblock */
519 mark_buffer_dirty(bh);
520 sync_dirty_buffer(bh);
521 brelse(bh);
523 goto resume;
525 error_out:
526 updateSuper(sb, FM_DIRTY);
528 resume:
530 * resume file system transactions
532 txResume(sb);
534 out:
535 return rc;