Linux Audio

Check our new training course

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