2 * Copyright (C) International Business Machines Corp., 2000-2004
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.
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.
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
22 * note: file system in transition to aggregate/fileset:
24 * file system mount is interpreted as the mount of aggregate,
25 * if not already mounted, and mount of the single/only fileset in
28 * a file system/aggregate is represented by an internal inode
29 * (aka mount inode) initialized with aggregate superblock;
30 * each vfs represents a fileset, and points to its "fileset inode
31 * allocation map inode" (aka fileset inode):
32 * (an aggregate itself is structured recursively as a filset:
33 * an internal vfs is constructed and points to its "fileset inode
34 * allocation map inode" (aka aggregate inode) where each inode
35 * represents a fileset inode) so that inode number is mapped to
36 * on-disk inode in uniform way at both aggregate and fileset level;
38 * each vnode/inode of a fileset is linked to its vfs (to facilitate
39 * per fileset inode operations, e.g., unmount of a fileset, etc.);
40 * each inode points to the mount inode (to facilitate access to
41 * per aggregate information, e.g., block size, etc.) as well as
46 * mntvfs -> fileset ipimap+ -> aggregate ipbmap -> aggregate ipaimap;
47 * fileset vfs -> vp(1) <-> ... <-> vp(n) <->vproot;
51 #include <linux/buffer_head.h>
52 #include <linux/log2.h>
54 #include "jfs_incore.h"
55 #include "jfs_filsys.h"
56 #include "jfs_superblock.h"
59 #include "jfs_metapage.h"
60 #include "jfs_debug.h"
66 static int chkSuper(struct super_block *);
67 static int logMOUNT(struct super_block *sb);
72 * FUNCTION: vfs_mount()
74 * PARAMETER: sb - super block
76 * RETURN: -EBUSY - device already mounted or open for write
77 * -EBUSY - cvrdvp already mounted;
78 * -EBUSY - mount table full
79 * -ENOTDIR- cvrdvp not directory on a device mount
80 * -ENXIO - device open failure
82 int jfs_mount(struct super_block *sb)
84 int rc = 0; /* Return code */
85 struct jfs_sb_info *sbi = JFS_SBI(sb);
86 struct inode *ipaimap = NULL;
87 struct inode *ipaimap2 = NULL;
88 struct inode *ipimap = NULL;
89 struct inode *ipbmap = NULL;
92 * read/validate superblock
93 * (initialize mount inode from the superblock)
95 if ((rc = chkSuper(sb))) {
99 ipaimap = diReadSpecial(sb, AGGREGATE_I, 0);
100 if (ipaimap == NULL) {
101 jfs_err("jfs_mount: Failed to read AGGREGATE_I");
105 sbi->ipaimap = ipaimap;
107 jfs_info("jfs_mount: ipaimap:0x%p", ipaimap);
110 * initialize aggregate inode allocation map
112 if ((rc = diMount(ipaimap))) {
113 jfs_err("jfs_mount: diMount(ipaimap) failed w/rc = %d", rc);
118 * open aggregate block allocation map
120 ipbmap = diReadSpecial(sb, BMAP_I, 0);
121 if (ipbmap == NULL) {
123 goto err_umount_ipaimap;
126 jfs_info("jfs_mount: ipbmap:0x%p", ipbmap);
128 sbi->ipbmap = ipbmap;
131 * initialize aggregate block allocation map
133 if ((rc = dbMount(ipbmap))) {
134 jfs_err("jfs_mount: dbMount failed w/rc = %d", rc);
139 * open the secondary aggregate inode allocation map
141 * This is a duplicate of the aggregate inode allocation map.
143 * hand craft a vfs in the same fashion as we did to read ipaimap.
144 * By adding INOSPEREXT (32) to the inode number, we are telling
145 * diReadSpecial that we are reading from the secondary aggregate
146 * inode table. This also creates a unique entry in the inode hash
149 if ((sbi->mntflag & JFS_BAD_SAIT) == 0) {
150 ipaimap2 = diReadSpecial(sb, AGGREGATE_I, 1);
152 jfs_err("jfs_mount: Failed to read AGGREGATE_I");
154 goto err_umount_ipbmap;
156 sbi->ipaimap2 = ipaimap2;
158 jfs_info("jfs_mount: ipaimap2:0x%p", ipaimap2);
161 * initialize secondary aggregate inode allocation map
163 if ((rc = diMount(ipaimap2))) {
164 jfs_err("jfs_mount: diMount(ipaimap2) failed, rc = %d",
169 /* Secondary aggregate inode table is not valid */
170 sbi->ipaimap2 = NULL;
173 * mount (the only/single) fileset
176 * open fileset inode allocation map (aka fileset inode)
178 ipimap = diReadSpecial(sb, FILESYSTEM_I, 0);
179 if (ipimap == NULL) {
180 jfs_err("jfs_mount: Failed to read FILESYSTEM_I");
181 /* open fileset secondary inode allocation map */
183 goto err_umount_ipaimap2;
185 jfs_info("jfs_mount: ipimap:0x%p", ipimap);
187 /* map further access of per fileset inodes by the fileset inode */
188 sbi->ipimap = ipimap;
190 /* initialize fileset inode allocation map */
191 if ((rc = diMount(ipimap))) {
192 jfs_err("jfs_mount: diMount failed w/rc = %d", rc);
202 /* close fileset inode allocation map inode */
203 diFreeSpecial(ipimap);
205 /* close secondary aggregate inode allocation map */
207 diUnmount(ipaimap2, 1);
209 /* close aggregate inodes */
211 diFreeSpecial(ipaimap2);
212 err_umount_ipbmap: /* close aggregate block allocation map */
213 dbUnmount(ipbmap, 1);
214 err_ipbmap: /* close aggregate inodes */
215 diFreeSpecial(ipbmap);
216 err_umount_ipaimap: /* close aggregate inode allocation map */
217 diUnmount(ipaimap, 1);
218 err_ipaimap: /* close aggregate inodes */
219 diFreeSpecial(ipaimap);
222 jfs_err("Mount JFS Failure: %d", rc);
228 * NAME: jfs_mount_rw(sb, remount)
230 * FUNCTION: Completes read-write mount, or remounts read-only volume
233 int jfs_mount_rw(struct super_block *sb, int remount)
235 struct jfs_sb_info *sbi = JFS_SBI(sb);
239 * If we are re-mounting a previously read-only volume, we want to
240 * re-read the inode and block maps, since fsck.jfs may have updated
244 if (chkSuper(sb) || (sbi->state != FM_CLEAN))
247 truncate_inode_pages(sbi->ipimap->i_mapping, 0);
248 truncate_inode_pages(sbi->ipbmap->i_mapping, 0);
249 diUnmount(sbi->ipimap, 1);
250 if ((rc = diMount(sbi->ipimap))) {
251 jfs_err("jfs_mount_rw: diMount failed!");
255 dbUnmount(sbi->ipbmap, 1);
256 if ((rc = dbMount(sbi->ipbmap))) {
257 jfs_err("jfs_mount_rw: dbMount failed!");
263 * open/initialize log
265 if ((rc = lmLogOpen(sb)))
269 * update file system superblock;
271 if ((rc = updateSuper(sb, FM_MOUNT))) {
272 jfs_err("jfs_mount: updateSuper failed w/rc = %d", rc);
278 * write MOUNT log record of the file system
288 * validate the superblock of the file system to be mounted and
289 * get the file system parameters.
292 * 0 with fragsize set if check successful
293 * error code if not successful
295 static int chkSuper(struct super_block *sb)
298 struct jfs_sb_info *sbi = JFS_SBI(sb);
299 struct jfs_superblock *j_sb;
300 struct buffer_head *bh;
301 int AIM_bytesize, AIT_bytesize;
302 int expected_AIM_bytesize, expected_AIT_bytesize;
303 s64 AIM_byte_addr, AIT_byte_addr, fsckwsp_addr;
304 s64 byte_addr_diff0, byte_addr_diff1;
307 if ((rc = readSuper(sb, &bh)))
309 j_sb = (struct jfs_superblock *)bh->b_data;
312 * validate superblock
314 /* validate fs signature */
315 if (strncmp(j_sb->s_magic, JFS_MAGIC, 4) ||
316 le32_to_cpu(j_sb->s_version) > JFS_VERSION) {
321 bsize = le32_to_cpu(j_sb->s_bsize);
323 if (bsize != PSIZE) {
324 jfs_err("Currently only 4K block size supported!");
330 jfs_info("superblock: flag:0x%08x state:0x%08x size:0x%Lx",
331 le32_to_cpu(j_sb->s_flag), le32_to_cpu(j_sb->s_state),
332 (unsigned long long) le64_to_cpu(j_sb->s_size));
334 /* validate the descriptors for Secondary AIM and AIT */
335 if ((j_sb->s_flag & cpu_to_le32(JFS_BAD_SAIT)) !=
336 cpu_to_le32(JFS_BAD_SAIT)) {
337 expected_AIM_bytesize = 2 * PSIZE;
338 AIM_bytesize = lengthPXD(&(j_sb->s_aim2)) * bsize;
339 expected_AIT_bytesize = 4 * PSIZE;
340 AIT_bytesize = lengthPXD(&(j_sb->s_ait2)) * bsize;
341 AIM_byte_addr = addressPXD(&(j_sb->s_aim2)) * bsize;
342 AIT_byte_addr = addressPXD(&(j_sb->s_ait2)) * bsize;
343 byte_addr_diff0 = AIT_byte_addr - AIM_byte_addr;
344 fsckwsp_addr = addressPXD(&(j_sb->s_fsckpxd)) * bsize;
345 byte_addr_diff1 = fsckwsp_addr - AIT_byte_addr;
346 if ((AIM_bytesize != expected_AIM_bytesize) ||
347 (AIT_bytesize != expected_AIT_bytesize) ||
348 (byte_addr_diff0 != AIM_bytesize) ||
349 (byte_addr_diff1 <= AIT_bytesize))
350 j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
353 if ((j_sb->s_flag & cpu_to_le32(JFS_GROUPCOMMIT)) !=
354 cpu_to_le32(JFS_GROUPCOMMIT))
355 j_sb->s_flag |= cpu_to_le32(JFS_GROUPCOMMIT);
357 /* validate fs state */
358 if (j_sb->s_state != cpu_to_le32(FM_CLEAN) &&
360 jfs_err("jfs_mount: Mount Failure: File System Dirty.");
365 sbi->state = le32_to_cpu(j_sb->s_state);
366 sbi->mntflag = le32_to_cpu(j_sb->s_flag);
369 * JFS always does I/O by 4K pages. Don't tell the buffer cache
370 * that we use anything else (leave s_blocksize alone).
373 sbi->l2bsize = le16_to_cpu(j_sb->s_l2bsize);
375 /* check some fields for possible corruption */
376 if (sbi->l2bsize != ilog2((u32)bsize) ||
378 le32_to_cpu(j_sb->s_state) > FM_STATE_MAX) {
380 jfs_err("jfs_mount: Mount Failure: superblock is corrupt!");
385 * For now, ignore s_pbsize, l2bfactor. All I/O going through buffer
388 sbi->nbperpage = PSIZE >> sbi->l2bsize;
389 sbi->l2nbperpage = L2PSIZE - sbi->l2bsize;
390 sbi->l2niperblk = sbi->l2bsize - L2DISIZE;
391 if (sbi->mntflag & JFS_INLINELOG)
392 sbi->logpxd = j_sb->s_logpxd;
394 sbi->logdev = new_decode_dev(le32_to_cpu(j_sb->s_logdev));
395 memcpy(sbi->uuid, j_sb->s_uuid, sizeof(sbi->uuid));
396 memcpy(sbi->loguuid, j_sb->s_loguuid, sizeof(sbi->uuid));
398 sbi->fsckpxd = j_sb->s_fsckpxd;
399 sbi->ait2 = j_sb->s_ait2;
410 * update synchronously superblock if it is mounted read-write.
412 int updateSuper(struct super_block *sb, uint state)
414 struct jfs_superblock *j_sb;
415 struct jfs_sb_info *sbi = JFS_SBI(sb);
416 struct buffer_head *bh;
419 if (sbi->flag & JFS_NOINTEGRITY) {
420 if (state == FM_DIRTY) {
421 sbi->p_state = state;
423 } else if (state == FM_MOUNT) {
424 sbi->p_state = sbi->state;
426 } else if (state == FM_CLEAN) {
427 state = sbi->p_state;
429 jfs_err("updateSuper: bad state");
430 } else if (sbi->state == FM_DIRTY)
433 if ((rc = readSuper(sb, &bh)))
436 j_sb = (struct jfs_superblock *)bh->b_data;
438 j_sb->s_state = cpu_to_le32(state);
441 if (state == FM_MOUNT) {
442 /* record log's dev_t and mount serial number */
443 j_sb->s_logdev = cpu_to_le32(new_encode_dev(sbi->log->bdev->bd_dev));
444 j_sb->s_logserial = cpu_to_le32(sbi->log->serial);
445 } else if (state == FM_CLEAN) {
447 * If this volume is shared with OS/2, OS/2 will need to
448 * recalculate DASD usage, since we don't deal with it.
450 if (j_sb->s_flag & cpu_to_le32(JFS_DASD_ENABLED))
451 j_sb->s_flag |= cpu_to_le32(JFS_DASD_PRIME);
454 mark_buffer_dirty(bh);
455 sync_dirty_buffer(bh);
465 * read superblock by raw sector address
467 int readSuper(struct super_block *sb, struct buffer_head **bpp)
469 /* read in primary superblock */
470 *bpp = sb_bread(sb, SUPER1_OFF >> sb->s_blocksize_bits);
474 /* read in secondary/replicated superblock */
475 *bpp = sb_bread(sb, SUPER2_OFF >> sb->s_blocksize_bits);
486 * function: write a MOUNT log record for file system.
488 * MOUNT record keeps logredo() from processing log records
489 * for this file system past this point in log.
490 * it is harmless if mount fails.
492 * note: MOUNT record is at aggregate level, not at fileset level,
493 * since log records of previous mounts of a fileset
494 * (e.g., AFTER record of extent allocation) have to be processed
495 * to update block allocation map at aggregate level.
497 static int logMOUNT(struct super_block *sb)
499 struct jfs_log *log = JFS_SBI(sb)->log;
504 lrd.type = cpu_to_le16(LOG_MOUNT);
506 lrd.aggregate = cpu_to_le32(new_encode_dev(sb->s_bdev->bd_dev));
507 lmLog(log, NULL, &lrd, NULL);