~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/gfs2/ops_fstype.c

Version: ~ [ linux-6.6-rc1 ] ~ [ linux-6.5.2 ] ~ [ linux-6.4.15 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.52 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.131 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.194 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.256 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.294 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.325 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp