recovery.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * recovery.c - NILFS recovery logic
  4. *
  5. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  6. *
  7. * Written by Ryusuke Konishi.
  8. */
  9. #include <linux/buffer_head.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/swap.h>
  12. #include <linux/slab.h>
  13. #include <linux/crc32.h>
  14. #include "nilfs.h"
  15. #include "segment.h"
  16. #include "sufile.h"
  17. #include "page.h"
  18. #include "segbuf.h"
  19. /*
  20. * Segment check result
  21. */
  22. enum {
  23. NILFS_SEG_VALID,
  24. NILFS_SEG_NO_SUPER_ROOT,
  25. NILFS_SEG_FAIL_IO,
  26. NILFS_SEG_FAIL_MAGIC,
  27. NILFS_SEG_FAIL_SEQ,
  28. NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT,
  29. NILFS_SEG_FAIL_CHECKSUM_FULL,
  30. NILFS_SEG_FAIL_CONSISTENCY,
  31. };
  32. /* work structure for recovery */
  33. struct nilfs_recovery_block {
  34. ino_t ino; /*
  35. * Inode number of the file that this block
  36. * belongs to
  37. */
  38. sector_t blocknr; /* block number */
  39. __u64 vblocknr; /* virtual block number */
  40. unsigned long blkoff; /* File offset of the data block (per block) */
  41. struct list_head list;
  42. };
  43. static int nilfs_warn_segment_error(struct super_block *sb, int err)
  44. {
  45. const char *msg = NULL;
  46. switch (err) {
  47. case NILFS_SEG_FAIL_IO:
  48. nilfs_err(sb, "I/O error reading segment");
  49. return -EIO;
  50. case NILFS_SEG_FAIL_MAGIC:
  51. msg = "Magic number mismatch";
  52. break;
  53. case NILFS_SEG_FAIL_SEQ:
  54. msg = "Sequence number mismatch";
  55. break;
  56. case NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT:
  57. msg = "Checksum error in super root";
  58. break;
  59. case NILFS_SEG_FAIL_CHECKSUM_FULL:
  60. msg = "Checksum error in segment payload";
  61. break;
  62. case NILFS_SEG_FAIL_CONSISTENCY:
  63. msg = "Inconsistency found";
  64. break;
  65. case NILFS_SEG_NO_SUPER_ROOT:
  66. msg = "No super root in the last segment";
  67. break;
  68. default:
  69. nilfs_err(sb, "unrecognized segment error %d", err);
  70. return -EINVAL;
  71. }
  72. nilfs_warn(sb, "invalid segment: %s", msg);
  73. return -EINVAL;
  74. }
  75. /**
  76. * nilfs_compute_checksum - compute checksum of blocks continuously
  77. * @nilfs: nilfs object
  78. * @bhs: buffer head of start block
  79. * @sum: place to store result
  80. * @offset: offset bytes in the first block
  81. * @check_bytes: number of bytes to be checked
  82. * @start: DBN of start block
  83. * @nblock: number of blocks to be checked
  84. */
  85. static int nilfs_compute_checksum(struct the_nilfs *nilfs,
  86. struct buffer_head *bhs, u32 *sum,
  87. unsigned long offset, u64 check_bytes,
  88. sector_t start, unsigned long nblock)
  89. {
  90. unsigned int blocksize = nilfs->ns_blocksize;
  91. unsigned long size;
  92. u32 crc;
  93. BUG_ON(offset >= blocksize);
  94. check_bytes -= offset;
  95. size = min_t(u64, check_bytes, blocksize - offset);
  96. crc = crc32_le(nilfs->ns_crc_seed,
  97. (unsigned char *)bhs->b_data + offset, size);
  98. if (--nblock > 0) {
  99. do {
  100. struct buffer_head *bh;
  101. bh = __bread(nilfs->ns_bdev, ++start, blocksize);
  102. if (!bh)
  103. return -EIO;
  104. check_bytes -= size;
  105. size = min_t(u64, check_bytes, blocksize);
  106. crc = crc32_le(crc, bh->b_data, size);
  107. brelse(bh);
  108. } while (--nblock > 0);
  109. }
  110. *sum = crc;
  111. return 0;
  112. }
  113. /**
  114. * nilfs_read_super_root_block - read super root block
  115. * @nilfs: nilfs object
  116. * @sr_block: disk block number of the super root block
  117. * @pbh: address of a buffer_head pointer to return super root buffer
  118. * @check: CRC check flag
  119. */
  120. int nilfs_read_super_root_block(struct the_nilfs *nilfs, sector_t sr_block,
  121. struct buffer_head **pbh, int check)
  122. {
  123. struct buffer_head *bh_sr;
  124. struct nilfs_super_root *sr;
  125. u32 crc;
  126. int ret;
  127. *pbh = NULL;
  128. bh_sr = __bread(nilfs->ns_bdev, sr_block, nilfs->ns_blocksize);
  129. if (unlikely(!bh_sr)) {
  130. ret = NILFS_SEG_FAIL_IO;
  131. goto failed;
  132. }
  133. sr = (struct nilfs_super_root *)bh_sr->b_data;
  134. if (check) {
  135. unsigned int bytes = le16_to_cpu(sr->sr_bytes);
  136. if (bytes == 0 || bytes > nilfs->ns_blocksize) {
  137. ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT;
  138. goto failed_bh;
  139. }
  140. if (nilfs_compute_checksum(
  141. nilfs, bh_sr, &crc, sizeof(sr->sr_sum), bytes,
  142. sr_block, 1)) {
  143. ret = NILFS_SEG_FAIL_IO;
  144. goto failed_bh;
  145. }
  146. if (crc != le32_to_cpu(sr->sr_sum)) {
  147. ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT;
  148. goto failed_bh;
  149. }
  150. }
  151. *pbh = bh_sr;
  152. return 0;
  153. failed_bh:
  154. brelse(bh_sr);
  155. failed:
  156. return nilfs_warn_segment_error(nilfs->ns_sb, ret);
  157. }
  158. /**
  159. * nilfs_read_log_header - read summary header of the specified log
  160. * @nilfs: nilfs object
  161. * @start_blocknr: start block number of the log
  162. * @sum: pointer to return segment summary structure
  163. */
  164. static struct buffer_head *
  165. nilfs_read_log_header(struct the_nilfs *nilfs, sector_t start_blocknr,
  166. struct nilfs_segment_summary **sum)
  167. {
  168. struct buffer_head *bh_sum;
  169. bh_sum = __bread(nilfs->ns_bdev, start_blocknr, nilfs->ns_blocksize);
  170. if (bh_sum)
  171. *sum = (struct nilfs_segment_summary *)bh_sum->b_data;
  172. return bh_sum;
  173. }
  174. /**
  175. * nilfs_validate_log - verify consistency of log
  176. * @nilfs: nilfs object
  177. * @seg_seq: sequence number of segment
  178. * @bh_sum: buffer head of summary block
  179. * @sum: segment summary struct
  180. */
  181. static int nilfs_validate_log(struct the_nilfs *nilfs, u64 seg_seq,
  182. struct buffer_head *bh_sum,
  183. struct nilfs_segment_summary *sum)
  184. {
  185. unsigned long nblock;
  186. u32 crc;
  187. int ret;
  188. ret = NILFS_SEG_FAIL_MAGIC;
  189. if (le32_to_cpu(sum->ss_magic) != NILFS_SEGSUM_MAGIC)
  190. goto out;
  191. ret = NILFS_SEG_FAIL_SEQ;
  192. if (le64_to_cpu(sum->ss_seq) != seg_seq)
  193. goto out;
  194. nblock = le32_to_cpu(sum->ss_nblocks);
  195. ret = NILFS_SEG_FAIL_CONSISTENCY;
  196. if (unlikely(nblock == 0 || nblock > nilfs->ns_blocks_per_segment))
  197. /* This limits the number of blocks read in the CRC check */
  198. goto out;
  199. ret = NILFS_SEG_FAIL_IO;
  200. if (nilfs_compute_checksum(nilfs, bh_sum, &crc, sizeof(sum->ss_datasum),
  201. ((u64)nblock << nilfs->ns_blocksize_bits),
  202. bh_sum->b_blocknr, nblock))
  203. goto out;
  204. ret = NILFS_SEG_FAIL_CHECKSUM_FULL;
  205. if (crc != le32_to_cpu(sum->ss_datasum))
  206. goto out;
  207. ret = 0;
  208. out:
  209. return ret;
  210. }
  211. /**
  212. * nilfs_read_summary_info - read an item on summary blocks of a log
  213. * @nilfs: nilfs object
  214. * @pbh: the current buffer head on summary blocks [in, out]
  215. * @offset: the current byte offset on summary blocks [in, out]
  216. * @bytes: byte size of the item to be read
  217. */
  218. static void *nilfs_read_summary_info(struct the_nilfs *nilfs,
  219. struct buffer_head **pbh,
  220. unsigned int *offset, unsigned int bytes)
  221. {
  222. void *ptr;
  223. sector_t blocknr;
  224. BUG_ON((*pbh)->b_size < *offset);
  225. if (bytes > (*pbh)->b_size - *offset) {
  226. blocknr = (*pbh)->b_blocknr;
  227. brelse(*pbh);
  228. *pbh = __bread(nilfs->ns_bdev, blocknr + 1,
  229. nilfs->ns_blocksize);
  230. if (unlikely(!*pbh))
  231. return NULL;
  232. *offset = 0;
  233. }
  234. ptr = (*pbh)->b_data + *offset;
  235. *offset += bytes;
  236. return ptr;
  237. }
  238. /**
  239. * nilfs_skip_summary_info - skip items on summary blocks of a log
  240. * @nilfs: nilfs object
  241. * @pbh: the current buffer head on summary blocks [in, out]
  242. * @offset: the current byte offset on summary blocks [in, out]
  243. * @bytes: byte size of the item to be skipped
  244. * @count: number of items to be skipped
  245. */
  246. static void nilfs_skip_summary_info(struct the_nilfs *nilfs,
  247. struct buffer_head **pbh,
  248. unsigned int *offset, unsigned int bytes,
  249. unsigned long count)
  250. {
  251. unsigned int rest_item_in_current_block
  252. = ((*pbh)->b_size - *offset) / bytes;
  253. if (count <= rest_item_in_current_block) {
  254. *offset += bytes * count;
  255. } else {
  256. sector_t blocknr = (*pbh)->b_blocknr;
  257. unsigned int nitem_per_block = (*pbh)->b_size / bytes;
  258. unsigned int bcnt;
  259. count -= rest_item_in_current_block;
  260. bcnt = DIV_ROUND_UP(count, nitem_per_block);
  261. *offset = bytes * (count - (bcnt - 1) * nitem_per_block);
  262. brelse(*pbh);
  263. *pbh = __bread(nilfs->ns_bdev, blocknr + bcnt,
  264. nilfs->ns_blocksize);
  265. }
  266. }
  267. /**
  268. * nilfs_scan_dsync_log - get block information of a log written for data sync
  269. * @nilfs: nilfs object
  270. * @start_blocknr: start block number of the log
  271. * @sum: log summary information
  272. * @head: list head to add nilfs_recovery_block struct
  273. */
  274. static int nilfs_scan_dsync_log(struct the_nilfs *nilfs, sector_t start_blocknr,
  275. struct nilfs_segment_summary *sum,
  276. struct list_head *head)
  277. {
  278. struct buffer_head *bh;
  279. unsigned int offset;
  280. u32 nfinfo, sumbytes;
  281. sector_t blocknr;
  282. ino_t ino;
  283. int err = -EIO;
  284. nfinfo = le32_to_cpu(sum->ss_nfinfo);
  285. if (!nfinfo)
  286. return 0;
  287. sumbytes = le32_to_cpu(sum->ss_sumbytes);
  288. blocknr = start_blocknr + DIV_ROUND_UP(sumbytes, nilfs->ns_blocksize);
  289. bh = __bread(nilfs->ns_bdev, start_blocknr, nilfs->ns_blocksize);
  290. if (unlikely(!bh))
  291. goto out;
  292. offset = le16_to_cpu(sum->ss_bytes);
  293. for (;;) {
  294. unsigned long nblocks, ndatablk, nnodeblk;
  295. struct nilfs_finfo *finfo;
  296. finfo = nilfs_read_summary_info(nilfs, &bh, &offset,
  297. sizeof(*finfo));
  298. if (unlikely(!finfo))
  299. goto out;
  300. ino = le64_to_cpu(finfo->fi_ino);
  301. nblocks = le32_to_cpu(finfo->fi_nblocks);
  302. ndatablk = le32_to_cpu(finfo->fi_ndatablk);
  303. nnodeblk = nblocks - ndatablk;
  304. while (ndatablk-- > 0) {
  305. struct nilfs_recovery_block *rb;
  306. struct nilfs_binfo_v *binfo;
  307. binfo = nilfs_read_summary_info(nilfs, &bh, &offset,
  308. sizeof(*binfo));
  309. if (unlikely(!binfo))
  310. goto out;
  311. rb = kmalloc(sizeof(*rb), GFP_NOFS);
  312. if (unlikely(!rb)) {
  313. err = -ENOMEM;
  314. goto out;
  315. }
  316. rb->ino = ino;
  317. rb->blocknr = blocknr++;
  318. rb->vblocknr = le64_to_cpu(binfo->bi_vblocknr);
  319. rb->blkoff = le64_to_cpu(binfo->bi_blkoff);
  320. /* INIT_LIST_HEAD(&rb->list); */
  321. list_add_tail(&rb->list, head);
  322. }
  323. if (--nfinfo == 0)
  324. break;
  325. blocknr += nnodeblk; /* always 0 for data sync logs */
  326. nilfs_skip_summary_info(nilfs, &bh, &offset, sizeof(__le64),
  327. nnodeblk);
  328. if (unlikely(!bh))
  329. goto out;
  330. }
  331. err = 0;
  332. out:
  333. brelse(bh); /* brelse(NULL) is just ignored */
  334. return err;
  335. }
  336. static void dispose_recovery_list(struct list_head *head)
  337. {
  338. while (!list_empty(head)) {
  339. struct nilfs_recovery_block *rb;
  340. rb = list_first_entry(head, struct nilfs_recovery_block, list);
  341. list_del(&rb->list);
  342. kfree(rb);
  343. }
  344. }
  345. struct nilfs_segment_entry {
  346. struct list_head list;
  347. __u64 segnum;
  348. };
  349. static int nilfs_segment_list_add(struct list_head *head, __u64 segnum)
  350. {
  351. struct nilfs_segment_entry *ent = kmalloc(sizeof(*ent), GFP_NOFS);
  352. if (unlikely(!ent))
  353. return -ENOMEM;
  354. ent->segnum = segnum;
  355. INIT_LIST_HEAD(&ent->list);
  356. list_add_tail(&ent->list, head);
  357. return 0;
  358. }
  359. void nilfs_dispose_segment_list(struct list_head *head)
  360. {
  361. while (!list_empty(head)) {
  362. struct nilfs_segment_entry *ent;
  363. ent = list_first_entry(head, struct nilfs_segment_entry, list);
  364. list_del(&ent->list);
  365. kfree(ent);
  366. }
  367. }
  368. static int nilfs_prepare_segment_for_recovery(struct the_nilfs *nilfs,
  369. struct super_block *sb,
  370. struct nilfs_recovery_info *ri)
  371. {
  372. struct list_head *head = &ri->ri_used_segments;
  373. struct nilfs_segment_entry *ent, *n;
  374. struct inode *sufile = nilfs->ns_sufile;
  375. __u64 segnum[4];
  376. int err;
  377. int i;
  378. segnum[0] = nilfs->ns_segnum;
  379. segnum[1] = nilfs->ns_nextnum;
  380. segnum[2] = ri->ri_segnum;
  381. segnum[3] = ri->ri_nextnum;
  382. /*
  383. * Releasing the next segment of the latest super root.
  384. * The next segment is invalidated by this recovery.
  385. */
  386. err = nilfs_sufile_free(sufile, segnum[1]);
  387. if (unlikely(err))
  388. goto failed;
  389. for (i = 1; i < 4; i++) {
  390. err = nilfs_segment_list_add(head, segnum[i]);
  391. if (unlikely(err))
  392. goto failed;
  393. }
  394. /*
  395. * Collecting segments written after the latest super root.
  396. * These are marked dirty to avoid being reallocated in the next write.
  397. */
  398. list_for_each_entry_safe(ent, n, head, list) {
  399. if (ent->segnum != segnum[0]) {
  400. err = nilfs_sufile_scrap(sufile, ent->segnum);
  401. if (unlikely(err))
  402. goto failed;
  403. }
  404. list_del(&ent->list);
  405. kfree(ent);
  406. }
  407. /* Allocate new segments for recovery */
  408. err = nilfs_sufile_alloc(sufile, &segnum[0]);
  409. if (unlikely(err))
  410. goto failed;
  411. nilfs->ns_pseg_offset = 0;
  412. nilfs->ns_seg_seq = ri->ri_seq + 2;
  413. nilfs->ns_nextnum = nilfs->ns_segnum = segnum[0];
  414. failed:
  415. /* No need to recover sufile because it will be destroyed on error */
  416. return err;
  417. }
  418. static int nilfs_recovery_copy_block(struct the_nilfs *nilfs,
  419. struct nilfs_recovery_block *rb,
  420. struct page *page)
  421. {
  422. struct buffer_head *bh_org;
  423. void *kaddr;
  424. bh_org = __bread(nilfs->ns_bdev, rb->blocknr, nilfs->ns_blocksize);
  425. if (unlikely(!bh_org))
  426. return -EIO;
  427. kaddr = kmap_atomic(page);
  428. memcpy(kaddr + bh_offset(bh_org), bh_org->b_data, bh_org->b_size);
  429. kunmap_atomic(kaddr);
  430. brelse(bh_org);
  431. return 0;
  432. }
  433. static int nilfs_recover_dsync_blocks(struct the_nilfs *nilfs,
  434. struct super_block *sb,
  435. struct nilfs_root *root,
  436. struct list_head *head,
  437. unsigned long *nr_salvaged_blocks)
  438. {
  439. struct inode *inode;
  440. struct nilfs_recovery_block *rb, *n;
  441. unsigned int blocksize = nilfs->ns_blocksize;
  442. struct page *page;
  443. loff_t pos;
  444. int err = 0, err2 = 0;
  445. list_for_each_entry_safe(rb, n, head, list) {
  446. inode = nilfs_iget(sb, root, rb->ino);
  447. if (IS_ERR(inode)) {
  448. err = PTR_ERR(inode);
  449. inode = NULL;
  450. goto failed_inode;
  451. }
  452. pos = rb->blkoff << inode->i_blkbits;
  453. err = block_write_begin(inode->i_mapping, pos, blocksize,
  454. 0, &page, nilfs_get_block);
  455. if (unlikely(err)) {
  456. loff_t isize = inode->i_size;
  457. if (pos + blocksize > isize)
  458. nilfs_write_failed(inode->i_mapping,
  459. pos + blocksize);
  460. goto failed_inode;
  461. }
  462. err = nilfs_recovery_copy_block(nilfs, rb, page);
  463. if (unlikely(err))
  464. goto failed_page;
  465. err = nilfs_set_file_dirty(inode, 1);
  466. if (unlikely(err))
  467. goto failed_page;
  468. block_write_end(NULL, inode->i_mapping, pos, blocksize,
  469. blocksize, page, NULL);
  470. unlock_page(page);
  471. put_page(page);
  472. (*nr_salvaged_blocks)++;
  473. goto next;
  474. failed_page:
  475. unlock_page(page);
  476. put_page(page);
  477. failed_inode:
  478. nilfs_warn(sb,
  479. "error %d recovering data block (ino=%lu, block-offset=%llu)",
  480. err, (unsigned long)rb->ino,
  481. (unsigned long long)rb->blkoff);
  482. if (!err2)
  483. err2 = err;
  484. next:
  485. iput(inode); /* iput(NULL) is just ignored */
  486. list_del_init(&rb->list);
  487. kfree(rb);
  488. }
  489. return err2;
  490. }
  491. /**
  492. * nilfs_do_roll_forward - salvage logical segments newer than the latest
  493. * checkpoint
  494. * @nilfs: nilfs object
  495. * @sb: super block instance
  496. * @ri: pointer to a nilfs_recovery_info
  497. */
  498. static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
  499. struct super_block *sb,
  500. struct nilfs_root *root,
  501. struct nilfs_recovery_info *ri)
  502. {
  503. struct buffer_head *bh_sum = NULL;
  504. struct nilfs_segment_summary *sum = NULL;
  505. sector_t pseg_start;
  506. sector_t seg_start, seg_end; /* Starting/ending DBN of full segment */
  507. unsigned long nsalvaged_blocks = 0;
  508. unsigned int flags;
  509. u64 seg_seq;
  510. __u64 segnum, nextnum = 0;
  511. int empty_seg = 0;
  512. int err = 0, ret;
  513. LIST_HEAD(dsync_blocks); /* list of data blocks to be recovered */
  514. enum {
  515. RF_INIT_ST,
  516. RF_DSYNC_ST, /* scanning data-sync segments */
  517. };
  518. int state = RF_INIT_ST;
  519. pseg_start = ri->ri_lsegs_start;
  520. seg_seq = ri->ri_lsegs_start_seq;
  521. segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
  522. nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
  523. while (segnum != ri->ri_segnum || pseg_start <= ri->ri_pseg_start) {
  524. brelse(bh_sum);
  525. bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum);
  526. if (!bh_sum) {
  527. err = -EIO;
  528. goto failed;
  529. }
  530. ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum);
  531. if (ret) {
  532. if (ret == NILFS_SEG_FAIL_IO) {
  533. err = -EIO;
  534. goto failed;
  535. }
  536. goto strayed;
  537. }
  538. flags = le16_to_cpu(sum->ss_flags);
  539. if (flags & NILFS_SS_SR)
  540. goto confused;
  541. /* Found a valid partial segment; do recovery actions */
  542. nextnum = nilfs_get_segnum_of_block(nilfs,
  543. le64_to_cpu(sum->ss_next));
  544. empty_seg = 0;
  545. nilfs->ns_ctime = le64_to_cpu(sum->ss_create);
  546. if (!(flags & NILFS_SS_GC))
  547. nilfs->ns_nongc_ctime = nilfs->ns_ctime;
  548. switch (state) {
  549. case RF_INIT_ST:
  550. if (!(flags & NILFS_SS_LOGBGN) ||
  551. !(flags & NILFS_SS_SYNDT))
  552. goto try_next_pseg;
  553. state = RF_DSYNC_ST;
  554. fallthrough;
  555. case RF_DSYNC_ST:
  556. if (!(flags & NILFS_SS_SYNDT))
  557. goto confused;
  558. err = nilfs_scan_dsync_log(nilfs, pseg_start, sum,
  559. &dsync_blocks);
  560. if (unlikely(err))
  561. goto failed;
  562. if (flags & NILFS_SS_LOGEND) {
  563. err = nilfs_recover_dsync_blocks(
  564. nilfs, sb, root, &dsync_blocks,
  565. &nsalvaged_blocks);
  566. if (unlikely(err))
  567. goto failed;
  568. state = RF_INIT_ST;
  569. }
  570. break; /* Fall through to try_next_pseg */
  571. }
  572. try_next_pseg:
  573. if (pseg_start == ri->ri_lsegs_end)
  574. break;
  575. pseg_start += le32_to_cpu(sum->ss_nblocks);
  576. if (pseg_start < seg_end)
  577. continue;
  578. goto feed_segment;
  579. strayed:
  580. if (pseg_start == ri->ri_lsegs_end)
  581. break;
  582. feed_segment:
  583. /* Looking to the next full segment */
  584. if (empty_seg++)
  585. break;
  586. seg_seq++;
  587. segnum = nextnum;
  588. nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
  589. pseg_start = seg_start;
  590. }
  591. if (nsalvaged_blocks) {
  592. nilfs_info(sb, "salvaged %lu blocks", nsalvaged_blocks);
  593. ri->ri_need_recovery = NILFS_RECOVERY_ROLLFORWARD_DONE;
  594. }
  595. out:
  596. brelse(bh_sum);
  597. dispose_recovery_list(&dsync_blocks);
  598. return err;
  599. confused:
  600. err = -EINVAL;
  601. failed:
  602. nilfs_err(sb,
  603. "error %d roll-forwarding partial segment at blocknr = %llu",
  604. err, (unsigned long long)pseg_start);
  605. goto out;
  606. }
  607. static void nilfs_finish_roll_forward(struct the_nilfs *nilfs,
  608. struct nilfs_recovery_info *ri)
  609. {
  610. struct buffer_head *bh;
  611. int err;
  612. if (nilfs_get_segnum_of_block(nilfs, ri->ri_lsegs_start) !=
  613. nilfs_get_segnum_of_block(nilfs, ri->ri_super_root))
  614. return;
  615. bh = __getblk(nilfs->ns_bdev, ri->ri_lsegs_start, nilfs->ns_blocksize);
  616. BUG_ON(!bh);
  617. memset(bh->b_data, 0, bh->b_size);
  618. set_buffer_dirty(bh);
  619. err = sync_dirty_buffer(bh);
  620. if (unlikely(err))
  621. nilfs_warn(nilfs->ns_sb,
  622. "buffer sync write failed during post-cleaning of recovery.");
  623. brelse(bh);
  624. }
  625. /**
  626. * nilfs_salvage_orphan_logs - salvage logs written after the latest checkpoint
  627. * @nilfs: nilfs object
  628. * @sb: super block instance
  629. * @ri: pointer to a nilfs_recovery_info struct to store search results.
  630. *
  631. * Return Value: On success, 0 is returned. On error, one of the following
  632. * negative error code is returned.
  633. *
  634. * %-EINVAL - Inconsistent filesystem state.
  635. *
  636. * %-EIO - I/O error
  637. *
  638. * %-ENOSPC - No space left on device (only in a panic state).
  639. *
  640. * %-ERESTARTSYS - Interrupted.
  641. *
  642. * %-ENOMEM - Insufficient memory available.
  643. */
  644. int nilfs_salvage_orphan_logs(struct the_nilfs *nilfs,
  645. struct super_block *sb,
  646. struct nilfs_recovery_info *ri)
  647. {
  648. struct nilfs_root *root;
  649. int err;
  650. if (ri->ri_lsegs_start == 0 || ri->ri_lsegs_end == 0)
  651. return 0;
  652. err = nilfs_attach_checkpoint(sb, ri->ri_cno, true, &root);
  653. if (unlikely(err)) {
  654. nilfs_err(sb, "error %d loading the latest checkpoint", err);
  655. return err;
  656. }
  657. err = nilfs_do_roll_forward(nilfs, sb, root, ri);
  658. if (unlikely(err))
  659. goto failed;
  660. if (ri->ri_need_recovery == NILFS_RECOVERY_ROLLFORWARD_DONE) {
  661. err = nilfs_prepare_segment_for_recovery(nilfs, sb, ri);
  662. if (unlikely(err)) {
  663. nilfs_err(sb, "error %d preparing segment for recovery",
  664. err);
  665. goto failed;
  666. }
  667. err = nilfs_attach_log_writer(sb, root);
  668. if (unlikely(err))
  669. goto failed;
  670. set_nilfs_discontinued(nilfs);
  671. err = nilfs_construct_segment(sb);
  672. nilfs_detach_log_writer(sb);
  673. if (unlikely(err)) {
  674. nilfs_err(sb, "error %d writing segment for recovery",
  675. err);
  676. goto failed;
  677. }
  678. nilfs_finish_roll_forward(nilfs, ri);
  679. }
  680. failed:
  681. nilfs_put_root(root);
  682. return err;
  683. }
  684. /**
  685. * nilfs_search_super_root - search the latest valid super root
  686. * @nilfs: the_nilfs
  687. * @ri: pointer to a nilfs_recovery_info struct to store search results.
  688. *
  689. * nilfs_search_super_root() looks for the latest super-root from a partial
  690. * segment pointed by the superblock. It sets up struct the_nilfs through
  691. * this search. It fills nilfs_recovery_info (ri) required for recovery.
  692. *
  693. * Return Value: On success, 0 is returned. On error, one of the following
  694. * negative error code is returned.
  695. *
  696. * %-EINVAL - No valid segment found
  697. *
  698. * %-EIO - I/O error
  699. *
  700. * %-ENOMEM - Insufficient memory available.
  701. */
  702. int nilfs_search_super_root(struct the_nilfs *nilfs,
  703. struct nilfs_recovery_info *ri)
  704. {
  705. struct buffer_head *bh_sum = NULL;
  706. struct nilfs_segment_summary *sum = NULL;
  707. sector_t pseg_start, pseg_end, sr_pseg_start = 0;
  708. sector_t seg_start, seg_end; /* range of full segment (block number) */
  709. sector_t b, end;
  710. unsigned long nblocks;
  711. unsigned int flags;
  712. u64 seg_seq;
  713. __u64 segnum, nextnum = 0;
  714. __u64 cno;
  715. LIST_HEAD(segments);
  716. int empty_seg = 0, scan_newer = 0;
  717. int ret;
  718. pseg_start = nilfs->ns_last_pseg;
  719. seg_seq = nilfs->ns_last_seq;
  720. cno = nilfs->ns_last_cno;
  721. segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
  722. /* Calculate range of segment */
  723. nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
  724. /* Read ahead segment */
  725. b = seg_start;
  726. while (b <= seg_end)
  727. __breadahead(nilfs->ns_bdev, b++, nilfs->ns_blocksize);
  728. for (;;) {
  729. brelse(bh_sum);
  730. ret = NILFS_SEG_FAIL_IO;
  731. bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum);
  732. if (!bh_sum)
  733. goto failed;
  734. ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum);
  735. if (ret) {
  736. if (ret == NILFS_SEG_FAIL_IO)
  737. goto failed;
  738. goto strayed;
  739. }
  740. nblocks = le32_to_cpu(sum->ss_nblocks);
  741. pseg_end = pseg_start + nblocks - 1;
  742. if (unlikely(pseg_end > seg_end)) {
  743. ret = NILFS_SEG_FAIL_CONSISTENCY;
  744. goto strayed;
  745. }
  746. /* A valid partial segment */
  747. ri->ri_pseg_start = pseg_start;
  748. ri->ri_seq = seg_seq;
  749. ri->ri_segnum = segnum;
  750. nextnum = nilfs_get_segnum_of_block(nilfs,
  751. le64_to_cpu(sum->ss_next));
  752. ri->ri_nextnum = nextnum;
  753. empty_seg = 0;
  754. flags = le16_to_cpu(sum->ss_flags);
  755. if (!(flags & NILFS_SS_SR) && !scan_newer) {
  756. /*
  757. * This will never happen because a superblock
  758. * (last_segment) always points to a pseg with
  759. * a super root.
  760. */
  761. ret = NILFS_SEG_FAIL_CONSISTENCY;
  762. goto failed;
  763. }
  764. if (pseg_start == seg_start) {
  765. nilfs_get_segment_range(nilfs, nextnum, &b, &end);
  766. while (b <= end)
  767. __breadahead(nilfs->ns_bdev, b++,
  768. nilfs->ns_blocksize);
  769. }
  770. if (!(flags & NILFS_SS_SR)) {
  771. if (!ri->ri_lsegs_start && (flags & NILFS_SS_LOGBGN)) {
  772. ri->ri_lsegs_start = pseg_start;
  773. ri->ri_lsegs_start_seq = seg_seq;
  774. }
  775. if (flags & NILFS_SS_LOGEND)
  776. ri->ri_lsegs_end = pseg_start;
  777. goto try_next_pseg;
  778. }
  779. /* A valid super root was found. */
  780. ri->ri_cno = cno++;
  781. ri->ri_super_root = pseg_end;
  782. ri->ri_lsegs_start = ri->ri_lsegs_end = 0;
  783. nilfs_dispose_segment_list(&segments);
  784. sr_pseg_start = pseg_start;
  785. nilfs->ns_pseg_offset = pseg_start + nblocks - seg_start;
  786. nilfs->ns_seg_seq = seg_seq;
  787. nilfs->ns_segnum = segnum;
  788. nilfs->ns_cno = cno; /* nilfs->ns_cno = ri->ri_cno + 1 */
  789. nilfs->ns_ctime = le64_to_cpu(sum->ss_create);
  790. nilfs->ns_nextnum = nextnum;
  791. if (scan_newer)
  792. ri->ri_need_recovery = NILFS_RECOVERY_SR_UPDATED;
  793. else {
  794. if (nilfs->ns_mount_state & NILFS_VALID_FS)
  795. goto super_root_found;
  796. scan_newer = 1;
  797. }
  798. try_next_pseg:
  799. /* Standing on a course, or met an inconsistent state */
  800. pseg_start += nblocks;
  801. if (pseg_start < seg_end)
  802. continue;
  803. goto feed_segment;
  804. strayed:
  805. /* Off the trail */
  806. if (!scan_newer)
  807. /*
  808. * This can happen if a checkpoint was written without
  809. * barriers, or as a result of an I/O failure.
  810. */
  811. goto failed;
  812. feed_segment:
  813. /* Looking to the next full segment */
  814. if (empty_seg++)
  815. goto super_root_found; /* found a valid super root */
  816. ret = nilfs_segment_list_add(&segments, segnum);
  817. if (unlikely(ret))
  818. goto failed;
  819. seg_seq++;
  820. segnum = nextnum;
  821. nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
  822. pseg_start = seg_start;
  823. }
  824. super_root_found:
  825. /* Updating pointers relating to the latest checkpoint */
  826. brelse(bh_sum);
  827. list_splice_tail(&segments, &ri->ri_used_segments);
  828. nilfs->ns_last_pseg = sr_pseg_start;
  829. nilfs->ns_last_seq = nilfs->ns_seg_seq;
  830. nilfs->ns_last_cno = ri->ri_cno;
  831. return 0;
  832. failed:
  833. brelse(bh_sum);
  834. nilfs_dispose_segment_list(&segments);
  835. return ret < 0 ? ret : nilfs_warn_segment_error(nilfs->ns_sb, ret);
  836. }