super.c 34 KB

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