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