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