super.c 34 KB

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