recovery.c 24 KB

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