super.c 34 KB

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