super.c 32 KB

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