]> git.neil.brown.name Git - LaFS.git/blob - inode.c
Update to 2.6.38
[LaFS.git] / inode.c
1
2 /*
3  * fs/lafs/inode.c
4  * Copyright (C) 2005-2009
5  * Neil Brown <neilb@suse.de>
6  * Released under the GPL, version 2
7  *
8  * generic inode handling
9  *
10  */
11
12 #include        "lafs.h"
13 #include <linux/random.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
16
17 static void check_atime_ref(struct inode *ino, int async);
18
19 /* Supporting an async 'iget' - as required by the cleaner -
20  * is slightly non-trivial.
21  * iget*_locked will normally wait for any inode with one
22  * of the flags I_FREEING I_CLEAR I_WILL_FREE I_NEW
23  * to either be unhashed or have the flag cleared.
24  * We cannot afford that wait in the cleaner as we could deadlock.
25  * So we use iget5_locked and provide a test function that fails
26  * if it finds the inode with any of those flags set.
27  * If it does see the inode like that it sets a flag in the 'ikey'
28  * that is passed in by reference so that it knows to continue
29  * failing (for consistency) and so that the 'set' function
30  * we provide can know to fail the 'set'.
31  * The result of this is that if iget finds an inode it would
32  * have to wait on, a flag is set and NULL is returned.
33  * An unfortunate side effect is that an inode will be allocated
34  * and then destroyed to no avail.
35  * This is avoided by calling ilookup5 first.  This also allows
36  * us to only allocate/load the data block if there really seems
37  * to be a need.
38  */
39 struct ikey {
40         ino_t inum;
41         struct inode *fsys;
42         bool was_busy;
43 };
44
45 static int sync_itest(struct inode *inode, void *data)
46 {
47         struct ikey *ik = data;
48
49         if (inode->i_ino != ik->inum ||
50             LAFSI(inode)->filesys != ik->fsys)
51                 return 0;
52         return 1;
53 }
54
55 static int async_itest(struct inode *inode, void *data)
56 {
57         struct ikey *ik = data;
58
59         if (ik->was_busy)
60                 /* found and is freeing */
61                 return 0;
62         if (!sync_itest(inode, data))
63                 return 0;
64         if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW)) {
65                 ik->was_busy = true;
66                 return 0;
67         }
68         return 1;
69 }
70
71 static int iset(struct inode *inode, void *data)
72 {
73         struct ikey *ik = data;
74         if (ik->was_busy)
75                 return -EBUSY;
76         inode->i_ino = ik->inum;
77         LAFSI(inode)->filesys = ik->fsys;
78         return 0;
79 }
80
81 struct inode *
82 lafs_iget(struct inode *fsys, ino_t inum, int async)
83 {
84         /* find, and load if needed, this inum */
85         struct inode *ino = NULL;
86         struct inode *oldino;
87         struct datablock *b = NULL;
88         struct ikey ik = { .inum = inum, .fsys = fsys, };
89         int err = 0;
90         struct super_block *sb = fsys->i_sb;
91
92         if (async) {
93                 /* We cannot afford to block on 'freeing_inode'
94                  * So use iget5_locked and refuse to match such
95                  * inodes.
96                  * If the inode is 'freeing', inum gets set to NO_INO.
97                  * ilookup5 is used first to avoid an unnecessary
98                  * alloc/free if the inode is locked in some way.
99                  */
100                 while (!ino) {
101
102                         err = 0;
103                         ino = ilookup5(sb, inum, async_itest, &ik);
104                         if (ino)
105                                 break;
106
107                         if (ik.was_busy)
108                                 err = -EAGAIN;
109
110                         /* For async we will always want the dblock loaded,
111                          * and we need to load it first as we cannot afford
112                          * to fail -EAGAIN once we have an I_NEW inode.
113                          */
114                         if (!b)
115                                 b = lafs_get_block(fsys, inum, NULL,
116                                                    GFP_NOFS, MKREF(iget));
117                         if (!b)
118                                 return ERR_PTR(-ENOMEM);
119
120                         if (!err)
121                                 err = lafs_read_block_async(b);
122
123                         if (!err) {
124                                 /* Have the block, so safe to iget */
125                                 ino = iget5_locked(sb, inum,
126                                                    async_itest, iset,
127                                                    &ik);
128                                 if (!ino) {
129                                         if (ik.was_busy)
130                                                 err = -EAGAIN;
131                                         else
132                                                 err = -ENOMEM;
133                                 }
134                         }
135                         if (err) {
136                                 if (test_and_set_bit(B_Async, &b->b.flags)) {
137                                         putdref(b, MKREF(iget));
138                                         return ERR_PTR(err);
139                                 }
140                                 getdref(b, MKREF(async));
141                         }
142                 }
143         } else
144                 ino = iget5_locked(sb, inum, sync_itest, iset, &ik);
145
146         if (!ino) {
147                 putdref(b, MKREF(iget));
148                 return ERR_PTR(-ENOMEM);
149         }
150
151         if (!(ino->i_state & I_NEW)) {
152                 putdref(b, MKREF(iget));
153                 if (ino->i_mode) {
154                         check_atime_ref(ino, async);
155                         return ino;
156                 }
157                 iput(ino);
158                 return ERR_PTR(-ENOENT);
159         }
160
161         igrab(LAFSI(ino)->filesys);
162
163         /* surprisingly the inode bdi does not default to the
164          * super_blocks bdi...
165          */
166         ino->i_data.backing_dev_info = sb->s_bdi;
167         /* Need to load block 'inum' from an inode file...
168          */
169         if (!b) {
170                 b = lafs_get_block(fsys, inum, NULL, GFP_KERNEL, MKREF(iget));
171                 if (!b)
172                         err = -ENOMEM;
173                 else
174                         err = lafs_read_block(b);
175         }
176         if (err)
177                 goto err;
178
179         oldino = rcu_my_inode(b);
180         if (oldino) {
181                 /* The inode is new, but the block thinks it has an
182                  * old inode, so we must be in the process of destroying
183                  * the old one.
184                  * So fail the lookup without even looking at the content
185                  * of the block (Which might not be clear yet).
186                  */
187                 spin_lock(&oldino->i_data.private_lock);
188                 if (!test_bit(I_Deleting, &LAFSI(oldino)->iflags)) {
189                         b->my_inode = NULL;
190                         LAFSI(oldino)->dblock = NULL;
191                         LAFSI(oldino)->iblock = NULL;
192                 }
193                 spin_unlock(&oldino->i_data.private_lock);
194         }
195         rcu_iput(oldino);
196         if (b->my_inode) {
197                 err = -ENOENT;
198                 goto err;
199         }
200
201         err = lafs_import_inode(ino, b);
202         if (err) {
203                 if (err != -ENOENT)
204                         printk("lafs_import_inode failed %d\n", err);
205                 goto err;
206         }
207         check_atime_ref(ino, async);
208         unlock_new_inode(ino);
209 out:
210         if (b && test_and_clear_bit(B_Async, &b->b.flags)) {
211                 putdref(b, MKREF(async));
212                 lafs_wake_thread(fs_from_inode(fsys));
213         }
214         putdref(b, MKREF(iget));
215         return ino;
216 err:
217         ino->i_nlink = 0;
218         unlock_new_inode(ino);
219         iput(ino);
220         ino = ERR_PTR(err);
221         goto out;
222 }
223
224 struct inode *
225 lafs_iget_fs(struct fs *fs, int fsnum, int inum, int async)
226 {
227         struct super_block *sb;
228         struct inode *rv;
229
230         sb = fs->prime_sb;
231
232         if (fsnum) {
233                 /* Need to locate or load the superblock for this
234                  * subordinate filesystem
235                  */
236                 struct inode *filesys;
237
238                 filesys = lafs_iget(fs->ss[0].root, fsnum, async);
239                 if (IS_ERR(filesys))
240                         return filesys;
241                 if (LAFSI(filesys)->type != TypeInodeFile) {
242                         iput(filesys);
243                         return ERR_PTR(-ENOENT);
244                 }
245                 rv = lafs_iget(filesys, inum, async);
246         } else if (inum) {
247                 rv = lafs_iget(fs->ss[0].root, inum, async);
248                 if (!IS_ERR(rv))
249                         atomic_inc(&sb->s_active);
250         } else {
251                 rv = igrab(fs->ss[0].root);
252                 atomic_inc(&sb->s_active);
253         }
254         return rv;
255 }
256
257 int __must_check
258 lafs_import_inode(struct inode *ino, struct datablock *b)
259 {
260         struct la_inode *lai = map_dblock(b);
261         struct lafs_inode *li = LAFSI(ino);
262         int err = -ENOENT;
263
264         if (lai->filetype == 0) {
265                 li->type = 0;
266                 ino->i_mode = 0;
267                 ino->i_nlink = 0;
268                 goto out;
269         }
270
271         ino->i_mode = S_IFREG;
272         ino->i_nlink = 1; /* For special file, set nlink so they
273                            * never appear unlinked */
274
275         err = -EINVAL;
276
277         LAFS_BUG(ino->i_ino != b->b.fileaddr, &b->b);
278         li->cblocks = le32_to_cpu(lai->data_blocks);
279         li->pblocks = li->ablocks = 0;
280         li->vfs_inode.i_blocks = ((blkcnt_t)li->cblocks
281                                   << (ino->i_sb->s_blocksize_bits - 9));
282         li->ciblocks = le32_to_cpu(lai->index_blocks);
283         li->piblocks = 0;
284         li->iflags = 0;
285
286         ino->i_generation = le16_to_cpu(lai->generation);
287         li->trunc_gen = lai->trunc_gen;
288         li->flags = lai->flags;
289         li->type = lai->filetype;
290         li->metadata_size = le16_to_cpu(lai->metadata_size);
291         li->depth = lai->depth;
292
293         dprintk("inode %lu type is %d\n", (unsigned long)ino->i_ino, li->type);
294
295         ino->i_data.a_ops = &lafs_file_aops;
296         li->trunc_next = 0;
297
298         switch (li->type) {
299         case TypeInodeFile:
300         {
301                 struct fs_md *i = &li->md.fs;
302                 struct fs_metadata *l = &lai->metadata[0].fs;
303                 int nlen;
304
305                 i->usagetable = le16_to_cpu(l->snapshot_usage_table);
306                 decode_time(&ino->i_mtime, le64_to_cpu(l->update_time));
307                 i->cblocks_used = le64_to_cpu(l->blocks_used);
308                 i->pblocks_used = i->ablocks_used = 0;
309                 i->blocks_allowed = le64_to_cpu(l->blocks_allowed);
310                 i->blocks_unalloc = 0;
311                 i->creation_age = le64_to_cpu(l->creation_age);
312                 i->inodes_used = le32_to_cpu(l->inodes_used);
313                 i->quota_inums[0] = le32_to_cpu(l->quota_inodes[0]);
314                 i->quota_inums[1] = le32_to_cpu(l->quota_inodes[1]);
315                 i->quota_inums[2] = le32_to_cpu(l->quota_inodes[2]);
316                 i->quota_inodes[0] = i->quota_inodes[1]
317                         = i->quota_inodes[2] = NULL;
318                 nlen = li->metadata_size - offsetof(struct la_inode,
319                                                     metadata[0].fs.name);
320                 i->accesstime = NULL;
321                 if (i->name)
322                         kfree(i->name);
323                 if (nlen == 0)
324                         i->name = NULL;
325                 else {
326                         /* Need to unmap the dblock to kmalloc because
327                          * the mapping makes us 'atomic'
328                          */
329                         unmap_dblock(b, lai);
330                         i->name = kmalloc(nlen+1, GFP_KERNEL);
331                         lai = map_dblock(b);
332                         l = &lai->metadata[0].fs;
333
334                         err = -ENOMEM;
335                         if (!i->name)
336                                 goto out;
337                         memcpy(i->name, l->name, nlen);
338                         i->name[nlen] = 0;
339                 }
340                 /* Make this look like a directory */
341                 ino->i_mode = S_IFDIR;
342                 ino->i_uid = 0;
343                 ino->i_gid = 0;
344                 ino->i_size = 0;
345                 ino->i_op = &lafs_subset_ino_operations;
346                 ino->i_fop = &lafs_subset_file_operations;
347                 break;
348         }
349
350         case TypeInodeMap:
351         {
352                 struct inodemap_md *m = &li->md.inodemap;
353                 struct inodemap_metadata *s = &lai->metadata[0].inodemap;
354                 m->size = le32_to_cpu(s->size);
355                 m->thisblock = NoBlock;
356                 m->nextbit = 0;
357                 break;
358         }
359
360         case TypeSegmentMap:
361         {
362                 struct su_md *m = &li->md.segmentusage;
363                 struct su_metadata *s = &lai->metadata[0].segmentusage;
364                 m->table_size = le32_to_cpu(s->table_size);
365                 break;
366         }
367
368         case TypeQuota:
369         {
370                 struct quota_md *m = &li->md.quota;
371                 struct quota_metadata *s = &lai->metadata[0].quota;
372                 m->gracetime = le32_to_cpu(s->gracetime);
373                 m->graceunits = le32_to_cpu(s->graceunits);
374                 break;
375         }
376         case TypeOrphanList:
377         {
378                 struct orphan_md *m = &li->md.orphan;
379                 /* This will be set via lafs_count_orphans */
380                 m->nextfree = 0;
381                 m->reserved = 0;
382                 break;
383         }
384         case TypeAccessTime:
385                 break;
386
387         default: /* TypeBase or larger */
388         {
389                 struct file_md *i = &li->md.file;
390                 struct file_metadata *l = &lai->metadata[0].file;
391                 struct dir_metadata *d = &lai->metadata[0].dir;
392                 struct special_metadata *s = &lai->metadata[0].special;
393
394                 if (li->type < TypeBase)
395                         goto out;
396                 i->flags = le16_to_cpu(l->flags);
397                 ino->i_mode = le16_to_cpu(l->mode);
398                 ino->i_uid = le32_to_cpu(l->userid);
399                 ino->i_gid = le32_to_cpu(l->groupid);
400                 i->treeid = le32_to_cpu(l->treeid);
401                 i->creationtime = le64_to_cpu(l->creationtime);
402                 decode_time(&ino->i_mtime, le64_to_cpu(l->modifytime));
403                 decode_time(&ino->i_ctime, le64_to_cpu(l->ctime));
404                 decode_time(&i->i_accesstime, le64_to_cpu(l->accesstime));
405                 ino->i_atime = i->i_accesstime;
406                 i->atime_offset = 0; /* Will be filled-in later probably */
407                 lafs_add_atime_offset(&ino->i_atime, i->atime_offset);
408                 ino->i_size = le64_to_cpu(l->size);
409                 i->parent = le32_to_cpu(l->parent);
410                 ino->i_nlink = le32_to_cpu(l->linkcount);
411                 if (ino->i_nlink == 0 && list_empty(&b->orphans) &&
412                     fs_from_inode(ino)->rolled) {
413                         /* This block should already be on the orphan
414                          * list, otherwise there is a filesystem
415                          * inconsistency.
416                          * Either the orphan file is wrong, or the
417                          * linkcount is wrong.
418                          * It is safest to assume the later - either
419                          * way an FS check would be needed to fix it.
420                          * Note: while roll-forward is happening, this
421                          * situation is perfectly possible and is handled
422                          * correctly.
423                          */
424                         /* FIXME set a superblock flag requesting
425                          * directory linkage checking
426                          */
427                         ino->i_nlink = 1;
428                 }
429
430                 dprintk("  mode = 0%o uid %d size %lld\n",
431                         ino->i_mode, ino->i_uid, ino->i_size);
432                 switch (li->type) {
433                 case TypeFile:
434                         ino->i_op = &lafs_file_ino_operations;
435                         ino->i_fop = &lafs_file_file_operations;
436                         ino->i_mode = (ino->i_mode & 07777)  | S_IFREG;
437                         break;
438                 case TypeDir:
439                         i->seed = le32_to_cpu(d->hash_seed);
440                         ino->i_op = &lafs_dir_ino_operations;
441                         ino->i_fop = &lafs_dir_file_operations;
442                         ino->i_mode = (ino->i_mode & 07777)  | S_IFDIR;
443                         {
444                                 u32 *b = (u32 *)lai;
445                                 dprintk("Hmm. %d %d %d\n",
446                                         (int)b[24],
447                                         (int)b[25],
448                                         (int)b[26]);
449                         }
450                         break;
451                 case TypeSymlink:
452                         ino->i_op = &lafs_link_ino_operations;
453                         ino->i_mode = (ino->i_mode & 07777)  | S_IFLNK;
454                         break;
455                 case TypeSpecial:
456                         /* the data had better be in the inode ... */
457                         ino->i_rdev = MKDEV(le32_to_cpu(s->major),
458                                             le32_to_cpu(s->minor));
459                         ino->i_op = &lafs_special_ino_operations;
460                         init_special_inode(ino, ino->i_mode, ino->i_rdev);
461                         break;
462                 }
463                 break;
464         }
465         }
466
467         ino->i_blkbits = ino->i_sb->s_blocksize_bits;
468         /* FIXME i_blocks and i_byte - used for quota?? */
469         err = 0;
470
471         /* Note: no refcount yet.  Either will remove the reference to the
472          * other when freed
473          */
474         li->dblock = b;
475         rcu_assign_pointer(b->my_inode, ino);
476
477 out:
478         if (err && li->type)
479                 printk("inode %lu type is %d\n",
480                        (unsigned long)ino->i_ino, li->type);
481         unmap_dblock(b, lai);
482         return err;
483 }
484
485 static void check_atime_ref(struct inode *ino, int async)
486 {
487         /* If there is an time file in this filesystem the inode
488          * should hold a reference to the relevant block in
489          * that file.
490          */
491         struct inode *root, *at;
492         u32 bnum;
493         struct datablock *b;
494         if (async)
495                 /* Never bother for async lookups */
496                 return;
497         if (LAFSI(ino)->type < TypeBase)
498                 return;
499         if (test_bit(I_AccessTime, &LAFSI(ino)->iflags))
500                 return;
501         root = LAFSI(ino)->filesys;
502         at = LAFSI(root)->md.fs.accesstime;
503         if (at == NULL)
504                 return;
505
506         if (LAFSI(ino)->md.file.atime_offset)
507                 LAFSI(ino)->md.file.atime_offset = 0;
508
509         /* "* 2" to get byte number, then shift to get block
510          * number
511          */
512         bnum = ino->i_ino >> (at->i_blkbits-1);
513         b = lafs_get_block(at, bnum, NULL, GFP_NOFS, MKREF(atime));
514         if (b) {
515                 if (lafs_read_block(b) == 0) {
516                         u16 *atp;
517                         int i;
518                         atp = map_dblock(b);
519                         i = (ino->i_ino * 2) & ((1<<at->i_blkbits)-1);
520                         LAFSI(ino)->md.file.atime_offset = le16_to_cpu(atp[i]);
521                         set_bit(I_AccessTime, &LAFSI(ino)->iflags);
522                         unmap_dblock(b, atp);
523                         lafs_add_atime_offset(&ino->i_atime,
524                                               LAFSI(ino)->md.file.atime_offset);
525                 } else
526                         putdref(b, MKREF(atime));
527         }
528 }
529
530 void lafs_add_atime_offset(struct timespec *atime, int offset)
531 {
532         int expon;
533         time_t mantissa;
534         if (offset == 0)
535                 return;
536
537         expon = offset & 0x1f;
538         if (expon)
539                 mantissa = (offset >> 5) | 0x800;
540         else
541                 mantissa = (offset >> 5);
542         if (expon >= 11) {
543                 /* seconds */
544                 mantissa <<= expon-11;
545                 atime->tv_sec += mantissa;
546         } else {
547                 /* milliseconds */
548                 if (expon)
549                         mantissa <<= expon-1;
550                 timespec_add_ns(atime, (s64)mantissa * 1000000);
551         }
552 }
553
554 static int normalise(int *mantissa)
555 {
556         /* Shift down until value can be stored in 12 bits:
557          * Top bit will be '1', so only 11 bits needed.
558          * Not used on values below 2048.
559          */
560         int shift = 0;
561         while (*mantissa >= 4096) {
562                 *mantissa >>= 1;
563                 shift ++;
564         }
565         return shift;
566 }
567
568 static int update_atime_delta(struct inode *ino)
569 {
570         /* calculate new delta to show the difference between
571          * i_atime and i_accesstime
572          */
573         int rv;
574         if (LAFSI(ino)->type < TypeBase)
575                 return 0;
576         if (timespec_compare(&ino->i_atime,
577                              &LAFSI(ino)->md.file.i_accesstime) <= 0) {
578                 /* We cannot store negative delta so if i_atime is in the
579                  * past, just store zero
580                  */
581                 rv = 0;
582         } else {
583                 struct timespec diff;
584                 int shift;
585
586                 diff = timespec_sub(ino->i_atime,
587                                     LAFSI(ino)->md.file.i_accesstime);
588                 if (diff.tv_sec >= 2048) {
589                         /* Just store the seconds */
590                         rv = diff.tv_sec;
591                         shift = normalise(&rv) + 11;
592                 } else {
593                         /* Store the milliseconds */
594                         int rv = diff.tv_nsec / 1000000;
595                         rv += diff.tv_sec * 1000;
596                         if (rv >= 2048)
597                                 shift = normalise(&rv) + 1;
598                         else
599                                 shift = 0;
600                 }
601                 if (shift > 31)
602                         rv = 0xFFFF;
603                 else {
604                         rv &= 0x7ff;
605                         rv <<= 5;
606                         rv |= shift;
607                 }
608         }
609         if (LAFSI(ino)->md.file.atime_offset == rv)
610                 return 0;
611
612         LAFSI(ino)->md.file.atime_offset = rv;
613         return 1;
614 }
615
616 static void store_atime_delta(struct inode *ino)
617 {
618         struct inode *at;
619         u32 bnum;
620         struct datablock *b;
621         u16 *atp;
622         int i;
623
624         if (!test_bit(I_AccessTime, &LAFSI(ino)->iflags))
625                 /* sorry, nothing we can do here */
626                 return;
627
628         /* We own a reference, so this lookup must succeed */
629         at = LAFSI(LAFSI(ino)->filesys)->md.fs.accesstime;
630         bnum = ino->i_ino >> (at->i_blkbits-1);
631         b = lafs_get_block(at, bnum, NULL, GFP_NOFS, MKREF(store_atime));
632         BUG_ON(!b);
633         atp = map_dblock(b);
634         i = (ino->i_ino * 2) & ((1<<at->i_blkbits)-1);
635         if (le16_to_cpu(atp[i]) != LAFSI(ino)->md.file.atime_offset) {
636                 atp[i] = cpu_to_le16(LAFSI(ino)->md.file.atime_offset);
637                 lafs_dirty_dblock(b);
638         }
639         unmap_dblock(b, atp);
640         putdref(b, MKREF(store_atime));
641 }
642
643 void lafs_inode_checkpin(struct inode *ino)
644 {
645         /* Make sure I_Pinned is set correctly.
646          * It should be set precisely if i_nlink is non-zero,
647          * and ->iblock is B_Pinned.
648          * When it is set, we own a reference to the inode.
649          *
650          * This needs to be called whenever we change
651          * i_nlink, and whenever we pin or unpin an InoIdx
652          * block.
653          */
654         if (ino->i_nlink == 0) {
655                 /* I_Pinned should not be set */
656                 if (test_and_clear_bit(I_Pinned, &LAFSI(ino)->iflags)) {
657                         if (ino->i_sb->s_type == &lafs_fs_type)
658                                 iput(ino);
659                         else
660                                 lafs_iput_fs(ino);
661                 }
662         } else {
663                 /* Need to check if iblock is Pinned. */
664                 struct indexblock *ib = NULL;
665                 if (LAFSI(ino)->iblock) {
666                         spin_lock(&ino->i_data.private_lock);
667                         ib = LAFSI(ino)->iblock;
668                         if (ib && !test_bit(B_Pinned, &ib->b.flags))
669                                 ib = NULL;
670                         spin_unlock(&ino->i_data.private_lock);
671                 }
672                 if (ib) {
673                         if (!test_and_set_bit(I_Pinned, &LAFSI(ino)->iflags)) {
674                                 if (ino->i_sb->s_type == &lafs_fs_type)
675                                         igrab(ino);
676                                 else
677                                         lafs_igrab_fs(ino);
678                         }
679                 } else {
680                         if (test_and_clear_bit(I_Pinned, &LAFSI(ino)->iflags)) {
681                         if (ino->i_sb->s_type == &lafs_fs_type)
682                                 iput(ino);
683                         else
684                                 lafs_iput_fs(ino);
685                         }
686                 }
687         }
688 }
689
690 struct datablock *lafs_inode_get_dblock(struct inode *ino, REFARG)
691 {
692         struct datablock *db;
693
694         spin_lock(&ino->i_data.private_lock);
695         db = LAFSI(ino)->dblock;
696         if (db) {
697                 if (db->b.inode == ino)
698                         getdref_locked(db, REF);
699                 else {
700                         spin_lock_nested(&db->b.inode->i_data.private_lock, 1);
701                         getdref_locked(db, REF);
702                         spin_unlock(&db->b.inode->i_data.private_lock);
703                 }
704         }
705         spin_unlock(&ino->i_data.private_lock);
706         return db;
707 }
708
709 struct datablock *lafs_inode_dblock(struct inode *ino, int async, REFARG)
710 {
711         struct datablock *db;
712         int err;
713
714         db = lafs_inode_get_dblock(ino, REF);
715
716         if (!db)
717                 db = lafs_get_block(LAFSI(ino)->filesys, ino->i_ino, NULL,
718                                     GFP_KERNEL, REF);
719         if (!db)
720                 return ERR_PTR(-ENOMEM);
721
722         LAFSI(ino)->dblock = db;
723         rcu_assign_pointer(db->my_inode, ino);
724         if (async)
725                 err = lafs_read_block_async(db);
726         else
727                 err = lafs_read_block(db);
728         if (err == 0)
729                 return db;
730
731         putdref(db, REF);
732         return ERR_PTR(err);
733 }
734
735 void lafs_inode_init(struct datablock *b, int type, int mode, struct inode *dir)
736 {
737         /* A new block has been allocated in an inode file to hold an
738          * inode.  We get to fill in initial values so that when
739          * 'iget' calls lafs_import_inode, the correct inode is
740          * loaded.
741          */
742         struct fs *fs = fs_from_inode(b->b.inode);
743         struct la_inode *lai = map_dblock(b);
744         int size;
745
746         lai->data_blocks = cpu_to_le32(0);
747         lai->index_blocks = cpu_to_le32(0);
748         get_random_bytes(&lai->generation, sizeof(lai->generation));
749         lai->depth = 1;
750         lai->trunc_gen = 0;
751         lai->filetype = type;
752         lai->flags = 0;
753
754         switch(type) {
755         case TypeInodeFile:
756         {
757                 struct fs_metadata *l = &lai->metadata[0].fs;
758                 size = sizeof(struct fs_metadata);
759                 l->update_time = 0;
760                 l->blocks_used = 0;
761                 l->blocks_allowed = 0;
762                 l->creation_age = fs->wc[0].cluster_seq;
763                 l->inodes_used = 0;
764                 l->quota_inodes[0] = 0;
765                 l->quota_inodes[1] = 0;
766                 l->quota_inodes[2] = 0;
767                 l->snapshot_usage_table = 0;
768                 l->pad = 0;
769                 /* name will be zero length and not used */
770                 break;
771         }
772         case TypeInodeMap:
773         {
774                 struct inodemap_metadata *l = &lai->metadata[0].inodemap;
775                 l->size = 0;
776                 size = sizeof(struct inodemap_metadata);
777                 break;
778         }
779         case TypeSegmentMap:
780                 size = sizeof(struct su_metadata);
781                 break;
782         case TypeQuota:
783                 size = sizeof(struct quota_metadata);
784                 break;
785         case TypeOrphanList:
786                 size = 0;
787                 break;
788         case TypeAccessTime:
789                 size = 0;
790                 break;
791         default:
792         {
793                 struct file_metadata *l = &lai->metadata[0].file;
794                 struct timespec now = CURRENT_TIME;
795
796                 l->flags = cpu_to_le16(0);
797                 l->userid = cpu_to_le32(current->cred->fsuid);
798                 if (dir && (dir->i_mode & S_ISGID)) {
799                         l->groupid = cpu_to_le32(dir->i_gid);
800                         if (type == TypeDir)
801                                 mode |= S_ISGID;
802                 } else
803                         l->groupid = cpu_to_le32(current->cred->fsgid);
804                 if (dir && LAFSI(dir)->md.file.treeid)
805                         l->treeid = cpu_to_le32(LAFSI(dir)->md.file.treeid);
806                 else
807                         l->treeid = l->userid;
808
809                 l->mode = cpu_to_le16(mode);
810                 l->creationtime = encode_time(&now);
811                 l->modifytime = l->creationtime;
812                 l->ctime = l->creationtime;
813                 l->accesstime = l->creationtime;
814                 l->size = 0;
815                 l->parent = dir ? cpu_to_le32(dir->i_ino) : 0;
816                 l->linkcount = 0;
817                 l->attrinode = 0;
818                 if (type == TypeDir) {
819                         struct dir_metadata *l = &lai->metadata[0].dir;
820                         u32 seed;
821                         get_random_bytes(&seed,
822                                          sizeof(seed));
823                         seed = (seed & ~7) | 1;
824                         l->hash_seed = cpu_to_le32(seed);
825                         size = sizeof(struct dir_metadata);
826                 } else if (type == TypeSpecial) {
827                         struct special_metadata *s = &lai->metadata[0].special;
828                         s->major = s->minor = 0;
829                         size = sizeof(struct special_metadata);
830                 } else
831                         size = sizeof(struct file_metadata);
832         }
833         }
834         size += sizeof(struct la_inode);
835         lai->metadata_size = cpu_to_le32(size);
836         memset(((char *)lai)+size, 0, fs->blocksize-size);
837         *(u16 *)(((char *)lai)+size) = cpu_to_le16(IBLK_EXTENT);
838
839         unmap_dblock(b, lai);
840         set_bit(B_Valid, &b->b.flags);
841         LAFS_BUG(!test_bit(B_Pinned, &b->b.flags), &b->b);
842         lafs_dirty_dblock(b);
843 }
844
845 static int inode_map_free(struct fs *fs, struct inode *fsys, u32 inum);
846
847 void lafs_evict_inode(struct inode *ino)
848 {
849         struct fs *fs = fs_from_inode(ino);
850         struct lafs_inode *li = LAFSI(ino);
851
852         if (ino->i_mode == 0) {
853                 /* There never was an inode here,
854                  * so nothing to do.
855                  * We just call end_writeback to get the
856                  * flags set properly.
857                  */
858                 end_writeback(ino);
859                 return;
860         }
861
862         dprintk("EVICT INODE %d\n", (int)ino->i_ino);
863
864
865         /* Normal truncation holds an igrab, so we cannot be
866          * deleted until any truncation finishes
867          */
868         BUG_ON(test_bit(I_Trunc, &LAFSI(ino)->iflags));
869
870         if (ino->i_nlink == 0) {
871                 struct datablock *b =
872                         lafs_inode_dblock(ino, SYNC, MKREF(delete_inode));
873                 i_size_write(ino, 0);
874                 truncate_inode_pages(&ino->i_data, 0);
875                 LAFSI(ino)->trunc_next = 0;
876                 set_bit(I_Deleting, &LAFSI(ino)->iflags);
877                 set_bit(I_Trunc, &LAFSI(ino)->iflags);
878                 lafs_igrab_fs(ino);
879                 if (!IS_ERR(b)) {
880                         set_bit(B_Claimed, &b->b.flags);
881                         lafs_add_orphan(fs, b);
882                         dprintk("PUNCH hole for %d\n", (int)b->b.fileaddr);
883                         putdref(b, MKREF(delete_inode));
884                 }
885                 inode_map_free(fs, LAFSI(ino)->filesys,  ino->i_ino);
886         } else
887                 truncate_inode_pages(&ino->i_data, 0);
888         end_writeback(ino);
889
890         dprintk("CLEAR INODE %d\n", (int)ino->i_ino);
891
892         li->type = 0;
893
894         /* Now is a good time to break the linkage between
895          * inode and dblock - but not if the file is
896          * being deleted
897          */
898         if (!test_bit(I_Deleting, &li->iflags)) {
899                 struct datablock *db;
900                 spin_lock(&ino->i_data.private_lock);
901                 db = li->dblock;
902                 if (db) {
903                         struct indexblock *ib = li->iblock;
904                         LAFS_BUG(ib && atomic_read(&ib->b.refcnt), &db->b);
905                         db->my_inode = NULL;
906                         li->dblock = NULL;
907                         li->iblock = NULL;
908                 }
909                 spin_unlock(&ino->i_data.private_lock);
910         }
911
912         /* FIXME release quota inodes if filesystem */
913 }
914
915 static int prune(void *data, u32 addr, u64 paddr, int len)
916 {
917         /* This whole index block is being pruned, just account
918          * for everything and it will be cleared afterwards
919          */
920         struct indexblock *ib = data;
921         struct inode *ino = ib->b.inode;
922         struct fs *fs = fs_from_inode(ino);
923         int ph = !!test_bit(B_Phase1, &ib->b.flags);
924         int i;
925         dprintk("PRUNE %d for %d at %lld\n", addr, len, (long long)paddr);
926         if (paddr == 0 || len == 0)
927                 return 0;
928         for (i = 0 ; i < len ; i++)
929                 lafs_summary_update(fs, ino, paddr+i, 0, 0, ph, 0);
930         return len;
931 }
932
933 static int prune_some(void *data, u32 addr, u64 paddr, int len)
934 {
935         /* Part of this index block is being pruned.  Copy
936          * what addresses we can into uninc_table so that
937          * it can be 'incorporated'
938          * We should probably share some code with
939          * lafs_allocated_block??
940          */
941         struct indexblock *ib = data;
942         struct inode *ino = ib->b.inode;
943         struct fs *fs = fs_from_inode(ino);
944         int ph = !!test_bit(B_Phase1, &ib->b.flags);
945         int i;
946
947         if (paddr == 0 || len == 0)
948                 return 0;
949         dprintk("PRUNE2 %d for %d at %lld\n", addr, len, (long long)paddr);
950         for (i = 0 ; i < len ; i++) {
951                 /* FIXME should allow longer truncation ranges in uninc_table
952                  * as they are easy to handle.
953                  */
954                 struct addr *a;
955                 if (addr + i < LAFSI(ino)->trunc_next)
956                         continue;
957                 spin_lock(&ino->i_data.private_lock);
958                 a = &ib->uninc_table.pending_addr
959                         [ib->uninc_table.pending_cnt - 1];
960                 if (ib->uninc_table.pending_cnt <
961                     ARRAY_SIZE(ib->uninc_table.pending_addr)) {
962                         a++;
963                         a->fileaddr = addr + i;
964                         a->physaddr = 0;
965                         a->cnt = 1;
966                         LAFS_BUG(!test_bit(B_Pinned, &ib->b.flags), &ib->b);
967                         ib->uninc_table.pending_cnt++;
968                 } else {
969                         spin_unlock(&ino->i_data.private_lock);
970                         break;
971                 }
972                 spin_unlock(&ino->i_data.private_lock);
973                 lafs_summary_update(fs, ino, paddr+i, 0, 0, ph, 0);
974         }
975         return i;
976 }
977
978 int lafs_inode_handle_orphan(struct datablock *b)
979 {
980         /* Don't need rcu protection for my_inode run_orphan
981          * holds a reference
982          */
983         struct indexblock *ib, *ib2;
984         struct inode *ino = b->my_inode;
985         struct fs *fs = fs_from_inode(ino);
986         u32 trunc_next, next_trunc;
987         int loop_cnt = 20;
988         int err = -ENOMEM;
989
990         if (!test_bit(I_Trunc, &LAFSI(ino)->iflags)) {
991                 if (test_bit(I_Deleting, &LAFSI(ino)->iflags)) {
992                         LAFS_BUG(ino->i_nlink, &b->b);
993                         if (LAFSI(ino)->cblocks +
994                             LAFSI(ino)->pblocks +
995                             LAFSI(ino)->ablocks +
996                             LAFSI(ino)->ciblocks +
997                             LAFSI(ino)->piblocks)
998                         printk("Deleting inode %lu: %ld+%ld+%ld %ld+%ld\n",
999                                ino->i_ino,
1000                                LAFSI(ino)->cblocks,
1001                                LAFSI(ino)->pblocks,
1002                                LAFSI(ino)->ablocks,
1003                                LAFSI(ino)->ciblocks,
1004                                LAFSI(ino)->piblocks);
1005                         BUG_ON(LAFSI(ino)->cblocks +
1006                                LAFSI(ino)->pblocks +
1007                                LAFSI(ino)->ablocks +
1008                                LAFSI(ino)->ciblocks +
1009                                LAFSI(ino)->piblocks);
1010                         if (lafs_erase_dblock_async(b))
1011                                 lafs_orphan_release(fs, b);
1012                 } else if (ino->i_nlink || LAFSI(ino)->type == 0)
1013                         lafs_orphan_release(fs, b);
1014                 else
1015                         lafs_orphan_forget(fs, b);
1016                 return 0;
1017         }
1018
1019         ib = lafs_make_iblock(ino, ADOPT, SYNC, MKREF(inode_handle_orphan));
1020         if (IS_ERR(ib))
1021                 return PTR_ERR(ib);
1022
1023         /* Here is the guts of 'truncate'.  We find the next leaf index
1024          * block and discard all the addresses there-in.
1025          */
1026         trunc_next = LAFSI(ino)->trunc_next;
1027
1028         if (trunc_next == 0xFFFFFFFF) {
1029                 /* truncate has finished in that all data blocks
1030                  * have been removed and all index block are either
1031                  * gone or pending incorporation at which point they will
1032                  * go.
1033                  * If we hit a phase change, we will need to postpone
1034                  * the rest of the cleaning until it completes.
1035                  * If there is a checkpoint happening, then all the work
1036                  * that we can do now, it will do for us.  So just
1037                  * let it.
1038                  */
1039                 struct indexblock *tmp;
1040                 struct indexblock *next;
1041                 u32 lastaddr;
1042
1043                 if (!test_bit(B_Pinned, &ib->b.flags)) {
1044                         /* must be finished */
1045                         LAFS_BUG(test_bit(B_Dirty, &ib->b.flags), &ib->b);
1046                         clear_bit(I_Trunc, &LAFSI(ino)->iflags);
1047                         lafs_iput_fs(ino);
1048                         wake_up(&fs->trunc_wait);
1049                         err = -ERESTARTSYS;
1050                         goto out2;
1051                 }
1052                 if (fs->checkpointing) {
1053                         /* This cannot happen with current code,
1054                          * but leave it in case we ever have
1055                          * orphan handling parallel with checkpoints
1056                          */
1057                         err = -EBUSY; /* Try again after the checkpoint */
1058                         goto out2;
1059                 }
1060
1061                 lastaddr = (i_size_read(ino) +
1062                             fs->blocksize - 1)
1063                         >> fs->blocksize_bits;
1064                 /* Find a Pinned descendent of ib which has no
1065                  * Pinned descendents and no PrimaryRef dependent
1066                  * (so take the last).
1067                  * Prefer blocks that are beyond EOF (again, take the last).
1068                  * If there are none, descend the last block that
1069                  * is not after EOF and look at its children.
1070                  */
1071                 ib2 = next = ib;
1072                 spin_lock(&ib->b.inode->i_data.private_lock);
1073                 while (next) {
1074                         ib2 = next;
1075                         next = NULL;
1076                         list_for_each_entry(tmp, &ib2->children, b.siblings) {
1077                                 if (!test_bit(B_Index, &tmp->b.flags) ||
1078                                     !test_bit(B_Pinned, &tmp->b.flags))
1079                                         continue;
1080                                 if (next == NULL ||
1081                                     tmp->b.fileaddr > next->b.fileaddr)
1082                                         next = tmp;
1083                         }
1084                 }
1085                 if (ib2->b.fileaddr < lastaddr) {
1086                         /* Must be all done */
1087                         spin_unlock(&ib->b.inode->i_data.private_lock);
1088                         clear_bit(I_Trunc, &LAFSI(ino)->iflags);
1089                         lafs_iput_fs(ino);
1090                         wake_up(&fs->trunc_wait);
1091                         err = -ERESTARTSYS;
1092                         goto out2;
1093                 }
1094                 getiref(ib2, MKREF(inode_handle_orphan2));
1095                 spin_unlock(&ib->b.inode->i_data.private_lock);
1096
1097                 /* ib2 is an index block beyond EOF with no
1098                  * Pinned children.
1099                  * Incorporating it should unpin it.
1100                  */
1101                 if (!list_empty(&ib2->children)) {
1102                         lafs_print_tree(&ib2->b, 3);
1103                         LAFS_BUG(1, &ib2->b);
1104                 }
1105
1106                 if (!lafs_iolock_written_async(&ib2->b)) {
1107                         putiref(ib2, MKREF(inode_handle_orphan2));
1108                         err = -EAGAIN;
1109                         goto out2;
1110                 }
1111                 while (ib2->uninc_table.pending_cnt || ib2->uninc)
1112                         lafs_incorporate(fs, ib2);
1113
1114                 if (test_bit(B_Dirty, &ib2->b.flags) ||
1115                     test_bit(B_Realloc, &ib2->b.flags))
1116                         lafs_cluster_allocate(&ib2->b, 0);
1117                 else
1118                         lafs_iounlock_block(&ib2->b);
1119
1120                 if (!list_empty(&ib2->b.siblings)) {
1121                         printk("looping on %s\n", strblk(&ib2->b));
1122                         loop_cnt--;
1123                         if (loop_cnt < 0)
1124                                 BUG();
1125                 }
1126                 putiref(ib2, MKREF(inode_handle_orphan2));
1127                 err = -ERESTARTSYS;
1128                 if (ib->uninc) {
1129                         if (lafs_iolock_written_async(&ib->b)) {
1130                                 while (ib->uninc)
1131                                         lafs_incorporate(fs, ib);
1132                                 lafs_iounlock_block(&ib->b);
1133                         } else
1134                                 err = -EAGAIN;
1135                 }
1136         out2:
1137                 putiref(ib, MKREF(inode_handle_orphan));
1138                 return err;
1139         }
1140
1141         putiref(ib, MKREF(inode_handle_orphan));
1142
1143         ib = lafs_leaf_find(ino, trunc_next, ADOPT, &next_trunc,
1144                             ASYNC, MKREF(inode_handle_orphan3));
1145         if (IS_ERR(ib))
1146                 return PTR_ERR(ib);
1147         /* now hold an iolock on ib */
1148
1149         /* Ok, trunc_next seems to refer to a block that exists.
1150          * We need to erase it..
1151          *
1152          * So we open up the index block ourselves, call
1153          * lafs_summary_update with each block address, and then
1154          * erase the block.
1155          */
1156
1157         if (LAFSI(ino)->depth == 0) {
1158                 /* Nothing to truncate */
1159                 clear_bit(I_Trunc, &LAFSI(ino)->iflags);
1160                 lafs_iput_fs(ino);
1161                 if (test_bit(B_Pinned, &ib->b.flags))
1162                         /* Need to move the dirtiness which keeps this
1163                          * pinned to the data block.
1164                          */
1165                         lafs_cluster_allocate(&ib->b, 0);
1166                 else
1167                         lafs_iounlock_block(&ib->b);
1168                 err = -ERESTARTSYS;
1169                 goto out_put;
1170         }
1171
1172         lafs_checkpoint_lock(fs);
1173         err = lafs_reserve_block(&ib->b, ReleaseSpace);
1174         if (err < 0)
1175                 goto out;
1176
1177         if (!test_bit(B_Valid, &ib->b.flags) &&
1178             test_bit(B_InoIdx, &ib->b.flags)) {
1179                 /* still invalid, just re-erase to remove
1180                  * pinning */
1181                 LAFSI(ino)->trunc_next = next_trunc;
1182                 lafs_cluster_allocate(&ib->b, 0);
1183                 err = -ERESTARTSYS;
1184                 goto out_unlocked;
1185         }
1186
1187         lafs_pin_block(&ib->b);
1188
1189         /* It might be that this can happen, in which case
1190          * we simply update trunc_next and loop.  But I'd like
1191          * to be sure before I implement that
1192          */
1193         if (!test_bit(B_Valid, &ib->b.flags)) {
1194                 printk("Not Valid: %s\n", strblk(&ib->b));
1195                 printk("depth = %d\n", LAFSI(ino)->depth);
1196                 if (test_bit(B_InoIdx, &ib->b.flags))
1197                         printk("DB: %s\n", strblk(&LAFSI(ib->b.inode)->dblock->b));
1198                 LAFSI(ino)->trunc_next = next_trunc;
1199                 //BUG_ON(!test_bit(B_Valid, &ib->b.flags));
1200                 err = -ERESTARTSYS;
1201                 goto out;
1202         }
1203
1204         if (ib->b.fileaddr < trunc_next &&
1205             lafs_leaf_next(ib, 0) < trunc_next) {
1206                 /* We only want to truncate part of this index block.
1207                  * So we copy addresses into uninc_table and then
1208                  * call lafs_incorporate.
1209                  * This might cause the index tree to grow, so we
1210                  * cannot trust next_trunc
1211                  */
1212                 if (ib->uninc_table.pending_cnt == 0 &&
1213                     ib->uninc == NULL) {
1214                         lafs_dirty_iblock(ib, 0);
1215                         /* FIXME this just removes 8 blocks at a time,
1216                          * which is not enough
1217                          */
1218                         lafs_walk_leaf_index(ib, prune_some, ib);
1219                 }
1220                 if (test_bit(B_Dirty, &ib->b.flags))
1221                         lafs_incorporate(fs, ib);
1222                 err = -ERESTARTSYS;
1223                 goto out;
1224         }
1225         LAFSI(ino)->trunc_next = next_trunc;
1226
1227         while (ib->uninc_table.pending_cnt || ib->uninc) {
1228                 /* There should be no Realloc data blocks here
1229                  * but index blocks might be realloc still.
1230                  */
1231                 LAFS_BUG(!test_bit(B_Dirty, &ib->b.flags) &&
1232                          !test_bit(B_Realloc, &ib->b.flags), &ib->b);
1233                 lafs_incorporate(fs, ib);
1234         }
1235         if (test_bit(B_InoIdx, &ib->b.flags) ||
1236             !test_bit(B_PhysValid, &ib->b.flags) ||
1237             ib->b.physaddr != 0) {
1238                 lafs_walk_leaf_index(ib, prune, ib);
1239                 lafs_clear_index(ib);
1240                 lafs_dirty_iblock(ib, 0);
1241         }
1242         if (test_bit(B_Dirty, &ib->b.flags))
1243                 lafs_incorporate(fs, ib);
1244         if (!list_empty(&ib->children))
1245                 lafs_print_tree(&ib->b, 2);
1246         LAFS_BUG(!list_empty(&ib->children), &ib->b);
1247         err = -ERESTARTSYS;
1248 out:
1249         lafs_iounlock_block(&ib->b);
1250 out_unlocked:
1251         lafs_checkpoint_unlock(fs);
1252 out_put:
1253         putiref(ib, MKREF(inode_handle_orphan3));
1254         return err;
1255 }
1256
1257 void lafs_dirty_inode(struct inode *ino)
1258 {
1259         /* this is called in one of three cases:
1260          * 1/ by lafs internally when dblock or iblock is pinned and
1261          *    ready to be dirtied
1262          * 2/ by writeout before requesting a write - to update mtime
1263          * 3/ by read to update atime
1264          *
1265          * We want to handle atime updates carefully as they may not change
1266          * the stored inode itself.
1267          * For all other updates, the inode dblock exists and is pinned.
1268          * In those cases we will be updating the inode and so can store
1269          * the atime exactly.
1270          * For an atime update, the dblock may not exists, or may not be
1271          * Pinned.  If it isn't then we don't want to make the inode dirty
1272          * but only want to update the delta stored in the atime file.
1273          * The block for that should already be pinned.
1274          *
1275          *
1276          * We mustn't update the data block as it could be in
1277          * writeout and we cannot always wait safely.
1278          * So require that anyone who really cares, dirties the datablock
1279          * or a child themselves.
1280          * When cluster_allocate eventually gets called, it will update
1281          * the datablock from the inode.
1282          * If an update has to wait for the next phase, lock_dblock
1283          * (e.g. in setattr) will do that.
1284          *
1285          * We also use this opportunity to update the filesystem modify time.
1286          */
1287         struct timespec now;
1288         struct inode *filesys;
1289         int atime_only = 1;
1290
1291         if (LAFSI(ino)->dblock) {
1292                 struct datablock *db;
1293                 spin_lock(&ino->i_data.private_lock);
1294                 db = LAFSI(ino)->dblock;
1295                 if (db && test_bit(B_Pinned, &db->b.flags))
1296                         atime_only = 0;
1297                 spin_unlock(&ino->i_data.private_lock);
1298         }
1299
1300         if (atime_only) {
1301                 if (update_atime_delta(ino))
1302                         store_atime_delta(ino);
1303                 return;
1304         }
1305
1306         set_bit(I_Dirty, &LAFSI(ino)->iflags);
1307         ino->i_sb->s_dirt = 1;
1308
1309         if (LAFSI(ino)->type < TypeBase)
1310                 return;
1311         LAFSI(ino)->md.file.i_accesstime = ino->i_atime;
1312         if (LAFSI(ino)->md.file.atime_offset) {
1313                 LAFSI(ino)->md.file.atime_offset = 0;
1314                 store_atime_delta(ino);
1315         }
1316
1317         now = current_fs_time(ino->i_sb);
1318         filesys = LAFSI(ino)->filesys;
1319         if (!timespec_equal(&filesys->i_mtime, &now)) {
1320                 filesys->i_mtime = now;
1321                 set_bit(I_Dirty, &LAFSI(filesys)->iflags);
1322         }
1323 }
1324
1325 int lafs_sync_inode(struct inode *ino, int wait)
1326 {
1327         /* fsync has been called on this file so we need
1328          * to sync any inode updates to the next cluster.
1329          *
1330          * If we cannot create an update record,
1331          * we wait for a phase change, which writes everything
1332          * out.
1333          */
1334         struct datablock *b;
1335         struct fs *fs = fs_from_inode(ino);
1336         struct update_handle uh;
1337         int err;
1338
1339         if (wait) {
1340                 if (LAFSI(ino)->update_cluster > 1)
1341                         lafs_cluster_wait(fs, LAFSI(ino)->update_cluster);
1342                 if (LAFSI(ino)->update_cluster == 1) {
1343                         lafs_checkpoint_lock(fs);
1344                         lafs_checkpoint_unlock_wait(fs);
1345                 }
1346                 return 0;
1347         }
1348
1349         LAFSI(ino)->update_cluster = 0;
1350         if (!test_bit(I_Dirty, &LAFSI(ino)->iflags))
1351                 return 0;
1352         b = lafs_inode_dblock(ino, SYNC, MKREF(write_inode));
1353         if (IS_ERR(b))
1354                 return PTR_ERR(b);
1355
1356         lafs_iolock_written(&b->b);
1357         lafs_inode_fillblock(ino);
1358         lafs_iounlock_block(&b->b);
1359
1360         err = lafs_cluster_update_prepare(&uh, fs, LAFS_INODE_LOG_SIZE);
1361         if (err)
1362                 lafs_cluster_update_abort(&uh);
1363         else {
1364                 lafs_checkpoint_lock(fs);
1365                 if (lafs_cluster_update_pin(&uh) == 0) {
1366                         if (test_and_clear_bit(B_Dirty, &b->b.flags))
1367                                 lafs_space_return(fs, 1);
1368                         LAFSI(ino)->update_cluster =
1369                                 lafs_cluster_update_commit
1370                                 (&uh, b, LAFS_INODE_LOG_START,
1371                                  LAFS_INODE_LOG_SIZE);
1372                 } else  
1373                         lafs_cluster_update_abort(&uh);
1374                 lafs_checkpoint_unlock(fs);
1375         }
1376         if (test_bit(B_Dirty, &b->b.flags)) {
1377                 /* FIXME need to write out the data block...
1378                  * Is that just lafs_cluster_allocate ?
1379                  */
1380         }
1381
1382         if (LAFSI(ino)->update_cluster == 0) {
1383                 lafs_checkpoint_lock(fs);
1384                 if (test_bit(B_Dirty, &b->b.flags))
1385                         LAFSI(ino)->update_cluster = 1;
1386                 lafs_checkpoint_start(fs);
1387                 lafs_checkpoint_unlock(fs);
1388         }
1389         putdref(b, MKREF(write_inode));
1390         return 0; /* FIXME should I return some error message??? */
1391 }
1392
1393 void lafs_inode_fillblock(struct inode *ino)
1394 {
1395         /* copy data from ino into the related data block */
1396
1397         struct lafs_inode *li = LAFSI(ino);
1398         struct datablock *db = li->dblock;
1399         struct la_inode *lai;
1400
1401         clear_bit(I_Dirty, &LAFSI(ino)->iflags);
1402
1403         lai = map_dblock(db);
1404         lai->data_blocks = cpu_to_le32(li->cblocks);
1405         lai->index_blocks = cpu_to_le32(li->ciblocks);
1406         lai->generation = cpu_to_le16(ino->i_generation);
1407         lai->trunc_gen = li->trunc_gen;
1408         lai->flags = li->flags;
1409         lai->filetype = li->type;
1410         if (lai->metadata_size != cpu_to_le16(li->metadata_size)) {
1411                 /* Changing metadata size is wierd.
1412                  * We will need to handle this somehow for xattrs
1413                  * For now we just want to cope with
1414                  * Dir -> InodeFile changes, and that guarantees us
1415                  * there is no index info - so just clear the index 
1416                  * area.
1417                  */
1418                 u16 *s = (u16*)(((char*)lai) + li->metadata_size);
1419                 BUG_ON(li->type != TypeInodeFile);
1420                 lai->metadata_size = cpu_to_le16(li->metadata_size);
1421                 memset(s, 0, ino->i_sb->s_blocksize - li->metadata_size);
1422                 *s = cpu_to_le16(IBLK_INDIRECT);
1423         }
1424         lai->depth = li->depth;
1425
1426         switch (li->type) {
1427         case TypeInodeFile:
1428         {
1429                 struct fs_md *i = &li->md.fs;
1430                 struct fs_metadata *l = &lai->metadata[0].fs;
1431                 int nlen;
1432
1433                 l->snapshot_usage_table = cpu_to_le16(i->usagetable);
1434                 l->update_time = cpu_to_le64(encode_time(&ino->i_mtime));
1435                 l->blocks_used = cpu_to_le64(i->cblocks_used);
1436                 l->blocks_allowed = cpu_to_le64(i->blocks_allowed);
1437                 l->creation_age = cpu_to_le64(i->creation_age);
1438                 l->inodes_used = cpu_to_le32(i->inodes_used);
1439                 l->quota_inodes[0] = cpu_to_le32(i->quota_inums[0]);
1440                 l->quota_inodes[1] = cpu_to_le32(i->quota_inums[1]);
1441                 l->quota_inodes[2] = cpu_to_le32(i->quota_inums[2]);
1442                 nlen = lai->metadata_size - offsetof(struct la_inode,
1443                                                      metadata[0].fs.name);
1444                 memset(l->name, 0, nlen);
1445                 if (i->name == NULL)
1446                         nlen = 0;
1447                 else if (strlen(i->name) < nlen)
1448                         nlen = strlen(i->name);
1449                 memcpy(l->name, i->name, nlen);
1450                 break;
1451         }
1452
1453         case TypeInodeMap:
1454         {
1455                 struct inodemap_md *m = &li->md.inodemap;
1456                 struct inodemap_metadata *s = &lai->metadata[0].inodemap;
1457                 s->size = cpu_to_le32(m->size);
1458                 break;
1459         }
1460
1461         case TypeSegmentMap:
1462         {
1463                 struct su_md *m = &li->md.segmentusage;
1464                 struct su_metadata *s = &lai->metadata[0].segmentusage;
1465                 s->table_size = cpu_to_le32(m->table_size);
1466                 break;
1467         }
1468
1469         case TypeQuota:
1470         {
1471                 struct quota_md *m = &li->md.quota;
1472                 struct quota_metadata *s = &lai->metadata[0].quota;
1473                 s->gracetime = cpu_to_le32(m->gracetime);
1474                 s->graceunits = cpu_to_le32(m->graceunits);
1475                 break;
1476         }
1477         case TypeOrphanList:
1478         case TypeAccessTime:
1479                 break;
1480
1481         default: /* TypeBase or larger */
1482         {
1483                 struct file_md *i = &li->md.file;
1484                 struct file_metadata *l = &lai->metadata[0].file;
1485                 struct dir_metadata *d = &lai->metadata[0].dir;
1486                 struct special_metadata *s = &lai->metadata[0].special;
1487
1488                 if (li->type < TypeBase)
1489                         break;
1490                 l->flags = cpu_to_le16(i->flags);
1491                 l->mode = cpu_to_le16(ino->i_mode);
1492                 l->userid = cpu_to_le32(ino->i_uid);
1493                 l->groupid = cpu_to_le32(ino->i_gid);
1494                 l->treeid = cpu_to_le32(i->treeid);
1495                 l->creationtime = cpu_to_le64(i->creationtime);
1496                 l->modifytime = cpu_to_le64(encode_time(&ino->i_mtime));
1497                 l->ctime = cpu_to_le64(encode_time(&ino->i_ctime));
1498                 l->accesstime = cpu_to_le64(encode_time(&i->i_accesstime));
1499                 l->size = cpu_to_le64(ino->i_size);
1500                 l->parent = cpu_to_le32(i->parent);
1501                 l->linkcount = cpu_to_le32(ino->i_nlink);
1502
1503                 switch (li->type) {
1504                 case TypeFile:
1505                         break;
1506                 case TypeDir:
1507                         d->hash_seed = cpu_to_le32(i->seed);
1508                         break;
1509                 case TypeSymlink:
1510                         break;
1511                 case TypeSpecial:
1512                         s->major = cpu_to_le32(MAJOR(ino->i_rdev));
1513                         s->minor = cpu_to_le32(MINOR(ino->i_rdev));
1514                         break;
1515                 }
1516         }
1517         }
1518         unmap_dblock(db, lai);
1519 }
1520
1521 /*-----------------------------------------------------------------------
1522  * Inode allocate map handling.
1523  * Inode 1 of each fileset is a bitmap of free inode numbers.
1524  * Whenever the file is extended in size, new bits are set to one.  They
1525  * are then cleared when the inode is allocated.  When a block becomes
1526  * full of zeros, we don't need to store it any more.
1527  *
1528  * We don't clear the bit until we are committed to creating an inode
1529  * This means we cannot clear it straight away, so two different threads
1530  * might see the same inode number as being available.  We have two
1531  * approaches to guard against this.
1532  * Firstly we have a 'current' pointer into the inodemap file and
1533  * increase that past the inode we return.  This discourages multiple
1534  * hits but as the pointer would need to be rewound occasionally it
1535  * isn't a guarantee.  The guarantee against multiple allocations is done
1536  * via a flag in the block representing an inode.  This is set
1537  * while an inode is being allocated.
1538  */
1539
1540 /* inode number allocation has the prealloc/pin/commit/abort structure
1541  * so it can be committed effectively
1542  */
1543
1544 static int
1545 choose_free_inum(struct fs *fs, struct inode *fsys, u32 *inump,
1546                  struct datablock **bp, int *restarted)
1547 {
1548         struct inode *im = lafs_iget(fsys, 1, SYNC);
1549         loff_t bnum;
1550         struct datablock *b;
1551         char *buf;
1552         int err;
1553         int bit;
1554
1555         if (*bp) {
1556                 struct inode *i = (*bp)->b.inode;
1557                 putdref(*bp, MKREF(cfi_map));
1558                 iput(i);
1559                 *bp = NULL;
1560         }
1561
1562         mutex_lock_nested(&im->i_mutex, I_MUTEX_QUOTA);
1563 retry:
1564         bnum = LAFSI(im)->md.inodemap.thisblock;
1565
1566         if (bnum == NoBlock ||
1567             LAFSI(im)->md.inodemap.nextbit >= (fs->blocksize<<3)) {
1568                 if (bnum == NoBlock)
1569                         bnum = LAFSI(im)->md.inodemap.size;
1570
1571                 if (bnum+1 < LAFSI(im)->md.inodemap.size)
1572                         bnum++;
1573                 else if (!*restarted) {
1574                         bnum = 0;
1575                         *restarted = 1;
1576                 } else {
1577                         /* Need to add a new block to the file */
1578                         bnum = LAFSI(im)->md.inodemap.size;
1579                         b = lafs_get_block(im, bnum, NULL, GFP_KERNEL,
1580                                            MKREF(cfi_map));
1581                         err = -ENOMEM;
1582                         if (!b)
1583                                 goto abort;
1584                         lafs_iolock_written(&b->b);
1585                         set_bit(B_PinPending, &b->b.flags);
1586                         lafs_iounlock_block(&b->b);
1587                 retry2:
1588                         lafs_checkpoint_lock(fs);
1589                         err = lafs_pin_dblock(b, NewSpace);
1590                         if (err == -EAGAIN) {
1591                                 lafs_checkpoint_unlock_wait(fs);
1592                                 goto retry2;
1593                         }
1594                         if (err < 0)
1595                                 goto abort_unlock;
1596
1597                         buf = map_dblock(b);
1598                         /* Set block to "all are free" */
1599                         memset(buf, 0xff, fs->blocksize);
1600                         unmap_dblock(b, buf);
1601                         set_bit(B_Valid, &b->b.flags);
1602                         LAFSI(im)->md.inodemap.size = bnum+1;
1603                         lafs_dirty_inode(im);
1604                         lafs_dirty_dblock(b);
1605                         lafs_checkpoint_unlock(fs);
1606                         putdref(b, MKREF(cfi_map));
1607                 }
1608                 b = NULL;
1609                 err = lafs_find_next(im, &bnum);
1610                 if (err < 0)
1611                         goto abort;
1612                 if (err == 0)
1613                         bnum = 0;
1614
1615                 LAFSI(im)->md.inodemap.nextbit = 0;
1616                 LAFSI(im)->md.inodemap.thisblock = bnum;
1617                 goto retry;
1618         }
1619         b = lafs_get_block(im, bnum, NULL, GFP_KERNEL, MKREF(cfi_map));
1620         err = -ENOSPC;
1621         if (!b)
1622                 goto abort;
1623         err = lafs_find_block(b, NOADOPT);
1624         if (err)
1625                 goto abort;
1626         if (b->b.physaddr == 0 && !test_bit(B_Valid, &b->b.flags)) {
1627                 LAFSI(im)->md.inodemap.nextbit =
1628                         (fs->blocksize<<3) + 1;
1629                 putdref(b,MKREF(cfi_map));
1630                 goto retry;
1631         }
1632         err = lafs_read_block(b);
1633         if (err)
1634                 goto abort;
1635
1636         bit = LAFSI(im)->md.inodemap.nextbit;
1637         LAFSI(im)->md.inodemap.thisblock = bnum;
1638         buf = map_dblock(b);
1639         while (bnum == 0 && bit < 16) {
1640                 /* Never return an inum below 16 - they are special */
1641                 if (!generic_test_le_bit(bit, (unsigned long *)buf))
1642                         generic___clear_le_bit(bit, (unsigned long *)buf);
1643                 bit++;
1644         }
1645
1646         bit = generic_find_next_le_bit((unsigned long *)buf,
1647                                        fs->blocksize<<3, bit);
1648         unmap_dblock(b, buf);
1649         LAFSI(im)->md.inodemap.nextbit = bit+1;
1650         if (bit >= fs->blocksize<<3) {
1651                 putdref(b,MKREF(cfi_map));
1652                 goto retry;
1653         }
1654         mutex_unlock(&im->i_mutex);
1655         *bp = b;
1656         *inump = bit + (bnum << (im->i_blkbits + 3));
1657         return 0;
1658
1659 abort_unlock:
1660         lafs_checkpoint_unlock(fs);
1661 abort:
1662         putdref(b, MKREF(cfi_map));
1663         *bp = NULL;
1664         mutex_unlock(&im->i_mutex);
1665         iput(im);
1666         return err;
1667 }
1668
1669 struct inode_map_new_info {
1670         struct datablock *ib, *mb;
1671 };
1672
1673 static int
1674 inode_map_new_prepare(struct fs *fs, int inum, struct inode *fsys,
1675                       struct inode_map_new_info *imni)
1676 {
1677         int choice = inum;
1678         int restarted = 0;
1679         int err = 0;
1680         struct datablock *b;
1681
1682         imni->ib = imni->mb = NULL;
1683 retry:
1684         if (inum == 0)
1685                 /* choose a possibly-free inode number */
1686                 err = choose_free_inum(fs, fsys, &choice,
1687                                        &imni->mb, &restarted);
1688         if (err)
1689                 return err;
1690
1691         b = lafs_get_block(fsys, choice, NULL, GFP_KERNEL,
1692                            MKREF(cfi_ino));
1693         if (!b)
1694                 return -ENOMEM;
1695
1696         if (test_and_set_bit(B_Claimed, &b->b.flags)) {
1697                 putdref(b, MKREF(cfi_ino));
1698                 if (inum)
1699                         return -EEXIST;
1700                 goto retry;
1701         }
1702         if (imni->mb) {
1703                 lafs_iolock_written(&imni->mb->b);
1704                 set_bit(B_PinPending, &imni->mb->b.flags);
1705                 lafs_iounlock_block(&imni->mb->b);
1706         }
1707         set_bit(B_PinPending, &b->b.flags);
1708         b->my_inode = NULL;
1709         imni->ib = b;
1710         return 0;
1711 }
1712
1713 static int
1714 inode_map_new_pin(struct inode_map_new_info *imni)
1715 {
1716         int err = 0;
1717         if (imni->mb)
1718                 err = lafs_pin_dblock(imni->mb, NewSpace);
1719         err = err ?: lafs_pin_dblock(imni->ib, NewSpace);
1720         return err;
1721 }
1722
1723 static void
1724 inode_map_new_commit(struct inode_map_new_info *imni)
1725 {
1726         unsigned long *buf;
1727
1728         if (imni->mb) {
1729                 int blksize = imni->ib->b.inode->i_sb->s_blocksize;
1730                 int bit = imni->ib->b.fileaddr & (blksize*8 - 1);
1731                 int hole = 0;
1732                 struct inode *ino = imni->mb->b.inode;
1733
1734                 mutex_lock_nested(&ino->i_mutex, I_MUTEX_QUOTA);
1735                 buf = map_dblock(imni->mb);
1736                 generic___clear_le_bit(bit, buf);
1737                 if (buf[blksize/sizeof(*buf)-1] == 0 &&
1738                     generic_find_next_le_bit(buf, blksize*8, 0) == blksize*8)
1739                         /* block is empty, punch a hole */
1740                         hole = 1;
1741
1742                 unmap_dblock(imni->mb, buf);
1743                 if (hole)
1744                         lafs_erase_dblock(imni->mb);
1745                 else
1746                         lafs_dirty_dblock(imni->mb);
1747
1748                 putdref(imni->mb, MKREF(cfi_map));
1749                 mutex_unlock(&ino->i_mutex);
1750                 iput(ino);
1751         }
1752         putdref(imni->ib, MKREF(cfi_ino));
1753 }
1754
1755 static void
1756 inode_map_new_abort(struct inode_map_new_info *imni)
1757 {
1758         if (imni->ib) {
1759                 clear_bit(B_Claimed, &imni->ib->b.flags);
1760                 clear_bit(B_PinPending, &imni->ib->b.flags);
1761                 lafs_orphan_release(fs_from_inode(imni->ib->b.inode),
1762                                     imni->ib);
1763         }
1764         putdref(imni->ib, MKREF(cfi_ino));
1765         if (imni->mb) {
1766                 struct inode *ino = imni->mb->b.inode;
1767                 putdref(imni->mb, MKREF(cfi_map));
1768                 iput(ino);
1769         }
1770 }
1771
1772 struct inode *
1773 lafs_new_inode(struct fs *fs, struct inode *fsys, struct inode *dir,
1774                int type, int inum, int mode, struct datablock **inodbp)
1775 {
1776         /* allocate and instantiate a new inode.  If inum is non-zero,
1777          * choose any number, otherwise we are creating a special inode
1778          * and have to use the given number.
1779          * This creation is committed independently of any name that might
1780          * subsequently be given to the inode.  So we register it as an
1781          * orphan so that it will be cleaned up if the name isn't
1782          * successfully created
1783          *
1784          */
1785         struct inode *ino;
1786         struct datablock *b;
1787         struct inode_map_new_info imni;
1788         struct update_handle ui;
1789         int err;
1790
1791         err = inode_map_new_prepare(fs, inum, fsys, &imni);
1792         err = lafs_cluster_update_prepare(&ui, fs, sizeof(struct la_inode))
1793                 ?: err;
1794         if (err == 0)
1795                 err = lafs_make_orphan(fs, imni.ib);
1796         if (err)
1797                 goto abort;
1798 retry:
1799         lafs_checkpoint_lock(fs);
1800
1801         err = inode_map_new_pin(&imni);
1802
1803         if (err == -EAGAIN) {
1804                 lafs_checkpoint_unlock_wait(fs);
1805                 goto retry;
1806         }
1807         if (err < 0)
1808                 goto abort_unlock;
1809
1810         b = getdref(imni.ib, MKREF(inode_new));
1811
1812         lafs_iolock_block(&b->b); /* make sure we don't race with the cleaner
1813                                    * and zero this inode while trying to load it
1814                                    */
1815         lafs_inode_init(b, type, mode, dir);
1816         lafs_iounlock_block(&b->b);
1817
1818         inode_map_new_commit(&imni);
1819         ino = lafs_iget(fsys, b->b.fileaddr, SYNC);
1820         if (IS_ERR(ino)) {
1821                 lafs_cluster_update_abort(&ui);
1822                 LAFS_BUG(1, &b->b);
1823         } else
1824                 lafs_cluster_update_commit(&ui, b, 0,
1825                                            LAFSI(ino)->metadata_size);
1826         LAFS_BUG(LAFSI(ino)->dblock != b, &b->b);
1827         LAFS_BUG(b->my_inode != ino, &b->b);
1828         lafs_checkpoint_unlock(fs);
1829
1830         if (inodbp)
1831                 *inodbp = b;
1832         else
1833                 putdref(b, MKREF(inode_new));
1834         return ino;
1835
1836 abort_unlock:
1837         lafs_checkpoint_unlock(fs);
1838         err = -ENOSPC;
1839 abort:
1840         inode_map_new_abort(&imni);
1841         lafs_cluster_update_abort(&ui);
1842         dprintk("After abort %d: %s\n", err, strblk(&imni.ib->b));
1843         return ERR_PTR(err);
1844 }
1845
1846 static int inode_map_free(struct fs *fs, struct inode *fsys, u32 inum)
1847 {
1848         struct inode *im = lafs_iget(fsys, 1, SYNC);
1849         int bit;
1850         unsigned long *buf;
1851         struct datablock *b;
1852         u32 bnum;
1853         int err;
1854
1855         mutex_lock_nested(&im->i_mutex, I_MUTEX_QUOTA);
1856
1857         bnum = inum >> (3 + fs->blocksize_bits);
1858         bit = inum - (bnum << (3 + fs->blocksize_bits));
1859         b = lafs_get_block(im, bnum, NULL, GFP_KERNEL, MKREF(inode_map_free));
1860         if (!b) {
1861                 mutex_unlock(&im->i_mutex);
1862                 iput(im);
1863                 return -ENOMEM;
1864         }
1865         err = lafs_read_block(b);
1866         if (err) {
1867                 putdref(b, MKREF(inode_map_free));
1868                 mutex_unlock(&im->i_mutex);
1869                 iput(im);
1870                 return err;
1871         }
1872         lafs_iolock_written(&b->b);
1873         set_bit(B_PinPending, &b->b.flags);
1874         lafs_iounlock_block(&b->b);
1875 retry:
1876         lafs_checkpoint_lock(fs);
1877         err = lafs_pin_dblock(b, ReleaseSpace);
1878         if (err == -EAGAIN) {
1879                 lafs_checkpoint_unlock_wait(fs);
1880                 goto retry;
1881         }
1882         BUG_ON(err < 0);
1883         buf = map_dblock(b);
1884         generic___set_le_bit(bit, buf);
1885         unmap_dblock(b, buf);
1886         lafs_dirty_dblock(b);
1887         putdref(b, MKREF(inode_map_free));
1888         lafs_checkpoint_unlock(fs);
1889         mutex_unlock(&im->i_mutex);
1890         iput(im);
1891         return 0;
1892 }
1893
1894 int lafs_inode_inuse(struct fs *fs, struct inode *fsys, u32 inum)
1895 {
1896         /* This is used during roll-forward to register a newly created
1897          * inode in the inode map
1898          */
1899         struct inode *im = lafs_iget(fsys, 1, SYNC);
1900         int bit;
1901         unsigned long *buf;
1902         struct datablock *b;
1903         u32 bnum;
1904         int err;
1905
1906         mutex_lock_nested(&im->i_mutex, I_MUTEX_QUOTA);
1907
1908         bnum = inum >> (3 + fs->blocksize_bits);
1909         bit = inum - (bnum << (3 + fs->blocksize_bits));
1910         if (bnum > LAFSI(im)->md.inodemap.size) {
1911                 /* inum to unbelievably big */
1912                 mutex_unlock(&im->i_mutex);
1913                 iput(im);
1914                 return -EINVAL;
1915         }
1916         b = lafs_get_block(im, bnum, NULL, GFP_KERNEL, MKREF(inode_map_free));
1917         if (!b) {
1918                 mutex_unlock(&im->i_mutex);
1919                 iput(im);
1920                 return -ENOMEM;
1921         }
1922
1923         err = lafs_read_block(b);
1924         if (err) {
1925                 putdref(b, MKREF(inode_map_free));
1926                 mutex_unlock(&im->i_mutex);
1927                 iput(im);
1928                 return err;
1929         }
1930
1931         lafs_iolock_written(&b->b);
1932         set_bit(B_PinPending, &b->b.flags);
1933         lafs_iounlock_block(&b->b);
1934 retry:
1935         lafs_checkpoint_lock(fs);
1936         err = lafs_pin_dblock(b, CleanSpace);
1937         if (err == -EAGAIN) {
1938                 lafs_checkpoint_unlock_wait(fs);
1939                 goto retry;
1940         }
1941         BUG_ON(err < 0);
1942         buf = map_dblock(b);
1943         if (bnum == LAFSI(im)->md.inodemap.size) {
1944                 /* need to add a new block to the file */
1945                 memset(buf, 0xff, fs->blocksize);
1946                 LAFSI(im)->md.inodemap.size = bnum + 1;
1947                 lafs_dirty_inode(im);
1948         }
1949         generic___clear_le_bit(bit, buf);
1950         unmap_dblock(b, buf);
1951         lafs_dirty_dblock(b);
1952         putdref(b, MKREF(inode_map_free));
1953         lafs_checkpoint_unlock(fs);
1954         mutex_unlock(&im->i_mutex);
1955         iput(im);
1956         return 0;
1957 }
1958
1959
1960
1961 int lafs_setattr(struct dentry *dentry, struct iattr *attr)
1962 {
1963         int err;
1964         struct inode *ino = dentry->d_inode;
1965         struct fs *fs = fs_from_inode(ino);
1966         struct datablock *db;
1967
1968         err = inode_change_ok(ino, attr);
1969         db = lafs_inode_dblock(ino, SYNC, MKREF(setattr));
1970         if (IS_ERR(db))
1971                 err = PTR_ERR(db);
1972         if (err)
1973                 return err;
1974
1975         /* We don't need iolock_written here as we don't
1976          * actually change the inode block yet
1977          */
1978         lafs_iolock_block(&db->b);
1979         set_bit(B_PinPending, &db->b.flags);
1980         lafs_iounlock_block(&db->b);
1981
1982         /* FIXME quota stuff */
1983
1984 again:
1985         lafs_checkpoint_lock(fs);
1986         err = lafs_pin_dblock(db, ReleaseSpace);
1987         if (err == -EAGAIN) {
1988                 lafs_checkpoint_unlock_wait(fs);
1989                 goto again;
1990         }
1991
1992         if (!err) {
1993                 if ((attr->ia_valid & ATTR_SIZE) &&
1994                     attr->ia_size != i_size_read(ino))
1995                         truncate_setsize(ino, attr->ia_size);
1996                 setattr_copy(ino, attr);
1997                 mark_inode_dirty(ino);
1998
1999                 lafs_dirty_dblock(db);
2000         }
2001         clear_bit(B_PinPending, &db->b.flags);
2002         putdref(db, MKREF(setattr));
2003         lafs_checkpoint_unlock(fs);
2004
2005         return err;
2006 }
2007
2008 void lafs_truncate(struct inode *ino)
2009 {
2010         /* Want to truncate this file.
2011          * i_size has already been changed, and the address space
2012          * has been cleaned up.
2013          * So just start the background truncate
2014          */
2015         struct fs *fs = fs_from_inode(ino);
2016         struct datablock *db = lafs_inode_dblock(ino, SYNC, MKREF(trunc));
2017         loff_t trunc_block;
2018         DEFINE_WAIT(wq);
2019
2020         if (IS_ERR(db))
2021                 return;
2022
2023         trunc_block = ((i_size_read(ino) + fs->blocksize - 1)
2024                        >> fs->blocksize_bits);
2025         /* We hold i_mutex, so regular orphan processing cannot
2026          * contine - we have to push it forward ourselves.
2027          */
2028         while (test_bit(I_Trunc, &LAFSI(ino)->iflags) &&
2029                LAFSI(ino)->trunc_next < trunc_block) {
2030                 prepare_to_wait(&fs->async_complete, &wq,
2031                                 TASK_UNINTERRUPTIBLE);
2032                 lafs_inode_handle_orphan(db);
2033                 if (test_bit(B_Orphan, &db->b.flags))
2034                         schedule();
2035         }
2036         finish_wait(&fs->async_complete, &wq);
2037
2038         /* There is nothing we can do about errors here.  The
2039          * most likely are ENOMEM which itself is very unlikely.
2040          * If this doesn't get registered as an orphan .... maybe
2041          * it will have to wait until something else truncates it.
2042          */
2043         lafs_make_orphan(fs, db);
2044
2045         if (!test_and_set_bit(I_Trunc, &LAFSI(ino)->iflags))
2046                 lafs_igrab_fs(ino);
2047         if (trunc_block == 0)
2048                 LAFSI(ino)->trunc_gen++;
2049         LAFSI(ino)->trunc_next = trunc_block;
2050         putdref(db, MKREF(trunc));
2051 }
2052
2053 const struct inode_operations lafs_special_ino_operations = {
2054         .setattr        = lafs_setattr,
2055         .getattr        = lafs_getattr,
2056         .truncate       = lafs_truncate,
2057 };