super.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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 = -1; /* 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. }
  331. if (sb2i >= 0 && (u64)blocknr << nilfs->ns_blocksize_bits == sb2off)
  332. goto out; /* super block location is unchanged */
  333. /* Get new super block buffer */
  334. newblocknr = sb2off >> nilfs->ns_blocksize_bits;
  335. offset = sb2off & (nilfs->ns_blocksize - 1);
  336. nsbh = sb_getblk(sb, newblocknr);
  337. if (!nsbh) {
  338. printk(KERN_WARNING
  339. "NILFS warning: unable to move secondary superblock "
  340. "to block %llu\n", (unsigned long long)newblocknr);
  341. ret = -EIO;
  342. goto out;
  343. }
  344. nsbp = (void *)nsbh->b_data + offset;
  345. memset(nsbp, 0, nilfs->ns_blocksize);
  346. if (sb2i >= 0) {
  347. memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
  348. brelse(nilfs->ns_sbh[sb2i]);
  349. nilfs->ns_sbh[sb2i] = nsbh;
  350. nilfs->ns_sbp[sb2i] = nsbp;
  351. } else if (nilfs->ns_sbh[0]->b_blocknr < nilfs->ns_first_data_block) {
  352. /* secondary super block will be restored to index 1 */
  353. nilfs->ns_sbh[1] = nsbh;
  354. nilfs->ns_sbp[1] = nsbp;
  355. } else {
  356. brelse(nsbh);
  357. }
  358. out:
  359. return ret;
  360. }
  361. /**
  362. * nilfs_resize_fs - resize the filesystem
  363. * @sb: super block instance
  364. * @newsize: new size of the filesystem (in bytes)
  365. */
  366. int nilfs_resize_fs(struct super_block *sb, __u64 newsize)
  367. {
  368. struct the_nilfs *nilfs = sb->s_fs_info;
  369. struct nilfs_super_block **sbp;
  370. __u64 devsize, newnsegs;
  371. loff_t sb2off;
  372. int ret;
  373. ret = -ERANGE;
  374. devsize = i_size_read(sb->s_bdev->bd_inode);
  375. if (newsize > devsize)
  376. goto out;
  377. /*
  378. * Write lock is required to protect some functions depending
  379. * on the number of segments, the number of reserved segments,
  380. * and so forth.
  381. */
  382. down_write(&nilfs->ns_segctor_sem);
  383. sb2off = NILFS_SB2_OFFSET_BYTES(newsize);
  384. newnsegs = sb2off >> nilfs->ns_blocksize_bits;
  385. do_div(newnsegs, nilfs->ns_blocks_per_segment);
  386. ret = nilfs_sufile_resize(nilfs->ns_sufile, newnsegs);
  387. up_write(&nilfs->ns_segctor_sem);
  388. if (ret < 0)
  389. goto out;
  390. ret = nilfs_construct_segment(sb);
  391. if (ret < 0)
  392. goto out;
  393. down_write(&nilfs->ns_sem);
  394. nilfs_move_2nd_super(sb, sb2off);
  395. ret = -EIO;
  396. sbp = nilfs_prepare_super(sb, 0);
  397. if (likely(sbp)) {
  398. nilfs_set_log_cursor(sbp[0], nilfs);
  399. /*
  400. * Drop NILFS_RESIZE_FS flag for compatibility with
  401. * mount-time resize which may be implemented in a
  402. * future release.
  403. */
  404. sbp[0]->s_state = cpu_to_le16(le16_to_cpu(sbp[0]->s_state) &
  405. ~NILFS_RESIZE_FS);
  406. sbp[0]->s_dev_size = cpu_to_le64(newsize);
  407. sbp[0]->s_nsegments = cpu_to_le64(nilfs->ns_nsegments);
  408. if (sbp[1])
  409. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  410. ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
  411. }
  412. up_write(&nilfs->ns_sem);
  413. /*
  414. * Reset the range of allocatable segments last. This order
  415. * is important in the case of expansion because the secondary
  416. * superblock must be protected from log write until migration
  417. * completes.
  418. */
  419. if (!ret)
  420. nilfs_sufile_set_alloc_range(nilfs->ns_sufile, 0, newnsegs - 1);
  421. out:
  422. return ret;
  423. }
  424. static void nilfs_put_super(struct super_block *sb)
  425. {
  426. struct the_nilfs *nilfs = sb->s_fs_info;
  427. nilfs_detach_log_writer(sb);
  428. if (!(sb->s_flags & MS_RDONLY)) {
  429. down_write(&nilfs->ns_sem);
  430. nilfs_cleanup_super(sb);
  431. up_write(&nilfs->ns_sem);
  432. }
  433. iput(nilfs->ns_sufile);
  434. iput(nilfs->ns_cpfile);
  435. iput(nilfs->ns_dat);
  436. destroy_nilfs(nilfs);
  437. sb->s_fs_info = NULL;
  438. }
  439. static int nilfs_sync_fs(struct super_block *sb, int wait)
  440. {
  441. struct the_nilfs *nilfs = sb->s_fs_info;
  442. struct nilfs_super_block **sbp;
  443. int err = 0;
  444. /* This function is called when super block should be written back */
  445. if (wait)
  446. err = nilfs_construct_segment(sb);
  447. down_write(&nilfs->ns_sem);
  448. if (nilfs_sb_dirty(nilfs)) {
  449. sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
  450. if (likely(sbp)) {
  451. nilfs_set_log_cursor(sbp[0], nilfs);
  452. nilfs_commit_super(sb, NILFS_SB_COMMIT);
  453. }
  454. }
  455. up_write(&nilfs->ns_sem);
  456. if (!err)
  457. err = nilfs_flush_device(nilfs);
  458. return err;
  459. }
  460. int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
  461. struct nilfs_root **rootp)
  462. {
  463. struct the_nilfs *nilfs = sb->s_fs_info;
  464. struct nilfs_root *root;
  465. struct nilfs_checkpoint *raw_cp;
  466. struct buffer_head *bh_cp;
  467. int err = -ENOMEM;
  468. root = nilfs_find_or_create_root(
  469. nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
  470. if (!root)
  471. return err;
  472. if (root->ifile)
  473. goto reuse; /* already attached checkpoint */
  474. down_read(&nilfs->ns_segctor_sem);
  475. err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
  476. &bh_cp);
  477. up_read(&nilfs->ns_segctor_sem);
  478. if (unlikely(err)) {
  479. if (err == -ENOENT || err == -EINVAL) {
  480. printk(KERN_ERR
  481. "NILFS: Invalid checkpoint "
  482. "(checkpoint number=%llu)\n",
  483. (unsigned long long)cno);
  484. err = -EINVAL;
  485. }
  486. goto failed;
  487. }
  488. err = nilfs_ifile_read(sb, root, nilfs->ns_inode_size,
  489. &raw_cp->cp_ifile_inode, &root->ifile);
  490. if (err)
  491. goto failed_bh;
  492. atomic64_set(&root->inodes_count,
  493. le64_to_cpu(raw_cp->cp_inodes_count));
  494. atomic64_set(&root->blocks_count,
  495. le64_to_cpu(raw_cp->cp_blocks_count));
  496. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  497. reuse:
  498. *rootp = root;
  499. return 0;
  500. failed_bh:
  501. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  502. failed:
  503. nilfs_put_root(root);
  504. return err;
  505. }
  506. static int nilfs_freeze(struct super_block *sb)
  507. {
  508. struct the_nilfs *nilfs = sb->s_fs_info;
  509. int err;
  510. if (sb->s_flags & MS_RDONLY)
  511. return 0;
  512. /* Mark super block clean */
  513. down_write(&nilfs->ns_sem);
  514. err = nilfs_cleanup_super(sb);
  515. up_write(&nilfs->ns_sem);
  516. return err;
  517. }
  518. static int nilfs_unfreeze(struct super_block *sb)
  519. {
  520. struct the_nilfs *nilfs = sb->s_fs_info;
  521. if (sb->s_flags & MS_RDONLY)
  522. return 0;
  523. down_write(&nilfs->ns_sem);
  524. nilfs_setup_super(sb, false);
  525. up_write(&nilfs->ns_sem);
  526. return 0;
  527. }
  528. static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  529. {
  530. struct super_block *sb = dentry->d_sb;
  531. struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
  532. struct the_nilfs *nilfs = root->nilfs;
  533. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  534. unsigned long long blocks;
  535. unsigned long overhead;
  536. unsigned long nrsvblocks;
  537. sector_t nfreeblocks;
  538. u64 nmaxinodes, nfreeinodes;
  539. int err;
  540. /*
  541. * Compute all of the segment blocks
  542. *
  543. * The blocks before first segment and after last segment
  544. * are excluded.
  545. */
  546. blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
  547. - nilfs->ns_first_data_block;
  548. nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
  549. /*
  550. * Compute the overhead
  551. *
  552. * When distributing meta data blocks outside segment structure,
  553. * We must count them as the overhead.
  554. */
  555. overhead = 0;
  556. err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
  557. if (unlikely(err))
  558. return err;
  559. err = nilfs_ifile_count_free_inodes(root->ifile,
  560. &nmaxinodes, &nfreeinodes);
  561. if (unlikely(err)) {
  562. printk(KERN_WARNING
  563. "NILFS warning: fail to count free inodes: err %d.\n",
  564. err);
  565. if (err == -ERANGE) {
  566. /*
  567. * If nilfs_palloc_count_max_entries() returns
  568. * -ERANGE error code then we simply treat
  569. * curent inodes count as maximum possible and
  570. * zero as free inodes value.
  571. */
  572. nmaxinodes = atomic64_read(&root->inodes_count);
  573. nfreeinodes = 0;
  574. err = 0;
  575. } else
  576. return err;
  577. }
  578. buf->f_type = NILFS_SUPER_MAGIC;
  579. buf->f_bsize = sb->s_blocksize;
  580. buf->f_blocks = blocks - overhead;
  581. buf->f_bfree = nfreeblocks;
  582. buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
  583. (buf->f_bfree - nrsvblocks) : 0;
  584. buf->f_files = nmaxinodes;
  585. buf->f_ffree = nfreeinodes;
  586. buf->f_namelen = NILFS_NAME_LEN;
  587. buf->f_fsid.val[0] = (u32)id;
  588. buf->f_fsid.val[1] = (u32)(id >> 32);
  589. return 0;
  590. }
  591. static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
  592. {
  593. struct super_block *sb = dentry->d_sb;
  594. struct the_nilfs *nilfs = sb->s_fs_info;
  595. struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
  596. if (!nilfs_test_opt(nilfs, BARRIER))
  597. seq_puts(seq, ",nobarrier");
  598. if (root->cno != NILFS_CPTREE_CURRENT_CNO)
  599. seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
  600. if (nilfs_test_opt(nilfs, ERRORS_PANIC))
  601. seq_puts(seq, ",errors=panic");
  602. if (nilfs_test_opt(nilfs, ERRORS_CONT))
  603. seq_puts(seq, ",errors=continue");
  604. if (nilfs_test_opt(nilfs, STRICT_ORDER))
  605. seq_puts(seq, ",order=strict");
  606. if (nilfs_test_opt(nilfs, NORECOVERY))
  607. seq_puts(seq, ",norecovery");
  608. if (nilfs_test_opt(nilfs, DISCARD))
  609. seq_puts(seq, ",discard");
  610. return 0;
  611. }
  612. static const struct super_operations nilfs_sops = {
  613. .alloc_inode = nilfs_alloc_inode,
  614. .destroy_inode = nilfs_destroy_inode,
  615. .dirty_inode = nilfs_dirty_inode,
  616. .evict_inode = nilfs_evict_inode,
  617. .put_super = nilfs_put_super,
  618. .sync_fs = nilfs_sync_fs,
  619. .freeze_fs = nilfs_freeze,
  620. .unfreeze_fs = nilfs_unfreeze,
  621. .statfs = nilfs_statfs,
  622. .remount_fs = nilfs_remount,
  623. .show_options = nilfs_show_options
  624. };
  625. enum {
  626. Opt_err_cont, Opt_err_panic, Opt_err_ro,
  627. Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
  628. Opt_discard, Opt_nodiscard, Opt_err,
  629. };
  630. static match_table_t tokens = {
  631. {Opt_err_cont, "errors=continue"},
  632. {Opt_err_panic, "errors=panic"},
  633. {Opt_err_ro, "errors=remount-ro"},
  634. {Opt_barrier, "barrier"},
  635. {Opt_nobarrier, "nobarrier"},
  636. {Opt_snapshot, "cp=%u"},
  637. {Opt_order, "order=%s"},
  638. {Opt_norecovery, "norecovery"},
  639. {Opt_discard, "discard"},
  640. {Opt_nodiscard, "nodiscard"},
  641. {Opt_err, NULL}
  642. };
  643. static int parse_options(char *options, struct super_block *sb, int is_remount)
  644. {
  645. struct the_nilfs *nilfs = sb->s_fs_info;
  646. char *p;
  647. substring_t args[MAX_OPT_ARGS];
  648. if (!options)
  649. return 1;
  650. while ((p = strsep(&options, ",")) != NULL) {
  651. int token;
  652. if (!*p)
  653. continue;
  654. token = match_token(p, tokens, args);
  655. switch (token) {
  656. case Opt_barrier:
  657. nilfs_set_opt(nilfs, BARRIER);
  658. break;
  659. case Opt_nobarrier:
  660. nilfs_clear_opt(nilfs, BARRIER);
  661. break;
  662. case Opt_order:
  663. if (strcmp(args[0].from, "relaxed") == 0)
  664. /* Ordered data semantics */
  665. nilfs_clear_opt(nilfs, STRICT_ORDER);
  666. else if (strcmp(args[0].from, "strict") == 0)
  667. /* Strict in-order semantics */
  668. nilfs_set_opt(nilfs, STRICT_ORDER);
  669. else
  670. return 0;
  671. break;
  672. case Opt_err_panic:
  673. nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_PANIC);
  674. break;
  675. case Opt_err_ro:
  676. nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_RO);
  677. break;
  678. case Opt_err_cont:
  679. nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_CONT);
  680. break;
  681. case Opt_snapshot:
  682. if (is_remount) {
  683. printk(KERN_ERR
  684. "NILFS: \"%s\" option is invalid "
  685. "for remount.\n", p);
  686. return 0;
  687. }
  688. break;
  689. case Opt_norecovery:
  690. nilfs_set_opt(nilfs, NORECOVERY);
  691. break;
  692. case Opt_discard:
  693. nilfs_set_opt(nilfs, DISCARD);
  694. break;
  695. case Opt_nodiscard:
  696. nilfs_clear_opt(nilfs, DISCARD);
  697. break;
  698. default:
  699. printk(KERN_ERR
  700. "NILFS: Unrecognized mount option \"%s\"\n", p);
  701. return 0;
  702. }
  703. }
  704. return 1;
  705. }
  706. static inline void
  707. nilfs_set_default_options(struct super_block *sb,
  708. struct nilfs_super_block *sbp)
  709. {
  710. struct the_nilfs *nilfs = sb->s_fs_info;
  711. nilfs->ns_mount_opt =
  712. NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
  713. }
  714. static int nilfs_setup_super(struct super_block *sb, int is_mount)
  715. {
  716. struct the_nilfs *nilfs = sb->s_fs_info;
  717. struct nilfs_super_block **sbp;
  718. int max_mnt_count;
  719. int mnt_count;
  720. /* nilfs->ns_sem must be locked by the caller. */
  721. sbp = nilfs_prepare_super(sb, 0);
  722. if (!sbp)
  723. return -EIO;
  724. if (!is_mount)
  725. goto skip_mount_setup;
  726. max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
  727. mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
  728. if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
  729. printk(KERN_WARNING
  730. "NILFS warning: mounting fs with errors\n");
  731. #if 0
  732. } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
  733. printk(KERN_WARNING
  734. "NILFS warning: maximal mount count reached\n");
  735. #endif
  736. }
  737. if (!max_mnt_count)
  738. sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
  739. sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
  740. sbp[0]->s_mtime = cpu_to_le64(get_seconds());
  741. skip_mount_setup:
  742. sbp[0]->s_state =
  743. cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
  744. /* synchronize sbp[1] with sbp[0] */
  745. if (sbp[1])
  746. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  747. return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
  748. }
  749. struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
  750. u64 pos, int blocksize,
  751. struct buffer_head **pbh)
  752. {
  753. unsigned long long sb_index = pos;
  754. unsigned long offset;
  755. offset = do_div(sb_index, blocksize);
  756. *pbh = sb_bread(sb, sb_index);
  757. if (!*pbh)
  758. return NULL;
  759. return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
  760. }
  761. int nilfs_store_magic_and_option(struct super_block *sb,
  762. struct nilfs_super_block *sbp,
  763. char *data)
  764. {
  765. struct the_nilfs *nilfs = sb->s_fs_info;
  766. sb->s_magic = le16_to_cpu(sbp->s_magic);
  767. /* FS independent flags */
  768. #ifdef NILFS_ATIME_DISABLE
  769. sb->s_flags |= MS_NOATIME;
  770. #endif
  771. nilfs_set_default_options(sb, sbp);
  772. nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
  773. nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
  774. nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
  775. nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
  776. return !parse_options(data, sb, 0) ? -EINVAL : 0 ;
  777. }
  778. int nilfs_check_feature_compatibility(struct super_block *sb,
  779. struct nilfs_super_block *sbp)
  780. {
  781. __u64 features;
  782. features = le64_to_cpu(sbp->s_feature_incompat) &
  783. ~NILFS_FEATURE_INCOMPAT_SUPP;
  784. if (features) {
  785. printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
  786. "optional features (%llx)\n",
  787. (unsigned long long)features);
  788. return -EINVAL;
  789. }
  790. features = le64_to_cpu(sbp->s_feature_compat_ro) &
  791. ~NILFS_FEATURE_COMPAT_RO_SUPP;
  792. if (!(sb->s_flags & MS_RDONLY) && features) {
  793. printk(KERN_ERR "NILFS: couldn't mount RDWR because of "
  794. "unsupported optional features (%llx)\n",
  795. (unsigned long long)features);
  796. return -EINVAL;
  797. }
  798. return 0;
  799. }
  800. static int nilfs_get_root_dentry(struct super_block *sb,
  801. struct nilfs_root *root,
  802. struct dentry **root_dentry)
  803. {
  804. struct inode *inode;
  805. struct dentry *dentry;
  806. int ret = 0;
  807. inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
  808. if (IS_ERR(inode)) {
  809. printk(KERN_ERR "NILFS: get root inode failed\n");
  810. ret = PTR_ERR(inode);
  811. goto out;
  812. }
  813. if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
  814. iput(inode);
  815. printk(KERN_ERR "NILFS: corrupt root inode.\n");
  816. ret = -EINVAL;
  817. goto out;
  818. }
  819. if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
  820. dentry = d_find_alias(inode);
  821. if (!dentry) {
  822. dentry = d_make_root(inode);
  823. if (!dentry) {
  824. ret = -ENOMEM;
  825. goto failed_dentry;
  826. }
  827. } else {
  828. iput(inode);
  829. }
  830. } else {
  831. dentry = d_obtain_root(inode);
  832. if (IS_ERR(dentry)) {
  833. ret = PTR_ERR(dentry);
  834. goto failed_dentry;
  835. }
  836. }
  837. *root_dentry = dentry;
  838. out:
  839. return ret;
  840. failed_dentry:
  841. printk(KERN_ERR "NILFS: get root dentry failed\n");
  842. goto out;
  843. }
  844. static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
  845. struct dentry **root_dentry)
  846. {
  847. struct the_nilfs *nilfs = s->s_fs_info;
  848. struct nilfs_root *root;
  849. int ret;
  850. mutex_lock(&nilfs->ns_snapshot_mount_mutex);
  851. down_read(&nilfs->ns_segctor_sem);
  852. ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
  853. up_read(&nilfs->ns_segctor_sem);
  854. if (ret < 0) {
  855. ret = (ret == -ENOENT) ? -EINVAL : ret;
  856. goto out;
  857. } else if (!ret) {
  858. printk(KERN_ERR "NILFS: The specified checkpoint is "
  859. "not a snapshot (checkpoint number=%llu).\n",
  860. (unsigned long long)cno);
  861. ret = -EINVAL;
  862. goto out;
  863. }
  864. ret = nilfs_attach_checkpoint(s, cno, false, &root);
  865. if (ret) {
  866. printk(KERN_ERR "NILFS: error loading snapshot "
  867. "(checkpoint number=%llu).\n",
  868. (unsigned long long)cno);
  869. goto out;
  870. }
  871. ret = nilfs_get_root_dentry(s, root, root_dentry);
  872. nilfs_put_root(root);
  873. out:
  874. mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
  875. return ret;
  876. }
  877. /**
  878. * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
  879. * @root_dentry: root dentry of the tree to be shrunk
  880. *
  881. * This function returns true if the tree was in-use.
  882. */
  883. static bool nilfs_tree_is_busy(struct dentry *root_dentry)
  884. {
  885. shrink_dcache_parent(root_dentry);
  886. return d_count(root_dentry) > 1;
  887. }
  888. int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
  889. {
  890. struct the_nilfs *nilfs = sb->s_fs_info;
  891. struct nilfs_root *root;
  892. struct inode *inode;
  893. struct dentry *dentry;
  894. int ret;
  895. if (cno > nilfs->ns_cno)
  896. return false;
  897. if (cno >= nilfs_last_cno(nilfs))
  898. return true; /* protect recent checkpoints */
  899. ret = false;
  900. root = nilfs_lookup_root(nilfs, cno);
  901. if (root) {
  902. inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
  903. if (inode) {
  904. dentry = d_find_alias(inode);
  905. if (dentry) {
  906. ret = nilfs_tree_is_busy(dentry);
  907. dput(dentry);
  908. }
  909. iput(inode);
  910. }
  911. nilfs_put_root(root);
  912. }
  913. return ret;
  914. }
  915. /**
  916. * nilfs_fill_super() - initialize a super block instance
  917. * @sb: super_block
  918. * @data: mount options
  919. * @silent: silent mode flag
  920. *
  921. * This function is called exclusively by nilfs->ns_mount_mutex.
  922. * So, the recovery process is protected from other simultaneous mounts.
  923. */
  924. static int
  925. nilfs_fill_super(struct super_block *sb, void *data, int silent)
  926. {
  927. struct the_nilfs *nilfs;
  928. struct nilfs_root *fsroot;
  929. __u64 cno;
  930. int err;
  931. nilfs = alloc_nilfs(sb->s_bdev);
  932. if (!nilfs)
  933. return -ENOMEM;
  934. sb->s_fs_info = nilfs;
  935. err = init_nilfs(nilfs, sb, (char *)data);
  936. if (err)
  937. goto failed_nilfs;
  938. sb->s_op = &nilfs_sops;
  939. sb->s_export_op = &nilfs_export_ops;
  940. sb->s_root = NULL;
  941. sb->s_time_gran = 1;
  942. sb->s_max_links = NILFS_LINK_MAX;
  943. sb->s_bdi = &bdev_get_queue(sb->s_bdev)->backing_dev_info;
  944. err = load_nilfs(nilfs, sb);
  945. if (err)
  946. goto failed_nilfs;
  947. cno = nilfs_last_cno(nilfs);
  948. err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
  949. if (err) {
  950. printk(KERN_ERR "NILFS: error loading last checkpoint "
  951. "(checkpoint number=%llu).\n", (unsigned long long)cno);
  952. goto failed_unload;
  953. }
  954. if (!(sb->s_flags & MS_RDONLY)) {
  955. err = nilfs_attach_log_writer(sb, fsroot);
  956. if (err)
  957. goto failed_checkpoint;
  958. }
  959. err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
  960. if (err)
  961. goto failed_segctor;
  962. nilfs_put_root(fsroot);
  963. if (!(sb->s_flags & MS_RDONLY)) {
  964. down_write(&nilfs->ns_sem);
  965. nilfs_setup_super(sb, true);
  966. up_write(&nilfs->ns_sem);
  967. }
  968. return 0;
  969. failed_segctor:
  970. nilfs_detach_log_writer(sb);
  971. failed_checkpoint:
  972. nilfs_put_root(fsroot);
  973. failed_unload:
  974. iput(nilfs->ns_sufile);
  975. iput(nilfs->ns_cpfile);
  976. iput(nilfs->ns_dat);
  977. failed_nilfs:
  978. destroy_nilfs(nilfs);
  979. return err;
  980. }
  981. static int nilfs_remount(struct super_block *sb, int *flags, char *data)
  982. {
  983. struct the_nilfs *nilfs = sb->s_fs_info;
  984. unsigned long old_sb_flags;
  985. unsigned long old_mount_opt;
  986. int err;
  987. sync_filesystem(sb);
  988. old_sb_flags = sb->s_flags;
  989. old_mount_opt = nilfs->ns_mount_opt;
  990. if (!parse_options(data, sb, 1)) {
  991. err = -EINVAL;
  992. goto restore_opts;
  993. }
  994. sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
  995. err = -EINVAL;
  996. if (!nilfs_valid_fs(nilfs)) {
  997. printk(KERN_WARNING "NILFS (device %s): couldn't "
  998. "remount because the filesystem is in an "
  999. "incomplete recovery state.\n", sb->s_id);
  1000. goto restore_opts;
  1001. }
  1002. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  1003. goto out;
  1004. if (*flags & MS_RDONLY) {
  1005. /* Shutting down log writer */
  1006. nilfs_detach_log_writer(sb);
  1007. sb->s_flags |= MS_RDONLY;
  1008. /*
  1009. * Remounting a valid RW partition RDONLY, so set
  1010. * the RDONLY flag and then mark the partition as valid again.
  1011. */
  1012. down_write(&nilfs->ns_sem);
  1013. nilfs_cleanup_super(sb);
  1014. up_write(&nilfs->ns_sem);
  1015. } else {
  1016. __u64 features;
  1017. struct nilfs_root *root;
  1018. /*
  1019. * Mounting a RDONLY partition read-write, so reread and
  1020. * store the current valid flag. (It may have been changed
  1021. * by fsck since we originally mounted the partition.)
  1022. */
  1023. down_read(&nilfs->ns_sem);
  1024. features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
  1025. ~NILFS_FEATURE_COMPAT_RO_SUPP;
  1026. up_read(&nilfs->ns_sem);
  1027. if (features) {
  1028. printk(KERN_WARNING "NILFS (device %s): couldn't "
  1029. "remount RDWR because of unsupported optional "
  1030. "features (%llx)\n",
  1031. sb->s_id, (unsigned long long)features);
  1032. err = -EROFS;
  1033. goto restore_opts;
  1034. }
  1035. sb->s_flags &= ~MS_RDONLY;
  1036. root = NILFS_I(d_inode(sb->s_root))->i_root;
  1037. err = nilfs_attach_log_writer(sb, root);
  1038. if (err)
  1039. goto restore_opts;
  1040. down_write(&nilfs->ns_sem);
  1041. nilfs_setup_super(sb, true);
  1042. up_write(&nilfs->ns_sem);
  1043. }
  1044. out:
  1045. return 0;
  1046. restore_opts:
  1047. sb->s_flags = old_sb_flags;
  1048. nilfs->ns_mount_opt = old_mount_opt;
  1049. return err;
  1050. }
  1051. struct nilfs_super_data {
  1052. struct block_device *bdev;
  1053. __u64 cno;
  1054. int flags;
  1055. };
  1056. /**
  1057. * nilfs_identify - pre-read mount options needed to identify mount instance
  1058. * @data: mount options
  1059. * @sd: nilfs_super_data
  1060. */
  1061. static int nilfs_identify(char *data, struct nilfs_super_data *sd)
  1062. {
  1063. char *p, *options = data;
  1064. substring_t args[MAX_OPT_ARGS];
  1065. int token;
  1066. int ret = 0;
  1067. do {
  1068. p = strsep(&options, ",");
  1069. if (p != NULL && *p) {
  1070. token = match_token(p, tokens, args);
  1071. if (token == Opt_snapshot) {
  1072. if (!(sd->flags & MS_RDONLY)) {
  1073. ret++;
  1074. } else {
  1075. sd->cno = simple_strtoull(args[0].from,
  1076. NULL, 0);
  1077. /*
  1078. * No need to see the end pointer;
  1079. * match_token() has done syntax
  1080. * checking.
  1081. */
  1082. if (sd->cno == 0)
  1083. ret++;
  1084. }
  1085. }
  1086. if (ret)
  1087. printk(KERN_ERR
  1088. "NILFS: invalid mount option: %s\n", p);
  1089. }
  1090. if (!options)
  1091. break;
  1092. BUG_ON(options == data);
  1093. *(options - 1) = ',';
  1094. } while (!ret);
  1095. return ret;
  1096. }
  1097. static int nilfs_set_bdev_super(struct super_block *s, void *data)
  1098. {
  1099. s->s_bdev = data;
  1100. s->s_dev = s->s_bdev->bd_dev;
  1101. return 0;
  1102. }
  1103. static int nilfs_test_bdev_super(struct super_block *s, void *data)
  1104. {
  1105. return (void *)s->s_bdev == data;
  1106. }
  1107. static struct dentry *
  1108. nilfs_mount(struct file_system_type *fs_type, int flags,
  1109. const char *dev_name, void *data)
  1110. {
  1111. struct nilfs_super_data sd;
  1112. struct super_block *s;
  1113. fmode_t mode = FMODE_READ | FMODE_EXCL;
  1114. struct dentry *root_dentry;
  1115. int err, s_new = false;
  1116. if (!(flags & MS_RDONLY))
  1117. mode |= FMODE_WRITE;
  1118. sd.bdev = blkdev_get_by_path(dev_name, mode, fs_type);
  1119. if (IS_ERR(sd.bdev))
  1120. return ERR_CAST(sd.bdev);
  1121. sd.cno = 0;
  1122. sd.flags = flags;
  1123. if (nilfs_identify((char *)data, &sd)) {
  1124. err = -EINVAL;
  1125. goto failed;
  1126. }
  1127. /*
  1128. * once the super is inserted into the list by sget, s_umount
  1129. * will protect the lockfs code from trying to start a snapshot
  1130. * while we are mounting
  1131. */
  1132. mutex_lock(&sd.bdev->bd_fsfreeze_mutex);
  1133. if (sd.bdev->bd_fsfreeze_count > 0) {
  1134. mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
  1135. err = -EBUSY;
  1136. goto failed;
  1137. }
  1138. s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, flags,
  1139. sd.bdev);
  1140. mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
  1141. if (IS_ERR(s)) {
  1142. err = PTR_ERR(s);
  1143. goto failed;
  1144. }
  1145. if (!s->s_root) {
  1146. char b[BDEVNAME_SIZE];
  1147. s_new = true;
  1148. /* New superblock instance created */
  1149. s->s_mode = mode;
  1150. strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id));
  1151. sb_set_blocksize(s, block_size(sd.bdev));
  1152. err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  1153. if (err)
  1154. goto failed_super;
  1155. s->s_flags |= MS_ACTIVE;
  1156. } else if (!sd.cno) {
  1157. if (nilfs_tree_is_busy(s->s_root)) {
  1158. if ((flags ^ s->s_flags) & MS_RDONLY) {
  1159. printk(KERN_ERR "NILFS: the device already "
  1160. "has a %s mount.\n",
  1161. (s->s_flags & MS_RDONLY) ?
  1162. "read-only" : "read/write");
  1163. err = -EBUSY;
  1164. goto failed_super;
  1165. }
  1166. } else {
  1167. /*
  1168. * Try remount to setup mount states if the current
  1169. * tree is not mounted and only snapshots use this sb.
  1170. */
  1171. err = nilfs_remount(s, &flags, data);
  1172. if (err)
  1173. goto failed_super;
  1174. }
  1175. }
  1176. if (sd.cno) {
  1177. err = nilfs_attach_snapshot(s, sd.cno, &root_dentry);
  1178. if (err)
  1179. goto failed_super;
  1180. } else {
  1181. root_dentry = dget(s->s_root);
  1182. }
  1183. if (!s_new)
  1184. blkdev_put(sd.bdev, mode);
  1185. return root_dentry;
  1186. failed_super:
  1187. deactivate_locked_super(s);
  1188. failed:
  1189. if (!s_new)
  1190. blkdev_put(sd.bdev, mode);
  1191. return ERR_PTR(err);
  1192. }
  1193. struct file_system_type nilfs_fs_type = {
  1194. .owner = THIS_MODULE,
  1195. .name = "nilfs2",
  1196. .mount = nilfs_mount,
  1197. .kill_sb = kill_block_super,
  1198. .fs_flags = FS_REQUIRES_DEV,
  1199. };
  1200. MODULE_ALIAS_FS("nilfs2");
  1201. static void nilfs_inode_init_once(void *obj)
  1202. {
  1203. struct nilfs_inode_info *ii = obj;
  1204. INIT_LIST_HEAD(&ii->i_dirty);
  1205. #ifdef CONFIG_NILFS_XATTR
  1206. init_rwsem(&ii->xattr_sem);
  1207. #endif
  1208. address_space_init_once(&ii->i_btnode_cache);
  1209. ii->i_bmap = &ii->i_bmap_data;
  1210. inode_init_once(&ii->vfs_inode);
  1211. }
  1212. static void nilfs_segbuf_init_once(void *obj)
  1213. {
  1214. memset(obj, 0, sizeof(struct nilfs_segment_buffer));
  1215. }
  1216. static void nilfs_destroy_cachep(void)
  1217. {
  1218. /*
  1219. * Make sure all delayed rcu free inodes are flushed before we
  1220. * destroy cache.
  1221. */
  1222. rcu_barrier();
  1223. if (nilfs_inode_cachep)
  1224. kmem_cache_destroy(nilfs_inode_cachep);
  1225. if (nilfs_transaction_cachep)
  1226. kmem_cache_destroy(nilfs_transaction_cachep);
  1227. if (nilfs_segbuf_cachep)
  1228. kmem_cache_destroy(nilfs_segbuf_cachep);
  1229. if (nilfs_btree_path_cache)
  1230. kmem_cache_destroy(nilfs_btree_path_cache);
  1231. }
  1232. static int __init nilfs_init_cachep(void)
  1233. {
  1234. nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
  1235. sizeof(struct nilfs_inode_info), 0,
  1236. SLAB_RECLAIM_ACCOUNT, nilfs_inode_init_once);
  1237. if (!nilfs_inode_cachep)
  1238. goto fail;
  1239. nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
  1240. sizeof(struct nilfs_transaction_info), 0,
  1241. SLAB_RECLAIM_ACCOUNT, NULL);
  1242. if (!nilfs_transaction_cachep)
  1243. goto fail;
  1244. nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
  1245. sizeof(struct nilfs_segment_buffer), 0,
  1246. SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
  1247. if (!nilfs_segbuf_cachep)
  1248. goto fail;
  1249. nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
  1250. sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
  1251. 0, 0, NULL);
  1252. if (!nilfs_btree_path_cache)
  1253. goto fail;
  1254. return 0;
  1255. fail:
  1256. nilfs_destroy_cachep();
  1257. return -ENOMEM;
  1258. }
  1259. static int __init init_nilfs_fs(void)
  1260. {
  1261. int err;
  1262. err = nilfs_init_cachep();
  1263. if (err)
  1264. goto fail;
  1265. err = nilfs_sysfs_init();
  1266. if (err)
  1267. goto free_cachep;
  1268. err = register_filesystem(&nilfs_fs_type);
  1269. if (err)
  1270. goto deinit_sysfs_entry;
  1271. printk(KERN_INFO "NILFS version 2 loaded\n");
  1272. return 0;
  1273. deinit_sysfs_entry:
  1274. nilfs_sysfs_exit();
  1275. free_cachep:
  1276. nilfs_destroy_cachep();
  1277. fail:
  1278. return err;
  1279. }
  1280. static void __exit exit_nilfs_fs(void)
  1281. {
  1282. nilfs_destroy_cachep();
  1283. nilfs_sysfs_exit();
  1284. unregister_filesystem(&nilfs_fs_type);
  1285. }
  1286. module_init(init_nilfs_fs)
  1287. module_exit(exit_nilfs_fs)