super.c 33 KB

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