ops_fstype.c 35 KB

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