the_nilfs.c 20 KB

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