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