recovery.c 24 KB

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