super.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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. /**
  73. * nilfs_error() - report failure condition on a filesystem
  74. *
  75. * nilfs_error() sets an ERROR_FS flag on the superblock as well as
  76. * reporting an error message. It should be called when NILFS detects
  77. * incoherences or defects of meta data on disk. As for sustainable
  78. * errors such as a single-shot I/O error, nilfs_warning() or the printk()
  79. * function should be used instead.
  80. *
  81. * The segment constructor must not call this function because it can
  82. * kill itself.
  83. */
  84. void nilfs_error(struct super_block *sb, const char *function,
  85. const char *fmt, ...)
  86. {
  87. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  88. va_list args;
  89. va_start(args, fmt);
  90. printk(KERN_CRIT "NILFS error (device %s): %s: ", sb->s_id, function);
  91. vprintk(fmt, args);
  92. printk("\n");
  93. va_end(args);
  94. if (!(sb->s_flags & MS_RDONLY)) {
  95. struct the_nilfs *nilfs = sbi->s_nilfs;
  96. down_write(&nilfs->ns_sem);
  97. if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
  98. nilfs->ns_mount_state |= NILFS_ERROR_FS;
  99. nilfs->ns_sbp[0]->s_state |=
  100. cpu_to_le16(NILFS_ERROR_FS);
  101. nilfs_commit_super(sbi, 1);
  102. }
  103. up_write(&nilfs->ns_sem);
  104. if (nilfs_test_opt(sbi, ERRORS_RO)) {
  105. printk(KERN_CRIT "Remounting filesystem read-only\n");
  106. sb->s_flags |= MS_RDONLY;
  107. }
  108. }
  109. if (nilfs_test_opt(sbi, ERRORS_PANIC))
  110. panic("NILFS (device %s): panic forced after error\n",
  111. sb->s_id);
  112. }
  113. void nilfs_warning(struct super_block *sb, const char *function,
  114. const char *fmt, ...)
  115. {
  116. va_list args;
  117. va_start(args, fmt);
  118. printk(KERN_WARNING "NILFS warning (device %s): %s: ",
  119. sb->s_id, function);
  120. vprintk(fmt, args);
  121. printk("\n");
  122. va_end(args);
  123. }
  124. struct inode *nilfs_alloc_inode_common(struct the_nilfs *nilfs)
  125. {
  126. struct nilfs_inode_info *ii;
  127. ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
  128. if (!ii)
  129. return NULL;
  130. ii->i_bh = NULL;
  131. ii->i_state = 0;
  132. ii->vfs_inode.i_version = 1;
  133. nilfs_btnode_cache_init(&ii->i_btnode_cache, nilfs->ns_bdi);
  134. return &ii->vfs_inode;
  135. }
  136. struct inode *nilfs_alloc_inode(struct super_block *sb)
  137. {
  138. return nilfs_alloc_inode_common(NILFS_SB(sb)->s_nilfs);
  139. }
  140. void nilfs_destroy_inode(struct inode *inode)
  141. {
  142. kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
  143. }
  144. static void nilfs_clear_inode(struct inode *inode)
  145. {
  146. struct nilfs_inode_info *ii = NILFS_I(inode);
  147. /*
  148. * Free resources allocated in nilfs_read_inode(), here.
  149. */
  150. BUG_ON(!list_empty(&ii->i_dirty));
  151. brelse(ii->i_bh);
  152. ii->i_bh = NULL;
  153. if (test_bit(NILFS_I_BMAP, &ii->i_state))
  154. nilfs_bmap_clear(ii->i_bmap);
  155. nilfs_btnode_cache_clear(&ii->i_btnode_cache);
  156. }
  157. static int nilfs_sync_super(struct nilfs_sb_info *sbi, int dupsb)
  158. {
  159. struct the_nilfs *nilfs = sbi->s_nilfs;
  160. int err;
  161. int barrier_done = 0;
  162. if (nilfs_test_opt(sbi, BARRIER)) {
  163. set_buffer_ordered(nilfs->ns_sbh[0]);
  164. barrier_done = 1;
  165. }
  166. retry:
  167. set_buffer_dirty(nilfs->ns_sbh[0]);
  168. err = sync_dirty_buffer(nilfs->ns_sbh[0]);
  169. if (err == -EOPNOTSUPP && barrier_done) {
  170. nilfs_warning(sbi->s_super, __func__,
  171. "barrier-based sync failed. "
  172. "disabling barriers\n");
  173. nilfs_clear_opt(sbi, BARRIER);
  174. barrier_done = 0;
  175. clear_buffer_ordered(nilfs->ns_sbh[0]);
  176. goto retry;
  177. }
  178. if (unlikely(err)) {
  179. printk(KERN_ERR
  180. "NILFS: unable to write superblock (err=%d)\n", err);
  181. if (err == -EIO && nilfs->ns_sbh[1]) {
  182. nilfs_fall_back_super_block(nilfs);
  183. goto retry;
  184. }
  185. } else {
  186. struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
  187. /*
  188. * The latest segment becomes trailable from the position
  189. * written in superblock.
  190. */
  191. clear_nilfs_discontinued(nilfs);
  192. /* update GC protection for recent segments */
  193. if (nilfs->ns_sbh[1]) {
  194. sbp = NULL;
  195. if (dupsb) {
  196. set_buffer_dirty(nilfs->ns_sbh[1]);
  197. if (!sync_dirty_buffer(nilfs->ns_sbh[1]))
  198. sbp = nilfs->ns_sbp[1];
  199. }
  200. }
  201. if (sbp) {
  202. spin_lock(&nilfs->ns_last_segment_lock);
  203. nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
  204. spin_unlock(&nilfs->ns_last_segment_lock);
  205. }
  206. }
  207. return err;
  208. }
  209. int nilfs_commit_super(struct nilfs_sb_info *sbi, int dupsb)
  210. {
  211. struct the_nilfs *nilfs = sbi->s_nilfs;
  212. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  213. sector_t nfreeblocks;
  214. time_t t;
  215. int err;
  216. /* nilfs->sem must be locked by the caller. */
  217. if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
  218. if (sbp[1] && sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC))
  219. nilfs_swap_super_block(nilfs);
  220. else {
  221. printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
  222. sbi->s_super->s_id);
  223. return -EIO;
  224. }
  225. }
  226. err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
  227. if (unlikely(err)) {
  228. printk(KERN_ERR "NILFS: failed to count free blocks\n");
  229. return err;
  230. }
  231. spin_lock(&nilfs->ns_last_segment_lock);
  232. sbp[0]->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
  233. sbp[0]->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
  234. sbp[0]->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
  235. spin_unlock(&nilfs->ns_last_segment_lock);
  236. t = get_seconds();
  237. nilfs->ns_sbwtime[0] = t;
  238. sbp[0]->s_free_blocks_count = cpu_to_le64(nfreeblocks);
  239. sbp[0]->s_wtime = cpu_to_le64(t);
  240. sbp[0]->s_sum = 0;
  241. sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
  242. (unsigned char *)sbp[0],
  243. nilfs->ns_sbsize));
  244. if (dupsb && sbp[1]) {
  245. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  246. nilfs->ns_sbwtime[1] = t;
  247. }
  248. clear_nilfs_sb_dirty(nilfs);
  249. return nilfs_sync_super(sbi, dupsb);
  250. }
  251. static void nilfs_put_super(struct super_block *sb)
  252. {
  253. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  254. struct the_nilfs *nilfs = sbi->s_nilfs;
  255. lock_kernel();
  256. nilfs_detach_segment_constructor(sbi);
  257. if (!(sb->s_flags & MS_RDONLY)) {
  258. down_write(&nilfs->ns_sem);
  259. nilfs->ns_sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
  260. nilfs_commit_super(sbi, 1);
  261. up_write(&nilfs->ns_sem);
  262. }
  263. down_write(&nilfs->ns_super_sem);
  264. if (nilfs->ns_current == sbi)
  265. nilfs->ns_current = NULL;
  266. up_write(&nilfs->ns_super_sem);
  267. nilfs_detach_checkpoint(sbi);
  268. put_nilfs(sbi->s_nilfs);
  269. sbi->s_super = NULL;
  270. sb->s_fs_info = NULL;
  271. nilfs_put_sbinfo(sbi);
  272. unlock_kernel();
  273. }
  274. static int nilfs_sync_fs(struct super_block *sb, int wait)
  275. {
  276. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  277. struct the_nilfs *nilfs = sbi->s_nilfs;
  278. int err = 0;
  279. /* This function is called when super block should be written back */
  280. if (wait)
  281. err = nilfs_construct_segment(sb);
  282. down_write(&nilfs->ns_sem);
  283. if (nilfs_sb_dirty(nilfs))
  284. nilfs_commit_super(sbi, 1);
  285. up_write(&nilfs->ns_sem);
  286. return err;
  287. }
  288. int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno)
  289. {
  290. struct the_nilfs *nilfs = sbi->s_nilfs;
  291. struct nilfs_checkpoint *raw_cp;
  292. struct buffer_head *bh_cp;
  293. int err;
  294. down_write(&nilfs->ns_super_sem);
  295. list_add(&sbi->s_list, &nilfs->ns_supers);
  296. up_write(&nilfs->ns_super_sem);
  297. sbi->s_ifile = nilfs_ifile_new(sbi, nilfs->ns_inode_size);
  298. if (!sbi->s_ifile)
  299. return -ENOMEM;
  300. down_read(&nilfs->ns_segctor_sem);
  301. err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
  302. &bh_cp);
  303. up_read(&nilfs->ns_segctor_sem);
  304. if (unlikely(err)) {
  305. if (err == -ENOENT || err == -EINVAL) {
  306. printk(KERN_ERR
  307. "NILFS: Invalid checkpoint "
  308. "(checkpoint number=%llu)\n",
  309. (unsigned long long)cno);
  310. err = -EINVAL;
  311. }
  312. goto failed;
  313. }
  314. err = nilfs_read_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode);
  315. if (unlikely(err))
  316. goto failed_bh;
  317. atomic_set(&sbi->s_inodes_count, le64_to_cpu(raw_cp->cp_inodes_count));
  318. atomic_set(&sbi->s_blocks_count, le64_to_cpu(raw_cp->cp_blocks_count));
  319. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  320. return 0;
  321. failed_bh:
  322. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  323. failed:
  324. nilfs_mdt_destroy(sbi->s_ifile);
  325. sbi->s_ifile = NULL;
  326. down_write(&nilfs->ns_super_sem);
  327. list_del_init(&sbi->s_list);
  328. up_write(&nilfs->ns_super_sem);
  329. return err;
  330. }
  331. void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi)
  332. {
  333. struct the_nilfs *nilfs = sbi->s_nilfs;
  334. nilfs_mdt_destroy(sbi->s_ifile);
  335. sbi->s_ifile = NULL;
  336. down_write(&nilfs->ns_super_sem);
  337. list_del_init(&sbi->s_list);
  338. up_write(&nilfs->ns_super_sem);
  339. }
  340. static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  341. {
  342. struct super_block *sb = dentry->d_sb;
  343. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  344. struct the_nilfs *nilfs = sbi->s_nilfs;
  345. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  346. unsigned long long blocks;
  347. unsigned long overhead;
  348. unsigned long nrsvblocks;
  349. sector_t nfreeblocks;
  350. int err;
  351. /*
  352. * Compute all of the segment blocks
  353. *
  354. * The blocks before first segment and after last segment
  355. * are excluded.
  356. */
  357. blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
  358. - nilfs->ns_first_data_block;
  359. nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
  360. /*
  361. * Compute the overhead
  362. *
  363. * When distributing meta data blocks outside segment structure,
  364. * We must count them as the overhead.
  365. */
  366. overhead = 0;
  367. err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
  368. if (unlikely(err))
  369. return err;
  370. buf->f_type = NILFS_SUPER_MAGIC;
  371. buf->f_bsize = sb->s_blocksize;
  372. buf->f_blocks = blocks - overhead;
  373. buf->f_bfree = nfreeblocks;
  374. buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
  375. (buf->f_bfree - nrsvblocks) : 0;
  376. buf->f_files = atomic_read(&sbi->s_inodes_count);
  377. buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */
  378. buf->f_namelen = NILFS_NAME_LEN;
  379. buf->f_fsid.val[0] = (u32)id;
  380. buf->f_fsid.val[1] = (u32)(id >> 32);
  381. return 0;
  382. }
  383. static int nilfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
  384. {
  385. struct super_block *sb = vfs->mnt_sb;
  386. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  387. if (!nilfs_test_opt(sbi, BARRIER))
  388. seq_printf(seq, ",nobarrier");
  389. if (nilfs_test_opt(sbi, SNAPSHOT))
  390. seq_printf(seq, ",cp=%llu",
  391. (unsigned long long int)sbi->s_snapshot_cno);
  392. if (nilfs_test_opt(sbi, ERRORS_PANIC))
  393. seq_printf(seq, ",errors=panic");
  394. if (nilfs_test_opt(sbi, ERRORS_CONT))
  395. seq_printf(seq, ",errors=continue");
  396. if (nilfs_test_opt(sbi, STRICT_ORDER))
  397. seq_printf(seq, ",order=strict");
  398. if (nilfs_test_opt(sbi, NORECOVERY))
  399. seq_printf(seq, ",norecovery");
  400. if (nilfs_test_opt(sbi, DISCARD))
  401. seq_printf(seq, ",discard");
  402. return 0;
  403. }
  404. static const struct super_operations nilfs_sops = {
  405. .alloc_inode = nilfs_alloc_inode,
  406. .destroy_inode = nilfs_destroy_inode,
  407. .dirty_inode = nilfs_dirty_inode,
  408. /* .write_inode = nilfs_write_inode, */
  409. /* .put_inode = nilfs_put_inode, */
  410. /* .drop_inode = nilfs_drop_inode, */
  411. .delete_inode = nilfs_delete_inode,
  412. .put_super = nilfs_put_super,
  413. /* .write_super = nilfs_write_super, */
  414. .sync_fs = nilfs_sync_fs,
  415. /* .write_super_lockfs */
  416. /* .unlockfs */
  417. .statfs = nilfs_statfs,
  418. .remount_fs = nilfs_remount,
  419. .clear_inode = nilfs_clear_inode,
  420. /* .umount_begin */
  421. .show_options = nilfs_show_options
  422. };
  423. static struct inode *
  424. nilfs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation)
  425. {
  426. struct inode *inode;
  427. if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO &&
  428. ino != NILFS_SKETCH_INO)
  429. return ERR_PTR(-ESTALE);
  430. inode = nilfs_iget(sb, ino);
  431. if (IS_ERR(inode))
  432. return ERR_CAST(inode);
  433. if (generation && inode->i_generation != generation) {
  434. iput(inode);
  435. return ERR_PTR(-ESTALE);
  436. }
  437. return inode;
  438. }
  439. static struct dentry *
  440. nilfs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
  441. int fh_type)
  442. {
  443. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  444. nilfs_nfs_get_inode);
  445. }
  446. static struct dentry *
  447. nilfs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len,
  448. int fh_type)
  449. {
  450. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  451. nilfs_nfs_get_inode);
  452. }
  453. static const struct export_operations nilfs_export_ops = {
  454. .fh_to_dentry = nilfs_fh_to_dentry,
  455. .fh_to_parent = nilfs_fh_to_parent,
  456. .get_parent = nilfs_get_parent,
  457. };
  458. enum {
  459. Opt_err_cont, Opt_err_panic, Opt_err_ro,
  460. Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
  461. Opt_discard, Opt_err,
  462. };
  463. static match_table_t tokens = {
  464. {Opt_err_cont, "errors=continue"},
  465. {Opt_err_panic, "errors=panic"},
  466. {Opt_err_ro, "errors=remount-ro"},
  467. {Opt_nobarrier, "nobarrier"},
  468. {Opt_snapshot, "cp=%u"},
  469. {Opt_order, "order=%s"},
  470. {Opt_norecovery, "norecovery"},
  471. {Opt_discard, "discard"},
  472. {Opt_err, NULL}
  473. };
  474. static int parse_options(char *options, struct super_block *sb)
  475. {
  476. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  477. char *p;
  478. substring_t args[MAX_OPT_ARGS];
  479. int option;
  480. if (!options)
  481. return 1;
  482. while ((p = strsep(&options, ",")) != NULL) {
  483. int token;
  484. if (!*p)
  485. continue;
  486. token = match_token(p, tokens, args);
  487. switch (token) {
  488. case Opt_nobarrier:
  489. nilfs_clear_opt(sbi, BARRIER);
  490. break;
  491. case Opt_order:
  492. if (strcmp(args[0].from, "relaxed") == 0)
  493. /* Ordered data semantics */
  494. nilfs_clear_opt(sbi, STRICT_ORDER);
  495. else if (strcmp(args[0].from, "strict") == 0)
  496. /* Strict in-order semantics */
  497. nilfs_set_opt(sbi, STRICT_ORDER);
  498. else
  499. return 0;
  500. break;
  501. case Opt_err_panic:
  502. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC);
  503. break;
  504. case Opt_err_ro:
  505. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO);
  506. break;
  507. case Opt_err_cont:
  508. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT);
  509. break;
  510. case Opt_snapshot:
  511. if (match_int(&args[0], &option) || option <= 0)
  512. return 0;
  513. if (!(sb->s_flags & MS_RDONLY))
  514. return 0;
  515. sbi->s_snapshot_cno = option;
  516. nilfs_set_opt(sbi, SNAPSHOT);
  517. break;
  518. case Opt_norecovery:
  519. nilfs_set_opt(sbi, NORECOVERY);
  520. break;
  521. case Opt_discard:
  522. nilfs_set_opt(sbi, DISCARD);
  523. break;
  524. default:
  525. printk(KERN_ERR
  526. "NILFS: Unrecognized mount option \"%s\"\n", p);
  527. return 0;
  528. }
  529. }
  530. return 1;
  531. }
  532. static inline void
  533. nilfs_set_default_options(struct nilfs_sb_info *sbi,
  534. struct nilfs_super_block *sbp)
  535. {
  536. sbi->s_mount_opt =
  537. NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
  538. }
  539. static int nilfs_setup_super(struct nilfs_sb_info *sbi)
  540. {
  541. struct the_nilfs *nilfs = sbi->s_nilfs;
  542. struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
  543. int max_mnt_count = le16_to_cpu(sbp->s_max_mnt_count);
  544. int mnt_count = le16_to_cpu(sbp->s_mnt_count);
  545. /* nilfs->sem must be locked by the caller. */
  546. if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
  547. printk(KERN_WARNING
  548. "NILFS warning: mounting fs with errors\n");
  549. #if 0
  550. } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
  551. printk(KERN_WARNING
  552. "NILFS warning: maximal mount count reached\n");
  553. #endif
  554. }
  555. if (!max_mnt_count)
  556. sbp->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
  557. sbp->s_mnt_count = cpu_to_le16(mnt_count + 1);
  558. sbp->s_state = cpu_to_le16(le16_to_cpu(sbp->s_state) & ~NILFS_VALID_FS);
  559. sbp->s_mtime = cpu_to_le64(get_seconds());
  560. return nilfs_commit_super(sbi, 1);
  561. }
  562. struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
  563. u64 pos, int blocksize,
  564. struct buffer_head **pbh)
  565. {
  566. unsigned long long sb_index = pos;
  567. unsigned long offset;
  568. offset = do_div(sb_index, blocksize);
  569. *pbh = sb_bread(sb, sb_index);
  570. if (!*pbh)
  571. return NULL;
  572. return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
  573. }
  574. int nilfs_store_magic_and_option(struct super_block *sb,
  575. struct nilfs_super_block *sbp,
  576. char *data)
  577. {
  578. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  579. sb->s_magic = le16_to_cpu(sbp->s_magic);
  580. /* FS independent flags */
  581. #ifdef NILFS_ATIME_DISABLE
  582. sb->s_flags |= MS_NOATIME;
  583. #endif
  584. nilfs_set_default_options(sbi, sbp);
  585. sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid);
  586. sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid);
  587. sbi->s_interval = le32_to_cpu(sbp->s_c_interval);
  588. sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max);
  589. return !parse_options(data, sb) ? -EINVAL : 0 ;
  590. }
  591. /**
  592. * nilfs_fill_super() - initialize a super block instance
  593. * @sb: super_block
  594. * @data: mount options
  595. * @silent: silent mode flag
  596. * @nilfs: the_nilfs struct
  597. *
  598. * This function is called exclusively by nilfs->ns_mount_mutex.
  599. * So, the recovery process is protected from other simultaneous mounts.
  600. */
  601. static int
  602. nilfs_fill_super(struct super_block *sb, void *data, int silent,
  603. struct the_nilfs *nilfs)
  604. {
  605. struct nilfs_sb_info *sbi;
  606. struct inode *root;
  607. __u64 cno;
  608. int err;
  609. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  610. if (!sbi)
  611. return -ENOMEM;
  612. sb->s_fs_info = sbi;
  613. get_nilfs(nilfs);
  614. sbi->s_nilfs = nilfs;
  615. sbi->s_super = sb;
  616. atomic_set(&sbi->s_count, 1);
  617. err = init_nilfs(nilfs, sbi, (char *)data);
  618. if (err)
  619. goto failed_sbi;
  620. spin_lock_init(&sbi->s_inode_lock);
  621. INIT_LIST_HEAD(&sbi->s_dirty_files);
  622. INIT_LIST_HEAD(&sbi->s_list);
  623. /*
  624. * Following initialization is overlapped because
  625. * nilfs_sb_info structure has been cleared at the beginning.
  626. * But we reserve them to keep our interest and make ready
  627. * for the future change.
  628. */
  629. get_random_bytes(&sbi->s_next_generation,
  630. sizeof(sbi->s_next_generation));
  631. spin_lock_init(&sbi->s_next_gen_lock);
  632. sb->s_op = &nilfs_sops;
  633. sb->s_export_op = &nilfs_export_ops;
  634. sb->s_root = NULL;
  635. sb->s_time_gran = 1;
  636. sb->s_bdi = nilfs->ns_bdi;
  637. err = load_nilfs(nilfs, sbi);
  638. if (err)
  639. goto failed_sbi;
  640. cno = nilfs_last_cno(nilfs);
  641. if (sb->s_flags & MS_RDONLY) {
  642. if (nilfs_test_opt(sbi, SNAPSHOT)) {
  643. down_read(&nilfs->ns_segctor_sem);
  644. err = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile,
  645. sbi->s_snapshot_cno);
  646. up_read(&nilfs->ns_segctor_sem);
  647. if (err < 0) {
  648. if (err == -ENOENT)
  649. err = -EINVAL;
  650. goto failed_sbi;
  651. }
  652. if (!err) {
  653. printk(KERN_ERR
  654. "NILFS: The specified checkpoint is "
  655. "not a snapshot "
  656. "(checkpoint number=%llu).\n",
  657. (unsigned long long)sbi->s_snapshot_cno);
  658. err = -EINVAL;
  659. goto failed_sbi;
  660. }
  661. cno = sbi->s_snapshot_cno;
  662. }
  663. }
  664. err = nilfs_attach_checkpoint(sbi, cno);
  665. if (err) {
  666. printk(KERN_ERR "NILFS: error loading a checkpoint"
  667. " (checkpoint number=%llu).\n", (unsigned long long)cno);
  668. goto failed_sbi;
  669. }
  670. if (!(sb->s_flags & MS_RDONLY)) {
  671. err = nilfs_attach_segment_constructor(sbi);
  672. if (err)
  673. goto failed_checkpoint;
  674. }
  675. root = nilfs_iget(sb, NILFS_ROOT_INO);
  676. if (IS_ERR(root)) {
  677. printk(KERN_ERR "NILFS: get root inode failed\n");
  678. err = PTR_ERR(root);
  679. goto failed_segctor;
  680. }
  681. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  682. iput(root);
  683. printk(KERN_ERR "NILFS: corrupt root inode.\n");
  684. err = -EINVAL;
  685. goto failed_segctor;
  686. }
  687. sb->s_root = d_alloc_root(root);
  688. if (!sb->s_root) {
  689. iput(root);
  690. printk(KERN_ERR "NILFS: get root dentry failed\n");
  691. err = -ENOMEM;
  692. goto failed_segctor;
  693. }
  694. if (!(sb->s_flags & MS_RDONLY)) {
  695. down_write(&nilfs->ns_sem);
  696. nilfs_setup_super(sbi);
  697. up_write(&nilfs->ns_sem);
  698. }
  699. down_write(&nilfs->ns_super_sem);
  700. if (!nilfs_test_opt(sbi, SNAPSHOT))
  701. nilfs->ns_current = sbi;
  702. up_write(&nilfs->ns_super_sem);
  703. return 0;
  704. failed_segctor:
  705. nilfs_detach_segment_constructor(sbi);
  706. failed_checkpoint:
  707. nilfs_detach_checkpoint(sbi);
  708. failed_sbi:
  709. put_nilfs(nilfs);
  710. sb->s_fs_info = NULL;
  711. nilfs_put_sbinfo(sbi);
  712. return err;
  713. }
  714. static int nilfs_remount(struct super_block *sb, int *flags, char *data)
  715. {
  716. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  717. struct nilfs_super_block *sbp;
  718. struct the_nilfs *nilfs = sbi->s_nilfs;
  719. unsigned long old_sb_flags;
  720. struct nilfs_mount_options old_opts;
  721. int was_snapshot, err;
  722. lock_kernel();
  723. down_write(&nilfs->ns_super_sem);
  724. old_sb_flags = sb->s_flags;
  725. old_opts.mount_opt = sbi->s_mount_opt;
  726. old_opts.snapshot_cno = sbi->s_snapshot_cno;
  727. was_snapshot = nilfs_test_opt(sbi, SNAPSHOT);
  728. if (!parse_options(data, sb)) {
  729. err = -EINVAL;
  730. goto restore_opts;
  731. }
  732. sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
  733. err = -EINVAL;
  734. if (was_snapshot) {
  735. if (!(*flags & MS_RDONLY)) {
  736. printk(KERN_ERR "NILFS (device %s): cannot remount "
  737. "snapshot read/write.\n",
  738. sb->s_id);
  739. goto restore_opts;
  740. } else if (sbi->s_snapshot_cno != old_opts.snapshot_cno) {
  741. printk(KERN_ERR "NILFS (device %s): cannot "
  742. "remount to a different snapshot.\n",
  743. sb->s_id);
  744. goto restore_opts;
  745. }
  746. } else {
  747. if (nilfs_test_opt(sbi, SNAPSHOT)) {
  748. printk(KERN_ERR "NILFS (device %s): cannot change "
  749. "a regular mount to a snapshot.\n",
  750. sb->s_id);
  751. goto restore_opts;
  752. }
  753. }
  754. if (!nilfs_valid_fs(nilfs)) {
  755. printk(KERN_WARNING "NILFS (device %s): couldn't "
  756. "remount because the filesystem is in an "
  757. "incomplete recovery state.\n", sb->s_id);
  758. goto restore_opts;
  759. }
  760. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  761. goto out;
  762. if (*flags & MS_RDONLY) {
  763. /* Shutting down the segment constructor */
  764. nilfs_detach_segment_constructor(sbi);
  765. sb->s_flags |= MS_RDONLY;
  766. /*
  767. * Remounting a valid RW partition RDONLY, so set
  768. * the RDONLY flag and then mark the partition as valid again.
  769. */
  770. down_write(&nilfs->ns_sem);
  771. sbp = nilfs->ns_sbp[0];
  772. if (!(sbp->s_state & le16_to_cpu(NILFS_VALID_FS)) &&
  773. (nilfs->ns_mount_state & NILFS_VALID_FS))
  774. sbp->s_state = cpu_to_le16(nilfs->ns_mount_state);
  775. sbp->s_mtime = cpu_to_le64(get_seconds());
  776. nilfs_commit_super(sbi, 1);
  777. up_write(&nilfs->ns_sem);
  778. } else {
  779. /*
  780. * Mounting a RDONLY partition read-write, so reread and
  781. * store the current valid flag. (It may have been changed
  782. * by fsck since we originally mounted the partition.)
  783. */
  784. sb->s_flags &= ~MS_RDONLY;
  785. err = nilfs_attach_segment_constructor(sbi);
  786. if (err)
  787. goto restore_opts;
  788. down_write(&nilfs->ns_sem);
  789. nilfs_setup_super(sbi);
  790. up_write(&nilfs->ns_sem);
  791. }
  792. out:
  793. up_write(&nilfs->ns_super_sem);
  794. unlock_kernel();
  795. return 0;
  796. restore_opts:
  797. sb->s_flags = old_sb_flags;
  798. sbi->s_mount_opt = old_opts.mount_opt;
  799. sbi->s_snapshot_cno = old_opts.snapshot_cno;
  800. up_write(&nilfs->ns_super_sem);
  801. unlock_kernel();
  802. return err;
  803. }
  804. struct nilfs_super_data {
  805. struct block_device *bdev;
  806. struct nilfs_sb_info *sbi;
  807. __u64 cno;
  808. int flags;
  809. };
  810. /**
  811. * nilfs_identify - pre-read mount options needed to identify mount instance
  812. * @data: mount options
  813. * @sd: nilfs_super_data
  814. */
  815. static int nilfs_identify(char *data, struct nilfs_super_data *sd)
  816. {
  817. char *p, *options = data;
  818. substring_t args[MAX_OPT_ARGS];
  819. int option, token;
  820. int ret = 0;
  821. do {
  822. p = strsep(&options, ",");
  823. if (p != NULL && *p) {
  824. token = match_token(p, tokens, args);
  825. if (token == Opt_snapshot) {
  826. if (!(sd->flags & MS_RDONLY))
  827. ret++;
  828. else {
  829. ret = match_int(&args[0], &option);
  830. if (!ret) {
  831. if (option > 0)
  832. sd->cno = option;
  833. else
  834. ret++;
  835. }
  836. }
  837. }
  838. if (ret)
  839. printk(KERN_ERR
  840. "NILFS: invalid mount option: %s\n", p);
  841. }
  842. if (!options)
  843. break;
  844. BUG_ON(options == data);
  845. *(options - 1) = ',';
  846. } while (!ret);
  847. return ret;
  848. }
  849. static int nilfs_set_bdev_super(struct super_block *s, void *data)
  850. {
  851. struct nilfs_super_data *sd = data;
  852. s->s_bdev = sd->bdev;
  853. s->s_dev = s->s_bdev->bd_dev;
  854. return 0;
  855. }
  856. static int nilfs_test_bdev_super(struct super_block *s, void *data)
  857. {
  858. struct nilfs_super_data *sd = data;
  859. return sd->sbi && s->s_fs_info == (void *)sd->sbi;
  860. }
  861. static int
  862. nilfs_get_sb(struct file_system_type *fs_type, int flags,
  863. const char *dev_name, void *data, struct vfsmount *mnt)
  864. {
  865. struct nilfs_super_data sd;
  866. struct super_block *s;
  867. fmode_t mode = FMODE_READ;
  868. struct the_nilfs *nilfs;
  869. int err, need_to_close = 1;
  870. if (!(flags & MS_RDONLY))
  871. mode |= FMODE_WRITE;
  872. sd.bdev = open_bdev_exclusive(dev_name, mode, fs_type);
  873. if (IS_ERR(sd.bdev))
  874. return PTR_ERR(sd.bdev);
  875. /*
  876. * To get mount instance using sget() vfs-routine, NILFS needs
  877. * much more information than normal filesystems to identify mount
  878. * instance. For snapshot mounts, not only a mount type (ro-mount
  879. * or rw-mount) but also a checkpoint number is required.
  880. */
  881. sd.cno = 0;
  882. sd.flags = flags;
  883. if (nilfs_identify((char *)data, &sd)) {
  884. err = -EINVAL;
  885. goto failed;
  886. }
  887. nilfs = find_or_create_nilfs(sd.bdev);
  888. if (!nilfs) {
  889. err = -ENOMEM;
  890. goto failed;
  891. }
  892. mutex_lock(&nilfs->ns_mount_mutex);
  893. if (!sd.cno) {
  894. /*
  895. * Check if an exclusive mount exists or not.
  896. * Snapshot mounts coexist with a current mount
  897. * (i.e. rw-mount or ro-mount), whereas rw-mount and
  898. * ro-mount are mutually exclusive.
  899. */
  900. down_read(&nilfs->ns_super_sem);
  901. if (nilfs->ns_current &&
  902. ((nilfs->ns_current->s_super->s_flags ^ flags)
  903. & MS_RDONLY)) {
  904. up_read(&nilfs->ns_super_sem);
  905. err = -EBUSY;
  906. goto failed_unlock;
  907. }
  908. up_read(&nilfs->ns_super_sem);
  909. }
  910. /*
  911. * Find existing nilfs_sb_info struct
  912. */
  913. sd.sbi = nilfs_find_sbinfo(nilfs, !(flags & MS_RDONLY), sd.cno);
  914. /*
  915. * Get super block instance holding the nilfs_sb_info struct.
  916. * A new instance is allocated if no existing mount is present or
  917. * existing instance has been unmounted.
  918. */
  919. s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, &sd);
  920. if (sd.sbi)
  921. nilfs_put_sbinfo(sd.sbi);
  922. if (IS_ERR(s)) {
  923. err = PTR_ERR(s);
  924. goto failed_unlock;
  925. }
  926. if (!s->s_root) {
  927. char b[BDEVNAME_SIZE];
  928. /* New superblock instance created */
  929. s->s_flags = flags;
  930. s->s_mode = mode;
  931. strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id));
  932. sb_set_blocksize(s, block_size(sd.bdev));
  933. err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0,
  934. nilfs);
  935. if (err)
  936. goto cancel_new;
  937. s->s_flags |= MS_ACTIVE;
  938. need_to_close = 0;
  939. }
  940. mutex_unlock(&nilfs->ns_mount_mutex);
  941. put_nilfs(nilfs);
  942. if (need_to_close)
  943. close_bdev_exclusive(sd.bdev, mode);
  944. simple_set_mnt(mnt, s);
  945. return 0;
  946. failed_unlock:
  947. mutex_unlock(&nilfs->ns_mount_mutex);
  948. put_nilfs(nilfs);
  949. failed:
  950. close_bdev_exclusive(sd.bdev, mode);
  951. return err;
  952. cancel_new:
  953. /* Abandoning the newly allocated superblock */
  954. mutex_unlock(&nilfs->ns_mount_mutex);
  955. put_nilfs(nilfs);
  956. deactivate_locked_super(s);
  957. /*
  958. * deactivate_locked_super() invokes close_bdev_exclusive().
  959. * We must finish all post-cleaning before this call;
  960. * put_nilfs() needs the block device.
  961. */
  962. return err;
  963. }
  964. struct file_system_type nilfs_fs_type = {
  965. .owner = THIS_MODULE,
  966. .name = "nilfs2",
  967. .get_sb = nilfs_get_sb,
  968. .kill_sb = kill_block_super,
  969. .fs_flags = FS_REQUIRES_DEV,
  970. };
  971. static void nilfs_inode_init_once(void *obj)
  972. {
  973. struct nilfs_inode_info *ii = obj;
  974. INIT_LIST_HEAD(&ii->i_dirty);
  975. #ifdef CONFIG_NILFS_XATTR
  976. init_rwsem(&ii->xattr_sem);
  977. #endif
  978. nilfs_btnode_cache_init_once(&ii->i_btnode_cache);
  979. ii->i_bmap = (struct nilfs_bmap *)&ii->i_bmap_union;
  980. inode_init_once(&ii->vfs_inode);
  981. }
  982. static void nilfs_segbuf_init_once(void *obj)
  983. {
  984. memset(obj, 0, sizeof(struct nilfs_segment_buffer));
  985. }
  986. static void nilfs_destroy_cachep(void)
  987. {
  988. if (nilfs_inode_cachep)
  989. kmem_cache_destroy(nilfs_inode_cachep);
  990. if (nilfs_transaction_cachep)
  991. kmem_cache_destroy(nilfs_transaction_cachep);
  992. if (nilfs_segbuf_cachep)
  993. kmem_cache_destroy(nilfs_segbuf_cachep);
  994. if (nilfs_btree_path_cache)
  995. kmem_cache_destroy(nilfs_btree_path_cache);
  996. }
  997. static int __init nilfs_init_cachep(void)
  998. {
  999. nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
  1000. sizeof(struct nilfs_inode_info), 0,
  1001. SLAB_RECLAIM_ACCOUNT, nilfs_inode_init_once);
  1002. if (!nilfs_inode_cachep)
  1003. goto fail;
  1004. nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
  1005. sizeof(struct nilfs_transaction_info), 0,
  1006. SLAB_RECLAIM_ACCOUNT, NULL);
  1007. if (!nilfs_transaction_cachep)
  1008. goto fail;
  1009. nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
  1010. sizeof(struct nilfs_segment_buffer), 0,
  1011. SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
  1012. if (!nilfs_segbuf_cachep)
  1013. goto fail;
  1014. nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
  1015. sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
  1016. 0, 0, NULL);
  1017. if (!nilfs_btree_path_cache)
  1018. goto fail;
  1019. return 0;
  1020. fail:
  1021. nilfs_destroy_cachep();
  1022. return -ENOMEM;
  1023. }
  1024. static int __init init_nilfs_fs(void)
  1025. {
  1026. int err;
  1027. err = nilfs_init_cachep();
  1028. if (err)
  1029. goto fail;
  1030. err = register_filesystem(&nilfs_fs_type);
  1031. if (err)
  1032. goto free_cachep;
  1033. printk(KERN_INFO "NILFS version 2 loaded\n");
  1034. return 0;
  1035. free_cachep:
  1036. nilfs_destroy_cachep();
  1037. fail:
  1038. return err;
  1039. }
  1040. static void __exit exit_nilfs_fs(void)
  1041. {
  1042. nilfs_destroy_cachep();
  1043. unregister_filesystem(&nilfs_fs_type);
  1044. }
  1045. module_init(init_nilfs_fs)
  1046. module_exit(exit_nilfs_fs)