super.c 33 KB

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