the_nilfs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * 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/log2.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: a pointer to the allocated nilfs object on success, or NULL on
  45. * failure.
  46. */
  47. struct the_nilfs *alloc_nilfs(struct super_block *sb)
  48. {
  49. struct the_nilfs *nilfs;
  50. nilfs = kzalloc_obj(*nilfs);
  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_last_segment_lock);
  62. nilfs->ns_cptree = RB_ROOT;
  63. spin_lock_init(&nilfs->ns_cptree_lock);
  64. init_rwsem(&nilfs->ns_segctor_sem);
  65. nilfs->ns_sb_update_freq = NILFS_SB_FREQ;
  66. return nilfs;
  67. }
  68. /**
  69. * destroy_nilfs - destroy nilfs object
  70. * @nilfs: nilfs object to be released
  71. */
  72. void destroy_nilfs(struct the_nilfs *nilfs)
  73. {
  74. might_sleep();
  75. if (nilfs_init(nilfs)) {
  76. brelse(nilfs->ns_sbh[0]);
  77. brelse(nilfs->ns_sbh[1]);
  78. }
  79. kfree(nilfs);
  80. }
  81. static int nilfs_load_super_root(struct the_nilfs *nilfs,
  82. struct super_block *sb, sector_t sr_block)
  83. {
  84. struct buffer_head *bh_sr;
  85. struct nilfs_super_root *raw_sr;
  86. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  87. struct nilfs_inode *rawi;
  88. unsigned int dat_entry_size, segment_usage_size, checkpoint_size;
  89. unsigned int inode_size;
  90. int err;
  91. err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
  92. if (unlikely(err))
  93. return err;
  94. down_read(&nilfs->ns_sem);
  95. dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
  96. checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
  97. segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
  98. up_read(&nilfs->ns_sem);
  99. inode_size = nilfs->ns_inode_size;
  100. rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size);
  101. err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat);
  102. if (err)
  103. goto failed;
  104. rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size);
  105. err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile);
  106. if (err)
  107. goto failed_dat;
  108. rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size);
  109. err = nilfs_sufile_read(sb, segment_usage_size, rawi,
  110. &nilfs->ns_sufile);
  111. if (err)
  112. goto failed_cpfile;
  113. raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
  114. nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
  115. failed:
  116. brelse(bh_sr);
  117. return err;
  118. failed_cpfile:
  119. iput(nilfs->ns_cpfile);
  120. failed_dat:
  121. iput(nilfs->ns_dat);
  122. goto failed;
  123. }
  124. static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
  125. {
  126. memset(ri, 0, sizeof(*ri));
  127. INIT_LIST_HEAD(&ri->ri_used_segments);
  128. }
  129. static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
  130. {
  131. nilfs_dispose_segment_list(&ri->ri_used_segments);
  132. }
  133. /**
  134. * nilfs_store_log_cursor - load log cursor from a super block
  135. * @nilfs: nilfs object
  136. * @sbp: buffer storing super block to be read
  137. *
  138. * nilfs_store_log_cursor() reads the last position of the log
  139. * containing a super root from a given super block, and initializes
  140. * relevant information on the nilfs object preparatory for log
  141. * scanning and recovery.
  142. *
  143. * Return: 0 on success, or %-EINVAL if current segment number is out
  144. * of range.
  145. */
  146. static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
  147. struct nilfs_super_block *sbp)
  148. {
  149. int ret = 0;
  150. nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
  151. nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
  152. nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
  153. nilfs->ns_prev_seq = nilfs->ns_last_seq;
  154. nilfs->ns_seg_seq = nilfs->ns_last_seq;
  155. nilfs->ns_segnum =
  156. nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
  157. nilfs->ns_cno = nilfs->ns_last_cno + 1;
  158. if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
  159. nilfs_err(nilfs->ns_sb,
  160. "pointed segment number is out of range: segnum=%llu, nsegments=%lu",
  161. (unsigned long long)nilfs->ns_segnum,
  162. nilfs->ns_nsegments);
  163. ret = -EINVAL;
  164. }
  165. return ret;
  166. }
  167. /**
  168. * nilfs_get_blocksize - get block size from raw superblock data
  169. * @sb: super block instance
  170. * @sbp: superblock raw data buffer
  171. * @blocksize: place to store block size
  172. *
  173. * nilfs_get_blocksize() calculates the block size from the block size
  174. * exponent information written in @sbp and stores it in @blocksize,
  175. * or aborts with an error message if it's too large.
  176. *
  177. * Return: 0 on success, or %-EINVAL if the block size is too large.
  178. */
  179. static int nilfs_get_blocksize(struct super_block *sb,
  180. struct nilfs_super_block *sbp, int *blocksize)
  181. {
  182. unsigned int shift_bits = le32_to_cpu(sbp->s_log_block_size);
  183. if (unlikely(shift_bits >
  184. ilog2(NILFS_MAX_BLOCK_SIZE) - BLOCK_SIZE_BITS)) {
  185. nilfs_err(sb, "too large filesystem blocksize: 2 ^ %u KiB",
  186. shift_bits);
  187. return -EINVAL;
  188. }
  189. *blocksize = BLOCK_SIZE << shift_bits;
  190. return 0;
  191. }
  192. /**
  193. * load_nilfs - load and recover the nilfs
  194. * @nilfs: the_nilfs structure to be released
  195. * @sb: super block instance used to recover past segment
  196. *
  197. * load_nilfs() searches and load the latest super root,
  198. * attaches the last segment, and does recovery if needed.
  199. * The caller must call this exclusively for simultaneous mounts.
  200. *
  201. * Return: 0 on success, or one of the following negative error codes on
  202. * failure:
  203. * * %-EINVAL - No valid segment found.
  204. * * %-EIO - I/O error.
  205. * * %-ENOMEM - Insufficient memory available.
  206. * * %-EROFS - Read only device or RO compat mode (if recovery is required)
  207. */
  208. int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb)
  209. {
  210. struct nilfs_recovery_info ri;
  211. unsigned int s_flags = sb->s_flags;
  212. int really_read_only = bdev_read_only(nilfs->ns_bdev);
  213. int valid_fs = nilfs_valid_fs(nilfs);
  214. int err;
  215. if (!valid_fs) {
  216. nilfs_warn(sb, "mounting unchecked fs");
  217. if (s_flags & SB_RDONLY) {
  218. nilfs_info(sb,
  219. "recovery required for readonly filesystem");
  220. nilfs_info(sb,
  221. "write access will be enabled during recovery");
  222. }
  223. }
  224. nilfs_init_recovery_info(&ri);
  225. err = nilfs_search_super_root(nilfs, &ri);
  226. if (unlikely(err)) {
  227. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  228. int blocksize;
  229. if (err != -EINVAL)
  230. goto scan_error;
  231. if (!nilfs_valid_sb(sbp[1])) {
  232. nilfs_warn(sb,
  233. "unable to fall back to spare super block");
  234. goto scan_error;
  235. }
  236. nilfs_info(sb, "trying rollback from an earlier position");
  237. /*
  238. * restore super block with its spare and reconfigure
  239. * relevant states of the nilfs object.
  240. */
  241. memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
  242. nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
  243. nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
  244. /* verify consistency between two super blocks */
  245. err = nilfs_get_blocksize(sb, sbp[0], &blocksize);
  246. if (err)
  247. goto scan_error;
  248. if (blocksize != nilfs->ns_blocksize) {
  249. nilfs_warn(sb,
  250. "blocksize differs between two super blocks (%d != %d)",
  251. blocksize, nilfs->ns_blocksize);
  252. err = -EINVAL;
  253. goto scan_error;
  254. }
  255. err = nilfs_store_log_cursor(nilfs, sbp[0]);
  256. if (err)
  257. goto scan_error;
  258. /* drop clean flag to allow roll-forward and recovery */
  259. nilfs->ns_mount_state &= ~NILFS_VALID_FS;
  260. valid_fs = 0;
  261. err = nilfs_search_super_root(nilfs, &ri);
  262. if (err)
  263. goto scan_error;
  264. }
  265. err = nilfs_load_super_root(nilfs, sb, ri.ri_super_root);
  266. if (unlikely(err)) {
  267. nilfs_err(sb, "error %d while loading super root", err);
  268. goto failed;
  269. }
  270. err = nilfs_sysfs_create_device_group(sb);
  271. if (unlikely(err))
  272. goto sysfs_error;
  273. if (valid_fs)
  274. goto skip_recovery;
  275. if (s_flags & SB_RDONLY) {
  276. __u64 features;
  277. if (nilfs_test_opt(nilfs, NORECOVERY)) {
  278. nilfs_info(sb,
  279. "norecovery option specified, skipping roll-forward recovery");
  280. goto skip_recovery;
  281. }
  282. features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
  283. ~NILFS_FEATURE_COMPAT_RO_SUPP;
  284. if (features) {
  285. nilfs_err(sb,
  286. "couldn't proceed with recovery because of unsupported optional features (%llx)",
  287. (unsigned long long)features);
  288. err = -EROFS;
  289. goto failed_unload;
  290. }
  291. if (really_read_only) {
  292. nilfs_err(sb,
  293. "write access unavailable, cannot proceed");
  294. err = -EROFS;
  295. goto failed_unload;
  296. }
  297. sb->s_flags &= ~SB_RDONLY;
  298. } else if (nilfs_test_opt(nilfs, NORECOVERY)) {
  299. nilfs_err(sb,
  300. "recovery cancelled because norecovery option was specified for a read/write mount");
  301. err = -EINVAL;
  302. goto failed_unload;
  303. }
  304. err = nilfs_salvage_orphan_logs(nilfs, sb, &ri);
  305. if (err)
  306. goto failed_unload;
  307. down_write(&nilfs->ns_sem);
  308. nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
  309. err = nilfs_cleanup_super(sb);
  310. up_write(&nilfs->ns_sem);
  311. if (err) {
  312. nilfs_err(sb,
  313. "error %d updating super block. recovery unfinished.",
  314. err);
  315. goto failed_unload;
  316. }
  317. nilfs_info(sb, "recovery complete");
  318. skip_recovery:
  319. nilfs_clear_recovery_info(&ri);
  320. sb->s_flags = s_flags;
  321. return 0;
  322. scan_error:
  323. nilfs_err(sb, "error %d while searching super root", err);
  324. goto failed;
  325. failed_unload:
  326. nilfs_sysfs_delete_device_group(nilfs);
  327. sysfs_error:
  328. iput(nilfs->ns_cpfile);
  329. iput(nilfs->ns_sufile);
  330. iput(nilfs->ns_dat);
  331. failed:
  332. nilfs_clear_recovery_info(&ri);
  333. sb->s_flags = s_flags;
  334. return err;
  335. }
  336. static unsigned long long nilfs_max_size(unsigned int blkbits)
  337. {
  338. unsigned int max_bits;
  339. unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
  340. max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
  341. if (max_bits < 64)
  342. res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
  343. return res;
  344. }
  345. /**
  346. * nilfs_nrsvsegs - calculate the number of reserved segments
  347. * @nilfs: nilfs object
  348. * @nsegs: total number of segments
  349. *
  350. * Return: Number of reserved segments.
  351. */
  352. unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs)
  353. {
  354. return max_t(unsigned long, NILFS_MIN_NRSVSEGS,
  355. DIV_ROUND_UP(nsegs * nilfs->ns_r_segments_percentage,
  356. 100));
  357. }
  358. /**
  359. * nilfs_max_segment_count - calculate the maximum number of segments
  360. * @nilfs: nilfs object
  361. *
  362. * Return: Maximum number of segments
  363. */
  364. static u64 nilfs_max_segment_count(struct the_nilfs *nilfs)
  365. {
  366. u64 max_count = U64_MAX;
  367. max_count = div64_ul(max_count, nilfs->ns_blocks_per_segment);
  368. return min_t(u64, max_count, ULONG_MAX);
  369. }
  370. void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs)
  371. {
  372. nilfs->ns_nsegments = nsegs;
  373. nilfs->ns_nrsvsegs = nilfs_nrsvsegs(nilfs, nsegs);
  374. }
  375. static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
  376. struct nilfs_super_block *sbp)
  377. {
  378. u64 nsegments, nblocks;
  379. if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
  380. nilfs_err(nilfs->ns_sb,
  381. "unsupported revision (superblock rev.=%d.%d, current rev.=%d.%d). Please check the version of mkfs.nilfs(2).",
  382. le32_to_cpu(sbp->s_rev_level),
  383. le16_to_cpu(sbp->s_minor_rev_level),
  384. NILFS_CURRENT_REV, NILFS_MINOR_REV);
  385. return -EINVAL;
  386. }
  387. nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
  388. if (nilfs->ns_sbsize > BLOCK_SIZE)
  389. return -EINVAL;
  390. nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
  391. if (nilfs->ns_inode_size > nilfs->ns_blocksize) {
  392. nilfs_err(nilfs->ns_sb, "too large inode size: %d bytes",
  393. nilfs->ns_inode_size);
  394. return -EINVAL;
  395. } else if (nilfs->ns_inode_size < NILFS_MIN_INODE_SIZE) {
  396. nilfs_err(nilfs->ns_sb, "too small inode size: %d bytes",
  397. nilfs->ns_inode_size);
  398. return -EINVAL;
  399. }
  400. nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
  401. if (nilfs->ns_first_ino < NILFS_USER_INO) {
  402. nilfs_err(nilfs->ns_sb,
  403. "too small lower limit for non-reserved inode numbers: %u",
  404. nilfs->ns_first_ino);
  405. return -EINVAL;
  406. }
  407. nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
  408. if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
  409. nilfs_err(nilfs->ns_sb, "too short segment: %lu blocks",
  410. nilfs->ns_blocks_per_segment);
  411. return -EINVAL;
  412. }
  413. nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
  414. nilfs->ns_r_segments_percentage =
  415. le32_to_cpu(sbp->s_r_segments_percentage);
  416. if (nilfs->ns_r_segments_percentage < 1 ||
  417. nilfs->ns_r_segments_percentage > 99) {
  418. nilfs_err(nilfs->ns_sb,
  419. "invalid reserved segments percentage: %lu",
  420. nilfs->ns_r_segments_percentage);
  421. return -EINVAL;
  422. }
  423. nsegments = le64_to_cpu(sbp->s_nsegments);
  424. if (nsegments > nilfs_max_segment_count(nilfs)) {
  425. nilfs_err(nilfs->ns_sb,
  426. "segment count %llu exceeds upper limit (%llu segments)",
  427. (unsigned long long)nsegments,
  428. (unsigned long long)nilfs_max_segment_count(nilfs));
  429. return -EINVAL;
  430. }
  431. nblocks = sb_bdev_nr_blocks(nilfs->ns_sb);
  432. if (nblocks) {
  433. u64 min_block_count = nsegments * nilfs->ns_blocks_per_segment;
  434. /*
  435. * To avoid failing to mount early device images without a
  436. * second superblock, exclude that block count from the
  437. * "min_block_count" calculation.
  438. */
  439. if (nblocks < min_block_count) {
  440. nilfs_err(nilfs->ns_sb,
  441. "total number of segment blocks %llu exceeds device size (%llu blocks)",
  442. (unsigned long long)min_block_count,
  443. (unsigned long long)nblocks);
  444. return -EINVAL;
  445. }
  446. }
  447. nilfs_set_nsegments(nilfs, nsegments);
  448. nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
  449. return 0;
  450. }
  451. static int nilfs_valid_sb(struct nilfs_super_block *sbp)
  452. {
  453. static unsigned char sum[4];
  454. const int sumoff = offsetof(struct nilfs_super_block, s_sum);
  455. size_t bytes;
  456. u32 crc;
  457. if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
  458. return 0;
  459. bytes = le16_to_cpu(sbp->s_bytes);
  460. if (bytes < sumoff + 4 || bytes > BLOCK_SIZE)
  461. return 0;
  462. crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
  463. sumoff);
  464. crc = crc32_le(crc, sum, 4);
  465. crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
  466. bytes - sumoff - 4);
  467. return crc == le32_to_cpu(sbp->s_sum);
  468. }
  469. /**
  470. * nilfs_sb2_bad_offset - check the location of the second superblock
  471. * @sbp: superblock raw data buffer
  472. * @offset: byte offset of second superblock calculated from device size
  473. *
  474. * nilfs_sb2_bad_offset() checks if the position on the second
  475. * superblock is valid or not based on the filesystem parameters
  476. * stored in @sbp. If @offset points to a location within the segment
  477. * area, or if the parameters themselves are not normal, it is
  478. * determined to be invalid.
  479. *
  480. * Return: true if invalid, false if valid.
  481. */
  482. static bool nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
  483. {
  484. unsigned int shift_bits = le32_to_cpu(sbp->s_log_block_size);
  485. u32 blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
  486. u64 nsegments = le64_to_cpu(sbp->s_nsegments);
  487. u64 index;
  488. if (blocks_per_segment < NILFS_SEG_MIN_BLOCKS ||
  489. shift_bits > ilog2(NILFS_MAX_BLOCK_SIZE) - BLOCK_SIZE_BITS)
  490. return true;
  491. index = offset >> (shift_bits + BLOCK_SIZE_BITS);
  492. do_div(index, blocks_per_segment);
  493. return index < nsegments;
  494. }
  495. static void nilfs_release_super_block(struct the_nilfs *nilfs)
  496. {
  497. int i;
  498. for (i = 0; i < 2; i++) {
  499. if (nilfs->ns_sbp[i]) {
  500. brelse(nilfs->ns_sbh[i]);
  501. nilfs->ns_sbh[i] = NULL;
  502. nilfs->ns_sbp[i] = NULL;
  503. }
  504. }
  505. }
  506. void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
  507. {
  508. brelse(nilfs->ns_sbh[0]);
  509. nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
  510. nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
  511. nilfs->ns_sbh[1] = NULL;
  512. nilfs->ns_sbp[1] = NULL;
  513. }
  514. void nilfs_swap_super_block(struct the_nilfs *nilfs)
  515. {
  516. struct buffer_head *tsbh = nilfs->ns_sbh[0];
  517. struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
  518. nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
  519. nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
  520. nilfs->ns_sbh[1] = tsbh;
  521. nilfs->ns_sbp[1] = tsbp;
  522. }
  523. static int nilfs_load_super_block(struct the_nilfs *nilfs,
  524. struct super_block *sb, int blocksize,
  525. struct nilfs_super_block **sbpp)
  526. {
  527. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  528. struct buffer_head **sbh = nilfs->ns_sbh;
  529. u64 sb2off, devsize = bdev_nr_bytes(nilfs->ns_bdev);
  530. int valid[2], swp = 0, older;
  531. if (devsize < NILFS_SEG_MIN_BLOCKS * NILFS_MIN_BLOCK_SIZE + 4096) {
  532. nilfs_err(sb, "device size too small");
  533. return -EINVAL;
  534. }
  535. sb2off = NILFS_SB2_OFFSET_BYTES(devsize);
  536. sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
  537. &sbh[0]);
  538. sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
  539. if (!sbp[0]) {
  540. if (!sbp[1]) {
  541. nilfs_err(sb, "unable to read superblock");
  542. return -EIO;
  543. }
  544. nilfs_warn(sb,
  545. "unable to read primary superblock (blocksize = %d)",
  546. blocksize);
  547. } else if (!sbp[1]) {
  548. nilfs_warn(sb,
  549. "unable to read secondary superblock (blocksize = %d)",
  550. blocksize);
  551. }
  552. /*
  553. * Compare two super blocks and set 1 in swp if the secondary
  554. * super block is valid and newer. Otherwise, set 0 in swp.
  555. */
  556. valid[0] = nilfs_valid_sb(sbp[0]);
  557. valid[1] = nilfs_valid_sb(sbp[1]);
  558. swp = valid[1] && (!valid[0] ||
  559. le64_to_cpu(sbp[1]->s_last_cno) >
  560. le64_to_cpu(sbp[0]->s_last_cno));
  561. if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
  562. brelse(sbh[1]);
  563. sbh[1] = NULL;
  564. sbp[1] = NULL;
  565. valid[1] = 0;
  566. swp = 0;
  567. }
  568. if (!valid[swp]) {
  569. nilfs_release_super_block(nilfs);
  570. nilfs_err(sb, "couldn't find nilfs on the device");
  571. return -EINVAL;
  572. }
  573. if (!valid[!swp])
  574. nilfs_warn(sb,
  575. "broken superblock, retrying with spare superblock (blocksize = %d)",
  576. blocksize);
  577. if (swp)
  578. nilfs_swap_super_block(nilfs);
  579. /*
  580. * Calculate the array index of the older superblock data.
  581. * If one has been dropped, set index 0 pointing to the remaining one,
  582. * otherwise set index 1 pointing to the old one (including if both
  583. * are the same).
  584. *
  585. * Divided case valid[0] valid[1] swp -> older
  586. * -------------------------------------------------------------
  587. * Both SBs are invalid 0 0 N/A (Error)
  588. * SB1 is invalid 0 1 1 0
  589. * SB2 is invalid 1 0 0 0
  590. * SB2 is newer 1 1 1 0
  591. * SB2 is older or the same 1 1 0 1
  592. */
  593. older = valid[1] ^ swp;
  594. nilfs->ns_sbwcount = 0;
  595. nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
  596. nilfs->ns_prot_seq = le64_to_cpu(sbp[older]->s_last_seq);
  597. *sbpp = sbp[0];
  598. return 0;
  599. }
  600. /**
  601. * init_nilfs - initialize a NILFS instance.
  602. * @nilfs: the_nilfs structure
  603. * @sb: super block
  604. *
  605. * init_nilfs() performs common initialization per block device (e.g.
  606. * reading the super block, getting disk layout information, initializing
  607. * shared fields in the_nilfs).
  608. *
  609. * Return: 0 on success, or a negative error code on failure.
  610. */
  611. int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb)
  612. {
  613. struct nilfs_super_block *sbp;
  614. int blocksize;
  615. int err;
  616. blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
  617. if (!blocksize) {
  618. nilfs_err(sb, "unable to set blocksize");
  619. err = -EINVAL;
  620. goto out;
  621. }
  622. err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
  623. if (err)
  624. goto out;
  625. err = nilfs_store_magic(sb, sbp);
  626. if (err)
  627. goto failed_sbh;
  628. err = nilfs_check_feature_compatibility(sb, sbp);
  629. if (err)
  630. goto failed_sbh;
  631. err = nilfs_get_blocksize(sb, sbp, &blocksize);
  632. if (err)
  633. goto failed_sbh;
  634. if (blocksize < NILFS_MIN_BLOCK_SIZE) {
  635. nilfs_err(sb,
  636. "couldn't mount because of unsupported filesystem blocksize %d",
  637. blocksize);
  638. err = -EINVAL;
  639. goto failed_sbh;
  640. }
  641. if (sb->s_blocksize != blocksize) {
  642. int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
  643. if (blocksize < hw_blocksize) {
  644. nilfs_err(sb,
  645. "blocksize %d too small for device (sector-size = %d)",
  646. blocksize, hw_blocksize);
  647. err = -EINVAL;
  648. goto failed_sbh;
  649. }
  650. nilfs_release_super_block(nilfs);
  651. if (!sb_set_blocksize(sb, blocksize)) {
  652. nilfs_err(sb, "bad blocksize %d", blocksize);
  653. err = -EINVAL;
  654. goto out;
  655. }
  656. err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
  657. if (err)
  658. goto out;
  659. /*
  660. * Not to failed_sbh; sbh is released automatically
  661. * when reloading fails.
  662. */
  663. }
  664. nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
  665. nilfs->ns_blocksize = blocksize;
  666. err = nilfs_store_disk_layout(nilfs, sbp);
  667. if (err)
  668. goto failed_sbh;
  669. sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
  670. nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
  671. err = nilfs_store_log_cursor(nilfs, sbp);
  672. if (err)
  673. goto failed_sbh;
  674. set_nilfs_init(nilfs);
  675. err = 0;
  676. out:
  677. return err;
  678. failed_sbh:
  679. nilfs_release_super_block(nilfs);
  680. goto out;
  681. }
  682. int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
  683. size_t nsegs)
  684. {
  685. sector_t seg_start, seg_end;
  686. sector_t start = 0, nblocks = 0;
  687. unsigned int sects_per_block;
  688. __u64 *sn;
  689. int ret = 0;
  690. sects_per_block = (1 << nilfs->ns_blocksize_bits) /
  691. bdev_logical_block_size(nilfs->ns_bdev);
  692. for (sn = segnump; sn < segnump + nsegs; sn++) {
  693. nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
  694. if (!nblocks) {
  695. start = seg_start;
  696. nblocks = seg_end - seg_start + 1;
  697. } else if (start + nblocks == seg_start) {
  698. nblocks += seg_end - seg_start + 1;
  699. } else {
  700. ret = blkdev_issue_discard(nilfs->ns_bdev,
  701. start * sects_per_block,
  702. nblocks * sects_per_block,
  703. GFP_NOFS);
  704. if (ret < 0)
  705. return ret;
  706. nblocks = 0;
  707. }
  708. }
  709. if (nblocks)
  710. ret = blkdev_issue_discard(nilfs->ns_bdev,
  711. start * sects_per_block,
  712. nblocks * sects_per_block,
  713. GFP_NOFS);
  714. return ret;
  715. }
  716. int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
  717. {
  718. unsigned long ncleansegs;
  719. ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
  720. *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
  721. return 0;
  722. }
  723. int nilfs_near_disk_full(struct the_nilfs *nilfs)
  724. {
  725. unsigned long ncleansegs, nincsegs;
  726. ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
  727. nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
  728. nilfs->ns_blocks_per_segment + 1;
  729. return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
  730. }
  731. struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
  732. {
  733. struct rb_node *n;
  734. struct nilfs_root *root;
  735. spin_lock(&nilfs->ns_cptree_lock);
  736. n = nilfs->ns_cptree.rb_node;
  737. while (n) {
  738. root = rb_entry(n, struct nilfs_root, rb_node);
  739. if (cno < root->cno) {
  740. n = n->rb_left;
  741. } else if (cno > root->cno) {
  742. n = n->rb_right;
  743. } else {
  744. refcount_inc(&root->count);
  745. spin_unlock(&nilfs->ns_cptree_lock);
  746. return root;
  747. }
  748. }
  749. spin_unlock(&nilfs->ns_cptree_lock);
  750. return NULL;
  751. }
  752. struct nilfs_root *
  753. nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
  754. {
  755. struct rb_node **p, *parent;
  756. struct nilfs_root *root, *new;
  757. int err;
  758. root = nilfs_lookup_root(nilfs, cno);
  759. if (root)
  760. return root;
  761. new = kzalloc_obj(*root);
  762. if (!new)
  763. return NULL;
  764. spin_lock(&nilfs->ns_cptree_lock);
  765. p = &nilfs->ns_cptree.rb_node;
  766. parent = NULL;
  767. while (*p) {
  768. parent = *p;
  769. root = rb_entry(parent, struct nilfs_root, rb_node);
  770. if (cno < root->cno) {
  771. p = &(*p)->rb_left;
  772. } else if (cno > root->cno) {
  773. p = &(*p)->rb_right;
  774. } else {
  775. refcount_inc(&root->count);
  776. spin_unlock(&nilfs->ns_cptree_lock);
  777. kfree(new);
  778. return root;
  779. }
  780. }
  781. new->cno = cno;
  782. new->ifile = NULL;
  783. new->nilfs = nilfs;
  784. refcount_set(&new->count, 1);
  785. atomic64_set(&new->inodes_count, 0);
  786. atomic64_set(&new->blocks_count, 0);
  787. rb_link_node(&new->rb_node, parent, p);
  788. rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
  789. spin_unlock(&nilfs->ns_cptree_lock);
  790. err = nilfs_sysfs_create_snapshot_group(new);
  791. if (err) {
  792. kfree(new);
  793. new = NULL;
  794. }
  795. return new;
  796. }
  797. void nilfs_put_root(struct nilfs_root *root)
  798. {
  799. struct the_nilfs *nilfs = root->nilfs;
  800. if (refcount_dec_and_lock(&root->count, &nilfs->ns_cptree_lock)) {
  801. rb_erase(&root->rb_node, &nilfs->ns_cptree);
  802. spin_unlock(&nilfs->ns_cptree_lock);
  803. nilfs_sysfs_delete_snapshot_group(root);
  804. iput(root->ifile);
  805. kfree(root);
  806. }
  807. }