the_nilfs.c 20 KB

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