super.c 35 KB

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