super.c 34 KB

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