recovery.c 24 KB

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