1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
4 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
5 */
6
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
9 #include <linux/sched.h>
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/blkdev.h>
15 #include <linux/kthread.h>
16 #include <linux/export.h>
17 #include <linux/namei.h>
18 #include <linux/mount.h>
19 #include <linux/gfs2_ondisk.h>
20 #include <linux/quotaops.h>
21 #include <linux/lockdep.h>
22 #include <linux/module.h>
23 #include <linux/backing-dev.h>
24 #include <linux/fs_parser.h>
25
26 #include "gfs2.h"
27 #include "incore.h"
28 #include "bmap.h"
29 #include "glock.h"
30 #include "glops.h"
31 #include "inode.h"
32 #include "recovery.h"
33 #include "rgrp.h"
34 #include "super.h"
35 #include "sys.h"
36 #include "util.h"
37 #include "log.h"
38 #include "quota.h"
39 #include "dir.h"
40 #include "meta_io.h"
41 #include "trace_gfs2.h"
42 #include "lops.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
gfs2_tune_init(struct gfs2_tune * gt)53 static void gfs2_tune_init(struct gfs2_tune *gt)
54 {
55 spin_lock_init(>->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
free_sbd(struct gfs2_sbd * sdp)65 void free_sbd(struct gfs2_sbd *sdp)
66 {
67 if (sdp->sd_lkstats)
68 free_percpu(sdp->sd_lkstats);
69 kfree(sdp);
70 }
71
init_sbd(struct super_block * sb)72 static struct gfs2_sbd *init_sbd(struct super_block *sb)
73 {
74 struct gfs2_sbd *sdp;
75 struct address_space *mapping;
76
77 sdp = kzalloc(sizeof(struct gfs2_sbd), GFP_KERNEL);
78 if (!sdp)
79 return NULL;
80
81 sdp->sd_vfs = sb;
82 sdp->sd_lkstats = alloc_percpu(struct gfs2_pcpu_lkstats);
83 if (!sdp->sd_lkstats)
84 goto fail;
85 sb->s_fs_info = sdp;
86
87 set_bit(SDF_NOJOURNALID, &sdp->sd_flags);
88 gfs2_tune_init(&sdp->sd_tune);
89
90 init_waitqueue_head(&sdp->sd_glock_wait);
91 init_waitqueue_head(&sdp->sd_async_glock_wait);
92 atomic_set(&sdp->sd_glock_disposal, 0);
93 init_completion(&sdp->sd_locking_init);
94 init_completion(&sdp->sd_wdack);
95 spin_lock_init(&sdp->sd_statfs_spin);
96
97 spin_lock_init(&sdp->sd_rindex_spin);
98 sdp->sd_rindex_tree.rb_node = NULL;
99
100 INIT_LIST_HEAD(&sdp->sd_jindex_list);
101 spin_lock_init(&sdp->sd_jindex_spin);
102 mutex_init(&sdp->sd_jindex_mutex);
103 init_completion(&sdp->sd_journal_ready);
104
105 INIT_LIST_HEAD(&sdp->sd_quota_list);
106 mutex_init(&sdp->sd_quota_mutex);
107 mutex_init(&sdp->sd_quota_sync_mutex);
108 init_waitqueue_head(&sdp->sd_quota_wait);
109 INIT_LIST_HEAD(&sdp->sd_trunc_list);
110 spin_lock_init(&sdp->sd_trunc_lock);
111 spin_lock_init(&sdp->sd_bitmap_lock);
112
113 INIT_LIST_HEAD(&sdp->sd_sc_inodes_list);
114
115 mapping = &sdp->sd_aspace;
116
117 address_space_init_once(mapping);
118 mapping->a_ops = &gfs2_rgrp_aops;
119 mapping->host = sb->s_bdev->bd_inode;
120 mapping->flags = 0;
121 mapping_set_gfp_mask(mapping, GFP_NOFS);
122 mapping->private_data = NULL;
123 mapping->writeback_index = 0;
124
125 spin_lock_init(&sdp->sd_log_lock);
126 atomic_set(&sdp->sd_log_pinned, 0);
127 INIT_LIST_HEAD(&sdp->sd_log_revokes);
128 INIT_LIST_HEAD(&sdp->sd_log_ordered);
129 spin_lock_init(&sdp->sd_ordered_lock);
130
131 init_waitqueue_head(&sdp->sd_log_waitq);
132 init_waitqueue_head(&sdp->sd_logd_waitq);
133 spin_lock_init(&sdp->sd_ail_lock);
134 INIT_LIST_HEAD(&sdp->sd_ail1_list);
135 INIT_LIST_HEAD(&sdp->sd_ail2_list);
136
137 init_rwsem(&sdp->sd_log_flush_lock);
138 atomic_set(&sdp->sd_log_in_flight, 0);
139 init_waitqueue_head(&sdp->sd_log_flush_wait);
140 atomic_set(&sdp->sd_freeze_state, SFS_UNFROZEN);
141 mutex_init(&sdp->sd_freeze_mutex);
142
143 return sdp;
144
145 fail:
146 free_sbd(sdp);
147 return NULL;
148 }
149
150 /**
151 * gfs2_check_sb - Check superblock
152 * @sdp: the filesystem
153 * @silent: Don't print a message if the check fails
154 *
155 * Checks the version code of the FS is one that we understand how to
156 * read and that the sizes of the various on-disk structures have not
157 * changed.
158 */
159
gfs2_check_sb(struct gfs2_sbd * sdp,int silent)160 static int gfs2_check_sb(struct gfs2_sbd *sdp, int silent)
161 {
162 struct gfs2_sb_host *sb = &sdp->sd_sb;
163
164 if (sb->sb_magic != GFS2_MAGIC ||
165 sb->sb_type != GFS2_METATYPE_SB) {
166 if (!silent)
167 pr_warn("not a GFS2 filesystem\n");
168 return -EINVAL;
169 }
170
171 if (sb->sb_fs_format < GFS2_FS_FORMAT_MIN ||
172 sb->sb_fs_format > GFS2_FS_FORMAT_MAX ||
173 sb->sb_multihost_format != GFS2_FORMAT_MULTI) {
174 fs_warn(sdp, "Unknown on-disk format, unable to mount\n");
175 return -EINVAL;
176 }
177
178 if (sb->sb_bsize < 512 || sb->sb_bsize > PAGE_SIZE ||
179 (sb->sb_bsize & (sb->sb_bsize - 1))) {
180 pr_warn("Invalid block size\n");
181 return -EINVAL;
182 }
183 if (sb->sb_bsize_shift != ffs(sb->sb_bsize) - 1) {
184 pr_warn("Invalid block size shift\n");
185 return -EINVAL;
186 }
187 return 0;
188 }
189
end_bio_io_page(struct bio * bio)190 static void end_bio_io_page(struct bio *bio)
191 {
192 struct page *page = bio->bi_private;
193
194 if (!bio->bi_status)
195 SetPageUptodate(page);
196 else
197 pr_warn("error %d reading superblock\n", bio->bi_status);
198 unlock_page(page);
199 }
200
gfs2_sb_in(struct gfs2_sbd * sdp,const void * buf)201 static void gfs2_sb_in(struct gfs2_sbd *sdp, const void *buf)
202 {
203 struct gfs2_sb_host *sb = &sdp->sd_sb;
204 struct super_block *s = sdp->sd_vfs;
205 const struct gfs2_sb *str = buf;
206
207 sb->sb_magic = be32_to_cpu(str->sb_header.mh_magic);
208 sb->sb_type = be32_to_cpu(str->sb_header.mh_type);
209 sb->sb_fs_format = be32_to_cpu(str->sb_fs_format);
210 sb->sb_multihost_format = be32_to_cpu(str->sb_multihost_format);
211 sb->sb_bsize = be32_to_cpu(str->sb_bsize);
212 sb->sb_bsize_shift = be32_to_cpu(str->sb_bsize_shift);
213 sb->sb_master_dir.no_addr = be64_to_cpu(str->sb_master_dir.no_addr);
214 sb->sb_master_dir.no_formal_ino = be64_to_cpu(str->sb_master_dir.no_formal_ino);
215 sb->sb_root_dir.no_addr = be64_to_cpu(str->sb_root_dir.no_addr);
216 sb->sb_root_dir.no_formal_ino = be64_to_cpu(str->sb_root_dir.no_formal_ino);
217
218 memcpy(sb->sb_lockproto, str->sb_lockproto, GFS2_LOCKNAME_LEN);
219 memcpy(sb->sb_locktable, str->sb_locktable, GFS2_LOCKNAME_LEN);
220 memcpy(&s->s_uuid, str->sb_uuid, 16);
221 }
222
223 /**
224 * gfs2_read_super - Read the gfs2 super block from disk
225 * @sdp: The GFS2 super block
226 * @sector: The location of the super block
227 * @silent: Don't print a message if the check fails
228 *
229 * This uses the bio functions to read the super block from disk
230 * because we want to be 100% sure that we never read cached data.
231 * A super block is read twice only during each GFS2 mount and is
232 * never written to by the filesystem. The first time its read no
233 * locks are held, and the only details which are looked at are those
234 * relating to the locking protocol. Once locking is up and working,
235 * the sb is read again under the lock to establish the location of
236 * the master directory (contains pointers to journals etc) and the
237 * root directory.
238 *
239 * Returns: 0 on success or error
240 */
241
gfs2_read_super(struct gfs2_sbd * sdp,sector_t sector,int silent)242 static int gfs2_read_super(struct gfs2_sbd *sdp, sector_t sector, int silent)
243 {
244 struct super_block *sb = sdp->sd_vfs;
245 struct gfs2_sb *p;
246 struct page *page;
247 struct bio *bio;
248
249 page = alloc_page(GFP_NOFS);
250 if (unlikely(!page))
251 return -ENOMEM;
252
253 ClearPageUptodate(page);
254 ClearPageDirty(page);
255 lock_page(page);
256
257 bio = bio_alloc(GFP_NOFS, 1);
258 bio->bi_iter.bi_sector = sector * (sb->s_blocksize >> 9);
259 bio_set_dev(bio, sb->s_bdev);
260 bio_add_page(bio, page, PAGE_SIZE, 0);
261
262 bio->bi_end_io = end_bio_io_page;
263 bio->bi_private = page;
264 bio_set_op_attrs(bio, REQ_OP_READ, REQ_META);
265 submit_bio(bio);
266 wait_on_page_locked(page);
267 bio_put(bio);
268 if (!PageUptodate(page)) {
269 __free_page(page);
270 return -EIO;
271 }
272 p = kmap(page);
273 gfs2_sb_in(sdp, p);
274 kunmap(page);
275 __free_page(page);
276 return gfs2_check_sb(sdp, silent);
277 }
278
279 /**
280 * gfs2_read_sb - Read super block
281 * @sdp: The GFS2 superblock
282 * @silent: Don't print message if mount fails
283 *
284 */
285
gfs2_read_sb(struct gfs2_sbd * sdp,int silent)286 static int gfs2_read_sb(struct gfs2_sbd *sdp, int silent)
287 {
288 u32 hash_blocks, ind_blocks, leaf_blocks;
289 u32 tmp_blocks;
290 unsigned int x;
291 int error;
292
293 error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift, silent);
294 if (error) {
295 if (!silent)
296 fs_err(sdp, "can't read superblock\n");
297 return error;
298 }
299
300 sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
301 GFS2_BASIC_BLOCK_SHIFT;
302 sdp->sd_fsb2bb = BIT(sdp->sd_fsb2bb_shift);
303 sdp->sd_diptrs = (sdp->sd_sb.sb_bsize -
304 sizeof(struct gfs2_dinode)) / sizeof(u64);
305 sdp->sd_inptrs = (sdp->sd_sb.sb_bsize -
306 sizeof(struct gfs2_meta_header)) / sizeof(u64);
307 sdp->sd_ldptrs = (sdp->sd_sb.sb_bsize -
308 sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
309 sdp->sd_jbsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header);
310 sdp->sd_hash_bsize = sdp->sd_sb.sb_bsize / 2;
311 sdp->sd_hash_bsize_shift = sdp->sd_sb.sb_bsize_shift - 1;
312 sdp->sd_hash_ptrs = sdp->sd_hash_bsize / sizeof(u64);
313 sdp->sd_qc_per_block = (sdp->sd_sb.sb_bsize -
314 sizeof(struct gfs2_meta_header)) /
315 sizeof(struct gfs2_quota_change);
316 sdp->sd_blocks_per_bitmap = (sdp->sd_sb.sb_bsize -
317 sizeof(struct gfs2_meta_header))
318 * GFS2_NBBY; /* not the rgrp bitmap, subsequent bitmaps only */
319
320 /*
321 * We always keep at least one block reserved for revokes in
322 * transactions. This greatly simplifies allocating additional
323 * revoke blocks.
324 */
325 atomic_set(&sdp->sd_log_revokes_available, sdp->sd_ldptrs);
326
327 /* Compute maximum reservation required to add a entry to a directory */
328
329 hash_blocks = DIV_ROUND_UP(sizeof(u64) * BIT(GFS2_DIR_MAX_DEPTH),
330 sdp->sd_jbsize);
331
332 ind_blocks = 0;
333 for (tmp_blocks = hash_blocks; tmp_blocks > sdp->sd_diptrs;) {
334 tmp_blocks = DIV_ROUND_UP(tmp_blocks, sdp->sd_inptrs);
335 ind_blocks += tmp_blocks;
336 }
337
338 leaf_blocks = 2 + GFS2_DIR_MAX_DEPTH;
339
340 sdp->sd_max_dirres = hash_blocks + ind_blocks + leaf_blocks;
341
342 sdp->sd_heightsize[0] = sdp->sd_sb.sb_bsize -
343 sizeof(struct gfs2_dinode);
344 sdp->sd_heightsize[1] = sdp->sd_sb.sb_bsize * sdp->sd_diptrs;
345 for (x = 2;; x++) {
346 u64 space, d;
347 u32 m;
348
349 space = sdp->sd_heightsize[x - 1] * sdp->sd_inptrs;
350 d = space;
351 m = do_div(d, sdp->sd_inptrs);
352
353 if (d != sdp->sd_heightsize[x - 1] || m)
354 break;
355 sdp->sd_heightsize[x] = space;
356 }
357 sdp->sd_max_height = x;
358 sdp->sd_heightsize[x] = ~0;
359 gfs2_assert(sdp, sdp->sd_max_height <= GFS2_MAX_META_HEIGHT);
360
361 sdp->sd_max_dents_per_leaf = (sdp->sd_sb.sb_bsize -
362 sizeof(struct gfs2_leaf)) /
363 GFS2_MIN_DIRENT_SIZE;
364 return 0;
365 }
366
init_names(struct gfs2_sbd * sdp,int silent)367 static int init_names(struct gfs2_sbd *sdp, int silent)
368 {
369 char *proto, *table;
370 int error = 0;
371
372 proto = sdp->sd_args.ar_lockproto;
373 table = sdp->sd_args.ar_locktable;
374
375 /* Try to autodetect */
376
377 if (!proto[0] || !table[0]) {
378 error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift, silent);
379 if (error)
380 return error;
381
382 if (!proto[0])
383 proto = sdp->sd_sb.sb_lockproto;
384 if (!table[0])
385 table = sdp->sd_sb.sb_locktable;
386 }
387
388 if (!table[0])
389 table = sdp->sd_vfs->s_id;
390
391 BUILD_BUG_ON(GFS2_LOCKNAME_LEN > GFS2_FSNAME_LEN);
392
393 strscpy(sdp->sd_proto_name, proto, GFS2_LOCKNAME_LEN);
394 strscpy(sdp->sd_table_name, table, GFS2_LOCKNAME_LEN);
395
396 table = sdp->sd_table_name;
397 while ((table = strchr(table, '/')))
398 *table = '_';
399
400 return error;
401 }
402
init_locking(struct gfs2_sbd * sdp,struct gfs2_holder * mount_gh,int undo)403 static int init_locking(struct gfs2_sbd *sdp, struct gfs2_holder *mount_gh,
404 int undo)
405 {
406 int error = 0;
407
408 if (undo)
409 goto fail_trans;
410
411 error = gfs2_glock_nq_num(sdp,
412 GFS2_MOUNT_LOCK, &gfs2_nondisk_glops,
413 LM_ST_EXCLUSIVE, LM_FLAG_NOEXP | GL_NOCACHE,
414 mount_gh);
415 if (error) {
416 fs_err(sdp, "can't acquire mount glock: %d\n", error);
417 goto fail;
418 }
419
420 error = gfs2_glock_nq_num(sdp,
421 GFS2_LIVE_LOCK, &gfs2_nondisk_glops,
422 LM_ST_SHARED,
423 LM_FLAG_NOEXP | GL_EXACT,
424 &sdp->sd_live_gh);
425 if (error) {
426 fs_err(sdp, "can't acquire live glock: %d\n", error);
427 goto fail_mount;
428 }
429
430 error = gfs2_glock_get(sdp, GFS2_RENAME_LOCK, &gfs2_nondisk_glops,
431 CREATE, &sdp->sd_rename_gl);
432 if (error) {
433 fs_err(sdp, "can't create rename glock: %d\n", error);
434 goto fail_live;
435 }
436
437 error = gfs2_glock_get(sdp, GFS2_FREEZE_LOCK, &gfs2_freeze_glops,
438 CREATE, &sdp->sd_freeze_gl);
439 if (error) {
440 fs_err(sdp, "can't create transaction glock: %d\n", error);
441 goto fail_rename;
442 }
443
444 return 0;
445
446 fail_trans:
447 gfs2_glock_put(sdp->sd_freeze_gl);
448 fail_rename:
449 gfs2_glock_put(sdp->sd_rename_gl);
450 fail_live:
451 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
452 fail_mount:
453 gfs2_glock_dq_uninit(mount_gh);
454 fail:
455 return error;
456 }
457
gfs2_lookup_root(struct super_block * sb,struct dentry ** dptr,u64 no_addr,const char * name)458 static int gfs2_lookup_root(struct super_block *sb, struct dentry **dptr,
459 u64 no_addr, const char *name)
460 {
461 struct gfs2_sbd *sdp = sb->s_fs_info;
462 struct dentry *dentry;
463 struct inode *inode;
464
465 inode = gfs2_inode_lookup(sb, DT_DIR, no_addr, 0,
466 GFS2_BLKST_FREE /* ignore */);
467 if (IS_ERR(inode)) {
468 fs_err(sdp, "can't read in %s inode: %ld\n", name, PTR_ERR(inode));
469 return PTR_ERR(inode);
470 }
471 dentry = d_make_root(inode);
472 if (!dentry) {
473 fs_err(sdp, "can't alloc %s dentry\n", name);
474 return -ENOMEM;
475 }
476 *dptr = dentry;
477 return 0;
478 }
479
init_sb(struct gfs2_sbd * sdp,int silent)480 static int init_sb(struct gfs2_sbd *sdp, int silent)
481 {
482 struct super_block *sb = sdp->sd_vfs;
483 struct gfs2_holder sb_gh;
484 u64 no_addr;
485 int ret;
486
487 ret = gfs2_glock_nq_num(sdp, GFS2_SB_LOCK, &gfs2_meta_glops,
488 LM_ST_SHARED, 0, &sb_gh);
489 if (ret) {
490 fs_err(sdp, "can't acquire superblock glock: %d\n", ret);
491 return ret;
492 }
493
494 ret = gfs2_read_sb(sdp, silent);
495 if (ret) {
496 fs_err(sdp, "can't read superblock: %d\n", ret);
497 goto out;
498 }
499
500 switch(sdp->sd_sb.sb_fs_format) {
501 case GFS2_FS_FORMAT_MAX:
502 sb->s_xattr = gfs2_xattr_handlers_max;
503 break;
504
505 case GFS2_FS_FORMAT_MIN:
506 sb->s_xattr = gfs2_xattr_handlers_min;
507 break;
508
509 default:
510 BUG();
511 }
512
513 /* Set up the buffer cache and SB for real */
514 if (sdp->sd_sb.sb_bsize < bdev_logical_block_size(sb->s_bdev)) {
515 ret = -EINVAL;
516 fs_err(sdp, "FS block size (%u) is too small for device "
517 "block size (%u)\n",
518 sdp->sd_sb.sb_bsize, bdev_logical_block_size(sb->s_bdev));
519 goto out;
520 }
521 if (sdp->sd_sb.sb_bsize > PAGE_SIZE) {
522 ret = -EINVAL;
523 fs_err(sdp, "FS block size (%u) is too big for machine "
524 "page size (%u)\n",
525 sdp->sd_sb.sb_bsize, (unsigned int)PAGE_SIZE);
526 goto out;
527 }
528 sb_set_blocksize(sb, sdp->sd_sb.sb_bsize);
529
530 /* Get the root inode */
531 no_addr = sdp->sd_sb.sb_root_dir.no_addr;
532 ret = gfs2_lookup_root(sb, &sdp->sd_root_dir, no_addr, "root");
533 if (ret)
534 goto out;
535
536 /* Get the master inode */
537 no_addr = sdp->sd_sb.sb_master_dir.no_addr;
538 ret = gfs2_lookup_root(sb, &sdp->sd_master_dir, no_addr, "master");
539 if (ret) {
540 dput(sdp->sd_root_dir);
541 goto out;
542 }
543 sb->s_root = dget(sdp->sd_args.ar_meta ? sdp->sd_master_dir : sdp->sd_root_dir);
544 out:
545 gfs2_glock_dq_uninit(&sb_gh);
546 return ret;
547 }
548
gfs2_others_may_mount(struct gfs2_sbd * sdp)549 static void gfs2_others_may_mount(struct gfs2_sbd *sdp)
550 {
551 char *message = "FIRSTMOUNT=Done";
552 char *envp[] = { message, NULL };
553
554 fs_info(sdp, "first mount done, others may mount\n");
555
556 if (sdp->sd_lockstruct.ls_ops->lm_first_done)
557 sdp->sd_lockstruct.ls_ops->lm_first_done(sdp);
558
559 kobject_uevent_env(&sdp->sd_kobj, KOBJ_CHANGE, envp);
560 }
561
562 /**
563 * gfs2_jindex_hold - Grab a lock on the jindex
564 * @sdp: The GFS2 superblock
565 * @ji_gh: the holder for the jindex glock
566 *
567 * Returns: errno
568 */
569
gfs2_jindex_hold(struct gfs2_sbd * sdp,struct gfs2_holder * ji_gh)570 static int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
571 {
572 struct gfs2_inode *dip = GFS2_I(sdp->sd_jindex);
573 struct qstr name;
574 char buf[20];
575 struct gfs2_jdesc *jd;
576 int error;
577
578 name.name = buf;
579
580 mutex_lock(&sdp->sd_jindex_mutex);
581
582 for (;;) {
583 struct gfs2_inode *jip;
584
585 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, ji_gh);
586 if (error)
587 break;
588
589 name.len = sprintf(buf, "journal%u", sdp->sd_journals);
590 name.hash = gfs2_disk_hash(name.name, name.len);
591
592 error = gfs2_dir_check(sdp->sd_jindex, &name, NULL);
593 if (error == -ENOENT) {
594 error = 0;
595 break;
596 }
597
598 gfs2_glock_dq_uninit(ji_gh);
599
600 if (error)
601 break;
602
603 error = -ENOMEM;
604 jd = kzalloc(sizeof(struct gfs2_jdesc), GFP_KERNEL);
605 if (!jd)
606 break;
607
608 INIT_LIST_HEAD(&jd->extent_list);
609 INIT_LIST_HEAD(&jd->jd_revoke_list);
610
611 INIT_WORK(&jd->jd_work, gfs2_recover_func);
612 jd->jd_inode = gfs2_lookupi(sdp->sd_jindex, &name, 1);
613 if (IS_ERR_OR_NULL(jd->jd_inode)) {
614 if (!jd->jd_inode)
615 error = -ENOENT;
616 else
617 error = PTR_ERR(jd->jd_inode);
618 kfree(jd);
619 break;
620 }
621
622 d_mark_dontcache(jd->jd_inode);
623 spin_lock(&sdp->sd_jindex_spin);
624 jd->jd_jid = sdp->sd_journals++;
625 jip = GFS2_I(jd->jd_inode);
626 jd->jd_no_addr = jip->i_no_addr;
627 list_add_tail(&jd->jd_list, &sdp->sd_jindex_list);
628 spin_unlock(&sdp->sd_jindex_spin);
629 }
630
631 mutex_unlock(&sdp->sd_jindex_mutex);
632
633 return error;
634 }
635
636 /**
637 * init_statfs - look up and initialize master and local (per node) statfs inodes
638 * @sdp: The GFS2 superblock
639 *
640 * This should be called after the jindex is initialized in init_journal() and
641 * before gfs2_journal_recovery() is called because we need to be able to write
642 * to these inodes during recovery.
643 *
644 * Returns: errno
645 */
init_statfs(struct gfs2_sbd * sdp)646 static int init_statfs(struct gfs2_sbd *sdp)
647 {
648 int error = 0;
649 struct inode *master = d_inode(sdp->sd_master_dir);
650 struct inode *pn = NULL;
651 char buf[30];
652 struct gfs2_jdesc *jd;
653 struct gfs2_inode *ip;
654
655 sdp->sd_statfs_inode = gfs2_lookup_simple(master, "statfs");
656 if (IS_ERR(sdp->sd_statfs_inode)) {
657 error = PTR_ERR(sdp->sd_statfs_inode);
658 fs_err(sdp, "can't read in statfs inode: %d\n", error);
659 goto out;
660 }
661 if (sdp->sd_args.ar_spectator)
662 goto out;
663
664 pn = gfs2_lookup_simple(master, "per_node");
665 if (IS_ERR(pn)) {
666 error = PTR_ERR(pn);
667 fs_err(sdp, "can't find per_node directory: %d\n", error);
668 goto put_statfs;
669 }
670
671 /* For each jid, lookup the corresponding local statfs inode in the
672 * per_node metafs directory and save it in the sdp->sd_sc_inodes_list. */
673 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
674 struct local_statfs_inode *lsi =
675 kmalloc(sizeof(struct local_statfs_inode), GFP_NOFS);
676 if (!lsi) {
677 error = -ENOMEM;
678 goto free_local;
679 }
680 sprintf(buf, "statfs_change%u", jd->jd_jid);
681 lsi->si_sc_inode = gfs2_lookup_simple(pn, buf);
682 if (IS_ERR(lsi->si_sc_inode)) {
683 error = PTR_ERR(lsi->si_sc_inode);
684 fs_err(sdp, "can't find local \"sc\" file#%u: %d\n",
685 jd->jd_jid, error);
686 kfree(lsi);
687 goto free_local;
688 }
689 lsi->si_jid = jd->jd_jid;
690 if (jd->jd_jid == sdp->sd_jdesc->jd_jid)
691 sdp->sd_sc_inode = lsi->si_sc_inode;
692
693 list_add_tail(&lsi->si_list, &sdp->sd_sc_inodes_list);
694 }
695
696 iput(pn);
697 pn = NULL;
698 ip = GFS2_I(sdp->sd_sc_inode);
699 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0,
700 &sdp->sd_sc_gh);
701 if (error) {
702 fs_err(sdp, "can't lock local \"sc\" file: %d\n", error);
703 goto free_local;
704 }
705 /* read in the local statfs buffer - other nodes don't change it. */
706 error = gfs2_meta_inode_buffer(ip, &sdp->sd_sc_bh);
707 if (error) {
708 fs_err(sdp, "Cannot read in local statfs: %d\n", error);
709 goto unlock_sd_gh;
710 }
711 return 0;
712
713 unlock_sd_gh:
714 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
715 free_local:
716 free_local_statfs_inodes(sdp);
717 iput(pn);
718 put_statfs:
719 iput(sdp->sd_statfs_inode);
720 out:
721 return error;
722 }
723
724 /* Uninitialize and free up memory used by the list of statfs inodes */
uninit_statfs(struct gfs2_sbd * sdp)725 static void uninit_statfs(struct gfs2_sbd *sdp)
726 {
727 if (!sdp->sd_args.ar_spectator) {
728 brelse(sdp->sd_sc_bh);
729 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
730 free_local_statfs_inodes(sdp);
731 }
732 iput(sdp->sd_statfs_inode);
733 }
734
init_journal(struct gfs2_sbd * sdp,int undo)735 static int init_journal(struct gfs2_sbd *sdp, int undo)
736 {
737 struct inode *master = d_inode(sdp->sd_master_dir);
738 struct gfs2_holder ji_gh;
739 struct gfs2_inode *ip;
740 int jindex = 1;
741 int error = 0;
742
743 if (undo) {
744 jindex = 0;
745 goto fail_statfs;
746 }
747
748 sdp->sd_jindex = gfs2_lookup_simple(master, "jindex");
749 if (IS_ERR(sdp->sd_jindex)) {
750 fs_err(sdp, "can't lookup journal index: %d\n", error);
751 return PTR_ERR(sdp->sd_jindex);
752 }
753
754 /* Load in the journal index special file */
755
756 error = gfs2_jindex_hold(sdp, &ji_gh);
757 if (error) {
758 fs_err(sdp, "can't read journal index: %d\n", error);
759 goto fail;
760 }
761
762 error = -EUSERS;
763 if (!gfs2_jindex_size(sdp)) {
764 fs_err(sdp, "no journals!\n");
765 goto fail_jindex;
766 }
767
768 atomic_set(&sdp->sd_log_blks_needed, 0);
769 if (sdp->sd_args.ar_spectator) {
770 sdp->sd_jdesc = gfs2_jdesc_find(sdp, 0);
771 atomic_set(&sdp->sd_log_blks_free, sdp->sd_jdesc->jd_blocks);
772 atomic_set(&sdp->sd_log_thresh1, 2*sdp->sd_jdesc->jd_blocks/5);
773 atomic_set(&sdp->sd_log_thresh2, 4*sdp->sd_jdesc->jd_blocks/5);
774 } else {
775 if (sdp->sd_lockstruct.ls_jid >= gfs2_jindex_size(sdp)) {
776 fs_err(sdp, "can't mount journal #%u\n",
777 sdp->sd_lockstruct.ls_jid);
778 fs_err(sdp, "there are only %u journals (0 - %u)\n",
779 gfs2_jindex_size(sdp),
780 gfs2_jindex_size(sdp) - 1);
781 goto fail_jindex;
782 }
783 sdp->sd_jdesc = gfs2_jdesc_find(sdp, sdp->sd_lockstruct.ls_jid);
784
785 error = gfs2_glock_nq_num(sdp, sdp->sd_lockstruct.ls_jid,
786 &gfs2_journal_glops,
787 LM_ST_EXCLUSIVE,
788 LM_FLAG_NOEXP | GL_NOCACHE,
789 &sdp->sd_journal_gh);
790 if (error) {
791 fs_err(sdp, "can't acquire journal glock: %d\n", error);
792 goto fail_jindex;
793 }
794
795 ip = GFS2_I(sdp->sd_jdesc->jd_inode);
796 sdp->sd_jinode_gl = ip->i_gl;
797 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
798 LM_FLAG_NOEXP | GL_EXACT | GL_NOCACHE,
799 &sdp->sd_jinode_gh);
800 if (error) {
801 fs_err(sdp, "can't acquire journal inode glock: %d\n",
802 error);
803 goto fail_journal_gh;
804 }
805
806 error = gfs2_jdesc_check(sdp->sd_jdesc);
807 if (error) {
808 fs_err(sdp, "my journal (%u) is bad: %d\n",
809 sdp->sd_jdesc->jd_jid, error);
810 goto fail_jinode_gh;
811 }
812 atomic_set(&sdp->sd_log_blks_free, sdp->sd_jdesc->jd_blocks);
813 atomic_set(&sdp->sd_log_thresh1, 2*sdp->sd_jdesc->jd_blocks/5);
814 atomic_set(&sdp->sd_log_thresh2, 4*sdp->sd_jdesc->jd_blocks/5);
815
816 /* Map the extents for this journal's blocks */
817 gfs2_map_journal_extents(sdp, sdp->sd_jdesc);
818 }
819 trace_gfs2_log_blocks(sdp, atomic_read(&sdp->sd_log_blks_free));
820
821 /* Lookup statfs inodes here so journal recovery can use them. */
822 error = init_statfs(sdp);
823 if (error)
824 goto fail_jinode_gh;
825
826 if (sdp->sd_lockstruct.ls_first) {
827 unsigned int x;
828 for (x = 0; x < sdp->sd_journals; x++) {
829 struct gfs2_jdesc *jd = gfs2_jdesc_find(sdp, x);
830
831 if (sdp->sd_args.ar_spectator) {
832 error = check_journal_clean(sdp, jd, true);
833 if (error)
834 goto fail_statfs;
835 continue;
836 }
837 error = gfs2_recover_journal(jd, true);
838 if (error) {
839 fs_err(sdp, "error recovering journal %u: %d\n",
840 x, error);
841 goto fail_statfs;
842 }
843 }
844
845 gfs2_others_may_mount(sdp);
846 } else if (!sdp->sd_args.ar_spectator) {
847 error = gfs2_recover_journal(sdp->sd_jdesc, true);
848 if (error) {
849 fs_err(sdp, "error recovering my journal: %d\n", error);
850 goto fail_statfs;
851 }
852 }
853
854 sdp->sd_log_idle = 1;
855 set_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags);
856 gfs2_glock_dq_uninit(&ji_gh);
857 jindex = 0;
858 INIT_WORK(&sdp->sd_freeze_work, gfs2_freeze_func);
859 return 0;
860
861 fail_statfs:
862 uninit_statfs(sdp);
863 fail_jinode_gh:
864 /* A withdraw may have done dq/uninit so now we need to check it */
865 if (!sdp->sd_args.ar_spectator &&
866 gfs2_holder_initialized(&sdp->sd_jinode_gh))
867 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
868 fail_journal_gh:
869 if (!sdp->sd_args.ar_spectator &&
870 gfs2_holder_initialized(&sdp->sd_journal_gh))
871 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
872 fail_jindex:
873 gfs2_jindex_free(sdp);
874 if (jindex)
875 gfs2_glock_dq_uninit(&ji_gh);
876 fail:
877 iput(sdp->sd_jindex);
878 return error;
879 }
880
881 static struct lock_class_key gfs2_quota_imutex_key;
882
init_inodes(struct gfs2_sbd * sdp,int undo)883 static int init_inodes(struct gfs2_sbd *sdp, int undo)
884 {
885 int error = 0;
886 struct inode *master = d_inode(sdp->sd_master_dir);
887
888 if (undo)
889 goto fail_qinode;
890
891 error = init_journal(sdp, undo);
892 complete_all(&sdp->sd_journal_ready);
893 if (error)
894 goto fail;
895
896 /* Read in the resource index inode */
897 sdp->sd_rindex = gfs2_lookup_simple(master, "rindex");
898 if (IS_ERR(sdp->sd_rindex)) {
899 error = PTR_ERR(sdp->sd_rindex);
900 fs_err(sdp, "can't get resource index inode: %d\n", error);
901 goto fail_journal;
902 }
903 sdp->sd_rindex_uptodate = 0;
904
905 /* Read in the quota inode */
906 sdp->sd_quota_inode = gfs2_lookup_simple(master, "quota");
907 if (IS_ERR(sdp->sd_quota_inode)) {
908 error = PTR_ERR(sdp->sd_quota_inode);
909 fs_err(sdp, "can't get quota file inode: %d\n", error);
910 goto fail_rindex;
911 }
912 /*
913 * i_rwsem on quota files is special. Since this inode is hidden system
914 * file, we are safe to define locking ourselves.
915 */
916 lockdep_set_class(&sdp->sd_quota_inode->i_rwsem,
917 &gfs2_quota_imutex_key);
918
919 error = gfs2_rindex_update(sdp);
920 if (error)
921 goto fail_qinode;
922
923 return 0;
924
925 fail_qinode:
926 iput(sdp->sd_quota_inode);
927 fail_rindex:
928 gfs2_clear_rgrpd(sdp);
929 iput(sdp->sd_rindex);
930 fail_journal:
931 init_journal(sdp, UNDO);
932 fail:
933 return error;
934 }
935
init_per_node(struct gfs2_sbd * sdp,int undo)936 static int init_per_node(struct gfs2_sbd *sdp, int undo)
937 {
938 struct inode *pn = NULL;
939 char buf[30];
940 int error = 0;
941 struct gfs2_inode *ip;
942 struct inode *master = d_inode(sdp->sd_master_dir);
943
944 if (sdp->sd_args.ar_spectator)
945 return 0;
946
947 if (undo)
948 goto fail_qc_gh;
949
950 pn = gfs2_lookup_simple(master, "per_node");
951 if (IS_ERR(pn)) {
952 error = PTR_ERR(pn);
953 fs_err(sdp, "can't find per_node directory: %d\n", error);
954 return error;
955 }
956
957 sprintf(buf, "quota_change%u", sdp->sd_jdesc->jd_jid);
958 sdp->sd_qc_inode = gfs2_lookup_simple(pn, buf);
959 if (IS_ERR(sdp->sd_qc_inode)) {
960 error = PTR_ERR(sdp->sd_qc_inode);
961 fs_err(sdp, "can't find local \"qc\" file: %d\n", error);
962 goto fail_ut_i;
963 }
964
965 iput(pn);
966 pn = NULL;
967
968 ip = GFS2_I(sdp->sd_qc_inode);
969 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0,
970 &sdp->sd_qc_gh);
971 if (error) {
972 fs_err(sdp, "can't lock local \"qc\" file: %d\n", error);
973 goto fail_qc_i;
974 }
975
976 return 0;
977
978 fail_qc_gh:
979 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
980 fail_qc_i:
981 iput(sdp->sd_qc_inode);
982 fail_ut_i:
983 iput(pn);
984 return error;
985 }
986
987 static const match_table_t nolock_tokens = {
988 { Opt_jid, "jid=%d", },
989 { Opt_err, NULL },
990 };
991
992 static const struct lm_lockops nolock_ops = {
993 .lm_proto_name = "lock_nolock",
994 .lm_put_lock = gfs2_glock_free,
995 .lm_tokens = &nolock_tokens,
996 };
997
998 /**
999 * gfs2_lm_mount - mount a locking protocol
1000 * @sdp: the filesystem
1001 * @silent: if 1, don't complain if the FS isn't a GFS2 fs
1002 *
1003 * Returns: errno
1004 */
1005
gfs2_lm_mount(struct gfs2_sbd * sdp,int silent)1006 static int gfs2_lm_mount(struct gfs2_sbd *sdp, int silent)
1007 {
1008 const struct lm_lockops *lm;
1009 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
1010 struct gfs2_args *args = &sdp->sd_args;
1011 const char *proto = sdp->sd_proto_name;
1012 const char *table = sdp->sd_table_name;
1013 char *o, *options;
1014 int ret;
1015
1016 if (!strcmp("lock_nolock", proto)) {
1017 lm = &nolock_ops;
1018 sdp->sd_args.ar_localflocks = 1;
1019 #ifdef CONFIG_GFS2_FS_LOCKING_DLM
1020 } else if (!strcmp("lock_dlm", proto)) {
1021 lm = &gfs2_dlm_ops;
1022 #endif
1023 } else {
1024 pr_info("can't find protocol %s\n", proto);
1025 return -ENOENT;
1026 }
1027
1028 fs_info(sdp, "Trying to join cluster \"%s\", \"%s\"\n", proto, table);
1029
1030 ls->ls_ops = lm;
1031 ls->ls_first = 1;
1032
1033 for (options = args->ar_hostdata; (o = strsep(&options, ":")); ) {
1034 substring_t tmp[MAX_OPT_ARGS];
1035 int token, option;
1036
1037 if (!o || !*o)
1038 continue;
1039
1040 token = match_token(o, *lm->lm_tokens, tmp);
1041 switch (token) {
1042 case Opt_jid:
1043 ret = match_int(&tmp[0], &option);
1044 if (ret || option < 0)
1045 goto hostdata_error;
1046 if (test_and_clear_bit(SDF_NOJOURNALID, &sdp->sd_flags))
1047 ls->ls_jid = option;
1048 break;
1049 case Opt_id:
1050 case Opt_nodir:
1051 /* Obsolete, but left for backward compat purposes */
1052 break;
1053 case Opt_first:
1054 ret = match_int(&tmp[0], &option);
1055 if (ret || (option != 0 && option != 1))
1056 goto hostdata_error;
1057 ls->ls_first = option;
1058 break;
1059 case Opt_err:
1060 default:
1061 hostdata_error:
1062 fs_info(sdp, "unknown hostdata (%s)\n", o);
1063 return -EINVAL;
1064 }
1065 }
1066
1067 if (lm->lm_mount == NULL) {
1068 fs_info(sdp, "Now mounting FS (format %u)...\n", sdp->sd_sb.sb_fs_format);
1069 complete_all(&sdp->sd_locking_init);
1070 return 0;
1071 }
1072 ret = lm->lm_mount(sdp, table);
1073 if (ret == 0)
1074 fs_info(sdp, "Joined cluster. Now mounting FS (format %u)...\n",
1075 sdp->sd_sb.sb_fs_format);
1076 complete_all(&sdp->sd_locking_init);
1077 return ret;
1078 }
1079
gfs2_lm_unmount(struct gfs2_sbd * sdp)1080 void gfs2_lm_unmount(struct gfs2_sbd *sdp)
1081 {
1082 const struct lm_lockops *lm = sdp->sd_lockstruct.ls_ops;
1083 if (likely(!gfs2_withdrawn(sdp)) && lm->lm_unmount)
1084 lm->lm_unmount(sdp);
1085 }
1086
wait_on_journal(struct gfs2_sbd * sdp)1087 static int wait_on_journal(struct gfs2_sbd *sdp)
1088 {
1089 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
1090 return 0;
1091
1092 return wait_on_bit(&sdp->sd_flags, SDF_NOJOURNALID, TASK_INTERRUPTIBLE)
1093 ? -EINTR : 0;
1094 }
1095
gfs2_online_uevent(struct gfs2_sbd * sdp)1096 void gfs2_online_uevent(struct gfs2_sbd *sdp)
1097 {
1098 struct super_block *sb = sdp->sd_vfs;
1099 char ro[20];
1100 char spectator[20];
1101 char *envp[] = { ro, spectator, NULL };
1102 sprintf(ro, "RDONLY=%d", sb_rdonly(sb));
1103 sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
1104 kobject_uevent_env(&sdp->sd_kobj, KOBJ_ONLINE, envp);
1105 }
1106
init_threads(struct gfs2_sbd * sdp)1107 static int init_threads(struct gfs2_sbd *sdp)
1108 {
1109 struct task_struct *p;
1110 int error = 0;
1111
1112 p = kthread_run(gfs2_logd, sdp, "gfs2_logd");
1113 if (IS_ERR(p)) {
1114 error = PTR_ERR(p);
1115 fs_err(sdp, "can't start logd thread: %d\n", error);
1116 return error;
1117 }
1118 sdp->sd_logd_process = p;
1119
1120 p = kthread_run(gfs2_quotad, sdp, "gfs2_quotad");
1121 if (IS_ERR(p)) {
1122 error = PTR_ERR(p);
1123 fs_err(sdp, "can't start quotad thread: %d\n", error);
1124 goto fail;
1125 }
1126 sdp->sd_quotad_process = p;
1127 return 0;
1128
1129 fail:
1130 kthread_stop(sdp->sd_logd_process);
1131 sdp->sd_logd_process = NULL;
1132 return error;
1133 }
1134
1135 /**
1136 * gfs2_fill_super - Read in superblock
1137 * @sb: The VFS superblock
1138 * @fc: Mount options and flags
1139 *
1140 * Returns: -errno
1141 */
gfs2_fill_super(struct super_block * sb,struct fs_context * fc)1142 static int gfs2_fill_super(struct super_block *sb, struct fs_context *fc)
1143 {
1144 struct gfs2_args *args = fc->fs_private;
1145 int silent = fc->sb_flags & SB_SILENT;
1146 struct gfs2_sbd *sdp;
1147 struct gfs2_holder mount_gh;
1148 struct gfs2_holder freeze_gh;
1149 int error;
1150
1151 sdp = init_sbd(sb);
1152 if (!sdp) {
1153 pr_warn("can't alloc struct gfs2_sbd\n");
1154 return -ENOMEM;
1155 }
1156 sdp->sd_args = *args;
1157
1158 if (sdp->sd_args.ar_spectator) {
1159 sb->s_flags |= SB_RDONLY;
1160 set_bit(SDF_RORECOVERY, &sdp->sd_flags);
1161 }
1162 if (sdp->sd_args.ar_posix_acl)
1163 sb->s_flags |= SB_POSIXACL;
1164 if (sdp->sd_args.ar_nobarrier)
1165 set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
1166
1167 sb->s_flags |= SB_NOSEC;
1168 sb->s_magic = GFS2_MAGIC;
1169 sb->s_op = &gfs2_super_ops;
1170 sb->s_d_op = &gfs2_dops;
1171 sb->s_export_op = &gfs2_export_ops;
1172 sb->s_qcop = &gfs2_quotactl_ops;
1173 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
1174 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
1175 sb->s_time_gran = 1;
1176 sb->s_maxbytes = MAX_LFS_FILESIZE;
1177
1178 /* Set up the buffer cache and fill in some fake block size values
1179 to allow us to read-in the on-disk superblock. */
1180 sdp->sd_sb.sb_bsize = sb_min_blocksize(sb, GFS2_BASIC_BLOCK);
1181 sdp->sd_sb.sb_bsize_shift = sb->s_blocksize_bits;
1182 sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
1183 GFS2_BASIC_BLOCK_SHIFT;
1184 sdp->sd_fsb2bb = BIT(sdp->sd_fsb2bb_shift);
1185
1186 sdp->sd_tune.gt_logd_secs = sdp->sd_args.ar_commit;
1187 sdp->sd_tune.gt_quota_quantum = sdp->sd_args.ar_quota_quantum;
1188 if (sdp->sd_args.ar_statfs_quantum) {
1189 sdp->sd_tune.gt_statfs_slow = 0;
1190 sdp->sd_tune.gt_statfs_quantum = sdp->sd_args.ar_statfs_quantum;
1191 } else {
1192 sdp->sd_tune.gt_statfs_slow = 1;
1193 sdp->sd_tune.gt_statfs_quantum = 30;
1194 }
1195
1196 error = init_names(sdp, silent);
1197 if (error)
1198 goto fail_free;
1199
1200 snprintf(sdp->sd_fsname, sizeof(sdp->sd_fsname), "%s", sdp->sd_table_name);
1201
1202 error = gfs2_sys_fs_add(sdp);
1203 if (error)
1204 goto fail_free;
1205
1206 gfs2_create_debugfs_file(sdp);
1207
1208 error = gfs2_lm_mount(sdp, silent);
1209 if (error)
1210 goto fail_debug;
1211
1212 error = init_locking(sdp, &mount_gh, DO);
1213 if (error)
1214 goto fail_lm;
1215
1216 error = init_sb(sdp, silent);
1217 if (error)
1218 goto fail_locking;
1219
1220 /* Turn rgrplvb on by default if fs format is recent enough */
1221 if (!sdp->sd_args.ar_got_rgrplvb && sdp->sd_sb.sb_fs_format > 1801)
1222 sdp->sd_args.ar_rgrplvb = 1;
1223
1224 error = wait_on_journal(sdp);
1225 if (error)
1226 goto fail_sb;
1227
1228 /*
1229 * If user space has failed to join the cluster or some similar
1230 * failure has occurred, then the journal id will contain a
1231 * negative (error) number. This will then be returned to the
1232 * caller (of the mount syscall). We do this even for spectator
1233 * mounts (which just write a jid of 0 to indicate "ok" even though
1234 * the jid is unused in the spectator case)
1235 */
1236 if (sdp->sd_lockstruct.ls_jid < 0) {
1237 error = sdp->sd_lockstruct.ls_jid;
1238 sdp->sd_lockstruct.ls_jid = 0;
1239 goto fail_sb;
1240 }
1241
1242 if (sdp->sd_args.ar_spectator)
1243 snprintf(sdp->sd_fsname, sizeof(sdp->sd_fsname), "%s.s",
1244 sdp->sd_table_name);
1245 else
1246 snprintf(sdp->sd_fsname, sizeof(sdp->sd_fsname), "%s.%u",
1247 sdp->sd_table_name, sdp->sd_lockstruct.ls_jid);
1248
1249 error = init_inodes(sdp, DO);
1250 if (error)
1251 goto fail_sb;
1252
1253 error = init_per_node(sdp, DO);
1254 if (error)
1255 goto fail_inodes;
1256
1257 error = gfs2_statfs_init(sdp);
1258 if (error) {
1259 fs_err(sdp, "can't initialize statfs subsystem: %d\n", error);
1260 goto fail_per_node;
1261 }
1262
1263 if (!sb_rdonly(sb)) {
1264 error = init_threads(sdp);
1265 if (error) {
1266 gfs2_withdraw_delayed(sdp);
1267 goto fail_per_node;
1268 }
1269 }
1270
1271 error = gfs2_freeze_lock(sdp, &freeze_gh, 0);
1272 if (error)
1273 goto fail_per_node;
1274
1275 if (!sb_rdonly(sb))
1276 error = gfs2_make_fs_rw(sdp);
1277
1278 gfs2_freeze_unlock(&freeze_gh);
1279 if (error) {
1280 if (sdp->sd_quotad_process)
1281 kthread_stop(sdp->sd_quotad_process);
1282 sdp->sd_quotad_process = NULL;
1283 if (sdp->sd_logd_process)
1284 kthread_stop(sdp->sd_logd_process);
1285 sdp->sd_logd_process = NULL;
1286 fs_err(sdp, "can't make FS RW: %d\n", error);
1287 goto fail_per_node;
1288 }
1289 gfs2_glock_dq_uninit(&mount_gh);
1290 gfs2_online_uevent(sdp);
1291 return 0;
1292
1293 fail_per_node:
1294 init_per_node(sdp, UNDO);
1295 fail_inodes:
1296 init_inodes(sdp, UNDO);
1297 fail_sb:
1298 if (sdp->sd_root_dir)
1299 dput(sdp->sd_root_dir);
1300 if (sdp->sd_master_dir)
1301 dput(sdp->sd_master_dir);
1302 if (sb->s_root)
1303 dput(sb->s_root);
1304 sb->s_root = NULL;
1305 fail_locking:
1306 init_locking(sdp, &mount_gh, UNDO);
1307 fail_lm:
1308 complete_all(&sdp->sd_journal_ready);
1309 gfs2_gl_hash_clear(sdp);
1310 gfs2_lm_unmount(sdp);
1311 fail_debug:
1312 gfs2_delete_debugfs_file(sdp);
1313 gfs2_sys_fs_del(sdp);
1314 fail_free:
1315 free_sbd(sdp);
1316 sb->s_fs_info = NULL;
1317 return error;
1318 }
1319
1320 /**
1321 * gfs2_get_tree - Get the GFS2 superblock and root directory
1322 * @fc: The filesystem context
1323 *
1324 * Returns: 0 or -errno on error
1325 */
gfs2_get_tree(struct fs_context * fc)1326 static int gfs2_get_tree(struct fs_context *fc)
1327 {
1328 struct gfs2_args *args = fc->fs_private;
1329 struct gfs2_sbd *sdp;
1330 int error;
1331
1332 error = get_tree_bdev(fc, gfs2_fill_super);
1333 if (error)
1334 return error;
1335
1336 sdp = fc->root->d_sb->s_fs_info;
1337 dput(fc->root);
1338 if (args->ar_meta)
1339 fc->root = dget(sdp->sd_master_dir);
1340 else
1341 fc->root = dget(sdp->sd_root_dir);
1342 return 0;
1343 }
1344
gfs2_fc_free(struct fs_context * fc)1345 static void gfs2_fc_free(struct fs_context *fc)
1346 {
1347 struct gfs2_args *args = fc->fs_private;
1348
1349 kfree(args);
1350 }
1351
1352 enum gfs2_param {
1353 Opt_lockproto,
1354 Opt_locktable,
1355 Opt_hostdata,
1356 Opt_spectator,
1357 Opt_ignore_local_fs,
1358 Opt_localflocks,
1359 Opt_localcaching,
1360 Opt_debug,
1361 Opt_upgrade,
1362 Opt_acl,
1363 Opt_quota,
1364 Opt_quota_flag,
1365 Opt_suiddir,
1366 Opt_data,
1367 Opt_meta,
1368 Opt_discard,
1369 Opt_commit,
1370 Opt_errors,
1371 Opt_statfs_quantum,
1372 Opt_statfs_percent,
1373 Opt_quota_quantum,
1374 Opt_barrier,
1375 Opt_rgrplvb,
1376 Opt_loccookie,
1377 };
1378
1379 static const struct constant_table gfs2_param_quota[] = {
1380 {"off", GFS2_QUOTA_OFF},
1381 {"account", GFS2_QUOTA_ACCOUNT},
1382 {"on", GFS2_QUOTA_ON},
1383 {}
1384 };
1385
1386 enum opt_data {
1387 Opt_data_writeback = GFS2_DATA_WRITEBACK,
1388 Opt_data_ordered = GFS2_DATA_ORDERED,
1389 };
1390
1391 static const struct constant_table gfs2_param_data[] = {
1392 {"writeback", Opt_data_writeback },
1393 {"ordered", Opt_data_ordered },
1394 {}
1395 };
1396
1397 enum opt_errors {
1398 Opt_errors_withdraw = GFS2_ERRORS_WITHDRAW,
1399 Opt_errors_panic = GFS2_ERRORS_PANIC,
1400 };
1401
1402 static const struct constant_table gfs2_param_errors[] = {
1403 {"withdraw", Opt_errors_withdraw },
1404 {"panic", Opt_errors_panic },
1405 {}
1406 };
1407
1408 static const struct fs_parameter_spec gfs2_fs_parameters[] = {
1409 fsparam_string ("lockproto", Opt_lockproto),
1410 fsparam_string ("locktable", Opt_locktable),
1411 fsparam_string ("hostdata", Opt_hostdata),
1412 fsparam_flag ("spectator", Opt_spectator),
1413 fsparam_flag ("norecovery", Opt_spectator),
1414 fsparam_flag ("ignore_local_fs", Opt_ignore_local_fs),
1415 fsparam_flag ("localflocks", Opt_localflocks),
1416 fsparam_flag ("localcaching", Opt_localcaching),
1417 fsparam_flag_no("debug", Opt_debug),
1418 fsparam_flag ("upgrade", Opt_upgrade),
1419 fsparam_flag_no("acl", Opt_acl),
1420 fsparam_flag_no("suiddir", Opt_suiddir),
1421 fsparam_enum ("data", Opt_data, gfs2_param_data),
1422 fsparam_flag ("meta", Opt_meta),
1423 fsparam_flag_no("discard", Opt_discard),
1424 fsparam_s32 ("commit", Opt_commit),
1425 fsparam_enum ("errors", Opt_errors, gfs2_param_errors),
1426 fsparam_s32 ("statfs_quantum", Opt_statfs_quantum),
1427 fsparam_s32 ("statfs_percent", Opt_statfs_percent),
1428 fsparam_s32 ("quota_quantum", Opt_quota_quantum),
1429 fsparam_flag_no("barrier", Opt_barrier),
1430 fsparam_flag_no("rgrplvb", Opt_rgrplvb),
1431 fsparam_flag_no("loccookie", Opt_loccookie),
1432 /* quota can be a flag or an enum so it gets special treatment */
1433 fsparam_flag_no("quota", Opt_quota_flag),
1434 fsparam_enum("quota", Opt_quota, gfs2_param_quota),
1435 {}
1436 };
1437
1438 /* Parse a single mount parameter */
gfs2_parse_param(struct fs_context * fc,struct fs_parameter * param)1439 static int gfs2_parse_param(struct fs_context *fc, struct fs_parameter *param)
1440 {
1441 struct gfs2_args *args = fc->fs_private;
1442 struct fs_parse_result result;
1443 int o;
1444
1445 o = fs_parse(fc, gfs2_fs_parameters, param, &result);
1446 if (o < 0)
1447 return o;
1448
1449 switch (o) {
1450 case Opt_lockproto:
1451 strscpy(args->ar_lockproto, param->string, GFS2_LOCKNAME_LEN);
1452 break;
1453 case Opt_locktable:
1454 strscpy(args->ar_locktable, param->string, GFS2_LOCKNAME_LEN);
1455 break;
1456 case Opt_hostdata:
1457 strscpy(args->ar_hostdata, param->string, GFS2_LOCKNAME_LEN);
1458 break;
1459 case Opt_spectator:
1460 args->ar_spectator = 1;
1461 break;
1462 case Opt_ignore_local_fs:
1463 /* Retained for backwards compat only */
1464 break;
1465 case Opt_localflocks:
1466 args->ar_localflocks = 1;
1467 break;
1468 case Opt_localcaching:
1469 /* Retained for backwards compat only */
1470 break;
1471 case Opt_debug:
1472 if (result.boolean && args->ar_errors == GFS2_ERRORS_PANIC)
1473 return invalfc(fc, "-o debug and -o errors=panic are mutually exclusive");
1474 args->ar_debug = result.boolean;
1475 break;
1476 case Opt_upgrade:
1477 /* Retained for backwards compat only */
1478 break;
1479 case Opt_acl:
1480 args->ar_posix_acl = result.boolean;
1481 break;
1482 case Opt_quota_flag:
1483 args->ar_quota = result.negated ? GFS2_QUOTA_OFF : GFS2_QUOTA_ON;
1484 break;
1485 case Opt_quota:
1486 args->ar_quota = result.int_32;
1487 break;
1488 case Opt_suiddir:
1489 args->ar_suiddir = result.boolean;
1490 break;
1491 case Opt_data:
1492 /* The uint_32 result maps directly to GFS2_DATA_* */
1493 args->ar_data = result.uint_32;
1494 break;
1495 case Opt_meta:
1496 args->ar_meta = 1;
1497 break;
1498 case Opt_discard:
1499 args->ar_discard = result.boolean;
1500 break;
1501 case Opt_commit:
1502 if (result.int_32 <= 0)
1503 return invalfc(fc, "commit mount option requires a positive numeric argument");
1504 args->ar_commit = result.int_32;
1505 break;
1506 case Opt_statfs_quantum:
1507 if (result.int_32 < 0)
1508 return invalfc(fc, "statfs_quantum mount option requires a non-negative numeric argument");
1509 args->ar_statfs_quantum = result.int_32;
1510 break;
1511 case Opt_quota_quantum:
1512 if (result.int_32 <= 0)
1513 return invalfc(fc, "quota_quantum mount option requires a positive numeric argument");
1514 args->ar_quota_quantum = result.int_32;
1515 break;
1516 case Opt_statfs_percent:
1517 if (result.int_32 < 0 || result.int_32 > 100)
1518 return invalfc(fc, "statfs_percent mount option requires a numeric argument between 0 and 100");
1519 args->ar_statfs_percent = result.int_32;
1520 break;
1521 case Opt_errors:
1522 if (args->ar_debug && result.uint_32 == GFS2_ERRORS_PANIC)
1523 return invalfc(fc, "-o debug and -o errors=panic are mutually exclusive");
1524 args->ar_errors = result.uint_32;
1525 break;
1526 case Opt_barrier:
1527 args->ar_nobarrier = result.boolean;
1528 break;
1529 case Opt_rgrplvb:
1530 args->ar_rgrplvb = result.boolean;
1531 args->ar_got_rgrplvb = 1;
1532 break;
1533 case Opt_loccookie:
1534 args->ar_loccookie = result.boolean;
1535 break;
1536 default:
1537 return invalfc(fc, "invalid mount option: %s", param->key);
1538 }
1539 return 0;
1540 }
1541
gfs2_reconfigure(struct fs_context * fc)1542 static int gfs2_reconfigure(struct fs_context *fc)
1543 {
1544 struct super_block *sb = fc->root->d_sb;
1545 struct gfs2_sbd *sdp = sb->s_fs_info;
1546 struct gfs2_args *oldargs = &sdp->sd_args;
1547 struct gfs2_args *newargs = fc->fs_private;
1548 struct gfs2_tune *gt = &sdp->sd_tune;
1549 int error = 0;
1550
1551 sync_filesystem(sb);
1552
1553 spin_lock(>->gt_spin);
1554 oldargs->ar_commit = gt->gt_logd_secs;
1555 oldargs->ar_quota_quantum = gt->gt_quota_quantum;
1556 if (gt->gt_statfs_slow)
1557 oldargs->ar_statfs_quantum = 0;
1558 else
1559 oldargs->ar_statfs_quantum = gt->gt_statfs_quantum;
1560 spin_unlock(>->gt_spin);
1561
1562 if (strcmp(newargs->ar_lockproto, oldargs->ar_lockproto)) {
1563 errorfc(fc, "reconfiguration of locking protocol not allowed");
1564 return -EINVAL;
1565 }
1566 if (strcmp(newargs->ar_locktable, oldargs->ar_locktable)) {
1567 errorfc(fc, "reconfiguration of lock table not allowed");
1568 return -EINVAL;
1569 }
1570 if (strcmp(newargs->ar_hostdata, oldargs->ar_hostdata)) {
1571 errorfc(fc, "reconfiguration of host data not allowed");
1572 return -EINVAL;
1573 }
1574 if (newargs->ar_spectator != oldargs->ar_spectator) {
1575 errorfc(fc, "reconfiguration of spectator mode not allowed");
1576 return -EINVAL;
1577 }
1578 if (newargs->ar_localflocks != oldargs->ar_localflocks) {
1579 errorfc(fc, "reconfiguration of localflocks not allowed");
1580 return -EINVAL;
1581 }
1582 if (newargs->ar_meta != oldargs->ar_meta) {
1583 errorfc(fc, "switching between gfs2 and gfs2meta not allowed");
1584 return -EINVAL;
1585 }
1586 if (oldargs->ar_spectator)
1587 fc->sb_flags |= SB_RDONLY;
1588
1589 if ((sb->s_flags ^ fc->sb_flags) & SB_RDONLY) {
1590 struct gfs2_holder freeze_gh;
1591
1592 error = gfs2_freeze_lock(sdp, &freeze_gh, 0);
1593 if (error)
1594 return -EINVAL;
1595
1596 if (fc->sb_flags & SB_RDONLY) {
1597 gfs2_make_fs_ro(sdp);
1598 } else {
1599 error = gfs2_make_fs_rw(sdp);
1600 if (error)
1601 errorfc(fc, "unable to remount read-write");
1602 }
1603 gfs2_freeze_unlock(&freeze_gh);
1604 }
1605 sdp->sd_args = *newargs;
1606
1607 if (sdp->sd_args.ar_posix_acl)
1608 sb->s_flags |= SB_POSIXACL;
1609 else
1610 sb->s_flags &= ~SB_POSIXACL;
1611 if (sdp->sd_args.ar_nobarrier)
1612 set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
1613 else
1614 clear_bit(SDF_NOBARRIERS, &sdp->sd_flags);
1615 spin_lock(>->gt_spin);
1616 gt->gt_logd_secs = newargs->ar_commit;
1617 gt->gt_quota_quantum = newargs->ar_quota_quantum;
1618 if (newargs->ar_statfs_quantum) {
1619 gt->gt_statfs_slow = 0;
1620 gt->gt_statfs_quantum = newargs->ar_statfs_quantum;
1621 }
1622 else {
1623 gt->gt_statfs_slow = 1;
1624 gt->gt_statfs_quantum = 30;
1625 }
1626 spin_unlock(>->gt_spin);
1627
1628 gfs2_online_uevent(sdp);
1629 return error;
1630 }
1631
1632 static const struct fs_context_operations gfs2_context_ops = {
1633 .free = gfs2_fc_free,
1634 .parse_param = gfs2_parse_param,
1635 .get_tree = gfs2_get_tree,
1636 .reconfigure = gfs2_reconfigure,
1637 };
1638
1639 /* Set up the filesystem mount context */
gfs2_init_fs_context(struct fs_context * fc)1640 static int gfs2_init_fs_context(struct fs_context *fc)
1641 {
1642 struct gfs2_args *args;
1643
1644 args = kmalloc(sizeof(*args), GFP_KERNEL);
1645 if (args == NULL)
1646 return -ENOMEM;
1647
1648 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
1649 struct gfs2_sbd *sdp = fc->root->d_sb->s_fs_info;
1650
1651 *args = sdp->sd_args;
1652 } else {
1653 memset(args, 0, sizeof(*args));
1654 args->ar_quota = GFS2_QUOTA_DEFAULT;
1655 args->ar_data = GFS2_DATA_DEFAULT;
1656 args->ar_commit = 30;
1657 args->ar_statfs_quantum = 30;
1658 args->ar_quota_quantum = 60;
1659 args->ar_errors = GFS2_ERRORS_DEFAULT;
1660 }
1661 fc->fs_private = args;
1662 fc->ops = &gfs2_context_ops;
1663 return 0;
1664 }
1665
set_meta_super(struct super_block * s,struct fs_context * fc)1666 static int set_meta_super(struct super_block *s, struct fs_context *fc)
1667 {
1668 return -EINVAL;
1669 }
1670
test_meta_super(struct super_block * s,struct fs_context * fc)1671 static int test_meta_super(struct super_block *s, struct fs_context *fc)
1672 {
1673 return (fc->sget_key == s->s_bdev);
1674 }
1675
gfs2_meta_get_tree(struct fs_context * fc)1676 static int gfs2_meta_get_tree(struct fs_context *fc)
1677 {
1678 struct super_block *s;
1679 struct gfs2_sbd *sdp;
1680 struct path path;
1681 int error;
1682
1683 if (!fc->source || !*fc->source)
1684 return -EINVAL;
1685
1686 error = kern_path(fc->source, LOOKUP_FOLLOW, &path);
1687 if (error) {
1688 pr_warn("path_lookup on %s returned error %d\n",
1689 fc->source, error);
1690 return error;
1691 }
1692 fc->fs_type = &gfs2_fs_type;
1693 fc->sget_key = path.dentry->d_sb->s_bdev;
1694 s = sget_fc(fc, test_meta_super, set_meta_super);
1695 path_put(&path);
1696 if (IS_ERR(s)) {
1697 pr_warn("gfs2 mount does not exist\n");
1698 return PTR_ERR(s);
1699 }
1700 if ((fc->sb_flags ^ s->s_flags) & SB_RDONLY) {
1701 deactivate_locked_super(s);
1702 return -EBUSY;
1703 }
1704 sdp = s->s_fs_info;
1705 fc->root = dget(sdp->sd_master_dir);
1706 return 0;
1707 }
1708
1709 static const struct fs_context_operations gfs2_meta_context_ops = {
1710 .free = gfs2_fc_free,
1711 .get_tree = gfs2_meta_get_tree,
1712 };
1713
gfs2_meta_init_fs_context(struct fs_context * fc)1714 static int gfs2_meta_init_fs_context(struct fs_context *fc)
1715 {
1716 int ret = gfs2_init_fs_context(fc);
1717
1718 if (ret)
1719 return ret;
1720
1721 fc->ops = &gfs2_meta_context_ops;
1722 return 0;
1723 }
1724
gfs2_kill_sb(struct super_block * sb)1725 static void gfs2_kill_sb(struct super_block *sb)
1726 {
1727 struct gfs2_sbd *sdp = sb->s_fs_info;
1728
1729 if (sdp == NULL) {
1730 kill_block_super(sb);
1731 return;
1732 }
1733
1734 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_SYNC | GFS2_LFC_KILL_SB);
1735 dput(sdp->sd_root_dir);
1736 dput(sdp->sd_master_dir);
1737 sdp->sd_root_dir = NULL;
1738 sdp->sd_master_dir = NULL;
1739 shrink_dcache_sb(sb);
1740 kill_block_super(sb);
1741 }
1742
1743 struct file_system_type gfs2_fs_type = {
1744 .name = "gfs2",
1745 .fs_flags = FS_REQUIRES_DEV,
1746 .init_fs_context = gfs2_init_fs_context,
1747 .parameters = gfs2_fs_parameters,
1748 .kill_sb = gfs2_kill_sb,
1749 .owner = THIS_MODULE,
1750 };
1751 MODULE_ALIAS_FS("gfs2");
1752
1753 struct file_system_type gfs2meta_fs_type = {
1754 .name = "gfs2meta",
1755 .fs_flags = FS_REQUIRES_DEV,
1756 .init_fs_context = gfs2_meta_init_fs_context,
1757 .owner = THIS_MODULE,
1758 };
1759 MODULE_ALIAS_FS("gfs2meta");
1760