super.c 34 KB

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