super.c 33 KB

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