super.c 33 KB

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