super.c 33 KB

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