super.c 30 KB

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