super.c 30 KB

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