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