the_nilfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /*
  2. * the_nilfs.c - the_nilfs shared structure.
  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. #include <linux/buffer_head.h>
  24. #include <linux/slab.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/crc32.h>
  28. #include "nilfs.h"
  29. #include "segment.h"
  30. #include "alloc.h"
  31. #include "cpfile.h"
  32. #include "sufile.h"
  33. #include "dat.h"
  34. #include "segbuf.h"
  35. static int nilfs_valid_sb(struct nilfs_super_block *sbp);
  36. void nilfs_set_last_segment(struct the_nilfs *nilfs,
  37. sector_t start_blocknr, u64 seq, __u64 cno)
  38. {
  39. spin_lock(&nilfs->ns_last_segment_lock);
  40. nilfs->ns_last_pseg = start_blocknr;
  41. nilfs->ns_last_seq = seq;
  42. nilfs->ns_last_cno = cno;
  43. if (!nilfs_sb_dirty(nilfs)) {
  44. if (nilfs->ns_prev_seq == nilfs->ns_last_seq)
  45. goto stay_cursor;
  46. set_nilfs_sb_dirty(nilfs);
  47. }
  48. nilfs->ns_prev_seq = nilfs->ns_last_seq;
  49. stay_cursor:
  50. spin_unlock(&nilfs->ns_last_segment_lock);
  51. }
  52. /**
  53. * alloc_nilfs - allocate a nilfs object
  54. * @bdev: block device to which the_nilfs is related
  55. *
  56. * Return Value: On success, pointer to the_nilfs is returned.
  57. * On error, NULL is returned.
  58. */
  59. struct the_nilfs *alloc_nilfs(struct block_device *bdev)
  60. {
  61. struct the_nilfs *nilfs;
  62. nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
  63. if (!nilfs)
  64. return NULL;
  65. nilfs->ns_bdev = bdev;
  66. atomic_set(&nilfs->ns_ndirtyblks, 0);
  67. init_rwsem(&nilfs->ns_sem);
  68. init_rwsem(&nilfs->ns_writer_sem);
  69. INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
  70. spin_lock_init(&nilfs->ns_last_segment_lock);
  71. nilfs->ns_cptree = RB_ROOT;
  72. spin_lock_init(&nilfs->ns_cptree_lock);
  73. init_rwsem(&nilfs->ns_segctor_sem);
  74. return nilfs;
  75. }
  76. /**
  77. * destroy_nilfs - destroy nilfs object
  78. * @nilfs: nilfs object to be released
  79. */
  80. void destroy_nilfs(struct the_nilfs *nilfs)
  81. {
  82. might_sleep();
  83. if (nilfs_loaded(nilfs)) {
  84. nilfs_mdt_destroy(nilfs->ns_sufile);
  85. nilfs_mdt_destroy(nilfs->ns_cpfile);
  86. nilfs_mdt_destroy(nilfs->ns_dat);
  87. nilfs_mdt_destroy(nilfs->ns_gc_dat);
  88. }
  89. if (nilfs_init(nilfs)) {
  90. brelse(nilfs->ns_sbh[0]);
  91. brelse(nilfs->ns_sbh[1]);
  92. }
  93. kfree(nilfs);
  94. }
  95. static int nilfs_load_super_root(struct the_nilfs *nilfs, sector_t sr_block)
  96. {
  97. struct buffer_head *bh_sr;
  98. struct nilfs_super_root *raw_sr;
  99. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  100. unsigned dat_entry_size, segment_usage_size, checkpoint_size;
  101. unsigned inode_size;
  102. int err;
  103. err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
  104. if (unlikely(err))
  105. return err;
  106. down_read(&nilfs->ns_sem);
  107. dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
  108. checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
  109. segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
  110. up_read(&nilfs->ns_sem);
  111. inode_size = nilfs->ns_inode_size;
  112. err = -ENOMEM;
  113. nilfs->ns_dat = nilfs_dat_new(nilfs, dat_entry_size);
  114. if (unlikely(!nilfs->ns_dat))
  115. goto failed;
  116. nilfs->ns_gc_dat = nilfs_dat_new(nilfs, dat_entry_size);
  117. if (unlikely(!nilfs->ns_gc_dat))
  118. goto failed_dat;
  119. nilfs->ns_cpfile = nilfs_cpfile_new(nilfs, checkpoint_size);
  120. if (unlikely(!nilfs->ns_cpfile))
  121. goto failed_gc_dat;
  122. nilfs->ns_sufile = nilfs_sufile_new(nilfs, segment_usage_size);
  123. if (unlikely(!nilfs->ns_sufile))
  124. goto failed_cpfile;
  125. nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat);
  126. err = nilfs_dat_read(nilfs->ns_dat, (void *)bh_sr->b_data +
  127. NILFS_SR_DAT_OFFSET(inode_size));
  128. if (unlikely(err))
  129. goto failed_sufile;
  130. err = nilfs_cpfile_read(nilfs->ns_cpfile, (void *)bh_sr->b_data +
  131. NILFS_SR_CPFILE_OFFSET(inode_size));
  132. if (unlikely(err))
  133. goto failed_sufile;
  134. err = nilfs_sufile_read(nilfs->ns_sufile, (void *)bh_sr->b_data +
  135. NILFS_SR_SUFILE_OFFSET(inode_size));
  136. if (unlikely(err))
  137. goto failed_sufile;
  138. raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
  139. nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
  140. failed:
  141. brelse(bh_sr);
  142. return err;
  143. failed_sufile:
  144. nilfs_mdt_destroy(nilfs->ns_sufile);
  145. failed_cpfile:
  146. nilfs_mdt_destroy(nilfs->ns_cpfile);
  147. failed_gc_dat:
  148. nilfs_mdt_destroy(nilfs->ns_gc_dat);
  149. failed_dat:
  150. nilfs_mdt_destroy(nilfs->ns_dat);
  151. goto failed;
  152. }
  153. static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
  154. {
  155. memset(ri, 0, sizeof(*ri));
  156. INIT_LIST_HEAD(&ri->ri_used_segments);
  157. }
  158. static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
  159. {
  160. nilfs_dispose_segment_list(&ri->ri_used_segments);
  161. }
  162. /**
  163. * nilfs_store_log_cursor - load log cursor from a super block
  164. * @nilfs: nilfs object
  165. * @sbp: buffer storing super block to be read
  166. *
  167. * nilfs_store_log_cursor() reads the last position of the log
  168. * containing a super root from a given super block, and initializes
  169. * relevant information on the nilfs object preparatory for log
  170. * scanning and recovery.
  171. */
  172. static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
  173. struct nilfs_super_block *sbp)
  174. {
  175. int ret = 0;
  176. nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
  177. nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
  178. nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
  179. nilfs->ns_prev_seq = nilfs->ns_last_seq;
  180. nilfs->ns_seg_seq = nilfs->ns_last_seq;
  181. nilfs->ns_segnum =
  182. nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
  183. nilfs->ns_cno = nilfs->ns_last_cno + 1;
  184. if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
  185. printk(KERN_ERR "NILFS invalid last segment number.\n");
  186. ret = -EINVAL;
  187. }
  188. return ret;
  189. }
  190. /**
  191. * load_nilfs - load and recover the nilfs
  192. * @nilfs: the_nilfs structure to be released
  193. * @sbi: nilfs_sb_info used to recover past segment
  194. *
  195. * load_nilfs() searches and load the latest super root,
  196. * attaches the last segment, and does recovery if needed.
  197. * The caller must call this exclusively for simultaneous mounts.
  198. */
  199. int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
  200. {
  201. struct nilfs_recovery_info ri;
  202. unsigned int s_flags = sbi->s_super->s_flags;
  203. int really_read_only = bdev_read_only(nilfs->ns_bdev);
  204. int valid_fs = nilfs_valid_fs(nilfs);
  205. int err;
  206. if (!valid_fs) {
  207. printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
  208. if (s_flags & MS_RDONLY) {
  209. printk(KERN_INFO "NILFS: INFO: recovery "
  210. "required for readonly filesystem.\n");
  211. printk(KERN_INFO "NILFS: write access will "
  212. "be enabled during recovery.\n");
  213. }
  214. }
  215. nilfs_init_recovery_info(&ri);
  216. err = nilfs_search_super_root(nilfs, &ri);
  217. if (unlikely(err)) {
  218. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  219. int blocksize;
  220. if (err != -EINVAL)
  221. goto scan_error;
  222. if (!nilfs_valid_sb(sbp[1])) {
  223. printk(KERN_WARNING
  224. "NILFS warning: unable to fall back to spare"
  225. "super block\n");
  226. goto scan_error;
  227. }
  228. printk(KERN_INFO
  229. "NILFS: try rollback from an earlier position\n");
  230. /*
  231. * restore super block with its spare and reconfigure
  232. * relevant states of the nilfs object.
  233. */
  234. memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
  235. nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
  236. nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
  237. /* verify consistency between two super blocks */
  238. blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
  239. if (blocksize != nilfs->ns_blocksize) {
  240. printk(KERN_WARNING
  241. "NILFS warning: blocksize differs between "
  242. "two super blocks (%d != %d)\n",
  243. blocksize, nilfs->ns_blocksize);
  244. goto scan_error;
  245. }
  246. err = nilfs_store_log_cursor(nilfs, sbp[0]);
  247. if (err)
  248. goto scan_error;
  249. /* drop clean flag to allow roll-forward and recovery */
  250. nilfs->ns_mount_state &= ~NILFS_VALID_FS;
  251. valid_fs = 0;
  252. err = nilfs_search_super_root(nilfs, &ri);
  253. if (err)
  254. goto scan_error;
  255. }
  256. err = nilfs_load_super_root(nilfs, ri.ri_super_root);
  257. if (unlikely(err)) {
  258. printk(KERN_ERR "NILFS: error loading super root.\n");
  259. goto failed;
  260. }
  261. if (valid_fs)
  262. goto skip_recovery;
  263. if (s_flags & MS_RDONLY) {
  264. __u64 features;
  265. if (nilfs_test_opt(sbi, NORECOVERY)) {
  266. printk(KERN_INFO "NILFS: norecovery option specified. "
  267. "skipping roll-forward recovery\n");
  268. goto skip_recovery;
  269. }
  270. features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
  271. ~NILFS_FEATURE_COMPAT_RO_SUPP;
  272. if (features) {
  273. printk(KERN_ERR "NILFS: couldn't proceed with "
  274. "recovery because of unsupported optional "
  275. "features (%llx)\n",
  276. (unsigned long long)features);
  277. err = -EROFS;
  278. goto failed_unload;
  279. }
  280. if (really_read_only) {
  281. printk(KERN_ERR "NILFS: write access "
  282. "unavailable, cannot proceed.\n");
  283. err = -EROFS;
  284. goto failed_unload;
  285. }
  286. sbi->s_super->s_flags &= ~MS_RDONLY;
  287. } else if (nilfs_test_opt(sbi, NORECOVERY)) {
  288. printk(KERN_ERR "NILFS: recovery cancelled because norecovery "
  289. "option was specified for a read/write mount\n");
  290. err = -EINVAL;
  291. goto failed_unload;
  292. }
  293. err = nilfs_salvage_orphan_logs(nilfs, sbi, &ri);
  294. if (err)
  295. goto failed_unload;
  296. down_write(&nilfs->ns_sem);
  297. nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
  298. err = nilfs_cleanup_super(sbi);
  299. up_write(&nilfs->ns_sem);
  300. if (err) {
  301. printk(KERN_ERR "NILFS: failed to update super block. "
  302. "recovery unfinished.\n");
  303. goto failed_unload;
  304. }
  305. printk(KERN_INFO "NILFS: recovery complete.\n");
  306. skip_recovery:
  307. set_nilfs_loaded(nilfs);
  308. nilfs_clear_recovery_info(&ri);
  309. sbi->s_super->s_flags = s_flags;
  310. return 0;
  311. scan_error:
  312. printk(KERN_ERR "NILFS: error searching super root.\n");
  313. goto failed;
  314. failed_unload:
  315. nilfs_mdt_destroy(nilfs->ns_cpfile);
  316. nilfs_mdt_destroy(nilfs->ns_sufile);
  317. nilfs_mdt_destroy(nilfs->ns_dat);
  318. nilfs_mdt_destroy(nilfs->ns_gc_dat);
  319. failed:
  320. nilfs_clear_recovery_info(&ri);
  321. sbi->s_super->s_flags = s_flags;
  322. return err;
  323. }
  324. static unsigned long long nilfs_max_size(unsigned int blkbits)
  325. {
  326. unsigned int max_bits;
  327. unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
  328. max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
  329. if (max_bits < 64)
  330. res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
  331. return res;
  332. }
  333. static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
  334. struct nilfs_super_block *sbp)
  335. {
  336. if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
  337. printk(KERN_ERR "NILFS: unsupported revision "
  338. "(superblock rev.=%d.%d, current rev.=%d.%d). "
  339. "Please check the version of mkfs.nilfs.\n",
  340. le32_to_cpu(sbp->s_rev_level),
  341. le16_to_cpu(sbp->s_minor_rev_level),
  342. NILFS_CURRENT_REV, NILFS_MINOR_REV);
  343. return -EINVAL;
  344. }
  345. nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
  346. if (nilfs->ns_sbsize > BLOCK_SIZE)
  347. return -EINVAL;
  348. nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
  349. nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
  350. nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
  351. if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
  352. printk(KERN_ERR "NILFS: too short segment.\n");
  353. return -EINVAL;
  354. }
  355. nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
  356. nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
  357. nilfs->ns_r_segments_percentage =
  358. le32_to_cpu(sbp->s_r_segments_percentage);
  359. nilfs->ns_nrsvsegs =
  360. max_t(unsigned long, NILFS_MIN_NRSVSEGS,
  361. DIV_ROUND_UP(nilfs->ns_nsegments *
  362. nilfs->ns_r_segments_percentage, 100));
  363. nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
  364. return 0;
  365. }
  366. static int nilfs_valid_sb(struct nilfs_super_block *sbp)
  367. {
  368. static unsigned char sum[4];
  369. const int sumoff = offsetof(struct nilfs_super_block, s_sum);
  370. size_t bytes;
  371. u32 crc;
  372. if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
  373. return 0;
  374. bytes = le16_to_cpu(sbp->s_bytes);
  375. if (bytes > BLOCK_SIZE)
  376. return 0;
  377. crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
  378. sumoff);
  379. crc = crc32_le(crc, sum, 4);
  380. crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
  381. bytes - sumoff - 4);
  382. return crc == le32_to_cpu(sbp->s_sum);
  383. }
  384. static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
  385. {
  386. return offset < ((le64_to_cpu(sbp->s_nsegments) *
  387. le32_to_cpu(sbp->s_blocks_per_segment)) <<
  388. (le32_to_cpu(sbp->s_log_block_size) + 10));
  389. }
  390. static void nilfs_release_super_block(struct the_nilfs *nilfs)
  391. {
  392. int i;
  393. for (i = 0; i < 2; i++) {
  394. if (nilfs->ns_sbp[i]) {
  395. brelse(nilfs->ns_sbh[i]);
  396. nilfs->ns_sbh[i] = NULL;
  397. nilfs->ns_sbp[i] = NULL;
  398. }
  399. }
  400. }
  401. void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
  402. {
  403. brelse(nilfs->ns_sbh[0]);
  404. nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
  405. nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
  406. nilfs->ns_sbh[1] = NULL;
  407. nilfs->ns_sbp[1] = NULL;
  408. }
  409. void nilfs_swap_super_block(struct the_nilfs *nilfs)
  410. {
  411. struct buffer_head *tsbh = nilfs->ns_sbh[0];
  412. struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
  413. nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
  414. nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
  415. nilfs->ns_sbh[1] = tsbh;
  416. nilfs->ns_sbp[1] = tsbp;
  417. }
  418. static int nilfs_load_super_block(struct the_nilfs *nilfs,
  419. struct super_block *sb, int blocksize,
  420. struct nilfs_super_block **sbpp)
  421. {
  422. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  423. struct buffer_head **sbh = nilfs->ns_sbh;
  424. u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
  425. int valid[2], swp = 0;
  426. sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
  427. &sbh[0]);
  428. sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
  429. if (!sbp[0]) {
  430. if (!sbp[1]) {
  431. printk(KERN_ERR "NILFS: unable to read superblock\n");
  432. return -EIO;
  433. }
  434. printk(KERN_WARNING
  435. "NILFS warning: unable to read primary superblock\n");
  436. } else if (!sbp[1])
  437. printk(KERN_WARNING
  438. "NILFS warning: unable to read secondary superblock\n");
  439. /*
  440. * Compare two super blocks and set 1 in swp if the secondary
  441. * super block is valid and newer. Otherwise, set 0 in swp.
  442. */
  443. valid[0] = nilfs_valid_sb(sbp[0]);
  444. valid[1] = nilfs_valid_sb(sbp[1]);
  445. swp = valid[1] && (!valid[0] ||
  446. le64_to_cpu(sbp[1]->s_last_cno) >
  447. le64_to_cpu(sbp[0]->s_last_cno));
  448. if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
  449. brelse(sbh[1]);
  450. sbh[1] = NULL;
  451. sbp[1] = NULL;
  452. swp = 0;
  453. }
  454. if (!valid[swp]) {
  455. nilfs_release_super_block(nilfs);
  456. printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
  457. sb->s_id);
  458. return -EINVAL;
  459. }
  460. if (!valid[!swp])
  461. printk(KERN_WARNING "NILFS warning: broken superblock. "
  462. "using spare superblock.\n");
  463. if (swp)
  464. nilfs_swap_super_block(nilfs);
  465. nilfs->ns_sbwcount = 0;
  466. nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
  467. nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
  468. *sbpp = sbp[0];
  469. return 0;
  470. }
  471. /**
  472. * init_nilfs - initialize a NILFS instance.
  473. * @nilfs: the_nilfs structure
  474. * @sbi: nilfs_sb_info
  475. * @sb: super block
  476. * @data: mount options
  477. *
  478. * init_nilfs() performs common initialization per block device (e.g.
  479. * reading the super block, getting disk layout information, initializing
  480. * shared fields in the_nilfs).
  481. *
  482. * Return Value: On success, 0 is returned. On error, a negative error
  483. * code is returned.
  484. */
  485. int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
  486. {
  487. struct super_block *sb = sbi->s_super;
  488. struct nilfs_super_block *sbp;
  489. struct backing_dev_info *bdi;
  490. int blocksize;
  491. int err;
  492. down_write(&nilfs->ns_sem);
  493. blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
  494. if (!blocksize) {
  495. printk(KERN_ERR "NILFS: unable to set blocksize\n");
  496. err = -EINVAL;
  497. goto out;
  498. }
  499. err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
  500. if (err)
  501. goto out;
  502. err = nilfs_store_magic_and_option(sb, sbp, data);
  503. if (err)
  504. goto failed_sbh;
  505. err = nilfs_check_feature_compatibility(sb, sbp);
  506. if (err)
  507. goto failed_sbh;
  508. blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
  509. if (blocksize < NILFS_MIN_BLOCK_SIZE ||
  510. blocksize > NILFS_MAX_BLOCK_SIZE) {
  511. printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
  512. "filesystem blocksize %d\n", blocksize);
  513. err = -EINVAL;
  514. goto failed_sbh;
  515. }
  516. if (sb->s_blocksize != blocksize) {
  517. int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
  518. if (blocksize < hw_blocksize) {
  519. printk(KERN_ERR
  520. "NILFS: blocksize %d too small for device "
  521. "(sector-size = %d).\n",
  522. blocksize, hw_blocksize);
  523. err = -EINVAL;
  524. goto failed_sbh;
  525. }
  526. nilfs_release_super_block(nilfs);
  527. sb_set_blocksize(sb, blocksize);
  528. err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
  529. if (err)
  530. goto out;
  531. /* not failed_sbh; sbh is released automatically
  532. when reloading fails. */
  533. }
  534. nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
  535. nilfs->ns_blocksize = blocksize;
  536. err = nilfs_store_disk_layout(nilfs, sbp);
  537. if (err)
  538. goto failed_sbh;
  539. sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
  540. nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
  541. bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
  542. nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
  543. err = nilfs_store_log_cursor(nilfs, sbp);
  544. if (err)
  545. goto failed_sbh;
  546. set_nilfs_init(nilfs);
  547. err = 0;
  548. out:
  549. up_write(&nilfs->ns_sem);
  550. return err;
  551. failed_sbh:
  552. nilfs_release_super_block(nilfs);
  553. goto out;
  554. }
  555. int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
  556. size_t nsegs)
  557. {
  558. sector_t seg_start, seg_end;
  559. sector_t start = 0, nblocks = 0;
  560. unsigned int sects_per_block;
  561. __u64 *sn;
  562. int ret = 0;
  563. sects_per_block = (1 << nilfs->ns_blocksize_bits) /
  564. bdev_logical_block_size(nilfs->ns_bdev);
  565. for (sn = segnump; sn < segnump + nsegs; sn++) {
  566. nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
  567. if (!nblocks) {
  568. start = seg_start;
  569. nblocks = seg_end - seg_start + 1;
  570. } else if (start + nblocks == seg_start) {
  571. nblocks += seg_end - seg_start + 1;
  572. } else {
  573. ret = blkdev_issue_discard(nilfs->ns_bdev,
  574. start * sects_per_block,
  575. nblocks * sects_per_block,
  576. GFP_NOFS,
  577. BLKDEV_IFL_WAIT |
  578. BLKDEV_IFL_BARRIER);
  579. if (ret < 0)
  580. return ret;
  581. nblocks = 0;
  582. }
  583. }
  584. if (nblocks)
  585. ret = blkdev_issue_discard(nilfs->ns_bdev,
  586. start * sects_per_block,
  587. nblocks * sects_per_block,
  588. GFP_NOFS,
  589. BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
  590. return ret;
  591. }
  592. int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
  593. {
  594. struct inode *dat = nilfs_dat_inode(nilfs);
  595. unsigned long ncleansegs;
  596. down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  597. ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
  598. up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  599. *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
  600. return 0;
  601. }
  602. int nilfs_near_disk_full(struct the_nilfs *nilfs)
  603. {
  604. unsigned long ncleansegs, nincsegs;
  605. ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
  606. nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
  607. nilfs->ns_blocks_per_segment + 1;
  608. return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
  609. }
  610. struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
  611. {
  612. struct rb_node *n;
  613. struct nilfs_root *root;
  614. spin_lock(&nilfs->ns_cptree_lock);
  615. n = nilfs->ns_cptree.rb_node;
  616. while (n) {
  617. root = rb_entry(n, struct nilfs_root, rb_node);
  618. if (cno < root->cno) {
  619. n = n->rb_left;
  620. } else if (cno > root->cno) {
  621. n = n->rb_right;
  622. } else {
  623. atomic_inc(&root->count);
  624. spin_unlock(&nilfs->ns_cptree_lock);
  625. return root;
  626. }
  627. }
  628. spin_unlock(&nilfs->ns_cptree_lock);
  629. return NULL;
  630. }
  631. struct nilfs_root *
  632. nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
  633. {
  634. struct rb_node **p, *parent;
  635. struct nilfs_root *root, *new;
  636. root = nilfs_lookup_root(nilfs, cno);
  637. if (root)
  638. return root;
  639. new = kmalloc(sizeof(*root), GFP_KERNEL);
  640. if (!new)
  641. return NULL;
  642. spin_lock(&nilfs->ns_cptree_lock);
  643. p = &nilfs->ns_cptree.rb_node;
  644. parent = NULL;
  645. while (*p) {
  646. parent = *p;
  647. root = rb_entry(parent, struct nilfs_root, rb_node);
  648. if (cno < root->cno) {
  649. p = &(*p)->rb_left;
  650. } else if (cno > root->cno) {
  651. p = &(*p)->rb_right;
  652. } else {
  653. atomic_inc(&root->count);
  654. spin_unlock(&nilfs->ns_cptree_lock);
  655. kfree(new);
  656. return root;
  657. }
  658. }
  659. new->cno = cno;
  660. new->ifile = NULL;
  661. new->nilfs = nilfs;
  662. atomic_set(&new->count, 1);
  663. atomic_set(&new->inodes_count, 0);
  664. atomic_set(&new->blocks_count, 0);
  665. rb_link_node(&new->rb_node, parent, p);
  666. rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
  667. spin_unlock(&nilfs->ns_cptree_lock);
  668. return new;
  669. }
  670. void nilfs_put_root(struct nilfs_root *root)
  671. {
  672. if (atomic_dec_and_test(&root->count)) {
  673. struct the_nilfs *nilfs = root->nilfs;
  674. spin_lock(&nilfs->ns_cptree_lock);
  675. rb_erase(&root->rb_node, &nilfs->ns_cptree);
  676. spin_unlock(&nilfs->ns_cptree_lock);
  677. if (root->ifile)
  678. nilfs_mdt_destroy(root->ifile);
  679. kfree(root);
  680. }
  681. }
  682. int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
  683. int snapshot_mount)
  684. {
  685. struct nilfs_root *root;
  686. int ret;
  687. if (cno < 0 || cno > nilfs->ns_cno)
  688. return false;
  689. if (cno >= nilfs_last_cno(nilfs))
  690. return true; /* protect recent checkpoints */
  691. ret = false;
  692. root = nilfs_lookup_root(nilfs, cno);
  693. if (root) {
  694. ret = true;
  695. nilfs_put_root(root);
  696. }
  697. return ret;
  698. }