super.c 33 KB

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