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