GNU Linux-libre 4.19.264-gnu1
[releases.git] / fs / gfs2 / ops_fstype.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
4  *
5  * This copyrighted material is made available to anyone wishing to use,
6  * modify, copy, or redistribute it subject to the terms and conditions
7  * of the GNU General Public License version 2.
8  */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/completion.h>
16 #include <linux/buffer_head.h>
17 #include <linux/blkdev.h>
18 #include <linux/kthread.h>
19 #include <linux/export.h>
20 #include <linux/namei.h>
21 #include <linux/mount.h>
22 #include <linux/gfs2_ondisk.h>
23 #include <linux/quotaops.h>
24 #include <linux/lockdep.h>
25 #include <linux/module.h>
26 #include <linux/backing-dev.h>
27
28 #include "gfs2.h"
29 #include "incore.h"
30 #include "bmap.h"
31 #include "glock.h"
32 #include "glops.h"
33 #include "inode.h"
34 #include "recovery.h"
35 #include "rgrp.h"
36 #include "super.h"
37 #include "sys.h"
38 #include "util.h"
39 #include "log.h"
40 #include "quota.h"
41 #include "dir.h"
42 #include "meta_io.h"
43 #include "trace_gfs2.h"
44
45 #define DO 0
46 #define UNDO 1
47
48 /**
49  * gfs2_tune_init - Fill a gfs2_tune structure with default values
50  * @gt: tune
51  *
52  */
53
54 static void gfs2_tune_init(struct gfs2_tune *gt)
55 {
56         spin_lock_init(&gt->gt_spin);
57
58         gt->gt_quota_warn_period = 10;
59         gt->gt_quota_scale_num = 1;
60         gt->gt_quota_scale_den = 1;
61         gt->gt_new_files_jdata = 0;
62         gt->gt_max_readahead = BIT(18);
63         gt->gt_complain_secs = 10;
64 }
65
66 static struct gfs2_sbd *init_sbd(struct super_block *sb)
67 {
68         struct gfs2_sbd *sdp;
69         struct address_space *mapping;
70
71         sdp = kzalloc(sizeof(struct gfs2_sbd), GFP_KERNEL);
72         if (!sdp)
73                 return NULL;
74
75         sdp->sd_vfs = sb;
76         sdp->sd_lkstats = alloc_percpu(struct gfs2_pcpu_lkstats);
77         if (!sdp->sd_lkstats) {
78                 kfree(sdp);
79                 return NULL;
80         }
81         sb->s_fs_info = sdp;
82
83         set_bit(SDF_NOJOURNALID, &sdp->sd_flags);
84         gfs2_tune_init(&sdp->sd_tune);
85
86         init_waitqueue_head(&sdp->sd_glock_wait);
87         atomic_set(&sdp->sd_glock_disposal, 0);
88         init_completion(&sdp->sd_locking_init);
89         init_completion(&sdp->sd_wdack);
90         spin_lock_init(&sdp->sd_statfs_spin);
91
92         spin_lock_init(&sdp->sd_rindex_spin);
93         sdp->sd_rindex_tree.rb_node = NULL;
94
95         INIT_LIST_HEAD(&sdp->sd_jindex_list);
96         spin_lock_init(&sdp->sd_jindex_spin);
97         mutex_init(&sdp->sd_jindex_mutex);
98         init_completion(&sdp->sd_journal_ready);
99
100         INIT_LIST_HEAD(&sdp->sd_quota_list);
101         mutex_init(&sdp->sd_quota_mutex);
102         mutex_init(&sdp->sd_quota_sync_mutex);
103         init_waitqueue_head(&sdp->sd_quota_wait);
104         INIT_LIST_HEAD(&sdp->sd_trunc_list);
105         spin_lock_init(&sdp->sd_trunc_lock);
106         spin_lock_init(&sdp->sd_bitmap_lock);
107
108         mapping = &sdp->sd_aspace;
109
110         address_space_init_once(mapping);
111         mapping->a_ops = &gfs2_rgrp_aops;
112         mapping->host = sb->s_bdev->bd_inode;
113         mapping->flags = 0;
114         mapping_set_gfp_mask(mapping, GFP_NOFS);
115         mapping->private_data = NULL;
116         mapping->writeback_index = 0;
117
118         spin_lock_init(&sdp->sd_log_lock);
119         atomic_set(&sdp->sd_log_pinned, 0);
120         INIT_LIST_HEAD(&sdp->sd_log_le_revoke);
121         INIT_LIST_HEAD(&sdp->sd_log_le_ordered);
122         spin_lock_init(&sdp->sd_ordered_lock);
123
124         init_waitqueue_head(&sdp->sd_log_waitq);
125         init_waitqueue_head(&sdp->sd_logd_waitq);
126         spin_lock_init(&sdp->sd_ail_lock);
127         INIT_LIST_HEAD(&sdp->sd_ail1_list);
128         INIT_LIST_HEAD(&sdp->sd_ail2_list);
129
130         init_rwsem(&sdp->sd_log_flush_lock);
131         atomic_set(&sdp->sd_log_in_flight, 0);
132         atomic_set(&sdp->sd_reserving_log, 0);
133         init_waitqueue_head(&sdp->sd_reserving_log_wait);
134         init_waitqueue_head(&sdp->sd_log_flush_wait);
135         atomic_set(&sdp->sd_freeze_state, SFS_UNFROZEN);
136         mutex_init(&sdp->sd_freeze_mutex);
137
138         return sdp;
139 }
140
141
142 /**
143  * gfs2_check_sb - Check superblock
144  * @sdp: the filesystem
145  * @sb: The superblock
146  * @silent: Don't print a message if the check fails
147  *
148  * Checks the version code of the FS is one that we understand how to
149  * read and that the sizes of the various on-disk structures have not
150  * changed.
151  */
152
153 static int gfs2_check_sb(struct gfs2_sbd *sdp, int silent)
154 {
155         struct gfs2_sb_host *sb = &sdp->sd_sb;
156
157         if (sb->sb_magic != GFS2_MAGIC ||
158             sb->sb_type != GFS2_METATYPE_SB) {
159                 if (!silent)
160                         pr_warn("not a GFS2 filesystem\n");
161                 return -EINVAL;
162         }
163
164         if (sb->sb_fs_format != GFS2_FORMAT_FS ||
165             sb->sb_multihost_format != GFS2_FORMAT_MULTI) {
166                 fs_warn(sdp, "Unknown on-disk format, unable to mount\n");
167                 return -EINVAL;
168         }
169
170         if (sb->sb_bsize < 512 || sb->sb_bsize > PAGE_SIZE ||
171             (sb->sb_bsize & (sb->sb_bsize - 1))) {
172                 pr_warn("Invalid superblock size\n");
173                 return -EINVAL;
174         }
175
176         return 0;
177 }
178
179 static void end_bio_io_page(struct bio *bio)
180 {
181         struct page *page = bio->bi_private;
182
183         if (!bio->bi_status)
184                 SetPageUptodate(page);
185         else
186                 pr_warn("error %d reading superblock\n", bio->bi_status);
187         unlock_page(page);
188 }
189
190 static void gfs2_sb_in(struct gfs2_sbd *sdp, const void *buf)
191 {
192         struct gfs2_sb_host *sb = &sdp->sd_sb;
193         struct super_block *s = sdp->sd_vfs;
194         const struct gfs2_sb *str = buf;
195
196         sb->sb_magic = be32_to_cpu(str->sb_header.mh_magic);
197         sb->sb_type = be32_to_cpu(str->sb_header.mh_type);
198         sb->sb_format = be32_to_cpu(str->sb_header.mh_format);
199         sb->sb_fs_format = be32_to_cpu(str->sb_fs_format);
200         sb->sb_multihost_format = be32_to_cpu(str->sb_multihost_format);
201         sb->sb_bsize = be32_to_cpu(str->sb_bsize);
202         sb->sb_bsize_shift = be32_to_cpu(str->sb_bsize_shift);
203         sb->sb_master_dir.no_addr = be64_to_cpu(str->sb_master_dir.no_addr);
204         sb->sb_master_dir.no_formal_ino = be64_to_cpu(str->sb_master_dir.no_formal_ino);
205         sb->sb_root_dir.no_addr = be64_to_cpu(str->sb_root_dir.no_addr);
206         sb->sb_root_dir.no_formal_ino = be64_to_cpu(str->sb_root_dir.no_formal_ino);
207
208         memcpy(sb->sb_lockproto, str->sb_lockproto, GFS2_LOCKNAME_LEN);
209         memcpy(sb->sb_locktable, str->sb_locktable, GFS2_LOCKNAME_LEN);
210         memcpy(&s->s_uuid, str->sb_uuid, 16);
211 }
212
213 /**
214  * gfs2_read_super - Read the gfs2 super block from disk
215  * @sdp: The GFS2 super block
216  * @sector: The location of the super block
217  * @error: The error code to return
218  *
219  * This uses the bio functions to read the super block from disk
220  * because we want to be 100% sure that we never read cached data.
221  * A super block is read twice only during each GFS2 mount and is
222  * never written to by the filesystem. The first time its read no
223  * locks are held, and the only details which are looked at are those
224  * relating to the locking protocol. Once locking is up and working,
225  * the sb is read again under the lock to establish the location of
226  * the master directory (contains pointers to journals etc) and the
227  * root directory.
228  *
229  * Returns: 0 on success or error
230  */
231
232 static int gfs2_read_super(struct gfs2_sbd *sdp, sector_t sector, int silent)
233 {
234         struct super_block *sb = sdp->sd_vfs;
235         struct gfs2_sb *p;
236         struct page *page;
237         struct bio *bio;
238
239         page = alloc_page(GFP_NOFS);
240         if (unlikely(!page))
241                 return -ENOMEM;
242
243         ClearPageUptodate(page);
244         ClearPageDirty(page);
245         lock_page(page);
246
247         bio = bio_alloc(GFP_NOFS, 1);
248         bio->bi_iter.bi_sector = sector * (sb->s_blocksize >> 9);
249         bio_set_dev(bio, sb->s_bdev);
250         bio_add_page(bio, page, PAGE_SIZE, 0);
251
252         bio->bi_end_io = end_bio_io_page;
253         bio->bi_private = page;
254         bio_set_op_attrs(bio, REQ_OP_READ, REQ_META);
255         submit_bio(bio);
256         wait_on_page_locked(page);
257         bio_put(bio);
258         if (!PageUptodate(page)) {
259                 __free_page(page);
260                 return -EIO;
261         }
262         p = kmap(page);
263         gfs2_sb_in(sdp, p);
264         kunmap(page);
265         __free_page(page);
266         return gfs2_check_sb(sdp, silent);
267 }
268
269 /**
270  * gfs2_read_sb - Read super block
271  * @sdp: The GFS2 superblock
272  * @silent: Don't print message if mount fails
273  *
274  */
275
276 static int gfs2_read_sb(struct gfs2_sbd *sdp, int silent)
277 {
278         u32 hash_blocks, ind_blocks, leaf_blocks;
279         u32 tmp_blocks;
280         unsigned int x;
281         int error;
282
283         error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift, silent);
284         if (error) {
285                 if (!silent)
286                         fs_err(sdp, "can't read superblock\n");
287                 return error;
288         }
289
290         sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
291                                GFS2_BASIC_BLOCK_SHIFT;
292         sdp->sd_fsb2bb = BIT(sdp->sd_fsb2bb_shift);
293         sdp->sd_diptrs = (sdp->sd_sb.sb_bsize -
294                           sizeof(struct gfs2_dinode)) / sizeof(u64);
295         sdp->sd_inptrs = (sdp->sd_sb.sb_bsize -
296                           sizeof(struct gfs2_meta_header)) / sizeof(u64);
297         sdp->sd_jbsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header);
298         sdp->sd_hash_bsize = sdp->sd_sb.sb_bsize / 2;
299         sdp->sd_hash_bsize_shift = sdp->sd_sb.sb_bsize_shift - 1;
300         sdp->sd_hash_ptrs = sdp->sd_hash_bsize / sizeof(u64);
301         sdp->sd_qc_per_block = (sdp->sd_sb.sb_bsize -
302                                 sizeof(struct gfs2_meta_header)) /
303                                 sizeof(struct gfs2_quota_change);
304         sdp->sd_blocks_per_bitmap = (sdp->sd_sb.sb_bsize -
305                                      sizeof(struct gfs2_meta_header))
306                 * GFS2_NBBY; /* not the rgrp bitmap, subsequent bitmaps only */
307
308         /* Compute maximum reservation required to add a entry to a directory */
309
310         hash_blocks = DIV_ROUND_UP(sizeof(u64) * BIT(GFS2_DIR_MAX_DEPTH),
311                              sdp->sd_jbsize);
312
313         ind_blocks = 0;
314         for (tmp_blocks = hash_blocks; tmp_blocks > sdp->sd_diptrs;) {
315                 tmp_blocks = DIV_ROUND_UP(tmp_blocks, sdp->sd_inptrs);
316                 ind_blocks += tmp_blocks;
317         }
318
319         leaf_blocks = 2 + GFS2_DIR_MAX_DEPTH;
320
321         sdp->sd_max_dirres = hash_blocks + ind_blocks + leaf_blocks;
322
323         sdp->sd_heightsize[0] = sdp->sd_sb.sb_bsize -
324                                 sizeof(struct gfs2_dinode);
325         sdp->sd_heightsize[1] = sdp->sd_sb.sb_bsize * sdp->sd_diptrs;
326         for (x = 2;; x++) {
327                 u64 space, d;
328                 u32 m;
329
330                 space = sdp->sd_heightsize[x - 1] * sdp->sd_inptrs;
331                 d = space;
332                 m = do_div(d, sdp->sd_inptrs);
333
334                 if (d != sdp->sd_heightsize[x - 1] || m)
335                         break;
336                 sdp->sd_heightsize[x] = space;
337         }
338         sdp->sd_max_height = x;
339         sdp->sd_heightsize[x] = ~0;
340         gfs2_assert(sdp, sdp->sd_max_height <= GFS2_MAX_META_HEIGHT);
341
342         sdp->sd_max_dents_per_leaf = (sdp->sd_sb.sb_bsize -
343                                       sizeof(struct gfs2_leaf)) /
344                                      GFS2_MIN_DIRENT_SIZE;
345         return 0;
346 }
347
348 static int init_names(struct gfs2_sbd *sdp, int silent)
349 {
350         char *proto, *table;
351         int error = 0;
352
353         proto = sdp->sd_args.ar_lockproto;
354         table = sdp->sd_args.ar_locktable;
355
356         /*  Try to autodetect  */
357
358         if (!proto[0] || !table[0]) {
359                 error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift, silent);
360                 if (error)
361                         return error;
362
363                 if (!proto[0])
364                         proto = sdp->sd_sb.sb_lockproto;
365                 if (!table[0])
366                         table = sdp->sd_sb.sb_locktable;
367         }
368
369         if (!table[0])
370                 table = sdp->sd_vfs->s_id;
371
372         strlcpy(sdp->sd_proto_name, proto, GFS2_FSNAME_LEN);
373         strlcpy(sdp->sd_table_name, table, GFS2_FSNAME_LEN);
374
375         table = sdp->sd_table_name;
376         while ((table = strchr(table, '/')))
377                 *table = '_';
378
379         return error;
380 }
381
382 static int init_locking(struct gfs2_sbd *sdp, struct gfs2_holder *mount_gh,
383                         int undo)
384 {
385         int error = 0;
386
387         if (undo)
388                 goto fail_trans;
389
390         error = gfs2_glock_nq_num(sdp,
391                                   GFS2_MOUNT_LOCK, &gfs2_nondisk_glops,
392                                   LM_ST_EXCLUSIVE, LM_FLAG_NOEXP | GL_NOCACHE,
393                                   mount_gh);
394         if (error) {
395                 fs_err(sdp, "can't acquire mount glock: %d\n", error);
396                 goto fail;
397         }
398
399         error = gfs2_glock_nq_num(sdp,
400                                   GFS2_LIVE_LOCK, &gfs2_nondisk_glops,
401                                   LM_ST_SHARED,
402                                   LM_FLAG_NOEXP | GL_EXACT,
403                                   &sdp->sd_live_gh);
404         if (error) {
405                 fs_err(sdp, "can't acquire live glock: %d\n", error);
406                 goto fail_mount;
407         }
408
409         error = gfs2_glock_get(sdp, GFS2_RENAME_LOCK, &gfs2_nondisk_glops,
410                                CREATE, &sdp->sd_rename_gl);
411         if (error) {
412                 fs_err(sdp, "can't create rename glock: %d\n", error);
413                 goto fail_live;
414         }
415
416         error = gfs2_glock_get(sdp, GFS2_FREEZE_LOCK, &gfs2_freeze_glops,
417                                CREATE, &sdp->sd_freeze_gl);
418         if (error) {
419                 fs_err(sdp, "can't create transaction glock: %d\n", error);
420                 goto fail_rename;
421         }
422
423         return 0;
424
425 fail_trans:
426         gfs2_glock_put(sdp->sd_freeze_gl);
427 fail_rename:
428         gfs2_glock_put(sdp->sd_rename_gl);
429 fail_live:
430         gfs2_glock_dq_uninit(&sdp->sd_live_gh);
431 fail_mount:
432         gfs2_glock_dq_uninit(mount_gh);
433 fail:
434         return error;
435 }
436
437 static int gfs2_lookup_root(struct super_block *sb, struct dentry **dptr,
438                             u64 no_addr, const char *name)
439 {
440         struct gfs2_sbd *sdp = sb->s_fs_info;
441         struct dentry *dentry;
442         struct inode *inode;
443
444         inode = gfs2_inode_lookup(sb, DT_DIR, no_addr, 0,
445                                   GFS2_BLKST_FREE /* ignore */);
446         if (IS_ERR(inode)) {
447                 fs_err(sdp, "can't read in %s inode: %ld\n", name, PTR_ERR(inode));
448                 return PTR_ERR(inode);
449         }
450         dentry = d_make_root(inode);
451         if (!dentry) {
452                 fs_err(sdp, "can't alloc %s dentry\n", name);
453                 return -ENOMEM;
454         }
455         *dptr = dentry;
456         return 0;
457 }
458
459 static int init_sb(struct gfs2_sbd *sdp, int silent)
460 {
461         struct super_block *sb = sdp->sd_vfs;
462         struct gfs2_holder sb_gh;
463         u64 no_addr;
464         int ret;
465
466         ret = gfs2_glock_nq_num(sdp, GFS2_SB_LOCK, &gfs2_meta_glops,
467                                 LM_ST_SHARED, 0, &sb_gh);
468         if (ret) {
469                 fs_err(sdp, "can't acquire superblock glock: %d\n", ret);
470                 return ret;
471         }
472
473         ret = gfs2_read_sb(sdp, silent);
474         if (ret) {
475                 fs_err(sdp, "can't read superblock: %d\n", ret);
476                 goto out;
477         }
478
479         /* Set up the buffer cache and SB for real */
480         if (sdp->sd_sb.sb_bsize < bdev_logical_block_size(sb->s_bdev)) {
481                 ret = -EINVAL;
482                 fs_err(sdp, "FS block size (%u) is too small for device "
483                        "block size (%u)\n",
484                        sdp->sd_sb.sb_bsize, bdev_logical_block_size(sb->s_bdev));
485                 goto out;
486         }
487         if (sdp->sd_sb.sb_bsize > PAGE_SIZE) {
488                 ret = -EINVAL;
489                 fs_err(sdp, "FS block size (%u) is too big for machine "
490                        "page size (%u)\n",
491                        sdp->sd_sb.sb_bsize, (unsigned int)PAGE_SIZE);
492                 goto out;
493         }
494         sb_set_blocksize(sb, sdp->sd_sb.sb_bsize);
495
496         /* Get the root inode */
497         no_addr = sdp->sd_sb.sb_root_dir.no_addr;
498         ret = gfs2_lookup_root(sb, &sdp->sd_root_dir, no_addr, "root");
499         if (ret)
500                 goto out;
501
502         /* Get the master inode */
503         no_addr = sdp->sd_sb.sb_master_dir.no_addr;
504         ret = gfs2_lookup_root(sb, &sdp->sd_master_dir, no_addr, "master");
505         if (ret) {
506                 dput(sdp->sd_root_dir);
507                 goto out;
508         }
509         sb->s_root = dget(sdp->sd_args.ar_meta ? sdp->sd_master_dir : sdp->sd_root_dir);
510 out:
511         gfs2_glock_dq_uninit(&sb_gh);
512         return ret;
513 }
514
515 static void gfs2_others_may_mount(struct gfs2_sbd *sdp)
516 {
517         char *message = "FIRSTMOUNT=Done";
518         char *envp[] = { message, NULL };
519
520         fs_info(sdp, "first mount done, others may mount\n");
521
522         if (sdp->sd_lockstruct.ls_ops->lm_first_done)
523                 sdp->sd_lockstruct.ls_ops->lm_first_done(sdp);
524
525         kobject_uevent_env(&sdp->sd_kobj, KOBJ_CHANGE, envp);
526 }
527
528 /**
529  * gfs2_jindex_hold - Grab a lock on the jindex
530  * @sdp: The GFS2 superblock
531  * @ji_gh: the holder for the jindex glock
532  *
533  * Returns: errno
534  */
535
536 static int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
537 {
538         struct gfs2_inode *dip = GFS2_I(sdp->sd_jindex);
539         struct qstr name;
540         char buf[20];
541         struct gfs2_jdesc *jd;
542         int error;
543
544         name.name = buf;
545
546         mutex_lock(&sdp->sd_jindex_mutex);
547
548         for (;;) {
549                 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, ji_gh);
550                 if (error)
551                         break;
552
553                 name.len = sprintf(buf, "journal%u", sdp->sd_journals);
554                 name.hash = gfs2_disk_hash(name.name, name.len);
555
556                 error = gfs2_dir_check(sdp->sd_jindex, &name, NULL);
557                 if (error == -ENOENT) {
558                         error = 0;
559                         break;
560                 }
561
562                 gfs2_glock_dq_uninit(ji_gh);
563
564                 if (error)
565                         break;
566
567                 error = -ENOMEM;
568                 jd = kzalloc(sizeof(struct gfs2_jdesc), GFP_KERNEL);
569                 if (!jd)
570                         break;
571
572                 INIT_LIST_HEAD(&jd->extent_list);
573                 INIT_LIST_HEAD(&jd->jd_revoke_list);
574
575                 INIT_WORK(&jd->jd_work, gfs2_recover_func);
576                 jd->jd_inode = gfs2_lookupi(sdp->sd_jindex, &name, 1);
577                 if (!jd->jd_inode || IS_ERR(jd->jd_inode)) {
578                         if (!jd->jd_inode)
579                                 error = -ENOENT;
580                         else
581                                 error = PTR_ERR(jd->jd_inode);
582                         kfree(jd);
583                         break;
584                 }
585
586                 spin_lock(&sdp->sd_jindex_spin);
587                 jd->jd_jid = sdp->sd_journals++;
588                 list_add_tail(&jd->jd_list, &sdp->sd_jindex_list);
589                 spin_unlock(&sdp->sd_jindex_spin);
590         }
591
592         mutex_unlock(&sdp->sd_jindex_mutex);
593
594         return error;
595 }
596
597 /**
598  * check_journal_clean - Make sure a journal is clean for a spectator mount
599  * @sdp: The GFS2 superblock
600  * @jd: The journal descriptor
601  *
602  * Returns: 0 if the journal is clean or locked, else an error
603  */
604 static int check_journal_clean(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd)
605 {
606         int error;
607         struct gfs2_holder j_gh;
608         struct gfs2_log_header_host head;
609         struct gfs2_inode *ip;
610
611         ip = GFS2_I(jd->jd_inode);
612         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_NOEXP |
613                                    GL_EXACT | GL_NOCACHE, &j_gh);
614         if (error) {
615                 fs_err(sdp, "Error locking journal for spectator mount.\n");
616                 return -EPERM;
617         }
618         error = gfs2_jdesc_check(jd);
619         if (error) {
620                 fs_err(sdp, "Error checking journal for spectator mount.\n");
621                 goto out_unlock;
622         }
623         error = gfs2_find_jhead(jd, &head);
624         if (error) {
625                 fs_err(sdp, "Error parsing journal for spectator mount.\n");
626                 goto out_unlock;
627         }
628         if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
629                 error = -EPERM;
630                 fs_err(sdp, "jid=%u: Journal is dirty, so the first mounter "
631                        "must not be a spectator.\n", jd->jd_jid);
632         }
633
634 out_unlock:
635         gfs2_glock_dq_uninit(&j_gh);
636         return error;
637 }
638
639 static int init_journal(struct gfs2_sbd *sdp, int undo)
640 {
641         struct inode *master = d_inode(sdp->sd_master_dir);
642         struct gfs2_holder ji_gh;
643         struct gfs2_inode *ip;
644         int jindex = 1;
645         int error = 0;
646
647         if (undo) {
648                 jindex = 0;
649                 goto fail_jinode_gh;
650         }
651
652         sdp->sd_jindex = gfs2_lookup_simple(master, "jindex");
653         if (IS_ERR(sdp->sd_jindex)) {
654                 fs_err(sdp, "can't lookup journal index: %d\n", error);
655                 return PTR_ERR(sdp->sd_jindex);
656         }
657
658         /* Load in the journal index special file */
659
660         error = gfs2_jindex_hold(sdp, &ji_gh);
661         if (error) {
662                 fs_err(sdp, "can't read journal index: %d\n", error);
663                 goto fail;
664         }
665
666         error = -EUSERS;
667         if (!gfs2_jindex_size(sdp)) {
668                 fs_err(sdp, "no journals!\n");
669                 goto fail_jindex;
670         }
671
672         atomic_set(&sdp->sd_log_blks_needed, 0);
673         if (sdp->sd_args.ar_spectator) {
674                 sdp->sd_jdesc = gfs2_jdesc_find(sdp, 0);
675                 atomic_set(&sdp->sd_log_blks_free, sdp->sd_jdesc->jd_blocks);
676                 atomic_set(&sdp->sd_log_thresh1, 2*sdp->sd_jdesc->jd_blocks/5);
677                 atomic_set(&sdp->sd_log_thresh2, 4*sdp->sd_jdesc->jd_blocks/5);
678         } else {
679                 if (sdp->sd_lockstruct.ls_jid >= gfs2_jindex_size(sdp)) {
680                         fs_err(sdp, "can't mount journal #%u\n",
681                                sdp->sd_lockstruct.ls_jid);
682                         fs_err(sdp, "there are only %u journals (0 - %u)\n",
683                                gfs2_jindex_size(sdp),
684                                gfs2_jindex_size(sdp) - 1);
685                         goto fail_jindex;
686                 }
687                 sdp->sd_jdesc = gfs2_jdesc_find(sdp, sdp->sd_lockstruct.ls_jid);
688
689                 error = gfs2_glock_nq_num(sdp, sdp->sd_lockstruct.ls_jid,
690                                           &gfs2_journal_glops,
691                                           LM_ST_EXCLUSIVE, LM_FLAG_NOEXP,
692                                           &sdp->sd_journal_gh);
693                 if (error) {
694                         fs_err(sdp, "can't acquire journal glock: %d\n", error);
695                         goto fail_jindex;
696                 }
697
698                 ip = GFS2_I(sdp->sd_jdesc->jd_inode);
699                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
700                                            LM_FLAG_NOEXP | GL_EXACT | GL_NOCACHE,
701                                            &sdp->sd_jinode_gh);
702                 if (error) {
703                         fs_err(sdp, "can't acquire journal inode glock: %d\n",
704                                error);
705                         goto fail_journal_gh;
706                 }
707
708                 error = gfs2_jdesc_check(sdp->sd_jdesc);
709                 if (error) {
710                         fs_err(sdp, "my journal (%u) is bad: %d\n",
711                                sdp->sd_jdesc->jd_jid, error);
712                         goto fail_jinode_gh;
713                 }
714                 atomic_set(&sdp->sd_log_blks_free, sdp->sd_jdesc->jd_blocks);
715                 atomic_set(&sdp->sd_log_thresh1, 2*sdp->sd_jdesc->jd_blocks/5);
716                 atomic_set(&sdp->sd_log_thresh2, 4*sdp->sd_jdesc->jd_blocks/5);
717
718                 /* Map the extents for this journal's blocks */
719                 gfs2_map_journal_extents(sdp, sdp->sd_jdesc);
720         }
721         trace_gfs2_log_blocks(sdp, atomic_read(&sdp->sd_log_blks_free));
722
723         if (sdp->sd_lockstruct.ls_first) {
724                 unsigned int x;
725                 for (x = 0; x < sdp->sd_journals; x++) {
726                         struct gfs2_jdesc *jd = gfs2_jdesc_find(sdp, x);
727
728                         if (sdp->sd_args.ar_spectator) {
729                                 error = check_journal_clean(sdp, jd);
730                                 if (error)
731                                         goto fail_jinode_gh;
732                                 continue;
733                         }
734                         error = gfs2_recover_journal(jd, true);
735                         if (error) {
736                                 fs_err(sdp, "error recovering journal %u: %d\n",
737                                        x, error);
738                                 goto fail_jinode_gh;
739                         }
740                 }
741
742                 gfs2_others_may_mount(sdp);
743         } else if (!sdp->sd_args.ar_spectator) {
744                 error = gfs2_recover_journal(sdp->sd_jdesc, true);
745                 if (error) {
746                         fs_err(sdp, "error recovering my journal: %d\n", error);
747                         goto fail_jinode_gh;
748                 }
749         }
750
751         sdp->sd_log_idle = 1;
752         set_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags);
753         gfs2_glock_dq_uninit(&ji_gh);
754         jindex = 0;
755         INIT_WORK(&sdp->sd_freeze_work, gfs2_freeze_func);
756         return 0;
757
758 fail_jinode_gh:
759         if (!sdp->sd_args.ar_spectator)
760                 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
761 fail_journal_gh:
762         if (!sdp->sd_args.ar_spectator)
763                 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
764 fail_jindex:
765         gfs2_jindex_free(sdp);
766         if (jindex)
767                 gfs2_glock_dq_uninit(&ji_gh);
768 fail:
769         iput(sdp->sd_jindex);
770         return error;
771 }
772
773 static struct lock_class_key gfs2_quota_imutex_key;
774
775 static int init_inodes(struct gfs2_sbd *sdp, int undo)
776 {
777         int error = 0;
778         struct inode *master = d_inode(sdp->sd_master_dir);
779
780         if (undo)
781                 goto fail_qinode;
782
783         error = init_journal(sdp, undo);
784         complete_all(&sdp->sd_journal_ready);
785         if (error)
786                 goto fail;
787
788         /* Read in the master statfs inode */
789         sdp->sd_statfs_inode = gfs2_lookup_simple(master, "statfs");
790         if (IS_ERR(sdp->sd_statfs_inode)) {
791                 error = PTR_ERR(sdp->sd_statfs_inode);
792                 fs_err(sdp, "can't read in statfs inode: %d\n", error);
793                 goto fail_journal;
794         }
795
796         /* Read in the resource index inode */
797         sdp->sd_rindex = gfs2_lookup_simple(master, "rindex");
798         if (IS_ERR(sdp->sd_rindex)) {
799                 error = PTR_ERR(sdp->sd_rindex);
800                 fs_err(sdp, "can't get resource index inode: %d\n", error);
801                 goto fail_statfs;
802         }
803         sdp->sd_rindex_uptodate = 0;
804
805         /* Read in the quota inode */
806         sdp->sd_quota_inode = gfs2_lookup_simple(master, "quota");
807         if (IS_ERR(sdp->sd_quota_inode)) {
808                 error = PTR_ERR(sdp->sd_quota_inode);
809                 fs_err(sdp, "can't get quota file inode: %d\n", error);
810                 goto fail_rindex;
811         }
812         /*
813          * i_rwsem on quota files is special. Since this inode is hidden system
814          * file, we are safe to define locking ourselves.
815          */
816         lockdep_set_class(&sdp->sd_quota_inode->i_rwsem,
817                           &gfs2_quota_imutex_key);
818
819         error = gfs2_rindex_update(sdp);
820         if (error)
821                 goto fail_qinode;
822
823         return 0;
824
825 fail_qinode:
826         iput(sdp->sd_quota_inode);
827 fail_rindex:
828         gfs2_clear_rgrpd(sdp);
829         iput(sdp->sd_rindex);
830 fail_statfs:
831         iput(sdp->sd_statfs_inode);
832 fail_journal:
833         init_journal(sdp, UNDO);
834 fail:
835         return error;
836 }
837
838 static int init_per_node(struct gfs2_sbd *sdp, int undo)
839 {
840         struct inode *pn = NULL;
841         char buf[30];
842         int error = 0;
843         struct gfs2_inode *ip;
844         struct inode *master = d_inode(sdp->sd_master_dir);
845
846         if (sdp->sd_args.ar_spectator)
847                 return 0;
848
849         if (undo)
850                 goto fail_qc_gh;
851
852         pn = gfs2_lookup_simple(master, "per_node");
853         if (IS_ERR(pn)) {
854                 error = PTR_ERR(pn);
855                 fs_err(sdp, "can't find per_node directory: %d\n", error);
856                 return error;
857         }
858
859         sprintf(buf, "statfs_change%u", sdp->sd_jdesc->jd_jid);
860         sdp->sd_sc_inode = gfs2_lookup_simple(pn, buf);
861         if (IS_ERR(sdp->sd_sc_inode)) {
862                 error = PTR_ERR(sdp->sd_sc_inode);
863                 fs_err(sdp, "can't find local \"sc\" file: %d\n", error);
864                 goto fail;
865         }
866
867         sprintf(buf, "quota_change%u", sdp->sd_jdesc->jd_jid);
868         sdp->sd_qc_inode = gfs2_lookup_simple(pn, buf);
869         if (IS_ERR(sdp->sd_qc_inode)) {
870                 error = PTR_ERR(sdp->sd_qc_inode);
871                 fs_err(sdp, "can't find local \"qc\" file: %d\n", error);
872                 goto fail_ut_i;
873         }
874
875         iput(pn);
876         pn = NULL;
877
878         ip = GFS2_I(sdp->sd_sc_inode);
879         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0,
880                                    &sdp->sd_sc_gh);
881         if (error) {
882                 fs_err(sdp, "can't lock local \"sc\" file: %d\n", error);
883                 goto fail_qc_i;
884         }
885
886         ip = GFS2_I(sdp->sd_qc_inode);
887         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0,
888                                    &sdp->sd_qc_gh);
889         if (error) {
890                 fs_err(sdp, "can't lock local \"qc\" file: %d\n", error);
891                 goto fail_ut_gh;
892         }
893
894         return 0;
895
896 fail_qc_gh:
897         gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
898 fail_ut_gh:
899         gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
900 fail_qc_i:
901         iput(sdp->sd_qc_inode);
902 fail_ut_i:
903         iput(sdp->sd_sc_inode);
904 fail:
905         iput(pn);
906         return error;
907 }
908
909 static const match_table_t nolock_tokens = {
910         { Opt_jid, "jid=%d", },
911         { Opt_err, NULL },
912 };
913
914 static const struct lm_lockops nolock_ops = {
915         .lm_proto_name = "lock_nolock",
916         .lm_put_lock = gfs2_glock_free,
917         .lm_tokens = &nolock_tokens,
918 };
919
920 /**
921  * gfs2_lm_mount - mount a locking protocol
922  * @sdp: the filesystem
923  * @args: mount arguments
924  * @silent: if 1, don't complain if the FS isn't a GFS2 fs
925  *
926  * Returns: errno
927  */
928
929 static int gfs2_lm_mount(struct gfs2_sbd *sdp, int silent)
930 {
931         const struct lm_lockops *lm;
932         struct lm_lockstruct *ls = &sdp->sd_lockstruct;
933         struct gfs2_args *args = &sdp->sd_args;
934         const char *proto = sdp->sd_proto_name;
935         const char *table = sdp->sd_table_name;
936         char *o, *options;
937         int ret;
938
939         if (!strcmp("lock_nolock", proto)) {
940                 lm = &nolock_ops;
941                 sdp->sd_args.ar_localflocks = 1;
942 #ifdef CONFIG_GFS2_FS_LOCKING_DLM
943         } else if (!strcmp("lock_dlm", proto)) {
944                 lm = &gfs2_dlm_ops;
945 #endif
946         } else {
947                 pr_info("can't find protocol %s\n", proto);
948                 return -ENOENT;
949         }
950
951         fs_info(sdp, "Trying to join cluster \"%s\", \"%s\"\n", proto, table);
952
953         ls->ls_ops = lm;
954         ls->ls_first = 1;
955
956         for (options = args->ar_hostdata; (o = strsep(&options, ":")); ) {
957                 substring_t tmp[MAX_OPT_ARGS];
958                 int token, option;
959
960                 if (!o || !*o)
961                         continue;
962
963                 token = match_token(o, *lm->lm_tokens, tmp);
964                 switch (token) {
965                 case Opt_jid:
966                         ret = match_int(&tmp[0], &option);
967                         if (ret || option < 0) 
968                                 goto hostdata_error;
969                         if (test_and_clear_bit(SDF_NOJOURNALID, &sdp->sd_flags))
970                                 ls->ls_jid = option;
971                         break;
972                 case Opt_id:
973                 case Opt_nodir:
974                         /* Obsolete, but left for backward compat purposes */
975                         break;
976                 case Opt_first:
977                         ret = match_int(&tmp[0], &option);
978                         if (ret || (option != 0 && option != 1))
979                                 goto hostdata_error;
980                         ls->ls_first = option;
981                         break;
982                 case Opt_err:
983                 default:
984 hostdata_error:
985                         fs_info(sdp, "unknown hostdata (%s)\n", o);
986                         return -EINVAL;
987                 }
988         }
989
990         if (lm->lm_mount == NULL) {
991                 fs_info(sdp, "Now mounting FS...\n");
992                 complete_all(&sdp->sd_locking_init);
993                 return 0;
994         }
995         ret = lm->lm_mount(sdp, table);
996         if (ret == 0)
997                 fs_info(sdp, "Joined cluster. Now mounting FS...\n");
998         complete_all(&sdp->sd_locking_init);
999         return ret;
1000 }
1001
1002 void gfs2_lm_unmount(struct gfs2_sbd *sdp)
1003 {
1004         const struct lm_lockops *lm = sdp->sd_lockstruct.ls_ops;
1005         if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) &&
1006             lm->lm_unmount)
1007                 lm->lm_unmount(sdp);
1008 }
1009
1010 static int wait_on_journal(struct gfs2_sbd *sdp)
1011 {
1012         if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
1013                 return 0;
1014
1015         return wait_on_bit(&sdp->sd_flags, SDF_NOJOURNALID, TASK_INTERRUPTIBLE)
1016                 ? -EINTR : 0;
1017 }
1018
1019 void gfs2_online_uevent(struct gfs2_sbd *sdp)
1020 {
1021         struct super_block *sb = sdp->sd_vfs;
1022         char ro[20];
1023         char spectator[20];
1024         char *envp[] = { ro, spectator, NULL };
1025         sprintf(ro, "RDONLY=%d", sb_rdonly(sb));
1026         sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
1027         kobject_uevent_env(&sdp->sd_kobj, KOBJ_ONLINE, envp);
1028 }
1029
1030 /**
1031  * fill_super - Read in superblock
1032  * @sb: The VFS superblock
1033  * @data: Mount options
1034  * @silent: Don't complain if it's not a GFS2 filesystem
1035  *
1036  * Returns: errno
1037  */
1038
1039 static int fill_super(struct super_block *sb, struct gfs2_args *args, int silent)
1040 {
1041         struct gfs2_sbd *sdp;
1042         struct gfs2_holder mount_gh;
1043         int error;
1044
1045         sdp = init_sbd(sb);
1046         if (!sdp) {
1047                 pr_warn("can't alloc struct gfs2_sbd\n");
1048                 return -ENOMEM;
1049         }
1050         sdp->sd_args = *args;
1051
1052         if (sdp->sd_args.ar_spectator) {
1053                 sb->s_flags |= SB_RDONLY;
1054                 set_bit(SDF_RORECOVERY, &sdp->sd_flags);
1055         }
1056         if (sdp->sd_args.ar_posix_acl)
1057                 sb->s_flags |= SB_POSIXACL;
1058         if (sdp->sd_args.ar_nobarrier)
1059                 set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
1060
1061         sb->s_flags |= SB_NOSEC;
1062         sb->s_magic = GFS2_MAGIC;
1063         sb->s_op = &gfs2_super_ops;
1064         sb->s_d_op = &gfs2_dops;
1065         sb->s_export_op = &gfs2_export_ops;
1066         sb->s_xattr = gfs2_xattr_handlers;
1067         sb->s_qcop = &gfs2_quotactl_ops;
1068         sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
1069         sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
1070         sb->s_time_gran = 1;
1071         sb->s_maxbytes = MAX_LFS_FILESIZE;
1072
1073         /* Set up the buffer cache and fill in some fake block size values
1074            to allow us to read-in the on-disk superblock. */
1075         sdp->sd_sb.sb_bsize = sb_min_blocksize(sb, GFS2_BASIC_BLOCK);
1076         sdp->sd_sb.sb_bsize_shift = sb->s_blocksize_bits;
1077         sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
1078                                GFS2_BASIC_BLOCK_SHIFT;
1079         sdp->sd_fsb2bb = BIT(sdp->sd_fsb2bb_shift);
1080
1081         sdp->sd_tune.gt_logd_secs = sdp->sd_args.ar_commit;
1082         sdp->sd_tune.gt_quota_quantum = sdp->sd_args.ar_quota_quantum;
1083         if (sdp->sd_args.ar_statfs_quantum) {
1084                 sdp->sd_tune.gt_statfs_slow = 0;
1085                 sdp->sd_tune.gt_statfs_quantum = sdp->sd_args.ar_statfs_quantum;
1086         } else {
1087                 sdp->sd_tune.gt_statfs_slow = 1;
1088                 sdp->sd_tune.gt_statfs_quantum = 30;
1089         }
1090
1091         error = init_names(sdp, silent);
1092         if (error) {
1093                 /* In this case, we haven't initialized sysfs, so we have to
1094                    manually free the sdp. */
1095                 free_percpu(sdp->sd_lkstats);
1096                 kfree(sdp);
1097                 sb->s_fs_info = NULL;
1098                 return error;
1099         }
1100
1101         snprintf(sdp->sd_fsname, sizeof(sdp->sd_fsname), "%s", sdp->sd_table_name);
1102
1103         error = gfs2_sys_fs_add(sdp);
1104         /*
1105          * If we hit an error here, gfs2_sys_fs_add will have called function
1106          * kobject_put which causes the sysfs usage count to go to zero, which
1107          * causes sysfs to call function gfs2_sbd_release, which frees sdp.
1108          * Subsequent error paths here will call gfs2_sys_fs_del, which also
1109          * kobject_put to free sdp.
1110          */
1111         if (error)
1112                 return error;
1113
1114         gfs2_create_debugfs_file(sdp);
1115
1116         error = gfs2_lm_mount(sdp, silent);
1117         if (error)
1118                 goto fail_debug;
1119
1120         error = init_locking(sdp, &mount_gh, DO);
1121         if (error)
1122                 goto fail_lm;
1123
1124         error = init_sb(sdp, silent);
1125         if (error)
1126                 goto fail_locking;
1127
1128         error = wait_on_journal(sdp);
1129         if (error)
1130                 goto fail_sb;
1131
1132         /*
1133          * If user space has failed to join the cluster or some similar
1134          * failure has occurred, then the journal id will contain a
1135          * negative (error) number. This will then be returned to the
1136          * caller (of the mount syscall). We do this even for spectator
1137          * mounts (which just write a jid of 0 to indicate "ok" even though
1138          * the jid is unused in the spectator case)
1139          */
1140         if (sdp->sd_lockstruct.ls_jid < 0) {
1141                 error = sdp->sd_lockstruct.ls_jid;
1142                 sdp->sd_lockstruct.ls_jid = 0;
1143                 goto fail_sb;
1144         }
1145
1146         if (sdp->sd_args.ar_spectator)
1147                 snprintf(sdp->sd_fsname, sizeof(sdp->sd_fsname), "%s.s",
1148                          sdp->sd_table_name);
1149         else
1150                 snprintf(sdp->sd_fsname, sizeof(sdp->sd_fsname), "%s.%u",
1151                          sdp->sd_table_name, sdp->sd_lockstruct.ls_jid);
1152
1153         error = init_inodes(sdp, DO);
1154         if (error)
1155                 goto fail_sb;
1156
1157         error = init_per_node(sdp, DO);
1158         if (error)
1159                 goto fail_inodes;
1160
1161         error = gfs2_statfs_init(sdp);
1162         if (error) {
1163                 fs_err(sdp, "can't initialize statfs subsystem: %d\n", error);
1164                 goto fail_per_node;
1165         }
1166
1167         if (sb_rdonly(sb)) {
1168                 struct gfs2_holder freeze_gh;
1169
1170                 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED,
1171                                            GL_EXACT, &freeze_gh);
1172                 if (error) {
1173                         fs_err(sdp, "can't make FS RO: %d\n", error);
1174                         goto fail_per_node;
1175                 }
1176                 gfs2_glock_dq_uninit(&freeze_gh);
1177         } else {
1178                 error = gfs2_make_fs_rw(sdp);
1179                 if (error) {
1180                         fs_err(sdp, "can't make FS RW: %d\n", error);
1181                         goto fail_per_node;
1182                 }
1183         }
1184
1185         gfs2_glock_dq_uninit(&mount_gh);
1186         gfs2_online_uevent(sdp);
1187         return 0;
1188
1189 fail_per_node:
1190         init_per_node(sdp, UNDO);
1191 fail_inodes:
1192         init_inodes(sdp, UNDO);
1193 fail_sb:
1194         if (sdp->sd_root_dir)
1195                 dput(sdp->sd_root_dir);
1196         if (sdp->sd_master_dir)
1197                 dput(sdp->sd_master_dir);
1198         if (sb->s_root)
1199                 dput(sb->s_root);
1200         sb->s_root = NULL;
1201 fail_locking:
1202         init_locking(sdp, &mount_gh, UNDO);
1203 fail_lm:
1204         complete_all(&sdp->sd_journal_ready);
1205         gfs2_gl_hash_clear(sdp);
1206         gfs2_lm_unmount(sdp);
1207 fail_debug:
1208         gfs2_delete_debugfs_file(sdp);
1209         free_percpu(sdp->sd_lkstats);
1210         /* gfs2_sys_fs_del must be the last thing we do, since it causes
1211          * sysfs to call function gfs2_sbd_release, which frees sdp. */
1212         gfs2_sys_fs_del(sdp);
1213         sb->s_fs_info = NULL;
1214         return error;
1215 }
1216
1217 static int set_gfs2_super(struct super_block *s, void *data)
1218 {
1219         s->s_bdev = data;
1220         s->s_dev = s->s_bdev->bd_dev;
1221         s->s_bdi = bdi_get(s->s_bdev->bd_bdi);
1222         return 0;
1223 }
1224
1225 static int test_gfs2_super(struct super_block *s, void *ptr)
1226 {
1227         struct block_device *bdev = ptr;
1228         return (bdev == s->s_bdev);
1229 }
1230
1231 /**
1232  * gfs2_mount - Get the GFS2 superblock
1233  * @fs_type: The GFS2 filesystem type
1234  * @flags: Mount flags
1235  * @dev_name: The name of the device
1236  * @data: The mount arguments
1237  *
1238  * Q. Why not use get_sb_bdev() ?
1239  * A. We need to select one of two root directories to mount, independent
1240  *    of whether this is the initial, or subsequent, mount of this sb
1241  *
1242  * Returns: 0 or -ve on error
1243  */
1244
1245 static struct dentry *gfs2_mount(struct file_system_type *fs_type, int flags,
1246                        const char *dev_name, void *data)
1247 {
1248         struct block_device *bdev;
1249         struct super_block *s;
1250         fmode_t mode = FMODE_READ | FMODE_EXCL;
1251         int error;
1252         struct gfs2_args args;
1253         struct gfs2_sbd *sdp;
1254
1255         if (!(flags & SB_RDONLY))
1256                 mode |= FMODE_WRITE;
1257
1258         bdev = blkdev_get_by_path(dev_name, mode, fs_type);
1259         if (IS_ERR(bdev))
1260                 return ERR_CAST(bdev);
1261
1262         /*
1263          * once the super is inserted into the list by sget, s_umount
1264          * will protect the lockfs code from trying to start a snapshot
1265          * while we are mounting
1266          */
1267         mutex_lock(&bdev->bd_fsfreeze_mutex);
1268         if (bdev->bd_fsfreeze_count > 0) {
1269                 mutex_unlock(&bdev->bd_fsfreeze_mutex);
1270                 error = -EBUSY;
1271                 goto error_bdev;
1272         }
1273         s = sget(fs_type, test_gfs2_super, set_gfs2_super, flags, bdev);
1274         mutex_unlock(&bdev->bd_fsfreeze_mutex);
1275         error = PTR_ERR(s);
1276         if (IS_ERR(s))
1277                 goto error_bdev;
1278
1279         if (s->s_root) {
1280                 /*
1281                  * s_umount nests inside bd_mutex during
1282                  * __invalidate_device().  blkdev_put() acquires
1283                  * bd_mutex and can't be called under s_umount.  Drop
1284                  * s_umount temporarily.  This is safe as we're
1285                  * holding an active reference.
1286                  */
1287                 up_write(&s->s_umount);
1288                 blkdev_put(bdev, mode);
1289                 down_write(&s->s_umount);
1290         } else {
1291                 /* s_mode must be set before deactivate_locked_super calls */
1292                 s->s_mode = mode;
1293         }
1294
1295         memset(&args, 0, sizeof(args));
1296         args.ar_quota = GFS2_QUOTA_DEFAULT;
1297         args.ar_data = GFS2_DATA_DEFAULT;
1298         args.ar_commit = 30;
1299         args.ar_statfs_quantum = 30;
1300         args.ar_quota_quantum = 60;
1301         args.ar_errors = GFS2_ERRORS_DEFAULT;
1302
1303         error = gfs2_mount_args(&args, data);
1304         if (error) {
1305                 pr_warn("can't parse mount arguments\n");
1306                 goto error_super;
1307         }
1308
1309         if (s->s_root) {
1310                 error = -EBUSY;
1311                 if ((flags ^ s->s_flags) & SB_RDONLY)
1312                         goto error_super;
1313         } else {
1314                 snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1315                 sb_set_blocksize(s, block_size(bdev));
1316                 error = fill_super(s, &args, flags & SB_SILENT ? 1 : 0);
1317                 if (error)
1318                         goto error_super;
1319                 s->s_flags |= SB_ACTIVE;
1320                 bdev->bd_super = s;
1321         }
1322
1323         sdp = s->s_fs_info;
1324         if (args.ar_meta)
1325                 return dget(sdp->sd_master_dir);
1326         else
1327                 return dget(sdp->sd_root_dir);
1328
1329 error_super:
1330         deactivate_locked_super(s);
1331         return ERR_PTR(error);
1332 error_bdev:
1333         blkdev_put(bdev, mode);
1334         return ERR_PTR(error);
1335 }
1336
1337 static int set_meta_super(struct super_block *s, void *ptr)
1338 {
1339         return -EINVAL;
1340 }
1341
1342 static struct dentry *gfs2_mount_meta(struct file_system_type *fs_type,
1343                         int flags, const char *dev_name, void *data)
1344 {
1345         struct super_block *s;
1346         struct gfs2_sbd *sdp;
1347         struct path path;
1348         int error;
1349
1350         if (!dev_name || !*dev_name)
1351                 return ERR_PTR(-EINVAL);
1352
1353         error = kern_path(dev_name, LOOKUP_FOLLOW, &path);
1354         if (error) {
1355                 pr_warn("path_lookup on %s returned error %d\n",
1356                         dev_name, error);
1357                 return ERR_PTR(error);
1358         }
1359         s = sget(&gfs2_fs_type, test_gfs2_super, set_meta_super, flags,
1360                  path.dentry->d_sb->s_bdev);
1361         path_put(&path);
1362         if (IS_ERR(s)) {
1363                 pr_warn("gfs2 mount does not exist\n");
1364                 return ERR_CAST(s);
1365         }
1366         if ((flags ^ s->s_flags) & SB_RDONLY) {
1367                 deactivate_locked_super(s);
1368                 return ERR_PTR(-EBUSY);
1369         }
1370         sdp = s->s_fs_info;
1371         return dget(sdp->sd_master_dir);
1372 }
1373
1374 static void gfs2_kill_sb(struct super_block *sb)
1375 {
1376         struct gfs2_sbd *sdp = sb->s_fs_info;
1377
1378         if (sdp == NULL) {
1379                 kill_block_super(sb);
1380                 return;
1381         }
1382
1383         gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_SYNC | GFS2_LFC_KILL_SB);
1384         dput(sdp->sd_root_dir);
1385         dput(sdp->sd_master_dir);
1386         sdp->sd_root_dir = NULL;
1387         sdp->sd_master_dir = NULL;
1388         shrink_dcache_sb(sb);
1389         free_percpu(sdp->sd_lkstats);
1390         kill_block_super(sb);
1391 }
1392
1393 struct file_system_type gfs2_fs_type = {
1394         .name = "gfs2",
1395         .fs_flags = FS_REQUIRES_DEV,
1396         .mount = gfs2_mount,
1397         .kill_sb = gfs2_kill_sb,
1398         .owner = THIS_MODULE,
1399 };
1400 MODULE_ALIAS_FS("gfs2");
1401
1402 struct file_system_type gfs2meta_fs_type = {
1403         .name = "gfs2meta",
1404         .fs_flags = FS_REQUIRES_DEV,
1405         .mount = gfs2_mount_meta,
1406         .owner = THIS_MODULE,
1407 };
1408 MODULE_ALIAS_FS("gfs2meta");