recovery.c 24 KB

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