recovery.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. rb = list_first_entry(head, struct nilfs_recovery_block, list);
  354. list_del(&rb->list);
  355. kfree(rb);
  356. }
  357. }
  358. struct nilfs_segment_entry {
  359. struct list_head list;
  360. __u64 segnum;
  361. };
  362. static int nilfs_segment_list_add(struct list_head *head, __u64 segnum)
  363. {
  364. struct nilfs_segment_entry *ent = kmalloc(sizeof(*ent), GFP_NOFS);
  365. if (unlikely(!ent))
  366. return -ENOMEM;
  367. ent->segnum = segnum;
  368. INIT_LIST_HEAD(&ent->list);
  369. list_add_tail(&ent->list, head);
  370. return 0;
  371. }
  372. void nilfs_dispose_segment_list(struct list_head *head)
  373. {
  374. while (!list_empty(head)) {
  375. struct nilfs_segment_entry *ent;
  376. ent = list_first_entry(head, struct nilfs_segment_entry, list);
  377. list_del(&ent->list);
  378. kfree(ent);
  379. }
  380. }
  381. static int nilfs_prepare_segment_for_recovery(struct the_nilfs *nilfs,
  382. struct super_block *sb,
  383. struct nilfs_recovery_info *ri)
  384. {
  385. struct list_head *head = &ri->ri_used_segments;
  386. struct nilfs_segment_entry *ent, *n;
  387. struct inode *sufile = nilfs->ns_sufile;
  388. __u64 segnum[4];
  389. int err;
  390. int i;
  391. segnum[0] = nilfs->ns_segnum;
  392. segnum[1] = nilfs->ns_nextnum;
  393. segnum[2] = ri->ri_segnum;
  394. segnum[3] = ri->ri_nextnum;
  395. /*
  396. * Releasing the next segment of the latest super root.
  397. * The next segment is invalidated by this recovery.
  398. */
  399. err = nilfs_sufile_free(sufile, segnum[1]);
  400. if (unlikely(err))
  401. goto failed;
  402. for (i = 1; i < 4; i++) {
  403. err = nilfs_segment_list_add(head, segnum[i]);
  404. if (unlikely(err))
  405. goto failed;
  406. }
  407. /*
  408. * Collecting segments written after the latest super root.
  409. * These are marked dirty to avoid being reallocated in the next write.
  410. */
  411. list_for_each_entry_safe(ent, n, head, list) {
  412. if (ent->segnum != segnum[0]) {
  413. err = nilfs_sufile_scrap(sufile, ent->segnum);
  414. if (unlikely(err))
  415. goto failed;
  416. }
  417. list_del(&ent->list);
  418. kfree(ent);
  419. }
  420. /* Allocate new segments for recovery */
  421. err = nilfs_sufile_alloc(sufile, &segnum[0]);
  422. if (unlikely(err))
  423. goto failed;
  424. nilfs->ns_pseg_offset = 0;
  425. nilfs->ns_seg_seq = ri->ri_seq + 2;
  426. nilfs->ns_nextnum = nilfs->ns_segnum = segnum[0];
  427. failed:
  428. /* No need to recover sufile because it will be destroyed on error */
  429. return err;
  430. }
  431. static int nilfs_recovery_copy_block(struct the_nilfs *nilfs,
  432. struct nilfs_recovery_block *rb,
  433. struct page *page)
  434. {
  435. struct buffer_head *bh_org;
  436. void *kaddr;
  437. bh_org = __bread(nilfs->ns_bdev, rb->blocknr, nilfs->ns_blocksize);
  438. if (unlikely(!bh_org))
  439. return -EIO;
  440. kaddr = kmap_atomic(page, KM_USER0);
  441. memcpy(kaddr + bh_offset(bh_org), bh_org->b_data, bh_org->b_size);
  442. kunmap_atomic(kaddr, KM_USER0);
  443. brelse(bh_org);
  444. return 0;
  445. }
  446. static int nilfs_recover_dsync_blocks(struct the_nilfs *nilfs,
  447. struct super_block *sb,
  448. struct nilfs_root *root,
  449. struct list_head *head,
  450. unsigned long *nr_salvaged_blocks)
  451. {
  452. struct inode *inode;
  453. struct nilfs_recovery_block *rb, *n;
  454. unsigned blocksize = nilfs->ns_blocksize;
  455. struct page *page;
  456. loff_t pos;
  457. int err = 0, err2 = 0;
  458. list_for_each_entry_safe(rb, n, head, list) {
  459. inode = nilfs_iget(sb, root, rb->ino);
  460. if (IS_ERR(inode)) {
  461. err = PTR_ERR(inode);
  462. inode = NULL;
  463. goto failed_inode;
  464. }
  465. pos = rb->blkoff << inode->i_blkbits;
  466. err = block_write_begin(inode->i_mapping, pos, blocksize,
  467. 0, &page, nilfs_get_block);
  468. if (unlikely(err)) {
  469. loff_t isize = inode->i_size;
  470. if (pos + blocksize > isize)
  471. vmtruncate(inode, isize);
  472. goto failed_inode;
  473. }
  474. err = nilfs_recovery_copy_block(nilfs, rb, page);
  475. if (unlikely(err))
  476. goto failed_page;
  477. err = nilfs_set_file_dirty(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. * @sb: super block instance
  509. * @ri: pointer to a nilfs_recovery_info
  510. */
  511. static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
  512. struct super_block *sb,
  513. struct nilfs_root *root,
  514. struct nilfs_recovery_info *ri)
  515. {
  516. struct buffer_head *bh_sum = NULL;
  517. struct nilfs_segment_summary *sum;
  518. sector_t pseg_start;
  519. sector_t seg_start, seg_end; /* Starting/ending DBN of full segment */
  520. unsigned long nsalvaged_blocks = 0;
  521. unsigned int flags;
  522. u64 seg_seq;
  523. __u64 segnum, nextnum = 0;
  524. int empty_seg = 0;
  525. int err = 0, ret;
  526. LIST_HEAD(dsync_blocks); /* list of data blocks to be recovered */
  527. enum {
  528. RF_INIT_ST,
  529. RF_DSYNC_ST, /* scanning data-sync segments */
  530. };
  531. int state = RF_INIT_ST;
  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, sb, root, &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. sb->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. return err;
  613. confused:
  614. err = -EINVAL;
  615. failed:
  616. printk(KERN_ERR
  617. "NILFS (device %s): Error roll-forwarding "
  618. "(err=%d, pseg block=%llu). ",
  619. sb->s_id, err, (unsigned long long)pseg_start);
  620. goto out;
  621. }
  622. static void nilfs_finish_roll_forward(struct the_nilfs *nilfs,
  623. struct nilfs_recovery_info *ri)
  624. {
  625. struct buffer_head *bh;
  626. int err;
  627. if (nilfs_get_segnum_of_block(nilfs, ri->ri_lsegs_start) !=
  628. nilfs_get_segnum_of_block(nilfs, ri->ri_super_root))
  629. return;
  630. bh = __getblk(nilfs->ns_bdev, ri->ri_lsegs_start, nilfs->ns_blocksize);
  631. BUG_ON(!bh);
  632. memset(bh->b_data, 0, bh->b_size);
  633. set_buffer_dirty(bh);
  634. err = sync_dirty_buffer(bh);
  635. if (unlikely(err))
  636. printk(KERN_WARNING
  637. "NILFS warning: buffer sync write failed during "
  638. "post-cleaning of recovery.\n");
  639. brelse(bh);
  640. }
  641. /**
  642. * nilfs_salvage_orphan_logs - salvage logs written after the latest checkpoint
  643. * @nilfs: nilfs object
  644. * @sb: super block instance
  645. * @ri: pointer to a nilfs_recovery_info struct to store search results.
  646. *
  647. * Return Value: On success, 0 is returned. On error, one of the following
  648. * negative error code is returned.
  649. *
  650. * %-EINVAL - Inconsistent filesystem state.
  651. *
  652. * %-EIO - I/O error
  653. *
  654. * %-ENOSPC - No space left on device (only in a panic state).
  655. *
  656. * %-ERESTARTSYS - Interrupted.
  657. *
  658. * %-ENOMEM - Insufficient memory available.
  659. */
  660. int nilfs_salvage_orphan_logs(struct the_nilfs *nilfs,
  661. struct super_block *sb,
  662. struct nilfs_recovery_info *ri)
  663. {
  664. struct nilfs_root *root;
  665. int err;
  666. if (ri->ri_lsegs_start == 0 || ri->ri_lsegs_end == 0)
  667. return 0;
  668. err = nilfs_attach_checkpoint(sb, ri->ri_cno, true, &root);
  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, sb, root, 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, sb, 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_log_writer(sb, root);
  685. if (unlikely(err))
  686. goto failed;
  687. set_nilfs_discontinued(nilfs);
  688. err = nilfs_construct_segment(sb);
  689. nilfs_detach_log_writer(sb);
  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_put_root(root);
  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. * %-ENOMEM - Insufficient memory available.
  718. */
  719. int nilfs_search_super_root(struct the_nilfs *nilfs,
  720. struct nilfs_recovery_info *ri)
  721. {
  722. struct buffer_head *bh_sum = NULL;
  723. struct nilfs_segment_summary *sum;
  724. sector_t pseg_start, pseg_end, sr_pseg_start = 0;
  725. sector_t seg_start, seg_end; /* range of full segment (block number) */
  726. sector_t b, end;
  727. unsigned long nblocks;
  728. unsigned int flags;
  729. u64 seg_seq;
  730. __u64 segnum, nextnum = 0;
  731. __u64 cno;
  732. LIST_HEAD(segments);
  733. int empty_seg = 0, scan_newer = 0;
  734. int ret;
  735. pseg_start = nilfs->ns_last_pseg;
  736. seg_seq = nilfs->ns_last_seq;
  737. cno = nilfs->ns_last_cno;
  738. segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
  739. /* Calculate range of segment */
  740. nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
  741. /* Read ahead segment */
  742. b = seg_start;
  743. while (b <= seg_end)
  744. __breadahead(nilfs->ns_bdev, b++, nilfs->ns_blocksize);
  745. for (;;) {
  746. brelse(bh_sum);
  747. ret = NILFS_SEG_FAIL_IO;
  748. bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum);
  749. if (!bh_sum)
  750. goto failed;
  751. ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum);
  752. if (ret) {
  753. if (ret == NILFS_SEG_FAIL_IO)
  754. goto failed;
  755. goto strayed;
  756. }
  757. nblocks = le32_to_cpu(sum->ss_nblocks);
  758. pseg_end = pseg_start + nblocks - 1;
  759. if (unlikely(pseg_end > seg_end)) {
  760. ret = NILFS_SEG_FAIL_CONSISTENCY;
  761. goto strayed;
  762. }
  763. /* A valid partial segment */
  764. ri->ri_pseg_start = pseg_start;
  765. ri->ri_seq = seg_seq;
  766. ri->ri_segnum = segnum;
  767. nextnum = nilfs_get_segnum_of_block(nilfs,
  768. le64_to_cpu(sum->ss_next));
  769. ri->ri_nextnum = nextnum;
  770. empty_seg = 0;
  771. flags = le16_to_cpu(sum->ss_flags);
  772. if (!(flags & NILFS_SS_SR) && !scan_newer) {
  773. /* This will never happen because a superblock
  774. (last_segment) always points to a pseg
  775. having a super root. */
  776. ret = NILFS_SEG_FAIL_CONSISTENCY;
  777. goto failed;
  778. }
  779. if (pseg_start == seg_start) {
  780. nilfs_get_segment_range(nilfs, nextnum, &b, &end);
  781. while (b <= end)
  782. __breadahead(nilfs->ns_bdev, b++,
  783. nilfs->ns_blocksize);
  784. }
  785. if (!(flags & NILFS_SS_SR)) {
  786. if (!ri->ri_lsegs_start && (flags & NILFS_SS_LOGBGN)) {
  787. ri->ri_lsegs_start = pseg_start;
  788. ri->ri_lsegs_start_seq = seg_seq;
  789. }
  790. if (flags & NILFS_SS_LOGEND)
  791. ri->ri_lsegs_end = pseg_start;
  792. goto try_next_pseg;
  793. }
  794. /* A valid super root was found. */
  795. ri->ri_cno = cno++;
  796. ri->ri_super_root = pseg_end;
  797. ri->ri_lsegs_start = ri->ri_lsegs_end = 0;
  798. nilfs_dispose_segment_list(&segments);
  799. sr_pseg_start = pseg_start;
  800. nilfs->ns_pseg_offset = pseg_start + nblocks - seg_start;
  801. nilfs->ns_seg_seq = seg_seq;
  802. nilfs->ns_segnum = segnum;
  803. nilfs->ns_cno = cno; /* nilfs->ns_cno = ri->ri_cno + 1 */
  804. nilfs->ns_ctime = le64_to_cpu(sum->ss_create);
  805. nilfs->ns_nextnum = nextnum;
  806. if (scan_newer)
  807. ri->ri_need_recovery = NILFS_RECOVERY_SR_UPDATED;
  808. else {
  809. if (nilfs->ns_mount_state & NILFS_VALID_FS)
  810. goto super_root_found;
  811. scan_newer = 1;
  812. }
  813. try_next_pseg:
  814. /* Standing on a course, or met an inconsistent state */
  815. pseg_start += nblocks;
  816. if (pseg_start < seg_end)
  817. continue;
  818. goto feed_segment;
  819. strayed:
  820. /* Off the trail */
  821. if (!scan_newer)
  822. /*
  823. * This can happen if a checkpoint was written without
  824. * barriers, or as a result of an I/O failure.
  825. */
  826. goto failed;
  827. feed_segment:
  828. /* Looking to the next full segment */
  829. if (empty_seg++)
  830. goto super_root_found; /* found a valid super root */
  831. ret = nilfs_segment_list_add(&segments, segnum);
  832. if (unlikely(ret))
  833. goto failed;
  834. seg_seq++;
  835. segnum = nextnum;
  836. nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
  837. pseg_start = seg_start;
  838. }
  839. super_root_found:
  840. /* Updating pointers relating to the latest checkpoint */
  841. brelse(bh_sum);
  842. list_splice_tail(&segments, &ri->ri_used_segments);
  843. nilfs->ns_last_pseg = sr_pseg_start;
  844. nilfs->ns_last_seq = nilfs->ns_seg_seq;
  845. nilfs->ns_last_cno = ri->ri_cno;
  846. return 0;
  847. failed:
  848. brelse(bh_sum);
  849. nilfs_dispose_segment_list(&segments);
  850. return (ret < 0) ? ret : nilfs_warn_segment_error(ret);
  851. }