the_nilfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. nilfs_clear_recovery_info(&ri);
  286. sbi->s_super->s_flags = s_flags;
  287. return 0;
  288. scan_error:
  289. printk(KERN_ERR "NILFS: error searching super root.\n");
  290. goto failed;
  291. failed_unload:
  292. iput(nilfs->ns_cpfile);
  293. iput(nilfs->ns_sufile);
  294. iput(nilfs->ns_dat);
  295. failed:
  296. nilfs_clear_recovery_info(&ri);
  297. sbi->s_super->s_flags = s_flags;
  298. return err;
  299. }
  300. static unsigned long long nilfs_max_size(unsigned int blkbits)
  301. {
  302. unsigned int max_bits;
  303. unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
  304. max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
  305. if (max_bits < 64)
  306. res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
  307. return res;
  308. }
  309. static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
  310. struct nilfs_super_block *sbp)
  311. {
  312. if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
  313. printk(KERN_ERR "NILFS: unsupported revision "
  314. "(superblock rev.=%d.%d, current rev.=%d.%d). "
  315. "Please check the version of mkfs.nilfs.\n",
  316. le32_to_cpu(sbp->s_rev_level),
  317. le16_to_cpu(sbp->s_minor_rev_level),
  318. NILFS_CURRENT_REV, NILFS_MINOR_REV);
  319. return -EINVAL;
  320. }
  321. nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
  322. if (nilfs->ns_sbsize > BLOCK_SIZE)
  323. return -EINVAL;
  324. nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
  325. nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
  326. nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
  327. if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
  328. printk(KERN_ERR "NILFS: too short segment.\n");
  329. return -EINVAL;
  330. }
  331. nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
  332. nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
  333. nilfs->ns_r_segments_percentage =
  334. le32_to_cpu(sbp->s_r_segments_percentage);
  335. nilfs->ns_nrsvsegs =
  336. max_t(unsigned long, NILFS_MIN_NRSVSEGS,
  337. DIV_ROUND_UP(nilfs->ns_nsegments *
  338. nilfs->ns_r_segments_percentage, 100));
  339. nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
  340. return 0;
  341. }
  342. static int nilfs_valid_sb(struct nilfs_super_block *sbp)
  343. {
  344. static unsigned char sum[4];
  345. const int sumoff = offsetof(struct nilfs_super_block, s_sum);
  346. size_t bytes;
  347. u32 crc;
  348. if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
  349. return 0;
  350. bytes = le16_to_cpu(sbp->s_bytes);
  351. if (bytes > BLOCK_SIZE)
  352. return 0;
  353. crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
  354. sumoff);
  355. crc = crc32_le(crc, sum, 4);
  356. crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
  357. bytes - sumoff - 4);
  358. return crc == le32_to_cpu(sbp->s_sum);
  359. }
  360. static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
  361. {
  362. return offset < ((le64_to_cpu(sbp->s_nsegments) *
  363. le32_to_cpu(sbp->s_blocks_per_segment)) <<
  364. (le32_to_cpu(sbp->s_log_block_size) + 10));
  365. }
  366. static void nilfs_release_super_block(struct the_nilfs *nilfs)
  367. {
  368. int i;
  369. for (i = 0; i < 2; i++) {
  370. if (nilfs->ns_sbp[i]) {
  371. brelse(nilfs->ns_sbh[i]);
  372. nilfs->ns_sbh[i] = NULL;
  373. nilfs->ns_sbp[i] = NULL;
  374. }
  375. }
  376. }
  377. void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
  378. {
  379. brelse(nilfs->ns_sbh[0]);
  380. nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
  381. nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
  382. nilfs->ns_sbh[1] = NULL;
  383. nilfs->ns_sbp[1] = NULL;
  384. }
  385. void nilfs_swap_super_block(struct the_nilfs *nilfs)
  386. {
  387. struct buffer_head *tsbh = nilfs->ns_sbh[0];
  388. struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
  389. nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
  390. nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
  391. nilfs->ns_sbh[1] = tsbh;
  392. nilfs->ns_sbp[1] = tsbp;
  393. }
  394. static int nilfs_load_super_block(struct the_nilfs *nilfs,
  395. struct super_block *sb, int blocksize,
  396. struct nilfs_super_block **sbpp)
  397. {
  398. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  399. struct buffer_head **sbh = nilfs->ns_sbh;
  400. u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
  401. int valid[2], swp = 0;
  402. sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
  403. &sbh[0]);
  404. sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
  405. if (!sbp[0]) {
  406. if (!sbp[1]) {
  407. printk(KERN_ERR "NILFS: unable to read superblock\n");
  408. return -EIO;
  409. }
  410. printk(KERN_WARNING
  411. "NILFS warning: unable to read primary superblock\n");
  412. } else if (!sbp[1])
  413. printk(KERN_WARNING
  414. "NILFS warning: unable to read secondary superblock\n");
  415. /*
  416. * Compare two super blocks and set 1 in swp if the secondary
  417. * super block is valid and newer. Otherwise, set 0 in swp.
  418. */
  419. valid[0] = nilfs_valid_sb(sbp[0]);
  420. valid[1] = nilfs_valid_sb(sbp[1]);
  421. swp = valid[1] && (!valid[0] ||
  422. le64_to_cpu(sbp[1]->s_last_cno) >
  423. le64_to_cpu(sbp[0]->s_last_cno));
  424. if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
  425. brelse(sbh[1]);
  426. sbh[1] = NULL;
  427. sbp[1] = NULL;
  428. swp = 0;
  429. }
  430. if (!valid[swp]) {
  431. nilfs_release_super_block(nilfs);
  432. printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
  433. sb->s_id);
  434. return -EINVAL;
  435. }
  436. if (!valid[!swp])
  437. printk(KERN_WARNING "NILFS warning: broken superblock. "
  438. "using spare superblock.\n");
  439. if (swp)
  440. nilfs_swap_super_block(nilfs);
  441. nilfs->ns_sbwcount = 0;
  442. nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
  443. nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
  444. *sbpp = sbp[0];
  445. return 0;
  446. }
  447. /**
  448. * init_nilfs - initialize a NILFS instance.
  449. * @nilfs: the_nilfs structure
  450. * @sbi: nilfs_sb_info
  451. * @sb: super block
  452. * @data: mount options
  453. *
  454. * init_nilfs() performs common initialization per block device (e.g.
  455. * reading the super block, getting disk layout information, initializing
  456. * shared fields in the_nilfs).
  457. *
  458. * Return Value: On success, 0 is returned. On error, a negative error
  459. * code is returned.
  460. */
  461. int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
  462. {
  463. struct super_block *sb = sbi->s_super;
  464. struct nilfs_super_block *sbp;
  465. int blocksize;
  466. int err;
  467. down_write(&nilfs->ns_sem);
  468. blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
  469. if (!blocksize) {
  470. printk(KERN_ERR "NILFS: unable to set blocksize\n");
  471. err = -EINVAL;
  472. goto out;
  473. }
  474. err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
  475. if (err)
  476. goto out;
  477. err = nilfs_store_magic_and_option(sb, sbp, data);
  478. if (err)
  479. goto failed_sbh;
  480. err = nilfs_check_feature_compatibility(sb, sbp);
  481. if (err)
  482. goto failed_sbh;
  483. blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
  484. if (blocksize < NILFS_MIN_BLOCK_SIZE ||
  485. blocksize > NILFS_MAX_BLOCK_SIZE) {
  486. printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
  487. "filesystem blocksize %d\n", blocksize);
  488. err = -EINVAL;
  489. goto failed_sbh;
  490. }
  491. if (sb->s_blocksize != blocksize) {
  492. int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
  493. if (blocksize < hw_blocksize) {
  494. printk(KERN_ERR
  495. "NILFS: blocksize %d too small for device "
  496. "(sector-size = %d).\n",
  497. blocksize, hw_blocksize);
  498. err = -EINVAL;
  499. goto failed_sbh;
  500. }
  501. nilfs_release_super_block(nilfs);
  502. sb_set_blocksize(sb, blocksize);
  503. err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
  504. if (err)
  505. goto out;
  506. /* not failed_sbh; sbh is released automatically
  507. when reloading fails. */
  508. }
  509. nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
  510. nilfs->ns_blocksize = blocksize;
  511. err = nilfs_store_disk_layout(nilfs, sbp);
  512. if (err)
  513. goto failed_sbh;
  514. sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
  515. nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
  516. err = nilfs_store_log_cursor(nilfs, sbp);
  517. if (err)
  518. goto failed_sbh;
  519. set_nilfs_init(nilfs);
  520. err = 0;
  521. out:
  522. up_write(&nilfs->ns_sem);
  523. return err;
  524. failed_sbh:
  525. nilfs_release_super_block(nilfs);
  526. goto out;
  527. }
  528. int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
  529. size_t nsegs)
  530. {
  531. sector_t seg_start, seg_end;
  532. sector_t start = 0, nblocks = 0;
  533. unsigned int sects_per_block;
  534. __u64 *sn;
  535. int ret = 0;
  536. sects_per_block = (1 << nilfs->ns_blocksize_bits) /
  537. bdev_logical_block_size(nilfs->ns_bdev);
  538. for (sn = segnump; sn < segnump + nsegs; sn++) {
  539. nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
  540. if (!nblocks) {
  541. start = seg_start;
  542. nblocks = seg_end - seg_start + 1;
  543. } else if (start + nblocks == seg_start) {
  544. nblocks += seg_end - seg_start + 1;
  545. } else {
  546. ret = blkdev_issue_discard(nilfs->ns_bdev,
  547. start * sects_per_block,
  548. nblocks * sects_per_block,
  549. GFP_NOFS, 0);
  550. if (ret < 0)
  551. return ret;
  552. nblocks = 0;
  553. }
  554. }
  555. if (nblocks)
  556. ret = blkdev_issue_discard(nilfs->ns_bdev,
  557. start * sects_per_block,
  558. nblocks * sects_per_block,
  559. GFP_NOFS, 0);
  560. return ret;
  561. }
  562. int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
  563. {
  564. unsigned long ncleansegs;
  565. down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  566. ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
  567. up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  568. *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
  569. return 0;
  570. }
  571. int nilfs_near_disk_full(struct the_nilfs *nilfs)
  572. {
  573. unsigned long ncleansegs, nincsegs;
  574. ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
  575. nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
  576. nilfs->ns_blocks_per_segment + 1;
  577. return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
  578. }
  579. struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
  580. {
  581. struct rb_node *n;
  582. struct nilfs_root *root;
  583. spin_lock(&nilfs->ns_cptree_lock);
  584. n = nilfs->ns_cptree.rb_node;
  585. while (n) {
  586. root = rb_entry(n, struct nilfs_root, rb_node);
  587. if (cno < root->cno) {
  588. n = n->rb_left;
  589. } else if (cno > root->cno) {
  590. n = n->rb_right;
  591. } else {
  592. atomic_inc(&root->count);
  593. spin_unlock(&nilfs->ns_cptree_lock);
  594. return root;
  595. }
  596. }
  597. spin_unlock(&nilfs->ns_cptree_lock);
  598. return NULL;
  599. }
  600. struct nilfs_root *
  601. nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
  602. {
  603. struct rb_node **p, *parent;
  604. struct nilfs_root *root, *new;
  605. root = nilfs_lookup_root(nilfs, cno);
  606. if (root)
  607. return root;
  608. new = kmalloc(sizeof(*root), GFP_KERNEL);
  609. if (!new)
  610. return NULL;
  611. spin_lock(&nilfs->ns_cptree_lock);
  612. p = &nilfs->ns_cptree.rb_node;
  613. parent = NULL;
  614. while (*p) {
  615. parent = *p;
  616. root = rb_entry(parent, struct nilfs_root, rb_node);
  617. if (cno < root->cno) {
  618. p = &(*p)->rb_left;
  619. } else if (cno > root->cno) {
  620. p = &(*p)->rb_right;
  621. } else {
  622. atomic_inc(&root->count);
  623. spin_unlock(&nilfs->ns_cptree_lock);
  624. kfree(new);
  625. return root;
  626. }
  627. }
  628. new->cno = cno;
  629. new->ifile = NULL;
  630. new->nilfs = nilfs;
  631. atomic_set(&new->count, 1);
  632. atomic_set(&new->inodes_count, 0);
  633. atomic_set(&new->blocks_count, 0);
  634. rb_link_node(&new->rb_node, parent, p);
  635. rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
  636. spin_unlock(&nilfs->ns_cptree_lock);
  637. return new;
  638. }
  639. void nilfs_put_root(struct nilfs_root *root)
  640. {
  641. if (atomic_dec_and_test(&root->count)) {
  642. struct the_nilfs *nilfs = root->nilfs;
  643. spin_lock(&nilfs->ns_cptree_lock);
  644. rb_erase(&root->rb_node, &nilfs->ns_cptree);
  645. spin_unlock(&nilfs->ns_cptree_lock);
  646. if (root->ifile)
  647. iput(root->ifile);
  648. kfree(root);
  649. }
  650. }