super.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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/random.h>
  46. #include <linux/crc32.h>
  47. #include <linux/smp_lock.h>
  48. #include <linux/vfs.h>
  49. #include <linux/writeback.h>
  50. #include <linux/kobject.h>
  51. #include <linux/exportfs.h>
  52. #include <linux/seq_file.h>
  53. #include <linux/mount.h>
  54. #include "nilfs.h"
  55. #include "mdt.h"
  56. #include "alloc.h"
  57. #include "page.h"
  58. #include "cpfile.h"
  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 int nilfs_remount(struct super_block *sb, int *flags, char *data);
  68. /**
  69. * nilfs_error() - report failure condition on a filesystem
  70. *
  71. * nilfs_error() sets an ERROR_FS flag on the superblock as well as
  72. * reporting an error message. It should be called when NILFS detects
  73. * incoherences or defects of meta data on disk. As for sustainable
  74. * errors such as a single-shot I/O error, nilfs_warning() or the printk()
  75. * function should be used instead.
  76. *
  77. * The segment constructor must not call this function because it can
  78. * kill itself.
  79. */
  80. void nilfs_error(struct super_block *sb, const char *function,
  81. const char *fmt, ...)
  82. {
  83. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  84. va_list args;
  85. va_start(args, fmt);
  86. printk(KERN_CRIT "NILFS error (device %s): %s: ", sb->s_id, function);
  87. vprintk(fmt, args);
  88. printk("\n");
  89. va_end(args);
  90. if (!(sb->s_flags & MS_RDONLY)) {
  91. struct the_nilfs *nilfs = sbi->s_nilfs;
  92. down_write(&nilfs->ns_sem);
  93. if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
  94. nilfs->ns_mount_state |= NILFS_ERROR_FS;
  95. nilfs->ns_sbp[0]->s_state |=
  96. cpu_to_le16(NILFS_ERROR_FS);
  97. nilfs_commit_super(sbi, 1);
  98. }
  99. up_write(&nilfs->ns_sem);
  100. if (nilfs_test_opt(sbi, ERRORS_RO)) {
  101. printk(KERN_CRIT "Remounting filesystem read-only\n");
  102. sb->s_flags |= MS_RDONLY;
  103. }
  104. }
  105. if (nilfs_test_opt(sbi, ERRORS_PANIC))
  106. panic("NILFS (device %s): panic forced after error\n",
  107. sb->s_id);
  108. }
  109. void nilfs_warning(struct super_block *sb, const char *function,
  110. const char *fmt, ...)
  111. {
  112. va_list args;
  113. va_start(args, fmt);
  114. printk(KERN_WARNING "NILFS warning (device %s): %s: ",
  115. sb->s_id, function);
  116. vprintk(fmt, args);
  117. printk("\n");
  118. va_end(args);
  119. }
  120. static struct kmem_cache *nilfs_inode_cachep;
  121. struct inode *nilfs_alloc_inode_common(struct the_nilfs *nilfs)
  122. {
  123. struct nilfs_inode_info *ii;
  124. ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
  125. if (!ii)
  126. return NULL;
  127. ii->i_bh = NULL;
  128. ii->i_state = 0;
  129. ii->vfs_inode.i_version = 1;
  130. nilfs_btnode_cache_init(&ii->i_btnode_cache, nilfs->ns_bdi);
  131. return &ii->vfs_inode;
  132. }
  133. struct inode *nilfs_alloc_inode(struct super_block *sb)
  134. {
  135. return nilfs_alloc_inode_common(NILFS_SB(sb)->s_nilfs);
  136. }
  137. void nilfs_destroy_inode(struct inode *inode)
  138. {
  139. kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
  140. }
  141. static void init_once(void *obj)
  142. {
  143. struct nilfs_inode_info *ii = obj;
  144. INIT_LIST_HEAD(&ii->i_dirty);
  145. #ifdef CONFIG_NILFS_XATTR
  146. init_rwsem(&ii->xattr_sem);
  147. #endif
  148. nilfs_btnode_cache_init_once(&ii->i_btnode_cache);
  149. ii->i_bmap = (struct nilfs_bmap *)&ii->i_bmap_union;
  150. inode_init_once(&ii->vfs_inode);
  151. }
  152. static int nilfs_init_inode_cache(void)
  153. {
  154. nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
  155. sizeof(struct nilfs_inode_info),
  156. 0, SLAB_RECLAIM_ACCOUNT,
  157. init_once);
  158. return (nilfs_inode_cachep == NULL) ? -ENOMEM : 0;
  159. }
  160. static inline void nilfs_destroy_inode_cache(void)
  161. {
  162. kmem_cache_destroy(nilfs_inode_cachep);
  163. }
  164. static void nilfs_clear_inode(struct inode *inode)
  165. {
  166. struct nilfs_inode_info *ii = NILFS_I(inode);
  167. /*
  168. * Free resources allocated in nilfs_read_inode(), here.
  169. */
  170. BUG_ON(!list_empty(&ii->i_dirty));
  171. brelse(ii->i_bh);
  172. ii->i_bh = NULL;
  173. if (test_bit(NILFS_I_BMAP, &ii->i_state))
  174. nilfs_bmap_clear(ii->i_bmap);
  175. nilfs_btnode_cache_clear(&ii->i_btnode_cache);
  176. }
  177. static int nilfs_sync_super(struct nilfs_sb_info *sbi, int dupsb)
  178. {
  179. struct the_nilfs *nilfs = sbi->s_nilfs;
  180. int err;
  181. int barrier_done = 0;
  182. if (nilfs_test_opt(sbi, BARRIER)) {
  183. set_buffer_ordered(nilfs->ns_sbh[0]);
  184. barrier_done = 1;
  185. }
  186. retry:
  187. set_buffer_dirty(nilfs->ns_sbh[0]);
  188. err = sync_dirty_buffer(nilfs->ns_sbh[0]);
  189. if (err == -EOPNOTSUPP && barrier_done) {
  190. nilfs_warning(sbi->s_super, __func__,
  191. "barrier-based sync failed. "
  192. "disabling barriers\n");
  193. nilfs_clear_opt(sbi, BARRIER);
  194. barrier_done = 0;
  195. clear_buffer_ordered(nilfs->ns_sbh[0]);
  196. goto retry;
  197. }
  198. if (unlikely(err)) {
  199. printk(KERN_ERR
  200. "NILFS: unable to write superblock (err=%d)\n", err);
  201. if (err == -EIO && nilfs->ns_sbh[1]) {
  202. nilfs_fall_back_super_block(nilfs);
  203. goto retry;
  204. }
  205. } else {
  206. struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
  207. /*
  208. * The latest segment becomes trailable from the position
  209. * written in superblock.
  210. */
  211. clear_nilfs_discontinued(nilfs);
  212. /* update GC protection for recent segments */
  213. if (nilfs->ns_sbh[1]) {
  214. sbp = NULL;
  215. if (dupsb) {
  216. set_buffer_dirty(nilfs->ns_sbh[1]);
  217. if (!sync_dirty_buffer(nilfs->ns_sbh[1]))
  218. sbp = nilfs->ns_sbp[1];
  219. }
  220. }
  221. if (sbp) {
  222. spin_lock(&nilfs->ns_last_segment_lock);
  223. nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
  224. spin_unlock(&nilfs->ns_last_segment_lock);
  225. }
  226. }
  227. return err;
  228. }
  229. int nilfs_commit_super(struct nilfs_sb_info *sbi, int dupsb)
  230. {
  231. struct the_nilfs *nilfs = sbi->s_nilfs;
  232. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  233. sector_t nfreeblocks;
  234. time_t t;
  235. int err;
  236. /* nilfs->sem must be locked by the caller. */
  237. if (sbp[0]->s_magic != NILFS_SUPER_MAGIC) {
  238. if (sbp[1] && sbp[1]->s_magic == NILFS_SUPER_MAGIC)
  239. nilfs_swap_super_block(nilfs);
  240. else {
  241. printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
  242. sbi->s_super->s_id);
  243. return -EIO;
  244. }
  245. }
  246. err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
  247. if (unlikely(err)) {
  248. printk(KERN_ERR "NILFS: failed to count free blocks\n");
  249. return err;
  250. }
  251. spin_lock(&nilfs->ns_last_segment_lock);
  252. sbp[0]->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
  253. sbp[0]->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
  254. sbp[0]->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
  255. spin_unlock(&nilfs->ns_last_segment_lock);
  256. t = get_seconds();
  257. nilfs->ns_sbwtime[0] = t;
  258. sbp[0]->s_free_blocks_count = cpu_to_le64(nfreeblocks);
  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 (dupsb && sbp[1]) {
  265. memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
  266. nilfs->ns_sbwtime[1] = t;
  267. }
  268. clear_nilfs_sb_dirty(nilfs);
  269. return nilfs_sync_super(sbi, dupsb);
  270. }
  271. static void nilfs_put_super(struct super_block *sb)
  272. {
  273. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  274. struct the_nilfs *nilfs = sbi->s_nilfs;
  275. lock_kernel();
  276. nilfs_detach_segment_constructor(sbi);
  277. if (!(sb->s_flags & MS_RDONLY)) {
  278. down_write(&nilfs->ns_sem);
  279. nilfs->ns_sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
  280. nilfs_commit_super(sbi, 1);
  281. up_write(&nilfs->ns_sem);
  282. }
  283. down_write(&nilfs->ns_super_sem);
  284. if (nilfs->ns_current == sbi)
  285. nilfs->ns_current = NULL;
  286. up_write(&nilfs->ns_super_sem);
  287. nilfs_detach_checkpoint(sbi);
  288. put_nilfs(sbi->s_nilfs);
  289. sbi->s_super = NULL;
  290. sb->s_fs_info = NULL;
  291. nilfs_put_sbinfo(sbi);
  292. unlock_kernel();
  293. }
  294. static int nilfs_sync_fs(struct super_block *sb, int wait)
  295. {
  296. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  297. struct the_nilfs *nilfs = sbi->s_nilfs;
  298. int err = 0;
  299. /* This function is called when super block should be written back */
  300. if (wait)
  301. err = nilfs_construct_segment(sb);
  302. down_write(&nilfs->ns_sem);
  303. if (nilfs_sb_dirty(nilfs))
  304. nilfs_commit_super(sbi, 1);
  305. up_write(&nilfs->ns_sem);
  306. return err;
  307. }
  308. int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno)
  309. {
  310. struct the_nilfs *nilfs = sbi->s_nilfs;
  311. struct nilfs_checkpoint *raw_cp;
  312. struct buffer_head *bh_cp;
  313. int err;
  314. down_write(&nilfs->ns_super_sem);
  315. list_add(&sbi->s_list, &nilfs->ns_supers);
  316. up_write(&nilfs->ns_super_sem);
  317. sbi->s_ifile = nilfs_ifile_new(sbi, nilfs->ns_inode_size);
  318. if (!sbi->s_ifile)
  319. return -ENOMEM;
  320. down_read(&nilfs->ns_segctor_sem);
  321. err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
  322. &bh_cp);
  323. up_read(&nilfs->ns_segctor_sem);
  324. if (unlikely(err)) {
  325. if (err == -ENOENT || err == -EINVAL) {
  326. printk(KERN_ERR
  327. "NILFS: Invalid checkpoint "
  328. "(checkpoint number=%llu)\n",
  329. (unsigned long long)cno);
  330. err = -EINVAL;
  331. }
  332. goto failed;
  333. }
  334. err = nilfs_read_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode);
  335. if (unlikely(err))
  336. goto failed_bh;
  337. atomic_set(&sbi->s_inodes_count, le64_to_cpu(raw_cp->cp_inodes_count));
  338. atomic_set(&sbi->s_blocks_count, le64_to_cpu(raw_cp->cp_blocks_count));
  339. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  340. return 0;
  341. failed_bh:
  342. nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
  343. failed:
  344. nilfs_mdt_destroy(sbi->s_ifile);
  345. sbi->s_ifile = NULL;
  346. down_write(&nilfs->ns_super_sem);
  347. list_del_init(&sbi->s_list);
  348. up_write(&nilfs->ns_super_sem);
  349. return err;
  350. }
  351. void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi)
  352. {
  353. struct the_nilfs *nilfs = sbi->s_nilfs;
  354. nilfs_mdt_destroy(sbi->s_ifile);
  355. sbi->s_ifile = NULL;
  356. down_write(&nilfs->ns_super_sem);
  357. list_del_init(&sbi->s_list);
  358. up_write(&nilfs->ns_super_sem);
  359. }
  360. static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  361. {
  362. struct super_block *sb = dentry->d_sb;
  363. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  364. struct the_nilfs *nilfs = sbi->s_nilfs;
  365. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  366. unsigned long long blocks;
  367. unsigned long overhead;
  368. unsigned long nrsvblocks;
  369. sector_t nfreeblocks;
  370. int err;
  371. /*
  372. * Compute all of the segment blocks
  373. *
  374. * The blocks before first segment and after last segment
  375. * are excluded.
  376. */
  377. blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
  378. - nilfs->ns_first_data_block;
  379. nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
  380. /*
  381. * Compute the overhead
  382. *
  383. * When distributing meta data blocks outside segment structure,
  384. * We must count them as the overhead.
  385. */
  386. overhead = 0;
  387. err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
  388. if (unlikely(err))
  389. return err;
  390. buf->f_type = NILFS_SUPER_MAGIC;
  391. buf->f_bsize = sb->s_blocksize;
  392. buf->f_blocks = blocks - overhead;
  393. buf->f_bfree = nfreeblocks;
  394. buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
  395. (buf->f_bfree - nrsvblocks) : 0;
  396. buf->f_files = atomic_read(&sbi->s_inodes_count);
  397. buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */
  398. buf->f_namelen = NILFS_NAME_LEN;
  399. buf->f_fsid.val[0] = (u32)id;
  400. buf->f_fsid.val[1] = (u32)(id >> 32);
  401. return 0;
  402. }
  403. static int nilfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
  404. {
  405. struct super_block *sb = vfs->mnt_sb;
  406. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  407. if (!nilfs_test_opt(sbi, BARRIER))
  408. seq_printf(seq, ",nobarrier");
  409. if (nilfs_test_opt(sbi, SNAPSHOT))
  410. seq_printf(seq, ",cp=%llu",
  411. (unsigned long long int)sbi->s_snapshot_cno);
  412. if (nilfs_test_opt(sbi, ERRORS_RO))
  413. seq_printf(seq, ",errors=remount-ro");
  414. if (nilfs_test_opt(sbi, ERRORS_PANIC))
  415. seq_printf(seq, ",errors=panic");
  416. if (nilfs_test_opt(sbi, STRICT_ORDER))
  417. seq_printf(seq, ",order=strict");
  418. if (nilfs_test_opt(sbi, NORECOVERY))
  419. seq_printf(seq, ",norecovery");
  420. if (nilfs_test_opt(sbi, DISCARD))
  421. seq_printf(seq, ",discard");
  422. return 0;
  423. }
  424. static const struct super_operations nilfs_sops = {
  425. .alloc_inode = nilfs_alloc_inode,
  426. .destroy_inode = nilfs_destroy_inode,
  427. .dirty_inode = nilfs_dirty_inode,
  428. /* .write_inode = nilfs_write_inode, */
  429. /* .put_inode = nilfs_put_inode, */
  430. /* .drop_inode = nilfs_drop_inode, */
  431. .delete_inode = nilfs_delete_inode,
  432. .put_super = nilfs_put_super,
  433. /* .write_super = nilfs_write_super, */
  434. .sync_fs = nilfs_sync_fs,
  435. /* .write_super_lockfs */
  436. /* .unlockfs */
  437. .statfs = nilfs_statfs,
  438. .remount_fs = nilfs_remount,
  439. .clear_inode = nilfs_clear_inode,
  440. /* .umount_begin */
  441. .show_options = nilfs_show_options
  442. };
  443. static struct inode *
  444. nilfs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation)
  445. {
  446. struct inode *inode;
  447. if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO &&
  448. ino != NILFS_SKETCH_INO)
  449. return ERR_PTR(-ESTALE);
  450. inode = nilfs_iget(sb, ino);
  451. if (IS_ERR(inode))
  452. return ERR_CAST(inode);
  453. if (generation && inode->i_generation != generation) {
  454. iput(inode);
  455. return ERR_PTR(-ESTALE);
  456. }
  457. return inode;
  458. }
  459. static struct dentry *
  460. nilfs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
  461. int fh_type)
  462. {
  463. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  464. nilfs_nfs_get_inode);
  465. }
  466. static struct dentry *
  467. nilfs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len,
  468. int fh_type)
  469. {
  470. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  471. nilfs_nfs_get_inode);
  472. }
  473. static const struct export_operations nilfs_export_ops = {
  474. .fh_to_dentry = nilfs_fh_to_dentry,
  475. .fh_to_parent = nilfs_fh_to_parent,
  476. .get_parent = nilfs_get_parent,
  477. };
  478. enum {
  479. Opt_err_cont, Opt_err_panic, Opt_err_ro,
  480. Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
  481. Opt_discard, Opt_err,
  482. };
  483. static match_table_t tokens = {
  484. {Opt_err_cont, "errors=continue"},
  485. {Opt_err_panic, "errors=panic"},
  486. {Opt_err_ro, "errors=remount-ro"},
  487. {Opt_nobarrier, "nobarrier"},
  488. {Opt_snapshot, "cp=%u"},
  489. {Opt_order, "order=%s"},
  490. {Opt_norecovery, "norecovery"},
  491. {Opt_discard, "discard"},
  492. {Opt_err, NULL}
  493. };
  494. static int parse_options(char *options, struct super_block *sb)
  495. {
  496. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  497. char *p;
  498. substring_t args[MAX_OPT_ARGS];
  499. int option;
  500. if (!options)
  501. return 1;
  502. while ((p = strsep(&options, ",")) != NULL) {
  503. int token;
  504. if (!*p)
  505. continue;
  506. token = match_token(p, tokens, args);
  507. switch (token) {
  508. case Opt_nobarrier:
  509. nilfs_clear_opt(sbi, BARRIER);
  510. break;
  511. case Opt_order:
  512. if (strcmp(args[0].from, "relaxed") == 0)
  513. /* Ordered data semantics */
  514. nilfs_clear_opt(sbi, STRICT_ORDER);
  515. else if (strcmp(args[0].from, "strict") == 0)
  516. /* Strict in-order semantics */
  517. nilfs_set_opt(sbi, STRICT_ORDER);
  518. else
  519. return 0;
  520. break;
  521. case Opt_err_panic:
  522. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC);
  523. break;
  524. case Opt_err_ro:
  525. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO);
  526. break;
  527. case Opt_err_cont:
  528. nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT);
  529. break;
  530. case Opt_snapshot:
  531. if (match_int(&args[0], &option) || option <= 0)
  532. return 0;
  533. if (!(sb->s_flags & MS_RDONLY))
  534. return 0;
  535. sbi->s_snapshot_cno = option;
  536. nilfs_set_opt(sbi, SNAPSHOT);
  537. break;
  538. case Opt_norecovery:
  539. nilfs_set_opt(sbi, NORECOVERY);
  540. break;
  541. case Opt_discard:
  542. nilfs_set_opt(sbi, DISCARD);
  543. break;
  544. default:
  545. printk(KERN_ERR
  546. "NILFS: Unrecognized mount option \"%s\"\n", p);
  547. return 0;
  548. }
  549. }
  550. return 1;
  551. }
  552. static inline void
  553. nilfs_set_default_options(struct nilfs_sb_info *sbi,
  554. struct nilfs_super_block *sbp)
  555. {
  556. sbi->s_mount_opt =
  557. NILFS_MOUNT_ERRORS_CONT | NILFS_MOUNT_BARRIER;
  558. }
  559. static int nilfs_setup_super(struct nilfs_sb_info *sbi)
  560. {
  561. struct the_nilfs *nilfs = sbi->s_nilfs;
  562. struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
  563. int max_mnt_count = le16_to_cpu(sbp->s_max_mnt_count);
  564. int mnt_count = le16_to_cpu(sbp->s_mnt_count);
  565. /* nilfs->sem must be locked by the caller. */
  566. if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
  567. printk(KERN_WARNING
  568. "NILFS warning: mounting fs with errors\n");
  569. #if 0
  570. } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
  571. printk(KERN_WARNING
  572. "NILFS warning: maximal mount count reached\n");
  573. #endif
  574. }
  575. if (!max_mnt_count)
  576. sbp->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
  577. sbp->s_mnt_count = cpu_to_le16(mnt_count + 1);
  578. sbp->s_state = cpu_to_le16(le16_to_cpu(sbp->s_state) & ~NILFS_VALID_FS);
  579. sbp->s_mtime = cpu_to_le64(get_seconds());
  580. return nilfs_commit_super(sbi, 1);
  581. }
  582. struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
  583. u64 pos, int blocksize,
  584. struct buffer_head **pbh)
  585. {
  586. unsigned long long sb_index = pos;
  587. unsigned long offset;
  588. offset = do_div(sb_index, blocksize);
  589. *pbh = sb_bread(sb, sb_index);
  590. if (!*pbh)
  591. return NULL;
  592. return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
  593. }
  594. int nilfs_store_magic_and_option(struct super_block *sb,
  595. struct nilfs_super_block *sbp,
  596. char *data)
  597. {
  598. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  599. sb->s_magic = le16_to_cpu(sbp->s_magic);
  600. /* FS independent flags */
  601. #ifdef NILFS_ATIME_DISABLE
  602. sb->s_flags |= MS_NOATIME;
  603. #endif
  604. nilfs_set_default_options(sbi, sbp);
  605. sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid);
  606. sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid);
  607. sbi->s_interval = le32_to_cpu(sbp->s_c_interval);
  608. sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max);
  609. return !parse_options(data, sb) ? -EINVAL : 0 ;
  610. }
  611. /**
  612. * nilfs_fill_super() - initialize a super block instance
  613. * @sb: super_block
  614. * @data: mount options
  615. * @silent: silent mode flag
  616. * @nilfs: the_nilfs struct
  617. *
  618. * This function is called exclusively by nilfs->ns_mount_mutex.
  619. * So, the recovery process is protected from other simultaneous mounts.
  620. */
  621. static int
  622. nilfs_fill_super(struct super_block *sb, void *data, int silent,
  623. struct the_nilfs *nilfs)
  624. {
  625. struct nilfs_sb_info *sbi;
  626. struct inode *root;
  627. __u64 cno;
  628. int err;
  629. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  630. if (!sbi)
  631. return -ENOMEM;
  632. sb->s_fs_info = sbi;
  633. get_nilfs(nilfs);
  634. sbi->s_nilfs = nilfs;
  635. sbi->s_super = sb;
  636. atomic_set(&sbi->s_count, 1);
  637. err = init_nilfs(nilfs, sbi, (char *)data);
  638. if (err)
  639. goto failed_sbi;
  640. spin_lock_init(&sbi->s_inode_lock);
  641. INIT_LIST_HEAD(&sbi->s_dirty_files);
  642. INIT_LIST_HEAD(&sbi->s_list);
  643. /*
  644. * Following initialization is overlapped because
  645. * nilfs_sb_info structure has been cleared at the beginning.
  646. * But we reserve them to keep our interest and make ready
  647. * for the future change.
  648. */
  649. get_random_bytes(&sbi->s_next_generation,
  650. sizeof(sbi->s_next_generation));
  651. spin_lock_init(&sbi->s_next_gen_lock);
  652. sb->s_op = &nilfs_sops;
  653. sb->s_export_op = &nilfs_export_ops;
  654. sb->s_root = NULL;
  655. sb->s_time_gran = 1;
  656. err = load_nilfs(nilfs, sbi);
  657. if (err)
  658. goto failed_sbi;
  659. cno = nilfs_last_cno(nilfs);
  660. if (sb->s_flags & MS_RDONLY) {
  661. if (nilfs_test_opt(sbi, SNAPSHOT)) {
  662. down_read(&nilfs->ns_segctor_sem);
  663. err = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile,
  664. sbi->s_snapshot_cno);
  665. up_read(&nilfs->ns_segctor_sem);
  666. if (err < 0) {
  667. if (err == -ENOENT)
  668. err = -EINVAL;
  669. goto failed_sbi;
  670. }
  671. if (!err) {
  672. printk(KERN_ERR
  673. "NILFS: The specified checkpoint is "
  674. "not a snapshot "
  675. "(checkpoint number=%llu).\n",
  676. (unsigned long long)sbi->s_snapshot_cno);
  677. err = -EINVAL;
  678. goto failed_sbi;
  679. }
  680. cno = sbi->s_snapshot_cno;
  681. } else
  682. /* Read-only mount */
  683. sbi->s_snapshot_cno = cno;
  684. }
  685. err = nilfs_attach_checkpoint(sbi, cno);
  686. if (err) {
  687. printk(KERN_ERR "NILFS: error loading a checkpoint"
  688. " (checkpoint number=%llu).\n", (unsigned long long)cno);
  689. goto failed_sbi;
  690. }
  691. if (!(sb->s_flags & MS_RDONLY)) {
  692. err = nilfs_attach_segment_constructor(sbi);
  693. if (err)
  694. goto failed_checkpoint;
  695. }
  696. root = nilfs_iget(sb, NILFS_ROOT_INO);
  697. if (IS_ERR(root)) {
  698. printk(KERN_ERR "NILFS: get root inode failed\n");
  699. err = PTR_ERR(root);
  700. goto failed_segctor;
  701. }
  702. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  703. iput(root);
  704. printk(KERN_ERR "NILFS: corrupt root inode.\n");
  705. err = -EINVAL;
  706. goto failed_segctor;
  707. }
  708. sb->s_root = d_alloc_root(root);
  709. if (!sb->s_root) {
  710. iput(root);
  711. printk(KERN_ERR "NILFS: get root dentry failed\n");
  712. err = -ENOMEM;
  713. goto failed_segctor;
  714. }
  715. if (!(sb->s_flags & MS_RDONLY)) {
  716. down_write(&nilfs->ns_sem);
  717. nilfs_setup_super(sbi);
  718. up_write(&nilfs->ns_sem);
  719. }
  720. down_write(&nilfs->ns_super_sem);
  721. if (!nilfs_test_opt(sbi, SNAPSHOT))
  722. nilfs->ns_current = sbi;
  723. up_write(&nilfs->ns_super_sem);
  724. return 0;
  725. failed_segctor:
  726. nilfs_detach_segment_constructor(sbi);
  727. failed_checkpoint:
  728. nilfs_detach_checkpoint(sbi);
  729. failed_sbi:
  730. put_nilfs(nilfs);
  731. sb->s_fs_info = NULL;
  732. nilfs_put_sbinfo(sbi);
  733. return err;
  734. }
  735. static int nilfs_remount(struct super_block *sb, int *flags, char *data)
  736. {
  737. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  738. struct nilfs_super_block *sbp;
  739. struct the_nilfs *nilfs = sbi->s_nilfs;
  740. unsigned long old_sb_flags;
  741. struct nilfs_mount_options old_opts;
  742. int err;
  743. lock_kernel();
  744. down_write(&nilfs->ns_super_sem);
  745. old_sb_flags = sb->s_flags;
  746. old_opts.mount_opt = sbi->s_mount_opt;
  747. old_opts.snapshot_cno = sbi->s_snapshot_cno;
  748. if (!parse_options(data, sb)) {
  749. err = -EINVAL;
  750. goto restore_opts;
  751. }
  752. sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
  753. if ((*flags & MS_RDONLY) &&
  754. sbi->s_snapshot_cno != old_opts.snapshot_cno) {
  755. printk(KERN_WARNING "NILFS (device %s): couldn't "
  756. "remount to a different snapshot.\n",
  757. sb->s_id);
  758. err = -EINVAL;
  759. goto restore_opts;
  760. }
  761. if (!nilfs_valid_fs(nilfs)) {
  762. printk(KERN_WARNING "NILFS (device %s): couldn't "
  763. "remount because the filesystem is in an "
  764. "incomplete recovery state.\n", sb->s_id);
  765. err = -EINVAL;
  766. goto restore_opts;
  767. }
  768. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  769. goto out;
  770. if (*flags & MS_RDONLY) {
  771. /* Shutting down the segment constructor */
  772. nilfs_detach_segment_constructor(sbi);
  773. sb->s_flags |= MS_RDONLY;
  774. sbi->s_snapshot_cno = nilfs_last_cno(nilfs);
  775. /* nilfs_set_opt(sbi, SNAPSHOT); */
  776. /*
  777. * Remounting a valid RW partition RDONLY, so set
  778. * the RDONLY flag and then mark the partition as valid again.
  779. */
  780. down_write(&nilfs->ns_sem);
  781. sbp = nilfs->ns_sbp[0];
  782. if (!(sbp->s_state & le16_to_cpu(NILFS_VALID_FS)) &&
  783. (nilfs->ns_mount_state & NILFS_VALID_FS))
  784. sbp->s_state = cpu_to_le16(nilfs->ns_mount_state);
  785. sbp->s_mtime = cpu_to_le64(get_seconds());
  786. nilfs_commit_super(sbi, 1);
  787. up_write(&nilfs->ns_sem);
  788. } else {
  789. /*
  790. * Mounting a RDONLY partition read-write, so reread and
  791. * store the current valid flag. (It may have been changed
  792. * by fsck since we originally mounted the partition.)
  793. */
  794. if (nilfs->ns_current && nilfs->ns_current != sbi) {
  795. printk(KERN_WARNING "NILFS (device %s): couldn't "
  796. "remount because an RW-mount exists.\n",
  797. sb->s_id);
  798. err = -EBUSY;
  799. goto restore_opts;
  800. }
  801. if (sbi->s_snapshot_cno != nilfs_last_cno(nilfs)) {
  802. printk(KERN_WARNING "NILFS (device %s): couldn't "
  803. "remount because the current RO-mount is not "
  804. "the latest one.\n",
  805. sb->s_id);
  806. err = -EINVAL;
  807. goto restore_opts;
  808. }
  809. sb->s_flags &= ~MS_RDONLY;
  810. nilfs_clear_opt(sbi, SNAPSHOT);
  811. sbi->s_snapshot_cno = 0;
  812. err = nilfs_attach_segment_constructor(sbi);
  813. if (err)
  814. goto restore_opts;
  815. down_write(&nilfs->ns_sem);
  816. nilfs_setup_super(sbi);
  817. up_write(&nilfs->ns_sem);
  818. nilfs->ns_current = sbi;
  819. }
  820. out:
  821. up_write(&nilfs->ns_super_sem);
  822. unlock_kernel();
  823. return 0;
  824. restore_opts:
  825. sb->s_flags = old_sb_flags;
  826. sbi->s_mount_opt = old_opts.mount_opt;
  827. sbi->s_snapshot_cno = old_opts.snapshot_cno;
  828. up_write(&nilfs->ns_super_sem);
  829. unlock_kernel();
  830. return err;
  831. }
  832. struct nilfs_super_data {
  833. struct block_device *bdev;
  834. struct nilfs_sb_info *sbi;
  835. __u64 cno;
  836. int flags;
  837. };
  838. /**
  839. * nilfs_identify - pre-read mount options needed to identify mount instance
  840. * @data: mount options
  841. * @sd: nilfs_super_data
  842. */
  843. static int nilfs_identify(char *data, struct nilfs_super_data *sd)
  844. {
  845. char *p, *options = data;
  846. substring_t args[MAX_OPT_ARGS];
  847. int option, token;
  848. int ret = 0;
  849. do {
  850. p = strsep(&options, ",");
  851. if (p != NULL && *p) {
  852. token = match_token(p, tokens, args);
  853. if (token == Opt_snapshot) {
  854. if (!(sd->flags & MS_RDONLY))
  855. ret++;
  856. else {
  857. ret = match_int(&args[0], &option);
  858. if (!ret) {
  859. if (option > 0)
  860. sd->cno = option;
  861. else
  862. ret++;
  863. }
  864. }
  865. }
  866. if (ret)
  867. printk(KERN_ERR
  868. "NILFS: invalid mount option: %s\n", p);
  869. }
  870. if (!options)
  871. break;
  872. BUG_ON(options == data);
  873. *(options - 1) = ',';
  874. } while (!ret);
  875. return ret;
  876. }
  877. static int nilfs_set_bdev_super(struct super_block *s, void *data)
  878. {
  879. struct nilfs_super_data *sd = data;
  880. s->s_bdev = sd->bdev;
  881. s->s_dev = s->s_bdev->bd_dev;
  882. return 0;
  883. }
  884. static int nilfs_test_bdev_super(struct super_block *s, void *data)
  885. {
  886. struct nilfs_super_data *sd = data;
  887. return sd->sbi && s->s_fs_info == (void *)sd->sbi;
  888. }
  889. static int
  890. nilfs_get_sb(struct file_system_type *fs_type, int flags,
  891. const char *dev_name, void *data, struct vfsmount *mnt)
  892. {
  893. struct nilfs_super_data sd;
  894. struct super_block *s;
  895. struct the_nilfs *nilfs;
  896. int err, need_to_close = 1;
  897. sd.bdev = open_bdev_exclusive(dev_name, flags, fs_type);
  898. if (IS_ERR(sd.bdev))
  899. return PTR_ERR(sd.bdev);
  900. /*
  901. * To get mount instance using sget() vfs-routine, NILFS needs
  902. * much more information than normal filesystems to identify mount
  903. * instance. For snapshot mounts, not only a mount type (ro-mount
  904. * or rw-mount) but also a checkpoint number is required.
  905. */
  906. sd.cno = 0;
  907. sd.flags = flags;
  908. if (nilfs_identify((char *)data, &sd)) {
  909. err = -EINVAL;
  910. goto failed;
  911. }
  912. nilfs = find_or_create_nilfs(sd.bdev);
  913. if (!nilfs) {
  914. err = -ENOMEM;
  915. goto failed;
  916. }
  917. mutex_lock(&nilfs->ns_mount_mutex);
  918. if (!sd.cno) {
  919. /*
  920. * Check if an exclusive mount exists or not.
  921. * Snapshot mounts coexist with a current mount
  922. * (i.e. rw-mount or ro-mount), whereas rw-mount and
  923. * ro-mount are mutually exclusive.
  924. */
  925. down_read(&nilfs->ns_super_sem);
  926. if (nilfs->ns_current &&
  927. ((nilfs->ns_current->s_super->s_flags ^ flags)
  928. & MS_RDONLY)) {
  929. up_read(&nilfs->ns_super_sem);
  930. err = -EBUSY;
  931. goto failed_unlock;
  932. }
  933. up_read(&nilfs->ns_super_sem);
  934. }
  935. /*
  936. * Find existing nilfs_sb_info struct
  937. */
  938. sd.sbi = nilfs_find_sbinfo(nilfs, !(flags & MS_RDONLY), sd.cno);
  939. /*
  940. * Get super block instance holding the nilfs_sb_info struct.
  941. * A new instance is allocated if no existing mount is present or
  942. * existing instance has been unmounted.
  943. */
  944. s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, &sd);
  945. if (sd.sbi)
  946. nilfs_put_sbinfo(sd.sbi);
  947. if (IS_ERR(s)) {
  948. err = PTR_ERR(s);
  949. goto failed_unlock;
  950. }
  951. if (!s->s_root) {
  952. char b[BDEVNAME_SIZE];
  953. /* New superblock instance created */
  954. s->s_flags = flags;
  955. strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id));
  956. sb_set_blocksize(s, block_size(sd.bdev));
  957. err = nilfs_fill_super(s, data, flags & MS_VERBOSE, nilfs);
  958. if (err)
  959. goto cancel_new;
  960. s->s_flags |= MS_ACTIVE;
  961. need_to_close = 0;
  962. }
  963. mutex_unlock(&nilfs->ns_mount_mutex);
  964. put_nilfs(nilfs);
  965. if (need_to_close)
  966. close_bdev_exclusive(sd.bdev, flags);
  967. simple_set_mnt(mnt, s);
  968. return 0;
  969. failed_unlock:
  970. mutex_unlock(&nilfs->ns_mount_mutex);
  971. put_nilfs(nilfs);
  972. failed:
  973. close_bdev_exclusive(sd.bdev, flags);
  974. return err;
  975. cancel_new:
  976. /* Abandoning the newly allocated superblock */
  977. mutex_unlock(&nilfs->ns_mount_mutex);
  978. put_nilfs(nilfs);
  979. deactivate_locked_super(s);
  980. /*
  981. * deactivate_super() invokes close_bdev_exclusive().
  982. * We must finish all post-cleaning before this call;
  983. * put_nilfs() needs the block device.
  984. */
  985. return err;
  986. }
  987. struct file_system_type nilfs_fs_type = {
  988. .owner = THIS_MODULE,
  989. .name = "nilfs2",
  990. .get_sb = nilfs_get_sb,
  991. .kill_sb = kill_block_super,
  992. .fs_flags = FS_REQUIRES_DEV,
  993. };
  994. static int __init init_nilfs_fs(void)
  995. {
  996. int err;
  997. err = nilfs_init_inode_cache();
  998. if (err)
  999. goto failed;
  1000. err = nilfs_init_transaction_cache();
  1001. if (err)
  1002. goto failed_inode_cache;
  1003. err = nilfs_init_segbuf_cache();
  1004. if (err)
  1005. goto failed_transaction_cache;
  1006. err = nilfs_btree_path_cache_init();
  1007. if (err)
  1008. goto failed_segbuf_cache;
  1009. err = register_filesystem(&nilfs_fs_type);
  1010. if (err)
  1011. goto failed_btree_path_cache;
  1012. return 0;
  1013. failed_btree_path_cache:
  1014. nilfs_btree_path_cache_destroy();
  1015. failed_segbuf_cache:
  1016. nilfs_destroy_segbuf_cache();
  1017. failed_transaction_cache:
  1018. nilfs_destroy_transaction_cache();
  1019. failed_inode_cache:
  1020. nilfs_destroy_inode_cache();
  1021. failed:
  1022. return err;
  1023. }
  1024. static void __exit exit_nilfs_fs(void)
  1025. {
  1026. nilfs_destroy_segbuf_cache();
  1027. nilfs_destroy_transaction_cache();
  1028. nilfs_destroy_inode_cache();
  1029. nilfs_btree_path_cache_destroy();
  1030. unregister_filesystem(&nilfs_fs_type);
  1031. }
  1032. module_init(init_nilfs_fs)
  1033. module_exit(exit_nilfs_fs)