recovery.c 23 KB

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