the_nilfs.c 20 KB

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