ops_fstype.c 35 KB

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