the_nilfs.c 23 KB

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