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