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