super.c 36 KB

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