super.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  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. static void nilfs_set_error(struct super_block *sb)
  70. {
  71. struct the_nilfs *nilfs = sb->s_fs_info;
  72. struct nilfs_super_block **sbp;
  73. down_write(&nilfs->ns_sem);
  74. if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
  75. nilfs->ns_mount_state |= NILFS_ERROR_FS;
  76. sbp = nilfs_prepare_super(sb, 0);
  77. if (likely(sbp)) {
  78. sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
  79. if (sbp[1])
  80. sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
  81. nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
  82. }
  83. }
  84. up_write(&nilfs->ns_sem);
  85. }
  86. /**
  87. * nilfs_error() - report failure condition on a filesystem
  88. *
  89. * nilfs_error() sets an ERROR_FS flag on the superblock as well as
  90. * reporting an error message. It should be called when NILFS detects
  91. * incoherences or defects of meta data on disk. As for sustainable
  92. * errors such as a single-shot I/O error, nilfs_warning() or the printk()
  93. * function should be used instead.
  94. *
  95. * The segment constructor must not call this function because it can
  96. * kill itself.
  97. */
  98. void nilfs_error(struct super_block *sb, const char *function,
  99. const char *fmt, ...)
  100. {
  101. struct the_nilfs *nilfs = sb->s_fs_info;
  102. struct va_format vaf;
  103. va_list args;
  104. va_start(args, fmt);
  105. vaf.fmt = fmt;
  106. vaf.va = &args;
  107. printk(KERN_CRIT "NILFS error (device %s): %s: %pV\n",
  108. sb->s_id, function, &vaf);
  109. va_end(args);
  110. if (!(sb->s_flags & MS_RDONLY)) {
  111. nilfs_set_error(sb);
  112. if (nilfs_test_opt(nilfs, ERRORS_RO)) {
  113. printk(KERN_CRIT "Remounting filesystem read-only\n");
  114. sb->s_flags |= MS_RDONLY;
  115. }
  116. }
  117. if (nilfs_test_opt(nilfs, ERRORS_PANIC))
  118. panic("NILFS (device %s): panic forced after error\n",
  119. sb->s_id);
  120. }
  121. void nilfs_warning(struct super_block *sb, const char *function,
  122. const char *fmt, ...)
  123. {
  124. struct va_format vaf;
  125. va_list args;
  126. va_start(args, fmt);
  127. vaf.fmt = fmt;
  128. vaf.va = &args;
  129. printk(KERN_WARNING "NILFS warning (device %s): %s: %pV\n",
  130. sb->s_id, function, &vaf);
  131. va_end(args);
  132. }
  133. struct inode *nilfs_alloc_inode(struct super_block *sb)
  134. {
  135. struct nilfs_inode_info *ii;
  136. ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
  137. if (!ii)
  138. return NULL;
  139. ii->i_bh = NULL;
  140. ii->i_state = 0;
  141. ii->i_cno = 0;
  142. ii->vfs_inode.i_version = 1;
  143. nilfs_mapping_init(&ii->i_btnode_cache, &ii->vfs_inode);
  144. return &ii->vfs_inode;
  145. }
  146. static void nilfs_i_callback(struct rcu_head *head)
  147. {
  148. struct inode *inode = container_of(head, struct inode, i_rcu);
  149. if (nilfs_is_metadata_file_inode(inode))
  150. nilfs_mdt_destroy(inode);
  151. kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
  152. }
  153. void nilfs_destroy_inode(struct inode *inode)
  154. {
  155. call_rcu(&inode->i_rcu, nilfs_i_callback);
  156. }
  157. static int nilfs_sync_super(struct super_block *sb, int flag)
  158. {
  159. struct the_nilfs *nilfs = sb->s_fs_info;
  160. int err;
  161. retry:
  162. set_buffer_dirty(nilfs->ns_sbh[0]);
  163. if (nilfs_test_opt(nilfs, BARRIER)) {
  164. err = __sync_dirty_buffer(nilfs->ns_sbh[0],
  165. WRITE_SYNC | WRITE_FLUSH_FUA);
  166. } else {
  167. err = sync_dirty_buffer(nilfs->ns_sbh[0]);
  168. }
  169. if (unlikely(err)) {
  170. printk(KERN_ERR
  171. "NILFS: unable to write superblock (err=%d)\n", err);
  172. if (err == -EIO && nilfs->ns_sbh[1]) {
  173. /*
  174. * sbp[0] points to newer log than sbp[1],
  175. * so copy sbp[0] to sbp[1] to take over sbp[0].
  176. */
  177. memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0],
  178. nilfs->ns_sbsize);
  179. nilfs_fall_back_super_block(nilfs);
  180. goto retry;
  181. }
  182. } else {
  183. struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
  184. nilfs->ns_sbwcount++;
  185. /*
  186. * The latest segment becomes trailable from the position
  187. * written in superblock.
  188. */
  189. clear_nilfs_discontinued(nilfs);
  190. /* update GC protection for recent segments */
  191. if (nilfs->ns_sbh[1]) {
  192. if (flag == NILFS_SB_COMMIT_ALL) {
  193. set_buffer_dirty(nilfs->ns_sbh[1]);
  194. if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
  195. goto out;
  196. }
  197. if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) <
  198. le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno))
  199. sbp = nilfs->ns_sbp[1];
  200. }
  201. spin_lock(&nilfs->ns_last_segment_lock);
  202. nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
  203. spin_unlock(&nilfs->ns_last_segment_lock);
  204. }
  205. out:
  206. return err;
  207. }
  208. void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
  209. struct the_nilfs *nilfs)
  210. {
  211. sector_t nfreeblocks;
  212. /* nilfs->ns_sem must be locked by the caller. */
  213. nilfs_count_free_blocks(nilfs, &nfreeblocks);
  214. sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
  215. spin_lock(&nilfs->ns_last_segment_lock);
  216. sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
  217. sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
  218. sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
  219. spin_unlock(&nilfs->ns_last_segment_lock);
  220. }
  221. struct nilfs_super_block **nilfs_prepare_super(struct super_block *sb,
  222. int flip)
  223. {
  224. struct the_nilfs *nilfs = sb->s_fs_info;
  225. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  226. /* nilfs->ns_sem must be locked by the caller. */
  227. if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
  228. if (sbp[1] &&
  229. sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
  230. memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
  231. } else {
  232. printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
  233. sb->s_id);
  234. return NULL;
  235. }
  236. } else if (sbp[1] &&
  237. sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
  238. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  239. }
  240. if (flip && sbp[1])
  241. nilfs_swap_super_block(nilfs);
  242. return sbp;
  243. }
  244. int nilfs_commit_super(struct super_block *sb, int flag)
  245. {
  246. struct the_nilfs *nilfs = sb->s_fs_info;
  247. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  248. time_t t;
  249. /* nilfs->ns_sem must be locked by the caller. */
  250. t = get_seconds();
  251. nilfs->ns_sbwtime = t;
  252. sbp[0]->s_wtime = cpu_to_le64(t);
  253. sbp[0]->s_sum = 0;
  254. sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
  255. (unsigned char *)sbp[0],
  256. nilfs->ns_sbsize));
  257. if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
  258. sbp[1]->s_wtime = sbp[0]->s_wtime;
  259. sbp[1]->s_sum = 0;
  260. sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
  261. (unsigned char *)sbp[1],
  262. nilfs->ns_sbsize));
  263. }
  264. clear_nilfs_sb_dirty(nilfs);
  265. nilfs->ns_flushed_device = 1;
  266. /* make sure store to ns_flushed_device cannot be reordered */
  267. smp_wmb();
  268. return nilfs_sync_super(sb, flag);
  269. }
  270. /**
  271. * nilfs_cleanup_super() - write filesystem state for cleanup
  272. * @sb: super block instance to be unmounted or degraded to read-only
  273. *
  274. * This function restores state flags in the on-disk super block.
  275. * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
  276. * filesystem was not clean previously.
  277. */
  278. int nilfs_cleanup_super(struct super_block *sb)
  279. {
  280. struct the_nilfs *nilfs = sb->s_fs_info;
  281. struct nilfs_super_block **sbp;
  282. int flag = NILFS_SB_COMMIT;
  283. int ret = -EIO;
  284. sbp = nilfs_prepare_super(sb, 0);
  285. if (sbp) {
  286. sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
  287. nilfs_set_log_cursor(sbp[0], nilfs);
  288. if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
  289. /*
  290. * make the "clean" flag also to the opposite
  291. * super block if both super blocks point to
  292. * the same checkpoint.
  293. */
  294. sbp[1]->s_state = sbp[0]->s_state;
  295. flag = NILFS_SB_COMMIT_ALL;
  296. }
  297. ret = nilfs_commit_super(sb, flag);
  298. }
  299. return ret;
  300. }
  301. /**
  302. * nilfs_move_2nd_super - relocate secondary super block
  303. * @sb: super block instance
  304. * @sb2off: new offset of the secondary super block (in bytes)
  305. */
  306. static int nilfs_move_2nd_super(struct super_block *sb, loff_t sb2off)
  307. {
  308. struct the_nilfs *nilfs = sb->s_fs_info;
  309. struct buffer_head *nsbh;
  310. struct nilfs_super_block *nsbp;
  311. sector_t blocknr, newblocknr;
  312. unsigned long offset;
  313. int sb2i; /* array index of the secondary superblock */
  314. int ret = 0;
  315. /* nilfs->ns_sem must be locked by the caller. */
  316. if (nilfs->ns_sbh[1] &&
  317. nilfs->ns_sbh[1]->b_blocknr > nilfs->ns_first_data_block) {
  318. sb2i = 1;
  319. blocknr = nilfs->ns_sbh[1]->b_blocknr;
  320. } else if (nilfs->ns_sbh[0]->b_blocknr > nilfs->ns_first_data_block) {
  321. sb2i = 0;
  322. blocknr = nilfs->ns_sbh[0]->b_blocknr;
  323. } else {
  324. sb2i = -1;
  325. blocknr = 0;
  326. }
  327. if (sb2i >= 0 && (u64)blocknr << nilfs->ns_blocksize_bits == sb2off)
  328. goto out; /* super block location is unchanged */
  329. /* Get new super block buffer */
  330. newblocknr = sb2off >> nilfs->ns_blocksize_bits;
  331. offset = sb2off & (nilfs->ns_blocksize - 1);
  332. nsbh = sb_getblk(sb, newblocknr);
  333. if (!nsbh) {
  334. printk(KERN_WARNING
  335. "NILFS warning: unable to move secondary superblock "
  336. "to block %llu\n", (unsigned long long)newblocknr);
  337. ret = -EIO;
  338. goto out;
  339. }
  340. nsbp = (void *)nsbh->b_data + offset;
  341. memset(nsbp, 0, nilfs->ns_blocksize);
  342. if (sb2i >= 0) {
  343. memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
  344. brelse(nilfs->ns_sbh[sb2i]);
  345. nilfs->ns_sbh[sb2i] = nsbh;
  346. nilfs->ns_sbp[sb2i] = nsbp;
  347. } else if (nilfs->ns_sbh[0]->b_blocknr < nilfs->ns_first_data_block) {
  348. /* secondary super block will be restored to index 1 */
  349. nilfs->ns_sbh[1] = nsbh;
  350. nilfs->ns_sbp[1] = nsbp;
  351. } else {
  352. brelse(nsbh);
  353. }
  354. out:
  355. return ret;
  356. }
  357. /**
  358. * nilfs_resize_fs - resize the filesystem
  359. * @sb: super block instance
  360. * @newsize: new size of the filesystem (in bytes)
  361. */
  362. int nilfs_resize_fs(struct super_block *sb, __u64 newsize)
  363. {
  364. struct the_nilfs *nilfs = sb->s_fs_info;
  365. struct nilfs_super_block **sbp;
  366. __u64 devsize, newnsegs;
  367. loff_t sb2off;
  368. int ret;
  369. ret = -ERANGE;
  370. devsize = i_size_read(sb->s_bdev->bd_inode);
  371. if (newsize > devsize)
  372. goto out;
  373. /*
  374. * Write lock is required to protect some functions depending
  375. * on the number of segments, the number of reserved segments,
  376. * and so forth.
  377. */
  378. down_write(&nilfs->ns_segctor_sem);
  379. sb2off = NILFS_SB2_OFFSET_BYTES(newsize);
  380. newnsegs = sb2off >> nilfs->ns_blocksize_bits;
  381. do_div(newnsegs, nilfs->ns_blocks_per_segment);
  382. ret = nilfs_sufile_resize(nilfs->ns_sufile, newnsegs);
  383. up_write(&nilfs->ns_segctor_sem);
  384. if (ret < 0)
  385. goto out;
  386. ret = nilfs_construct_segment(sb);
  387. if (ret < 0)
  388. goto out;
  389. down_write(&nilfs->ns_sem);
  390. nilfs_move_2nd_super(sb, sb2off);
  391. ret = -EIO;
  392. sbp = nilfs_prepare_super(sb, 0);
  393. if (likely(sbp)) {
  394. nilfs_set_log_cursor(sbp[0], nilfs);
  395. /*
  396. * Drop NILFS_RESIZE_FS flag for compatibility with
  397. * mount-time resize which may be implemented in a
  398. * future release.
  399. */
  400. sbp[0]->s_state = cpu_to_le16(le16_to_cpu(sbp[0]->s_state) &
  401. ~NILFS_RESIZE_FS);
  402. sbp[0]->s_dev_size = cpu_to_le64(newsize);
  403. sbp[0]->s_nsegments = cpu_to_le64(nilfs->ns_nsegments);
  404. if (sbp[1])
  405. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  406. ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
  407. }
  408. up_write(&nilfs->ns_sem);
  409. /*
  410. * Reset the range of allocatable segments last. This order
  411. * is important in the case of expansion because the secondary
  412. * superblock must be protected from log write until migration
  413. * completes.
  414. */
  415. if (!ret)
  416. nilfs_sufile_set_alloc_range(nilfs->ns_sufile, 0, newnsegs - 1);
  417. out:
  418. return ret;
  419. }
  420. static void nilfs_put_super(struct super_block *sb)
  421. {
  422. struct the_nilfs *nilfs = sb->s_fs_info;
  423. nilfs_detach_log_writer(sb);
  424. if (!(sb->s_flags & MS_RDONLY)) {
  425. down_write(&nilfs->ns_sem);
  426. nilfs_cleanup_super(sb);
  427. up_write(&nilfs->ns_sem);
  428. }
  429. iput(nilfs->ns_sufile);
  430. iput(nilfs->ns_cpfile);
  431. iput(nilfs->ns_dat);
  432. destroy_nilfs(nilfs);
  433. sb->s_fs_info = NULL;
  434. }
  435. static int nilfs_sync_fs(struct super_block *sb, int wait)
  436. {
  437. struct the_nilfs *nilfs = sb->s_fs_info;
  438. struct nilfs_super_block **sbp;
  439. int err = 0;
  440. /* This function is called when super block should be written back */
  441. if (wait)
  442. err = nilfs_construct_segment(sb);
  443. down_write(&nilfs->ns_sem);
  444. if (nilfs_sb_dirty(nilfs)) {
  445. sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
  446. if (likely(sbp)) {
  447. nilfs_set_log_cursor(sbp[0], nilfs);
  448. nilfs_commit_super(sb, NILFS_SB_COMMIT);
  449. }
  450. }
  451. up_write(&nilfs->ns_sem);
  452. if (!err)
  453. err = nilfs_flush_device(nilfs);
  454. return err;
  455. }
  456. int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
  457. struct nilfs_root **rootp)
  458. {
  459. struct the_nilfs *nilfs = sb->s_fs_info;
  460. struct nilfs_root *root;
  461. struct nilfs_checkpoint *raw_cp;
  462. struct buffer_head *bh_cp;
  463. int err = -ENOMEM;
  464. root = nilfs_find_or_create_root(
  465. nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
  466. if (!root)
  467. return err;
  468. if (root->ifile)
  469. goto reuse; /* already attached checkpoint */
  470. down_read(&nilfs->ns_segctor_sem);
  471. err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
  472. &bh_cp);
  473. up_read(&nilfs->ns_segctor_sem);
  474. if (unlikely(err)) {
  475. if (err == -ENOENT || err == -EINVAL) {
  476. printk(KERN_ERR
  477. "NILFS: Invalid checkpoint "
  478. "(checkpoint number=%llu)\n",
  479. (unsigned long long)cno);
  480. err = -EINVAL;
  481. }
  482. goto failed;
  483. }
  484. err = nilfs_ifile_read(sb, root, nilfs->ns_inode_size,
  485. &raw_cp->cp_ifile_inode, &root->ifile);
  486. if (err)
  487. goto failed_bh;
  488. atomic64_set(&root->inodes_count,
  489. le64_to_cpu(raw_cp->cp_inodes_count));
  490. atomic64_set(&root->blocks_count,
  491. le64_to_cpu(raw_cp->cp_blocks_count));
  492. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  493. reuse:
  494. *rootp = root;
  495. return 0;
  496. failed_bh:
  497. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  498. failed:
  499. nilfs_put_root(root);
  500. return err;
  501. }
  502. static int nilfs_freeze(struct super_block *sb)
  503. {
  504. struct the_nilfs *nilfs = sb->s_fs_info;
  505. int err;
  506. if (sb->s_flags & MS_RDONLY)
  507. return 0;
  508. /* Mark super block clean */
  509. down_write(&nilfs->ns_sem);
  510. err = nilfs_cleanup_super(sb);
  511. up_write(&nilfs->ns_sem);
  512. return err;
  513. }
  514. static int nilfs_unfreeze(struct super_block *sb)
  515. {
  516. struct the_nilfs *nilfs = sb->s_fs_info;
  517. if (sb->s_flags & MS_RDONLY)
  518. return 0;
  519. down_write(&nilfs->ns_sem);
  520. nilfs_setup_super(sb, false);
  521. up_write(&nilfs->ns_sem);
  522. return 0;
  523. }
  524. static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  525. {
  526. struct super_block *sb = dentry->d_sb;
  527. struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
  528. struct the_nilfs *nilfs = root->nilfs;
  529. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  530. unsigned long long blocks;
  531. unsigned long overhead;
  532. unsigned long nrsvblocks;
  533. sector_t nfreeblocks;
  534. u64 nmaxinodes, nfreeinodes;
  535. int err;
  536. /*
  537. * Compute all of the segment blocks
  538. *
  539. * The blocks before first segment and after last segment
  540. * are excluded.
  541. */
  542. blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
  543. - nilfs->ns_first_data_block;
  544. nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
  545. /*
  546. * Compute the overhead
  547. *
  548. * When distributing meta data blocks outside segment structure,
  549. * We must count them as the overhead.
  550. */
  551. overhead = 0;
  552. err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
  553. if (unlikely(err))
  554. return err;
  555. err = nilfs_ifile_count_free_inodes(root->ifile,
  556. &nmaxinodes, &nfreeinodes);
  557. if (unlikely(err)) {
  558. printk(KERN_WARNING
  559. "NILFS warning: fail to count free inodes: err %d.\n",
  560. 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. printk(KERN_ERR
  680. "NILFS: \"%s\" option is invalid "
  681. "for remount.\n", 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. printk(KERN_ERR
  696. "NILFS: Unrecognized mount option \"%s\"\n", 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. printk(KERN_WARNING
  726. "NILFS warning: mounting fs with errors\n");
  727. #if 0
  728. } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
  729. printk(KERN_WARNING
  730. "NILFS warning: maximal mount count reached\n");
  731. #endif
  732. }
  733. if (!max_mnt_count)
  734. sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
  735. sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
  736. sbp[0]->s_mtime = cpu_to_le64(get_seconds());
  737. skip_mount_setup:
  738. sbp[0]->s_state =
  739. cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
  740. /* synchronize sbp[1] with sbp[0] */
  741. if (sbp[1])
  742. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  743. return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
  744. }
  745. struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
  746. u64 pos, int blocksize,
  747. struct buffer_head **pbh)
  748. {
  749. unsigned long long sb_index = pos;
  750. unsigned long offset;
  751. offset = do_div(sb_index, blocksize);
  752. *pbh = sb_bread(sb, sb_index);
  753. if (!*pbh)
  754. return NULL;
  755. return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
  756. }
  757. int nilfs_store_magic_and_option(struct super_block *sb,
  758. struct nilfs_super_block *sbp,
  759. char *data)
  760. {
  761. struct the_nilfs *nilfs = sb->s_fs_info;
  762. sb->s_magic = le16_to_cpu(sbp->s_magic);
  763. /* FS independent flags */
  764. #ifdef NILFS_ATIME_DISABLE
  765. sb->s_flags |= MS_NOATIME;
  766. #endif
  767. nilfs_set_default_options(sb, sbp);
  768. nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
  769. nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
  770. nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
  771. nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
  772. return !parse_options(data, sb, 0) ? -EINVAL : 0;
  773. }
  774. int nilfs_check_feature_compatibility(struct super_block *sb,
  775. struct nilfs_super_block *sbp)
  776. {
  777. __u64 features;
  778. features = le64_to_cpu(sbp->s_feature_incompat) &
  779. ~NILFS_FEATURE_INCOMPAT_SUPP;
  780. if (features) {
  781. printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
  782. "optional features (%llx)\n",
  783. (unsigned long long)features);
  784. return -EINVAL;
  785. }
  786. features = le64_to_cpu(sbp->s_feature_compat_ro) &
  787. ~NILFS_FEATURE_COMPAT_RO_SUPP;
  788. if (!(sb->s_flags & MS_RDONLY) && features) {
  789. printk(KERN_ERR "NILFS: couldn't mount RDWR because of "
  790. "unsupported optional features (%llx)\n",
  791. (unsigned long long)features);
  792. return -EINVAL;
  793. }
  794. return 0;
  795. }
  796. static int nilfs_get_root_dentry(struct super_block *sb,
  797. struct nilfs_root *root,
  798. struct dentry **root_dentry)
  799. {
  800. struct inode *inode;
  801. struct dentry *dentry;
  802. int ret = 0;
  803. inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
  804. if (IS_ERR(inode)) {
  805. printk(KERN_ERR "NILFS: get root inode failed\n");
  806. ret = PTR_ERR(inode);
  807. goto out;
  808. }
  809. if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
  810. iput(inode);
  811. printk(KERN_ERR "NILFS: corrupt root inode.\n");
  812. ret = -EINVAL;
  813. goto out;
  814. }
  815. if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
  816. dentry = d_find_alias(inode);
  817. if (!dentry) {
  818. dentry = d_make_root(inode);
  819. if (!dentry) {
  820. ret = -ENOMEM;
  821. goto failed_dentry;
  822. }
  823. } else {
  824. iput(inode);
  825. }
  826. } else {
  827. dentry = d_obtain_root(inode);
  828. if (IS_ERR(dentry)) {
  829. ret = PTR_ERR(dentry);
  830. goto failed_dentry;
  831. }
  832. }
  833. *root_dentry = dentry;
  834. out:
  835. return ret;
  836. failed_dentry:
  837. printk(KERN_ERR "NILFS: get root dentry failed\n");
  838. goto out;
  839. }
  840. static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
  841. struct dentry **root_dentry)
  842. {
  843. struct the_nilfs *nilfs = s->s_fs_info;
  844. struct nilfs_root *root;
  845. int ret;
  846. mutex_lock(&nilfs->ns_snapshot_mount_mutex);
  847. down_read(&nilfs->ns_segctor_sem);
  848. ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
  849. up_read(&nilfs->ns_segctor_sem);
  850. if (ret < 0) {
  851. ret = (ret == -ENOENT) ? -EINVAL : ret;
  852. goto out;
  853. } else if (!ret) {
  854. printk(KERN_ERR "NILFS: The specified checkpoint is "
  855. "not a snapshot (checkpoint number=%llu).\n",
  856. (unsigned long long)cno);
  857. ret = -EINVAL;
  858. goto out;
  859. }
  860. ret = nilfs_attach_checkpoint(s, cno, false, &root);
  861. if (ret) {
  862. printk(KERN_ERR "NILFS: error loading snapshot "
  863. "(checkpoint number=%llu).\n",
  864. (unsigned long long)cno);
  865. goto out;
  866. }
  867. ret = nilfs_get_root_dentry(s, root, root_dentry);
  868. nilfs_put_root(root);
  869. out:
  870. mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
  871. return ret;
  872. }
  873. /**
  874. * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
  875. * @root_dentry: root dentry of the tree to be shrunk
  876. *
  877. * This function returns true if the tree was in-use.
  878. */
  879. static bool nilfs_tree_is_busy(struct dentry *root_dentry)
  880. {
  881. shrink_dcache_parent(root_dentry);
  882. return d_count(root_dentry) > 1;
  883. }
  884. int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
  885. {
  886. struct the_nilfs *nilfs = sb->s_fs_info;
  887. struct nilfs_root *root;
  888. struct inode *inode;
  889. struct dentry *dentry;
  890. int ret;
  891. if (cno > nilfs->ns_cno)
  892. return false;
  893. if (cno >= nilfs_last_cno(nilfs))
  894. return true; /* protect recent checkpoints */
  895. ret = false;
  896. root = nilfs_lookup_root(nilfs, cno);
  897. if (root) {
  898. inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
  899. if (inode) {
  900. dentry = d_find_alias(inode);
  901. if (dentry) {
  902. ret = nilfs_tree_is_busy(dentry);
  903. dput(dentry);
  904. }
  905. iput(inode);
  906. }
  907. nilfs_put_root(root);
  908. }
  909. return ret;
  910. }
  911. /**
  912. * nilfs_fill_super() - initialize a super block instance
  913. * @sb: super_block
  914. * @data: mount options
  915. * @silent: silent mode flag
  916. *
  917. * This function is called exclusively by nilfs->ns_mount_mutex.
  918. * So, the recovery process is protected from other simultaneous mounts.
  919. */
  920. static int
  921. nilfs_fill_super(struct super_block *sb, void *data, int silent)
  922. {
  923. struct the_nilfs *nilfs;
  924. struct nilfs_root *fsroot;
  925. __u64 cno;
  926. int err;
  927. nilfs = alloc_nilfs(sb->s_bdev);
  928. if (!nilfs)
  929. return -ENOMEM;
  930. sb->s_fs_info = nilfs;
  931. err = init_nilfs(nilfs, sb, (char *)data);
  932. if (err)
  933. goto failed_nilfs;
  934. sb->s_op = &nilfs_sops;
  935. sb->s_export_op = &nilfs_export_ops;
  936. sb->s_root = NULL;
  937. sb->s_time_gran = 1;
  938. sb->s_max_links = NILFS_LINK_MAX;
  939. sb->s_bdi = &bdev_get_queue(sb->s_bdev)->backing_dev_info;
  940. err = load_nilfs(nilfs, sb);
  941. if (err)
  942. goto failed_nilfs;
  943. cno = nilfs_last_cno(nilfs);
  944. err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
  945. if (err) {
  946. printk(KERN_ERR "NILFS: error loading last checkpoint "
  947. "(checkpoint number=%llu).\n", (unsigned long long)cno);
  948. goto failed_unload;
  949. }
  950. if (!(sb->s_flags & MS_RDONLY)) {
  951. err = nilfs_attach_log_writer(sb, fsroot);
  952. if (err)
  953. goto failed_checkpoint;
  954. }
  955. err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
  956. if (err)
  957. goto failed_segctor;
  958. nilfs_put_root(fsroot);
  959. if (!(sb->s_flags & MS_RDONLY)) {
  960. down_write(&nilfs->ns_sem);
  961. nilfs_setup_super(sb, true);
  962. up_write(&nilfs->ns_sem);
  963. }
  964. return 0;
  965. failed_segctor:
  966. nilfs_detach_log_writer(sb);
  967. failed_checkpoint:
  968. nilfs_put_root(fsroot);
  969. failed_unload:
  970. iput(nilfs->ns_sufile);
  971. iput(nilfs->ns_cpfile);
  972. iput(nilfs->ns_dat);
  973. failed_nilfs:
  974. destroy_nilfs(nilfs);
  975. return err;
  976. }
  977. static int nilfs_remount(struct super_block *sb, int *flags, char *data)
  978. {
  979. struct the_nilfs *nilfs = sb->s_fs_info;
  980. unsigned long old_sb_flags;
  981. unsigned long old_mount_opt;
  982. int err;
  983. sync_filesystem(sb);
  984. old_sb_flags = sb->s_flags;
  985. old_mount_opt = nilfs->ns_mount_opt;
  986. if (!parse_options(data, sb, 1)) {
  987. err = -EINVAL;
  988. goto restore_opts;
  989. }
  990. sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
  991. err = -EINVAL;
  992. if (!nilfs_valid_fs(nilfs)) {
  993. printk(KERN_WARNING "NILFS (device %s): couldn't "
  994. "remount because the filesystem is in an "
  995. "incomplete recovery state.\n", sb->s_id);
  996. goto restore_opts;
  997. }
  998. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  999. goto out;
  1000. if (*flags & MS_RDONLY) {
  1001. /* Shutting down log writer */
  1002. nilfs_detach_log_writer(sb);
  1003. sb->s_flags |= MS_RDONLY;
  1004. /*
  1005. * Remounting a valid RW partition RDONLY, so set
  1006. * the RDONLY flag and then mark the partition as valid again.
  1007. */
  1008. down_write(&nilfs->ns_sem);
  1009. nilfs_cleanup_super(sb);
  1010. up_write(&nilfs->ns_sem);
  1011. } else {
  1012. __u64 features;
  1013. struct nilfs_root *root;
  1014. /*
  1015. * Mounting a RDONLY partition read-write, so reread and
  1016. * store the current valid flag. (It may have been changed
  1017. * by fsck since we originally mounted the partition.)
  1018. */
  1019. down_read(&nilfs->ns_sem);
  1020. features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
  1021. ~NILFS_FEATURE_COMPAT_RO_SUPP;
  1022. up_read(&nilfs->ns_sem);
  1023. if (features) {
  1024. printk(KERN_WARNING "NILFS (device %s): couldn't "
  1025. "remount RDWR because of unsupported optional "
  1026. "features (%llx)\n",
  1027. sb->s_id, (unsigned long long)features);
  1028. err = -EROFS;
  1029. goto restore_opts;
  1030. }
  1031. sb->s_flags &= ~MS_RDONLY;
  1032. root = NILFS_I(d_inode(sb->s_root))->i_root;
  1033. err = nilfs_attach_log_writer(sb, root);
  1034. if (err)
  1035. goto restore_opts;
  1036. down_write(&nilfs->ns_sem);
  1037. nilfs_setup_super(sb, true);
  1038. up_write(&nilfs->ns_sem);
  1039. }
  1040. out:
  1041. return 0;
  1042. restore_opts:
  1043. sb->s_flags = old_sb_flags;
  1044. nilfs->ns_mount_opt = old_mount_opt;
  1045. return err;
  1046. }
  1047. struct nilfs_super_data {
  1048. struct block_device *bdev;
  1049. __u64 cno;
  1050. int flags;
  1051. };
  1052. /**
  1053. * nilfs_identify - pre-read mount options needed to identify mount instance
  1054. * @data: mount options
  1055. * @sd: nilfs_super_data
  1056. */
  1057. static int nilfs_identify(char *data, struct nilfs_super_data *sd)
  1058. {
  1059. char *p, *options = data;
  1060. substring_t args[MAX_OPT_ARGS];
  1061. int token;
  1062. int ret = 0;
  1063. do {
  1064. p = strsep(&options, ",");
  1065. if (p != NULL && *p) {
  1066. token = match_token(p, tokens, args);
  1067. if (token == Opt_snapshot) {
  1068. if (!(sd->flags & MS_RDONLY)) {
  1069. ret++;
  1070. } else {
  1071. sd->cno = simple_strtoull(args[0].from,
  1072. NULL, 0);
  1073. /*
  1074. * No need to see the end pointer;
  1075. * match_token() has done syntax
  1076. * checking.
  1077. */
  1078. if (sd->cno == 0)
  1079. ret++;
  1080. }
  1081. }
  1082. if (ret)
  1083. printk(KERN_ERR
  1084. "NILFS: invalid mount option: %s\n", p);
  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 & MS_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 & MS_SILENT ? 1 : 0);
  1148. if (err)
  1149. goto failed_super;
  1150. s->s_flags |= MS_ACTIVE;
  1151. } else if (!sd.cno) {
  1152. if (nilfs_tree_is_busy(s->s_root)) {
  1153. if ((flags ^ s->s_flags) & MS_RDONLY) {
  1154. printk(KERN_ERR "NILFS: the device already "
  1155. "has a %s mount.\n",
  1156. (s->s_flags & MS_RDONLY) ?
  1157. "read-only" : "read/write");
  1158. err = -EBUSY;
  1159. goto failed_super;
  1160. }
  1161. } else {
  1162. /*
  1163. * Try remount to setup mount states if the current
  1164. * tree is not mounted and only snapshots use this sb.
  1165. */
  1166. err = nilfs_remount(s, &flags, data);
  1167. if (err)
  1168. goto failed_super;
  1169. }
  1170. }
  1171. if (sd.cno) {
  1172. err = nilfs_attach_snapshot(s, sd.cno, &root_dentry);
  1173. if (err)
  1174. goto failed_super;
  1175. } else {
  1176. root_dentry = dget(s->s_root);
  1177. }
  1178. if (!s_new)
  1179. blkdev_put(sd.bdev, mode);
  1180. return root_dentry;
  1181. failed_super:
  1182. deactivate_locked_super(s);
  1183. failed:
  1184. if (!s_new)
  1185. blkdev_put(sd.bdev, mode);
  1186. return ERR_PTR(err);
  1187. }
  1188. struct file_system_type nilfs_fs_type = {
  1189. .owner = THIS_MODULE,
  1190. .name = "nilfs2",
  1191. .mount = nilfs_mount,
  1192. .kill_sb = kill_block_super,
  1193. .fs_flags = FS_REQUIRES_DEV,
  1194. };
  1195. MODULE_ALIAS_FS("nilfs2");
  1196. static void nilfs_inode_init_once(void *obj)
  1197. {
  1198. struct nilfs_inode_info *ii = obj;
  1199. INIT_LIST_HEAD(&ii->i_dirty);
  1200. #ifdef CONFIG_NILFS_XATTR
  1201. init_rwsem(&ii->xattr_sem);
  1202. #endif
  1203. address_space_init_once(&ii->i_btnode_cache);
  1204. ii->i_bmap = &ii->i_bmap_data;
  1205. inode_init_once(&ii->vfs_inode);
  1206. }
  1207. static void nilfs_segbuf_init_once(void *obj)
  1208. {
  1209. memset(obj, 0, sizeof(struct nilfs_segment_buffer));
  1210. }
  1211. static void nilfs_destroy_cachep(void)
  1212. {
  1213. /*
  1214. * Make sure all delayed rcu free inodes are flushed before we
  1215. * destroy cache.
  1216. */
  1217. rcu_barrier();
  1218. kmem_cache_destroy(nilfs_inode_cachep);
  1219. kmem_cache_destroy(nilfs_transaction_cachep);
  1220. kmem_cache_destroy(nilfs_segbuf_cachep);
  1221. kmem_cache_destroy(nilfs_btree_path_cache);
  1222. }
  1223. static int __init nilfs_init_cachep(void)
  1224. {
  1225. nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
  1226. sizeof(struct nilfs_inode_info), 0,
  1227. SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
  1228. nilfs_inode_init_once);
  1229. if (!nilfs_inode_cachep)
  1230. goto fail;
  1231. nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
  1232. sizeof(struct nilfs_transaction_info), 0,
  1233. SLAB_RECLAIM_ACCOUNT, NULL);
  1234. if (!nilfs_transaction_cachep)
  1235. goto fail;
  1236. nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
  1237. sizeof(struct nilfs_segment_buffer), 0,
  1238. SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
  1239. if (!nilfs_segbuf_cachep)
  1240. goto fail;
  1241. nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
  1242. sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
  1243. 0, 0, NULL);
  1244. if (!nilfs_btree_path_cache)
  1245. goto fail;
  1246. return 0;
  1247. fail:
  1248. nilfs_destroy_cachep();
  1249. return -ENOMEM;
  1250. }
  1251. static int __init init_nilfs_fs(void)
  1252. {
  1253. int err;
  1254. err = nilfs_init_cachep();
  1255. if (err)
  1256. goto fail;
  1257. err = nilfs_sysfs_init();
  1258. if (err)
  1259. goto free_cachep;
  1260. err = register_filesystem(&nilfs_fs_type);
  1261. if (err)
  1262. goto deinit_sysfs_entry;
  1263. printk(KERN_INFO "NILFS version 2 loaded\n");
  1264. return 0;
  1265. deinit_sysfs_entry:
  1266. nilfs_sysfs_exit();
  1267. free_cachep:
  1268. nilfs_destroy_cachep();
  1269. fail:
  1270. return err;
  1271. }
  1272. static void __exit exit_nilfs_fs(void)
  1273. {
  1274. nilfs_destroy_cachep();
  1275. nilfs_sysfs_exit();
  1276. unregister_filesystem(&nilfs_fs_type);
  1277. }
  1278. module_init(init_nilfs_fs)
  1279. module_exit(exit_nilfs_fs)