]> git.neil.brown.name Git - LaFS.git/blob - roll.c
README update
[LaFS.git] / roll.c
1
2 /*
3  * fs/lafs/roll.c
4  * Copyright (C) 2005-2010
5  * Neil Brown <neilb@suse.de>
6  * Released under the GPL, version 2
7  *
8  * 'rollforward'
9  *
10  * This file handles mounting of a filesystem once the superblocks
11  * have been loaded.
12  * It loads the root inode (the root of the filesystem, not of the
13  * directory tree) and then handles roll-forward to pick up and changes
14  * there are not in the filesystem yet, either due to a crash, or because
15  * they cannot be consistently stored easily (final segusage/quota info).
16  *
17  * Roll-forward reads write-cluster header and handle things as appropriate.
18  * Data blocks are only processed if they belong to:
19  *   - regular files
20  *   - segusage files
21  *   - quota files.
22  * A data block in a regular file implies an extension of the file size
23  * to the end of the block, if it was previously at or before the start
24  * of the block.  Datablocks that were just moved for cleaning are
25  * ignored.
26  *
27  * Index blocks are always ignored - they need to be recalculated.
28  *
29  * 'miniblocks' or 'updates' are always processed - they represent an
30  * atomic update that might affect multiple files - those files for which
31  * data blocks are ignored.
32  * Updates are understood:
33  * - for inodes.  The update simply over-writes part of the inode metadata,
34  *       which could affect the link count or size.  Such inodes become
35  *       orphans in case truncation or deletion is needed.  This can create
36  *       an inode which might affect the inode usage map.
37  * - for directories.  The update identifies a name and an inode number.
38  *       This can imply a change to the inode's link count and again could
39  *       make it an orphan.  In some cases updates are paired, possibly across
40  *       different directories.  This is needed for 'rename'.
41  *
42  * Each write-cluster has three levels of validation.
43  *  Firstly, if the header is internally consistent, with correct tag,
44  *   uuid, and sequence, then we know a write was attempted, and anything that
45  *   must be written before that was successfully written.
46  *  Secondly, if the header has a correct checksum, then it is all correct,
47  *   and the miniblocks are valid.
48  *  Thirdly, if the next or next-but-one header (depending on verify_type) is
49  *   internally consistent, than we know that the data blocks in this cluster
50  *   were all written successfully.
51  */
52
53 #include        "lafs.h"
54 #include        <linux/slab.h>
55
56 static int
57 roll_valid(struct fs *fs, struct cluster_head *ch, unsigned long long addr)
58 {
59         /* return 1 if the cluster_head looks locally valid.
60          * Don't check checksum as we may not have the whole head
61          */
62         if (memcmp(ch->idtag, "LaFSHead", 8) != 0)
63                 return 0;
64         if (memcmp(fs->state->uuid, ch->uuid, 16) != 0)
65                 return 0;
66         if (le64_to_cpu(ch->this_addr) != addr)
67                 return 0;
68         switch (le16_to_cpu(ch->verify_type)) {
69         case VerifyNull:
70         case VerifyNext:
71         case VerifyNext2:
72                 break;
73         default:
74                 return 0;
75         }
76         if (le16_to_cpu(ch->Clength) > fs->max_segment)
77                 return 0;
78         return 1;
79 }
80
81 /*
82  * roll_locate scopes out the full extent of the required roll-forward.
83  * It starts at the start of the last checkpoint (recorded in the stateblock)
84  * and checks that the end of the checkpoint exists, and continues following
85  * the chain as far as valid cluster heads can be found.
86  * roll_locate returns 0 if proper endpoints were found,
87  * or -EIO if CheckpointStart and CheckpointEnd weren't found properly
88  * "next" will contain the address of the next cluster to be written to,
89  * "last" the cluster before that, and "seq" the seq number for next cluster
90  * "maxp" will be used to report the maximum size of a cluster head.
91  */
92 static int
93 roll_locate(struct fs *fs, u64 start,
94             u64 *nextp, u64 *lastp, u64 *seqp,
95             int *maxp, struct page *p)
96 {
97         struct cluster_head *ch;
98         u64 this, prev, prev2, last, next;
99         u64 seq = 0;
100         int max = 0;
101         int prevtype, prev2type;
102
103         ch = (struct cluster_head *)page_address(p);
104
105         this = start; prev = start;
106
107         /* First we walk through the checkpoint section, which should
108          * all be valid.
109          */
110         do {
111                 if (lafs_load_page(fs, p, this, 1) != 0) {
112                         printk(KERN_ERR "LaFS: Could not read cluster %llu\n",
113                                (unsigned long long) this);
114                         return -EIO;
115                 }
116                 if (!roll_valid(fs, ch, this)) {
117                         printk(KERN_ERR "LaFS: Bad cluster at %llu\n",
118                                (unsigned long long) this);
119                         return -EIO;
120                 }
121                 if (this == start) {
122                         seq = le64_to_cpu(ch->seq);
123                         if (!(ch->flags & CH_CheckpointStart)) {
124                                 printk(KERN_ERR "LaFS: Cluster at %llu not CheckpointStart!!\n",
125                                        (unsigned long long)this);
126                                 return -EIO;
127                         }
128                 } else if (seq != le64_to_cpu(ch->seq)) {
129                         printk(KERN_ERR "LaFS: Cluster sequence bad at %llu: %llu->%llu\n",
130                                (unsigned long long)this,
131                                (unsigned long long)seq,
132                                (unsigned long long)le64_to_cpu(ch->seq));
133                         return -EIO;
134                 }
135
136                 if (this != start && le64_to_cpu(ch->prev_addr) != prev) {
137                         printk(KERN_ERR "LaFS: Cluster Linkage error at %llu: %llu != %llu\n",
138                                (unsigned long long)this,
139                                (unsigned long long)le64_to_cpu(ch->prev_addr),
140                                (unsigned long long)prev);
141                         return -EIO;
142                 }
143                 if (!ch->flags & CH_Checkpoint) {
144                         printk(KERN_ERR "LaFS: Cluster %llu not a Checkpoint cluster\n",
145                                (unsigned long long)this);
146                         return -EIO;
147                 }
148                 dprintk("Found seq %llu at %llu\n",
149                         (unsigned long long)seq, (unsigned long long)this);
150                 if (le16_to_cpu(ch->Hlength) > max)
151                         max = le16_to_cpu(ch->Hlength);
152                 prev = this;
153                 this = le64_to_cpu(ch->next_addr);
154                 seq++;
155         } while (!(ch->flags & CH_CheckpointEnd));
156
157         /* 'seq' is sequence number of 'this' */
158         dprintk("CheckpointEnd found at %llu, seq %llu\n", prev, seq-1);
159
160         /* now we need to step forward a bit more carefully, as any
161          * cluster we find now could easily be bad.
162          * We keep:
163          *   this - address of cluster we are now considering
164          *   prev - address of previous cluster
165          *   prevtype - verify type of previous cluster
166          *   prev2 - address of cluster before prev
167          *   prev2type - verify type of that cluster.
168          *   start - "next_addr" entry from last known-good cluster
169          *
170          *
171          */
172
173         last = prev;
174         next = this;
175         prev2 = prev;
176         prevtype = prev2type = VerifyNull;
177
178         while (1) {
179                 if (lafs_load_page(fs, p, this, 1) != 0)
180                         break;
181                 if (!roll_valid(fs, ch, this))
182                         break;
183                 if (le64_to_cpu(ch->prev_addr) != prev)
184                         break;
185                 if (le64_to_cpu(ch->seq) != seq)
186                         break;
187
188                 /* this head looks valid, so we can possibly verify previous
189                  * clusters
190                  */
191                 if (le16_to_cpu(ch->Hlength) > max)
192                         max = le16_to_cpu(ch->Hlength);
193
194                 if (prev2type == VerifyNext2) {
195                         next = prev;
196                         last = prev2;
197                 }
198                 if (prevtype == VerifyNext) {
199                         next = this;
200                         last = prev;
201                 }
202
203                 /* shift prev info back */
204                 prev2 = prev;
205                 prev2type = prevtype;
206                 prev = this;
207                 prevtype = le16_to_cpu(ch->verify_type);
208                 this = le64_to_cpu(ch->next_addr);
209                 if (prevtype == VerifyNull) {
210                         next = this;
211                         last = prev;
212                 }
213                 seq++;
214         }
215
216         dprintk("LaFS: Next address to write is %llu\n", next);
217         *nextp = next;
218         *lastp = last;
219         if (next == this)
220                 *seqp = seq;
221         else if (next == prev)
222                 *seqp = seq-1;
223         else if (next == prev2)
224                 *seqp = seq-2;
225         else
226                 BUG();
227         *maxp = max;
228         return 0;
229 }
230
231 static int __must_check
232 roll_mini(struct fs *fs, int fsnum, int inum, int trunc,
233           u32 bnum, int offset, int len, char *data)
234 {
235         struct inode *inode;
236         struct inode *fsinode;
237         struct lafs_inode *li;
238         struct datablock *db = NULL;
239         int err = 0;
240         void *buf;
241         char *name;
242         struct page *page;
243         void *fsdata;
244
245         dprintk("Roll Mini  %d/%d/%lu/%d,%d\n",
246                 fsnum, inum, (unsigned long) bnum,
247                 offset, len);
248
249         /* The handling of miniblock updates is quite different for
250          * different objects.
251          *
252          * inode-files: meta-data updates, including size, are allowed.
253          *      index update and data update are not (data update must
254          *      go through the file).  Implied creation requires
255          *      orphan handling
256          * regular-files: We don't create miniblocks for regular files,
257          *      but we might write an inode with embedded data and want
258          *      that data to be safe.  When those inodes are found, at
259          *      miniblock is synthesised from the data so we need to
260          *      handle it.
261          * symlink,dev,pipe: as with reg-files
262          * directory: add/remove entries.  Each miniblock has an address and
263          *      identifies a name, an inode number, and one of:
264          *        LINK  - create a link with this name to the inode
265          *        UNLINK - remove the link
266          *        REN_SOURCE - record this info against the 'address' which must
267          *            be unique in this checkpoint across all directories
268          *        REN_TARGET - The source with matching 'address' is being
269          *            renamed to here.  So unlink the source and either create the
270          *            target (if inode is zero) or replace the target.  This
271          *            miniblock could be in a different directory to the matching
272          *            REN_SOURCE.
273          */
274
275         inode = lafs_iget_fs(fs, fsnum, inum, SYNC);
276         if (IS_ERR(inode))
277                 return PTR_ERR(inode);
278
279         li = LAFSI(inode);
280
281         switch (li->type) {
282         default: /* Any unknown type is an error */
283                 printk(KERN_WARNING "LAFS impossibly file type for roll-forward: %d\n",
284                        li->type);
285                 err = -EIO;
286                 break;
287
288         case TypeInodeFile:
289
290                 if (fsnum) {
291                         printk(KERN_WARNING "LAFS: Ignoring impossible sub-subset\n");
292                         break;
293                 }
294
295                 fsinode = inode;
296                 inode = lafs_iget_fs(fs, inum, bnum, SYNC);
297                 if (IS_ERR(inode)) {
298                         err = PTR_ERR(inode);
299                         if (err != -ENOENT || offset != 0) {
300                                 lafs_iput_fs(fsinode);
301                                 return err;
302                         }
303
304                         db = lafs_get_block(fsinode, bnum, NULL, GFP_KERNEL,
305                                             MKREF(roll));
306                         lafs_inode_inuse(fs, fsinode, bnum);
307                         lafs_iput_fs(fsinode);
308                         if (!db)
309                                 db = ERR_PTR(-ENOMEM);
310                 } else {
311                         lafs_iput_fs(fsinode);
312                         db = lafs_inode_dblock(inode, SYNC, MKREF(roll));
313                         if (!IS_ERR(db))
314                                 /* Make sure block is in-sync with inode */
315                                 lafs_inode_fillblock(inode);
316                 }
317                 if (IS_ERR(db)) {
318                         err = PTR_ERR(db);
319                         break;
320                 }
321                 /* Should normally iolock the block, but we don't
322                  * need that during roll-forward */
323                 set_bit(B_PinPending, &db->b.flags);
324                 lafs_pin_dblock(db, CleanSpace);
325                 buf = map_dblock(db);
326                 memcpy(buf+offset, data, len);
327                 unmap_dblock(db, buf);
328                 if (inode)
329                         err = lafs_import_inode(inode, db);
330                 else {
331                         inode = lafs_iget_fs(fs, inum, bnum, SYNC);
332
333                 }
334                 lafs_dirty_dblock(db);
335                 break;
336
337         case TypeDir:
338                 /* 'bnum' is the handle for match 'rename' parts.
339                  * 'offset' is the DIROP type
340                  * 'len' is 4 plus length of name.
341                  * data contains 4-byte inode number, then name
342                  */
343                 if (len <= 4) { 
344                         err = -EIO;
345                         break;
346                 }
347                 inum = le32_to_cpu(*(u32*)data);
348                 name = data + 4;
349                 err = lafs_dir_roll_mini(inode, bnum, offset, inum, name, len-4);
350                 break;
351
352         case TypeFile:
353         case TypeSymlink:
354         case TypeSpecial:
355                 if (bnum != 0 || offset != 0) {
356                         /* We currently only expect update at the very start
357                          * of a (small) file.
358                          * So reject anything else.
359                          */
360                         err = -EIO;
361                         break;
362                 }
363                 err = pagecache_write_begin(NULL, inode->i_mapping,
364                                             0, len, 0,
365                                             &page, &fsdata);
366                 if (!err) {
367                         char *b = kmap_atomic(page, KM_USER0);
368                         memcpy(b, data, len);
369                         kunmap_atomic(b, KM_USER0);
370                         pagecache_write_end(NULL, inode->i_mapping,
371                                             0, len, len, page, fsdata);
372                 }
373                 break;
374         }
375         /* We borrow the orphan list to keep a reference on
376          * this inode until all processing is finished
377          * to make sure inodes that are about to get linked
378          * don't get deleted early
379          */
380         if (inode->i_nlink == 0) {
381                 if (!db)
382                         db = lafs_inode_get_dblock(inode, MKREF(roll));
383                 if (db &&
384                     list_empty(&db->orphans)) {
385                         list_add(&db->orphans, &fs->pending_orphans);
386                         lafs_igrab_fs(inode);
387                         getdref(db, MKREF(roll_orphan));
388                 }
389         }
390         putdref(db, MKREF(roll));
391         lafs_iput_fs(inode);
392         return err;
393 }
394
395 static int __must_check
396 roll_block(struct fs *fs, int fsnum, int inum, int trunc,
397            u32 bnum, u64 baddr, int bytes, u64 tstamp, struct page *p)
398 {
399         struct inode *inode;
400         struct datablock *blk = NULL;
401         struct lafs_inode *li;
402         int err = 0;
403
404         /* We found this block during roll-forward and need to
405          * include it in the filesystem.
406          * If 'bytes' is 0, the this is a 'hole' and we should
407          * ignore baddr
408          */
409         if (bytes == DescHole)
410                 baddr = 0;
411
412         dprintk("Roll Block %d/%d/%lu/%llu\n",
413                 fsnum, inum, (unsigned long) bnum,
414                 (unsigned long long)baddr);
415
416         /* find/load the inode */
417         inode = lafs_iget_fs(fs, fsnum, inum, SYNC);
418         if (IS_ERR(inode))
419                 return PTR_ERR(inode);
420
421         /* check type */
422         li = LAFSI(inode);
423
424         dprintk("Got the inode, type %d %p size %llu\n", li->type,
425                 inode, inode->i_size);
426
427         switch (li->type) {
428                 struct la_inode *lai;
429                 int mdsize;
430
431         default: /* most filetypes are simply ignored */
432                 break;
433
434         case TypeInodeFile:
435                 /* The only part of an inode that might be interesting
436                  * is embedded data: All metadata changes get logged
437                  * as miniblocks.
438                  * Further the data can only be interesting for non-directories,
439                  * as directory updates are also logged as miniblocks.
440                  * So if this is a depth==0 non-directory inode,
441                  * treat the data as a miniblock update.
442                  */
443                 if (bytes != fs->blocksize)
444                         break;
445                 err = lafs_load_page(fs, p, baddr, 1);
446                 dprintk("inode load page err %d\n", err);
447                 if (err)
448                         break;
449                 lai = (struct la_inode *)page_address(p);
450                 mdsize = le16_to_cpu(lai->metadata_size);
451                 if (lai->filetype >= TypeBase &&
452                     lai->filetype != TypeDir  &&
453                     lai->depth == 0 &&
454                     mdsize > 1 && mdsize < fs->blocksize) {
455                         u64 sz = le64_to_cpu(lai->metadata[0].file.size);
456                         if (sz <= fs->blocksize - mdsize)
457                                 err = roll_mini(fs, inum, bnum, -1, 0, 0,
458                                                 (int)sz,
459                                                 page_address(p) + mdsize);
460                 }
461                 break;
462
463         case TypeSegmentMap:
464         case TypeQuota:
465                 /* These only get merged while in a checkpoint. */
466                 if (fs->qphase == fs->phase)
467                         break;
468                 /* FALL THROUGH */
469         case TypeFile:
470         case TypeSymlink:
471                 /* merge into the file and possibly extend inode.size
472                  * Only extend the size if it was before this block.
473                  * i.e. if size was to the middle of this block, we don't
474                  * extend the size
475                  */
476                 dprintk("FILE type\n");
477                 err = -ENOMEM;
478                 blk = lafs_get_block(inode, bnum, NULL, GFP_KERNEL,
479                                      MKREF(roll));
480                 if (!blk)
481                         break;
482
483                 err = lafs_find_block(blk, ADOPT);
484                 if (err)
485                         break;
486                 if (blk->b.physaddr == baddr)
487                         /* already correctly indexed */
488                         break;
489
490                 if (li->type >= TypeBase && bytes != DescHole &&
491                     inode->i_size <= ((loff_t)bnum << inode->i_blkbits)) {
492                         inode->i_size = ((loff_t)bnum << inode->i_blkbits) + bytes;
493                         set_bit(I_Dirty, &LAFSI(inode)->iflags);
494                 }
495                 if (tstamp) {
496                         decode_time(&inode->i_mtime, tstamp);
497                         decode_time(&inode->i_ctime, tstamp);
498                         set_bit(I_Dirty, &LAFSI(inode)->iflags);
499                 }
500
501                 /* FIXME: we pretend this is a dirty, pinned block
502                  * so the lower-level code doesn't get confused.
503                  * Is this really the best approach?
504                  * Do I need to release some space here?
505                  */
506                 set_bit(B_PinPending, &blk->b.flags); /* Don't need iolock as no io yet */
507                 lafs_pin_dblock(blk, CleanSpace); /* cannot fail during ! ->rolled */
508
509                 lafs_iolock_block(&blk->b);
510                 /* The '1' in lafs_summary_update assumes SegRef is set, so
511                  * assert that it is.
512                  */
513                 LAFS_BUG(!test_bit(B_SegRef, &blk->b.flags), &blk->b);
514                 lafs_summary_update(fs, blk->b.inode, blk->b.physaddr, baddr,
515                                     0, fs->phase, 1);
516                 blk->b.physaddr = baddr;
517                 lafs_dirty_iblock(blk->b.parent, 0);
518                 set_bit(B_Writeback, &blk->b.flags);
519                 lafs_iounlock_block(&blk->b);
520
521                 while (lafs_add_block_address(fs, &blk->b) == 0)
522                         /* Just like in lafs_phase_flip, there is no special
523                          * action required here.
524                          */
525                         ;
526
527                 dprintk("Allocated block %lu to %llu\n",
528                         (unsigned long)bnum, baddr);
529                 lafs_writeback_done(&blk->b);
530
531                 clear_bit(B_PinPending, &blk->b.flags);
532                 /* If we had previously read this block for some reason,
533                  * the contents are now invalid.  If they are dirty,
534                  * we have a real problem as those changes cannot be saved.
535                  */
536                 LAFS_BUG(test_bit(B_Dirty, &blk->b.flags), &blk->b);
537                 clear_bit(B_Valid, &blk->b.flags);
538
539                 break;
540         }
541         if (blk)
542                 putdref(blk, MKREF(roll));
543
544         if (inode->i_nlink == 0) {
545                 struct datablock *db = lafs_inode_get_dblock(inode, MKREF(roll));
546                 if (db &&
547                     list_empty(&db->orphans)) {
548                         list_add(&db->orphans, &fs->pending_orphans);
549                         lafs_igrab_fs(inode);
550                         getdref(db, MKREF(roll_orphan));
551                 }
552                 putdref(db, MKREF(roll));
553         }
554         lafs_iput_fs(inode);
555         dprintk("leaving with error %d\n", err);
556         return err;
557 }
558
559 static int __must_check
560 roll_one(struct fs *fs, u64 *addrp, struct page *p, struct page *pg,
561          int max)
562 {
563         u64 addr = *addrp;
564         struct cluster_head *ch = (struct cluster_head *)page_address(p);
565         struct group_head *gh;
566         struct descriptor *desc;
567         int i;
568         u64 baddr;
569         int err;
570         int blocksize = fs->blocksize;
571         struct segpos seg;
572         int header_blocks;
573
574         /* we "know" buf is big enough */
575         err = lafs_load_pages(fs, p, addr, max/blocksize);
576         if (err)
577                 return err;
578
579         /* just minimal checks, as we have looked at this already */
580         if (!roll_valid(fs, ch, addr))
581                 return -EIO;
582         if (lafs_calc_cluster_csum(ch) != ch->checksum)
583                 return -EIO;
584         *addrp = le64_to_cpu(ch->next_addr);
585
586         if (le16_to_cpu(ch->Hlength) > max)
587                 return -EIO;
588
589         lafs_seg_setpos(fs, &seg, addr);
590         lafs_seg_setsize(fs, &seg, le16_to_cpu(ch->Clength));
591         header_blocks = (le16_to_cpu(ch->Hlength) + blocksize - 1) / blocksize;
592         for (i = 0; i < header_blocks; i++) {
593                 baddr = lafs_seg_next(fs, &seg);
594                 BUG_ON(baddr != addr + i);
595         }
596
597         if (!(ch->flags & CH_Checkpoint))
598                 fs->qphase = fs->phase;
599
600         gh = ch->groups;
601         i = 0;
602         while (((char *)gh - (char *)ch) < le16_to_cpu(ch->Hlength)) {
603                 int j = 0;
604                 int inum = le32_to_cpu(gh->inum);
605                 int fsnum = le32_to_cpu(gh->fsnum);
606                 int trunc = le16_to_cpu(gh->truncatenum_and_flag) & 0x7fff;
607                 int flg   = le16_to_cpu(gh->truncatenum_and_flag) & 0x8000;
608                 u64 tstamp = le64_to_cpu(gh->timestamp);
609
610                 desc = gh->u.desc;
611                 while (((char *)desc - (char *)gh) <
612                        le16_to_cpu(gh->group_size_words)*4) {
613                         if (le16_to_cpu(desc->block_bytes) <= DescMiniOffset ||
614                             le16_to_cpu(desc->block_bytes) == DescIndex) {
615                                 u32 bnum = le32_to_cpu(desc->block_num);
616                                 int cnt = le16_to_cpu(desc->block_cnt);
617                                 int bytes = le16_to_cpu(desc->block_bytes);
618
619                                 if (le16_to_cpu(desc->block_bytes) == DescIndex
620                                     && cnt != 1)
621                                         return -EIO; /* FIXME is this
622                                                       * the best
623                                                       * response */
624                                 /* FIXME range check count */
625                                 while (!err && cnt--) {
626                                         if (bytes != DescHole)
627                                                 baddr = lafs_seg_next(fs, &seg);
628                                         if (bytes != DescHole &&
629                                             !baddr) {
630                                                 /* We have fallen off the end of
631                                                  * the write-cluster - something
632                                                  * is wrong with the header
633                                                  */
634                                                 printk(KERN_WARNING "LAFS: cluster size is wrong\n");
635                                                 return -EIO;
636                                         }
637                                         if (!flg && bytes != DescIndex)
638                                                 err = roll_block(fs, fsnum, inum, trunc,
639                                                                  bnum, baddr,
640                                                                  cnt == 0 || bytes == DescHole
641                                                                  ? bytes
642                                                                  : blocksize,
643                                                                  tstamp,
644                                                                  pg);
645                                         bnum++;
646                                 }
647                                 desc++;
648                         } else {
649                                 struct miniblock *mb = (struct miniblock *)desc;
650                                 u32 bnum = le32_to_cpu(mb->block_num);
651                                 int offset = le16_to_cpu(mb->block_offset);
652                                 int len = le16_to_cpu(mb->length)
653                                         - DescMiniOffset;
654                                 if (!flg)
655                                         err = roll_mini(fs, fsnum, inum, trunc,
656                                                         bnum, offset, len, (char *)(mb+1));
657
658                                 mb++;
659                                 mb = (struct miniblock *)(((char*)mb)
660                                                           + ROUND_UP(len));
661                                 desc = (struct descriptor *)mb;
662                         }
663                         j++;
664                         if (err)
665                                 break;
666                 }
667                 gh = (struct group_head *)desc;
668                 i++;
669                 if (err)
670                         break;
671         }
672         if (ch->flags & CH_CheckpointEnd)
673                 fs->qphase = fs->phase;
674         return err;
675 }
676
677 static int roll_forward(struct fs *fs)
678 {
679         u64 first, next = 0, last = 0, seq = 0;
680         int max = 0;
681         struct page *p, *pg;
682         int err;
683         int blocksize = fs->blocksize;
684         int dev;
685         u32 seg;
686         u32 offset;
687         int order = 0;
688         struct list_head pending;
689
690         fs->phase = 1;
691         fs->qphase = 0;
692         fs->checkpointing = CH_Checkpoint;
693         clear_bit(DelayYouth, &fs->fsstate);
694
695         first = fs->checkpointcluster;
696         p = alloc_pages(GFP_KERNEL, order);
697         if (!p)
698                 return -ENOMEM;
699
700         err = roll_locate(fs, first, &next, &last, &seq, &max, p);
701
702         max = ((max + blocksize - 1) / blocksize) * blocksize;
703
704         if (!err && max > PAGE_SIZE) {
705                 __free_pages(p, order);
706                 order = get_order(max * blocksize);
707                 p = alloc_pages(order, GFP_KERNEL);
708                 if (!p)
709                         err = -EFBIG;
710         }
711         if (err) {
712                 __free_pages(p, order);
713                 return err;
714         }
715
716         pg = alloc_page(GFP_KERNEL);
717         if (!pg) {
718                 __free_pages(p, order);
719                 return -ENOMEM;
720         }
721
722         err = lafs_cluster_init(fs, 0, next, last, seq);
723         if (err) {
724                 __free_pages(p, order); put_page(pg);
725                 return err;
726         }
727         lafs_cluster_init(fs, 1, 0, 0, 0);
728
729         virttoseg(fs, first, &dev, &seg, &offset);
730
731         while (first != next) {
732                 int dev2;
733                 u32 seg2;
734
735                 virttoseg(fs, first, &dev2, &seg2, &offset);
736                 err = roll_one(fs, &first, p, pg, max);
737                 if (err)
738                         break;
739
740                 if (fs->qphase == fs->phase &&
741                     fs->checkpointing) {
742                         fs->checkpointing = 0;
743                         clear_bit(DelayYouth, &fs->fsstate);
744                         lafs_seg_apply_all(fs);
745                 }
746
747                 if (dev2 != dev || seg2 != seg) {
748                         /* New segment - need to make sure youth is correct */
749                         dev = dev2;
750                         seg = seg2;
751                         /* if fs->checkpointing, seg_apply_all will do the youth
752                          * update
753                          */
754                         if (fs->checkpointing == 0)
755                                 lafs_update_youth(fs, dev, seg);
756                 }
757         }
758         __free_pages(p, order);
759         put_page(pg);
760
761         lafs_add_active(fs, next);
762
763         /* pending_renames will normally be empty, but it is not
764          * impossible that we crashed and an awkward time.  So just
765          * clean up whatever is there 
766          */
767         while (fs->pending_renames != NULL) {
768                 struct rename_roll *rr = fs->pending_renames;
769                 fs->pending_renames = rr->next;
770                 iput(rr->dir);
771                 iput(rr->inode);
772                 kfree(rr);
773         }
774
775
776         /* Now we release all the nlink==0 inodes that we found */
777         INIT_LIST_HEAD(&pending);
778         list_splice_init(&fs->pending_orphans, &pending);
779         while (!list_empty(&pending)) {
780                 struct datablock *db = list_first_entry(&pending,
781                                                         struct datablock,
782                                                         orphans);
783                 list_del_init(&db->orphans);
784                 if (db->my_inode->i_nlink == 0)
785                         lafs_make_orphan(fs, db);
786                 lafs_iput_fs(db->my_inode);
787                 putdref(db, MKREF(roll_orphan));
788         }
789         fs->rolled = 1;
790         return err;
791 }
792
793 int
794 lafs_mount(struct fs *fs)
795 {
796         struct datablock *b = NULL;
797         struct inode *rootino;
798         struct inode *rootdir;
799         struct inode *aino, *oino;
800         struct dentry *de;
801         int err;
802         int d;
803         struct sb_key *k = fs->prime_sb->s_fs_info;
804         int orphan_count;
805
806         fs->rolled = 0;
807         fs->ss[0].root = rootino = iget_locked(fs->prime_sb, 0);
808         k->root = rootino;
809         LAFSI(rootino)->filesys = rootino;
810
811         err = -ENOMEM;
812         if (!rootino)
813                 goto err;
814         b = lafs_get_block(rootino, 0, NULL, GFP_KERNEL, MKREF(mount));
815         if (!b)
816                 goto err;
817         set_bit(B_Root, &b->b.flags);
818         b->b.physaddr = fs->ss[0].root_addr;
819         set_bit(B_PhysValid, &b->b.flags);
820         err = lafs_load_block(&b->b, NULL);
821         if (err)
822                 goto err;
823         err = lafs_wait_block(&b->b);
824         if (err)
825                 goto err;
826
827         err = lafs_import_inode(rootino, b);
828         if (err)
829                 goto err;
830         putdref(b, MKREF(mount));
831         b = NULL;
832
833         unlock_new_inode(rootino);
834
835         rootdir = lafs_iget(rootino, 2, SYNC);
836         err = PTR_ERR(rootdir);
837         if (IS_ERR(rootdir))
838                 goto err;
839         de = d_alloc_root(rootdir);
840         err = PTR_ERR(de);
841         if (IS_ERR(de)) {
842                 iput(rootdir);
843                 goto err;
844         }
845         fs->prime_sb->s_root = de;
846
847         oino = lafs_iget(rootino, 8, SYNC);
848         err = PTR_ERR(oino);
849         if (IS_ERR(oino))
850                 goto err;
851         if (LAFSI(oino)->type != TypeOrphanList) {
852                 iput(oino);
853                 err = -EINVAL;
854                 goto err;
855         }
856         fs->orphans = oino;
857         for (d = 0; d < fs->devices ; d++) {
858                 struct inode *sino = lafs_iget(rootino,
859                                                fs->devs[d].usage_inum,
860                                                SYNC);
861                 err = PTR_ERR(sino);
862                 if (IS_ERR(sino))
863                         goto err;
864                 if (LAFSI(sino)->type != TypeSegmentMap) {
865                         iput(sino);
866                         err = -EINVAL;
867                         goto err;
868                 }
869                 fs->devs[d].segsum = sino;
870         }
871         orphan_count = lafs_count_orphans(fs->orphans);
872         LAFSI(fs->orphans)->md.orphan.nextfree = orphan_count;
873
874         lafs_checkpoint_lock(fs);
875         err = roll_forward(fs);
876         lafs_checkpoint_unlock(fs);
877
878         lafs_add_orphans(fs, fs->orphans, orphan_count);
879
880         for (d = 0; d < 4; d++) {
881                 struct page *p = alloc_page(GFP_KERNEL);
882                 if (!p)
883                         err = -ENOMEM;
884                 fs->cleaner.seg[d].chead = p;
885                 INIT_LIST_HEAD(&fs->cleaner.seg[d].cleaning);
886         }
887
888         aino = lafs_iget(rootino, 3, SYNC);
889         if (!IS_ERR(aino)) {
890                 if (LAFSI(aino)->type != TypeAccessTime) {
891                         iput(aino);
892                         err = -EINVAL;
893                 } else
894                         LAFSI(fs->ss[0].root)->md.fs.accesstime = aino;
895         } else if (PTR_ERR(aino) != -ENOENT)
896                 err = PTR_ERR(aino);
897
898 err:
899         putdref(b, MKREF(mount));
900         return err;
901 }