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);
  441. memcpy(kaddr + bh_offset(bh_org), bh_org->b_data, bh_org->b_size);
  442. kunmap_atomic(kaddr);
  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. nilfs_write_failed(inode->i_mapping,
  472. pos + blocksize);
  473. goto failed_inode;
  474. }
  475. err = nilfs_recovery_copy_block(nilfs, rb, page);
  476. if (unlikely(err))
  477. goto failed_page;
  478. err = nilfs_set_file_dirty(inode, 1);
  479. if (unlikely(err))
  480. goto failed_page;
  481. block_write_end(NULL, inode->i_mapping, pos, blocksize,
  482. blocksize, page, NULL);
  483. unlock_page(page);
  484. page_cache_release(page);
  485. (*nr_salvaged_blocks)++;
  486. goto next;
  487. failed_page:
  488. unlock_page(page);
  489. page_cache_release(page);
  490. failed_inode:
  491. printk(KERN_WARNING
  492. "NILFS warning: error recovering data block "
  493. "(err=%d, ino=%lu, block-offset=%llu)\n",
  494. err, (unsigned long)rb->ino,
  495. (unsigned long long)rb->blkoff);
  496. if (!err2)
  497. err2 = err;
  498. next:
  499. iput(inode); /* iput(NULL) is just ignored */
  500. list_del_init(&rb->list);
  501. kfree(rb);
  502. }
  503. return err2;
  504. }
  505. /**
  506. * nilfs_do_roll_forward - salvage logical segments newer than the latest
  507. * checkpoint
  508. * @nilfs: nilfs object
  509. * @sb: super block instance
  510. * @ri: pointer to a nilfs_recovery_info
  511. */
  512. static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
  513. struct super_block *sb,
  514. struct nilfs_root *root,
  515. struct nilfs_recovery_info *ri)
  516. {
  517. struct buffer_head *bh_sum = NULL;
  518. struct nilfs_segment_summary *sum = NULL;
  519. sector_t pseg_start;
  520. sector_t seg_start, seg_end; /* Starting/ending DBN of full segment */
  521. unsigned long nsalvaged_blocks = 0;
  522. unsigned int flags;
  523. u64 seg_seq;
  524. __u64 segnum, nextnum = 0;
  525. int empty_seg = 0;
  526. int err = 0, ret;
  527. LIST_HEAD(dsync_blocks); /* list of data blocks to be recovered */
  528. enum {
  529. RF_INIT_ST,
  530. RF_DSYNC_ST, /* scanning data-sync segments */
  531. };
  532. int state = RF_INIT_ST;
  533. pseg_start = ri->ri_lsegs_start;
  534. seg_seq = ri->ri_lsegs_start_seq;
  535. segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
  536. nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
  537. while (segnum != ri->ri_segnum || pseg_start <= ri->ri_pseg_start) {
  538. brelse(bh_sum);
  539. bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum);
  540. if (!bh_sum) {
  541. err = -EIO;
  542. goto failed;
  543. }
  544. ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum);
  545. if (ret) {
  546. if (ret == NILFS_SEG_FAIL_IO) {
  547. err = -EIO;
  548. goto failed;
  549. }
  550. goto strayed;
  551. }
  552. flags = le16_to_cpu(sum->ss_flags);
  553. if (flags & NILFS_SS_SR)
  554. goto confused;
  555. /* Found a valid partial segment; do recovery actions */
  556. nextnum = nilfs_get_segnum_of_block(nilfs,
  557. le64_to_cpu(sum->ss_next));
  558. empty_seg = 0;
  559. nilfs->ns_ctime = le64_to_cpu(sum->ss_create);
  560. if (!(flags & NILFS_SS_GC))
  561. nilfs->ns_nongc_ctime = nilfs->ns_ctime;
  562. switch (state) {
  563. case RF_INIT_ST:
  564. if (!(flags & NILFS_SS_LOGBGN) ||
  565. !(flags & NILFS_SS_SYNDT))
  566. goto try_next_pseg;
  567. state = RF_DSYNC_ST;
  568. /* Fall through */
  569. case RF_DSYNC_ST:
  570. if (!(flags & NILFS_SS_SYNDT))
  571. goto confused;
  572. err = nilfs_scan_dsync_log(nilfs, pseg_start, sum,
  573. &dsync_blocks);
  574. if (unlikely(err))
  575. goto failed;
  576. if (flags & NILFS_SS_LOGEND) {
  577. err = nilfs_recover_dsync_blocks(
  578. nilfs, sb, root, &dsync_blocks,
  579. &nsalvaged_blocks);
  580. if (unlikely(err))
  581. goto failed;
  582. state = RF_INIT_ST;
  583. }
  584. break; /* Fall through to try_next_pseg */
  585. }
  586. try_next_pseg:
  587. if (pseg_start == ri->ri_lsegs_end)
  588. break;
  589. pseg_start += le32_to_cpu(sum->ss_nblocks);
  590. if (pseg_start < seg_end)
  591. continue;
  592. goto feed_segment;
  593. strayed:
  594. if (pseg_start == ri->ri_lsegs_end)
  595. break;
  596. feed_segment:
  597. /* Looking to the next full segment */
  598. if (empty_seg++)
  599. break;
  600. seg_seq++;
  601. segnum = nextnum;
  602. nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
  603. pseg_start = seg_start;
  604. }
  605. if (nsalvaged_blocks) {
  606. printk(KERN_INFO "NILFS (device %s): salvaged %lu blocks\n",
  607. sb->s_id, nsalvaged_blocks);
  608. ri->ri_need_recovery = NILFS_RECOVERY_ROLLFORWARD_DONE;
  609. }
  610. out:
  611. brelse(bh_sum);
  612. dispose_recovery_list(&dsync_blocks);
  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. sb->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. * @sb: super block instance
  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 super_block *sb,
  663. struct nilfs_recovery_info *ri)
  664. {
  665. struct nilfs_root *root;
  666. int err;
  667. if (ri->ri_lsegs_start == 0 || ri->ri_lsegs_end == 0)
  668. return 0;
  669. err = nilfs_attach_checkpoint(sb, ri->ri_cno, true, &root);
  670. if (unlikely(err)) {
  671. printk(KERN_ERR
  672. "NILFS: error loading the latest checkpoint.\n");
  673. return err;
  674. }
  675. err = nilfs_do_roll_forward(nilfs, sb, root, ri);
  676. if (unlikely(err))
  677. goto failed;
  678. if (ri->ri_need_recovery == NILFS_RECOVERY_ROLLFORWARD_DONE) {
  679. err = nilfs_prepare_segment_for_recovery(nilfs, sb, ri);
  680. if (unlikely(err)) {
  681. printk(KERN_ERR "NILFS: Error preparing segments for "
  682. "recovery.\n");
  683. goto failed;
  684. }
  685. err = nilfs_attach_log_writer(sb, root);
  686. if (unlikely(err))
  687. goto failed;
  688. set_nilfs_discontinued(nilfs);
  689. err = nilfs_construct_segment(sb);
  690. nilfs_detach_log_writer(sb);
  691. if (unlikely(err)) {
  692. printk(KERN_ERR "NILFS: Oops! recovery failed. "
  693. "(err=%d)\n", err);
  694. goto failed;
  695. }
  696. nilfs_finish_roll_forward(nilfs, ri);
  697. }
  698. failed:
  699. nilfs_put_root(root);
  700. return err;
  701. }
  702. /**
  703. * nilfs_search_super_root - search the latest valid super root
  704. * @nilfs: the_nilfs
  705. * @ri: pointer to a nilfs_recovery_info struct to store search results.
  706. *
  707. * nilfs_search_super_root() looks for the latest super-root from a partial
  708. * segment pointed by the superblock. It sets up struct the_nilfs through
  709. * this search. It fills nilfs_recovery_info (ri) required for recovery.
  710. *
  711. * Return Value: On success, 0 is returned. On error, one of the following
  712. * negative error code is returned.
  713. *
  714. * %-EINVAL - No valid segment found
  715. *
  716. * %-EIO - I/O error
  717. *
  718. * %-ENOMEM - Insufficient memory available.
  719. */
  720. int nilfs_search_super_root(struct the_nilfs *nilfs,
  721. struct nilfs_recovery_info *ri)
  722. {
  723. struct buffer_head *bh_sum = NULL;
  724. struct nilfs_segment_summary *sum = NULL;
  725. sector_t pseg_start, pseg_end, sr_pseg_start = 0;
  726. sector_t seg_start, seg_end; /* range of full segment (block number) */
  727. sector_t b, end;
  728. unsigned long nblocks;
  729. unsigned int flags;
  730. u64 seg_seq;
  731. __u64 segnum, nextnum = 0;
  732. __u64 cno;
  733. LIST_HEAD(segments);
  734. int empty_seg = 0, scan_newer = 0;
  735. int ret;
  736. pseg_start = nilfs->ns_last_pseg;
  737. seg_seq = nilfs->ns_last_seq;
  738. cno = nilfs->ns_last_cno;
  739. segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
  740. /* Calculate range of segment */
  741. nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
  742. /* Read ahead segment */
  743. b = seg_start;
  744. while (b <= seg_end)
  745. __breadahead(nilfs->ns_bdev, b++, nilfs->ns_blocksize);
  746. for (;;) {
  747. brelse(bh_sum);
  748. ret = NILFS_SEG_FAIL_IO;
  749. bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum);
  750. if (!bh_sum)
  751. goto failed;
  752. ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum);
  753. if (ret) {
  754. if (ret == NILFS_SEG_FAIL_IO)
  755. goto failed;
  756. goto strayed;
  757. }
  758. nblocks = le32_to_cpu(sum->ss_nblocks);
  759. pseg_end = pseg_start + nblocks - 1;
  760. if (unlikely(pseg_end > seg_end)) {
  761. ret = NILFS_SEG_FAIL_CONSISTENCY;
  762. goto strayed;
  763. }
  764. /* A valid partial segment */
  765. ri->ri_pseg_start = pseg_start;
  766. ri->ri_seq = seg_seq;
  767. ri->ri_segnum = segnum;
  768. nextnum = nilfs_get_segnum_of_block(nilfs,
  769. le64_to_cpu(sum->ss_next));
  770. ri->ri_nextnum = nextnum;
  771. empty_seg = 0;
  772. flags = le16_to_cpu(sum->ss_flags);
  773. if (!(flags & NILFS_SS_SR) && !scan_newer) {
  774. /* This will never happen because a superblock
  775. (last_segment) always points to a pseg
  776. having a super root. */
  777. ret = NILFS_SEG_FAIL_CONSISTENCY;
  778. goto failed;
  779. }
  780. if (pseg_start == seg_start) {
  781. nilfs_get_segment_range(nilfs, nextnum, &b, &end);
  782. while (b <= end)
  783. __breadahead(nilfs->ns_bdev, b++,
  784. nilfs->ns_blocksize);
  785. }
  786. if (!(flags & NILFS_SS_SR)) {
  787. if (!ri->ri_lsegs_start && (flags & NILFS_SS_LOGBGN)) {
  788. ri->ri_lsegs_start = pseg_start;
  789. ri->ri_lsegs_start_seq = seg_seq;
  790. }
  791. if (flags & NILFS_SS_LOGEND)
  792. ri->ri_lsegs_end = pseg_start;
  793. goto try_next_pseg;
  794. }
  795. /* A valid super root was found. */
  796. ri->ri_cno = cno++;
  797. ri->ri_super_root = pseg_end;
  798. ri->ri_lsegs_start = ri->ri_lsegs_end = 0;
  799. nilfs_dispose_segment_list(&segments);
  800. sr_pseg_start = pseg_start;
  801. nilfs->ns_pseg_offset = pseg_start + nblocks - seg_start;
  802. nilfs->ns_seg_seq = seg_seq;
  803. nilfs->ns_segnum = segnum;
  804. nilfs->ns_cno = cno; /* nilfs->ns_cno = ri->ri_cno + 1 */
  805. nilfs->ns_ctime = le64_to_cpu(sum->ss_create);
  806. nilfs->ns_nextnum = nextnum;
  807. if (scan_newer)
  808. ri->ri_need_recovery = NILFS_RECOVERY_SR_UPDATED;
  809. else {
  810. if (nilfs->ns_mount_state & NILFS_VALID_FS)
  811. goto super_root_found;
  812. scan_newer = 1;
  813. }
  814. try_next_pseg:
  815. /* Standing on a course, or met an inconsistent state */
  816. pseg_start += nblocks;
  817. if (pseg_start < seg_end)
  818. continue;
  819. goto feed_segment;
  820. strayed:
  821. /* Off the trail */
  822. if (!scan_newer)
  823. /*
  824. * This can happen if a checkpoint was written without
  825. * barriers, or as a result of an I/O failure.
  826. */
  827. goto failed;
  828. feed_segment:
  829. /* Looking to the next full segment */
  830. if (empty_seg++)
  831. goto super_root_found; /* found a valid super root */
  832. ret = nilfs_segment_list_add(&segments, segnum);
  833. if (unlikely(ret))
  834. goto failed;
  835. seg_seq++;
  836. segnum = nextnum;
  837. nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
  838. pseg_start = seg_start;
  839. }
  840. super_root_found:
  841. /* Updating pointers relating to the latest checkpoint */
  842. brelse(bh_sum);
  843. list_splice_tail(&segments, &ri->ri_used_segments);
  844. nilfs->ns_last_pseg = sr_pseg_start;
  845. nilfs->ns_last_seq = nilfs->ns_seg_seq;
  846. nilfs->ns_last_cno = ri->ri_cno;
  847. return 0;
  848. failed:
  849. brelse(bh_sum);
  850. nilfs_dispose_segment_list(&segments);
  851. return (ret < 0) ? ret : nilfs_warn_segment_error(ret);
  852. }