blob: 9b4f205d11736dc4d109828260055c2af34c0e9d [file] [log] [blame]
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -07001/*
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
23#include <linux/buffer_head.h>
24#include <linux/blkdev.h>
25#include <linux/swap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -070027#include <linux/crc32.h>
28#include "nilfs.h"
29#include "segment.h"
30#include "sufile.h"
31#include "page.h"
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -070032#include "segbuf.h"
33
34/*
35 * Segment check result
36 */
37enum {
38 NILFS_SEG_VALID,
39 NILFS_SEG_NO_SUPER_ROOT,
40 NILFS_SEG_FAIL_IO,
41 NILFS_SEG_FAIL_MAGIC,
42 NILFS_SEG_FAIL_SEQ,
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -070043 NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT,
44 NILFS_SEG_FAIL_CHECKSUM_FULL,
45 NILFS_SEG_FAIL_CONSISTENCY,
46};
47
48/* work structure for recovery */
49struct nilfs_recovery_block {
50 ino_t ino; /* Inode number of the file that this block
51 belongs to */
52 sector_t blocknr; /* block number */
53 __u64 vblocknr; /* virtual block number */
54 unsigned long blkoff; /* File offset of the data block (per block) */
55 struct list_head list;
56};
57
58
59static int nilfs_warn_segment_error(int err)
60{
61 switch (err) {
62 case NILFS_SEG_FAIL_IO:
63 printk(KERN_WARNING
64 "NILFS warning: I/O error on loading last segment\n");
65 return -EIO;
66 case NILFS_SEG_FAIL_MAGIC:
67 printk(KERN_WARNING
68 "NILFS warning: Segment magic number invalid\n");
69 break;
70 case NILFS_SEG_FAIL_SEQ:
71 printk(KERN_WARNING
72 "NILFS warning: Sequence number mismatch\n");
73 break;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -070074 case NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT:
75 printk(KERN_WARNING
76 "NILFS warning: Checksum error in super root\n");
77 break;
78 case NILFS_SEG_FAIL_CHECKSUM_FULL:
79 printk(KERN_WARNING
80 "NILFS warning: Checksum error in segment payload\n");
81 break;
82 case NILFS_SEG_FAIL_CONSISTENCY:
83 printk(KERN_WARNING
84 "NILFS warning: Inconsistent segment\n");
85 break;
86 case NILFS_SEG_NO_SUPER_ROOT:
87 printk(KERN_WARNING
88 "NILFS warning: No super root in the last segment\n");
89 break;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -070090 }
91 return -EINVAL;
92}
93
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -070094/**
Ryusuke Konishi8b940252010-05-23 01:39:02 +090095 * nilfs_compute_checksum - compute checksum of blocks continuously
96 * @nilfs: nilfs object
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -070097 * @bhs: buffer head of start block
98 * @sum: place to store result
99 * @offset: offset bytes in the first block
100 * @check_bytes: number of bytes to be checked
101 * @start: DBN of start block
102 * @nblock: number of blocks to be checked
103 */
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900104static int nilfs_compute_checksum(struct the_nilfs *nilfs,
105 struct buffer_head *bhs, u32 *sum,
106 unsigned long offset, u64 check_bytes,
107 sector_t start, unsigned long nblock)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700108{
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900109 unsigned int blocksize = nilfs->ns_blocksize;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700110 unsigned long size;
111 u32 crc;
112
113 BUG_ON(offset >= blocksize);
114 check_bytes -= offset;
115 size = min_t(u64, check_bytes, blocksize - offset);
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900116 crc = crc32_le(nilfs->ns_crc_seed,
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700117 (unsigned char *)bhs->b_data + offset, size);
118 if (--nblock > 0) {
119 do {
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900120 struct buffer_head *bh;
121
122 bh = __bread(nilfs->ns_bdev, ++start, blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700123 if (!bh)
124 return -EIO;
125 check_bytes -= size;
126 size = min_t(u64, check_bytes, blocksize);
127 crc = crc32_le(crc, bh->b_data, size);
128 brelse(bh);
129 } while (--nblock > 0);
130 }
131 *sum = crc;
132 return 0;
133}
134
135/**
136 * nilfs_read_super_root_block - read super root block
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900137 * @nilfs: nilfs object
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700138 * @sr_block: disk block number of the super root block
139 * @pbh: address of a buffer_head pointer to return super root buffer
140 * @check: CRC check flag
141 */
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900142int nilfs_read_super_root_block(struct the_nilfs *nilfs, sector_t sr_block,
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700143 struct buffer_head **pbh, int check)
144{
145 struct buffer_head *bh_sr;
146 struct nilfs_super_root *sr;
147 u32 crc;
148 int ret;
149
150 *pbh = NULL;
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900151 bh_sr = __bread(nilfs->ns_bdev, sr_block, nilfs->ns_blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700152 if (unlikely(!bh_sr)) {
153 ret = NILFS_SEG_FAIL_IO;
154 goto failed;
155 }
156
157 sr = (struct nilfs_super_root *)bh_sr->b_data;
158 if (check) {
159 unsigned bytes = le16_to_cpu(sr->sr_bytes);
160
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900161 if (bytes == 0 || bytes > nilfs->ns_blocksize) {
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700162 ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT;
163 goto failed_bh;
164 }
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900165 if (nilfs_compute_checksum(
166 nilfs, bh_sr, &crc, sizeof(sr->sr_sum), bytes,
167 sr_block, 1)) {
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700168 ret = NILFS_SEG_FAIL_IO;
169 goto failed_bh;
170 }
171 if (crc != le32_to_cpu(sr->sr_sum)) {
172 ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT;
173 goto failed_bh;
174 }
175 }
176 *pbh = bh_sr;
177 return 0;
178
179 failed_bh:
180 brelse(bh_sr);
181
182 failed:
183 return nilfs_warn_segment_error(ret);
184}
185
186/**
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900187 * nilfs_read_log_header - read summary header of the specified log
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900188 * @nilfs: nilfs object
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900189 * @start_blocknr: start block number of the log
190 * @sum: pointer to return segment summary structure
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700191 */
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900192static struct buffer_head *
193nilfs_read_log_header(struct the_nilfs *nilfs, sector_t start_blocknr,
194 struct nilfs_segment_summary **sum)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700195{
196 struct buffer_head *bh_sum;
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900197
198 bh_sum = __bread(nilfs->ns_bdev, start_blocknr, nilfs->ns_blocksize);
199 if (bh_sum)
200 *sum = (struct nilfs_segment_summary *)bh_sum->b_data;
201 return bh_sum;
202}
203
204/**
205 * nilfs_validate_log - verify consistency of log
206 * @nilfs: nilfs object
207 * @seg_seq: sequence number of segment
208 * @bh_sum: buffer head of summary block
209 * @sum: segment summary struct
210 */
211static int nilfs_validate_log(struct the_nilfs *nilfs, u64 seg_seq,
212 struct buffer_head *bh_sum,
213 struct nilfs_segment_summary *sum)
214{
Jiro SEKIBA03f29362010-02-18 19:11:35 +0900215 unsigned long nblock;
216 u32 crc;
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900217 int ret;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700218
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900219 ret = NILFS_SEG_FAIL_MAGIC;
220 if (le32_to_cpu(sum->ss_magic) != NILFS_SEGSUM_MAGIC)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700221 goto out;
222
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900223 ret = NILFS_SEG_FAIL_SEQ;
224 if (le64_to_cpu(sum->ss_seq) != seg_seq)
225 goto out;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700226
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900227 nblock = le32_to_cpu(sum->ss_nblocks);
228 ret = NILFS_SEG_FAIL_CONSISTENCY;
229 if (unlikely(nblock == 0 || nblock > nilfs->ns_blocks_per_segment))
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700230 /* This limits the number of blocks read in the CRC check */
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900231 goto out;
232
233 ret = NILFS_SEG_FAIL_IO;
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900234 if (nilfs_compute_checksum(nilfs, bh_sum, &crc, sizeof(sum->ss_datasum),
235 ((u64)nblock << nilfs->ns_blocksize_bits),
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900236 bh_sum->b_blocknr, nblock))
237 goto out;
238
239 ret = NILFS_SEG_FAIL_CHECKSUM_FULL;
240 if (crc != le32_to_cpu(sum->ss_datasum))
241 goto out;
242 ret = 0;
243out:
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700244 return ret;
245}
246
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900247/**
248 * nilfs_read_summary_info - read an item on summary blocks of a log
249 * @nilfs: nilfs object
250 * @pbh: the current buffer head on summary blocks [in, out]
251 * @offset: the current byte offset on summary blocks [in, out]
252 * @bytes: byte size of the item to be read
253 */
254static void *nilfs_read_summary_info(struct the_nilfs *nilfs,
255 struct buffer_head **pbh,
256 unsigned int *offset, unsigned int bytes)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700257{
258 void *ptr;
259 sector_t blocknr;
260
261 BUG_ON((*pbh)->b_size < *offset);
262 if (bytes > (*pbh)->b_size - *offset) {
263 blocknr = (*pbh)->b_blocknr;
264 brelse(*pbh);
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900265 *pbh = __bread(nilfs->ns_bdev, blocknr + 1,
266 nilfs->ns_blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700267 if (unlikely(!*pbh))
268 return NULL;
269 *offset = 0;
270 }
271 ptr = (*pbh)->b_data + *offset;
272 *offset += bytes;
273 return ptr;
274}
275
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900276/**
277 * nilfs_skip_summary_info - skip items on summary blocks of a log
278 * @nilfs: nilfs object
279 * @pbh: the current buffer head on summary blocks [in, out]
280 * @offset: the current byte offset on summary blocks [in, out]
281 * @bytes: byte size of the item to be skipped
282 * @count: number of items to be skipped
283 */
284static void nilfs_skip_summary_info(struct the_nilfs *nilfs,
285 struct buffer_head **pbh,
286 unsigned int *offset, unsigned int bytes,
287 unsigned long count)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700288{
289 unsigned int rest_item_in_current_block
290 = ((*pbh)->b_size - *offset) / bytes;
291
292 if (count <= rest_item_in_current_block) {
293 *offset += bytes * count;
294 } else {
295 sector_t blocknr = (*pbh)->b_blocknr;
296 unsigned int nitem_per_block = (*pbh)->b_size / bytes;
297 unsigned int bcnt;
298
299 count -= rest_item_in_current_block;
300 bcnt = DIV_ROUND_UP(count, nitem_per_block);
301 *offset = bytes * (count - (bcnt - 1) * nitem_per_block);
302
303 brelse(*pbh);
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900304 *pbh = __bread(nilfs->ns_bdev, blocknr + bcnt,
305 nilfs->ns_blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700306 }
307}
308
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900309/**
310 * nilfs_scan_dsync_log - get block information of a log written for data sync
311 * @nilfs: nilfs object
312 * @start_blocknr: start block number of the log
Ryusuke Konishi85655482010-05-23 19:46:44 +0900313 * @sum: log summary information
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900314 * @head: list head to add nilfs_recovery_block struct
315 */
316static int nilfs_scan_dsync_log(struct the_nilfs *nilfs, sector_t start_blocknr,
Ryusuke Konishi85655482010-05-23 19:46:44 +0900317 struct nilfs_segment_summary *sum,
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900318 struct list_head *head)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700319{
320 struct buffer_head *bh;
321 unsigned int offset;
Ryusuke Konishi85655482010-05-23 19:46:44 +0900322 u32 nfinfo, sumbytes;
323 sector_t blocknr;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700324 ino_t ino;
325 int err = -EIO;
326
Ryusuke Konishi85655482010-05-23 19:46:44 +0900327 nfinfo = le32_to_cpu(sum->ss_nfinfo);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700328 if (!nfinfo)
329 return 0;
330
Ryusuke Konishi85655482010-05-23 19:46:44 +0900331 sumbytes = le32_to_cpu(sum->ss_sumbytes);
332 blocknr = start_blocknr + DIV_ROUND_UP(sumbytes, nilfs->ns_blocksize);
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900333 bh = __bread(nilfs->ns_bdev, start_blocknr, nilfs->ns_blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700334 if (unlikely(!bh))
335 goto out;
336
Ryusuke Konishi85655482010-05-23 19:46:44 +0900337 offset = le16_to_cpu(sum->ss_bytes);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700338 for (;;) {
339 unsigned long nblocks, ndatablk, nnodeblk;
340 struct nilfs_finfo *finfo;
341
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900342 finfo = nilfs_read_summary_info(nilfs, &bh, &offset,
343 sizeof(*finfo));
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700344 if (unlikely(!finfo))
345 goto out;
346
347 ino = le64_to_cpu(finfo->fi_ino);
348 nblocks = le32_to_cpu(finfo->fi_nblocks);
349 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
350 nnodeblk = nblocks - ndatablk;
351
352 while (ndatablk-- > 0) {
353 struct nilfs_recovery_block *rb;
354 struct nilfs_binfo_v *binfo;
355
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900356 binfo = nilfs_read_summary_info(nilfs, &bh, &offset,
357 sizeof(*binfo));
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700358 if (unlikely(!binfo))
359 goto out;
360
361 rb = kmalloc(sizeof(*rb), GFP_NOFS);
362 if (unlikely(!rb)) {
363 err = -ENOMEM;
364 goto out;
365 }
366 rb->ino = ino;
367 rb->blocknr = blocknr++;
368 rb->vblocknr = le64_to_cpu(binfo->bi_vblocknr);
369 rb->blkoff = le64_to_cpu(binfo->bi_blkoff);
370 /* INIT_LIST_HEAD(&rb->list); */
371 list_add_tail(&rb->list, head);
372 }
373 if (--nfinfo == 0)
374 break;
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900375 blocknr += nnodeblk; /* always 0 for data sync logs */
376 nilfs_skip_summary_info(nilfs, &bh, &offset, sizeof(__le64),
377 nnodeblk);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700378 if (unlikely(!bh))
379 goto out;
380 }
381 err = 0;
382 out:
383 brelse(bh); /* brelse(NULL) is just ignored */
384 return err;
385}
386
387static void dispose_recovery_list(struct list_head *head)
388{
389 while (!list_empty(head)) {
Ryusuke Konishi0cc1283882011-05-05 12:56:51 +0900390 struct nilfs_recovery_block *rb;
391
392 rb = list_first_entry(head, struct nilfs_recovery_block, list);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700393 list_del(&rb->list);
394 kfree(rb);
395 }
396}
397
Ryusuke Konishi654137d2009-05-17 19:07:21 +0900398struct nilfs_segment_entry {
399 struct list_head list;
400 __u64 segnum;
401};
402
403static int nilfs_segment_list_add(struct list_head *head, __u64 segnum)
404{
405 struct nilfs_segment_entry *ent = kmalloc(sizeof(*ent), GFP_NOFS);
406
407 if (unlikely(!ent))
408 return -ENOMEM;
409
410 ent->segnum = segnum;
411 INIT_LIST_HEAD(&ent->list);
412 list_add_tail(&ent->list, head);
413 return 0;
414}
415
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700416void nilfs_dispose_segment_list(struct list_head *head)
417{
418 while (!list_empty(head)) {
Ryusuke Konishi0cc1283882011-05-05 12:56:51 +0900419 struct nilfs_segment_entry *ent;
420
421 ent = list_first_entry(head, struct nilfs_segment_entry, list);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700422 list_del(&ent->list);
Ryusuke Konishi654137d2009-05-17 19:07:21 +0900423 kfree(ent);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700424 }
425}
426
427static int nilfs_prepare_segment_for_recovery(struct the_nilfs *nilfs,
Ryusuke Konishif7545142011-03-09 11:05:08 +0900428 struct super_block *sb,
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700429 struct nilfs_recovery_info *ri)
430{
431 struct list_head *head = &ri->ri_used_segments;
432 struct nilfs_segment_entry *ent, *n;
433 struct inode *sufile = nilfs->ns_sufile;
434 __u64 segnum[4];
435 int err;
436 int i;
437
438 segnum[0] = nilfs->ns_segnum;
439 segnum[1] = nilfs->ns_nextnum;
440 segnum[2] = ri->ri_segnum;
441 segnum[3] = ri->ri_nextnum;
442
443 /*
444 * Releasing the next segment of the latest super root.
445 * The next segment is invalidated by this recovery.
446 */
447 err = nilfs_sufile_free(sufile, segnum[1]);
448 if (unlikely(err))
449 goto failed;
450
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700451 for (i = 1; i < 4; i++) {
Ryusuke Konishi654137d2009-05-17 19:07:21 +0900452 err = nilfs_segment_list_add(head, segnum[i]);
453 if (unlikely(err))
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700454 goto failed;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700455 }
456
457 /*
458 * Collecting segments written after the latest super root.
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700459 * These are marked dirty to avoid being reallocated in the next write.
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700460 */
461 list_for_each_entry_safe(ent, n, head, list) {
Ryusuke Konishic85399c2009-04-05 18:30:58 +0900462 if (ent->segnum != segnum[0]) {
463 err = nilfs_sufile_scrap(sufile, ent->segnum);
464 if (unlikely(err))
465 goto failed;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700466 }
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700467 list_del(&ent->list);
Ryusuke Konishi654137d2009-05-17 19:07:21 +0900468 kfree(ent);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700469 }
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700470
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700471 /* Allocate new segments for recovery */
472 err = nilfs_sufile_alloc(sufile, &segnum[0]);
473 if (unlikely(err))
474 goto failed;
475
476 nilfs->ns_pseg_offset = 0;
477 nilfs->ns_seg_seq = ri->ri_seq + 2;
478 nilfs->ns_nextnum = nilfs->ns_segnum = segnum[0];
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700479
480 failed:
481 /* No need to recover sufile because it will be destroyed on error */
482 return err;
483}
484
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900485static int nilfs_recovery_copy_block(struct the_nilfs *nilfs,
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700486 struct nilfs_recovery_block *rb,
487 struct page *page)
488{
489 struct buffer_head *bh_org;
490 void *kaddr;
491
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900492 bh_org = __bread(nilfs->ns_bdev, rb->blocknr, nilfs->ns_blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700493 if (unlikely(!bh_org))
494 return -EIO;
495
Cong Wang7b9c0972011-11-25 23:14:33 +0800496 kaddr = kmap_atomic(page);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700497 memcpy(kaddr + bh_offset(bh_org), bh_org->b_data, bh_org->b_size);
Cong Wang7b9c0972011-11-25 23:14:33 +0800498 kunmap_atomic(kaddr);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700499 brelse(bh_org);
500 return 0;
501}
502
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900503static int nilfs_recover_dsync_blocks(struct the_nilfs *nilfs,
Ryusuke Konishif7545142011-03-09 11:05:08 +0900504 struct super_block *sb,
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900505 struct nilfs_root *root,
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900506 struct list_head *head,
507 unsigned long *nr_salvaged_blocks)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700508{
509 struct inode *inode;
510 struct nilfs_recovery_block *rb, *n;
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900511 unsigned blocksize = nilfs->ns_blocksize;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700512 struct page *page;
513 loff_t pos;
514 int err = 0, err2 = 0;
515
516 list_for_each_entry_safe(rb, n, head, list) {
Ryusuke Konishif7545142011-03-09 11:05:08 +0900517 inode = nilfs_iget(sb, root, rb->ino);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700518 if (IS_ERR(inode)) {
519 err = PTR_ERR(inode);
520 inode = NULL;
521 goto failed_inode;
522 }
523
524 pos = rb->blkoff << inode->i_blkbits;
Christoph Hellwig155130a2010-06-04 11:29:58 +0200525 err = block_write_begin(inode->i_mapping, pos, blocksize,
526 0, &page, nilfs_get_block);
527 if (unlikely(err)) {
528 loff_t isize = inode->i_size;
529 if (pos + blocksize > isize)
Marco Stornelli2d1b3992012-12-15 11:57:37 +0100530 nilfs_write_failed(inode->i_mapping,
531 pos + blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700532 goto failed_inode;
Christoph Hellwig155130a2010-06-04 11:29:58 +0200533 }
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700534
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900535 err = nilfs_recovery_copy_block(nilfs, rb, page);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700536 if (unlikely(err))
537 goto failed_page;
538
Ryusuke Konishibcbc8c62010-12-27 00:05:49 +0900539 err = nilfs_set_file_dirty(inode, 1);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700540 if (unlikely(err))
541 goto failed_page;
542
543 block_write_end(NULL, inode->i_mapping, pos, blocksize,
544 blocksize, page, NULL);
545
546 unlock_page(page);
547 page_cache_release(page);
548
549 (*nr_salvaged_blocks)++;
550 goto next;
551
552 failed_page:
553 unlock_page(page);
554 page_cache_release(page);
555
556 failed_inode:
557 printk(KERN_WARNING
558 "NILFS warning: error recovering data block "
559 "(err=%d, ino=%lu, block-offset=%llu)\n",
Heiko Carstensb5696e52009-09-03 17:42:48 +0200560 err, (unsigned long)rb->ino,
561 (unsigned long long)rb->blkoff);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700562 if (!err2)
563 err2 = err;
564 next:
565 iput(inode); /* iput(NULL) is just ignored */
566 list_del_init(&rb->list);
567 kfree(rb);
568 }
569 return err2;
570}
571
572/**
573 * nilfs_do_roll_forward - salvage logical segments newer than the latest
574 * checkpoint
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900575 * @nilfs: nilfs object
Ryusuke Konishif7545142011-03-09 11:05:08 +0900576 * @sb: super block instance
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700577 * @ri: pointer to a nilfs_recovery_info
578 */
579static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
Ryusuke Konishif7545142011-03-09 11:05:08 +0900580 struct super_block *sb,
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900581 struct nilfs_root *root,
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700582 struct nilfs_recovery_info *ri)
583{
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900584 struct buffer_head *bh_sum = NULL;
Ryusuke Konishi4f050282015-11-06 16:32:16 -0800585 struct nilfs_segment_summary *sum = NULL;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700586 sector_t pseg_start;
587 sector_t seg_start, seg_end; /* Starting/ending DBN of full segment */
588 unsigned long nsalvaged_blocks = 0;
Ryusuke Konishi85655482010-05-23 19:46:44 +0900589 unsigned int flags;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700590 u64 seg_seq;
591 __u64 segnum, nextnum = 0;
592 int empty_seg = 0;
593 int err = 0, ret;
594 LIST_HEAD(dsync_blocks); /* list of data blocks to be recovered */
595 enum {
596 RF_INIT_ST,
597 RF_DSYNC_ST, /* scanning data-sync segments */
598 };
599 int state = RF_INIT_ST;
600
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700601 pseg_start = ri->ri_lsegs_start;
602 seg_seq = ri->ri_lsegs_start_seq;
603 segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
604 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
605
606 while (segnum != ri->ri_segnum || pseg_start <= ri->ri_pseg_start) {
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900607 brelse(bh_sum);
608 bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum);
609 if (!bh_sum) {
610 err = -EIO;
611 goto failed;
612 }
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700613
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900614 ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700615 if (ret) {
616 if (ret == NILFS_SEG_FAIL_IO) {
617 err = -EIO;
618 goto failed;
619 }
620 goto strayed;
621 }
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900622
Ryusuke Konishi85655482010-05-23 19:46:44 +0900623 flags = le16_to_cpu(sum->ss_flags);
624 if (flags & NILFS_SS_SR)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700625 goto confused;
626
627 /* Found a valid partial segment; do recovery actions */
Ryusuke Konishi85655482010-05-23 19:46:44 +0900628 nextnum = nilfs_get_segnum_of_block(nilfs,
629 le64_to_cpu(sum->ss_next));
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700630 empty_seg = 0;
Ryusuke Konishi85655482010-05-23 19:46:44 +0900631 nilfs->ns_ctime = le64_to_cpu(sum->ss_create);
632 if (!(flags & NILFS_SS_GC))
633 nilfs->ns_nongc_ctime = nilfs->ns_ctime;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700634
635 switch (state) {
636 case RF_INIT_ST:
Ryusuke Konishi85655482010-05-23 19:46:44 +0900637 if (!(flags & NILFS_SS_LOGBGN) ||
638 !(flags & NILFS_SS_SYNDT))
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700639 goto try_next_pseg;
640 state = RF_DSYNC_ST;
641 /* Fall through */
642 case RF_DSYNC_ST:
Ryusuke Konishi85655482010-05-23 19:46:44 +0900643 if (!(flags & NILFS_SS_SYNDT))
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700644 goto confused;
645
Ryusuke Konishi85655482010-05-23 19:46:44 +0900646 err = nilfs_scan_dsync_log(nilfs, pseg_start, sum,
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900647 &dsync_blocks);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700648 if (unlikely(err))
649 goto failed;
Ryusuke Konishi85655482010-05-23 19:46:44 +0900650 if (flags & NILFS_SS_LOGEND) {
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900651 err = nilfs_recover_dsync_blocks(
Ryusuke Konishif7545142011-03-09 11:05:08 +0900652 nilfs, sb, root, &dsync_blocks,
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900653 &nsalvaged_blocks);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700654 if (unlikely(err))
655 goto failed;
656 state = RF_INIT_ST;
657 }
658 break; /* Fall through to try_next_pseg */
659 }
660
661 try_next_pseg:
662 if (pseg_start == ri->ri_lsegs_end)
663 break;
Ryusuke Konishi85655482010-05-23 19:46:44 +0900664 pseg_start += le32_to_cpu(sum->ss_nblocks);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700665 if (pseg_start < seg_end)
666 continue;
667 goto feed_segment;
668
669 strayed:
670 if (pseg_start == ri->ri_lsegs_end)
671 break;
672
673 feed_segment:
674 /* Looking to the next full segment */
675 if (empty_seg++)
676 break;
677 seg_seq++;
678 segnum = nextnum;
679 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
680 pseg_start = seg_start;
681 }
682
683 if (nsalvaged_blocks) {
684 printk(KERN_INFO "NILFS (device %s): salvaged %lu blocks\n",
Ryusuke Konishif7545142011-03-09 11:05:08 +0900685 sb->s_id, nsalvaged_blocks);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700686 ri->ri_need_recovery = NILFS_RECOVERY_ROLLFORWARD_DONE;
687 }
688 out:
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900689 brelse(bh_sum);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700690 dispose_recovery_list(&dsync_blocks);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700691 return err;
692
693 confused:
694 err = -EINVAL;
695 failed:
696 printk(KERN_ERR
697 "NILFS (device %s): Error roll-forwarding "
698 "(err=%d, pseg block=%llu). ",
Ryusuke Konishif7545142011-03-09 11:05:08 +0900699 sb->s_id, err, (unsigned long long)pseg_start);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700700 goto out;
701}
702
703static void nilfs_finish_roll_forward(struct the_nilfs *nilfs,
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700704 struct nilfs_recovery_info *ri)
705{
706 struct buffer_head *bh;
707 int err;
708
709 if (nilfs_get_segnum_of_block(nilfs, ri->ri_lsegs_start) !=
710 nilfs_get_segnum_of_block(nilfs, ri->ri_super_root))
711 return;
712
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900713 bh = __getblk(nilfs->ns_bdev, ri->ri_lsegs_start, nilfs->ns_blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700714 BUG_ON(!bh);
715 memset(bh->b_data, 0, bh->b_size);
716 set_buffer_dirty(bh);
717 err = sync_dirty_buffer(bh);
718 if (unlikely(err))
719 printk(KERN_WARNING
720 "NILFS warning: buffer sync write failed during "
721 "post-cleaning of recovery.\n");
722 brelse(bh);
723}
724
725/**
Ryusuke Konishiaee5ce22010-05-23 12:21:57 +0900726 * nilfs_salvage_orphan_logs - salvage logs written after the latest checkpoint
727 * @nilfs: nilfs object
Ryusuke Konishif7545142011-03-09 11:05:08 +0900728 * @sb: super block instance
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700729 * @ri: pointer to a nilfs_recovery_info struct to store search results.
730 *
731 * Return Value: On success, 0 is returned. On error, one of the following
732 * negative error code is returned.
733 *
734 * %-EINVAL - Inconsistent filesystem state.
735 *
736 * %-EIO - I/O error
737 *
738 * %-ENOSPC - No space left on device (only in a panic state).
739 *
740 * %-ERESTARTSYS - Interrupted.
741 *
742 * %-ENOMEM - Insufficient memory available.
743 */
Ryusuke Konishiaee5ce22010-05-23 12:21:57 +0900744int nilfs_salvage_orphan_logs(struct the_nilfs *nilfs,
Ryusuke Konishif7545142011-03-09 11:05:08 +0900745 struct super_block *sb,
Ryusuke Konishiaee5ce22010-05-23 12:21:57 +0900746 struct nilfs_recovery_info *ri)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700747{
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900748 struct nilfs_root *root;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700749 int err;
750
751 if (ri->ri_lsegs_start == 0 || ri->ri_lsegs_end == 0)
752 return 0;
753
Ryusuke Konishif7545142011-03-09 11:05:08 +0900754 err = nilfs_attach_checkpoint(sb, ri->ri_cno, true, &root);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700755 if (unlikely(err)) {
756 printk(KERN_ERR
757 "NILFS: error loading the latest checkpoint.\n");
758 return err;
759 }
760
Ryusuke Konishif7545142011-03-09 11:05:08 +0900761 err = nilfs_do_roll_forward(nilfs, sb, root, ri);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700762 if (unlikely(err))
763 goto failed;
764
765 if (ri->ri_need_recovery == NILFS_RECOVERY_ROLLFORWARD_DONE) {
Ryusuke Konishif7545142011-03-09 11:05:08 +0900766 err = nilfs_prepare_segment_for_recovery(nilfs, sb, ri);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700767 if (unlikely(err)) {
768 printk(KERN_ERR "NILFS: Error preparing segments for "
769 "recovery.\n");
770 goto failed;
771 }
772
Ryusuke Konishif7545142011-03-09 11:05:08 +0900773 err = nilfs_attach_log_writer(sb, root);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700774 if (unlikely(err))
775 goto failed;
776
777 set_nilfs_discontinued(nilfs);
Ryusuke Konishif7545142011-03-09 11:05:08 +0900778 err = nilfs_construct_segment(sb);
779 nilfs_detach_log_writer(sb);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700780
781 if (unlikely(err)) {
782 printk(KERN_ERR "NILFS: Oops! recovery failed. "
783 "(err=%d)\n", err);
784 goto failed;
785 }
786
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900787 nilfs_finish_roll_forward(nilfs, ri);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700788 }
789
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700790 failed:
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900791 nilfs_put_root(root);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700792 return err;
793}
794
795/**
796 * nilfs_search_super_root - search the latest valid super root
797 * @nilfs: the_nilfs
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700798 * @ri: pointer to a nilfs_recovery_info struct to store search results.
799 *
800 * nilfs_search_super_root() looks for the latest super-root from a partial
801 * segment pointed by the superblock. It sets up struct the_nilfs through
802 * this search. It fills nilfs_recovery_info (ri) required for recovery.
803 *
804 * Return Value: On success, 0 is returned. On error, one of the following
805 * negative error code is returned.
806 *
807 * %-EINVAL - No valid segment found
808 *
809 * %-EIO - I/O error
Ryusuke Konishi2d72b992010-06-28 19:15:25 +0900810 *
811 * %-ENOMEM - Insufficient memory available.
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700812 */
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900813int nilfs_search_super_root(struct the_nilfs *nilfs,
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700814 struct nilfs_recovery_info *ri)
815{
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900816 struct buffer_head *bh_sum = NULL;
Ryusuke Konishi4f050282015-11-06 16:32:16 -0800817 struct nilfs_segment_summary *sum = NULL;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700818 sector_t pseg_start, pseg_end, sr_pseg_start = 0;
819 sector_t seg_start, seg_end; /* range of full segment (block number) */
Ryusuke Konishi050b4142009-11-19 22:24:48 +0900820 sector_t b, end;
Ryusuke Konishi85655482010-05-23 19:46:44 +0900821 unsigned long nblocks;
822 unsigned int flags;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700823 u64 seg_seq;
824 __u64 segnum, nextnum = 0;
825 __u64 cno;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700826 LIST_HEAD(segments);
827 int empty_seg = 0, scan_newer = 0;
828 int ret;
829
830 pseg_start = nilfs->ns_last_pseg;
831 seg_seq = nilfs->ns_last_seq;
832 cno = nilfs->ns_last_cno;
833 segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
834
835 /* Calculate range of segment */
836 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
837
Ryusuke Konishi050b4142009-11-19 22:24:48 +0900838 /* Read ahead segment */
839 b = seg_start;
840 while (b <= seg_end)
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900841 __breadahead(nilfs->ns_bdev, b++, nilfs->ns_blocksize);
Ryusuke Konishi050b4142009-11-19 22:24:48 +0900842
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700843 for (;;) {
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900844 brelse(bh_sum);
845 ret = NILFS_SEG_FAIL_IO;
846 bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum);
847 if (!bh_sum)
848 goto failed;
849
850 ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700851 if (ret) {
852 if (ret == NILFS_SEG_FAIL_IO)
853 goto failed;
854 goto strayed;
855 }
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900856
Ryusuke Konishi85655482010-05-23 19:46:44 +0900857 nblocks = le32_to_cpu(sum->ss_nblocks);
858 pseg_end = pseg_start + nblocks - 1;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700859 if (unlikely(pseg_end > seg_end)) {
860 ret = NILFS_SEG_FAIL_CONSISTENCY;
861 goto strayed;
862 }
863
864 /* A valid partial segment */
865 ri->ri_pseg_start = pseg_start;
866 ri->ri_seq = seg_seq;
867 ri->ri_segnum = segnum;
Ryusuke Konishi85655482010-05-23 19:46:44 +0900868 nextnum = nilfs_get_segnum_of_block(nilfs,
869 le64_to_cpu(sum->ss_next));
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700870 ri->ri_nextnum = nextnum;
871 empty_seg = 0;
872
Ryusuke Konishi85655482010-05-23 19:46:44 +0900873 flags = le16_to_cpu(sum->ss_flags);
874 if (!(flags & NILFS_SS_SR) && !scan_newer) {
Ryusuke Konishi050b4142009-11-19 22:24:48 +0900875 /* This will never happen because a superblock
876 (last_segment) always points to a pseg
877 having a super root. */
878 ret = NILFS_SEG_FAIL_CONSISTENCY;
879 goto failed;
880 }
881
882 if (pseg_start == seg_start) {
883 nilfs_get_segment_range(nilfs, nextnum, &b, &end);
884 while (b <= end)
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900885 __breadahead(nilfs->ns_bdev, b++,
886 nilfs->ns_blocksize);
Ryusuke Konishi050b4142009-11-19 22:24:48 +0900887 }
Ryusuke Konishi85655482010-05-23 19:46:44 +0900888 if (!(flags & NILFS_SS_SR)) {
889 if (!ri->ri_lsegs_start && (flags & NILFS_SS_LOGBGN)) {
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700890 ri->ri_lsegs_start = pseg_start;
891 ri->ri_lsegs_start_seq = seg_seq;
892 }
Ryusuke Konishi85655482010-05-23 19:46:44 +0900893 if (flags & NILFS_SS_LOGEND)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700894 ri->ri_lsegs_end = pseg_start;
895 goto try_next_pseg;
896 }
897
898 /* A valid super root was found. */
899 ri->ri_cno = cno++;
900 ri->ri_super_root = pseg_end;
901 ri->ri_lsegs_start = ri->ri_lsegs_end = 0;
902
903 nilfs_dispose_segment_list(&segments);
Ryusuke Konishi85655482010-05-23 19:46:44 +0900904 sr_pseg_start = pseg_start;
905 nilfs->ns_pseg_offset = pseg_start + nblocks - seg_start;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700906 nilfs->ns_seg_seq = seg_seq;
907 nilfs->ns_segnum = segnum;
908 nilfs->ns_cno = cno; /* nilfs->ns_cno = ri->ri_cno + 1 */
Ryusuke Konishi85655482010-05-23 19:46:44 +0900909 nilfs->ns_ctime = le64_to_cpu(sum->ss_create);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700910 nilfs->ns_nextnum = nextnum;
911
912 if (scan_newer)
913 ri->ri_need_recovery = NILFS_RECOVERY_SR_UPDATED;
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700914 else {
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700915 if (nilfs->ns_mount_state & NILFS_VALID_FS)
916 goto super_root_found;
917 scan_newer = 1;
918 }
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700919
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700920 try_next_pseg:
921 /* Standing on a course, or met an inconsistent state */
Ryusuke Konishi85655482010-05-23 19:46:44 +0900922 pseg_start += nblocks;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700923 if (pseg_start < seg_end)
924 continue;
925 goto feed_segment;
926
927 strayed:
928 /* Off the trail */
929 if (!scan_newer)
930 /*
931 * This can happen if a checkpoint was written without
932 * barriers, or as a result of an I/O failure.
933 */
934 goto failed;
935
936 feed_segment:
937 /* Looking to the next full segment */
938 if (empty_seg++)
939 goto super_root_found; /* found a valid super root */
940
Ryusuke Konishi654137d2009-05-17 19:07:21 +0900941 ret = nilfs_segment_list_add(&segments, segnum);
942 if (unlikely(ret))
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700943 goto failed;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700944
945 seg_seq++;
946 segnum = nextnum;
947 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
948 pseg_start = seg_start;
949 }
950
951 super_root_found:
952 /* Updating pointers relating to the latest checkpoint */
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900953 brelse(bh_sum);
Ryusuke Konishi0935db72009-11-29 02:39:11 +0900954 list_splice_tail(&segments, &ri->ri_used_segments);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700955 nilfs->ns_last_pseg = sr_pseg_start;
956 nilfs->ns_last_seq = nilfs->ns_seg_seq;
957 nilfs->ns_last_cno = ri->ri_cno;
958 return 0;
959
960 failed:
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900961 brelse(bh_sum);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700962 nilfs_dispose_segment_list(&segments);
963 return (ret < 0) ? ret : nilfs_warn_segment_error(ret);
964}