super.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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/vfs.h>
  48. #include <linux/writeback.h>
  49. #include <linux/kobject.h>
  50. #include <linux/seq_file.h>
  51. #include <linux/mount.h>
  52. #include "nilfs.h"
  53. #include "export.h"
  54. #include "mdt.h"
  55. #include "alloc.h"
  56. #include "btree.h"
  57. #include "btnode.h"
  58. #include "page.h"
  59. #include "cpfile.h"
  60. #include "ifile.h"
  61. #include "dat.h"
  62. #include "segment.h"
  63. #include "segbuf.h"
  64. MODULE_AUTHOR("NTT Corp.");
  65. MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
  66. "(NILFS)");
  67. MODULE_LICENSE("GPL");
  68. struct kmem_cache *nilfs_inode_cachep;
  69. struct kmem_cache *nilfs_transaction_cachep;
  70. struct kmem_cache *nilfs_segbuf_cachep;
  71. struct kmem_cache *nilfs_btree_path_cache;
  72. static int nilfs_remount(struct super_block *sb, int *flags, char *data);
  73. static void nilfs_set_error(struct nilfs_sb_info *sbi)
  74. {
  75. struct the_nilfs *nilfs = sbi->s_nilfs;
  76. struct nilfs_super_block **sbp;
  77. down_write(&nilfs->ns_sem);
  78. if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
  79. nilfs->ns_mount_state |= NILFS_ERROR_FS;
  80. sbp = nilfs_prepare_super(sbi, 0);
  81. if (likely(sbp)) {
  82. sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
  83. if (sbp[1])
  84. sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
  85. nilfs_commit_super(sbi, NILFS_SB_COMMIT_ALL);
  86. }
  87. }
  88. up_write(&nilfs->ns_sem);
  89. }
  90. /**
  91. * nilfs_error() - report failure condition on a filesystem
  92. *
  93. * nilfs_error() sets an ERROR_FS flag on the superblock as well as
  94. * reporting an error message. It should be called when NILFS detects
  95. * incoherences or defects of meta data on disk. As for sustainable
  96. * errors such as a single-shot I/O error, nilfs_warning() or the printk()
  97. * function should be used instead.
  98. *
  99. * The segment constructor must not call this function because it can
  100. * kill itself.
  101. */
  102. void nilfs_error(struct super_block *sb, const char *function,
  103. const char *fmt, ...)
  104. {
  105. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  106. va_list args;
  107. va_start(args, fmt);
  108. printk(KERN_CRIT "NILFS error (device %s): %s: ", sb->s_id, function);
  109. vprintk(fmt, args);
  110. printk("\n");
  111. va_end(args);
  112. if (!(sb->s_flags & MS_RDONLY)) {
  113. nilfs_set_error(sbi);
  114. if (nilfs_test_opt(sbi, ERRORS_RO)) {
  115. printk(KERN_CRIT "Remounting filesystem read-only\n");
  116. sb->s_flags |= MS_RDONLY;
  117. }
  118. }
  119. if (nilfs_test_opt(sbi, ERRORS_PANIC))
  120. panic("NILFS (device %s): panic forced after error\n",
  121. sb->s_id);
  122. }
  123. void nilfs_warning(struct super_block *sb, const char *function,
  124. const char *fmt, ...)
  125. {
  126. va_list args;
  127. va_start(args, fmt);
  128. printk(KERN_WARNING "NILFS warning (device %s): %s: ",
  129. sb->s_id, function);
  130. vprintk(fmt, args);
  131. printk("\n");
  132. va_end(args);
  133. }
  134. struct inode *nilfs_alloc_inode_common(struct the_nilfs *nilfs)
  135. {
  136. struct nilfs_inode_info *ii;
  137. ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
  138. if (!ii)
  139. return NULL;
  140. ii->i_bh = NULL;
  141. ii->i_state = 0;
  142. ii->i_cno = 0;
  143. ii->vfs_inode.i_version = 1;
  144. nilfs_btnode_cache_init(&ii->i_btnode_cache, nilfs->ns_bdi);
  145. return &ii->vfs_inode;
  146. }
  147. struct inode *nilfs_alloc_inode(struct super_block *sb)
  148. {
  149. return nilfs_alloc_inode_common(NILFS_SB(sb)->s_nilfs);
  150. }
  151. void nilfs_destroy_inode(struct inode *inode)
  152. {
  153. struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
  154. if (mdi) {
  155. kfree(mdi->mi_bgl); /* kfree(NULL) is safe */
  156. kfree(mdi);
  157. }
  158. kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
  159. }
  160. static int nilfs_sync_super(struct nilfs_sb_info *sbi, int flag)
  161. {
  162. struct the_nilfs *nilfs = sbi->s_nilfs;
  163. int err;
  164. retry:
  165. set_buffer_dirty(nilfs->ns_sbh[0]);
  166. if (nilfs_test_opt(sbi, BARRIER)) {
  167. err = __sync_dirty_buffer(nilfs->ns_sbh[0],
  168. WRITE_SYNC | WRITE_BARRIER);
  169. if (err == -EOPNOTSUPP) {
  170. nilfs_warning(sbi->s_super, __func__,
  171. "barrier-based sync failed. "
  172. "disabling barriers\n");
  173. nilfs_clear_opt(sbi, BARRIER);
  174. goto retry;
  175. }
  176. } else {
  177. err = sync_dirty_buffer(nilfs->ns_sbh[0]);
  178. }
  179. if (unlikely(err)) {
  180. printk(KERN_ERR
  181. "NILFS: unable to write superblock (err=%d)\n", err);
  182. if (err == -EIO && nilfs->ns_sbh[1]) {
  183. /*
  184. * sbp[0] points to newer log than sbp[1],
  185. * so copy sbp[0] to sbp[1] to take over sbp[0].
  186. */
  187. memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0],
  188. nilfs->ns_sbsize);
  189. nilfs_fall_back_super_block(nilfs);
  190. goto retry;
  191. }
  192. } else {
  193. struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
  194. nilfs->ns_sbwcount++;
  195. /*
  196. * The latest segment becomes trailable from the position
  197. * written in superblock.
  198. */
  199. clear_nilfs_discontinued(nilfs);
  200. /* update GC protection for recent segments */
  201. if (nilfs->ns_sbh[1]) {
  202. if (flag == NILFS_SB_COMMIT_ALL) {
  203. set_buffer_dirty(nilfs->ns_sbh[1]);
  204. if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
  205. goto out;
  206. }
  207. if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) <
  208. le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno))
  209. sbp = nilfs->ns_sbp[1];
  210. }
  211. spin_lock(&nilfs->ns_last_segment_lock);
  212. nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
  213. spin_unlock(&nilfs->ns_last_segment_lock);
  214. }
  215. out:
  216. return err;
  217. }
  218. void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
  219. struct the_nilfs *nilfs)
  220. {
  221. sector_t nfreeblocks;
  222. /* nilfs->ns_sem must be locked by the caller. */
  223. nilfs_count_free_blocks(nilfs, &nfreeblocks);
  224. sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
  225. spin_lock(&nilfs->ns_last_segment_lock);
  226. sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
  227. sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
  228. sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
  229. spin_unlock(&nilfs->ns_last_segment_lock);
  230. }
  231. struct nilfs_super_block **nilfs_prepare_super(struct nilfs_sb_info *sbi,
  232. int flip)
  233. {
  234. struct the_nilfs *nilfs = sbi->s_nilfs;
  235. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  236. /* nilfs->ns_sem must be locked by the caller. */
  237. if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
  238. if (sbp[1] &&
  239. sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
  240. memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
  241. } else {
  242. printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
  243. sbi->s_super->s_id);
  244. return NULL;
  245. }
  246. } else if (sbp[1] &&
  247. sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
  248. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  249. }
  250. if (flip && sbp[1])
  251. nilfs_swap_super_block(nilfs);
  252. return sbp;
  253. }
  254. int nilfs_commit_super(struct nilfs_sb_info *sbi, int flag)
  255. {
  256. struct the_nilfs *nilfs = sbi->s_nilfs;
  257. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  258. time_t t;
  259. /* nilfs->ns_sem must be locked by the caller. */
  260. t = get_seconds();
  261. nilfs->ns_sbwtime = t;
  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 (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
  268. sbp[1]->s_wtime = sbp[0]->s_wtime;
  269. sbp[1]->s_sum = 0;
  270. sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
  271. (unsigned char *)sbp[1],
  272. nilfs->ns_sbsize));
  273. }
  274. clear_nilfs_sb_dirty(nilfs);
  275. return nilfs_sync_super(sbi, flag);
  276. }
  277. /**
  278. * nilfs_cleanup_super() - write filesystem state for cleanup
  279. * @sbi: nilfs_sb_info to be unmounted or degraded to read-only
  280. *
  281. * This function restores state flags in the on-disk super block.
  282. * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
  283. * filesystem was not clean previously.
  284. */
  285. int nilfs_cleanup_super(struct nilfs_sb_info *sbi)
  286. {
  287. struct nilfs_super_block **sbp;
  288. int flag = NILFS_SB_COMMIT;
  289. int ret = -EIO;
  290. sbp = nilfs_prepare_super(sbi, 0);
  291. if (sbp) {
  292. sbp[0]->s_state = cpu_to_le16(sbi->s_nilfs->ns_mount_state);
  293. nilfs_set_log_cursor(sbp[0], sbi->s_nilfs);
  294. if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
  295. /*
  296. * make the "clean" flag also to the opposite
  297. * super block if both super blocks point to
  298. * the same checkpoint.
  299. */
  300. sbp[1]->s_state = sbp[0]->s_state;
  301. flag = NILFS_SB_COMMIT_ALL;
  302. }
  303. ret = nilfs_commit_super(sbi, flag);
  304. }
  305. return ret;
  306. }
  307. static void nilfs_put_super(struct super_block *sb)
  308. {
  309. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  310. struct the_nilfs *nilfs = sbi->s_nilfs;
  311. nilfs_detach_segment_constructor(sbi);
  312. if (!(sb->s_flags & MS_RDONLY)) {
  313. down_write(&nilfs->ns_sem);
  314. nilfs_cleanup_super(sbi);
  315. up_write(&nilfs->ns_sem);
  316. }
  317. destroy_nilfs(nilfs);
  318. sbi->s_super = NULL;
  319. sb->s_fs_info = NULL;
  320. kfree(sbi);
  321. }
  322. static int nilfs_sync_fs(struct super_block *sb, int wait)
  323. {
  324. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  325. struct the_nilfs *nilfs = sbi->s_nilfs;
  326. struct nilfs_super_block **sbp;
  327. int err = 0;
  328. /* This function is called when super block should be written back */
  329. if (wait)
  330. err = nilfs_construct_segment(sb);
  331. down_write(&nilfs->ns_sem);
  332. if (nilfs_sb_dirty(nilfs)) {
  333. sbp = nilfs_prepare_super(sbi, nilfs_sb_will_flip(nilfs));
  334. if (likely(sbp)) {
  335. nilfs_set_log_cursor(sbp[0], nilfs);
  336. nilfs_commit_super(sbi, NILFS_SB_COMMIT);
  337. }
  338. }
  339. up_write(&nilfs->ns_sem);
  340. return err;
  341. }
  342. int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno, int curr_mnt,
  343. struct nilfs_root **rootp)
  344. {
  345. struct the_nilfs *nilfs = sbi->s_nilfs;
  346. struct nilfs_root *root;
  347. struct nilfs_checkpoint *raw_cp;
  348. struct buffer_head *bh_cp;
  349. int err = -ENOMEM;
  350. root = nilfs_find_or_create_root(
  351. nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
  352. if (!root)
  353. return err;
  354. if (root->ifile)
  355. goto reuse; /* already attached checkpoint */
  356. root->ifile = nilfs_ifile_new(sbi, nilfs->ns_inode_size);
  357. if (!root->ifile)
  358. goto failed;
  359. down_read(&nilfs->ns_segctor_sem);
  360. err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
  361. &bh_cp);
  362. up_read(&nilfs->ns_segctor_sem);
  363. if (unlikely(err)) {
  364. if (err == -ENOENT || err == -EINVAL) {
  365. printk(KERN_ERR
  366. "NILFS: Invalid checkpoint "
  367. "(checkpoint number=%llu)\n",
  368. (unsigned long long)cno);
  369. err = -EINVAL;
  370. }
  371. goto failed;
  372. }
  373. err = nilfs_read_inode_common(root->ifile, &raw_cp->cp_ifile_inode);
  374. if (unlikely(err))
  375. goto failed_bh;
  376. atomic_set(&root->inodes_count, le64_to_cpu(raw_cp->cp_inodes_count));
  377. atomic_set(&root->blocks_count, le64_to_cpu(raw_cp->cp_blocks_count));
  378. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  379. reuse:
  380. *rootp = root;
  381. return 0;
  382. failed_bh:
  383. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  384. failed:
  385. nilfs_put_root(root);
  386. return err;
  387. }
  388. static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  389. {
  390. struct super_block *sb = dentry->d_sb;
  391. struct nilfs_root *root = NILFS_I(dentry->d_inode)->i_root;
  392. struct the_nilfs *nilfs = root->nilfs;
  393. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  394. unsigned long long blocks;
  395. unsigned long overhead;
  396. unsigned long nrsvblocks;
  397. sector_t nfreeblocks;
  398. int err;
  399. /*
  400. * Compute all of the segment blocks
  401. *
  402. * The blocks before first segment and after last segment
  403. * are excluded.
  404. */
  405. blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
  406. - nilfs->ns_first_data_block;
  407. nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
  408. /*
  409. * Compute the overhead
  410. *
  411. * When distributing meta data blocks outside segment structure,
  412. * We must count them as the overhead.
  413. */
  414. overhead = 0;
  415. err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
  416. if (unlikely(err))
  417. return err;
  418. buf->f_type = NILFS_SUPER_MAGIC;
  419. buf->f_bsize = sb->s_blocksize;
  420. buf->f_blocks = blocks - overhead;
  421. buf->f_bfree = nfreeblocks;
  422. buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
  423. (buf->f_bfree - nrsvblocks) : 0;
  424. buf->f_files = atomic_read(&root->inodes_count);
  425. buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */
  426. buf->f_namelen = NILFS_NAME_LEN;
  427. buf->f_fsid.val[0] = (u32)id;
  428. buf->f_fsid.val[1] = (u32)(id >> 32);
  429. return 0;
  430. }
  431. static int nilfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
  432. {
  433. struct super_block *sb = vfs->mnt_sb;
  434. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  435. struct nilfs_root *root = NILFS_I(vfs->mnt_root->d_inode)->i_root;
  436. if (!nilfs_test_opt(sbi, BARRIER))
  437. seq_puts(seq, ",nobarrier");
  438. if (root->cno != NILFS_CPTREE_CURRENT_CNO)
  439. seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
  440. if (nilfs_test_opt(sbi, ERRORS_PANIC))
  441. seq_puts(seq, ",errors=panic");
  442. if (nilfs_test_opt(sbi, ERRORS_CONT))
  443. seq_puts(seq, ",errors=continue");
  444. if (nilfs_test_opt(sbi, STRICT_ORDER))
  445. seq_puts(seq, ",order=strict");
  446. if (nilfs_test_opt(sbi, NORECOVERY))
  447. seq_puts(seq, ",norecovery");
  448. if (nilfs_test_opt(sbi, DISCARD))
  449. seq_puts(seq, ",discard");
  450. return 0;
  451. }
  452. static const struct super_operations nilfs_sops = {
  453. .alloc_inode = nilfs_alloc_inode,
  454. .destroy_inode = nilfs_destroy_inode,
  455. .dirty_inode = nilfs_dirty_inode,
  456. /* .write_inode = nilfs_write_inode, */
  457. /* .put_inode = nilfs_put_inode, */
  458. /* .drop_inode = nilfs_drop_inode, */
  459. .evict_inode = nilfs_evict_inode,
  460. .put_super = nilfs_put_super,
  461. /* .write_super = nilfs_write_super, */
  462. .sync_fs = nilfs_sync_fs,
  463. /* .write_super_lockfs */
  464. /* .unlockfs */
  465. .statfs = nilfs_statfs,
  466. .remount_fs = nilfs_remount,
  467. /* .umount_begin */
  468. .show_options = nilfs_show_options
  469. };
  470. enum {
  471. Opt_err_cont, Opt_err_panic, Opt_err_ro,
  472. Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
  473. Opt_discard, Opt_nodiscard, Opt_err,
  474. };
  475. static match_table_t tokens = {
  476. {Opt_err_cont, "errors=continue"},
  477. {Opt_err_panic, "errors=panic"},
  478. {Opt_err_ro, "errors=remount-ro"},
  479. {Opt_barrier, "barrier"},
  480. {Opt_nobarrier, "nobarrier"},
  481. {Opt_snapshot, "cp=%u"},
  482. {Opt_order, "order=%s"},
  483. {Opt_norecovery, "norecovery"},
  484. {Opt_discard, "discard"},
  485. {Opt_nodiscard, "nodiscard"},
  486. {Opt_err, NULL}
  487. };
  488. static int parse_options(char *options, struct super_block *sb, int is_remount)
  489. {
  490. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  491. char *p;
  492. substring_t args[MAX_OPT_ARGS];
  493. int option;
  494. if (!options)
  495. return 1;
  496. while ((p = strsep(&options, ",")) != NULL) {
  497. int token;
  498. if (!*p)
  499. continue;
  500. token = match_token(p, tokens, args);
  501. switch (token) {
  502. case Opt_barrier:
  503. nilfs_set_opt(sbi, BARRIER);
  504. break;
  505. case Opt_nobarrier:
  506. nilfs_clear_opt(sbi, BARRIER);
  507. break;
  508. case Opt_order:
  509. if (strcmp(args[0].from, "relaxed") == 0)
  510. /* Ordered data semantics */
  511. nilfs_clear_opt(sbi, STRICT_ORDER);
  512. else if (strcmp(args[0].from, "strict") == 0)
  513. /* Strict in-order semantics */
  514. nilfs_set_opt(sbi, STRICT_ORDER);
  515. else
  516. return 0;
  517. break;
  518. case Opt_err_panic:
  519. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC);
  520. break;
  521. case Opt_err_ro:
  522. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO);
  523. break;
  524. case Opt_err_cont:
  525. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT);
  526. break;
  527. case Opt_snapshot:
  528. if (match_int(&args[0], &option) || option <= 0)
  529. return 0;
  530. if (is_remount) {
  531. printk(KERN_ERR
  532. "NILFS: \"%s\" option is invalid "
  533. "for remount.\n", p);
  534. return 0;
  535. }
  536. break;
  537. case Opt_norecovery:
  538. nilfs_set_opt(sbi, NORECOVERY);
  539. break;
  540. case Opt_discard:
  541. nilfs_set_opt(sbi, DISCARD);
  542. break;
  543. case Opt_nodiscard:
  544. nilfs_clear_opt(sbi, DISCARD);
  545. break;
  546. default:
  547. printk(KERN_ERR
  548. "NILFS: Unrecognized mount option \"%s\"\n", p);
  549. return 0;
  550. }
  551. }
  552. return 1;
  553. }
  554. static inline void
  555. nilfs_set_default_options(struct nilfs_sb_info *sbi,
  556. struct nilfs_super_block *sbp)
  557. {
  558. sbi->s_mount_opt =
  559. NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
  560. }
  561. static int nilfs_setup_super(struct nilfs_sb_info *sbi)
  562. {
  563. struct the_nilfs *nilfs = sbi->s_nilfs;
  564. struct nilfs_super_block **sbp;
  565. int max_mnt_count;
  566. int mnt_count;
  567. /* nilfs->ns_sem must be locked by the caller. */
  568. sbp = nilfs_prepare_super(sbi, 0);
  569. if (!sbp)
  570. return -EIO;
  571. max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
  572. mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
  573. if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
  574. printk(KERN_WARNING
  575. "NILFS warning: mounting fs with errors\n");
  576. #if 0
  577. } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
  578. printk(KERN_WARNING
  579. "NILFS warning: maximal mount count reached\n");
  580. #endif
  581. }
  582. if (!max_mnt_count)
  583. sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
  584. sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
  585. sbp[0]->s_state =
  586. cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
  587. sbp[0]->s_mtime = cpu_to_le64(get_seconds());
  588. /* synchronize sbp[1] with sbp[0] */
  589. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  590. return nilfs_commit_super(sbi, NILFS_SB_COMMIT_ALL);
  591. }
  592. struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
  593. u64 pos, int blocksize,
  594. struct buffer_head **pbh)
  595. {
  596. unsigned long long sb_index = pos;
  597. unsigned long offset;
  598. offset = do_div(sb_index, blocksize);
  599. *pbh = sb_bread(sb, sb_index);
  600. if (!*pbh)
  601. return NULL;
  602. return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
  603. }
  604. int nilfs_store_magic_and_option(struct super_block *sb,
  605. struct nilfs_super_block *sbp,
  606. char *data)
  607. {
  608. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  609. sb->s_magic = le16_to_cpu(sbp->s_magic);
  610. /* FS independent flags */
  611. #ifdef NILFS_ATIME_DISABLE
  612. sb->s_flags |= MS_NOATIME;
  613. #endif
  614. nilfs_set_default_options(sbi, sbp);
  615. sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid);
  616. sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid);
  617. sbi->s_interval = le32_to_cpu(sbp->s_c_interval);
  618. sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max);
  619. return !parse_options(data, sb, 0) ? -EINVAL : 0 ;
  620. }
  621. int nilfs_check_feature_compatibility(struct super_block *sb,
  622. struct nilfs_super_block *sbp)
  623. {
  624. __u64 features;
  625. features = le64_to_cpu(sbp->s_feature_incompat) &
  626. ~NILFS_FEATURE_INCOMPAT_SUPP;
  627. if (features) {
  628. printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
  629. "optional features (%llx)\n",
  630. (unsigned long long)features);
  631. return -EINVAL;
  632. }
  633. features = le64_to_cpu(sbp->s_feature_compat_ro) &
  634. ~NILFS_FEATURE_COMPAT_RO_SUPP;
  635. if (!(sb->s_flags & MS_RDONLY) && features) {
  636. printk(KERN_ERR "NILFS: couldn't mount RDWR because of "
  637. "unsupported optional features (%llx)\n",
  638. (unsigned long long)features);
  639. return -EINVAL;
  640. }
  641. return 0;
  642. }
  643. static int nilfs_get_root_dentry(struct super_block *sb,
  644. struct nilfs_root *root,
  645. struct dentry **root_dentry)
  646. {
  647. struct inode *inode;
  648. struct dentry *dentry;
  649. int ret = 0;
  650. inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
  651. if (IS_ERR(inode)) {
  652. printk(KERN_ERR "NILFS: get root inode failed\n");
  653. ret = PTR_ERR(inode);
  654. goto out;
  655. }
  656. if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
  657. iput(inode);
  658. printk(KERN_ERR "NILFS: corrupt root inode.\n");
  659. ret = -EINVAL;
  660. goto out;
  661. }
  662. if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
  663. dentry = d_find_alias(inode);
  664. if (!dentry) {
  665. dentry = d_alloc_root(inode);
  666. if (!dentry) {
  667. iput(inode);
  668. ret = -ENOMEM;
  669. goto failed_dentry;
  670. }
  671. } else {
  672. iput(inode);
  673. }
  674. } else {
  675. dentry = d_obtain_alias(inode);
  676. if (IS_ERR(dentry)) {
  677. ret = PTR_ERR(dentry);
  678. goto failed_dentry;
  679. }
  680. }
  681. *root_dentry = dentry;
  682. out:
  683. return ret;
  684. failed_dentry:
  685. printk(KERN_ERR "NILFS: get root dentry failed\n");
  686. goto out;
  687. }
  688. static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
  689. struct dentry **root_dentry)
  690. {
  691. struct the_nilfs *nilfs = NILFS_SB(s)->s_nilfs;
  692. struct nilfs_root *root;
  693. int ret;
  694. down_read(&nilfs->ns_segctor_sem);
  695. ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
  696. up_read(&nilfs->ns_segctor_sem);
  697. if (ret < 0) {
  698. ret = (ret == -ENOENT) ? -EINVAL : ret;
  699. goto out;
  700. } else if (!ret) {
  701. printk(KERN_ERR "NILFS: The specified checkpoint is "
  702. "not a snapshot (checkpoint number=%llu).\n",
  703. (unsigned long long)cno);
  704. ret = -EINVAL;
  705. goto out;
  706. }
  707. ret = nilfs_attach_checkpoint(NILFS_SB(s), cno, false, &root);
  708. if (ret) {
  709. printk(KERN_ERR "NILFS: error loading snapshot "
  710. "(checkpoint number=%llu).\n",
  711. (unsigned long long)cno);
  712. goto out;
  713. }
  714. ret = nilfs_get_root_dentry(s, root, root_dentry);
  715. nilfs_put_root(root);
  716. out:
  717. return ret;
  718. }
  719. static int nilfs_tree_was_touched(struct dentry *root_dentry)
  720. {
  721. return atomic_read(&root_dentry->d_count) > 1;
  722. }
  723. /**
  724. * nilfs_try_to_shrink_tree() - try to shrink dentries of a checkpoint
  725. * @root_dentry: root dentry of the tree to be shrunk
  726. *
  727. * This function returns true if the tree was in-use.
  728. */
  729. static int nilfs_try_to_shrink_tree(struct dentry *root_dentry)
  730. {
  731. if (have_submounts(root_dentry))
  732. return true;
  733. shrink_dcache_parent(root_dentry);
  734. return nilfs_tree_was_touched(root_dentry);
  735. }
  736. /**
  737. * nilfs_fill_super() - initialize a super block instance
  738. * @sb: super_block
  739. * @data: mount options
  740. * @silent: silent mode flag
  741. *
  742. * This function is called exclusively by nilfs->ns_mount_mutex.
  743. * So, the recovery process is protected from other simultaneous mounts.
  744. */
  745. static int
  746. nilfs_fill_super(struct super_block *sb, void *data, int silent)
  747. {
  748. struct the_nilfs *nilfs;
  749. struct nilfs_sb_info *sbi;
  750. struct nilfs_root *fsroot;
  751. __u64 cno;
  752. int err;
  753. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  754. if (!sbi)
  755. return -ENOMEM;
  756. sb->s_fs_info = sbi;
  757. sbi->s_super = sb;
  758. nilfs = alloc_nilfs(sb->s_bdev);
  759. if (!nilfs) {
  760. err = -ENOMEM;
  761. goto failed_sbi;
  762. }
  763. sbi->s_nilfs = nilfs;
  764. err = init_nilfs(nilfs, sbi, (char *)data);
  765. if (err)
  766. goto failed_nilfs;
  767. spin_lock_init(&sbi->s_inode_lock);
  768. INIT_LIST_HEAD(&sbi->s_dirty_files);
  769. /*
  770. * Following initialization is overlapped because
  771. * nilfs_sb_info structure has been cleared at the beginning.
  772. * But we reserve them to keep our interest and make ready
  773. * for the future change.
  774. */
  775. get_random_bytes(&sbi->s_next_generation,
  776. sizeof(sbi->s_next_generation));
  777. spin_lock_init(&sbi->s_next_gen_lock);
  778. sb->s_op = &nilfs_sops;
  779. sb->s_export_op = &nilfs_export_ops;
  780. sb->s_root = NULL;
  781. sb->s_time_gran = 1;
  782. sb->s_bdi = nilfs->ns_bdi;
  783. err = load_nilfs(nilfs, sbi);
  784. if (err)
  785. goto failed_nilfs;
  786. cno = nilfs_last_cno(nilfs);
  787. err = nilfs_attach_checkpoint(sbi, cno, true, &fsroot);
  788. if (err) {
  789. printk(KERN_ERR "NILFS: error loading last checkpoint "
  790. "(checkpoint number=%llu).\n", (unsigned long long)cno);
  791. goto failed_nilfs;
  792. }
  793. if (!(sb->s_flags & MS_RDONLY)) {
  794. err = nilfs_attach_segment_constructor(sbi, fsroot);
  795. if (err)
  796. goto failed_checkpoint;
  797. }
  798. err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
  799. if (err)
  800. goto failed_segctor;
  801. nilfs_put_root(fsroot);
  802. if (!(sb->s_flags & MS_RDONLY)) {
  803. down_write(&nilfs->ns_sem);
  804. nilfs_setup_super(sbi);
  805. up_write(&nilfs->ns_sem);
  806. }
  807. return 0;
  808. failed_segctor:
  809. nilfs_detach_segment_constructor(sbi);
  810. failed_checkpoint:
  811. nilfs_put_root(fsroot);
  812. failed_nilfs:
  813. destroy_nilfs(nilfs);
  814. failed_sbi:
  815. sb->s_fs_info = NULL;
  816. kfree(sbi);
  817. return err;
  818. }
  819. static int nilfs_remount(struct super_block *sb, int *flags, char *data)
  820. {
  821. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  822. struct the_nilfs *nilfs = sbi->s_nilfs;
  823. unsigned long old_sb_flags;
  824. struct nilfs_mount_options old_opts;
  825. int err;
  826. old_sb_flags = sb->s_flags;
  827. old_opts.mount_opt = sbi->s_mount_opt;
  828. if (!parse_options(data, sb, 1)) {
  829. err = -EINVAL;
  830. goto restore_opts;
  831. }
  832. sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
  833. err = -EINVAL;
  834. if (!nilfs_valid_fs(nilfs)) {
  835. printk(KERN_WARNING "NILFS (device %s): couldn't "
  836. "remount because the filesystem is in an "
  837. "incomplete recovery state.\n", sb->s_id);
  838. goto restore_opts;
  839. }
  840. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  841. goto out;
  842. if (*flags & MS_RDONLY) {
  843. /* Shutting down the segment constructor */
  844. nilfs_detach_segment_constructor(sbi);
  845. sb->s_flags |= MS_RDONLY;
  846. /*
  847. * Remounting a valid RW partition RDONLY, so set
  848. * the RDONLY flag and then mark the partition as valid again.
  849. */
  850. down_write(&nilfs->ns_sem);
  851. nilfs_cleanup_super(sbi);
  852. up_write(&nilfs->ns_sem);
  853. } else {
  854. __u64 features;
  855. struct nilfs_root *root;
  856. /*
  857. * Mounting a RDONLY partition read-write, so reread and
  858. * store the current valid flag. (It may have been changed
  859. * by fsck since we originally mounted the partition.)
  860. */
  861. down_read(&nilfs->ns_sem);
  862. features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
  863. ~NILFS_FEATURE_COMPAT_RO_SUPP;
  864. up_read(&nilfs->ns_sem);
  865. if (features) {
  866. printk(KERN_WARNING "NILFS (device %s): couldn't "
  867. "remount RDWR because of unsupported optional "
  868. "features (%llx)\n",
  869. sb->s_id, (unsigned long long)features);
  870. err = -EROFS;
  871. goto restore_opts;
  872. }
  873. sb->s_flags &= ~MS_RDONLY;
  874. root = NILFS_I(sb->s_root->d_inode)->i_root;
  875. err = nilfs_attach_segment_constructor(sbi, root);
  876. if (err)
  877. goto restore_opts;
  878. down_write(&nilfs->ns_sem);
  879. nilfs_setup_super(sbi);
  880. up_write(&nilfs->ns_sem);
  881. }
  882. out:
  883. return 0;
  884. restore_opts:
  885. sb->s_flags = old_sb_flags;
  886. sbi->s_mount_opt = old_opts.mount_opt;
  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. s->s_bdev = data;
  937. s->s_dev = s->s_bdev->bd_dev;
  938. return 0;
  939. }
  940. static int nilfs_test_bdev_super(struct super_block *s, void *data)
  941. {
  942. return (void *)s->s_bdev == data;
  943. }
  944. static int
  945. nilfs_get_sb(struct file_system_type *fs_type, int flags,
  946. const char *dev_name, void *data, struct vfsmount *mnt)
  947. {
  948. struct nilfs_super_data sd;
  949. struct super_block *s;
  950. fmode_t mode = FMODE_READ;
  951. struct dentry *root_dentry;
  952. int err, s_new = false;
  953. if (!(flags & MS_RDONLY))
  954. mode |= FMODE_WRITE;
  955. sd.bdev = open_bdev_exclusive(dev_name, mode, fs_type);
  956. if (IS_ERR(sd.bdev))
  957. return PTR_ERR(sd.bdev);
  958. sd.cno = 0;
  959. sd.flags = flags;
  960. if (nilfs_identify((char *)data, &sd)) {
  961. err = -EINVAL;
  962. goto failed;
  963. }
  964. s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, sd.bdev);
  965. if (IS_ERR(s)) {
  966. err = PTR_ERR(s);
  967. goto failed;
  968. }
  969. if (!s->s_root) {
  970. char b[BDEVNAME_SIZE];
  971. s_new = true;
  972. /* New superblock instance created */
  973. s->s_flags = flags;
  974. s->s_mode = mode;
  975. strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id));
  976. sb_set_blocksize(s, block_size(sd.bdev));
  977. err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  978. if (err)
  979. goto failed_super;
  980. s->s_flags |= MS_ACTIVE;
  981. } else if (!sd.cno) {
  982. int busy = false;
  983. if (nilfs_tree_was_touched(s->s_root)) {
  984. busy = nilfs_try_to_shrink_tree(s->s_root);
  985. if (busy && (flags ^ s->s_flags) & MS_RDONLY) {
  986. printk(KERN_ERR "NILFS: the device already "
  987. "has a %s mount.\n",
  988. (s->s_flags & MS_RDONLY) ?
  989. "read-only" : "read/write");
  990. err = -EBUSY;
  991. goto failed_super;
  992. }
  993. }
  994. if (!busy) {
  995. /*
  996. * Try remount to setup mount states if the current
  997. * tree is not mounted and only snapshots use this sb.
  998. */
  999. err = nilfs_remount(s, &flags, data);
  1000. if (err)
  1001. goto failed_super;
  1002. }
  1003. }
  1004. if (sd.cno) {
  1005. err = nilfs_attach_snapshot(s, sd.cno, &root_dentry);
  1006. if (err)
  1007. goto failed_super;
  1008. } else {
  1009. root_dentry = dget(s->s_root);
  1010. }
  1011. if (!s_new)
  1012. close_bdev_exclusive(sd.bdev, mode);
  1013. mnt->mnt_sb = s;
  1014. mnt->mnt_root = root_dentry;
  1015. return 0;
  1016. failed_super:
  1017. deactivate_locked_super(s);
  1018. failed:
  1019. if (!s_new)
  1020. close_bdev_exclusive(sd.bdev, mode);
  1021. return err;
  1022. }
  1023. struct file_system_type nilfs_fs_type = {
  1024. .owner = THIS_MODULE,
  1025. .name = "nilfs2",
  1026. .get_sb = nilfs_get_sb,
  1027. .kill_sb = kill_block_super,
  1028. .fs_flags = FS_REQUIRES_DEV,
  1029. };
  1030. static void nilfs_inode_init_once(void *obj)
  1031. {
  1032. struct nilfs_inode_info *ii = obj;
  1033. INIT_LIST_HEAD(&ii->i_dirty);
  1034. #ifdef CONFIG_NILFS_XATTR
  1035. init_rwsem(&ii->xattr_sem);
  1036. #endif
  1037. nilfs_btnode_cache_init_once(&ii->i_btnode_cache);
  1038. ii->i_bmap = &ii->i_bmap_data;
  1039. inode_init_once(&ii->vfs_inode);
  1040. }
  1041. static void nilfs_segbuf_init_once(void *obj)
  1042. {
  1043. memset(obj, 0, sizeof(struct nilfs_segment_buffer));
  1044. }
  1045. static void nilfs_destroy_cachep(void)
  1046. {
  1047. if (nilfs_inode_cachep)
  1048. kmem_cache_destroy(nilfs_inode_cachep);
  1049. if (nilfs_transaction_cachep)
  1050. kmem_cache_destroy(nilfs_transaction_cachep);
  1051. if (nilfs_segbuf_cachep)
  1052. kmem_cache_destroy(nilfs_segbuf_cachep);
  1053. if (nilfs_btree_path_cache)
  1054. kmem_cache_destroy(nilfs_btree_path_cache);
  1055. }
  1056. static int __init nilfs_init_cachep(void)
  1057. {
  1058. nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
  1059. sizeof(struct nilfs_inode_info), 0,
  1060. SLAB_RECLAIM_ACCOUNT, nilfs_inode_init_once);
  1061. if (!nilfs_inode_cachep)
  1062. goto fail;
  1063. nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
  1064. sizeof(struct nilfs_transaction_info), 0,
  1065. SLAB_RECLAIM_ACCOUNT, NULL);
  1066. if (!nilfs_transaction_cachep)
  1067. goto fail;
  1068. nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
  1069. sizeof(struct nilfs_segment_buffer), 0,
  1070. SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
  1071. if (!nilfs_segbuf_cachep)
  1072. goto fail;
  1073. nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
  1074. sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
  1075. 0, 0, NULL);
  1076. if (!nilfs_btree_path_cache)
  1077. goto fail;
  1078. return 0;
  1079. fail:
  1080. nilfs_destroy_cachep();
  1081. return -ENOMEM;
  1082. }
  1083. static int __init init_nilfs_fs(void)
  1084. {
  1085. int err;
  1086. err = nilfs_init_cachep();
  1087. if (err)
  1088. goto fail;
  1089. err = register_filesystem(&nilfs_fs_type);
  1090. if (err)
  1091. goto free_cachep;
  1092. printk(KERN_INFO "NILFS version 2 loaded\n");
  1093. return 0;
  1094. free_cachep:
  1095. nilfs_destroy_cachep();
  1096. fail:
  1097. return err;
  1098. }
  1099. static void __exit exit_nilfs_fs(void)
  1100. {
  1101. nilfs_destroy_cachep();
  1102. unregister_filesystem(&nilfs_fs_type);
  1103. }
  1104. module_init(init_nilfs_fs)
  1105. module_exit(exit_nilfs_fs)