super.c 36 KB

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