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