super.c 33 KB

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