super.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. /*
  2. * super.c - NILFS module and super block management.
  3. *
  4. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Written by Ryusuke Konishi <ryusuke@osrg.net>
  21. */
  22. /*
  23. * linux/fs/ext2/super.c
  24. *
  25. * Copyright (C) 1992, 1993, 1994, 1995
  26. * Remy Card (card@masi.ibp.fr)
  27. * Laboratoire MASI - Institut Blaise Pascal
  28. * Universite Pierre et Marie Curie (Paris VI)
  29. *
  30. * from
  31. *
  32. * linux/fs/minix/inode.c
  33. *
  34. * Copyright (C) 1991, 1992 Linus Torvalds
  35. *
  36. * Big-endian to little-endian byte-swapping/bitmaps by
  37. * David S. Miller (davem@caip.rutgers.edu), 1995
  38. */
  39. #include <linux/module.h>
  40. #include <linux/string.h>
  41. #include <linux/slab.h>
  42. #include <linux/init.h>
  43. #include <linux/blkdev.h>
  44. #include <linux/parser.h>
  45. #include <linux/random.h>
  46. #include <linux/crc32.h>
  47. #include <linux/smp_lock.h>
  48. #include <linux/vfs.h>
  49. #include <linux/writeback.h>
  50. #include <linux/kobject.h>
  51. #include <linux/exportfs.h>
  52. #include <linux/seq_file.h>
  53. #include <linux/mount.h>
  54. #include "nilfs.h"
  55. #include "mdt.h"
  56. #include "alloc.h"
  57. #include "page.h"
  58. #include "cpfile.h"
  59. #include "ifile.h"
  60. #include "dat.h"
  61. #include "segment.h"
  62. #include "segbuf.h"
  63. MODULE_AUTHOR("NTT Corp.");
  64. MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
  65. "(NILFS)");
  66. MODULE_LICENSE("GPL");
  67. struct kmem_cache *nilfs_inode_cachep;
  68. struct kmem_cache *nilfs_transaction_cachep;
  69. struct kmem_cache *nilfs_segbuf_cachep;
  70. struct kmem_cache *nilfs_btree_path_cache;
  71. static int nilfs_remount(struct super_block *sb, int *flags, char *data);
  72. static void nilfs_set_error(struct nilfs_sb_info *sbi)
  73. {
  74. struct the_nilfs *nilfs = sbi->s_nilfs;
  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(sbi, 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(sbi, 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 nilfs_sb_info *sbi = NILFS_SB(sb);
  105. va_list args;
  106. va_start(args, fmt);
  107. printk(KERN_CRIT "NILFS error (device %s): %s: ", sb->s_id, function);
  108. vprintk(fmt, args);
  109. printk("\n");
  110. va_end(args);
  111. if (!(sb->s_flags & MS_RDONLY)) {
  112. nilfs_set_error(sbi);
  113. if (nilfs_test_opt(sbi, ERRORS_RO)) {
  114. printk(KERN_CRIT "Remounting filesystem read-only\n");
  115. sb->s_flags |= MS_RDONLY;
  116. }
  117. }
  118. if (nilfs_test_opt(sbi, ERRORS_PANIC))
  119. panic("NILFS (device %s): panic forced after error\n",
  120. sb->s_id);
  121. }
  122. void nilfs_warning(struct super_block *sb, const char *function,
  123. const char *fmt, ...)
  124. {
  125. va_list args;
  126. va_start(args, fmt);
  127. printk(KERN_WARNING "NILFS warning (device %s): %s: ",
  128. sb->s_id, function);
  129. vprintk(fmt, args);
  130. printk("\n");
  131. va_end(args);
  132. }
  133. struct inode *nilfs_alloc_inode_common(struct the_nilfs *nilfs)
  134. {
  135. struct nilfs_inode_info *ii;
  136. ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
  137. if (!ii)
  138. return NULL;
  139. ii->i_bh = NULL;
  140. ii->i_state = 0;
  141. ii->vfs_inode.i_version = 1;
  142. nilfs_btnode_cache_init(&ii->i_btnode_cache, nilfs->ns_bdi);
  143. return &ii->vfs_inode;
  144. }
  145. struct inode *nilfs_alloc_inode(struct super_block *sb)
  146. {
  147. return nilfs_alloc_inode_common(NILFS_SB(sb)->s_nilfs);
  148. }
  149. void nilfs_destroy_inode(struct inode *inode)
  150. {
  151. kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
  152. }
  153. static void nilfs_clear_inode(struct inode *inode)
  154. {
  155. struct nilfs_inode_info *ii = NILFS_I(inode);
  156. /*
  157. * Free resources allocated in nilfs_read_inode(), here.
  158. */
  159. BUG_ON(!list_empty(&ii->i_dirty));
  160. brelse(ii->i_bh);
  161. ii->i_bh = NULL;
  162. if (test_bit(NILFS_I_BMAP, &ii->i_state))
  163. nilfs_bmap_clear(ii->i_bmap);
  164. nilfs_btnode_cache_clear(&ii->i_btnode_cache);
  165. }
  166. static int nilfs_sync_super(struct nilfs_sb_info *sbi, int flag)
  167. {
  168. struct the_nilfs *nilfs = sbi->s_nilfs;
  169. int err;
  170. int barrier_done = 0;
  171. if (nilfs_test_opt(sbi, BARRIER)) {
  172. set_buffer_ordered(nilfs->ns_sbh[0]);
  173. barrier_done = 1;
  174. }
  175. retry:
  176. set_buffer_dirty(nilfs->ns_sbh[0]);
  177. err = sync_dirty_buffer(nilfs->ns_sbh[0]);
  178. if (err == -EOPNOTSUPP && barrier_done) {
  179. nilfs_warning(sbi->s_super, __func__,
  180. "barrier-based sync failed. "
  181. "disabling barriers\n");
  182. nilfs_clear_opt(sbi, BARRIER);
  183. barrier_done = 0;
  184. clear_buffer_ordered(nilfs->ns_sbh[0]);
  185. goto retry;
  186. }
  187. if (unlikely(err)) {
  188. printk(KERN_ERR
  189. "NILFS: unable to write superblock (err=%d)\n", err);
  190. if (err == -EIO && nilfs->ns_sbh[1]) {
  191. /*
  192. * sbp[0] points to newer log than sbp[1],
  193. * so copy sbp[0] to sbp[1] to take over sbp[0].
  194. */
  195. memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0],
  196. nilfs->ns_sbsize);
  197. nilfs_fall_back_super_block(nilfs);
  198. goto retry;
  199. }
  200. } else {
  201. struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
  202. nilfs->ns_sbwcount++;
  203. /*
  204. * The latest segment becomes trailable from the position
  205. * written in superblock.
  206. */
  207. clear_nilfs_discontinued(nilfs);
  208. /* update GC protection for recent segments */
  209. if (nilfs->ns_sbh[1]) {
  210. if (flag == NILFS_SB_COMMIT_ALL) {
  211. set_buffer_dirty(nilfs->ns_sbh[1]);
  212. if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
  213. goto out;
  214. }
  215. if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) <
  216. le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno))
  217. sbp = nilfs->ns_sbp[1];
  218. }
  219. spin_lock(&nilfs->ns_last_segment_lock);
  220. nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
  221. spin_unlock(&nilfs->ns_last_segment_lock);
  222. }
  223. out:
  224. return err;
  225. }
  226. void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
  227. struct the_nilfs *nilfs)
  228. {
  229. sector_t nfreeblocks;
  230. /* nilfs->ns_sem must be locked by the caller. */
  231. nilfs_count_free_blocks(nilfs, &nfreeblocks);
  232. sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
  233. spin_lock(&nilfs->ns_last_segment_lock);
  234. sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
  235. sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
  236. sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
  237. spin_unlock(&nilfs->ns_last_segment_lock);
  238. }
  239. struct nilfs_super_block **nilfs_prepare_super(struct nilfs_sb_info *sbi,
  240. int flip)
  241. {
  242. struct the_nilfs *nilfs = sbi->s_nilfs;
  243. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  244. /* nilfs->ns_sem must be locked by the caller. */
  245. if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
  246. if (sbp[1] &&
  247. sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
  248. memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
  249. } else {
  250. printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
  251. sbi->s_super->s_id);
  252. return NULL;
  253. }
  254. } else if (sbp[1] &&
  255. sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
  256. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  257. }
  258. if (flip && sbp[1])
  259. nilfs_swap_super_block(nilfs);
  260. return sbp;
  261. }
  262. int nilfs_commit_super(struct nilfs_sb_info *sbi, int flag)
  263. {
  264. struct the_nilfs *nilfs = sbi->s_nilfs;
  265. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  266. time_t t;
  267. /* nilfs->ns_sem must be locked by the caller. */
  268. t = get_seconds();
  269. nilfs->ns_sbwtime = t;
  270. sbp[0]->s_wtime = cpu_to_le64(t);
  271. sbp[0]->s_sum = 0;
  272. sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
  273. (unsigned char *)sbp[0],
  274. nilfs->ns_sbsize));
  275. if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
  276. sbp[1]->s_wtime = sbp[0]->s_wtime;
  277. sbp[1]->s_sum = 0;
  278. sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
  279. (unsigned char *)sbp[1],
  280. nilfs->ns_sbsize));
  281. }
  282. clear_nilfs_sb_dirty(nilfs);
  283. return nilfs_sync_super(sbi, flag);
  284. }
  285. /**
  286. * nilfs_cleanup_super() - write filesystem state for cleanup
  287. * @sbi: nilfs_sb_info to be unmounted or degraded to read-only
  288. *
  289. * This function restores state flags in the on-disk super block.
  290. * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
  291. * filesystem was not clean previously.
  292. */
  293. int nilfs_cleanup_super(struct nilfs_sb_info *sbi)
  294. {
  295. struct nilfs_super_block **sbp;
  296. int flag = NILFS_SB_COMMIT;
  297. int ret = -EIO;
  298. sbp = nilfs_prepare_super(sbi, 0);
  299. if (sbp) {
  300. sbp[0]->s_state = cpu_to_le16(sbi->s_nilfs->ns_mount_state);
  301. nilfs_set_log_cursor(sbp[0], sbi->s_nilfs);
  302. if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
  303. /*
  304. * make the "clean" flag also to the opposite
  305. * super block if both super blocks point to
  306. * the same checkpoint.
  307. */
  308. sbp[1]->s_state = sbp[0]->s_state;
  309. flag = NILFS_SB_COMMIT_ALL;
  310. }
  311. ret = nilfs_commit_super(sbi, flag);
  312. }
  313. return ret;
  314. }
  315. static void nilfs_put_super(struct super_block *sb)
  316. {
  317. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  318. struct the_nilfs *nilfs = sbi->s_nilfs;
  319. lock_kernel();
  320. nilfs_detach_segment_constructor(sbi);
  321. if (!(sb->s_flags & MS_RDONLY)) {
  322. down_write(&nilfs->ns_sem);
  323. nilfs_cleanup_super(sbi);
  324. up_write(&nilfs->ns_sem);
  325. }
  326. down_write(&nilfs->ns_super_sem);
  327. if (nilfs->ns_current == sbi)
  328. nilfs->ns_current = NULL;
  329. up_write(&nilfs->ns_super_sem);
  330. nilfs_detach_checkpoint(sbi);
  331. put_nilfs(sbi->s_nilfs);
  332. sbi->s_super = NULL;
  333. sb->s_fs_info = NULL;
  334. nilfs_put_sbinfo(sbi);
  335. unlock_kernel();
  336. }
  337. static int nilfs_sync_fs(struct super_block *sb, int wait)
  338. {
  339. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  340. struct the_nilfs *nilfs = sbi->s_nilfs;
  341. struct nilfs_super_block **sbp;
  342. int err = 0;
  343. /* This function is called when super block should be written back */
  344. if (wait)
  345. err = nilfs_construct_segment(sb);
  346. down_write(&nilfs->ns_sem);
  347. if (nilfs_sb_dirty(nilfs)) {
  348. sbp = nilfs_prepare_super(sbi, nilfs_sb_will_flip(nilfs));
  349. if (likely(sbp)) {
  350. nilfs_set_log_cursor(sbp[0], nilfs);
  351. nilfs_commit_super(sbi, NILFS_SB_COMMIT);
  352. }
  353. }
  354. up_write(&nilfs->ns_sem);
  355. return err;
  356. }
  357. int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno)
  358. {
  359. struct the_nilfs *nilfs = sbi->s_nilfs;
  360. struct nilfs_checkpoint *raw_cp;
  361. struct buffer_head *bh_cp;
  362. int err;
  363. down_write(&nilfs->ns_super_sem);
  364. list_add(&sbi->s_list, &nilfs->ns_supers);
  365. up_write(&nilfs->ns_super_sem);
  366. sbi->s_ifile = nilfs_ifile_new(sbi, nilfs->ns_inode_size);
  367. if (!sbi->s_ifile)
  368. return -ENOMEM;
  369. down_read(&nilfs->ns_segctor_sem);
  370. err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
  371. &bh_cp);
  372. up_read(&nilfs->ns_segctor_sem);
  373. if (unlikely(err)) {
  374. if (err == -ENOENT || err == -EINVAL) {
  375. printk(KERN_ERR
  376. "NILFS: Invalid checkpoint "
  377. "(checkpoint number=%llu)\n",
  378. (unsigned long long)cno);
  379. err = -EINVAL;
  380. }
  381. goto failed;
  382. }
  383. err = nilfs_read_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode);
  384. if (unlikely(err))
  385. goto failed_bh;
  386. atomic_set(&sbi->s_inodes_count, le64_to_cpu(raw_cp->cp_inodes_count));
  387. atomic_set(&sbi->s_blocks_count, le64_to_cpu(raw_cp->cp_blocks_count));
  388. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  389. return 0;
  390. failed_bh:
  391. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  392. failed:
  393. nilfs_mdt_destroy(sbi->s_ifile);
  394. sbi->s_ifile = NULL;
  395. down_write(&nilfs->ns_super_sem);
  396. list_del_init(&sbi->s_list);
  397. up_write(&nilfs->ns_super_sem);
  398. return err;
  399. }
  400. void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi)
  401. {
  402. struct the_nilfs *nilfs = sbi->s_nilfs;
  403. nilfs_mdt_destroy(sbi->s_ifile);
  404. sbi->s_ifile = NULL;
  405. down_write(&nilfs->ns_super_sem);
  406. list_del_init(&sbi->s_list);
  407. up_write(&nilfs->ns_super_sem);
  408. }
  409. static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  410. {
  411. struct super_block *sb = dentry->d_sb;
  412. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  413. struct the_nilfs *nilfs = sbi->s_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(&sbi->s_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. if (!nilfs_test_opt(sbi, BARRIER))
  457. seq_printf(seq, ",nobarrier");
  458. if (nilfs_test_opt(sbi, SNAPSHOT))
  459. seq_printf(seq, ",cp=%llu",
  460. (unsigned long long int)sbi->s_snapshot_cno);
  461. if (nilfs_test_opt(sbi, ERRORS_PANIC))
  462. seq_printf(seq, ",errors=panic");
  463. if (nilfs_test_opt(sbi, ERRORS_CONT))
  464. seq_printf(seq, ",errors=continue");
  465. if (nilfs_test_opt(sbi, STRICT_ORDER))
  466. seq_printf(seq, ",order=strict");
  467. if (nilfs_test_opt(sbi, NORECOVERY))
  468. seq_printf(seq, ",norecovery");
  469. if (nilfs_test_opt(sbi, DISCARD))
  470. seq_printf(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. .delete_inode = nilfs_delete_inode,
  481. .put_super = nilfs_put_super,
  482. /* .write_super = nilfs_write_super, */
  483. .sync_fs = nilfs_sync_fs,
  484. /* .write_super_lockfs */
  485. /* .unlockfs */
  486. .statfs = nilfs_statfs,
  487. .remount_fs = nilfs_remount,
  488. .clear_inode = nilfs_clear_inode,
  489. /* .umount_begin */
  490. .show_options = nilfs_show_options
  491. };
  492. static struct inode *
  493. nilfs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation)
  494. {
  495. struct inode *inode;
  496. if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO &&
  497. ino != NILFS_SKETCH_INO)
  498. return ERR_PTR(-ESTALE);
  499. inode = nilfs_iget(sb, ino);
  500. if (IS_ERR(inode))
  501. return ERR_CAST(inode);
  502. if (generation && inode->i_generation != generation) {
  503. iput(inode);
  504. return ERR_PTR(-ESTALE);
  505. }
  506. return inode;
  507. }
  508. static struct dentry *
  509. nilfs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
  510. int fh_type)
  511. {
  512. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  513. nilfs_nfs_get_inode);
  514. }
  515. static struct dentry *
  516. nilfs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len,
  517. int fh_type)
  518. {
  519. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  520. nilfs_nfs_get_inode);
  521. }
  522. static const struct export_operations nilfs_export_ops = {
  523. .fh_to_dentry = nilfs_fh_to_dentry,
  524. .fh_to_parent = nilfs_fh_to_parent,
  525. .get_parent = nilfs_get_parent,
  526. };
  527. enum {
  528. Opt_err_cont, Opt_err_panic, Opt_err_ro,
  529. Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
  530. Opt_discard, Opt_err,
  531. };
  532. static match_table_t tokens = {
  533. {Opt_err_cont, "errors=continue"},
  534. {Opt_err_panic, "errors=panic"},
  535. {Opt_err_ro, "errors=remount-ro"},
  536. {Opt_barrier, "barrier"},
  537. {Opt_nobarrier, "nobarrier"},
  538. {Opt_snapshot, "cp=%u"},
  539. {Opt_order, "order=%s"},
  540. {Opt_norecovery, "norecovery"},
  541. {Opt_discard, "discard"},
  542. {Opt_err, NULL}
  543. };
  544. static int parse_options(char *options, struct super_block *sb)
  545. {
  546. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  547. char *p;
  548. substring_t args[MAX_OPT_ARGS];
  549. int option;
  550. if (!options)
  551. return 1;
  552. while ((p = strsep(&options, ",")) != NULL) {
  553. int token;
  554. if (!*p)
  555. continue;
  556. token = match_token(p, tokens, args);
  557. switch (token) {
  558. case Opt_barrier:
  559. nilfs_set_opt(sbi, BARRIER);
  560. break;
  561. case Opt_nobarrier:
  562. nilfs_clear_opt(sbi, BARRIER);
  563. break;
  564. case Opt_order:
  565. if (strcmp(args[0].from, "relaxed") == 0)
  566. /* Ordered data semantics */
  567. nilfs_clear_opt(sbi, STRICT_ORDER);
  568. else if (strcmp(args[0].from, "strict") == 0)
  569. /* Strict in-order semantics */
  570. nilfs_set_opt(sbi, STRICT_ORDER);
  571. else
  572. return 0;
  573. break;
  574. case Opt_err_panic:
  575. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC);
  576. break;
  577. case Opt_err_ro:
  578. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO);
  579. break;
  580. case Opt_err_cont:
  581. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT);
  582. break;
  583. case Opt_snapshot:
  584. if (match_int(&args[0], &option) || option <= 0)
  585. return 0;
  586. if (!(sb->s_flags & MS_RDONLY))
  587. return 0;
  588. sbi->s_snapshot_cno = option;
  589. nilfs_set_opt(sbi, SNAPSHOT);
  590. break;
  591. case Opt_norecovery:
  592. nilfs_set_opt(sbi, NORECOVERY);
  593. break;
  594. case Opt_discard:
  595. nilfs_set_opt(sbi, DISCARD);
  596. break;
  597. default:
  598. printk(KERN_ERR
  599. "NILFS: Unrecognized mount option \"%s\"\n", p);
  600. return 0;
  601. }
  602. }
  603. return 1;
  604. }
  605. static inline void
  606. nilfs_set_default_options(struct nilfs_sb_info *sbi,
  607. struct nilfs_super_block *sbp)
  608. {
  609. sbi->s_mount_opt =
  610. NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
  611. }
  612. static int nilfs_setup_super(struct nilfs_sb_info *sbi)
  613. {
  614. struct the_nilfs *nilfs = sbi->s_nilfs;
  615. struct nilfs_super_block **sbp;
  616. int max_mnt_count;
  617. int mnt_count;
  618. /* nilfs->ns_sem must be locked by the caller. */
  619. sbp = nilfs_prepare_super(sbi, 0);
  620. if (!sbp)
  621. return -EIO;
  622. max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
  623. mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
  624. if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
  625. printk(KERN_WARNING
  626. "NILFS warning: mounting fs with errors\n");
  627. #if 0
  628. } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
  629. printk(KERN_WARNING
  630. "NILFS warning: maximal mount count reached\n");
  631. #endif
  632. }
  633. if (!max_mnt_count)
  634. sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
  635. sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
  636. sbp[0]->s_state =
  637. cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
  638. sbp[0]->s_mtime = cpu_to_le64(get_seconds());
  639. /* synchronize sbp[1] with sbp[0] */
  640. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  641. return nilfs_commit_super(sbi, NILFS_SB_COMMIT_ALL);
  642. }
  643. struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
  644. u64 pos, int blocksize,
  645. struct buffer_head **pbh)
  646. {
  647. unsigned long long sb_index = pos;
  648. unsigned long offset;
  649. offset = do_div(sb_index, blocksize);
  650. *pbh = sb_bread(sb, sb_index);
  651. if (!*pbh)
  652. return NULL;
  653. return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
  654. }
  655. int nilfs_store_magic_and_option(struct super_block *sb,
  656. struct nilfs_super_block *sbp,
  657. char *data)
  658. {
  659. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  660. sb->s_magic = le16_to_cpu(sbp->s_magic);
  661. /* FS independent flags */
  662. #ifdef NILFS_ATIME_DISABLE
  663. sb->s_flags |= MS_NOATIME;
  664. #endif
  665. nilfs_set_default_options(sbi, sbp);
  666. sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid);
  667. sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid);
  668. sbi->s_interval = le32_to_cpu(sbp->s_c_interval);
  669. sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max);
  670. return !parse_options(data, sb) ? -EINVAL : 0 ;
  671. }
  672. /**
  673. * nilfs_fill_super() - initialize a super block instance
  674. * @sb: super_block
  675. * @data: mount options
  676. * @silent: silent mode flag
  677. * @nilfs: the_nilfs struct
  678. *
  679. * This function is called exclusively by nilfs->ns_mount_mutex.
  680. * So, the recovery process is protected from other simultaneous mounts.
  681. */
  682. static int
  683. nilfs_fill_super(struct super_block *sb, void *data, int silent,
  684. struct the_nilfs *nilfs)
  685. {
  686. struct nilfs_sb_info *sbi;
  687. struct inode *root;
  688. __u64 cno;
  689. int err;
  690. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  691. if (!sbi)
  692. return -ENOMEM;
  693. sb->s_fs_info = sbi;
  694. get_nilfs(nilfs);
  695. sbi->s_nilfs = nilfs;
  696. sbi->s_super = sb;
  697. atomic_set(&sbi->s_count, 1);
  698. err = init_nilfs(nilfs, sbi, (char *)data);
  699. if (err)
  700. goto failed_sbi;
  701. spin_lock_init(&sbi->s_inode_lock);
  702. INIT_LIST_HEAD(&sbi->s_dirty_files);
  703. INIT_LIST_HEAD(&sbi->s_list);
  704. /*
  705. * Following initialization is overlapped because
  706. * nilfs_sb_info structure has been cleared at the beginning.
  707. * But we reserve them to keep our interest and make ready
  708. * for the future change.
  709. */
  710. get_random_bytes(&sbi->s_next_generation,
  711. sizeof(sbi->s_next_generation));
  712. spin_lock_init(&sbi->s_next_gen_lock);
  713. sb->s_op = &nilfs_sops;
  714. sb->s_export_op = &nilfs_export_ops;
  715. sb->s_root = NULL;
  716. sb->s_time_gran = 1;
  717. sb->s_bdi = nilfs->ns_bdi;
  718. err = load_nilfs(nilfs, sbi);
  719. if (err)
  720. goto failed_sbi;
  721. cno = nilfs_last_cno(nilfs);
  722. if (sb->s_flags & MS_RDONLY) {
  723. if (nilfs_test_opt(sbi, SNAPSHOT)) {
  724. down_read(&nilfs->ns_segctor_sem);
  725. err = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile,
  726. sbi->s_snapshot_cno);
  727. up_read(&nilfs->ns_segctor_sem);
  728. if (err < 0) {
  729. if (err == -ENOENT)
  730. err = -EINVAL;
  731. goto failed_sbi;
  732. }
  733. if (!err) {
  734. printk(KERN_ERR
  735. "NILFS: The specified checkpoint is "
  736. "not a snapshot "
  737. "(checkpoint number=%llu).\n",
  738. (unsigned long long)sbi->s_snapshot_cno);
  739. err = -EINVAL;
  740. goto failed_sbi;
  741. }
  742. cno = sbi->s_snapshot_cno;
  743. }
  744. }
  745. err = nilfs_attach_checkpoint(sbi, cno);
  746. if (err) {
  747. printk(KERN_ERR "NILFS: error loading a checkpoint"
  748. " (checkpoint number=%llu).\n", (unsigned long long)cno);
  749. goto failed_sbi;
  750. }
  751. if (!(sb->s_flags & MS_RDONLY)) {
  752. err = nilfs_attach_segment_constructor(sbi);
  753. if (err)
  754. goto failed_checkpoint;
  755. }
  756. root = nilfs_iget(sb, NILFS_ROOT_INO);
  757. if (IS_ERR(root)) {
  758. printk(KERN_ERR "NILFS: get root inode failed\n");
  759. err = PTR_ERR(root);
  760. goto failed_segctor;
  761. }
  762. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  763. iput(root);
  764. printk(KERN_ERR "NILFS: corrupt root inode.\n");
  765. err = -EINVAL;
  766. goto failed_segctor;
  767. }
  768. sb->s_root = d_alloc_root(root);
  769. if (!sb->s_root) {
  770. iput(root);
  771. printk(KERN_ERR "NILFS: get root dentry failed\n");
  772. err = -ENOMEM;
  773. goto failed_segctor;
  774. }
  775. if (!(sb->s_flags & MS_RDONLY)) {
  776. down_write(&nilfs->ns_sem);
  777. nilfs_setup_super(sbi);
  778. up_write(&nilfs->ns_sem);
  779. }
  780. down_write(&nilfs->ns_super_sem);
  781. if (!nilfs_test_opt(sbi, SNAPSHOT))
  782. nilfs->ns_current = sbi;
  783. up_write(&nilfs->ns_super_sem);
  784. return 0;
  785. failed_segctor:
  786. nilfs_detach_segment_constructor(sbi);
  787. failed_checkpoint:
  788. nilfs_detach_checkpoint(sbi);
  789. failed_sbi:
  790. put_nilfs(nilfs);
  791. sb->s_fs_info = NULL;
  792. nilfs_put_sbinfo(sbi);
  793. return err;
  794. }
  795. static int nilfs_remount(struct super_block *sb, int *flags, char *data)
  796. {
  797. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  798. struct the_nilfs *nilfs = sbi->s_nilfs;
  799. unsigned long old_sb_flags;
  800. struct nilfs_mount_options old_opts;
  801. int was_snapshot, err;
  802. lock_kernel();
  803. down_write(&nilfs->ns_super_sem);
  804. old_sb_flags = sb->s_flags;
  805. old_opts.mount_opt = sbi->s_mount_opt;
  806. old_opts.snapshot_cno = sbi->s_snapshot_cno;
  807. was_snapshot = nilfs_test_opt(sbi, SNAPSHOT);
  808. if (!parse_options(data, sb)) {
  809. err = -EINVAL;
  810. goto restore_opts;
  811. }
  812. sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
  813. err = -EINVAL;
  814. if (was_snapshot) {
  815. if (!(*flags & MS_RDONLY)) {
  816. printk(KERN_ERR "NILFS (device %s): cannot remount "
  817. "snapshot read/write.\n",
  818. sb->s_id);
  819. goto restore_opts;
  820. } else if (sbi->s_snapshot_cno != old_opts.snapshot_cno) {
  821. printk(KERN_ERR "NILFS (device %s): cannot "
  822. "remount to a different snapshot.\n",
  823. sb->s_id);
  824. goto restore_opts;
  825. }
  826. } else {
  827. if (nilfs_test_opt(sbi, SNAPSHOT)) {
  828. printk(KERN_ERR "NILFS (device %s): cannot change "
  829. "a regular mount to a snapshot.\n",
  830. sb->s_id);
  831. goto restore_opts;
  832. }
  833. }
  834. if (!nilfs_valid_fs(nilfs)) {
  835. printk(KERN_WARNING "NILFS (device %s): couldn't "
  836. "remount because the filesystem is in an "
  837. "incomplete recovery state.\n", sb->s_id);
  838. goto restore_opts;
  839. }
  840. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  841. goto out;
  842. if (*flags & MS_RDONLY) {
  843. /* Shutting down the segment constructor */
  844. nilfs_detach_segment_constructor(sbi);
  845. sb->s_flags |= MS_RDONLY;
  846. /*
  847. * Remounting a valid RW partition RDONLY, so set
  848. * the RDONLY flag and then mark the partition as valid again.
  849. */
  850. down_write(&nilfs->ns_sem);
  851. nilfs_cleanup_super(sbi);
  852. up_write(&nilfs->ns_sem);
  853. } else {
  854. /*
  855. * Mounting a RDONLY partition read-write, so reread and
  856. * store the current valid flag. (It may have been changed
  857. * by fsck since we originally mounted the partition.)
  858. */
  859. sb->s_flags &= ~MS_RDONLY;
  860. err = nilfs_attach_segment_constructor(sbi);
  861. if (err)
  862. goto restore_opts;
  863. down_write(&nilfs->ns_sem);
  864. nilfs_setup_super(sbi);
  865. up_write(&nilfs->ns_sem);
  866. }
  867. out:
  868. up_write(&nilfs->ns_super_sem);
  869. unlock_kernel();
  870. return 0;
  871. restore_opts:
  872. sb->s_flags = old_sb_flags;
  873. sbi->s_mount_opt = old_opts.mount_opt;
  874. sbi->s_snapshot_cno = old_opts.snapshot_cno;
  875. up_write(&nilfs->ns_super_sem);
  876. unlock_kernel();
  877. return err;
  878. }
  879. struct nilfs_super_data {
  880. struct block_device *bdev;
  881. struct nilfs_sb_info *sbi;
  882. __u64 cno;
  883. int flags;
  884. };
  885. /**
  886. * nilfs_identify - pre-read mount options needed to identify mount instance
  887. * @data: mount options
  888. * @sd: nilfs_super_data
  889. */
  890. static int nilfs_identify(char *data, struct nilfs_super_data *sd)
  891. {
  892. char *p, *options = data;
  893. substring_t args[MAX_OPT_ARGS];
  894. int option, token;
  895. int ret = 0;
  896. do {
  897. p = strsep(&options, ",");
  898. if (p != NULL && *p) {
  899. token = match_token(p, tokens, args);
  900. if (token == Opt_snapshot) {
  901. if (!(sd->flags & MS_RDONLY))
  902. ret++;
  903. else {
  904. ret = match_int(&args[0], &option);
  905. if (!ret) {
  906. if (option > 0)
  907. sd->cno = option;
  908. else
  909. ret++;
  910. }
  911. }
  912. }
  913. if (ret)
  914. printk(KERN_ERR
  915. "NILFS: invalid mount option: %s\n", p);
  916. }
  917. if (!options)
  918. break;
  919. BUG_ON(options == data);
  920. *(options - 1) = ',';
  921. } while (!ret);
  922. return ret;
  923. }
  924. static int nilfs_set_bdev_super(struct super_block *s, void *data)
  925. {
  926. struct nilfs_super_data *sd = data;
  927. s->s_bdev = sd->bdev;
  928. s->s_dev = s->s_bdev->bd_dev;
  929. return 0;
  930. }
  931. static int nilfs_test_bdev_super(struct super_block *s, void *data)
  932. {
  933. struct nilfs_super_data *sd = data;
  934. return sd->sbi && s->s_fs_info == (void *)sd->sbi;
  935. }
  936. static int
  937. nilfs_get_sb(struct file_system_type *fs_type, int flags,
  938. const char *dev_name, void *data, struct vfsmount *mnt)
  939. {
  940. struct nilfs_super_data sd;
  941. struct super_block *s;
  942. fmode_t mode = FMODE_READ;
  943. struct the_nilfs *nilfs;
  944. int err, need_to_close = 1;
  945. if (!(flags & MS_RDONLY))
  946. mode |= FMODE_WRITE;
  947. sd.bdev = open_bdev_exclusive(dev_name, mode, fs_type);
  948. if (IS_ERR(sd.bdev))
  949. return PTR_ERR(sd.bdev);
  950. /*
  951. * To get mount instance using sget() vfs-routine, NILFS needs
  952. * much more information than normal filesystems to identify mount
  953. * instance. For snapshot mounts, not only a mount type (ro-mount
  954. * or rw-mount) but also a checkpoint number is required.
  955. */
  956. sd.cno = 0;
  957. sd.flags = flags;
  958. if (nilfs_identify((char *)data, &sd)) {
  959. err = -EINVAL;
  960. goto failed;
  961. }
  962. nilfs = find_or_create_nilfs(sd.bdev);
  963. if (!nilfs) {
  964. err = -ENOMEM;
  965. goto failed;
  966. }
  967. mutex_lock(&nilfs->ns_mount_mutex);
  968. if (!sd.cno) {
  969. /*
  970. * Check if an exclusive mount exists or not.
  971. * Snapshot mounts coexist with a current mount
  972. * (i.e. rw-mount or ro-mount), whereas rw-mount and
  973. * ro-mount are mutually exclusive.
  974. */
  975. down_read(&nilfs->ns_super_sem);
  976. if (nilfs->ns_current &&
  977. ((nilfs->ns_current->s_super->s_flags ^ flags)
  978. & MS_RDONLY)) {
  979. up_read(&nilfs->ns_super_sem);
  980. err = -EBUSY;
  981. goto failed_unlock;
  982. }
  983. up_read(&nilfs->ns_super_sem);
  984. }
  985. /*
  986. * Find existing nilfs_sb_info struct
  987. */
  988. sd.sbi = nilfs_find_sbinfo(nilfs, !(flags & MS_RDONLY), sd.cno);
  989. /*
  990. * Get super block instance holding the nilfs_sb_info struct.
  991. * A new instance is allocated if no existing mount is present or
  992. * existing instance has been unmounted.
  993. */
  994. s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, &sd);
  995. if (sd.sbi)
  996. nilfs_put_sbinfo(sd.sbi);
  997. if (IS_ERR(s)) {
  998. err = PTR_ERR(s);
  999. goto failed_unlock;
  1000. }
  1001. if (!s->s_root) {
  1002. char b[BDEVNAME_SIZE];
  1003. /* New superblock instance created */
  1004. s->s_flags = flags;
  1005. s->s_mode = mode;
  1006. strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id));
  1007. sb_set_blocksize(s, block_size(sd.bdev));
  1008. err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0,
  1009. nilfs);
  1010. if (err)
  1011. goto cancel_new;
  1012. s->s_flags |= MS_ACTIVE;
  1013. need_to_close = 0;
  1014. }
  1015. mutex_unlock(&nilfs->ns_mount_mutex);
  1016. put_nilfs(nilfs);
  1017. if (need_to_close)
  1018. close_bdev_exclusive(sd.bdev, mode);
  1019. simple_set_mnt(mnt, s);
  1020. return 0;
  1021. failed_unlock:
  1022. mutex_unlock(&nilfs->ns_mount_mutex);
  1023. put_nilfs(nilfs);
  1024. failed:
  1025. close_bdev_exclusive(sd.bdev, mode);
  1026. return err;
  1027. cancel_new:
  1028. /* Abandoning the newly allocated superblock */
  1029. mutex_unlock(&nilfs->ns_mount_mutex);
  1030. put_nilfs(nilfs);
  1031. deactivate_locked_super(s);
  1032. /*
  1033. * deactivate_locked_super() invokes close_bdev_exclusive().
  1034. * We must finish all post-cleaning before this call;
  1035. * put_nilfs() needs the block device.
  1036. */
  1037. return err;
  1038. }
  1039. struct file_system_type nilfs_fs_type = {
  1040. .owner = THIS_MODULE,
  1041. .name = "nilfs2",
  1042. .get_sb = nilfs_get_sb,
  1043. .kill_sb = kill_block_super,
  1044. .fs_flags = FS_REQUIRES_DEV,
  1045. };
  1046. static void nilfs_inode_init_once(void *obj)
  1047. {
  1048. struct nilfs_inode_info *ii = obj;
  1049. INIT_LIST_HEAD(&ii->i_dirty);
  1050. #ifdef CONFIG_NILFS_XATTR
  1051. init_rwsem(&ii->xattr_sem);
  1052. #endif
  1053. nilfs_btnode_cache_init_once(&ii->i_btnode_cache);
  1054. ii->i_bmap = (struct nilfs_bmap *)&ii->i_bmap_union;
  1055. inode_init_once(&ii->vfs_inode);
  1056. }
  1057. static void nilfs_segbuf_init_once(void *obj)
  1058. {
  1059. memset(obj, 0, sizeof(struct nilfs_segment_buffer));
  1060. }
  1061. static void nilfs_destroy_cachep(void)
  1062. {
  1063. if (nilfs_inode_cachep)
  1064. kmem_cache_destroy(nilfs_inode_cachep);
  1065. if (nilfs_transaction_cachep)
  1066. kmem_cache_destroy(nilfs_transaction_cachep);
  1067. if (nilfs_segbuf_cachep)
  1068. kmem_cache_destroy(nilfs_segbuf_cachep);
  1069. if (nilfs_btree_path_cache)
  1070. kmem_cache_destroy(nilfs_btree_path_cache);
  1071. }
  1072. static int __init nilfs_init_cachep(void)
  1073. {
  1074. nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
  1075. sizeof(struct nilfs_inode_info), 0,
  1076. SLAB_RECLAIM_ACCOUNT, nilfs_inode_init_once);
  1077. if (!nilfs_inode_cachep)
  1078. goto fail;
  1079. nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
  1080. sizeof(struct nilfs_transaction_info), 0,
  1081. SLAB_RECLAIM_ACCOUNT, NULL);
  1082. if (!nilfs_transaction_cachep)
  1083. goto fail;
  1084. nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
  1085. sizeof(struct nilfs_segment_buffer), 0,
  1086. SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
  1087. if (!nilfs_segbuf_cachep)
  1088. goto fail;
  1089. nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
  1090. sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
  1091. 0, 0, NULL);
  1092. if (!nilfs_btree_path_cache)
  1093. goto fail;
  1094. return 0;
  1095. fail:
  1096. nilfs_destroy_cachep();
  1097. return -ENOMEM;
  1098. }
  1099. static int __init init_nilfs_fs(void)
  1100. {
  1101. int err;
  1102. err = nilfs_init_cachep();
  1103. if (err)
  1104. goto fail;
  1105. err = register_filesystem(&nilfs_fs_type);
  1106. if (err)
  1107. goto free_cachep;
  1108. printk(KERN_INFO "NILFS version 2 loaded\n");
  1109. return 0;
  1110. free_cachep:
  1111. nilfs_destroy_cachep();
  1112. fail:
  1113. return err;
  1114. }
  1115. static void __exit exit_nilfs_fs(void)
  1116. {
  1117. nilfs_destroy_cachep();
  1118. unregister_filesystem(&nilfs_fs_type);
  1119. }
  1120. module_init(init_nilfs_fs)
  1121. module_exit(exit_nilfs_fs)