super.c 31 KB

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