the_nilfs.c 21 KB

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