super.c 33 KB

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