the_nilfs.c 21 KB

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