ops_fstype.c 35 KB

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