super.c 33 KB

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