super.c 33 KB

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