the_nilfs.c 24 KB

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