blob: aa4ff7bcaff23dd8931de14fbd629467819772e0 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) International Business Machines Corp., 2000-2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
6/*
7 * Module: jfs_mount.c
8 *
9 * note: file system in transition to aggregate/fileset:
10 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -050011 * file system mount is interpreted as the mount of aggregate,
12 * if not already mounted, and mount of the single/only fileset in
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * the aggregate;
14 *
15 * a file system/aggregate is represented by an internal inode
16 * (aka mount inode) initialized with aggregate superblock;
Dave Kleikamp63f83c92006-10-02 09:55:27 -050017 * each vfs represents a fileset, and points to its "fileset inode
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * allocation map inode" (aka fileset inode):
Dave Kleikamp63f83c92006-10-02 09:55:27 -050019 * (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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * 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 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -050031 * aggregate
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * 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>
Christoph Hellwig621c1f42020-06-20 09:16:44 +020039#include <linux/blkdev.h>
Randy Dunlap3bef1982020-12-18 12:17:16 -080040#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "jfs_incore.h"
43#include "jfs_filsys.h"
44#include "jfs_superblock.h"
45#include "jfs_dmap.h"
46#include "jfs_imap.h"
47#include "jfs_metapage.h"
48#include "jfs_debug.h"
49
50
51/*
52 * forward references
53 */
54static int chkSuper(struct super_block *);
55static int logMOUNT(struct super_block *sb);
56
57/*
58 * NAME: jfs_mount(sb)
59 *
60 * FUNCTION: vfs_mount()
61 *
62 * PARAMETER: sb - super block
63 *
64 * RETURN: -EBUSY - device already mounted or open for write
65 * -EBUSY - cvrdvp already mounted;
66 * -EBUSY - mount table full
67 * -ENOTDIR- cvrdvp not directory on a device mount
68 * -ENXIO - device open failure
69 */
70int jfs_mount(struct super_block *sb)
71{
Dave Kleikampf720e3b2007-06-06 15:28:35 -050072 int rc = 0; /* Return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct jfs_sb_info *sbi = JFS_SBI(sb);
74 struct inode *ipaimap = NULL;
75 struct inode *ipaimap2 = NULL;
76 struct inode *ipimap = NULL;
77 struct inode *ipbmap = NULL;
78
79 /*
Dave Kleikamp63f83c92006-10-02 09:55:27 -050080 * read/validate superblock
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * (initialize mount inode from the superblock)
82 */
83 if ((rc = chkSuper(sb))) {
Dongliang Muc48a14d2021-09-04 10:37:41 +080084 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
87 ipaimap = diReadSpecial(sb, AGGREGATE_I, 0);
88 if (ipaimap == NULL) {
Paul Bolle426d3102010-08-07 12:30:03 +020089 jfs_err("jfs_mount: Failed to read AGGREGATE_I");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 rc = -EIO;
Dongliang Muc48a14d2021-09-04 10:37:41 +080091 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93 sbi->ipaimap = ipaimap;
94
95 jfs_info("jfs_mount: ipaimap:0x%p", ipaimap);
96
97 /*
98 * initialize aggregate inode allocation map
99 */
100 if ((rc = diMount(ipaimap))) {
101 jfs_err("jfs_mount: diMount(ipaimap) failed w/rc = %d", rc);
Dongliang Muc48a14d2021-09-04 10:37:41 +0800102 goto err_ipaimap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104
105 /*
106 * open aggregate block allocation map
107 */
108 ipbmap = diReadSpecial(sb, BMAP_I, 0);
109 if (ipbmap == NULL) {
110 rc = -EIO;
Dongliang Muc48a14d2021-09-04 10:37:41 +0800111 goto err_umount_ipaimap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113
114 jfs_info("jfs_mount: ipbmap:0x%p", ipbmap);
115
116 sbi->ipbmap = ipbmap;
117
118 /*
119 * initialize aggregate block allocation map
120 */
121 if ((rc = dbMount(ipbmap))) {
122 jfs_err("jfs_mount: dbMount failed w/rc = %d", rc);
Dongliang Muc48a14d2021-09-04 10:37:41 +0800123 goto err_ipbmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125
126 /*
127 * open the secondary aggregate inode allocation map
128 *
129 * This is a duplicate of the aggregate inode allocation map.
130 *
131 * hand craft a vfs in the same fashion as we did to read ipaimap.
132 * By adding INOSPEREXT (32) to the inode number, we are telling
133 * diReadSpecial that we are reading from the secondary aggregate
134 * inode table. This also creates a unique entry in the inode hash
135 * table.
136 */
137 if ((sbi->mntflag & JFS_BAD_SAIT) == 0) {
138 ipaimap2 = diReadSpecial(sb, AGGREGATE_I, 1);
Joe Perches09aaa742007-11-13 22:16:08 -0600139 if (!ipaimap2) {
Paul Bolle426d3102010-08-07 12:30:03 +0200140 jfs_err("jfs_mount: Failed to read AGGREGATE_I");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 rc = -EIO;
Dongliang Muc48a14d2021-09-04 10:37:41 +0800142 goto err_umount_ipbmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144 sbi->ipaimap2 = ipaimap2;
145
146 jfs_info("jfs_mount: ipaimap2:0x%p", ipaimap2);
147
148 /*
149 * initialize secondary aggregate inode allocation map
150 */
151 if ((rc = diMount(ipaimap2))) {
152 jfs_err("jfs_mount: diMount(ipaimap2) failed, rc = %d",
153 rc);
Dongliang Muc48a14d2021-09-04 10:37:41 +0800154 goto err_ipaimap2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 } else
157 /* Secondary aggregate inode table is not valid */
158 sbi->ipaimap2 = NULL;
159
160 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500161 * mount (the only/single) fileset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 */
163 /*
164 * open fileset inode allocation map (aka fileset inode)
165 */
166 ipimap = diReadSpecial(sb, FILESYSTEM_I, 0);
167 if (ipimap == NULL) {
168 jfs_err("jfs_mount: Failed to read FILESYSTEM_I");
169 /* open fileset secondary inode allocation map */
170 rc = -EIO;
Dongliang Muc48a14d2021-09-04 10:37:41 +0800171 goto err_umount_ipaimap2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 jfs_info("jfs_mount: ipimap:0x%p", ipimap);
174
175 /* map further access of per fileset inodes by the fileset inode */
176 sbi->ipimap = ipimap;
177
178 /* initialize fileset inode allocation map */
179 if ((rc = diMount(ipimap))) {
180 jfs_err("jfs_mount: diMount failed w/rc = %d", rc);
Dongliang Muc48a14d2021-09-04 10:37:41 +0800181 goto err_ipimap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183
Dongliang Muc48a14d2021-09-04 10:37:41 +0800184 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500187 * unwind on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
Dongliang Muc48a14d2021-09-04 10:37:41 +0800189err_ipimap:
190 /* close fileset inode allocation map inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 diFreeSpecial(ipimap);
Dongliang Muc48a14d2021-09-04 10:37:41 +0800192err_umount_ipaimap2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* close secondary aggregate inode allocation map */
Dongliang Muc48a14d2021-09-04 10:37:41 +0800194 if (ipaimap2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 diUnmount(ipaimap2, 1);
Dongliang Muc48a14d2021-09-04 10:37:41 +0800196err_ipaimap2:
197 /* close aggregate inodes */
198 if (ipaimap2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 diFreeSpecial(ipaimap2);
Dongliang Muc48a14d2021-09-04 10:37:41 +0800200err_umount_ipbmap: /* close aggregate block allocation map */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 dbUnmount(ipbmap, 1);
Dongliang Muc48a14d2021-09-04 10:37:41 +0800202err_ipbmap: /* close aggregate inodes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 diFreeSpecial(ipbmap);
Dongliang Muc48a14d2021-09-04 10:37:41 +0800204err_umount_ipaimap: /* close aggregate inode allocation map */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 diUnmount(ipaimap, 1);
Dongliang Muc48a14d2021-09-04 10:37:41 +0800206err_ipaimap: /* close aggregate inodes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 diFreeSpecial(ipaimap);
Dongliang Muc48a14d2021-09-04 10:37:41 +0800208out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (rc)
210 jfs_err("Mount JFS Failure: %d", rc);
211
212 return rc;
213}
214
215/*
216 * NAME: jfs_mount_rw(sb, remount)
217 *
218 * FUNCTION: Completes read-write mount, or remounts read-only volume
219 * as read-write
220 */
221int jfs_mount_rw(struct super_block *sb, int remount)
222{
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500223 struct jfs_sb_info *sbi = JFS_SBI(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 int rc;
225
226 /*
227 * If we are re-mounting a previously read-only volume, we want to
228 * re-read the inode and block maps, since fsck.jfs may have updated
229 * them.
230 */
231 if (remount) {
232 if (chkSuper(sb) || (sbi->state != FM_CLEAN))
233 return -EINVAL;
234
235 truncate_inode_pages(sbi->ipimap->i_mapping, 0);
236 truncate_inode_pages(sbi->ipbmap->i_mapping, 0);
237 diUnmount(sbi->ipimap, 1);
238 if ((rc = diMount(sbi->ipimap))) {
239 jfs_err("jfs_mount_rw: diMount failed!");
240 return rc;
241 }
242
243 dbUnmount(sbi->ipbmap, 1);
244 if ((rc = dbMount(sbi->ipbmap))) {
245 jfs_err("jfs_mount_rw: dbMount failed!");
246 return rc;
247 }
248 }
249
250 /*
251 * open/initialize log
252 */
253 if ((rc = lmLogOpen(sb)))
254 return rc;
255
256 /*
257 * update file system superblock;
258 */
259 if ((rc = updateSuper(sb, FM_MOUNT))) {
260 jfs_err("jfs_mount: updateSuper failed w/rc = %d", rc);
261 lmLogClose(sb);
262 return rc;
263 }
264
265 /*
266 * write MOUNT log record of the file system
267 */
268 logMOUNT(sb);
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return rc;
271}
272
273/*
274 * chkSuper()
275 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500276 * validate the superblock of the file system to be mounted and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * get the file system parameters.
278 *
279 * returns
280 * 0 with fragsize set if check successful
281 * error code if not successful
282 */
283static int chkSuper(struct super_block *sb)
284{
285 int rc = 0;
286 struct jfs_sb_info *sbi = JFS_SBI(sb);
287 struct jfs_superblock *j_sb;
288 struct buffer_head *bh;
289 int AIM_bytesize, AIT_bytesize;
290 int expected_AIM_bytesize, expected_AIT_bytesize;
291 s64 AIM_byte_addr, AIT_byte_addr, fsckwsp_addr;
292 s64 byte_addr_diff0, byte_addr_diff1;
293 s32 bsize;
294
295 if ((rc = readSuper(sb, &bh)))
296 return rc;
297 j_sb = (struct jfs_superblock *)bh->b_data;
298
299 /*
300 * validate superblock
301 */
302 /* validate fs signature */
303 if (strncmp(j_sb->s_magic, JFS_MAGIC, 4) ||
304 le32_to_cpu(j_sb->s_version) > JFS_VERSION) {
305 rc = -EINVAL;
306 goto out;
307 }
308
309 bsize = le32_to_cpu(j_sb->s_bsize);
310#ifdef _JFS_4K
311 if (bsize != PSIZE) {
312 jfs_err("Currently only 4K block size supported!");
313 rc = -EINVAL;
314 goto out;
315 }
316#endif /* _JFS_4K */
317
318 jfs_info("superblock: flag:0x%08x state:0x%08x size:0x%Lx",
319 le32_to_cpu(j_sb->s_flag), le32_to_cpu(j_sb->s_state),
320 (unsigned long long) le64_to_cpu(j_sb->s_size));
321
322 /* validate the descriptors for Secondary AIM and AIT */
323 if ((j_sb->s_flag & cpu_to_le32(JFS_BAD_SAIT)) !=
324 cpu_to_le32(JFS_BAD_SAIT)) {
325 expected_AIM_bytesize = 2 * PSIZE;
326 AIM_bytesize = lengthPXD(&(j_sb->s_aim2)) * bsize;
327 expected_AIT_bytesize = 4 * PSIZE;
328 AIT_bytesize = lengthPXD(&(j_sb->s_ait2)) * bsize;
329 AIM_byte_addr = addressPXD(&(j_sb->s_aim2)) * bsize;
330 AIT_byte_addr = addressPXD(&(j_sb->s_ait2)) * bsize;
331 byte_addr_diff0 = AIT_byte_addr - AIM_byte_addr;
332 fsckwsp_addr = addressPXD(&(j_sb->s_fsckpxd)) * bsize;
333 byte_addr_diff1 = fsckwsp_addr - AIT_byte_addr;
334 if ((AIM_bytesize != expected_AIM_bytesize) ||
335 (AIT_bytesize != expected_AIT_bytesize) ||
336 (byte_addr_diff0 != AIM_bytesize) ||
337 (byte_addr_diff1 <= AIT_bytesize))
338 j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
339 }
340
341 if ((j_sb->s_flag & cpu_to_le32(JFS_GROUPCOMMIT)) !=
342 cpu_to_le32(JFS_GROUPCOMMIT))
343 j_sb->s_flag |= cpu_to_le32(JFS_GROUPCOMMIT);
344
345 /* validate fs state */
346 if (j_sb->s_state != cpu_to_le32(FM_CLEAN) &&
David Howellsbc98a422017-07-17 08:45:34 +0100347 !sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 jfs_err("jfs_mount: Mount Failure: File System Dirty.");
349 rc = -EINVAL;
350 goto out;
351 }
352
353 sbi->state = le32_to_cpu(j_sb->s_state);
354 sbi->mntflag = le32_to_cpu(j_sb->s_flag);
355
356 /*
357 * JFS always does I/O by 4K pages. Don't tell the buffer cache
358 * that we use anything else (leave s_blocksize alone).
359 */
360 sbi->bsize = bsize;
361 sbi->l2bsize = le16_to_cpu(j_sb->s_l2bsize);
362
Randy Dunlap3bef1982020-12-18 12:17:16 -0800363 /* check some fields for possible corruption */
364 if (sbi->l2bsize != ilog2((u32)bsize) ||
365 j_sb->pad != 0 ||
366 le32_to_cpu(j_sb->s_state) > FM_STATE_MAX) {
367 rc = -EINVAL;
368 jfs_err("jfs_mount: Mount Failure: superblock is corrupt!");
369 goto out;
370 }
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /*
373 * For now, ignore s_pbsize, l2bfactor. All I/O going through buffer
374 * cache.
375 */
376 sbi->nbperpage = PSIZE >> sbi->l2bsize;
377 sbi->l2nbperpage = L2PSIZE - sbi->l2bsize;
378 sbi->l2niperblk = sbi->l2bsize - L2DISIZE;
379 if (sbi->mntflag & JFS_INLINELOG)
380 sbi->logpxd = j_sb->s_logpxd;
381 else {
382 sbi->logdev = new_decode_dev(le32_to_cpu(j_sb->s_logdev));
Andy Shevchenko2e3bc612019-01-10 15:41:53 +0200383 uuid_copy(&sbi->uuid, &j_sb->s_uuid);
384 uuid_copy(&sbi->loguuid, &j_sb->s_loguuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386 sbi->fsckpxd = j_sb->s_fsckpxd;
387 sbi->ait2 = j_sb->s_ait2;
388
389 out:
390 brelse(bh);
391 return rc;
392}
393
394
395/*
396 * updateSuper()
397 *
398 * update synchronously superblock if it is mounted read-write.
399 */
400int updateSuper(struct super_block *sb, uint state)
401{
402 struct jfs_superblock *j_sb;
403 struct jfs_sb_info *sbi = JFS_SBI(sb);
404 struct buffer_head *bh;
405 int rc;
406
407 if (sbi->flag & JFS_NOINTEGRITY) {
408 if (state == FM_DIRTY) {
409 sbi->p_state = state;
410 return 0;
411 } else if (state == FM_MOUNT) {
412 sbi->p_state = sbi->state;
413 state = FM_DIRTY;
414 } else if (state == FM_CLEAN) {
415 state = sbi->p_state;
416 } else
417 jfs_err("updateSuper: bad state");
418 } else if (sbi->state == FM_DIRTY)
419 return 0;
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if ((rc = readSuper(sb, &bh)))
422 return rc;
423
424 j_sb = (struct jfs_superblock *)bh->b_data;
425
426 j_sb->s_state = cpu_to_le32(state);
427 sbi->state = state;
428
429 if (state == FM_MOUNT) {
430 /* record log's dev_t and mount serial number */
431 j_sb->s_logdev = cpu_to_le32(new_encode_dev(sbi->log->bdev->bd_dev));
432 j_sb->s_logserial = cpu_to_le32(sbi->log->serial);
433 } else if (state == FM_CLEAN) {
434 /*
435 * If this volume is shared with OS/2, OS/2 will need to
436 * recalculate DASD usage, since we don't deal with it.
437 */
438 if (j_sb->s_flag & cpu_to_le32(JFS_DASD_ENABLED))
439 j_sb->s_flag |= cpu_to_le32(JFS_DASD_PRIME);
440 }
441
442 mark_buffer_dirty(bh);
443 sync_dirty_buffer(bh);
444 brelse(bh);
445
446 return 0;
447}
448
449
450/*
451 * readSuper()
452 *
453 * read superblock by raw sector address
454 */
455int readSuper(struct super_block *sb, struct buffer_head **bpp)
456{
457 /* read in primary superblock */
458 *bpp = sb_bread(sb, SUPER1_OFF >> sb->s_blocksize_bits);
459 if (*bpp)
460 return 0;
461
462 /* read in secondary/replicated superblock */
463 *bpp = sb_bread(sb, SUPER2_OFF >> sb->s_blocksize_bits);
464 if (*bpp)
465 return 0;
466
467 return -EIO;
468}
469
470
471/*
472 * logMOUNT()
473 *
474 * function: write a MOUNT log record for file system.
475 *
476 * MOUNT record keeps logredo() from processing log records
477 * for this file system past this point in log.
478 * it is harmless if mount fails.
479 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500480 * note: MOUNT record is at aggregate level, not at fileset level,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 * since log records of previous mounts of a fileset
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500482 * (e.g., AFTER record of extent allocation) have to be processed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 * to update block allocation map at aggregate level.
484 */
485static int logMOUNT(struct super_block *sb)
486{
487 struct jfs_log *log = JFS_SBI(sb)->log;
488 struct lrd lrd;
489
490 lrd.logtid = 0;
491 lrd.backchain = 0;
492 lrd.type = cpu_to_le16(LOG_MOUNT);
493 lrd.length = 0;
494 lrd.aggregate = cpu_to_le32(new_encode_dev(sb->s_bdev->bd_dev));
495 lmLog(log, NULL, &lrd, NULL);
496
497 return 0;
498}