super.c 36 KB

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