xref: /openbmc/linux/fs/jfs/jfs_mount.c (revision 29c37341)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) International Business Machines Corp., 2000-2004
4  */
5 
6 /*
7  * Module: jfs_mount.c
8  *
9  * note: file system in transition to aggregate/fileset:
10  *
11  * file system mount is interpreted as the mount of aggregate,
12  * if not already mounted, and mount of the single/only fileset in
13  * the aggregate;
14  *
15  * a file system/aggregate is represented by an internal inode
16  * (aka mount inode) initialized with aggregate superblock;
17  * each vfs represents a fileset, and points to its "fileset inode
18  * allocation map inode" (aka fileset inode):
19  * (an aggregate itself is structured recursively as a filset:
20  * an internal vfs is constructed and points to its "fileset inode
21  * allocation map inode" (aka aggregate inode) where each inode
22  * represents a fileset inode) so that inode number is mapped to
23  * on-disk inode in uniform way at both aggregate and fileset level;
24  *
25  * each vnode/inode of a fileset is linked to its vfs (to facilitate
26  * per fileset inode operations, e.g., unmount of a fileset, etc.);
27  * each inode points to the mount inode (to facilitate access to
28  * per aggregate information, e.g., block size, etc.) as well as
29  * its file set inode.
30  *
31  *   aggregate
32  *   ipmnt
33  *   mntvfs -> fileset ipimap+ -> aggregate ipbmap -> aggregate ipaimap;
34  *             fileset vfs     -> vp(1) <-> ... <-> vp(n) <->vproot;
35  */
36 
37 #include <linux/fs.h>
38 #include <linux/buffer_head.h>
39 #include <linux/blkdev.h>
40 
41 #include "jfs_incore.h"
42 #include "jfs_filsys.h"
43 #include "jfs_superblock.h"
44 #include "jfs_dmap.h"
45 #include "jfs_imap.h"
46 #include "jfs_metapage.h"
47 #include "jfs_debug.h"
48 
49 
50 /*
51  * forward references
52  */
53 static int chkSuper(struct super_block *);
54 static int logMOUNT(struct super_block *sb);
55 
56 /*
57  * NAME:	jfs_mount(sb)
58  *
59  * FUNCTION:	vfs_mount()
60  *
61  * PARAMETER:	sb	- super block
62  *
63  * RETURN:	-EBUSY	- device already mounted or open for write
64  *		-EBUSY	- cvrdvp already mounted;
65  *		-EBUSY	- mount table full
66  *		-ENOTDIR- cvrdvp not directory on a device mount
67  *		-ENXIO	- device open failure
68  */
69 int jfs_mount(struct super_block *sb)
70 {
71 	int rc = 0;		/* Return code */
72 	struct jfs_sb_info *sbi = JFS_SBI(sb);
73 	struct inode *ipaimap = NULL;
74 	struct inode *ipaimap2 = NULL;
75 	struct inode *ipimap = NULL;
76 	struct inode *ipbmap = NULL;
77 
78 	/*
79 	 * read/validate superblock
80 	 * (initialize mount inode from the superblock)
81 	 */
82 	if ((rc = chkSuper(sb))) {
83 		goto errout20;
84 	}
85 
86 	ipaimap = diReadSpecial(sb, AGGREGATE_I, 0);
87 	if (ipaimap == NULL) {
88 		jfs_err("jfs_mount: Failed to read AGGREGATE_I");
89 		rc = -EIO;
90 		goto errout20;
91 	}
92 	sbi->ipaimap = ipaimap;
93 
94 	jfs_info("jfs_mount: ipaimap:0x%p", ipaimap);
95 
96 	/*
97 	 * initialize aggregate inode allocation map
98 	 */
99 	if ((rc = diMount(ipaimap))) {
100 		jfs_err("jfs_mount: diMount(ipaimap) failed w/rc = %d", rc);
101 		goto errout21;
102 	}
103 
104 	/*
105 	 * open aggregate block allocation map
106 	 */
107 	ipbmap = diReadSpecial(sb, BMAP_I, 0);
108 	if (ipbmap == NULL) {
109 		rc = -EIO;
110 		goto errout22;
111 	}
112 
113 	jfs_info("jfs_mount: ipbmap:0x%p", ipbmap);
114 
115 	sbi->ipbmap = ipbmap;
116 
117 	/*
118 	 * initialize aggregate block allocation map
119 	 */
120 	if ((rc = dbMount(ipbmap))) {
121 		jfs_err("jfs_mount: dbMount failed w/rc = %d", rc);
122 		goto errout22;
123 	}
124 
125 	/*
126 	 * open the secondary aggregate inode allocation map
127 	 *
128 	 * This is a duplicate of the aggregate inode allocation map.
129 	 *
130 	 * hand craft a vfs in the same fashion as we did to read ipaimap.
131 	 * By adding INOSPEREXT (32) to the inode number, we are telling
132 	 * diReadSpecial that we are reading from the secondary aggregate
133 	 * inode table.  This also creates a unique entry in the inode hash
134 	 * table.
135 	 */
136 	if ((sbi->mntflag & JFS_BAD_SAIT) == 0) {
137 		ipaimap2 = diReadSpecial(sb, AGGREGATE_I, 1);
138 		if (!ipaimap2) {
139 			jfs_err("jfs_mount: Failed to read AGGREGATE_I");
140 			rc = -EIO;
141 			goto errout35;
142 		}
143 		sbi->ipaimap2 = ipaimap2;
144 
145 		jfs_info("jfs_mount: ipaimap2:0x%p", ipaimap2);
146 
147 		/*
148 		 * initialize secondary aggregate inode allocation map
149 		 */
150 		if ((rc = diMount(ipaimap2))) {
151 			jfs_err("jfs_mount: diMount(ipaimap2) failed, rc = %d",
152 				rc);
153 			goto errout35;
154 		}
155 	} else
156 		/* Secondary aggregate inode table is not valid */
157 		sbi->ipaimap2 = NULL;
158 
159 	/*
160 	 *	mount (the only/single) fileset
161 	 */
162 	/*
163 	 * open fileset inode allocation map (aka fileset inode)
164 	 */
165 	ipimap = diReadSpecial(sb, FILESYSTEM_I, 0);
166 	if (ipimap == NULL) {
167 		jfs_err("jfs_mount: Failed to read FILESYSTEM_I");
168 		/* open fileset secondary inode allocation map */
169 		rc = -EIO;
170 		goto errout40;
171 	}
172 	jfs_info("jfs_mount: ipimap:0x%p", ipimap);
173 
174 	/* map further access of per fileset inodes by the fileset inode */
175 	sbi->ipimap = ipimap;
176 
177 	/* initialize fileset inode allocation map */
178 	if ((rc = diMount(ipimap))) {
179 		jfs_err("jfs_mount: diMount failed w/rc = %d", rc);
180 		goto errout41;
181 	}
182 
183 	goto out;
184 
185 	/*
186 	 *	unwind on error
187 	 */
188       errout41:		/* close fileset inode allocation map inode */
189 	diFreeSpecial(ipimap);
190 
191       errout40:		/* fileset closed */
192 
193 	/* close secondary aggregate inode allocation map */
194 	if (ipaimap2) {
195 		diUnmount(ipaimap2, 1);
196 		diFreeSpecial(ipaimap2);
197 	}
198 
199       errout35:
200 
201 	/* close aggregate block allocation map */
202 	dbUnmount(ipbmap, 1);
203 	diFreeSpecial(ipbmap);
204 
205       errout22:		/* close aggregate inode allocation map */
206 
207 	diUnmount(ipaimap, 1);
208 
209       errout21:		/* close aggregate inodes */
210 	diFreeSpecial(ipaimap);
211       errout20:		/* aggregate closed */
212 
213       out:
214 
215 	if (rc)
216 		jfs_err("Mount JFS Failure: %d", rc);
217 
218 	return rc;
219 }
220 
221 /*
222  * NAME:	jfs_mount_rw(sb, remount)
223  *
224  * FUNCTION:	Completes read-write mount, or remounts read-only volume
225  *		as read-write
226  */
227 int jfs_mount_rw(struct super_block *sb, int remount)
228 {
229 	struct jfs_sb_info *sbi = JFS_SBI(sb);
230 	int rc;
231 
232 	/*
233 	 * If we are re-mounting a previously read-only volume, we want to
234 	 * re-read the inode and block maps, since fsck.jfs may have updated
235 	 * them.
236 	 */
237 	if (remount) {
238 		if (chkSuper(sb) || (sbi->state != FM_CLEAN))
239 			return -EINVAL;
240 
241 		truncate_inode_pages(sbi->ipimap->i_mapping, 0);
242 		truncate_inode_pages(sbi->ipbmap->i_mapping, 0);
243 		diUnmount(sbi->ipimap, 1);
244 		if ((rc = diMount(sbi->ipimap))) {
245 			jfs_err("jfs_mount_rw: diMount failed!");
246 			return rc;
247 		}
248 
249 		dbUnmount(sbi->ipbmap, 1);
250 		if ((rc = dbMount(sbi->ipbmap))) {
251 			jfs_err("jfs_mount_rw: dbMount failed!");
252 			return rc;
253 		}
254 	}
255 
256 	/*
257 	 * open/initialize log
258 	 */
259 	if ((rc = lmLogOpen(sb)))
260 		return rc;
261 
262 	/*
263 	 * update file system superblock;
264 	 */
265 	if ((rc = updateSuper(sb, FM_MOUNT))) {
266 		jfs_err("jfs_mount: updateSuper failed w/rc = %d", rc);
267 		lmLogClose(sb);
268 		return rc;
269 	}
270 
271 	/*
272 	 * write MOUNT log record of the file system
273 	 */
274 	logMOUNT(sb);
275 
276 	return rc;
277 }
278 
279 /*
280  *	chkSuper()
281  *
282  * validate the superblock of the file system to be mounted and
283  * get the file system parameters.
284  *
285  * returns
286  *	0 with fragsize set if check successful
287  *	error code if not successful
288  */
289 static int chkSuper(struct super_block *sb)
290 {
291 	int rc = 0;
292 	struct jfs_sb_info *sbi = JFS_SBI(sb);
293 	struct jfs_superblock *j_sb;
294 	struct buffer_head *bh;
295 	int AIM_bytesize, AIT_bytesize;
296 	int expected_AIM_bytesize, expected_AIT_bytesize;
297 	s64 AIM_byte_addr, AIT_byte_addr, fsckwsp_addr;
298 	s64 byte_addr_diff0, byte_addr_diff1;
299 	s32 bsize;
300 
301 	if ((rc = readSuper(sb, &bh)))
302 		return rc;
303 	j_sb = (struct jfs_superblock *)bh->b_data;
304 
305 	/*
306 	 * validate superblock
307 	 */
308 	/* validate fs signature */
309 	if (strncmp(j_sb->s_magic, JFS_MAGIC, 4) ||
310 	    le32_to_cpu(j_sb->s_version) > JFS_VERSION) {
311 		rc = -EINVAL;
312 		goto out;
313 	}
314 
315 	bsize = le32_to_cpu(j_sb->s_bsize);
316 #ifdef _JFS_4K
317 	if (bsize != PSIZE) {
318 		jfs_err("Currently only 4K block size supported!");
319 		rc = -EINVAL;
320 		goto out;
321 	}
322 #endif				/* _JFS_4K */
323 
324 	jfs_info("superblock: flag:0x%08x state:0x%08x size:0x%Lx",
325 		 le32_to_cpu(j_sb->s_flag), le32_to_cpu(j_sb->s_state),
326 		 (unsigned long long) le64_to_cpu(j_sb->s_size));
327 
328 	/* validate the descriptors for Secondary AIM and AIT */
329 	if ((j_sb->s_flag & cpu_to_le32(JFS_BAD_SAIT)) !=
330 	    cpu_to_le32(JFS_BAD_SAIT)) {
331 		expected_AIM_bytesize = 2 * PSIZE;
332 		AIM_bytesize = lengthPXD(&(j_sb->s_aim2)) * bsize;
333 		expected_AIT_bytesize = 4 * PSIZE;
334 		AIT_bytesize = lengthPXD(&(j_sb->s_ait2)) * bsize;
335 		AIM_byte_addr = addressPXD(&(j_sb->s_aim2)) * bsize;
336 		AIT_byte_addr = addressPXD(&(j_sb->s_ait2)) * bsize;
337 		byte_addr_diff0 = AIT_byte_addr - AIM_byte_addr;
338 		fsckwsp_addr = addressPXD(&(j_sb->s_fsckpxd)) * bsize;
339 		byte_addr_diff1 = fsckwsp_addr - AIT_byte_addr;
340 		if ((AIM_bytesize != expected_AIM_bytesize) ||
341 		    (AIT_bytesize != expected_AIT_bytesize) ||
342 		    (byte_addr_diff0 != AIM_bytesize) ||
343 		    (byte_addr_diff1 <= AIT_bytesize))
344 			j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
345 	}
346 
347 	if ((j_sb->s_flag & cpu_to_le32(JFS_GROUPCOMMIT)) !=
348 	    cpu_to_le32(JFS_GROUPCOMMIT))
349 		j_sb->s_flag |= cpu_to_le32(JFS_GROUPCOMMIT);
350 
351 	/* validate fs state */
352 	if (j_sb->s_state != cpu_to_le32(FM_CLEAN) &&
353 	    !sb_rdonly(sb)) {
354 		jfs_err("jfs_mount: Mount Failure: File System Dirty.");
355 		rc = -EINVAL;
356 		goto out;
357 	}
358 
359 	sbi->state = le32_to_cpu(j_sb->s_state);
360 	sbi->mntflag = le32_to_cpu(j_sb->s_flag);
361 
362 	/*
363 	 * JFS always does I/O by 4K pages.  Don't tell the buffer cache
364 	 * that we use anything else (leave s_blocksize alone).
365 	 */
366 	sbi->bsize = bsize;
367 	sbi->l2bsize = le16_to_cpu(j_sb->s_l2bsize);
368 
369 	/*
370 	 * For now, ignore s_pbsize, l2bfactor.  All I/O going through buffer
371 	 * cache.
372 	 */
373 	sbi->nbperpage = PSIZE >> sbi->l2bsize;
374 	sbi->l2nbperpage = L2PSIZE - sbi->l2bsize;
375 	sbi->l2niperblk = sbi->l2bsize - L2DISIZE;
376 	if (sbi->mntflag & JFS_INLINELOG)
377 		sbi->logpxd = j_sb->s_logpxd;
378 	else {
379 		sbi->logdev = new_decode_dev(le32_to_cpu(j_sb->s_logdev));
380 		uuid_copy(&sbi->uuid, &j_sb->s_uuid);
381 		uuid_copy(&sbi->loguuid, &j_sb->s_loguuid);
382 	}
383 	sbi->fsckpxd = j_sb->s_fsckpxd;
384 	sbi->ait2 = j_sb->s_ait2;
385 
386       out:
387 	brelse(bh);
388 	return rc;
389 }
390 
391 
392 /*
393  *	updateSuper()
394  *
395  * update synchronously superblock if it is mounted read-write.
396  */
397 int updateSuper(struct super_block *sb, uint state)
398 {
399 	struct jfs_superblock *j_sb;
400 	struct jfs_sb_info *sbi = JFS_SBI(sb);
401 	struct buffer_head *bh;
402 	int rc;
403 
404 	if (sbi->flag & JFS_NOINTEGRITY) {
405 		if (state == FM_DIRTY) {
406 			sbi->p_state = state;
407 			return 0;
408 		} else if (state == FM_MOUNT) {
409 			sbi->p_state = sbi->state;
410 			state = FM_DIRTY;
411 		} else if (state == FM_CLEAN) {
412 			state = sbi->p_state;
413 		} else
414 			jfs_err("updateSuper: bad state");
415 	} else if (sbi->state == FM_DIRTY)
416 		return 0;
417 
418 	if ((rc = readSuper(sb, &bh)))
419 		return rc;
420 
421 	j_sb = (struct jfs_superblock *)bh->b_data;
422 
423 	j_sb->s_state = cpu_to_le32(state);
424 	sbi->state = state;
425 
426 	if (state == FM_MOUNT) {
427 		/* record log's dev_t and mount serial number */
428 		j_sb->s_logdev = cpu_to_le32(new_encode_dev(sbi->log->bdev->bd_dev));
429 		j_sb->s_logserial = cpu_to_le32(sbi->log->serial);
430 	} else if (state == FM_CLEAN) {
431 		/*
432 		 * If this volume is shared with OS/2, OS/2 will need to
433 		 * recalculate DASD usage, since we don't deal with it.
434 		 */
435 		if (j_sb->s_flag & cpu_to_le32(JFS_DASD_ENABLED))
436 			j_sb->s_flag |= cpu_to_le32(JFS_DASD_PRIME);
437 	}
438 
439 	mark_buffer_dirty(bh);
440 	sync_dirty_buffer(bh);
441 	brelse(bh);
442 
443 	return 0;
444 }
445 
446 
447 /*
448  *	readSuper()
449  *
450  * read superblock by raw sector address
451  */
452 int readSuper(struct super_block *sb, struct buffer_head **bpp)
453 {
454 	/* read in primary superblock */
455 	*bpp = sb_bread(sb, SUPER1_OFF >> sb->s_blocksize_bits);
456 	if (*bpp)
457 		return 0;
458 
459 	/* read in secondary/replicated superblock */
460 	*bpp = sb_bread(sb, SUPER2_OFF >> sb->s_blocksize_bits);
461 	if (*bpp)
462 		return 0;
463 
464 	return -EIO;
465 }
466 
467 
468 /*
469  *	logMOUNT()
470  *
471  * function: write a MOUNT log record for file system.
472  *
473  * MOUNT record keeps logredo() from processing log records
474  * for this file system past this point in log.
475  * it is harmless if mount fails.
476  *
477  * note: MOUNT record is at aggregate level, not at fileset level,
478  * since log records of previous mounts of a fileset
479  * (e.g., AFTER record of extent allocation) have to be processed
480  * to update block allocation map at aggregate level.
481  */
482 static int logMOUNT(struct super_block *sb)
483 {
484 	struct jfs_log *log = JFS_SBI(sb)->log;
485 	struct lrd lrd;
486 
487 	lrd.logtid = 0;
488 	lrd.backchain = 0;
489 	lrd.type = cpu_to_le16(LOG_MOUNT);
490 	lrd.length = 0;
491 	lrd.aggregate = cpu_to_le32(new_encode_dev(sb->s_bdev->bd_dev));
492 	lmLog(log, NULL, &lrd, NULL);
493 
494 	return 0;
495 }
496