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