super.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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/exportfs.h>
  51. #include <linux/seq_file.h>
  52. #include <linux/mount.h>
  53. #include "nilfs.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->vfs_inode.i_version = 1;
  143. nilfs_btnode_cache_init(&ii->i_btnode_cache, nilfs->ns_bdi);
  144. return &ii->vfs_inode;
  145. }
  146. struct inode *nilfs_alloc_inode(struct super_block *sb)
  147. {
  148. return nilfs_alloc_inode_common(NILFS_SB(sb)->s_nilfs);
  149. }
  150. void nilfs_destroy_inode(struct inode *inode)
  151. {
  152. kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
  153. }
  154. static int nilfs_sync_super(struct nilfs_sb_info *sbi, int flag)
  155. {
  156. struct the_nilfs *nilfs = sbi->s_nilfs;
  157. int err;
  158. retry:
  159. set_buffer_dirty(nilfs->ns_sbh[0]);
  160. if (nilfs_test_opt(sbi, BARRIER)) {
  161. err = __sync_dirty_buffer(nilfs->ns_sbh[0],
  162. WRITE_SYNC | WRITE_FLUSH_FUA);
  163. } else {
  164. err = sync_dirty_buffer(nilfs->ns_sbh[0]);
  165. }
  166. if (unlikely(err)) {
  167. printk(KERN_ERR
  168. "NILFS: unable to write superblock (err=%d)\n", err);
  169. if (err == -EIO && nilfs->ns_sbh[1]) {
  170. /*
  171. * sbp[0] points to newer log than sbp[1],
  172. * so copy sbp[0] to sbp[1] to take over sbp[0].
  173. */
  174. memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0],
  175. nilfs->ns_sbsize);
  176. nilfs_fall_back_super_block(nilfs);
  177. goto retry;
  178. }
  179. } else {
  180. struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
  181. nilfs->ns_sbwcount++;
  182. /*
  183. * The latest segment becomes trailable from the position
  184. * written in superblock.
  185. */
  186. clear_nilfs_discontinued(nilfs);
  187. /* update GC protection for recent segments */
  188. if (nilfs->ns_sbh[1]) {
  189. if (flag == NILFS_SB_COMMIT_ALL) {
  190. set_buffer_dirty(nilfs->ns_sbh[1]);
  191. if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
  192. goto out;
  193. }
  194. if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) <
  195. le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno))
  196. sbp = nilfs->ns_sbp[1];
  197. }
  198. spin_lock(&nilfs->ns_last_segment_lock);
  199. nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
  200. spin_unlock(&nilfs->ns_last_segment_lock);
  201. }
  202. out:
  203. return err;
  204. }
  205. void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
  206. struct the_nilfs *nilfs)
  207. {
  208. sector_t nfreeblocks;
  209. /* nilfs->ns_sem must be locked by the caller. */
  210. nilfs_count_free_blocks(nilfs, &nfreeblocks);
  211. sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
  212. spin_lock(&nilfs->ns_last_segment_lock);
  213. sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
  214. sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
  215. sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
  216. spin_unlock(&nilfs->ns_last_segment_lock);
  217. }
  218. struct nilfs_super_block **nilfs_prepare_super(struct nilfs_sb_info *sbi,
  219. int flip)
  220. {
  221. struct the_nilfs *nilfs = sbi->s_nilfs;
  222. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  223. /* nilfs->ns_sem must be locked by the caller. */
  224. if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
  225. if (sbp[1] &&
  226. sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
  227. memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
  228. } else {
  229. printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
  230. sbi->s_super->s_id);
  231. return NULL;
  232. }
  233. } else if (sbp[1] &&
  234. sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
  235. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  236. }
  237. if (flip && sbp[1])
  238. nilfs_swap_super_block(nilfs);
  239. return sbp;
  240. }
  241. int nilfs_commit_super(struct nilfs_sb_info *sbi, int flag)
  242. {
  243. struct the_nilfs *nilfs = sbi->s_nilfs;
  244. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  245. time_t t;
  246. /* nilfs->ns_sem must be locked by the caller. */
  247. t = get_seconds();
  248. nilfs->ns_sbwtime = t;
  249. sbp[0]->s_wtime = cpu_to_le64(t);
  250. sbp[0]->s_sum = 0;
  251. sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
  252. (unsigned char *)sbp[0],
  253. nilfs->ns_sbsize));
  254. if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
  255. sbp[1]->s_wtime = sbp[0]->s_wtime;
  256. sbp[1]->s_sum = 0;
  257. sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
  258. (unsigned char *)sbp[1],
  259. nilfs->ns_sbsize));
  260. }
  261. clear_nilfs_sb_dirty(nilfs);
  262. return nilfs_sync_super(sbi, flag);
  263. }
  264. /**
  265. * nilfs_cleanup_super() - write filesystem state for cleanup
  266. * @sbi: nilfs_sb_info to be unmounted or degraded to read-only
  267. *
  268. * This function restores state flags in the on-disk super block.
  269. * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
  270. * filesystem was not clean previously.
  271. */
  272. int nilfs_cleanup_super(struct nilfs_sb_info *sbi)
  273. {
  274. struct nilfs_super_block **sbp;
  275. int flag = NILFS_SB_COMMIT;
  276. int ret = -EIO;
  277. sbp = nilfs_prepare_super(sbi, 0);
  278. if (sbp) {
  279. sbp[0]->s_state = cpu_to_le16(sbi->s_nilfs->ns_mount_state);
  280. nilfs_set_log_cursor(sbp[0], sbi->s_nilfs);
  281. if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
  282. /*
  283. * make the "clean" flag also to the opposite
  284. * super block if both super blocks point to
  285. * the same checkpoint.
  286. */
  287. sbp[1]->s_state = sbp[0]->s_state;
  288. flag = NILFS_SB_COMMIT_ALL;
  289. }
  290. ret = nilfs_commit_super(sbi, flag);
  291. }
  292. return ret;
  293. }
  294. static void nilfs_put_super(struct super_block *sb)
  295. {
  296. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  297. struct the_nilfs *nilfs = sbi->s_nilfs;
  298. nilfs_detach_segment_constructor(sbi);
  299. if (!(sb->s_flags & MS_RDONLY)) {
  300. down_write(&nilfs->ns_sem);
  301. nilfs_cleanup_super(sbi);
  302. up_write(&nilfs->ns_sem);
  303. }
  304. down_write(&nilfs->ns_super_sem);
  305. if (nilfs->ns_current == sbi)
  306. nilfs->ns_current = NULL;
  307. up_write(&nilfs->ns_super_sem);
  308. nilfs_detach_checkpoint(sbi);
  309. put_nilfs(sbi->s_nilfs);
  310. sbi->s_super = NULL;
  311. sb->s_fs_info = NULL;
  312. nilfs_put_sbinfo(sbi);
  313. }
  314. static int nilfs_sync_fs(struct super_block *sb, int wait)
  315. {
  316. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  317. struct the_nilfs *nilfs = sbi->s_nilfs;
  318. struct nilfs_super_block **sbp;
  319. int err = 0;
  320. /* This function is called when super block should be written back */
  321. if (wait)
  322. err = nilfs_construct_segment(sb);
  323. down_write(&nilfs->ns_sem);
  324. if (nilfs_sb_dirty(nilfs)) {
  325. sbp = nilfs_prepare_super(sbi, nilfs_sb_will_flip(nilfs));
  326. if (likely(sbp)) {
  327. nilfs_set_log_cursor(sbp[0], nilfs);
  328. nilfs_commit_super(sbi, NILFS_SB_COMMIT);
  329. }
  330. }
  331. up_write(&nilfs->ns_sem);
  332. return err;
  333. }
  334. int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno)
  335. {
  336. struct the_nilfs *nilfs = sbi->s_nilfs;
  337. struct nilfs_checkpoint *raw_cp;
  338. struct buffer_head *bh_cp;
  339. int err;
  340. down_write(&nilfs->ns_super_sem);
  341. list_add(&sbi->s_list, &nilfs->ns_supers);
  342. up_write(&nilfs->ns_super_sem);
  343. err = -ENOMEM;
  344. sbi->s_ifile = nilfs_ifile_new(sbi, nilfs->ns_inode_size);
  345. if (!sbi->s_ifile)
  346. goto delist;
  347. down_read(&nilfs->ns_segctor_sem);
  348. err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
  349. &bh_cp);
  350. up_read(&nilfs->ns_segctor_sem);
  351. if (unlikely(err)) {
  352. if (err == -ENOENT || err == -EINVAL) {
  353. printk(KERN_ERR
  354. "NILFS: Invalid checkpoint "
  355. "(checkpoint number=%llu)\n",
  356. (unsigned long long)cno);
  357. err = -EINVAL;
  358. }
  359. goto failed;
  360. }
  361. err = nilfs_read_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode);
  362. if (unlikely(err))
  363. goto failed_bh;
  364. atomic_set(&sbi->s_inodes_count, le64_to_cpu(raw_cp->cp_inodes_count));
  365. atomic_set(&sbi->s_blocks_count, le64_to_cpu(raw_cp->cp_blocks_count));
  366. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  367. return 0;
  368. failed_bh:
  369. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  370. failed:
  371. nilfs_mdt_destroy(sbi->s_ifile);
  372. sbi->s_ifile = NULL;
  373. delist:
  374. down_write(&nilfs->ns_super_sem);
  375. list_del_init(&sbi->s_list);
  376. up_write(&nilfs->ns_super_sem);
  377. return err;
  378. }
  379. void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi)
  380. {
  381. struct the_nilfs *nilfs = sbi->s_nilfs;
  382. nilfs_mdt_destroy(sbi->s_ifile);
  383. sbi->s_ifile = NULL;
  384. down_write(&nilfs->ns_super_sem);
  385. list_del_init(&sbi->s_list);
  386. up_write(&nilfs->ns_super_sem);
  387. }
  388. static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  389. {
  390. struct super_block *sb = dentry->d_sb;
  391. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  392. struct the_nilfs *nilfs = sbi->s_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(&sbi->s_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. if (!nilfs_test_opt(sbi, BARRIER))
  436. seq_puts(seq, ",nobarrier");
  437. if (nilfs_test_opt(sbi, SNAPSHOT))
  438. seq_printf(seq, ",cp=%llu",
  439. (unsigned long long int)sbi->s_snapshot_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. static struct inode *
  471. nilfs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation)
  472. {
  473. struct inode *inode;
  474. if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO &&
  475. ino != NILFS_SKETCH_INO)
  476. return ERR_PTR(-ESTALE);
  477. inode = nilfs_iget(sb, ino);
  478. if (IS_ERR(inode))
  479. return ERR_CAST(inode);
  480. if (generation && inode->i_generation != generation) {
  481. iput(inode);
  482. return ERR_PTR(-ESTALE);
  483. }
  484. return inode;
  485. }
  486. static struct dentry *
  487. nilfs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
  488. int fh_type)
  489. {
  490. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  491. nilfs_nfs_get_inode);
  492. }
  493. static struct dentry *
  494. nilfs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len,
  495. int fh_type)
  496. {
  497. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  498. nilfs_nfs_get_inode);
  499. }
  500. static const struct export_operations nilfs_export_ops = {
  501. .fh_to_dentry = nilfs_fh_to_dentry,
  502. .fh_to_parent = nilfs_fh_to_parent,
  503. .get_parent = nilfs_get_parent,
  504. };
  505. enum {
  506. Opt_err_cont, Opt_err_panic, Opt_err_ro,
  507. Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
  508. Opt_discard, Opt_nodiscard, Opt_err,
  509. };
  510. static match_table_t tokens = {
  511. {Opt_err_cont, "errors=continue"},
  512. {Opt_err_panic, "errors=panic"},
  513. {Opt_err_ro, "errors=remount-ro"},
  514. {Opt_barrier, "barrier"},
  515. {Opt_nobarrier, "nobarrier"},
  516. {Opt_snapshot, "cp=%u"},
  517. {Opt_order, "order=%s"},
  518. {Opt_norecovery, "norecovery"},
  519. {Opt_discard, "discard"},
  520. {Opt_nodiscard, "nodiscard"},
  521. {Opt_err, NULL}
  522. };
  523. static int parse_options(char *options, struct super_block *sb, int is_remount)
  524. {
  525. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  526. char *p;
  527. substring_t args[MAX_OPT_ARGS];
  528. int option;
  529. if (!options)
  530. return 1;
  531. while ((p = strsep(&options, ",")) != NULL) {
  532. int token;
  533. if (!*p)
  534. continue;
  535. token = match_token(p, tokens, args);
  536. switch (token) {
  537. case Opt_barrier:
  538. nilfs_set_opt(sbi, BARRIER);
  539. break;
  540. case Opt_nobarrier:
  541. nilfs_clear_opt(sbi, BARRIER);
  542. break;
  543. case Opt_order:
  544. if (strcmp(args[0].from, "relaxed") == 0)
  545. /* Ordered data semantics */
  546. nilfs_clear_opt(sbi, STRICT_ORDER);
  547. else if (strcmp(args[0].from, "strict") == 0)
  548. /* Strict in-order semantics */
  549. nilfs_set_opt(sbi, STRICT_ORDER);
  550. else
  551. return 0;
  552. break;
  553. case Opt_err_panic:
  554. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC);
  555. break;
  556. case Opt_err_ro:
  557. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO);
  558. break;
  559. case Opt_err_cont:
  560. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT);
  561. break;
  562. case Opt_snapshot:
  563. if (match_int(&args[0], &option) || option <= 0)
  564. return 0;
  565. if (is_remount) {
  566. if (!nilfs_test_opt(sbi, SNAPSHOT)) {
  567. printk(KERN_ERR
  568. "NILFS: cannot change regular "
  569. "mount to snapshot.\n");
  570. return 0;
  571. } else if (option != sbi->s_snapshot_cno) {
  572. printk(KERN_ERR
  573. "NILFS: cannot remount to a "
  574. "different snapshot.\n");
  575. return 0;
  576. }
  577. break;
  578. }
  579. if (!(sb->s_flags & MS_RDONLY)) {
  580. printk(KERN_ERR "NILFS: cannot mount snapshot "
  581. "read/write. A read-only option is "
  582. "required.\n");
  583. return 0;
  584. }
  585. sbi->s_snapshot_cno = option;
  586. nilfs_set_opt(sbi, SNAPSHOT);
  587. break;
  588. case Opt_norecovery:
  589. nilfs_set_opt(sbi, NORECOVERY);
  590. break;
  591. case Opt_discard:
  592. nilfs_set_opt(sbi, DISCARD);
  593. break;
  594. case Opt_nodiscard:
  595. nilfs_clear_opt(sbi, DISCARD);
  596. break;
  597. default:
  598. printk(KERN_ERR
  599. "NILFS: Unrecognized mount option \"%s\"\n", p);
  600. return 0;
  601. }
  602. }
  603. return 1;
  604. }
  605. static inline void
  606. nilfs_set_default_options(struct nilfs_sb_info *sbi,
  607. struct nilfs_super_block *sbp)
  608. {
  609. sbi->s_mount_opt =
  610. NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
  611. }
  612. static int nilfs_setup_super(struct nilfs_sb_info *sbi)
  613. {
  614. struct the_nilfs *nilfs = sbi->s_nilfs;
  615. struct nilfs_super_block **sbp;
  616. int max_mnt_count;
  617. int mnt_count;
  618. /* nilfs->ns_sem must be locked by the caller. */
  619. sbp = nilfs_prepare_super(sbi, 0);
  620. if (!sbp)
  621. return -EIO;
  622. max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
  623. mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
  624. if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
  625. printk(KERN_WARNING
  626. "NILFS warning: mounting fs with errors\n");
  627. #if 0
  628. } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
  629. printk(KERN_WARNING
  630. "NILFS warning: maximal mount count reached\n");
  631. #endif
  632. }
  633. if (!max_mnt_count)
  634. sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
  635. sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
  636. sbp[0]->s_state =
  637. cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
  638. sbp[0]->s_mtime = cpu_to_le64(get_seconds());
  639. /* synchronize sbp[1] with sbp[0] */
  640. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  641. return nilfs_commit_super(sbi, NILFS_SB_COMMIT_ALL);
  642. }
  643. struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
  644. u64 pos, int blocksize,
  645. struct buffer_head **pbh)
  646. {
  647. unsigned long long sb_index = pos;
  648. unsigned long offset;
  649. offset = do_div(sb_index, blocksize);
  650. *pbh = sb_bread(sb, sb_index);
  651. if (!*pbh)
  652. return NULL;
  653. return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
  654. }
  655. int nilfs_store_magic_and_option(struct super_block *sb,
  656. struct nilfs_super_block *sbp,
  657. char *data)
  658. {
  659. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  660. sb->s_magic = le16_to_cpu(sbp->s_magic);
  661. /* FS independent flags */
  662. #ifdef NILFS_ATIME_DISABLE
  663. sb->s_flags |= MS_NOATIME;
  664. #endif
  665. nilfs_set_default_options(sbi, sbp);
  666. sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid);
  667. sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid);
  668. sbi->s_interval = le32_to_cpu(sbp->s_c_interval);
  669. sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max);
  670. return !parse_options(data, sb, 0) ? -EINVAL : 0 ;
  671. }
  672. int nilfs_check_feature_compatibility(struct super_block *sb,
  673. struct nilfs_super_block *sbp)
  674. {
  675. __u64 features;
  676. features = le64_to_cpu(sbp->s_feature_incompat) &
  677. ~NILFS_FEATURE_INCOMPAT_SUPP;
  678. if (features) {
  679. printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
  680. "optional features (%llx)\n",
  681. (unsigned long long)features);
  682. return -EINVAL;
  683. }
  684. features = le64_to_cpu(sbp->s_feature_compat_ro) &
  685. ~NILFS_FEATURE_COMPAT_RO_SUPP;
  686. if (!(sb->s_flags & MS_RDONLY) && features) {
  687. printk(KERN_ERR "NILFS: couldn't mount RDWR because of "
  688. "unsupported optional features (%llx)\n",
  689. (unsigned long long)features);
  690. return -EINVAL;
  691. }
  692. return 0;
  693. }
  694. /**
  695. * nilfs_fill_super() - initialize a super block instance
  696. * @sb: super_block
  697. * @data: mount options
  698. * @silent: silent mode flag
  699. * @nilfs: the_nilfs struct
  700. *
  701. * This function is called exclusively by nilfs->ns_mount_mutex.
  702. * So, the recovery process is protected from other simultaneous mounts.
  703. */
  704. static int
  705. nilfs_fill_super(struct super_block *sb, void *data, int silent,
  706. struct the_nilfs *nilfs)
  707. {
  708. struct nilfs_sb_info *sbi;
  709. struct inode *root;
  710. __u64 cno;
  711. int err;
  712. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  713. if (!sbi)
  714. return -ENOMEM;
  715. sb->s_fs_info = sbi;
  716. get_nilfs(nilfs);
  717. sbi->s_nilfs = nilfs;
  718. sbi->s_super = sb;
  719. atomic_set(&sbi->s_count, 1);
  720. err = init_nilfs(nilfs, sbi, (char *)data);
  721. if (err)
  722. goto failed_sbi;
  723. spin_lock_init(&sbi->s_inode_lock);
  724. INIT_LIST_HEAD(&sbi->s_dirty_files);
  725. INIT_LIST_HEAD(&sbi->s_list);
  726. /*
  727. * Following initialization is overlapped because
  728. * nilfs_sb_info structure has been cleared at the beginning.
  729. * But we reserve them to keep our interest and make ready
  730. * for the future change.
  731. */
  732. get_random_bytes(&sbi->s_next_generation,
  733. sizeof(sbi->s_next_generation));
  734. spin_lock_init(&sbi->s_next_gen_lock);
  735. sb->s_op = &nilfs_sops;
  736. sb->s_export_op = &nilfs_export_ops;
  737. sb->s_root = NULL;
  738. sb->s_time_gran = 1;
  739. sb->s_bdi = nilfs->ns_bdi;
  740. err = load_nilfs(nilfs, sbi);
  741. if (err)
  742. goto failed_sbi;
  743. cno = nilfs_last_cno(nilfs);
  744. if (sb->s_flags & MS_RDONLY) {
  745. if (nilfs_test_opt(sbi, SNAPSHOT)) {
  746. down_read(&nilfs->ns_segctor_sem);
  747. err = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile,
  748. sbi->s_snapshot_cno);
  749. up_read(&nilfs->ns_segctor_sem);
  750. if (err < 0) {
  751. if (err == -ENOENT)
  752. err = -EINVAL;
  753. goto failed_sbi;
  754. }
  755. if (!err) {
  756. printk(KERN_ERR
  757. "NILFS: The specified checkpoint is "
  758. "not a snapshot "
  759. "(checkpoint number=%llu).\n",
  760. (unsigned long long)sbi->s_snapshot_cno);
  761. err = -EINVAL;
  762. goto failed_sbi;
  763. }
  764. cno = sbi->s_snapshot_cno;
  765. }
  766. }
  767. err = nilfs_attach_checkpoint(sbi, cno);
  768. if (err) {
  769. printk(KERN_ERR "NILFS: error loading a checkpoint"
  770. " (checkpoint number=%llu).\n", (unsigned long long)cno);
  771. goto failed_sbi;
  772. }
  773. if (!(sb->s_flags & MS_RDONLY)) {
  774. err = nilfs_attach_segment_constructor(sbi);
  775. if (err)
  776. goto failed_checkpoint;
  777. }
  778. root = nilfs_iget(sb, NILFS_ROOT_INO);
  779. if (IS_ERR(root)) {
  780. printk(KERN_ERR "NILFS: get root inode failed\n");
  781. err = PTR_ERR(root);
  782. goto failed_segctor;
  783. }
  784. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  785. iput(root);
  786. printk(KERN_ERR "NILFS: corrupt root inode.\n");
  787. err = -EINVAL;
  788. goto failed_segctor;
  789. }
  790. sb->s_root = d_alloc_root(root);
  791. if (!sb->s_root) {
  792. iput(root);
  793. printk(KERN_ERR "NILFS: get root dentry failed\n");
  794. err = -ENOMEM;
  795. goto failed_segctor;
  796. }
  797. if (!(sb->s_flags & MS_RDONLY)) {
  798. down_write(&nilfs->ns_sem);
  799. nilfs_setup_super(sbi);
  800. up_write(&nilfs->ns_sem);
  801. }
  802. down_write(&nilfs->ns_super_sem);
  803. if (!nilfs_test_opt(sbi, SNAPSHOT))
  804. nilfs->ns_current = sbi;
  805. up_write(&nilfs->ns_super_sem);
  806. return 0;
  807. failed_segctor:
  808. nilfs_detach_segment_constructor(sbi);
  809. failed_checkpoint:
  810. nilfs_detach_checkpoint(sbi);
  811. failed_sbi:
  812. put_nilfs(nilfs);
  813. sb->s_fs_info = NULL;
  814. nilfs_put_sbinfo(sbi);
  815. return err;
  816. }
  817. static int nilfs_remount(struct super_block *sb, int *flags, char *data)
  818. {
  819. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  820. struct the_nilfs *nilfs = sbi->s_nilfs;
  821. unsigned long old_sb_flags;
  822. struct nilfs_mount_options old_opts;
  823. int was_snapshot, err;
  824. down_write(&nilfs->ns_super_sem);
  825. old_sb_flags = sb->s_flags;
  826. old_opts.mount_opt = sbi->s_mount_opt;
  827. old_opts.snapshot_cno = sbi->s_snapshot_cno;
  828. was_snapshot = nilfs_test_opt(sbi, SNAPSHOT);
  829. if (!parse_options(data, sb, 1)) {
  830. err = -EINVAL;
  831. goto restore_opts;
  832. }
  833. sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
  834. err = -EINVAL;
  835. if (was_snapshot && !(*flags & MS_RDONLY)) {
  836. printk(KERN_ERR "NILFS (device %s): cannot remount snapshot "
  837. "read/write.\n", sb->s_id);
  838. goto restore_opts;
  839. }
  840. if (!nilfs_valid_fs(nilfs)) {
  841. printk(KERN_WARNING "NILFS (device %s): couldn't "
  842. "remount because the filesystem is in an "
  843. "incomplete recovery state.\n", sb->s_id);
  844. goto restore_opts;
  845. }
  846. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  847. goto out;
  848. if (*flags & MS_RDONLY) {
  849. /* Shutting down the segment constructor */
  850. nilfs_detach_segment_constructor(sbi);
  851. sb->s_flags |= MS_RDONLY;
  852. /*
  853. * Remounting a valid RW partition RDONLY, so set
  854. * the RDONLY flag and then mark the partition as valid again.
  855. */
  856. down_write(&nilfs->ns_sem);
  857. nilfs_cleanup_super(sbi);
  858. up_write(&nilfs->ns_sem);
  859. } else {
  860. __u64 features;
  861. /*
  862. * Mounting a RDONLY partition read-write, so reread and
  863. * store the current valid flag. (It may have been changed
  864. * by fsck since we originally mounted the partition.)
  865. */
  866. down_read(&nilfs->ns_sem);
  867. features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
  868. ~NILFS_FEATURE_COMPAT_RO_SUPP;
  869. up_read(&nilfs->ns_sem);
  870. if (features) {
  871. printk(KERN_WARNING "NILFS (device %s): couldn't "
  872. "remount RDWR because of unsupported optional "
  873. "features (%llx)\n",
  874. sb->s_id, (unsigned long long)features);
  875. err = -EROFS;
  876. goto restore_opts;
  877. }
  878. sb->s_flags &= ~MS_RDONLY;
  879. err = nilfs_attach_segment_constructor(sbi);
  880. if (err)
  881. goto restore_opts;
  882. down_write(&nilfs->ns_sem);
  883. nilfs_setup_super(sbi);
  884. up_write(&nilfs->ns_sem);
  885. }
  886. out:
  887. up_write(&nilfs->ns_super_sem);
  888. return 0;
  889. restore_opts:
  890. sb->s_flags = old_sb_flags;
  891. sbi->s_mount_opt = old_opts.mount_opt;
  892. sbi->s_snapshot_cno = old_opts.snapshot_cno;
  893. up_write(&nilfs->ns_super_sem);
  894. return err;
  895. }
  896. struct nilfs_super_data {
  897. struct block_device *bdev;
  898. struct nilfs_sb_info *sbi;
  899. __u64 cno;
  900. int flags;
  901. };
  902. /**
  903. * nilfs_identify - pre-read mount options needed to identify mount instance
  904. * @data: mount options
  905. * @sd: nilfs_super_data
  906. */
  907. static int nilfs_identify(char *data, struct nilfs_super_data *sd)
  908. {
  909. char *p, *options = data;
  910. substring_t args[MAX_OPT_ARGS];
  911. int option, token;
  912. int ret = 0;
  913. do {
  914. p = strsep(&options, ",");
  915. if (p != NULL && *p) {
  916. token = match_token(p, tokens, args);
  917. if (token == Opt_snapshot) {
  918. if (!(sd->flags & MS_RDONLY))
  919. ret++;
  920. else {
  921. ret = match_int(&args[0], &option);
  922. if (!ret) {
  923. if (option > 0)
  924. sd->cno = option;
  925. else
  926. ret++;
  927. }
  928. }
  929. }
  930. if (ret)
  931. printk(KERN_ERR
  932. "NILFS: invalid mount option: %s\n", p);
  933. }
  934. if (!options)
  935. break;
  936. BUG_ON(options == data);
  937. *(options - 1) = ',';
  938. } while (!ret);
  939. return ret;
  940. }
  941. static int nilfs_set_bdev_super(struct super_block *s, void *data)
  942. {
  943. struct nilfs_super_data *sd = data;
  944. s->s_bdev = sd->bdev;
  945. s->s_dev = s->s_bdev->bd_dev;
  946. return 0;
  947. }
  948. static int nilfs_test_bdev_super(struct super_block *s, void *data)
  949. {
  950. struct nilfs_super_data *sd = data;
  951. return sd->sbi && s->s_fs_info == (void *)sd->sbi;
  952. }
  953. static int
  954. nilfs_get_sb(struct file_system_type *fs_type, int flags,
  955. const char *dev_name, void *data, struct vfsmount *mnt)
  956. {
  957. struct nilfs_super_data sd;
  958. struct super_block *s;
  959. fmode_t mode = FMODE_READ;
  960. struct the_nilfs *nilfs;
  961. int err, need_to_close = 1;
  962. if (!(flags & MS_RDONLY))
  963. mode |= FMODE_WRITE;
  964. sd.bdev = open_bdev_exclusive(dev_name, mode, fs_type);
  965. if (IS_ERR(sd.bdev))
  966. return PTR_ERR(sd.bdev);
  967. /*
  968. * To get mount instance using sget() vfs-routine, NILFS needs
  969. * much more information than normal filesystems to identify mount
  970. * instance. For snapshot mounts, not only a mount type (ro-mount
  971. * or rw-mount) but also a checkpoint number is required.
  972. */
  973. sd.cno = 0;
  974. sd.flags = flags;
  975. if (nilfs_identify((char *)data, &sd)) {
  976. err = -EINVAL;
  977. goto failed;
  978. }
  979. nilfs = find_or_create_nilfs(sd.bdev);
  980. if (!nilfs) {
  981. err = -ENOMEM;
  982. goto failed;
  983. }
  984. mutex_lock(&nilfs->ns_mount_mutex);
  985. if (!sd.cno) {
  986. /*
  987. * Check if an exclusive mount exists or not.
  988. * Snapshot mounts coexist with a current mount
  989. * (i.e. rw-mount or ro-mount), whereas rw-mount and
  990. * ro-mount are mutually exclusive.
  991. */
  992. down_read(&nilfs->ns_super_sem);
  993. if (nilfs->ns_current &&
  994. ((nilfs->ns_current->s_super->s_flags ^ flags)
  995. & MS_RDONLY)) {
  996. up_read(&nilfs->ns_super_sem);
  997. err = -EBUSY;
  998. goto failed_unlock;
  999. }
  1000. up_read(&nilfs->ns_super_sem);
  1001. }
  1002. /*
  1003. * Find existing nilfs_sb_info struct
  1004. */
  1005. sd.sbi = nilfs_find_sbinfo(nilfs, !(flags & MS_RDONLY), sd.cno);
  1006. /*
  1007. * Get super block instance holding the nilfs_sb_info struct.
  1008. * A new instance is allocated if no existing mount is present or
  1009. * existing instance has been unmounted.
  1010. */
  1011. s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, &sd);
  1012. if (sd.sbi)
  1013. nilfs_put_sbinfo(sd.sbi);
  1014. if (IS_ERR(s)) {
  1015. err = PTR_ERR(s);
  1016. goto failed_unlock;
  1017. }
  1018. if (!s->s_root) {
  1019. char b[BDEVNAME_SIZE];
  1020. /* New superblock instance created */
  1021. s->s_flags = flags;
  1022. s->s_mode = mode;
  1023. strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id));
  1024. sb_set_blocksize(s, block_size(sd.bdev));
  1025. err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0,
  1026. nilfs);
  1027. if (err)
  1028. goto cancel_new;
  1029. s->s_flags |= MS_ACTIVE;
  1030. need_to_close = 0;
  1031. }
  1032. mutex_unlock(&nilfs->ns_mount_mutex);
  1033. put_nilfs(nilfs);
  1034. if (need_to_close)
  1035. close_bdev_exclusive(sd.bdev, mode);
  1036. simple_set_mnt(mnt, s);
  1037. return 0;
  1038. failed_unlock:
  1039. mutex_unlock(&nilfs->ns_mount_mutex);
  1040. put_nilfs(nilfs);
  1041. failed:
  1042. close_bdev_exclusive(sd.bdev, mode);
  1043. return err;
  1044. cancel_new:
  1045. /* Abandoning the newly allocated superblock */
  1046. mutex_unlock(&nilfs->ns_mount_mutex);
  1047. put_nilfs(nilfs);
  1048. deactivate_locked_super(s);
  1049. /*
  1050. * deactivate_locked_super() invokes close_bdev_exclusive().
  1051. * We must finish all post-cleaning before this call;
  1052. * put_nilfs() needs the block device.
  1053. */
  1054. return err;
  1055. }
  1056. struct file_system_type nilfs_fs_type = {
  1057. .owner = THIS_MODULE,
  1058. .name = "nilfs2",
  1059. .get_sb = nilfs_get_sb,
  1060. .kill_sb = kill_block_super,
  1061. .fs_flags = FS_REQUIRES_DEV,
  1062. };
  1063. static void nilfs_inode_init_once(void *obj)
  1064. {
  1065. struct nilfs_inode_info *ii = obj;
  1066. INIT_LIST_HEAD(&ii->i_dirty);
  1067. #ifdef CONFIG_NILFS_XATTR
  1068. init_rwsem(&ii->xattr_sem);
  1069. #endif
  1070. nilfs_btnode_cache_init_once(&ii->i_btnode_cache);
  1071. ii->i_bmap = &ii->i_bmap_data;
  1072. inode_init_once(&ii->vfs_inode);
  1073. }
  1074. static void nilfs_segbuf_init_once(void *obj)
  1075. {
  1076. memset(obj, 0, sizeof(struct nilfs_segment_buffer));
  1077. }
  1078. static void nilfs_destroy_cachep(void)
  1079. {
  1080. if (nilfs_inode_cachep)
  1081. kmem_cache_destroy(nilfs_inode_cachep);
  1082. if (nilfs_transaction_cachep)
  1083. kmem_cache_destroy(nilfs_transaction_cachep);
  1084. if (nilfs_segbuf_cachep)
  1085. kmem_cache_destroy(nilfs_segbuf_cachep);
  1086. if (nilfs_btree_path_cache)
  1087. kmem_cache_destroy(nilfs_btree_path_cache);
  1088. }
  1089. static int __init nilfs_init_cachep(void)
  1090. {
  1091. nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
  1092. sizeof(struct nilfs_inode_info), 0,
  1093. SLAB_RECLAIM_ACCOUNT, nilfs_inode_init_once);
  1094. if (!nilfs_inode_cachep)
  1095. goto fail;
  1096. nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
  1097. sizeof(struct nilfs_transaction_info), 0,
  1098. SLAB_RECLAIM_ACCOUNT, NULL);
  1099. if (!nilfs_transaction_cachep)
  1100. goto fail;
  1101. nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
  1102. sizeof(struct nilfs_segment_buffer), 0,
  1103. SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
  1104. if (!nilfs_segbuf_cachep)
  1105. goto fail;
  1106. nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
  1107. sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
  1108. 0, 0, NULL);
  1109. if (!nilfs_btree_path_cache)
  1110. goto fail;
  1111. return 0;
  1112. fail:
  1113. nilfs_destroy_cachep();
  1114. return -ENOMEM;
  1115. }
  1116. static int __init init_nilfs_fs(void)
  1117. {
  1118. int err;
  1119. err = nilfs_init_cachep();
  1120. if (err)
  1121. goto fail;
  1122. err = register_filesystem(&nilfs_fs_type);
  1123. if (err)
  1124. goto free_cachep;
  1125. printk(KERN_INFO "NILFS version 2 loaded\n");
  1126. return 0;
  1127. free_cachep:
  1128. nilfs_destroy_cachep();
  1129. fail:
  1130. return err;
  1131. }
  1132. static void __exit exit_nilfs_fs(void)
  1133. {
  1134. nilfs_destroy_cachep();
  1135. unregister_filesystem(&nilfs_fs_type);
  1136. }
  1137. module_init(init_nilfs_fs)
  1138. module_exit(exit_nilfs_fs)