super.c 36 KB

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