super.c 31 KB

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