xref: /openbmc/linux/fs/jfs/resize.c (revision 9e255e2b)
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  */
50 int 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 = i_size_read(sb->s_bdev->bd_inode) >> sb->s_blocksize_bits;
90 
91 	if (VolumeSize) {
92 		if (newLVSize > VolumeSize) {
93 			printk(KERN_WARNING "jfs_extendfs: invalid size\n");
94 			rc = -EINVAL;
95 			goto out;
96 		}
97 	} else {
98 		/* check the device */
99 		bh = sb_bread(sb, newLVSize - 1);
100 		if (!bh) {
101 			printk(KERN_WARNING "jfs_extendfs: invalid size\n");
102 			rc = -EINVAL;
103 			goto out;
104 		}
105 		bforget(bh);
106 	}
107 
108 	/* Can't extend write-protected drive */
109 
110 	if (isReadOnly(ipbmap)) {
111 		printk(KERN_WARNING "jfs_extendfs: read-only file system\n");
112 		rc = -EROFS;
113 		goto out;
114 	}
115 
116 	/*
117 	 *	reconfigure LV spaces
118 	 *	---------------------
119 	 *
120 	 * validate new size, or, if not specified, determine new size
121 	 */
122 
123 	/*
124 	 * reconfigure inline log space:
125 	 */
126 	if ((sbi->mntflag & JFS_INLINELOG)) {
127 		if (newLogSize == 0) {
128 			/*
129 			 * no size specified: default to 1/256 of aggregate
130 			 * size; rounded up to a megabyte boundary;
131 			 */
132 			newLogSize = newLVSize >> 8;
133 			t32 = (1 << (20 - sbi->l2bsize)) - 1;
134 			newLogSize = (newLogSize + t32) & ~t32;
135 			newLogSize =
136 			    min(newLogSize, MEGABYTE32 >> sbi->l2bsize);
137 		} else {
138 			/*
139 			 * convert the newLogSize to fs blocks.
140 			 *
141 			 * Since this is given in megabytes, it will always be
142 			 * an even number of pages.
143 			 */
144 			newLogSize = (newLogSize * MEGABYTE) >> sbi->l2bsize;
145 		}
146 
147 	} else
148 		newLogSize = 0;
149 
150 	newLogAddress = newLVSize - newLogSize;
151 
152 	/*
153 	 * reconfigure fsck work space:
154 	 *
155 	 * configure it to the end of the logical volume regardless of
156 	 * whether file system extends to the end of the aggregate;
157 	 * Need enough 4k pages to cover:
158 	 *  - 1 bit per block in aggregate rounded up to BPERDMAP boundary
159 	 *  - 1 extra page to handle control page and intermediate level pages
160 	 *  - 50 extra pages for the chkdsk service log
161 	 */
162 	t64 = ((newLVSize - newLogSize + BPERDMAP - 1) >> L2BPERDMAP)
163 	    << L2BPERDMAP;
164 	t32 = DIV_ROUND_UP(t64, BITSPERPAGE) + 1 + 50;
165 	newFSCKSize = t32 << sbi->l2nbperpage;
166 	newFSCKAddress = newLogAddress - newFSCKSize;
167 
168 	/*
169 	 * compute new file system space;
170 	 */
171 	newFSSize = newLVSize - newLogSize - newFSCKSize;
172 
173 	/* file system cannot be shrunk */
174 	if (newFSSize < bmp->db_mapsize) {
175 		rc = -EINVAL;
176 		goto out;
177 	}
178 
179 	/*
180 	 * If we're expanding enough that the inline log does not overlap
181 	 * the old one, we can format the new log before we quiesce the
182 	 * filesystem.
183 	 */
184 	if ((sbi->mntflag & JFS_INLINELOG) && (newLogAddress > oldLVSize)) {
185 		if ((rc = lmLogFormat(log, newLogAddress, newLogSize)))
186 			goto out;
187 		log_formatted = 1;
188 	}
189 	/*
190 	 *	quiesce file system
191 	 *
192 	 * (prepare to move the inline log and to prevent map update)
193 	 *
194 	 * block any new transactions and wait for completion of
195 	 * all wip transactions and flush modified pages s.t.
196 	 * on-disk file system is in consistent state and
197 	 * log is not required for recovery.
198 	 */
199 	txQuiesce(sb);
200 
201 	/* Reset size of direct inode */
202 	sbi->direct_inode->i_size =  i_size_read(sb->s_bdev->bd_inode);
203 
204 	if (sbi->mntflag & JFS_INLINELOG) {
205 		/*
206 		 * deactivate old inline log
207 		 */
208 		lmLogShutdown(log);
209 
210 		/*
211 		 * mark on-disk super block for fs in transition;
212 		 *
213 		 * update on-disk superblock for the new space configuration
214 		 * of inline log space and fsck work space descriptors:
215 		 * N.B. FS descriptor is NOT updated;
216 		 *
217 		 * crash recovery:
218 		 * logredo(): if FM_EXTENDFS, return to fsck() for cleanup;
219 		 * fsck(): if FM_EXTENDFS, reformat inline log and fsck
220 		 * workspace from superblock inline log descriptor and fsck
221 		 * workspace descriptor;
222 		 */
223 
224 		/* read in superblock */
225 		if ((rc = readSuper(sb, &bh)))
226 			goto error_out;
227 		j_sb = (struct jfs_superblock *)bh->b_data;
228 
229 		/* mark extendfs() in progress */
230 		j_sb->s_state |= cpu_to_le32(FM_EXTENDFS);
231 		j_sb->s_xsize = cpu_to_le64(newFSSize);
232 		PXDaddress(&j_sb->s_xfsckpxd, newFSCKAddress);
233 		PXDlength(&j_sb->s_xfsckpxd, newFSCKSize);
234 		PXDaddress(&j_sb->s_xlogpxd, newLogAddress);
235 		PXDlength(&j_sb->s_xlogpxd, newLogSize);
236 
237 		/* synchronously update superblock */
238 		mark_buffer_dirty(bh);
239 		sync_dirty_buffer(bh);
240 		brelse(bh);
241 
242 		/*
243 		 * format new inline log synchronously;
244 		 *
245 		 * crash recovery: if log move in progress,
246 		 * reformat log and exit success;
247 		 */
248 		if (!log_formatted)
249 			if ((rc = lmLogFormat(log, newLogAddress, newLogSize)))
250 				goto error_out;
251 
252 		/*
253 		 * activate new log
254 		 */
255 		log->base = newLogAddress;
256 		log->size = newLogSize >> (L2LOGPSIZE - sb->s_blocksize_bits);
257 		if ((rc = lmLogInit(log)))
258 			goto error_out;
259 	}
260 
261 	/*
262 	 *	extend block allocation map
263 	 *	---------------------------
264 	 *
265 	 * extendfs() for new extension, retry after crash recovery;
266 	 *
267 	 * note: both logredo() and fsck() rebuild map from
268 	 * the bitmap and configuration parameter from superblock
269 	 * (disregarding all other control information in the map);
270 	 *
271 	 * superblock:
272 	 *  s_size: aggregate size in physical blocks;
273 	 */
274 	/*
275 	 *	compute the new block allocation map configuration
276 	 *
277 	 * map dinode:
278 	 *  di_size: map file size in byte;
279 	 *  di_nblocks: number of blocks allocated for map file;
280 	 *  di_mapsize: number of blocks in aggregate (covered by map);
281 	 * map control page:
282 	 *  db_mapsize: number of blocks in aggregate (covered by map);
283 	 */
284 	newMapSize = newFSSize;
285 	/* number of data pages of new bmap file:
286 	 * roundup new size to full dmap page boundary and
287 	 * add 1 extra dmap page for next extendfs()
288 	 */
289 	t64 = (newMapSize - 1) + BPERDMAP;
290 	newNpages = BLKTODMAPN(t64) + 1;
291 
292 	/*
293 	 *	extend map from current map (WITHOUT growing mapfile)
294 	 *
295 	 * map new extension with unmapped part of the last partial
296 	 * dmap page, if applicable, and extra page(s) allocated
297 	 * at end of bmap by mkfs() or previous extendfs();
298 	 */
299       extendBmap:
300 	/* compute number of blocks requested to extend */
301 	mapSize = bmp->db_mapsize;
302 	XAddress = mapSize;	/* eXtension Address */
303 	XSize = newMapSize - mapSize;	/* eXtension Size */
304 	old_agsize = bmp->db_agsize;	/* We need to know if this changes */
305 
306 	/* compute number of blocks that can be extended by current mapfile */
307 	t64 = dbMapFileSizeToMapSize(ipbmap);
308 	if (mapSize > t64) {
309 		printk(KERN_ERR "jfs_extendfs: mapSize (0x%Lx) > t64 (0x%Lx)\n",
310 		       (long long) mapSize, (long long) t64);
311 		rc = -EIO;
312 		goto error_out;
313 	}
314 	nblocks = min(t64 - mapSize, XSize);
315 
316 	/*
317 	 * update map pages for new extension:
318 	 *
319 	 * update/init dmap and bubble up the control hierarchy
320 	 * incrementally fold up dmaps into upper levels;
321 	 * update bmap control page;
322 	 */
323 	if ((rc = dbExtendFS(ipbmap, XAddress, nblocks)))
324 		goto error_out;
325 
326 	agsizechanged |= (bmp->db_agsize != old_agsize);
327 
328 	/*
329 	 * the map now has extended to cover additional nblocks:
330 	 * dn_mapsize = oldMapsize + nblocks;
331 	 */
332 	/* ipbmap->i_mapsize += nblocks; */
333 	XSize -= nblocks;
334 
335 	/*
336 	 *	grow map file to cover remaining extension
337 	 *	and/or one extra dmap page for next extendfs();
338 	 *
339 	 * allocate new map pages and its backing blocks, and
340 	 * update map file xtree
341 	 */
342 	/* compute number of data pages of current bmap file */
343 	nPages = ipbmap->i_size >> L2PSIZE;
344 
345 	/* need to grow map file ? */
346 	if (nPages == newNpages)
347 		goto finalizeBmap;
348 
349 	/*
350 	 * grow bmap file for the new map pages required:
351 	 *
352 	 * allocate growth at the start of newly extended region;
353 	 * bmap file only grows sequentially, i.e., both data pages
354 	 * and possibly xtree index pages may grow in append mode,
355 	 * s.t. logredo() can reconstruct pre-extension state
356 	 * by washing away bmap file of pages outside s_size boundary;
357 	 */
358 	/*
359 	 * journal map file growth as if a regular file growth:
360 	 * (note: bmap is created with di_mode = IFJOURNAL|IFREG);
361 	 *
362 	 * journaling of bmap file growth is not required since
363 	 * logredo() do/can not use log records of bmap file growth
364 	 * but it provides careful write semantics, pmap update, etc.;
365 	 */
366 	/* synchronous write of data pages: bmap data pages are
367 	 * cached in meta-data cache, and not written out
368 	 * by txCommit();
369 	 */
370 	rc = filemap_fdatawait(ipbmap->i_mapping);
371 	if (rc)
372 		goto error_out;
373 
374 	rc = filemap_write_and_wait(ipbmap->i_mapping);
375 	if (rc)
376 		goto error_out;
377 
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 	inode_add_bytes(ipbmap, xlen << sbi->l2bsize);
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       finalizeBmap:
419 	/* finalize bmap */
420 	dbFinalizeBmap(ipbmap);
421 
422 	/*
423 	 *	update inode allocation map
424 	 *	---------------------------
425 	 *
426 	 * move iag lists from old to new iag;
427 	 * agstart field is not updated for logredo() to reconstruct
428 	 * iag lists if system crash occurs.
429 	 * (computation of ag number from agstart based on agsize
430 	 * will correctly identify the new ag);
431 	 */
432 	/* if new AG size the same as old AG size, done! */
433 	if (agsizechanged) {
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 		sync_dirty_buffer(bh2);
516 		brelse(bh2);
517 	}
518 
519 	/* write primary superblock */
520 	mark_buffer_dirty(bh);
521 	sync_dirty_buffer(bh);
522 	brelse(bh);
523 
524 	goto resume;
525 
526       error_out:
527 	jfs_error(sb, "\n");
528 
529       resume:
530 	/*
531 	 *	resume file system transactions
532 	 */
533 	txResume(sb);
534 
535       out:
536 	return rc;
537 }
538