super.c 30 KB

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