super.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /*
  2. * super.c - NILFS module and super block management.
  3. *
  4. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Written by Ryusuke Konishi <ryusuke@osrg.net>
  21. */
  22. /*
  23. * linux/fs/ext2/super.c
  24. *
  25. * Copyright (C) 1992, 1993, 1994, 1995
  26. * Remy Card (card@masi.ibp.fr)
  27. * Laboratoire MASI - Institut Blaise Pascal
  28. * Universite Pierre et Marie Curie (Paris VI)
  29. *
  30. * from
  31. *
  32. * linux/fs/minix/inode.c
  33. *
  34. * Copyright (C) 1991, 1992 Linus Torvalds
  35. *
  36. * Big-endian to little-endian byte-swapping/bitmaps by
  37. * David S. Miller (davem@caip.rutgers.edu), 1995
  38. */
  39. #include <linux/module.h>
  40. #include <linux/string.h>
  41. #include <linux/slab.h>
  42. #include <linux/init.h>
  43. #include <linux/blkdev.h>
  44. #include <linux/parser.h>
  45. #include <linux/random.h>
  46. #include <linux/crc32.h>
  47. #include <linux/smp_lock.h>
  48. #include <linux/vfs.h>
  49. #include <linux/writeback.h>
  50. #include <linux/kobject.h>
  51. #include <linux/exportfs.h>
  52. #include "nilfs.h"
  53. #include "mdt.h"
  54. #include "alloc.h"
  55. #include "page.h"
  56. #include "cpfile.h"
  57. #include "ifile.h"
  58. #include "dat.h"
  59. #include "segment.h"
  60. #include "segbuf.h"
  61. MODULE_AUTHOR("NTT Corp.");
  62. MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
  63. "(NILFS)");
  64. MODULE_VERSION(NILFS_VERSION);
  65. MODULE_LICENSE("GPL");
  66. static int nilfs_remount(struct super_block *sb, int *flags, char *data);
  67. static int test_exclusive_mount(struct file_system_type *fs_type,
  68. struct block_device *bdev, int flags);
  69. /**
  70. * nilfs_error() - report failure condition on a filesystem
  71. *
  72. * nilfs_error() sets an ERROR_FS flag on the superblock as well as
  73. * reporting an error message. It should be called when NILFS detects
  74. * incoherences or defects of meta data on disk. As for sustainable
  75. * errors such as a single-shot I/O error, nilfs_warning() or the printk()
  76. * function should be used instead.
  77. *
  78. * The segment constructor must not call this function because it can
  79. * kill itself.
  80. */
  81. void nilfs_error(struct super_block *sb, const char *function,
  82. const char *fmt, ...)
  83. {
  84. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  85. va_list args;
  86. va_start(args, fmt);
  87. printk(KERN_CRIT "NILFS error (device %s): %s: ", sb->s_id, function);
  88. vprintk(fmt, args);
  89. printk("\n");
  90. va_end(args);
  91. if (!(sb->s_flags & MS_RDONLY)) {
  92. struct the_nilfs *nilfs = sbi->s_nilfs;
  93. if (!nilfs_test_opt(sbi, ERRORS_CONT))
  94. nilfs_detach_segment_constructor(sbi);
  95. down_write(&nilfs->ns_sem);
  96. if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
  97. nilfs->ns_mount_state |= NILFS_ERROR_FS;
  98. nilfs->ns_sbp[0]->s_state |=
  99. cpu_to_le16(NILFS_ERROR_FS);
  100. nilfs_commit_super(sbi, 1);
  101. }
  102. up_write(&nilfs->ns_sem);
  103. if (nilfs_test_opt(sbi, ERRORS_RO)) {
  104. printk(KERN_CRIT "Remounting filesystem read-only\n");
  105. sb->s_flags |= MS_RDONLY;
  106. }
  107. }
  108. if (nilfs_test_opt(sbi, ERRORS_PANIC))
  109. panic("NILFS (device %s): panic forced after error\n",
  110. sb->s_id);
  111. }
  112. void nilfs_warning(struct super_block *sb, const char *function,
  113. const char *fmt, ...)
  114. {
  115. va_list args;
  116. va_start(args, fmt);
  117. printk(KERN_WARNING "NILFS warning (device %s): %s: ",
  118. sb->s_id, function);
  119. vprintk(fmt, args);
  120. printk("\n");
  121. va_end(args);
  122. }
  123. static struct kmem_cache *nilfs_inode_cachep;
  124. struct inode *nilfs_alloc_inode(struct super_block *sb)
  125. {
  126. struct nilfs_inode_info *ii;
  127. ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
  128. if (!ii)
  129. return NULL;
  130. ii->i_bh = NULL;
  131. ii->i_state = 0;
  132. ii->vfs_inode.i_version = 1;
  133. nilfs_btnode_cache_init(&ii->i_btnode_cache);
  134. return &ii->vfs_inode;
  135. }
  136. void nilfs_destroy_inode(struct inode *inode)
  137. {
  138. kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
  139. }
  140. static void init_once(void *obj)
  141. {
  142. struct nilfs_inode_info *ii = obj;
  143. INIT_LIST_HEAD(&ii->i_dirty);
  144. #ifdef CONFIG_NILFS_XATTR
  145. init_rwsem(&ii->xattr_sem);
  146. #endif
  147. nilfs_btnode_cache_init_once(&ii->i_btnode_cache);
  148. ii->i_bmap = (struct nilfs_bmap *)&ii->i_bmap_union;
  149. inode_init_once(&ii->vfs_inode);
  150. }
  151. static int nilfs_init_inode_cache(void)
  152. {
  153. nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
  154. sizeof(struct nilfs_inode_info),
  155. 0, SLAB_RECLAIM_ACCOUNT,
  156. init_once);
  157. return (nilfs_inode_cachep == NULL) ? -ENOMEM : 0;
  158. }
  159. static inline void nilfs_destroy_inode_cache(void)
  160. {
  161. kmem_cache_destroy(nilfs_inode_cachep);
  162. }
  163. static void nilfs_clear_inode(struct inode *inode)
  164. {
  165. struct nilfs_inode_info *ii = NILFS_I(inode);
  166. #ifdef CONFIG_NILFS_POSIX_ACL
  167. if (ii->i_acl && ii->i_acl != NILFS_ACL_NOT_CACHED) {
  168. posix_acl_release(ii->i_acl);
  169. ii->i_acl = NILFS_ACL_NOT_CACHED;
  170. }
  171. if (ii->i_default_acl && ii->i_default_acl != NILFS_ACL_NOT_CACHED) {
  172. posix_acl_release(ii->i_default_acl);
  173. ii->i_default_acl = NILFS_ACL_NOT_CACHED;
  174. }
  175. #endif
  176. /*
  177. * Free resources allocated in nilfs_read_inode(), here.
  178. */
  179. BUG_ON(!list_empty(&ii->i_dirty));
  180. brelse(ii->i_bh);
  181. ii->i_bh = NULL;
  182. if (test_bit(NILFS_I_BMAP, &ii->i_state))
  183. nilfs_bmap_clear(ii->i_bmap);
  184. nilfs_btnode_cache_clear(&ii->i_btnode_cache);
  185. }
  186. static int nilfs_sync_super(struct nilfs_sb_info *sbi, int dupsb)
  187. {
  188. struct the_nilfs *nilfs = sbi->s_nilfs;
  189. int err;
  190. int barrier_done = 0;
  191. if (nilfs_test_opt(sbi, BARRIER)) {
  192. set_buffer_ordered(nilfs->ns_sbh[0]);
  193. barrier_done = 1;
  194. }
  195. retry:
  196. set_buffer_dirty(nilfs->ns_sbh[0]);
  197. err = sync_dirty_buffer(nilfs->ns_sbh[0]);
  198. if (err == -EOPNOTSUPP && barrier_done) {
  199. nilfs_warning(sbi->s_super, __func__,
  200. "barrier-based sync failed. "
  201. "disabling barriers\n");
  202. nilfs_clear_opt(sbi, BARRIER);
  203. barrier_done = 0;
  204. clear_buffer_ordered(nilfs->ns_sbh[0]);
  205. goto retry;
  206. }
  207. if (unlikely(err)) {
  208. printk(KERN_ERR
  209. "NILFS: unable to write superblock (err=%d)\n", err);
  210. if (err == -EIO && nilfs->ns_sbh[1]) {
  211. nilfs_fall_back_super_block(nilfs);
  212. goto retry;
  213. }
  214. } else {
  215. struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
  216. /*
  217. * The latest segment becomes trailable from the position
  218. * written in superblock.
  219. */
  220. clear_nilfs_discontinued(nilfs);
  221. /* update GC protection for recent segments */
  222. if (nilfs->ns_sbh[1]) {
  223. sbp = NULL;
  224. if (dupsb) {
  225. set_buffer_dirty(nilfs->ns_sbh[1]);
  226. if (!sync_dirty_buffer(nilfs->ns_sbh[1]))
  227. sbp = nilfs->ns_sbp[1];
  228. }
  229. }
  230. if (sbp) {
  231. spin_lock(&nilfs->ns_last_segment_lock);
  232. nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
  233. spin_unlock(&nilfs->ns_last_segment_lock);
  234. }
  235. }
  236. return err;
  237. }
  238. int nilfs_commit_super(struct nilfs_sb_info *sbi, int dupsb)
  239. {
  240. struct the_nilfs *nilfs = sbi->s_nilfs;
  241. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  242. sector_t nfreeblocks;
  243. time_t t;
  244. int err;
  245. /* nilfs->sem must be locked by the caller. */
  246. if (sbp[0]->s_magic != NILFS_SUPER_MAGIC) {
  247. if (sbp[1] && sbp[1]->s_magic == NILFS_SUPER_MAGIC)
  248. nilfs_swap_super_block(nilfs);
  249. else {
  250. printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
  251. sbi->s_super->s_id);
  252. return -EIO;
  253. }
  254. }
  255. err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
  256. if (unlikely(err)) {
  257. printk(KERN_ERR "NILFS: failed to count free blocks\n");
  258. return err;
  259. }
  260. spin_lock(&nilfs->ns_last_segment_lock);
  261. sbp[0]->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
  262. sbp[0]->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
  263. sbp[0]->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
  264. spin_unlock(&nilfs->ns_last_segment_lock);
  265. t = get_seconds();
  266. nilfs->ns_sbwtime[0] = t;
  267. sbp[0]->s_free_blocks_count = cpu_to_le64(nfreeblocks);
  268. sbp[0]->s_wtime = cpu_to_le64(t);
  269. sbp[0]->s_sum = 0;
  270. sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
  271. (unsigned char *)sbp[0],
  272. nilfs->ns_sbsize));
  273. if (dupsb && sbp[1]) {
  274. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  275. nilfs->ns_sbwtime[1] = t;
  276. }
  277. sbi->s_super->s_dirt = 0;
  278. return nilfs_sync_super(sbi, dupsb);
  279. }
  280. static void nilfs_put_super(struct super_block *sb)
  281. {
  282. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  283. struct the_nilfs *nilfs = sbi->s_nilfs;
  284. nilfs_detach_segment_constructor(sbi);
  285. if (!(sb->s_flags & MS_RDONLY)) {
  286. down_write(&nilfs->ns_sem);
  287. nilfs->ns_sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
  288. nilfs_commit_super(sbi, 1);
  289. up_write(&nilfs->ns_sem);
  290. }
  291. nilfs_detach_checkpoint(sbi);
  292. put_nilfs(sbi->s_nilfs);
  293. sbi->s_super = NULL;
  294. sb->s_fs_info = NULL;
  295. kfree(sbi);
  296. }
  297. /**
  298. * nilfs_write_super - write super block(s) of NILFS
  299. * @sb: super_block
  300. *
  301. * nilfs_write_super() gets a fs-dependent lock, writes super block(s), and
  302. * clears s_dirt. This function is called in the section protected by
  303. * lock_super().
  304. *
  305. * The s_dirt flag is managed by each filesystem and we protect it by ns_sem
  306. * of the struct the_nilfs. Lock order must be as follows:
  307. *
  308. * 1. lock_super()
  309. * 2. down_write(&nilfs->ns_sem)
  310. *
  311. * Inside NILFS, locking ns_sem is enough to protect s_dirt and the buffer
  312. * of the super block (nilfs->ns_sbp[]).
  313. *
  314. * In most cases, VFS functions call lock_super() before calling these
  315. * methods. So we must be careful not to bring on deadlocks when using
  316. * lock_super(); see generic_shutdown_super(), write_super(), and so on.
  317. *
  318. * Note that order of lock_kernel() and lock_super() depends on contexts
  319. * of VFS. We should also note that lock_kernel() can be used in its
  320. * protective section and only the outermost one has an effect.
  321. */
  322. static void nilfs_write_super(struct super_block *sb)
  323. {
  324. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  325. struct the_nilfs *nilfs = sbi->s_nilfs;
  326. down_write(&nilfs->ns_sem);
  327. if (!(sb->s_flags & MS_RDONLY)) {
  328. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  329. u64 t = get_seconds();
  330. int dupsb;
  331. if (!nilfs_discontinued(nilfs) && t >= nilfs->ns_sbwtime[0] &&
  332. t < nilfs->ns_sbwtime[0] + NILFS_SB_FREQ) {
  333. up_write(&nilfs->ns_sem);
  334. return;
  335. }
  336. dupsb = sbp[1] && t > nilfs->ns_sbwtime[1] + NILFS_ALTSB_FREQ;
  337. nilfs_commit_super(sbi, dupsb);
  338. }
  339. sb->s_dirt = 0;
  340. up_write(&nilfs->ns_sem);
  341. }
  342. static int nilfs_sync_fs(struct super_block *sb, int wait)
  343. {
  344. int err = 0;
  345. /* This function is called when super block should be written back */
  346. if (wait)
  347. err = nilfs_construct_segment(sb);
  348. return err;
  349. }
  350. int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno)
  351. {
  352. struct the_nilfs *nilfs = sbi->s_nilfs;
  353. struct nilfs_checkpoint *raw_cp;
  354. struct buffer_head *bh_cp;
  355. int err;
  356. down_write(&nilfs->ns_sem);
  357. list_add(&sbi->s_list, &nilfs->ns_supers);
  358. up_write(&nilfs->ns_sem);
  359. sbi->s_ifile = nilfs_mdt_new(
  360. nilfs, sbi->s_super, NILFS_IFILE_INO, NILFS_IFILE_GFP);
  361. if (!sbi->s_ifile)
  362. return -ENOMEM;
  363. err = nilfs_palloc_init_blockgroup(sbi->s_ifile, nilfs->ns_inode_size);
  364. if (unlikely(err))
  365. goto failed;
  366. err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
  367. &bh_cp);
  368. if (unlikely(err)) {
  369. if (err == -ENOENT || err == -EINVAL) {
  370. printk(KERN_ERR
  371. "NILFS: Invalid checkpoint "
  372. "(checkpoint number=%llu)\n",
  373. (unsigned long long)cno);
  374. err = -EINVAL;
  375. }
  376. goto failed;
  377. }
  378. err = nilfs_read_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode);
  379. if (unlikely(err))
  380. goto failed_bh;
  381. atomic_set(&sbi->s_inodes_count, le64_to_cpu(raw_cp->cp_inodes_count));
  382. atomic_set(&sbi->s_blocks_count, le64_to_cpu(raw_cp->cp_blocks_count));
  383. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  384. return 0;
  385. failed_bh:
  386. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  387. failed:
  388. nilfs_mdt_destroy(sbi->s_ifile);
  389. sbi->s_ifile = NULL;
  390. down_write(&nilfs->ns_sem);
  391. list_del_init(&sbi->s_list);
  392. up_write(&nilfs->ns_sem);
  393. return err;
  394. }
  395. void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi)
  396. {
  397. struct the_nilfs *nilfs = sbi->s_nilfs;
  398. nilfs_mdt_clear(sbi->s_ifile);
  399. nilfs_mdt_destroy(sbi->s_ifile);
  400. sbi->s_ifile = NULL;
  401. down_write(&nilfs->ns_sem);
  402. list_del_init(&sbi->s_list);
  403. up_write(&nilfs->ns_sem);
  404. }
  405. static int nilfs_mark_recovery_complete(struct nilfs_sb_info *sbi)
  406. {
  407. struct the_nilfs *nilfs = sbi->s_nilfs;
  408. int err = 0;
  409. down_write(&nilfs->ns_sem);
  410. if (!(nilfs->ns_mount_state & NILFS_VALID_FS)) {
  411. nilfs->ns_mount_state |= NILFS_VALID_FS;
  412. err = nilfs_commit_super(sbi, 1);
  413. if (likely(!err))
  414. printk(KERN_INFO "NILFS: recovery complete.\n");
  415. }
  416. up_write(&nilfs->ns_sem);
  417. return err;
  418. }
  419. static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  420. {
  421. struct super_block *sb = dentry->d_sb;
  422. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  423. unsigned long long blocks;
  424. unsigned long overhead;
  425. unsigned long nrsvblocks;
  426. sector_t nfreeblocks;
  427. struct the_nilfs *nilfs = sbi->s_nilfs;
  428. int err;
  429. /*
  430. * Compute all of the segment blocks
  431. *
  432. * The blocks before first segment and after last segment
  433. * are excluded.
  434. */
  435. blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
  436. - nilfs->ns_first_data_block;
  437. nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
  438. /*
  439. * Compute the overhead
  440. *
  441. * When distributing meta data blocks outside semgent structure,
  442. * We must count them as the overhead.
  443. */
  444. overhead = 0;
  445. err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
  446. if (unlikely(err))
  447. return err;
  448. buf->f_type = NILFS_SUPER_MAGIC;
  449. buf->f_bsize = sb->s_blocksize;
  450. buf->f_blocks = blocks - overhead;
  451. buf->f_bfree = nfreeblocks;
  452. buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
  453. (buf->f_bfree - nrsvblocks) : 0;
  454. buf->f_files = atomic_read(&sbi->s_inodes_count);
  455. buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */
  456. buf->f_namelen = NILFS_NAME_LEN;
  457. return 0;
  458. }
  459. static struct super_operations nilfs_sops = {
  460. .alloc_inode = nilfs_alloc_inode,
  461. .destroy_inode = nilfs_destroy_inode,
  462. .dirty_inode = nilfs_dirty_inode,
  463. /* .write_inode = nilfs_write_inode, */
  464. /* .put_inode = nilfs_put_inode, */
  465. /* .drop_inode = nilfs_drop_inode, */
  466. .delete_inode = nilfs_delete_inode,
  467. .put_super = nilfs_put_super,
  468. .write_super = nilfs_write_super,
  469. .sync_fs = nilfs_sync_fs,
  470. /* .write_super_lockfs */
  471. /* .unlockfs */
  472. .statfs = nilfs_statfs,
  473. .remount_fs = nilfs_remount,
  474. .clear_inode = nilfs_clear_inode,
  475. /* .umount_begin */
  476. /* .show_options */
  477. };
  478. static struct inode *
  479. nilfs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation)
  480. {
  481. struct inode *inode;
  482. if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO &&
  483. ino != NILFS_SKETCH_INO)
  484. return ERR_PTR(-ESTALE);
  485. inode = nilfs_iget(sb, ino);
  486. if (IS_ERR(inode))
  487. return ERR_CAST(inode);
  488. if (generation && inode->i_generation != generation) {
  489. iput(inode);
  490. return ERR_PTR(-ESTALE);
  491. }
  492. return inode;
  493. }
  494. static struct dentry *
  495. nilfs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
  496. int fh_type)
  497. {
  498. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  499. nilfs_nfs_get_inode);
  500. }
  501. static struct dentry *
  502. nilfs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len,
  503. int fh_type)
  504. {
  505. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  506. nilfs_nfs_get_inode);
  507. }
  508. static struct export_operations nilfs_export_ops = {
  509. .fh_to_dentry = nilfs_fh_to_dentry,
  510. .fh_to_parent = nilfs_fh_to_parent,
  511. .get_parent = nilfs_get_parent,
  512. };
  513. enum {
  514. Opt_err_cont, Opt_err_panic, Opt_err_ro,
  515. Opt_barrier, Opt_snapshot, Opt_order,
  516. Opt_err,
  517. };
  518. static match_table_t tokens = {
  519. {Opt_err_cont, "errors=continue"},
  520. {Opt_err_panic, "errors=panic"},
  521. {Opt_err_ro, "errors=remount-ro"},
  522. {Opt_barrier, "barrier=%s"},
  523. {Opt_snapshot, "cp=%u"},
  524. {Opt_order, "order=%s"},
  525. {Opt_err, NULL}
  526. };
  527. static int match_bool(substring_t *s, int *result)
  528. {
  529. int len = s->to - s->from;
  530. if (strncmp(s->from, "on", len) == 0)
  531. *result = 1;
  532. else if (strncmp(s->from, "off", len) == 0)
  533. *result = 0;
  534. else
  535. return 1;
  536. return 0;
  537. }
  538. static int parse_options(char *options, struct super_block *sb)
  539. {
  540. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  541. char *p;
  542. substring_t args[MAX_OPT_ARGS];
  543. int option;
  544. if (!options)
  545. return 1;
  546. while ((p = strsep(&options, ",")) != NULL) {
  547. int token;
  548. if (!*p)
  549. continue;
  550. token = match_token(p, tokens, args);
  551. switch (token) {
  552. case Opt_barrier:
  553. if (match_bool(&args[0], &option))
  554. return 0;
  555. if (option)
  556. nilfs_set_opt(sbi, BARRIER);
  557. else
  558. nilfs_clear_opt(sbi, BARRIER);
  559. break;
  560. case Opt_order:
  561. if (strcmp(args[0].from, "relaxed") == 0)
  562. /* Ordered data semantics */
  563. nilfs_clear_opt(sbi, STRICT_ORDER);
  564. else if (strcmp(args[0].from, "strict") == 0)
  565. /* Strict in-order semantics */
  566. nilfs_set_opt(sbi, STRICT_ORDER);
  567. else
  568. return 0;
  569. break;
  570. case Opt_err_panic:
  571. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC);
  572. break;
  573. case Opt_err_ro:
  574. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO);
  575. break;
  576. case Opt_err_cont:
  577. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT);
  578. break;
  579. case Opt_snapshot:
  580. if (match_int(&args[0], &option) || option <= 0)
  581. return 0;
  582. if (!(sb->s_flags & MS_RDONLY))
  583. return 0;
  584. sbi->s_snapshot_cno = option;
  585. nilfs_set_opt(sbi, SNAPSHOT);
  586. break;
  587. default:
  588. printk(KERN_ERR
  589. "NILFS: Unrecognized mount option \"%s\"\n", p);
  590. return 0;
  591. }
  592. }
  593. return 1;
  594. }
  595. static inline void
  596. nilfs_set_default_options(struct nilfs_sb_info *sbi,
  597. struct nilfs_super_block *sbp)
  598. {
  599. sbi->s_mount_opt =
  600. NILFS_MOUNT_ERRORS_CONT | NILFS_MOUNT_BARRIER;
  601. }
  602. static int nilfs_setup_super(struct nilfs_sb_info *sbi)
  603. {
  604. struct the_nilfs *nilfs = sbi->s_nilfs;
  605. struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
  606. int max_mnt_count = le16_to_cpu(sbp->s_max_mnt_count);
  607. int mnt_count = le16_to_cpu(sbp->s_mnt_count);
  608. /* nilfs->sem must be locked by the caller. */
  609. if (!(nilfs->ns_mount_state & NILFS_VALID_FS)) {
  610. printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
  611. } else if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
  612. printk(KERN_WARNING
  613. "NILFS warning: mounting fs with errors\n");
  614. #if 0
  615. } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
  616. printk(KERN_WARNING
  617. "NILFS warning: maximal mount count reached\n");
  618. #endif
  619. }
  620. if (!max_mnt_count)
  621. sbp->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
  622. sbp->s_mnt_count = cpu_to_le16(mnt_count + 1);
  623. sbp->s_state = cpu_to_le16(le16_to_cpu(sbp->s_state) & ~NILFS_VALID_FS);
  624. sbp->s_mtime = cpu_to_le64(get_seconds());
  625. return nilfs_commit_super(sbi, 1);
  626. }
  627. struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
  628. u64 pos, int blocksize,
  629. struct buffer_head **pbh)
  630. {
  631. unsigned long long sb_index = pos;
  632. unsigned long offset;
  633. offset = do_div(sb_index, blocksize);
  634. *pbh = sb_bread(sb, sb_index);
  635. if (!*pbh)
  636. return NULL;
  637. return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
  638. }
  639. int nilfs_store_magic_and_option(struct super_block *sb,
  640. struct nilfs_super_block *sbp,
  641. char *data)
  642. {
  643. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  644. sb->s_magic = le16_to_cpu(sbp->s_magic);
  645. /* FS independent flags */
  646. #ifdef NILFS_ATIME_DISABLE
  647. sb->s_flags |= MS_NOATIME;
  648. #endif
  649. nilfs_set_default_options(sbi, sbp);
  650. sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid);
  651. sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid);
  652. sbi->s_interval = le32_to_cpu(sbp->s_c_interval);
  653. sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max);
  654. return !parse_options(data, sb) ? -EINVAL : 0 ;
  655. }
  656. /**
  657. * nilfs_fill_super() - initialize a super block instance
  658. * @sb: super_block
  659. * @data: mount options
  660. * @silent: silent mode flag
  661. * @nilfs: the_nilfs struct
  662. *
  663. * This function is called exclusively by bd_mount_mutex.
  664. * So, the recovery process is protected from other simultaneous mounts.
  665. */
  666. static int
  667. nilfs_fill_super(struct super_block *sb, void *data, int silent,
  668. struct the_nilfs *nilfs)
  669. {
  670. struct nilfs_sb_info *sbi;
  671. struct inode *root;
  672. __u64 cno;
  673. int err;
  674. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  675. if (!sbi)
  676. return -ENOMEM;
  677. sb->s_fs_info = sbi;
  678. get_nilfs(nilfs);
  679. sbi->s_nilfs = nilfs;
  680. sbi->s_super = sb;
  681. err = init_nilfs(nilfs, sbi, (char *)data);
  682. if (err)
  683. goto failed_sbi;
  684. spin_lock_init(&sbi->s_inode_lock);
  685. INIT_LIST_HEAD(&sbi->s_dirty_files);
  686. INIT_LIST_HEAD(&sbi->s_list);
  687. /*
  688. * Following initialization is overlapped because
  689. * nilfs_sb_info structure has been cleared at the beginning.
  690. * But we reserve them to keep our interest and make ready
  691. * for the future change.
  692. */
  693. get_random_bytes(&sbi->s_next_generation,
  694. sizeof(sbi->s_next_generation));
  695. spin_lock_init(&sbi->s_next_gen_lock);
  696. sb->s_op = &nilfs_sops;
  697. sb->s_export_op = &nilfs_export_ops;
  698. sb->s_root = NULL;
  699. sb->s_time_gran = 1;
  700. if (!nilfs_loaded(nilfs)) {
  701. err = load_nilfs(nilfs, sbi);
  702. if (err)
  703. goto failed_sbi;
  704. }
  705. cno = nilfs_last_cno(nilfs);
  706. if (sb->s_flags & MS_RDONLY) {
  707. if (nilfs_test_opt(sbi, SNAPSHOT)) {
  708. err = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile,
  709. sbi->s_snapshot_cno);
  710. if (err < 0)
  711. goto failed_sbi;
  712. if (!err) {
  713. printk(KERN_ERR
  714. "NILFS: The specified checkpoint is "
  715. "not a snapshot "
  716. "(checkpoint number=%llu).\n",
  717. (unsigned long long)sbi->s_snapshot_cno);
  718. err = -EINVAL;
  719. goto failed_sbi;
  720. }
  721. cno = sbi->s_snapshot_cno;
  722. } else
  723. /* Read-only mount */
  724. sbi->s_snapshot_cno = cno;
  725. }
  726. err = nilfs_attach_checkpoint(sbi, cno);
  727. if (err) {
  728. printk(KERN_ERR "NILFS: error loading a checkpoint"
  729. " (checkpoint number=%llu).\n", (unsigned long long)cno);
  730. goto failed_sbi;
  731. }
  732. if (!(sb->s_flags & MS_RDONLY)) {
  733. err = nilfs_attach_segment_constructor(sbi);
  734. if (err)
  735. goto failed_checkpoint;
  736. }
  737. root = nilfs_iget(sb, NILFS_ROOT_INO);
  738. if (IS_ERR(root)) {
  739. printk(KERN_ERR "NILFS: get root inode failed\n");
  740. err = PTR_ERR(root);
  741. goto failed_segctor;
  742. }
  743. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  744. iput(root);
  745. printk(KERN_ERR "NILFS: corrupt root inode.\n");
  746. err = -EINVAL;
  747. goto failed_segctor;
  748. }
  749. sb->s_root = d_alloc_root(root);
  750. if (!sb->s_root) {
  751. iput(root);
  752. printk(KERN_ERR "NILFS: get root dentry failed\n");
  753. err = -ENOMEM;
  754. goto failed_segctor;
  755. }
  756. if (!(sb->s_flags & MS_RDONLY)) {
  757. down_write(&nilfs->ns_sem);
  758. nilfs_setup_super(sbi);
  759. up_write(&nilfs->ns_sem);
  760. }
  761. err = nilfs_mark_recovery_complete(sbi);
  762. if (unlikely(err)) {
  763. printk(KERN_ERR "NILFS: recovery failed.\n");
  764. goto failed_root;
  765. }
  766. return 0;
  767. failed_root:
  768. dput(sb->s_root);
  769. sb->s_root = NULL;
  770. failed_segctor:
  771. nilfs_detach_segment_constructor(sbi);
  772. failed_checkpoint:
  773. nilfs_detach_checkpoint(sbi);
  774. failed_sbi:
  775. put_nilfs(nilfs);
  776. sb->s_fs_info = NULL;
  777. kfree(sbi);
  778. return err;
  779. }
  780. static int nilfs_remount(struct super_block *sb, int *flags, char *data)
  781. {
  782. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  783. struct nilfs_super_block *sbp;
  784. struct the_nilfs *nilfs = sbi->s_nilfs;
  785. unsigned long old_sb_flags;
  786. struct nilfs_mount_options old_opts;
  787. int err;
  788. old_sb_flags = sb->s_flags;
  789. old_opts.mount_opt = sbi->s_mount_opt;
  790. old_opts.snapshot_cno = sbi->s_snapshot_cno;
  791. if (!parse_options(data, sb)) {
  792. err = -EINVAL;
  793. goto restore_opts;
  794. }
  795. sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
  796. if ((*flags & MS_RDONLY) &&
  797. sbi->s_snapshot_cno != old_opts.snapshot_cno) {
  798. printk(KERN_WARNING "NILFS (device %s): couldn't "
  799. "remount to a different snapshot. \n",
  800. sb->s_id);
  801. err = -EINVAL;
  802. goto restore_opts;
  803. }
  804. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  805. goto out;
  806. if (*flags & MS_RDONLY) {
  807. /* Shutting down the segment constructor */
  808. nilfs_detach_segment_constructor(sbi);
  809. sb->s_flags |= MS_RDONLY;
  810. sbi->s_snapshot_cno = nilfs_last_cno(nilfs);
  811. /* nilfs_set_opt(sbi, SNAPSHOT); */
  812. /*
  813. * Remounting a valid RW partition RDONLY, so set
  814. * the RDONLY flag and then mark the partition as valid again.
  815. */
  816. down_write(&nilfs->ns_sem);
  817. sbp = nilfs->ns_sbp[0];
  818. if (!(sbp->s_state & le16_to_cpu(NILFS_VALID_FS)) &&
  819. (nilfs->ns_mount_state & NILFS_VALID_FS))
  820. sbp->s_state = cpu_to_le16(nilfs->ns_mount_state);
  821. sbp->s_mtime = cpu_to_le64(get_seconds());
  822. nilfs_commit_super(sbi, 1);
  823. up_write(&nilfs->ns_sem);
  824. } else {
  825. /*
  826. * Mounting a RDONLY partition read-write, so reread and
  827. * store the current valid flag. (It may have been changed
  828. * by fsck since we originally mounted the partition.)
  829. */
  830. down(&sb->s_bdev->bd_mount_sem);
  831. /* Check existing RW-mount */
  832. if (test_exclusive_mount(sb->s_type, sb->s_bdev, 0)) {
  833. printk(KERN_WARNING "NILFS (device %s): couldn't "
  834. "remount because a RW-mount exists.\n",
  835. sb->s_id);
  836. err = -EBUSY;
  837. goto rw_remount_failed;
  838. }
  839. if (sbi->s_snapshot_cno != nilfs_last_cno(nilfs)) {
  840. printk(KERN_WARNING "NILFS (device %s): couldn't "
  841. "remount because the current RO-mount is not "
  842. "the latest one.\n",
  843. sb->s_id);
  844. err = -EINVAL;
  845. goto rw_remount_failed;
  846. }
  847. sb->s_flags &= ~MS_RDONLY;
  848. nilfs_clear_opt(sbi, SNAPSHOT);
  849. sbi->s_snapshot_cno = 0;
  850. err = nilfs_attach_segment_constructor(sbi);
  851. if (err)
  852. goto rw_remount_failed;
  853. down_write(&nilfs->ns_sem);
  854. nilfs_setup_super(sbi);
  855. up_write(&nilfs->ns_sem);
  856. up(&sb->s_bdev->bd_mount_sem);
  857. }
  858. out:
  859. return 0;
  860. rw_remount_failed:
  861. up(&sb->s_bdev->bd_mount_sem);
  862. restore_opts:
  863. sb->s_flags = old_sb_flags;
  864. sbi->s_mount_opt = old_opts.mount_opt;
  865. sbi->s_snapshot_cno = old_opts.snapshot_cno;
  866. return err;
  867. }
  868. struct nilfs_super_data {
  869. struct block_device *bdev;
  870. __u64 cno;
  871. int flags;
  872. };
  873. /**
  874. * nilfs_identify - pre-read mount options needed to identify mount instance
  875. * @data: mount options
  876. * @sd: nilfs_super_data
  877. */
  878. static int nilfs_identify(char *data, struct nilfs_super_data *sd)
  879. {
  880. char *p, *options = data;
  881. substring_t args[MAX_OPT_ARGS];
  882. int option, token;
  883. int ret = 0;
  884. do {
  885. p = strsep(&options, ",");
  886. if (p != NULL && *p) {
  887. token = match_token(p, tokens, args);
  888. if (token == Opt_snapshot) {
  889. if (!(sd->flags & MS_RDONLY))
  890. ret++;
  891. else {
  892. ret = match_int(&args[0], &option);
  893. if (!ret) {
  894. if (option > 0)
  895. sd->cno = option;
  896. else
  897. ret++;
  898. }
  899. }
  900. }
  901. if (ret)
  902. printk(KERN_ERR
  903. "NILFS: invalid mount option: %s\n", p);
  904. }
  905. if (!options)
  906. break;
  907. BUG_ON(options == data);
  908. *(options - 1) = ',';
  909. } while (!ret);
  910. return ret;
  911. }
  912. static int nilfs_set_bdev_super(struct super_block *s, void *data)
  913. {
  914. struct nilfs_super_data *sd = data;
  915. s->s_bdev = sd->bdev;
  916. s->s_dev = s->s_bdev->bd_dev;
  917. return 0;
  918. }
  919. static int nilfs_test_bdev_super(struct super_block *s, void *data)
  920. {
  921. struct nilfs_super_data *sd = data;
  922. return s->s_bdev == sd->bdev;
  923. }
  924. static int nilfs_test_bdev_super2(struct super_block *s, void *data)
  925. {
  926. struct nilfs_super_data *sd = data;
  927. int ret;
  928. if (s->s_bdev != sd->bdev)
  929. return 0;
  930. if (!((s->s_flags | sd->flags) & MS_RDONLY))
  931. return 1; /* Reuse an old R/W-mode super_block */
  932. if (s->s_flags & sd->flags & MS_RDONLY) {
  933. if (down_read_trylock(&s->s_umount)) {
  934. ret = s->s_root &&
  935. (sd->cno == NILFS_SB(s)->s_snapshot_cno);
  936. up_read(&s->s_umount);
  937. /*
  938. * This path is locked with sb_lock by sget().
  939. * So, drop_super() causes deadlock.
  940. */
  941. return ret;
  942. }
  943. }
  944. return 0;
  945. }
  946. static int
  947. nilfs_get_sb(struct file_system_type *fs_type, int flags,
  948. const char *dev_name, void *data, struct vfsmount *mnt)
  949. {
  950. struct nilfs_super_data sd;
  951. struct super_block *s, *s2;
  952. struct the_nilfs *nilfs = NULL;
  953. int err, need_to_close = 1;
  954. sd.bdev = open_bdev_exclusive(dev_name, flags, fs_type);
  955. if (IS_ERR(sd.bdev))
  956. return PTR_ERR(sd.bdev);
  957. /*
  958. * To get mount instance using sget() vfs-routine, NILFS needs
  959. * much more information than normal filesystems to identify mount
  960. * instance. For snapshot mounts, not only a mount type (ro-mount
  961. * or rw-mount) but also a checkpoint number is required.
  962. * The results are passed in sget() using nilfs_super_data.
  963. */
  964. sd.cno = 0;
  965. sd.flags = flags;
  966. if (nilfs_identify((char *)data, &sd)) {
  967. err = -EINVAL;
  968. goto failed;
  969. }
  970. /*
  971. * once the super is inserted into the list by sget, s_umount
  972. * will protect the lockfs code from trying to start a snapshot
  973. * while we are mounting
  974. */
  975. down(&sd.bdev->bd_mount_sem);
  976. if (!sd.cno &&
  977. (err = test_exclusive_mount(fs_type, sd.bdev, flags ^ MS_RDONLY))) {
  978. err = (err < 0) ? : -EBUSY;
  979. goto failed_unlock;
  980. }
  981. /*
  982. * Phase-1: search any existent instance and get the_nilfs
  983. */
  984. s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, &sd);
  985. if (IS_ERR(s))
  986. goto error_s;
  987. if (!s->s_root) {
  988. err = -ENOMEM;
  989. nilfs = alloc_nilfs(sd.bdev);
  990. if (!nilfs)
  991. goto cancel_new;
  992. } else {
  993. struct nilfs_sb_info *sbi = NILFS_SB(s);
  994. /*
  995. * s_umount protects super_block from unmount process;
  996. * It covers pointers of nilfs_sb_info and the_nilfs.
  997. */
  998. nilfs = sbi->s_nilfs;
  999. get_nilfs(nilfs);
  1000. up_write(&s->s_umount);
  1001. /*
  1002. * Phase-2: search specified snapshot or R/W mode super_block
  1003. */
  1004. if (!sd.cno)
  1005. /* trying to get the latest checkpoint. */
  1006. sd.cno = nilfs_last_cno(nilfs);
  1007. s2 = sget(fs_type, nilfs_test_bdev_super2,
  1008. nilfs_set_bdev_super, &sd);
  1009. deactivate_super(s);
  1010. /*
  1011. * Although deactivate_super() invokes close_bdev_exclusive() at
  1012. * kill_block_super(). Here, s is an existent mount; we need
  1013. * one more close_bdev_exclusive() call.
  1014. */
  1015. s = s2;
  1016. if (IS_ERR(s))
  1017. goto error_s;
  1018. }
  1019. if (!s->s_root) {
  1020. char b[BDEVNAME_SIZE];
  1021. s->s_flags = flags;
  1022. strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id));
  1023. sb_set_blocksize(s, block_size(sd.bdev));
  1024. err = nilfs_fill_super(s, data, flags & MS_VERBOSE, nilfs);
  1025. if (err)
  1026. goto cancel_new;
  1027. s->s_flags |= MS_ACTIVE;
  1028. need_to_close = 0;
  1029. } else if (!(s->s_flags & MS_RDONLY)) {
  1030. err = -EBUSY;
  1031. }
  1032. up(&sd.bdev->bd_mount_sem);
  1033. put_nilfs(nilfs);
  1034. if (need_to_close)
  1035. close_bdev_exclusive(sd.bdev, flags);
  1036. simple_set_mnt(mnt, s);
  1037. return 0;
  1038. error_s:
  1039. up(&sd.bdev->bd_mount_sem);
  1040. if (nilfs)
  1041. put_nilfs(nilfs);
  1042. close_bdev_exclusive(sd.bdev, flags);
  1043. return PTR_ERR(s);
  1044. failed_unlock:
  1045. up(&sd.bdev->bd_mount_sem);
  1046. failed:
  1047. close_bdev_exclusive(sd.bdev, flags);
  1048. return err;
  1049. cancel_new:
  1050. /* Abandoning the newly allocated superblock */
  1051. up(&sd.bdev->bd_mount_sem);
  1052. if (nilfs)
  1053. put_nilfs(nilfs);
  1054. up_write(&s->s_umount);
  1055. deactivate_super(s);
  1056. /*
  1057. * deactivate_super() invokes close_bdev_exclusive().
  1058. * We must finish all post-cleaning before this call;
  1059. * put_nilfs() and unlocking bd_mount_sem need the block device.
  1060. */
  1061. return err;
  1062. }
  1063. static int nilfs_test_bdev_super3(struct super_block *s, void *data)
  1064. {
  1065. struct nilfs_super_data *sd = data;
  1066. int ret;
  1067. if (s->s_bdev != sd->bdev)
  1068. return 0;
  1069. if (down_read_trylock(&s->s_umount)) {
  1070. ret = (s->s_flags & MS_RDONLY) && s->s_root &&
  1071. nilfs_test_opt(NILFS_SB(s), SNAPSHOT);
  1072. up_read(&s->s_umount);
  1073. if (ret)
  1074. return 0; /* ignore snapshot mounts */
  1075. }
  1076. return !((sd->flags ^ s->s_flags) & MS_RDONLY);
  1077. }
  1078. static int __false_bdev_super(struct super_block *s, void *data)
  1079. {
  1080. #if 0 /* XXX: workaround for lock debug. This is not good idea */
  1081. up_write(&s->s_umount);
  1082. #endif
  1083. return -EFAULT;
  1084. }
  1085. /**
  1086. * test_exclusive_mount - check whether an exclusive RW/RO mount exists or not.
  1087. * fs_type: filesystem type
  1088. * bdev: block device
  1089. * flag: 0 (check rw-mount) or MS_RDONLY (check ro-mount)
  1090. * res: pointer to an integer to store result
  1091. *
  1092. * This function must be called within a section protected by bd_mount_mutex.
  1093. */
  1094. static int test_exclusive_mount(struct file_system_type *fs_type,
  1095. struct block_device *bdev, int flags)
  1096. {
  1097. struct super_block *s;
  1098. struct nilfs_super_data sd = { .flags = flags, .bdev = bdev };
  1099. s = sget(fs_type, nilfs_test_bdev_super3, __false_bdev_super, &sd);
  1100. if (IS_ERR(s)) {
  1101. if (PTR_ERR(s) != -EFAULT)
  1102. return PTR_ERR(s);
  1103. return 0; /* Not found */
  1104. }
  1105. up_write(&s->s_umount);
  1106. deactivate_super(s);
  1107. return 1; /* Found */
  1108. }
  1109. struct file_system_type nilfs_fs_type = {
  1110. .owner = THIS_MODULE,
  1111. .name = "nilfs2",
  1112. .get_sb = nilfs_get_sb,
  1113. .kill_sb = kill_block_super,
  1114. .fs_flags = FS_REQUIRES_DEV,
  1115. };
  1116. static int __init init_nilfs_fs(void)
  1117. {
  1118. int err;
  1119. err = nilfs_init_inode_cache();
  1120. if (err)
  1121. goto failed;
  1122. err = nilfs_init_transaction_cache();
  1123. if (err)
  1124. goto failed_inode_cache;
  1125. err = nilfs_init_segbuf_cache();
  1126. if (err)
  1127. goto failed_transaction_cache;
  1128. err = nilfs_btree_path_cache_init();
  1129. if (err)
  1130. goto failed_segbuf_cache;
  1131. err = register_filesystem(&nilfs_fs_type);
  1132. if (err)
  1133. goto failed_btree_path_cache;
  1134. return 0;
  1135. failed_btree_path_cache:
  1136. nilfs_btree_path_cache_destroy();
  1137. failed_segbuf_cache:
  1138. nilfs_destroy_segbuf_cache();
  1139. failed_transaction_cache:
  1140. nilfs_destroy_transaction_cache();
  1141. failed_inode_cache:
  1142. nilfs_destroy_inode_cache();
  1143. failed:
  1144. return err;
  1145. }
  1146. static void __exit exit_nilfs_fs(void)
  1147. {
  1148. nilfs_destroy_segbuf_cache();
  1149. nilfs_destroy_transaction_cache();
  1150. nilfs_destroy_inode_cache();
  1151. nilfs_btree_path_cache_destroy();
  1152. unregister_filesystem(&nilfs_fs_type);
  1153. }
  1154. module_init(init_nilfs_fs)
  1155. module_exit(exit_nilfs_fs)