recovery.c 24 KB

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