super.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * NILFS module and super block management.
  4. *
  5. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  6. *
  7. * Written by Ryusuke Konishi.
  8. */
  9. /*
  10. * linux/fs/ext2/super.c
  11. *
  12. * Copyright (C) 1992, 1993, 1994, 1995
  13. * Remy Card (card@masi.ibp.fr)
  14. * Laboratoire MASI - Institut Blaise Pascal
  15. * Universite Pierre et Marie Curie (Paris VI)
  16. *
  17. * from
  18. *
  19. * linux/fs/minix/inode.c
  20. *
  21. * Copyright (C) 1991, 1992 Linus Torvalds
  22. *
  23. * Big-endian to little-endian byte-swapping/bitmaps by
  24. * David S. Miller (davem@caip.rutgers.edu), 1995
  25. */
  26. #include <linux/module.h>
  27. #include <linux/string.h>
  28. #include <linux/slab.h>
  29. #include <linux/init.h>
  30. #include <linux/blkdev.h>
  31. #include <linux/crc32.h>
  32. #include <linux/vfs.h>
  33. #include <linux/writeback.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/mount.h>
  36. #include <linux/fs_context.h>
  37. #include <linux/fs_parser.h>
  38. #include "nilfs.h"
  39. #include "export.h"
  40. #include "mdt.h"
  41. #include "alloc.h"
  42. #include "btree.h"
  43. #include "btnode.h"
  44. #include "page.h"
  45. #include "cpfile.h"
  46. #include "sufile.h" /* nilfs_sufile_resize(), nilfs_sufile_set_alloc_range() */
  47. #include "ifile.h"
  48. #include "dat.h"
  49. #include "segment.h"
  50. #include "segbuf.h"
  51. MODULE_AUTHOR("NTT Corp.");
  52. MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
  53. "(NILFS)");
  54. MODULE_LICENSE("GPL");
  55. static struct kmem_cache *nilfs_inode_cachep;
  56. struct kmem_cache *nilfs_transaction_cachep;
  57. struct kmem_cache *nilfs_segbuf_cachep;
  58. struct kmem_cache *nilfs_btree_path_cache;
  59. static int nilfs_setup_super(struct super_block *sb, int is_mount);
  60. void __nilfs_msg(struct super_block *sb, const char *fmt, ...)
  61. {
  62. struct va_format vaf;
  63. va_list args;
  64. int level;
  65. va_start(args, fmt);
  66. level = printk_get_level(fmt);
  67. vaf.fmt = printk_skip_level(fmt);
  68. vaf.va = &args;
  69. if (sb)
  70. printk("%c%cNILFS (%s): %pV\n",
  71. KERN_SOH_ASCII, level, sb->s_id, &vaf);
  72. else
  73. printk("%c%cNILFS: %pV\n",
  74. KERN_SOH_ASCII, level, &vaf);
  75. va_end(args);
  76. }
  77. static void nilfs_set_error(struct super_block *sb)
  78. {
  79. struct the_nilfs *nilfs = sb->s_fs_info;
  80. struct nilfs_super_block **sbp;
  81. down_write(&nilfs->ns_sem);
  82. if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
  83. nilfs->ns_mount_state |= NILFS_ERROR_FS;
  84. sbp = nilfs_prepare_super(sb, 0);
  85. if (likely(sbp)) {
  86. sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
  87. if (sbp[1])
  88. sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
  89. nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
  90. }
  91. }
  92. up_write(&nilfs->ns_sem);
  93. }
  94. /**
  95. * __nilfs_error() - report failure condition on a filesystem
  96. * @sb: super block instance
  97. * @function: name of calling function
  98. * @fmt: format string for message to be output
  99. * @...: optional arguments to @fmt
  100. *
  101. * __nilfs_error() sets an ERROR_FS flag on the superblock as well as
  102. * reporting an error message. This function should be called when
  103. * NILFS detects incoherences or defects of meta data on disk.
  104. *
  105. * This implements the body of nilfs_error() macro. Normally,
  106. * nilfs_error() should be used. As for sustainable errors such as a
  107. * single-shot I/O error, nilfs_err() should be used instead.
  108. *
  109. * Callers should not add a trailing newline since this will do it.
  110. */
  111. void __nilfs_error(struct super_block *sb, const char *function,
  112. const char *fmt, ...)
  113. {
  114. struct the_nilfs *nilfs = sb->s_fs_info;
  115. struct va_format vaf;
  116. va_list args;
  117. va_start(args, fmt);
  118. vaf.fmt = fmt;
  119. vaf.va = &args;
  120. printk(KERN_CRIT "NILFS error (device %s): %s: %pV\n",
  121. sb->s_id, function, &vaf);
  122. va_end(args);
  123. if (!sb_rdonly(sb)) {
  124. nilfs_set_error(sb);
  125. if (nilfs_test_opt(nilfs, ERRORS_RO)) {
  126. printk(KERN_CRIT "Remounting filesystem read-only\n");
  127. sb->s_flags |= SB_RDONLY;
  128. }
  129. }
  130. if (nilfs_test_opt(nilfs, ERRORS_PANIC))
  131. panic("NILFS (device %s): panic forced after error\n",
  132. sb->s_id);
  133. }
  134. struct inode *nilfs_alloc_inode(struct super_block *sb)
  135. {
  136. struct nilfs_inode_info *ii;
  137. ii = alloc_inode_sb(sb, nilfs_inode_cachep, GFP_NOFS);
  138. if (!ii)
  139. return NULL;
  140. ii->i_bh = NULL;
  141. ii->i_state = 0;
  142. ii->i_type = 0;
  143. ii->i_cno = 0;
  144. ii->i_assoc_inode = NULL;
  145. ii->i_bmap = &ii->i_bmap_data;
  146. return &ii->vfs_inode;
  147. }
  148. static void nilfs_free_inode(struct inode *inode)
  149. {
  150. if (nilfs_is_metadata_file_inode(inode))
  151. nilfs_mdt_destroy(inode);
  152. kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
  153. }
  154. static int nilfs_sync_super(struct super_block *sb, int flag)
  155. {
  156. struct the_nilfs *nilfs = sb->s_fs_info;
  157. int err;
  158. retry:
  159. set_buffer_dirty(nilfs->ns_sbh[0]);
  160. if (nilfs_test_opt(nilfs, BARRIER)) {
  161. err = __sync_dirty_buffer(nilfs->ns_sbh[0],
  162. REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
  163. } else {
  164. err = sync_dirty_buffer(nilfs->ns_sbh[0]);
  165. }
  166. if (unlikely(err)) {
  167. nilfs_err(sb, "unable to write superblock: err=%d", err);
  168. if (err == -EIO && nilfs->ns_sbh[1]) {
  169. /*
  170. * sbp[0] points to newer log than sbp[1],
  171. * so copy sbp[0] to sbp[1] to take over sbp[0].
  172. */
  173. memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0],
  174. nilfs->ns_sbsize);
  175. nilfs_fall_back_super_block(nilfs);
  176. goto retry;
  177. }
  178. } else {
  179. struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
  180. nilfs->ns_sbwcount++;
  181. /*
  182. * The latest segment becomes trailable from the position
  183. * written in superblock.
  184. */
  185. clear_nilfs_discontinued(nilfs);
  186. /* update GC protection for recent segments */
  187. if (nilfs->ns_sbh[1]) {
  188. if (flag == NILFS_SB_COMMIT_ALL) {
  189. set_buffer_dirty(nilfs->ns_sbh[1]);
  190. if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
  191. goto out;
  192. }
  193. if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) <
  194. le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno))
  195. sbp = nilfs->ns_sbp[1];
  196. }
  197. spin_lock(&nilfs->ns_last_segment_lock);
  198. nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
  199. spin_unlock(&nilfs->ns_last_segment_lock);
  200. }
  201. out:
  202. return err;
  203. }
  204. void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
  205. struct the_nilfs *nilfs)
  206. {
  207. sector_t nfreeblocks;
  208. /* nilfs->ns_sem must be locked by the caller. */
  209. nilfs_count_free_blocks(nilfs, &nfreeblocks);
  210. sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
  211. spin_lock(&nilfs->ns_last_segment_lock);
  212. sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
  213. sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
  214. sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
  215. spin_unlock(&nilfs->ns_last_segment_lock);
  216. }
  217. struct nilfs_super_block **nilfs_prepare_super(struct super_block *sb,
  218. int flip)
  219. {
  220. struct the_nilfs *nilfs = sb->s_fs_info;
  221. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  222. /* nilfs->ns_sem must be locked by the caller. */
  223. if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
  224. if (sbp[1] &&
  225. sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
  226. memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
  227. } else {
  228. nilfs_crit(sb, "superblock broke");
  229. return NULL;
  230. }
  231. } else if (sbp[1] &&
  232. sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
  233. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  234. }
  235. if (flip && sbp[1])
  236. nilfs_swap_super_block(nilfs);
  237. return sbp;
  238. }
  239. int nilfs_commit_super(struct super_block *sb, int flag)
  240. {
  241. struct the_nilfs *nilfs = sb->s_fs_info;
  242. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  243. time64_t t;
  244. /* nilfs->ns_sem must be locked by the caller. */
  245. t = ktime_get_real_seconds();
  246. nilfs->ns_sbwtime = t;
  247. sbp[0]->s_wtime = cpu_to_le64(t);
  248. sbp[0]->s_sum = 0;
  249. sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
  250. (unsigned char *)sbp[0],
  251. nilfs->ns_sbsize));
  252. if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
  253. sbp[1]->s_wtime = sbp[0]->s_wtime;
  254. sbp[1]->s_sum = 0;
  255. sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
  256. (unsigned char *)sbp[1],
  257. nilfs->ns_sbsize));
  258. }
  259. clear_nilfs_sb_dirty(nilfs);
  260. nilfs->ns_flushed_device = 1;
  261. /* make sure store to ns_flushed_device cannot be reordered */
  262. smp_wmb();
  263. return nilfs_sync_super(sb, flag);
  264. }
  265. /**
  266. * nilfs_cleanup_super() - write filesystem state for cleanup
  267. * @sb: super block instance to be unmounted or degraded to read-only
  268. *
  269. * This function restores state flags in the on-disk super block.
  270. * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
  271. * filesystem was not clean previously.
  272. */
  273. int nilfs_cleanup_super(struct super_block *sb)
  274. {
  275. struct the_nilfs *nilfs = sb->s_fs_info;
  276. struct nilfs_super_block **sbp;
  277. int flag = NILFS_SB_COMMIT;
  278. int ret = -EIO;
  279. sbp = nilfs_prepare_super(sb, 0);
  280. if (sbp) {
  281. sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
  282. nilfs_set_log_cursor(sbp[0], nilfs);
  283. if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
  284. /*
  285. * make the "clean" flag also to the opposite
  286. * super block if both super blocks point to
  287. * the same checkpoint.
  288. */
  289. sbp[1]->s_state = sbp[0]->s_state;
  290. flag = NILFS_SB_COMMIT_ALL;
  291. }
  292. ret = nilfs_commit_super(sb, flag);
  293. }
  294. return ret;
  295. }
  296. /**
  297. * nilfs_move_2nd_super - relocate secondary super block
  298. * @sb: super block instance
  299. * @sb2off: new offset of the secondary super block (in bytes)
  300. */
  301. static int nilfs_move_2nd_super(struct super_block *sb, loff_t sb2off)
  302. {
  303. struct the_nilfs *nilfs = sb->s_fs_info;
  304. struct buffer_head *nsbh;
  305. struct nilfs_super_block *nsbp;
  306. sector_t blocknr, newblocknr;
  307. unsigned long offset;
  308. int sb2i; /* array index of the secondary superblock */
  309. int ret = 0;
  310. /* nilfs->ns_sem must be locked by the caller. */
  311. if (nilfs->ns_sbh[1] &&
  312. nilfs->ns_sbh[1]->b_blocknr > nilfs->ns_first_data_block) {
  313. sb2i = 1;
  314. blocknr = nilfs->ns_sbh[1]->b_blocknr;
  315. } else if (nilfs->ns_sbh[0]->b_blocknr > nilfs->ns_first_data_block) {
  316. sb2i = 0;
  317. blocknr = nilfs->ns_sbh[0]->b_blocknr;
  318. } else {
  319. sb2i = -1;
  320. blocknr = 0;
  321. }
  322. if (sb2i >= 0 && (u64)blocknr << nilfs->ns_blocksize_bits == sb2off)
  323. goto out; /* super block location is unchanged */
  324. /* Get new super block buffer */
  325. newblocknr = sb2off >> nilfs->ns_blocksize_bits;
  326. offset = sb2off & (nilfs->ns_blocksize - 1);
  327. nsbh = sb_getblk(sb, newblocknr);
  328. if (!nsbh) {
  329. nilfs_warn(sb,
  330. "unable to move secondary superblock to block %llu",
  331. (unsigned long long)newblocknr);
  332. ret = -EIO;
  333. goto out;
  334. }
  335. nsbp = (void *)nsbh->b_data + offset;
  336. lock_buffer(nsbh);
  337. if (sb2i >= 0) {
  338. /*
  339. * The position of the second superblock only changes by 4KiB,
  340. * which is larger than the maximum superblock data size
  341. * (= 1KiB), so there is no need to use memmove() to allow
  342. * overlap between source and destination.
  343. */
  344. memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
  345. /*
  346. * Zero fill after copy to avoid overwriting in case of move
  347. * within the same block.
  348. */
  349. memset(nsbh->b_data, 0, offset);
  350. memset((void *)nsbp + nilfs->ns_sbsize, 0,
  351. nsbh->b_size - offset - nilfs->ns_sbsize);
  352. } else {
  353. memset(nsbh->b_data, 0, nsbh->b_size);
  354. }
  355. set_buffer_uptodate(nsbh);
  356. unlock_buffer(nsbh);
  357. if (sb2i >= 0) {
  358. brelse(nilfs->ns_sbh[sb2i]);
  359. nilfs->ns_sbh[sb2i] = nsbh;
  360. nilfs->ns_sbp[sb2i] = nsbp;
  361. } else if (nilfs->ns_sbh[0]->b_blocknr < nilfs->ns_first_data_block) {
  362. /* secondary super block will be restored to index 1 */
  363. nilfs->ns_sbh[1] = nsbh;
  364. nilfs->ns_sbp[1] = nsbp;
  365. } else {
  366. brelse(nsbh);
  367. }
  368. out:
  369. return ret;
  370. }
  371. /**
  372. * nilfs_resize_fs - resize the filesystem
  373. * @sb: super block instance
  374. * @newsize: new size of the filesystem (in bytes)
  375. */
  376. int nilfs_resize_fs(struct super_block *sb, __u64 newsize)
  377. {
  378. struct the_nilfs *nilfs = sb->s_fs_info;
  379. struct nilfs_super_block **sbp;
  380. __u64 devsize, newnsegs;
  381. loff_t sb2off;
  382. int ret;
  383. ret = -ERANGE;
  384. devsize = bdev_nr_bytes(sb->s_bdev);
  385. if (newsize > devsize)
  386. goto out;
  387. /*
  388. * Prevent underflow in second superblock position calculation.
  389. * The exact minimum size check is done in nilfs_sufile_resize().
  390. */
  391. if (newsize < 4096) {
  392. ret = -ENOSPC;
  393. goto out;
  394. }
  395. /*
  396. * Write lock is required to protect some functions depending
  397. * on the number of segments, the number of reserved segments,
  398. * and so forth.
  399. */
  400. down_write(&nilfs->ns_segctor_sem);
  401. sb2off = NILFS_SB2_OFFSET_BYTES(newsize);
  402. newnsegs = sb2off >> nilfs->ns_blocksize_bits;
  403. newnsegs = div64_ul(newnsegs, nilfs->ns_blocks_per_segment);
  404. ret = nilfs_sufile_resize(nilfs->ns_sufile, newnsegs);
  405. up_write(&nilfs->ns_segctor_sem);
  406. if (ret < 0)
  407. goto out;
  408. ret = nilfs_construct_segment(sb);
  409. if (ret < 0)
  410. goto out;
  411. down_write(&nilfs->ns_sem);
  412. nilfs_move_2nd_super(sb, sb2off);
  413. ret = -EIO;
  414. sbp = nilfs_prepare_super(sb, 0);
  415. if (likely(sbp)) {
  416. nilfs_set_log_cursor(sbp[0], nilfs);
  417. /*
  418. * Drop NILFS_RESIZE_FS flag for compatibility with
  419. * mount-time resize which may be implemented in a
  420. * future release.
  421. */
  422. sbp[0]->s_state = cpu_to_le16(le16_to_cpu(sbp[0]->s_state) &
  423. ~NILFS_RESIZE_FS);
  424. sbp[0]->s_dev_size = cpu_to_le64(newsize);
  425. sbp[0]->s_nsegments = cpu_to_le64(nilfs->ns_nsegments);
  426. if (sbp[1])
  427. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  428. ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
  429. }
  430. up_write(&nilfs->ns_sem);
  431. /*
  432. * Reset the range of allocatable segments last. This order
  433. * is important in the case of expansion because the secondary
  434. * superblock must be protected from log write until migration
  435. * completes.
  436. */
  437. if (!ret)
  438. nilfs_sufile_set_alloc_range(nilfs->ns_sufile, 0, newnsegs - 1);
  439. out:
  440. return ret;
  441. }
  442. static void nilfs_put_super(struct super_block *sb)
  443. {
  444. struct the_nilfs *nilfs = sb->s_fs_info;
  445. nilfs_detach_log_writer(sb);
  446. if (!sb_rdonly(sb)) {
  447. down_write(&nilfs->ns_sem);
  448. nilfs_cleanup_super(sb);
  449. up_write(&nilfs->ns_sem);
  450. }
  451. nilfs_sysfs_delete_device_group(nilfs);
  452. iput(nilfs->ns_sufile);
  453. iput(nilfs->ns_cpfile);
  454. iput(nilfs->ns_dat);
  455. destroy_nilfs(nilfs);
  456. sb->s_fs_info = NULL;
  457. }
  458. static int nilfs_sync_fs(struct super_block *sb, int wait)
  459. {
  460. struct the_nilfs *nilfs = sb->s_fs_info;
  461. struct nilfs_super_block **sbp;
  462. int err = 0;
  463. /* This function is called when super block should be written back */
  464. if (wait)
  465. err = nilfs_construct_segment(sb);
  466. down_write(&nilfs->ns_sem);
  467. if (nilfs_sb_dirty(nilfs)) {
  468. sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
  469. if (likely(sbp)) {
  470. nilfs_set_log_cursor(sbp[0], nilfs);
  471. nilfs_commit_super(sb, NILFS_SB_COMMIT);
  472. }
  473. }
  474. up_write(&nilfs->ns_sem);
  475. if (!err)
  476. err = nilfs_flush_device(nilfs);
  477. return err;
  478. }
  479. int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
  480. struct nilfs_root **rootp)
  481. {
  482. struct the_nilfs *nilfs = sb->s_fs_info;
  483. struct nilfs_root *root;
  484. int err = -ENOMEM;
  485. root = nilfs_find_or_create_root(
  486. nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
  487. if (!root)
  488. return err;
  489. if (root->ifile)
  490. goto reuse; /* already attached checkpoint */
  491. down_read(&nilfs->ns_segctor_sem);
  492. err = nilfs_ifile_read(sb, root, cno, nilfs->ns_inode_size);
  493. up_read(&nilfs->ns_segctor_sem);
  494. if (unlikely(err))
  495. goto failed;
  496. reuse:
  497. *rootp = root;
  498. return 0;
  499. failed:
  500. if (err == -EINVAL)
  501. nilfs_err(sb, "Invalid checkpoint (checkpoint number=%llu)",
  502. (unsigned long long)cno);
  503. nilfs_put_root(root);
  504. return err;
  505. }
  506. static int nilfs_freeze(struct super_block *sb)
  507. {
  508. struct the_nilfs *nilfs = sb->s_fs_info;
  509. int err;
  510. if (sb_rdonly(sb))
  511. return 0;
  512. /* Mark super block clean */
  513. down_write(&nilfs->ns_sem);
  514. err = nilfs_cleanup_super(sb);
  515. up_write(&nilfs->ns_sem);
  516. return err;
  517. }
  518. static int nilfs_unfreeze(struct super_block *sb)
  519. {
  520. struct the_nilfs *nilfs = sb->s_fs_info;
  521. if (sb_rdonly(sb))
  522. return 0;
  523. down_write(&nilfs->ns_sem);
  524. nilfs_setup_super(sb, false);
  525. up_write(&nilfs->ns_sem);
  526. return 0;
  527. }
  528. static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  529. {
  530. struct super_block *sb = dentry->d_sb;
  531. struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
  532. struct the_nilfs *nilfs = root->nilfs;
  533. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  534. unsigned long long blocks;
  535. unsigned long overhead;
  536. unsigned long nrsvblocks;
  537. sector_t nfreeblocks;
  538. u64 nmaxinodes, nfreeinodes;
  539. int err;
  540. /*
  541. * Compute all of the segment blocks
  542. *
  543. * The blocks before first segment and after last segment
  544. * are excluded.
  545. */
  546. blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
  547. - nilfs->ns_first_data_block;
  548. nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
  549. /*
  550. * Compute the overhead
  551. *
  552. * When distributing meta data blocks outside segment structure,
  553. * We must count them as the overhead.
  554. */
  555. overhead = 0;
  556. err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
  557. if (unlikely(err))
  558. return err;
  559. err = nilfs_ifile_count_free_inodes(root->ifile,
  560. &nmaxinodes, &nfreeinodes);
  561. if (unlikely(err)) {
  562. nilfs_warn(sb, "failed to count free inodes: err=%d", err);
  563. if (err == -ERANGE) {
  564. /*
  565. * If nilfs_palloc_count_max_entries() returns
  566. * -ERANGE error code then we simply treat
  567. * curent inodes count as maximum possible and
  568. * zero as free inodes value.
  569. */
  570. nmaxinodes = atomic64_read(&root->inodes_count);
  571. nfreeinodes = 0;
  572. err = 0;
  573. } else
  574. return err;
  575. }
  576. buf->f_type = NILFS_SUPER_MAGIC;
  577. buf->f_bsize = sb->s_blocksize;
  578. buf->f_blocks = blocks - overhead;
  579. buf->f_bfree = nfreeblocks;
  580. buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
  581. (buf->f_bfree - nrsvblocks) : 0;
  582. buf->f_files = nmaxinodes;
  583. buf->f_ffree = nfreeinodes;
  584. buf->f_namelen = NILFS_NAME_LEN;
  585. buf->f_fsid = u64_to_fsid(id);
  586. return 0;
  587. }
  588. static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
  589. {
  590. struct super_block *sb = dentry->d_sb;
  591. struct the_nilfs *nilfs = sb->s_fs_info;
  592. struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
  593. if (!nilfs_test_opt(nilfs, BARRIER))
  594. seq_puts(seq, ",nobarrier");
  595. if (root->cno != NILFS_CPTREE_CURRENT_CNO)
  596. seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
  597. if (nilfs_test_opt(nilfs, ERRORS_PANIC))
  598. seq_puts(seq, ",errors=panic");
  599. if (nilfs_test_opt(nilfs, ERRORS_CONT))
  600. seq_puts(seq, ",errors=continue");
  601. if (nilfs_test_opt(nilfs, STRICT_ORDER))
  602. seq_puts(seq, ",order=strict");
  603. if (nilfs_test_opt(nilfs, NORECOVERY))
  604. seq_puts(seq, ",norecovery");
  605. if (nilfs_test_opt(nilfs, DISCARD))
  606. seq_puts(seq, ",discard");
  607. return 0;
  608. }
  609. static const struct super_operations nilfs_sops = {
  610. .alloc_inode = nilfs_alloc_inode,
  611. .free_inode = nilfs_free_inode,
  612. .dirty_inode = nilfs_dirty_inode,
  613. .evict_inode = nilfs_evict_inode,
  614. .put_super = nilfs_put_super,
  615. .sync_fs = nilfs_sync_fs,
  616. .freeze_fs = nilfs_freeze,
  617. .unfreeze_fs = nilfs_unfreeze,
  618. .statfs = nilfs_statfs,
  619. .show_options = nilfs_show_options
  620. };
  621. enum {
  622. Opt_err, Opt_barrier, Opt_snapshot, Opt_order, Opt_norecovery,
  623. Opt_discard,
  624. };
  625. static const struct constant_table nilfs_param_err[] = {
  626. {"continue", NILFS_MOUNT_ERRORS_CONT},
  627. {"panic", NILFS_MOUNT_ERRORS_PANIC},
  628. {"remount-ro", NILFS_MOUNT_ERRORS_RO},
  629. {}
  630. };
  631. static const struct fs_parameter_spec nilfs_param_spec[] = {
  632. fsparam_enum ("errors", Opt_err, nilfs_param_err),
  633. fsparam_flag_no ("barrier", Opt_barrier),
  634. fsparam_u64 ("cp", Opt_snapshot),
  635. fsparam_string ("order", Opt_order),
  636. fsparam_flag ("norecovery", Opt_norecovery),
  637. fsparam_flag_no ("discard", Opt_discard),
  638. {}
  639. };
  640. struct nilfs_fs_context {
  641. unsigned long ns_mount_opt;
  642. __u64 cno;
  643. };
  644. static int nilfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
  645. {
  646. struct nilfs_fs_context *nilfs = fc->fs_private;
  647. int is_remount = fc->purpose == FS_CONTEXT_FOR_RECONFIGURE;
  648. struct fs_parse_result result;
  649. int opt;
  650. opt = fs_parse(fc, nilfs_param_spec, param, &result);
  651. if (opt < 0)
  652. return opt;
  653. switch (opt) {
  654. case Opt_barrier:
  655. if (result.negated)
  656. nilfs_clear_opt(nilfs, BARRIER);
  657. else
  658. nilfs_set_opt(nilfs, BARRIER);
  659. break;
  660. case Opt_order:
  661. if (strcmp(param->string, "relaxed") == 0)
  662. /* Ordered data semantics */
  663. nilfs_clear_opt(nilfs, STRICT_ORDER);
  664. else if (strcmp(param->string, "strict") == 0)
  665. /* Strict in-order semantics */
  666. nilfs_set_opt(nilfs, STRICT_ORDER);
  667. else
  668. return -EINVAL;
  669. break;
  670. case Opt_err:
  671. nilfs->ns_mount_opt &= ~NILFS_MOUNT_ERROR_MODE;
  672. nilfs->ns_mount_opt |= result.uint_32;
  673. break;
  674. case Opt_snapshot:
  675. if (is_remount) {
  676. struct super_block *sb = fc->root->d_sb;
  677. nilfs_err(sb,
  678. "\"%s\" option is invalid for remount",
  679. param->key);
  680. return -EINVAL;
  681. }
  682. if (result.uint_64 == 0) {
  683. nilfs_err(NULL,
  684. "invalid option \"cp=0\": invalid checkpoint number 0");
  685. return -EINVAL;
  686. }
  687. nilfs->cno = result.uint_64;
  688. break;
  689. case Opt_norecovery:
  690. nilfs_set_opt(nilfs, NORECOVERY);
  691. break;
  692. case Opt_discard:
  693. if (result.negated)
  694. nilfs_clear_opt(nilfs, DISCARD);
  695. else
  696. nilfs_set_opt(nilfs, DISCARD);
  697. break;
  698. default:
  699. return -EINVAL;
  700. }
  701. return 0;
  702. }
  703. static int nilfs_setup_super(struct super_block *sb, int is_mount)
  704. {
  705. struct the_nilfs *nilfs = sb->s_fs_info;
  706. struct nilfs_super_block **sbp;
  707. int max_mnt_count;
  708. int mnt_count;
  709. /* nilfs->ns_sem must be locked by the caller. */
  710. sbp = nilfs_prepare_super(sb, 0);
  711. if (!sbp)
  712. return -EIO;
  713. if (!is_mount)
  714. goto skip_mount_setup;
  715. max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
  716. mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
  717. if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
  718. nilfs_warn(sb, "mounting fs with errors");
  719. #if 0
  720. } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
  721. nilfs_warn(sb, "maximal mount count reached");
  722. #endif
  723. }
  724. if (!max_mnt_count)
  725. sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
  726. sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
  727. sbp[0]->s_mtime = cpu_to_le64(ktime_get_real_seconds());
  728. skip_mount_setup:
  729. sbp[0]->s_state =
  730. cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
  731. /* synchronize sbp[1] with sbp[0] */
  732. if (sbp[1])
  733. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  734. return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
  735. }
  736. struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
  737. u64 pos, int blocksize,
  738. struct buffer_head **pbh)
  739. {
  740. unsigned long long sb_index = pos;
  741. unsigned long offset;
  742. offset = do_div(sb_index, blocksize);
  743. *pbh = sb_bread(sb, sb_index);
  744. if (!*pbh)
  745. return NULL;
  746. return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
  747. }
  748. int nilfs_store_magic(struct super_block *sb,
  749. struct nilfs_super_block *sbp)
  750. {
  751. struct the_nilfs *nilfs = sb->s_fs_info;
  752. sb->s_magic = le16_to_cpu(sbp->s_magic);
  753. /* FS independent flags */
  754. #ifdef NILFS_ATIME_DISABLE
  755. sb->s_flags |= SB_NOATIME;
  756. #endif
  757. nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
  758. nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
  759. nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
  760. nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
  761. return 0;
  762. }
  763. int nilfs_check_feature_compatibility(struct super_block *sb,
  764. struct nilfs_super_block *sbp)
  765. {
  766. __u64 features;
  767. features = le64_to_cpu(sbp->s_feature_incompat) &
  768. ~NILFS_FEATURE_INCOMPAT_SUPP;
  769. if (features) {
  770. nilfs_err(sb,
  771. "couldn't mount because of unsupported optional features (%llx)",
  772. (unsigned long long)features);
  773. return -EINVAL;
  774. }
  775. features = le64_to_cpu(sbp->s_feature_compat_ro) &
  776. ~NILFS_FEATURE_COMPAT_RO_SUPP;
  777. if (!sb_rdonly(sb) && features) {
  778. nilfs_err(sb,
  779. "couldn't mount RDWR because of unsupported optional features (%llx)",
  780. (unsigned long long)features);
  781. return -EINVAL;
  782. }
  783. return 0;
  784. }
  785. static int nilfs_get_root_dentry(struct super_block *sb,
  786. struct nilfs_root *root,
  787. struct dentry **root_dentry)
  788. {
  789. struct inode *inode;
  790. struct dentry *dentry;
  791. int ret = 0;
  792. inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
  793. if (IS_ERR(inode)) {
  794. ret = PTR_ERR(inode);
  795. nilfs_err(sb, "error %d getting root inode", ret);
  796. goto out;
  797. }
  798. if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
  799. iput(inode);
  800. nilfs_err(sb, "corrupt root inode");
  801. ret = -EINVAL;
  802. goto out;
  803. }
  804. if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
  805. dentry = d_find_alias(inode);
  806. if (!dentry) {
  807. dentry = d_make_root(inode);
  808. if (!dentry) {
  809. ret = -ENOMEM;
  810. goto failed_dentry;
  811. }
  812. } else {
  813. iput(inode);
  814. }
  815. } else {
  816. dentry = d_obtain_root(inode);
  817. if (IS_ERR(dentry)) {
  818. ret = PTR_ERR(dentry);
  819. goto failed_dentry;
  820. }
  821. }
  822. *root_dentry = dentry;
  823. out:
  824. return ret;
  825. failed_dentry:
  826. nilfs_err(sb, "error %d getting root dentry", ret);
  827. goto out;
  828. }
  829. static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
  830. struct dentry **root_dentry)
  831. {
  832. struct the_nilfs *nilfs = s->s_fs_info;
  833. struct nilfs_root *root;
  834. int ret;
  835. mutex_lock(&nilfs->ns_snapshot_mount_mutex);
  836. down_read(&nilfs->ns_segctor_sem);
  837. ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
  838. up_read(&nilfs->ns_segctor_sem);
  839. if (ret < 0) {
  840. ret = (ret == -ENOENT) ? -EINVAL : ret;
  841. goto out;
  842. } else if (!ret) {
  843. nilfs_err(s,
  844. "The specified checkpoint is not a snapshot (checkpoint number=%llu)",
  845. (unsigned long long)cno);
  846. ret = -EINVAL;
  847. goto out;
  848. }
  849. ret = nilfs_attach_checkpoint(s, cno, false, &root);
  850. if (ret) {
  851. nilfs_err(s,
  852. "error %d while loading snapshot (checkpoint number=%llu)",
  853. ret, (unsigned long long)cno);
  854. goto out;
  855. }
  856. ret = nilfs_get_root_dentry(s, root, root_dentry);
  857. nilfs_put_root(root);
  858. out:
  859. mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
  860. return ret;
  861. }
  862. /**
  863. * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
  864. * @root_dentry: root dentry of the tree to be shrunk
  865. *
  866. * This function returns true if the tree was in-use.
  867. */
  868. static bool nilfs_tree_is_busy(struct dentry *root_dentry)
  869. {
  870. shrink_dcache_parent(root_dentry);
  871. return d_count(root_dentry) > 1;
  872. }
  873. int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
  874. {
  875. struct the_nilfs *nilfs = sb->s_fs_info;
  876. struct nilfs_root *root;
  877. struct inode *inode;
  878. struct dentry *dentry;
  879. int ret;
  880. if (cno > nilfs->ns_cno)
  881. return false;
  882. if (cno >= nilfs_last_cno(nilfs))
  883. return true; /* protect recent checkpoints */
  884. ret = false;
  885. root = nilfs_lookup_root(nilfs, cno);
  886. if (root) {
  887. inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
  888. if (inode) {
  889. dentry = d_find_alias(inode);
  890. if (dentry) {
  891. ret = nilfs_tree_is_busy(dentry);
  892. dput(dentry);
  893. }
  894. iput(inode);
  895. }
  896. nilfs_put_root(root);
  897. }
  898. return ret;
  899. }
  900. /**
  901. * nilfs_fill_super() - initialize a super block instance
  902. * @sb: super_block
  903. * @fc: filesystem context
  904. *
  905. * This function is called exclusively by nilfs->ns_mount_mutex.
  906. * So, the recovery process is protected from other simultaneous mounts.
  907. */
  908. static int
  909. nilfs_fill_super(struct super_block *sb, struct fs_context *fc)
  910. {
  911. struct the_nilfs *nilfs;
  912. struct nilfs_root *fsroot;
  913. struct nilfs_fs_context *ctx = fc->fs_private;
  914. __u64 cno;
  915. int err;
  916. nilfs = alloc_nilfs(sb);
  917. if (!nilfs)
  918. return -ENOMEM;
  919. sb->s_fs_info = nilfs;
  920. err = init_nilfs(nilfs, sb);
  921. if (err)
  922. goto failed_nilfs;
  923. /* Copy in parsed mount options */
  924. nilfs->ns_mount_opt = ctx->ns_mount_opt;
  925. sb->s_op = &nilfs_sops;
  926. sb->s_export_op = &nilfs_export_ops;
  927. sb->s_root = NULL;
  928. sb->s_time_gran = 1;
  929. sb->s_max_links = NILFS_LINK_MAX;
  930. sb->s_bdi = bdi_get(sb->s_bdev->bd_disk->bdi);
  931. err = load_nilfs(nilfs, sb);
  932. if (err)
  933. goto failed_nilfs;
  934. super_set_uuid(sb, nilfs->ns_sbp[0]->s_uuid,
  935. sizeof(nilfs->ns_sbp[0]->s_uuid));
  936. super_set_sysfs_name_bdev(sb);
  937. cno = nilfs_last_cno(nilfs);
  938. err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
  939. if (err) {
  940. nilfs_err(sb,
  941. "error %d while loading last checkpoint (checkpoint number=%llu)",
  942. err, (unsigned long long)cno);
  943. goto failed_unload;
  944. }
  945. if (!sb_rdonly(sb)) {
  946. err = nilfs_attach_log_writer(sb, fsroot);
  947. if (err)
  948. goto failed_checkpoint;
  949. }
  950. err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
  951. if (err)
  952. goto failed_segctor;
  953. nilfs_put_root(fsroot);
  954. if (!sb_rdonly(sb)) {
  955. down_write(&nilfs->ns_sem);
  956. nilfs_setup_super(sb, true);
  957. up_write(&nilfs->ns_sem);
  958. }
  959. return 0;
  960. failed_segctor:
  961. nilfs_detach_log_writer(sb);
  962. failed_checkpoint:
  963. nilfs_put_root(fsroot);
  964. failed_unload:
  965. nilfs_sysfs_delete_device_group(nilfs);
  966. iput(nilfs->ns_sufile);
  967. iput(nilfs->ns_cpfile);
  968. iput(nilfs->ns_dat);
  969. failed_nilfs:
  970. destroy_nilfs(nilfs);
  971. return err;
  972. }
  973. static int nilfs_reconfigure(struct fs_context *fc)
  974. {
  975. struct nilfs_fs_context *ctx = fc->fs_private;
  976. struct super_block *sb = fc->root->d_sb;
  977. struct the_nilfs *nilfs = sb->s_fs_info;
  978. int err;
  979. sync_filesystem(sb);
  980. err = -EINVAL;
  981. if (!nilfs_valid_fs(nilfs)) {
  982. nilfs_warn(sb,
  983. "couldn't remount because the filesystem is in an incomplete recovery state");
  984. goto ignore_opts;
  985. }
  986. if ((bool)(fc->sb_flags & SB_RDONLY) == sb_rdonly(sb))
  987. goto out;
  988. if (fc->sb_flags & SB_RDONLY) {
  989. sb->s_flags |= SB_RDONLY;
  990. /*
  991. * Remounting a valid RW partition RDONLY, so set
  992. * the RDONLY flag and then mark the partition as valid again.
  993. */
  994. down_write(&nilfs->ns_sem);
  995. nilfs_cleanup_super(sb);
  996. up_write(&nilfs->ns_sem);
  997. } else {
  998. __u64 features;
  999. struct nilfs_root *root;
  1000. /*
  1001. * Mounting a RDONLY partition read-write, so reread and
  1002. * store the current valid flag. (It may have been changed
  1003. * by fsck since we originally mounted the partition.)
  1004. */
  1005. down_read(&nilfs->ns_sem);
  1006. features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
  1007. ~NILFS_FEATURE_COMPAT_RO_SUPP;
  1008. up_read(&nilfs->ns_sem);
  1009. if (features) {
  1010. nilfs_warn(sb,
  1011. "couldn't remount RDWR because of unsupported optional features (%llx)",
  1012. (unsigned long long)features);
  1013. err = -EROFS;
  1014. goto ignore_opts;
  1015. }
  1016. sb->s_flags &= ~SB_RDONLY;
  1017. root = NILFS_I(d_inode(sb->s_root))->i_root;
  1018. err = nilfs_attach_log_writer(sb, root);
  1019. if (err) {
  1020. sb->s_flags |= SB_RDONLY;
  1021. goto ignore_opts;
  1022. }
  1023. down_write(&nilfs->ns_sem);
  1024. nilfs_setup_super(sb, true);
  1025. up_write(&nilfs->ns_sem);
  1026. }
  1027. out:
  1028. sb->s_flags = (sb->s_flags & ~SB_POSIXACL);
  1029. /* Copy over parsed remount options */
  1030. nilfs->ns_mount_opt = ctx->ns_mount_opt;
  1031. return 0;
  1032. ignore_opts:
  1033. return err;
  1034. }
  1035. static int
  1036. nilfs_get_tree(struct fs_context *fc)
  1037. {
  1038. struct nilfs_fs_context *ctx = fc->fs_private;
  1039. struct super_block *s;
  1040. dev_t dev;
  1041. int err;
  1042. if (ctx->cno && !(fc->sb_flags & SB_RDONLY)) {
  1043. nilfs_err(NULL,
  1044. "invalid option \"cp=%llu\": read-only option is not specified",
  1045. ctx->cno);
  1046. return -EINVAL;
  1047. }
  1048. err = lookup_bdev(fc->source, &dev);
  1049. if (err)
  1050. return err;
  1051. s = sget_dev(fc, dev);
  1052. if (IS_ERR(s))
  1053. return PTR_ERR(s);
  1054. if (!s->s_root) {
  1055. err = setup_bdev_super(s, fc->sb_flags, fc);
  1056. if (!err)
  1057. err = nilfs_fill_super(s, fc);
  1058. if (err)
  1059. goto failed_super;
  1060. s->s_flags |= SB_ACTIVE;
  1061. } else if (!ctx->cno) {
  1062. if (nilfs_tree_is_busy(s->s_root)) {
  1063. if ((fc->sb_flags ^ s->s_flags) & SB_RDONLY) {
  1064. nilfs_err(s,
  1065. "the device already has a %s mount.",
  1066. sb_rdonly(s) ? "read-only" : "read/write");
  1067. err = -EBUSY;
  1068. goto failed_super;
  1069. }
  1070. } else {
  1071. /*
  1072. * Try reconfigure to setup mount states if the current
  1073. * tree is not mounted and only snapshots use this sb.
  1074. *
  1075. * Since nilfs_reconfigure() requires fc->root to be
  1076. * set, set it first and release it on failure.
  1077. */
  1078. fc->root = dget(s->s_root);
  1079. err = nilfs_reconfigure(fc);
  1080. if (err) {
  1081. dput(fc->root);
  1082. fc->root = NULL; /* prevent double release */
  1083. goto failed_super;
  1084. }
  1085. return 0;
  1086. }
  1087. }
  1088. if (ctx->cno) {
  1089. struct dentry *root_dentry;
  1090. err = nilfs_attach_snapshot(s, ctx->cno, &root_dentry);
  1091. if (err)
  1092. goto failed_super;
  1093. fc->root = root_dentry;
  1094. return 0;
  1095. }
  1096. fc->root = dget(s->s_root);
  1097. return 0;
  1098. failed_super:
  1099. deactivate_locked_super(s);
  1100. return err;
  1101. }
  1102. static void nilfs_free_fc(struct fs_context *fc)
  1103. {
  1104. kfree(fc->fs_private);
  1105. }
  1106. static const struct fs_context_operations nilfs_context_ops = {
  1107. .parse_param = nilfs_parse_param,
  1108. .get_tree = nilfs_get_tree,
  1109. .reconfigure = nilfs_reconfigure,
  1110. .free = nilfs_free_fc,
  1111. };
  1112. static int nilfs_init_fs_context(struct fs_context *fc)
  1113. {
  1114. struct nilfs_fs_context *ctx;
  1115. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1116. if (!ctx)
  1117. return -ENOMEM;
  1118. ctx->ns_mount_opt = NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
  1119. fc->fs_private = ctx;
  1120. fc->ops = &nilfs_context_ops;
  1121. return 0;
  1122. }
  1123. struct file_system_type nilfs_fs_type = {
  1124. .owner = THIS_MODULE,
  1125. .name = "nilfs2",
  1126. .kill_sb = kill_block_super,
  1127. .fs_flags = FS_REQUIRES_DEV,
  1128. .init_fs_context = nilfs_init_fs_context,
  1129. .parameters = nilfs_param_spec,
  1130. };
  1131. MODULE_ALIAS_FS("nilfs2");
  1132. static void nilfs_inode_init_once(void *obj)
  1133. {
  1134. struct nilfs_inode_info *ii = obj;
  1135. INIT_LIST_HEAD(&ii->i_dirty);
  1136. #ifdef CONFIG_NILFS_XATTR
  1137. init_rwsem(&ii->xattr_sem);
  1138. #endif
  1139. inode_init_once(&ii->vfs_inode);
  1140. }
  1141. static void nilfs_segbuf_init_once(void *obj)
  1142. {
  1143. memset(obj, 0, sizeof(struct nilfs_segment_buffer));
  1144. }
  1145. static void nilfs_destroy_cachep(void)
  1146. {
  1147. /*
  1148. * Make sure all delayed rcu free inodes are flushed before we
  1149. * destroy cache.
  1150. */
  1151. rcu_barrier();
  1152. kmem_cache_destroy(nilfs_inode_cachep);
  1153. kmem_cache_destroy(nilfs_transaction_cachep);
  1154. kmem_cache_destroy(nilfs_segbuf_cachep);
  1155. kmem_cache_destroy(nilfs_btree_path_cache);
  1156. }
  1157. static int __init nilfs_init_cachep(void)
  1158. {
  1159. nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
  1160. sizeof(struct nilfs_inode_info), 0,
  1161. SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
  1162. nilfs_inode_init_once);
  1163. if (!nilfs_inode_cachep)
  1164. goto fail;
  1165. nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
  1166. sizeof(struct nilfs_transaction_info), 0,
  1167. SLAB_RECLAIM_ACCOUNT, NULL);
  1168. if (!nilfs_transaction_cachep)
  1169. goto fail;
  1170. nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
  1171. sizeof(struct nilfs_segment_buffer), 0,
  1172. SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
  1173. if (!nilfs_segbuf_cachep)
  1174. goto fail;
  1175. nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
  1176. sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
  1177. 0, 0, NULL);
  1178. if (!nilfs_btree_path_cache)
  1179. goto fail;
  1180. return 0;
  1181. fail:
  1182. nilfs_destroy_cachep();
  1183. return -ENOMEM;
  1184. }
  1185. static int __init init_nilfs_fs(void)
  1186. {
  1187. int err;
  1188. err = nilfs_init_cachep();
  1189. if (err)
  1190. goto fail;
  1191. err = nilfs_sysfs_init();
  1192. if (err)
  1193. goto free_cachep;
  1194. err = register_filesystem(&nilfs_fs_type);
  1195. if (err)
  1196. goto deinit_sysfs_entry;
  1197. printk(KERN_INFO "NILFS version 2 loaded\n");
  1198. return 0;
  1199. deinit_sysfs_entry:
  1200. nilfs_sysfs_exit();
  1201. free_cachep:
  1202. nilfs_destroy_cachep();
  1203. fail:
  1204. return err;
  1205. }
  1206. static void __exit exit_nilfs_fs(void)
  1207. {
  1208. nilfs_destroy_cachep();
  1209. nilfs_sysfs_exit();
  1210. unregister_filesystem(&nilfs_fs_type);
  1211. }
  1212. module_init(init_nilfs_fs)
  1213. module_exit(exit_nilfs_fs)