super.c 33 KB

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