super.c 36 KB

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