the_nilfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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 "seglist.h"
  35. #include "segbuf.h"
  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. spin_unlock(&nilfs->ns_last_segment_lock);
  44. }
  45. /**
  46. * alloc_nilfs - allocate the_nilfs structure
  47. * @bdev: block device to which the_nilfs is related
  48. *
  49. * alloc_nilfs() allocates memory for the_nilfs and
  50. * initializes its reference count and locks.
  51. *
  52. * Return Value: On success, pointer to the_nilfs is returned.
  53. * On error, NULL is returned.
  54. */
  55. struct the_nilfs *alloc_nilfs(struct block_device *bdev)
  56. {
  57. struct the_nilfs *nilfs;
  58. nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
  59. if (!nilfs)
  60. return NULL;
  61. nilfs->ns_bdev = bdev;
  62. atomic_set(&nilfs->ns_count, 1);
  63. atomic_set(&nilfs->ns_writer_refcount, -1);
  64. atomic_set(&nilfs->ns_ndirtyblks, 0);
  65. init_rwsem(&nilfs->ns_sem);
  66. mutex_init(&nilfs->ns_writer_mutex);
  67. INIT_LIST_HEAD(&nilfs->ns_supers);
  68. spin_lock_init(&nilfs->ns_last_segment_lock);
  69. nilfs->ns_gc_inodes_h = NULL;
  70. init_rwsem(&nilfs->ns_segctor_sem);
  71. return nilfs;
  72. }
  73. /**
  74. * put_nilfs - release a reference to the_nilfs
  75. * @nilfs: the_nilfs structure to be released
  76. *
  77. * put_nilfs() decrements a reference counter of the_nilfs.
  78. * If the reference count reaches zero, the_nilfs is freed.
  79. */
  80. void put_nilfs(struct the_nilfs *nilfs)
  81. {
  82. if (!atomic_dec_and_test(&nilfs->ns_count))
  83. return;
  84. /*
  85. * Increment of ns_count never occur below because the caller
  86. * of get_nilfs() holds at least one reference to the_nilfs.
  87. * Thus its exclusion control is not required here.
  88. */
  89. might_sleep();
  90. if (nilfs_loaded(nilfs)) {
  91. nilfs_mdt_clear(nilfs->ns_sufile);
  92. nilfs_mdt_destroy(nilfs->ns_sufile);
  93. nilfs_mdt_clear(nilfs->ns_cpfile);
  94. nilfs_mdt_destroy(nilfs->ns_cpfile);
  95. nilfs_mdt_clear(nilfs->ns_dat);
  96. nilfs_mdt_destroy(nilfs->ns_dat);
  97. /* XXX: how and when to clear nilfs->ns_gc_dat? */
  98. nilfs_mdt_destroy(nilfs->ns_gc_dat);
  99. }
  100. if (nilfs_init(nilfs)) {
  101. nilfs_destroy_gccache(nilfs);
  102. brelse(nilfs->ns_sbh[0]);
  103. brelse(nilfs->ns_sbh[1]);
  104. }
  105. kfree(nilfs);
  106. }
  107. static int nilfs_load_super_root(struct the_nilfs *nilfs,
  108. struct nilfs_sb_info *sbi, sector_t sr_block)
  109. {
  110. static struct lock_class_key dat_lock_key;
  111. struct buffer_head *bh_sr;
  112. struct nilfs_super_root *raw_sr;
  113. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  114. unsigned dat_entry_size, segment_usage_size, checkpoint_size;
  115. unsigned inode_size;
  116. int err;
  117. err = nilfs_read_super_root_block(sbi->s_super, sr_block, &bh_sr, 1);
  118. if (unlikely(err))
  119. return err;
  120. down_read(&nilfs->ns_sem);
  121. dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
  122. checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
  123. segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
  124. up_read(&nilfs->ns_sem);
  125. inode_size = nilfs->ns_inode_size;
  126. err = -ENOMEM;
  127. nilfs->ns_dat = nilfs_mdt_new(
  128. nilfs, NULL, NILFS_DAT_INO, NILFS_DAT_GFP);
  129. if (unlikely(!nilfs->ns_dat))
  130. goto failed;
  131. nilfs->ns_gc_dat = nilfs_mdt_new(
  132. nilfs, NULL, NILFS_DAT_INO, NILFS_DAT_GFP);
  133. if (unlikely(!nilfs->ns_gc_dat))
  134. goto failed_dat;
  135. nilfs->ns_cpfile = nilfs_mdt_new(
  136. nilfs, NULL, NILFS_CPFILE_INO, NILFS_CPFILE_GFP);
  137. if (unlikely(!nilfs->ns_cpfile))
  138. goto failed_gc_dat;
  139. nilfs->ns_sufile = nilfs_mdt_new(
  140. nilfs, NULL, NILFS_SUFILE_INO, NILFS_SUFILE_GFP);
  141. if (unlikely(!nilfs->ns_sufile))
  142. goto failed_cpfile;
  143. err = nilfs_palloc_init_blockgroup(nilfs->ns_dat, dat_entry_size);
  144. if (unlikely(err))
  145. goto failed_sufile;
  146. err = nilfs_palloc_init_blockgroup(nilfs->ns_gc_dat, dat_entry_size);
  147. if (unlikely(err))
  148. goto failed_sufile;
  149. lockdep_set_class(&NILFS_MDT(nilfs->ns_dat)->mi_sem, &dat_lock_key);
  150. lockdep_set_class(&NILFS_MDT(nilfs->ns_gc_dat)->mi_sem, &dat_lock_key);
  151. nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat);
  152. nilfs_mdt_set_entry_size(nilfs->ns_cpfile, checkpoint_size,
  153. sizeof(struct nilfs_cpfile_header));
  154. nilfs_mdt_set_entry_size(nilfs->ns_sufile, segment_usage_size,
  155. sizeof(struct nilfs_sufile_header));
  156. err = nilfs_mdt_read_inode_direct(
  157. nilfs->ns_dat, bh_sr, NILFS_SR_DAT_OFFSET(inode_size));
  158. if (unlikely(err))
  159. goto failed_sufile;
  160. err = nilfs_mdt_read_inode_direct(
  161. nilfs->ns_cpfile, bh_sr, NILFS_SR_CPFILE_OFFSET(inode_size));
  162. if (unlikely(err))
  163. goto failed_sufile;
  164. err = nilfs_mdt_read_inode_direct(
  165. nilfs->ns_sufile, bh_sr, NILFS_SR_SUFILE_OFFSET(inode_size));
  166. if (unlikely(err))
  167. goto failed_sufile;
  168. raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
  169. nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
  170. failed:
  171. brelse(bh_sr);
  172. return err;
  173. failed_sufile:
  174. nilfs_mdt_destroy(nilfs->ns_sufile);
  175. failed_cpfile:
  176. nilfs_mdt_destroy(nilfs->ns_cpfile);
  177. failed_gc_dat:
  178. nilfs_mdt_destroy(nilfs->ns_gc_dat);
  179. failed_dat:
  180. nilfs_mdt_destroy(nilfs->ns_dat);
  181. goto failed;
  182. }
  183. static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
  184. {
  185. memset(ri, 0, sizeof(*ri));
  186. INIT_LIST_HEAD(&ri->ri_used_segments);
  187. }
  188. static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
  189. {
  190. nilfs_dispose_segment_list(&ri->ri_used_segments);
  191. }
  192. /**
  193. * load_nilfs - load and recover the nilfs
  194. * @nilfs: the_nilfs structure to be released
  195. * @sbi: nilfs_sb_info used to recover past segment
  196. *
  197. * load_nilfs() searches and load the latest super root,
  198. * attaches the last segment, and does recovery if needed.
  199. * The caller must call this exclusively for simultaneous mounts.
  200. */
  201. int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
  202. {
  203. struct nilfs_recovery_info ri;
  204. unsigned int s_flags = sbi->s_super->s_flags;
  205. int really_read_only = bdev_read_only(nilfs->ns_bdev);
  206. unsigned valid_fs;
  207. int err = 0;
  208. nilfs_init_recovery_info(&ri);
  209. down_write(&nilfs->ns_sem);
  210. valid_fs = (nilfs->ns_mount_state & NILFS_VALID_FS);
  211. up_write(&nilfs->ns_sem);
  212. if (!valid_fs && (s_flags & MS_RDONLY)) {
  213. printk(KERN_INFO "NILFS: INFO: recovery "
  214. "required for readonly filesystem.\n");
  215. if (really_read_only) {
  216. printk(KERN_ERR "NILFS: write access "
  217. "unavailable, cannot proceed.\n");
  218. err = -EROFS;
  219. goto failed;
  220. }
  221. printk(KERN_INFO "NILFS: write access will "
  222. "be enabled during recovery.\n");
  223. sbi->s_super->s_flags &= ~MS_RDONLY;
  224. }
  225. err = nilfs_search_super_root(nilfs, sbi, &ri);
  226. if (unlikely(err)) {
  227. printk(KERN_ERR "NILFS: error searching super root.\n");
  228. goto failed;
  229. }
  230. err = nilfs_load_super_root(nilfs, sbi, ri.ri_super_root);
  231. if (unlikely(err)) {
  232. printk(KERN_ERR "NILFS: error loading super root.\n");
  233. goto failed;
  234. }
  235. if (!valid_fs) {
  236. err = nilfs_recover_logical_segments(nilfs, sbi, &ri);
  237. if (unlikely(err)) {
  238. nilfs_mdt_destroy(nilfs->ns_cpfile);
  239. nilfs_mdt_destroy(nilfs->ns_sufile);
  240. nilfs_mdt_destroy(nilfs->ns_dat);
  241. goto failed;
  242. }
  243. if (ri.ri_need_recovery == NILFS_RECOVERY_SR_UPDATED)
  244. sbi->s_super->s_dirt = 1;
  245. }
  246. set_nilfs_loaded(nilfs);
  247. failed:
  248. nilfs_clear_recovery_info(&ri);
  249. sbi->s_super->s_flags = s_flags;
  250. return err;
  251. }
  252. static unsigned long long nilfs_max_size(unsigned int blkbits)
  253. {
  254. unsigned int max_bits;
  255. unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
  256. max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
  257. if (max_bits < 64)
  258. res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
  259. return res;
  260. }
  261. static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
  262. struct nilfs_super_block *sbp)
  263. {
  264. if (le32_to_cpu(sbp->s_rev_level) != NILFS_CURRENT_REV) {
  265. printk(KERN_ERR "NILFS: revision mismatch "
  266. "(superblock rev.=%d.%d, current rev.=%d.%d). "
  267. "Please check the version of mkfs.nilfs.\n",
  268. le32_to_cpu(sbp->s_rev_level),
  269. le16_to_cpu(sbp->s_minor_rev_level),
  270. NILFS_CURRENT_REV, NILFS_MINOR_REV);
  271. return -EINVAL;
  272. }
  273. nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
  274. if (nilfs->ns_sbsize > BLOCK_SIZE)
  275. return -EINVAL;
  276. nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
  277. nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
  278. nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
  279. if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
  280. printk(KERN_ERR "NILFS: too short segment. \n");
  281. return -EINVAL;
  282. }
  283. nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
  284. nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
  285. nilfs->ns_r_segments_percentage =
  286. le32_to_cpu(sbp->s_r_segments_percentage);
  287. nilfs->ns_nrsvsegs =
  288. max_t(unsigned long, NILFS_MIN_NRSVSEGS,
  289. DIV_ROUND_UP(nilfs->ns_nsegments *
  290. nilfs->ns_r_segments_percentage, 100));
  291. nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
  292. return 0;
  293. }
  294. static int nilfs_valid_sb(struct nilfs_super_block *sbp)
  295. {
  296. static unsigned char sum[4];
  297. const int sumoff = offsetof(struct nilfs_super_block, s_sum);
  298. size_t bytes;
  299. u32 crc;
  300. if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
  301. return 0;
  302. bytes = le16_to_cpu(sbp->s_bytes);
  303. if (bytes > BLOCK_SIZE)
  304. return 0;
  305. crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
  306. sumoff);
  307. crc = crc32_le(crc, sum, 4);
  308. crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
  309. bytes - sumoff - 4);
  310. return crc == le32_to_cpu(sbp->s_sum);
  311. }
  312. static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
  313. {
  314. return offset < ((le64_to_cpu(sbp->s_nsegments) *
  315. le32_to_cpu(sbp->s_blocks_per_segment)) <<
  316. (le32_to_cpu(sbp->s_log_block_size) + 10));
  317. }
  318. static void nilfs_release_super_block(struct the_nilfs *nilfs)
  319. {
  320. int i;
  321. for (i = 0; i < 2; i++) {
  322. if (nilfs->ns_sbp[i]) {
  323. brelse(nilfs->ns_sbh[i]);
  324. nilfs->ns_sbh[i] = NULL;
  325. nilfs->ns_sbp[i] = NULL;
  326. }
  327. }
  328. }
  329. void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
  330. {
  331. brelse(nilfs->ns_sbh[0]);
  332. nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
  333. nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
  334. nilfs->ns_sbh[1] = NULL;
  335. nilfs->ns_sbp[1] = NULL;
  336. }
  337. void nilfs_swap_super_block(struct the_nilfs *nilfs)
  338. {
  339. struct buffer_head *tsbh = nilfs->ns_sbh[0];
  340. struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
  341. nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
  342. nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
  343. nilfs->ns_sbh[1] = tsbh;
  344. nilfs->ns_sbp[1] = tsbp;
  345. }
  346. static int nilfs_load_super_block(struct the_nilfs *nilfs,
  347. struct super_block *sb, int blocksize,
  348. struct nilfs_super_block **sbpp)
  349. {
  350. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  351. struct buffer_head **sbh = nilfs->ns_sbh;
  352. u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
  353. int valid[2], swp = 0;
  354. sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
  355. &sbh[0]);
  356. sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
  357. if (!sbp[0]) {
  358. if (!sbp[1]) {
  359. printk(KERN_ERR "NILFS: unable to read superblock\n");
  360. return -EIO;
  361. }
  362. printk(KERN_WARNING
  363. "NILFS warning: unable to read primary superblock\n");
  364. } else if (!sbp[1])
  365. printk(KERN_WARNING
  366. "NILFS warning: unable to read secondary superblock\n");
  367. valid[0] = nilfs_valid_sb(sbp[0]);
  368. valid[1] = nilfs_valid_sb(sbp[1]);
  369. swp = valid[1] &&
  370. (!valid[0] ||
  371. le64_to_cpu(sbp[1]->s_wtime) > le64_to_cpu(sbp[0]->s_wtime));
  372. if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
  373. brelse(sbh[1]);
  374. sbh[1] = NULL;
  375. sbp[1] = NULL;
  376. swp = 0;
  377. }
  378. if (!valid[swp]) {
  379. nilfs_release_super_block(nilfs);
  380. printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
  381. sb->s_id);
  382. return -EINVAL;
  383. }
  384. if (swp) {
  385. printk(KERN_WARNING "NILFS warning: broken superblock. "
  386. "using spare superblock.\n");
  387. nilfs_swap_super_block(nilfs);
  388. }
  389. nilfs->ns_sbwtime[0] = le64_to_cpu(sbp[0]->s_wtime);
  390. nilfs->ns_sbwtime[1] = valid[!swp] ? le64_to_cpu(sbp[1]->s_wtime) : 0;
  391. nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
  392. *sbpp = sbp[0];
  393. return 0;
  394. }
  395. /**
  396. * init_nilfs - initialize a NILFS instance.
  397. * @nilfs: the_nilfs structure
  398. * @sbi: nilfs_sb_info
  399. * @sb: super block
  400. * @data: mount options
  401. *
  402. * init_nilfs() performs common initialization per block device (e.g.
  403. * reading the super block, getting disk layout information, initializing
  404. * shared fields in the_nilfs). It takes on some portion of the jobs
  405. * typically done by a fill_super() routine. This division arises from
  406. * the nature that multiple NILFS instances may be simultaneously
  407. * mounted on a device.
  408. * For multiple mounts on the same device, only the first mount
  409. * invokes these tasks.
  410. *
  411. * Return Value: On success, 0 is returned. On error, a negative error
  412. * code is returned.
  413. */
  414. int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
  415. {
  416. struct super_block *sb = sbi->s_super;
  417. struct nilfs_super_block *sbp;
  418. struct backing_dev_info *bdi;
  419. int blocksize;
  420. int err;
  421. down_write(&nilfs->ns_sem);
  422. if (nilfs_init(nilfs)) {
  423. /* Load values from existing the_nilfs */
  424. sbp = nilfs->ns_sbp[0];
  425. err = nilfs_store_magic_and_option(sb, sbp, data);
  426. if (err)
  427. goto out;
  428. blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
  429. if (sb->s_blocksize != blocksize &&
  430. !sb_set_blocksize(sb, blocksize)) {
  431. printk(KERN_ERR "NILFS: blocksize %d unfit to device\n",
  432. blocksize);
  433. err = -EINVAL;
  434. }
  435. sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
  436. goto out;
  437. }
  438. blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
  439. if (!blocksize) {
  440. printk(KERN_ERR "NILFS: unable to set blocksize\n");
  441. err = -EINVAL;
  442. goto out;
  443. }
  444. err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
  445. if (err)
  446. goto out;
  447. err = nilfs_store_magic_and_option(sb, sbp, data);
  448. if (err)
  449. goto failed_sbh;
  450. blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
  451. if (sb->s_blocksize != blocksize) {
  452. int hw_blocksize = bdev_hardsect_size(sb->s_bdev);
  453. if (blocksize < hw_blocksize) {
  454. printk(KERN_ERR
  455. "NILFS: blocksize %d too small for device "
  456. "(sector-size = %d).\n",
  457. blocksize, hw_blocksize);
  458. err = -EINVAL;
  459. goto failed_sbh;
  460. }
  461. nilfs_release_super_block(nilfs);
  462. sb_set_blocksize(sb, blocksize);
  463. err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
  464. if (err)
  465. goto out;
  466. /* not failed_sbh; sbh is released automatically
  467. when reloading fails. */
  468. }
  469. nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
  470. err = nilfs_store_disk_layout(nilfs, sbp);
  471. if (err)
  472. goto failed_sbh;
  473. sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
  474. nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
  475. bdi = nilfs->ns_bdev->bd_inode_backing_dev_info;
  476. if (!bdi)
  477. bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
  478. nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
  479. /* Finding last segment */
  480. nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
  481. nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
  482. nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
  483. nilfs->ns_seg_seq = nilfs->ns_last_seq;
  484. nilfs->ns_segnum =
  485. nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
  486. nilfs->ns_cno = nilfs->ns_last_cno + 1;
  487. if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
  488. printk(KERN_ERR "NILFS invalid last segment number.\n");
  489. err = -EINVAL;
  490. goto failed_sbh;
  491. }
  492. /* Dummy values */
  493. nilfs->ns_free_segments_count =
  494. nilfs->ns_nsegments - (nilfs->ns_segnum + 1);
  495. /* Initialize gcinode cache */
  496. err = nilfs_init_gccache(nilfs);
  497. if (err)
  498. goto failed_sbh;
  499. set_nilfs_init(nilfs);
  500. err = 0;
  501. out:
  502. up_write(&nilfs->ns_sem);
  503. return err;
  504. failed_sbh:
  505. nilfs_release_super_block(nilfs);
  506. goto out;
  507. }
  508. int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
  509. {
  510. struct inode *dat = nilfs_dat_inode(nilfs);
  511. unsigned long ncleansegs;
  512. int err;
  513. down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  514. err = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile, &ncleansegs);
  515. up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  516. if (likely(!err))
  517. *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
  518. return err;
  519. }
  520. int nilfs_near_disk_full(struct the_nilfs *nilfs)
  521. {
  522. struct inode *sufile = nilfs->ns_sufile;
  523. unsigned long ncleansegs, nincsegs;
  524. int ret;
  525. ret = nilfs_sufile_get_ncleansegs(sufile, &ncleansegs);
  526. if (likely(!ret)) {
  527. nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
  528. nilfs->ns_blocks_per_segment + 1;
  529. if (ncleansegs <= nilfs->ns_nrsvsegs + nincsegs)
  530. ret++;
  531. }
  532. return ret;
  533. }
  534. int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
  535. int snapshot_mount)
  536. {
  537. struct nilfs_sb_info *sbi;
  538. int ret = 0;
  539. down_read(&nilfs->ns_sem);
  540. if (cno == 0 || cno > nilfs->ns_cno)
  541. goto out_unlock;
  542. list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
  543. if (sbi->s_snapshot_cno == cno &&
  544. (!snapshot_mount || nilfs_test_opt(sbi, SNAPSHOT))) {
  545. /* exclude read-only mounts */
  546. ret++;
  547. break;
  548. }
  549. }
  550. /* for protecting recent checkpoints */
  551. if (cno >= nilfs_last_cno(nilfs))
  552. ret++;
  553. out_unlock:
  554. up_read(&nilfs->ns_sem);
  555. return ret;
  556. }