super.c 33 KB

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