super.c 32 KB

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