]> git.neil.brown.name Git - LaFS.git/blob - inode.c
erase_dblock - add a random clear_bit
[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 atime 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.  So just shift by 1 less than blkbits.
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                 /* FIXME - I could lose an update here - do I care? */
638                 /* Can only reserve NewSpace with checkpoint locked... */
639                 lafs_checkpoint_lock(fs_from_inode(ino));
640                 if (lafs_reserve_block(&b->b, NewSpace) == 0)
641                         lafs_dirty_dblock(b);
642                 lafs_checkpoint_unlock(fs_from_inode(ino));
643         }
644         unmap_dblock(b, atp);
645         putdref(b, MKREF(store_atime));
646 }
647
648 void lafs_inode_checkpin(struct inode *ino)
649 {
650         /* Make sure I_Pinned is set correctly.
651          * It should be set precisely if i_nlink is non-zero,
652          * and ->iblock is B_Pinned.
653          * When it is set, we own a reference to the inode.
654          *
655          * This needs to be called whenever we change
656          * i_nlink, and whenever we pin or unpin an InoIdx
657          * block.
658          */
659         if (ino->i_nlink == 0) {
660                 /* I_Pinned should not be set */
661                 if (test_and_clear_bit(I_Pinned, &LAFSI(ino)->iflags)) {
662                         if (ino->i_sb->s_type == &lafs_fs_type)
663                                 iput(ino);
664                         else
665                                 lafs_iput_fs(ino);
666                 }
667         } else {
668                 /* Need to check if iblock is Pinned. */
669                 struct indexblock *ib = NULL;
670                 if (LAFSI(ino)->iblock) {
671                         spin_lock(&ino->i_data.private_lock);
672                         ib = LAFSI(ino)->iblock;
673                         if (ib && !test_bit(B_Pinned, &ib->b.flags))
674                                 ib = NULL;
675                         spin_unlock(&ino->i_data.private_lock);
676                 }
677                 if (ib) {
678                         if (!test_and_set_bit(I_Pinned, &LAFSI(ino)->iflags)) {
679                                 if (ino->i_sb->s_type == &lafs_fs_type)
680                                         igrab(ino);
681                                 else
682                                         lafs_igrab_fs(ino);
683                         }
684                 } else {
685                         if (test_and_clear_bit(I_Pinned, &LAFSI(ino)->iflags)) {
686                         if (ino->i_sb->s_type == &lafs_fs_type)
687                                 iput(ino);
688                         else
689                                 lafs_iput_fs(ino);
690                         }
691                 }
692         }
693 }
694
695 struct datablock *lafs_inode_get_dblock(struct inode *ino, REFARG)
696 {
697         struct datablock *db;
698
699         spin_lock(&ino->i_data.private_lock);
700         db = LAFSI(ino)->dblock;
701         if (db) {
702                 if (db->b.inode == ino)
703                         getdref_locked(db, REF);
704                 else {
705                         spin_lock_nested(&db->b.inode->i_data.private_lock, 1);
706                         getdref_locked(db, REF);
707                         spin_unlock(&db->b.inode->i_data.private_lock);
708                 }
709         }
710         spin_unlock(&ino->i_data.private_lock);
711         return db;
712 }
713
714 struct datablock *lafs_inode_dblock(struct inode *ino, int async, REFARG)
715 {
716         struct datablock *db;
717         int err;
718
719         db = lafs_inode_get_dblock(ino, REF);
720
721         if (!db)
722                 db = lafs_get_block(LAFSI(ino)->filesys, ino->i_ino, NULL,
723                                     GFP_KERNEL, REF);
724         if (!db)
725                 return ERR_PTR(-ENOMEM);
726
727         LAFSI(ino)->dblock = db;
728         rcu_assign_pointer(db->my_inode, ino);
729         if (async)
730                 err = lafs_read_block_async(db);
731         else
732                 err = lafs_read_block(db);
733         if (err == 0)
734                 return db;
735
736         putdref(db, REF);
737         return ERR_PTR(err);
738 }
739
740 void lafs_inode_init(struct datablock *b, int type, int mode, struct inode *dir)
741 {
742         /* A new block has been allocated in an inode file to hold an
743          * inode.  We get to fill in initial values so that when
744          * 'iget' calls lafs_import_inode, the correct inode is
745          * loaded.
746          */
747         struct fs *fs = fs_from_inode(b->b.inode);
748         struct la_inode *lai = map_dblock(b);
749         int size;
750
751         lai->data_blocks = cpu_to_le32(0);
752         lai->index_blocks = cpu_to_le32(0);
753         get_random_bytes(&lai->generation, sizeof(lai->generation));
754         lai->depth = 1;
755         lai->trunc_gen = 0;
756         lai->filetype = type;
757         lai->flags = 0;
758
759         switch(type) {
760         case TypeInodeFile:
761         {
762                 struct fs_metadata *l = &lai->metadata[0].fs;
763                 size = sizeof(struct fs_metadata);
764                 l->update_time = 0;
765                 l->blocks_used = 0;
766                 l->blocks_allowed = 0;
767                 l->creation_age = fs->wc[0].cluster_seq;
768                 l->inodes_used = 0;
769                 l->quota_inodes[0] = 0;
770                 l->quota_inodes[1] = 0;
771                 l->quota_inodes[2] = 0;
772                 l->snapshot_usage_table = 0;
773                 l->pad = 0;
774                 /* name will be zero length and not used */
775                 break;
776         }
777         case TypeInodeMap:
778         {
779                 struct inodemap_metadata *l = &lai->metadata[0].inodemap;
780                 l->size = 0;
781                 size = sizeof(struct inodemap_metadata);
782                 break;
783         }
784         case TypeSegmentMap:
785                 size = sizeof(struct su_metadata);
786                 break;
787         case TypeQuota:
788                 size = sizeof(struct quota_metadata);
789                 break;
790         case TypeOrphanList:
791                 size = 0;
792                 break;
793         case TypeAccessTime:
794                 size = 0;
795                 break;
796         default:
797         {
798                 struct file_metadata *l = &lai->metadata[0].file;
799                 struct timespec now = CURRENT_TIME;
800
801                 l->flags = cpu_to_le16(0);
802                 l->userid = cpu_to_le32(current->cred->fsuid);
803                 if (dir && (dir->i_mode & S_ISGID)) {
804                         l->groupid = cpu_to_le32(dir->i_gid);
805                         if (type == TypeDir)
806                                 mode |= S_ISGID;
807                 } else
808                         l->groupid = cpu_to_le32(current->cred->fsgid);
809                 if (dir && LAFSI(dir)->md.file.treeid)
810                         l->treeid = cpu_to_le32(LAFSI(dir)->md.file.treeid);
811                 else
812                         l->treeid = l->userid;
813
814                 l->mode = cpu_to_le16(mode);
815                 l->creationtime = encode_time(&now);
816                 l->modifytime = l->creationtime;
817                 l->ctime = l->creationtime;
818                 l->accesstime = l->creationtime;
819                 l->size = 0;
820                 l->parent = dir ? cpu_to_le32(dir->i_ino) : 0;
821                 l->linkcount = 0;
822                 l->attrinode = 0;
823                 if (type == TypeDir) {
824                         struct dir_metadata *l = &lai->metadata[0].dir;
825                         u32 seed;
826                         get_random_bytes(&seed,
827                                          sizeof(seed));
828                         seed = (seed & ~7) | 1;
829                         l->hash_seed = cpu_to_le32(seed);
830                         size = sizeof(struct dir_metadata);
831                 } else if (type == TypeSpecial) {
832                         struct special_metadata *s = &lai->metadata[0].special;
833                         s->major = s->minor = 0;
834                         size = sizeof(struct special_metadata);
835                 } else
836                         size = sizeof(struct file_metadata);
837         }
838         }
839         size += sizeof(struct la_inode);
840         lai->metadata_size = cpu_to_le32(size);
841         memset(((char *)lai)+size, 0, fs->blocksize-size);
842         *(u16 *)(((char *)lai)+size) = cpu_to_le16(IBLK_EXTENT);
843
844         unmap_dblock(b, lai);
845         set_bit(B_Valid, &b->b.flags);
846         LAFS_BUG(!test_bit(B_Pinned, &b->b.flags), &b->b);
847         lafs_dirty_dblock(b);
848 }
849
850 static int inode_map_free(struct fs *fs, struct inode *fsys, u32 inum);
851
852 void lafs_evict_inode(struct inode *ino)
853 {
854         struct fs *fs = fs_from_inode(ino);
855         struct lafs_inode *li = LAFSI(ino);
856
857         if (ino->i_mode == 0) {
858                 /* There never was an inode here,
859                  * so nothing to do.
860                  * We just call end_writeback to get the
861                  * flags set properly.
862                  */
863                 end_writeback(ino);
864                 return;
865         }
866
867         dprintk("EVICT INODE %d\n", (int)ino->i_ino);
868
869
870         /* Normal truncation holds an igrab, so we cannot be
871          * deleted until any truncation finishes
872          */
873         BUG_ON(test_bit(I_Trunc, &LAFSI(ino)->iflags));
874
875         if (ino->i_nlink == 0) {
876                 struct datablock *b =
877                         lafs_inode_dblock(ino, SYNC, MKREF(delete_inode));
878                 i_size_write(ino, 0);
879                 truncate_inode_pages(&ino->i_data, 0);
880                 LAFSI(ino)->trunc_next = 0;
881                 set_bit(I_Deleting, &LAFSI(ino)->iflags);
882                 set_bit(I_Trunc, &LAFSI(ino)->iflags);
883                 lafs_igrab_fs(ino);
884                 if (!IS_ERR(b)) {
885                         set_bit(B_Claimed, &b->b.flags);
886                         lafs_add_orphan(fs, b);
887                         dprintk("PUNCH hole for %d\n", (int)b->b.fileaddr);
888                         putdref(b, MKREF(delete_inode));
889                 }
890                 inode_map_free(fs, LAFSI(ino)->filesys,  ino->i_ino);
891         } else
892                 truncate_inode_pages(&ino->i_data, 0);
893         end_writeback(ino);
894
895         dprintk("CLEAR INODE %d\n", (int)ino->i_ino);
896
897         li->type = 0;
898
899         /* Now is a good time to break the linkage between
900          * inode and dblock - but not if the file is
901          * being deleted
902          */
903         if (!test_bit(I_Deleting, &li->iflags)) {
904                 struct datablock *db;
905                 spin_lock(&ino->i_data.private_lock);
906                 db = li->dblock;
907                 if (db) {
908                         struct indexblock *ib = li->iblock;
909                         LAFS_BUG(ib && atomic_read(&ib->b.refcnt), &db->b);
910                         db->my_inode = NULL;
911                         li->dblock = NULL;
912                         li->iblock = NULL;
913                 }
914                 spin_unlock(&ino->i_data.private_lock);
915         }
916
917         /* FIXME release quota inodes if filesystem */
918 }
919
920 static int prune(void *data, u32 addr, u64 paddr, int len)
921 {
922         /* This whole index block is being pruned, just account
923          * for everything and it will be cleared afterwards
924          */
925         struct indexblock *ib = data;
926         struct inode *ino = ib->b.inode;
927         struct fs *fs = fs_from_inode(ino);
928         int ph = !!test_bit(B_Phase1, &ib->b.flags);
929         int i;
930         dprintk("PRUNE %d for %d at %lld\n", addr, len, (long long)paddr);
931         if (paddr == 0 || len == 0)
932                 return 0;
933         for (i = 0 ; i < len ; i++)
934                 lafs_summary_update(fs, ino, paddr+i, 0, 0, ph, 0);
935         return len;
936 }
937
938 static int prune_some(void *data, u32 addr, u64 paddr, int len)
939 {
940         /* Part of this index block is being pruned.  Copy
941          * what addresses we can into uninc_table so that
942          * it can be 'incorporated'
943          * We should probably share some code with
944          * lafs_allocated_block??
945          */
946         struct indexblock *ib = data;
947         struct inode *ino = ib->b.inode;
948         struct fs *fs = fs_from_inode(ino);
949         int ph = !!test_bit(B_Phase1, &ib->b.flags);
950         int i;
951
952         if (paddr == 0 || len == 0)
953                 return 0;
954         dprintk("PRUNE2 %d for %d at %lld\n", addr, len, (long long)paddr);
955         for (i = 0 ; i < len ; i++) {
956                 /* FIXME should allow longer truncation ranges in uninc_table
957                  * as they are easy to handle.
958                  */
959                 struct addr *a;
960                 if (addr + i < LAFSI(ino)->trunc_next)
961                         continue;
962                 spin_lock(&ino->i_data.private_lock);
963                 a = &ib->uninc_table.pending_addr
964                         [ib->uninc_table.pending_cnt - 1];
965                 if (ib->uninc_table.pending_cnt <
966                     ARRAY_SIZE(ib->uninc_table.pending_addr)) {
967                         a++;
968                         a->fileaddr = addr + i;
969                         a->physaddr = 0;
970                         a->cnt = 1;
971                         LAFS_BUG(!test_bit(B_Pinned, &ib->b.flags), &ib->b);
972                         ib->uninc_table.pending_cnt++;
973                 } else {
974                         spin_unlock(&ino->i_data.private_lock);
975                         break;
976                 }
977                 spin_unlock(&ino->i_data.private_lock);
978                 lafs_summary_update(fs, ino, paddr+i, 0, 0, ph, 0);
979         }
980         return i;
981 }
982
983 int lafs_inode_handle_orphan(struct datablock *b)
984 {
985         /* Don't need rcu protection for my_inode run_orphan
986          * holds a reference
987          */
988         struct indexblock *ib, *ib2;
989         struct inode *ino = b->my_inode;
990         struct fs *fs = fs_from_inode(ino);
991         u32 trunc_next, next_trunc;
992         int loop_cnt = 20;
993         int err = -ENOMEM;
994
995         if (!test_bit(I_Trunc, &LAFSI(ino)->iflags)) {
996                 if (test_bit(I_Deleting, &LAFSI(ino)->iflags)) {
997                         LAFS_BUG(ino->i_nlink, &b->b);
998                         if (LAFSI(ino)->cblocks +
999                             LAFSI(ino)->pblocks +
1000                             LAFSI(ino)->ablocks +
1001                             LAFSI(ino)->ciblocks +
1002                             LAFSI(ino)->piblocks)
1003                         printk("Deleting inode %lu: %ld+%ld+%ld %ld+%ld\n",
1004                                ino->i_ino,
1005                                LAFSI(ino)->cblocks,
1006                                LAFSI(ino)->pblocks,
1007                                LAFSI(ino)->ablocks,
1008                                LAFSI(ino)->ciblocks,
1009                                LAFSI(ino)->piblocks);
1010                         BUG_ON(LAFSI(ino)->cblocks +
1011                                LAFSI(ino)->pblocks +
1012                                LAFSI(ino)->ablocks +
1013                                LAFSI(ino)->ciblocks +
1014                                LAFSI(ino)->piblocks);
1015                         if (lafs_erase_dblock_async(b))
1016                                 lafs_orphan_release(fs, b);
1017                 } else if (ino->i_nlink || LAFSI(ino)->type == 0)
1018                         lafs_orphan_release(fs, b);
1019                 else
1020                         lafs_orphan_forget(fs, b);
1021                 return 0;
1022         }
1023
1024         ib = lafs_make_iblock(ino, ADOPT, SYNC, MKREF(inode_handle_orphan));
1025         if (IS_ERR(ib))
1026                 return PTR_ERR(ib);
1027
1028         /* Here is the guts of 'truncate'.  We find the next leaf index
1029          * block and discard all the addresses there-in.
1030          */
1031         trunc_next = LAFSI(ino)->trunc_next;
1032
1033         if (trunc_next == 0xFFFFFFFF) {
1034                 /* truncate has finished in that all data blocks
1035                  * have been removed and all index block are either
1036                  * gone or pending incorporation at which point they will
1037                  * go.
1038                  * If we hit a phase change, we will need to postpone
1039                  * the rest of the cleaning until it completes.
1040                  * If there is a checkpoint happening, then all the work
1041                  * that we can do now, it will do for us.  So just
1042                  * let it.
1043                  */
1044                 struct indexblock *tmp;
1045                 struct indexblock *next;
1046                 u32 lastaddr;
1047
1048                 if (!test_bit(B_Pinned, &ib->b.flags)) {
1049                         /* must be finished */
1050                         LAFS_BUG(test_bit(B_Dirty, &ib->b.flags), &ib->b);
1051                         clear_bit(I_Trunc, &LAFSI(ino)->iflags);
1052                         lafs_iput_fs(ino);
1053                         wake_up(&fs->trunc_wait);
1054                         err = -ERESTARTSYS;
1055                         goto out2;
1056                 }
1057                 if (fs->checkpointing) {
1058                         /* This cannot happen with current code,
1059                          * but leave it in case we ever have
1060                          * orphan handling parallel with checkpoints
1061                          */
1062                         err = -EBUSY; /* Try again after the checkpoint */
1063                         goto out2;
1064                 }
1065
1066                 lastaddr = (i_size_read(ino) +
1067                             fs->blocksize - 1)
1068                         >> fs->blocksize_bits;
1069                 /* Find a Pinned descendent of ib which has no
1070                  * Pinned descendents and no PrimaryRef dependent
1071                  * (so take the last).
1072                  * Prefer blocks that are beyond EOF (again, take the last).
1073                  * If there are none, descend the last block that
1074                  * is not after EOF and look at its children.
1075                  */
1076                 ib2 = next = ib;
1077                 spin_lock(&ib->b.inode->i_data.private_lock);
1078                 while (next) {
1079                         ib2 = next;
1080                         next = NULL;
1081                         list_for_each_entry(tmp, &ib2->children, b.siblings) {
1082                                 if (!test_bit(B_Index, &tmp->b.flags) ||
1083                                     !test_bit(B_Pinned, &tmp->b.flags))
1084                                         continue;
1085                                 if (next == NULL ||
1086                                     tmp->b.fileaddr > next->b.fileaddr)
1087                                         next = tmp;
1088                         }
1089                 }
1090                 if (ib2->b.fileaddr < lastaddr) {
1091                         /* Must be all done */
1092                         spin_unlock(&ib->b.inode->i_data.private_lock);
1093                         clear_bit(I_Trunc, &LAFSI(ino)->iflags);
1094                         lafs_iput_fs(ino);
1095                         wake_up(&fs->trunc_wait);
1096                         err = -ERESTARTSYS;
1097                         goto out2;
1098                 }
1099                 getiref(ib2, MKREF(inode_handle_orphan2));
1100                 spin_unlock(&ib->b.inode->i_data.private_lock);
1101
1102                 /* ib2 is an index block beyond EOF with no
1103                  * Pinned children.
1104                  * Incorporating it should unpin it.
1105                  */
1106                 if (!list_empty(&ib2->children)) {
1107                         lafs_print_tree(&ib2->b, 3);
1108                         LAFS_BUG(1, &ib2->b);
1109                 }
1110
1111                 if (!lafs_iolock_written_async(&ib2->b)) {
1112                         putiref(ib2, MKREF(inode_handle_orphan2));
1113                         err = -EAGAIN;
1114                         goto out2;
1115                 }
1116                 while (ib2->uninc_table.pending_cnt || ib2->uninc)
1117                         lafs_incorporate(fs, ib2);
1118
1119                 if (test_bit(B_Dirty, &ib2->b.flags) ||
1120                     test_bit(B_Realloc, &ib2->b.flags))
1121                         lafs_cluster_allocate(&ib2->b, 0);
1122                 else
1123                         lafs_iounlock_block(&ib2->b);
1124
1125                 if (!list_empty(&ib2->b.siblings)) {
1126                         printk("looping on %s\n", strblk(&ib2->b));
1127                         loop_cnt--;
1128                         if (loop_cnt < 0)
1129                                 BUG();
1130                 }
1131                 putiref(ib2, MKREF(inode_handle_orphan2));
1132                 err = -ERESTARTSYS;
1133                 if (ib->uninc) {
1134                         if (lafs_iolock_written_async(&ib->b)) {
1135                                 while (ib->uninc)
1136                                         lafs_incorporate(fs, ib);
1137                                 lafs_iounlock_block(&ib->b);
1138                         } else
1139                                 err = -EAGAIN;
1140                 }
1141         out2:
1142                 putiref(ib, MKREF(inode_handle_orphan));
1143                 return err;
1144         }
1145
1146         putiref(ib, MKREF(inode_handle_orphan));
1147
1148         ib = lafs_leaf_find(ino, trunc_next, ADOPT, &next_trunc,
1149                             ASYNC, MKREF(inode_handle_orphan3));
1150         if (IS_ERR(ib))
1151                 return PTR_ERR(ib);
1152         /* now hold an iolock on ib */
1153
1154         /* Ok, trunc_next seems to refer to a block that exists.
1155          * We need to erase it..
1156          *
1157          * So we open up the index block ourselves, call
1158          * lafs_summary_update with each block address, and then
1159          * erase the block.
1160          */
1161
1162         if (LAFSI(ino)->depth == 0) {
1163                 /* Nothing to truncate */
1164                 clear_bit(I_Trunc, &LAFSI(ino)->iflags);
1165                 lafs_iput_fs(ino);
1166                 if (test_bit(B_Pinned, &ib->b.flags))
1167                         /* Need to move the dirtiness which keeps this
1168                          * pinned to the data block.
1169                          */
1170                         lafs_cluster_allocate(&ib->b, 0);
1171                 else
1172                         lafs_iounlock_block(&ib->b);
1173                 err = -ERESTARTSYS;
1174                 goto out_put;
1175         }
1176
1177         lafs_checkpoint_lock(fs);
1178         err = lafs_reserve_block(&ib->b, ReleaseSpace);
1179         if (err < 0)
1180                 goto out;
1181
1182         if (!test_bit(B_Valid, &ib->b.flags) &&
1183             test_bit(B_InoIdx, &ib->b.flags)) {
1184                 /* still invalid, just re-erase to remove
1185                  * pinning */
1186                 LAFSI(ino)->trunc_next = next_trunc;
1187                 lafs_cluster_allocate(&ib->b, 0);
1188                 err = -ERESTARTSYS;
1189                 goto out_unlocked;
1190         }
1191
1192         lafs_pin_block(&ib->b);
1193
1194         /* It might be that this can happen, in which case
1195          * we simply update trunc_next and loop.  But I'd like
1196          * to be sure before I implement that
1197          */
1198         if (!test_bit(B_Valid, &ib->b.flags)) {
1199                 printk("Not Valid: %s\n", strblk(&ib->b));
1200                 printk("depth = %d\n", LAFSI(ino)->depth);
1201                 if (test_bit(B_InoIdx, &ib->b.flags))
1202                         printk("DB: %s\n", strblk(&LAFSI(ib->b.inode)->dblock->b));
1203                 LAFSI(ino)->trunc_next = next_trunc;
1204                 //BUG_ON(!test_bit(B_Valid, &ib->b.flags));
1205                 err = -ERESTARTSYS;
1206                 goto out;
1207         }
1208
1209         if (ib->b.fileaddr < trunc_next &&
1210             lafs_leaf_next(ib, 0) < trunc_next) {
1211                 /* We only want to truncate part of this index block.
1212                  * So we copy addresses into uninc_table and then
1213                  * call lafs_incorporate.
1214                  * This might cause the index tree to grow, so we
1215                  * cannot trust next_trunc
1216                  */
1217                 if (ib->uninc_table.pending_cnt == 0 &&
1218                     ib->uninc == NULL) {
1219                         lafs_dirty_iblock(ib, 0);
1220                         /* FIXME this just removes 8 blocks at a time,
1221                          * which is not enough
1222                          */
1223                         lafs_walk_leaf_index(ib, prune_some, ib);
1224                 }
1225                 if (test_bit(B_Dirty, &ib->b.flags))
1226                         lafs_incorporate(fs, ib);
1227                 err = -ERESTARTSYS;
1228                 goto out;
1229         }
1230         LAFSI(ino)->trunc_next = next_trunc;
1231
1232         while (ib->uninc_table.pending_cnt || ib->uninc) {
1233                 /* There should be no Realloc data blocks here
1234                  * but index blocks might be realloc still.
1235                  */
1236                 LAFS_BUG(!test_bit(B_Dirty, &ib->b.flags) &&
1237                          !test_bit(B_Realloc, &ib->b.flags), &ib->b);
1238                 lafs_incorporate(fs, ib);
1239         }
1240         if (test_bit(B_InoIdx, &ib->b.flags) ||
1241             !test_bit(B_PhysValid, &ib->b.flags) ||
1242             ib->b.physaddr != 0) {
1243                 lafs_walk_leaf_index(ib, prune, ib);
1244                 lafs_clear_index(ib);
1245                 lafs_dirty_iblock(ib, 0);
1246         }
1247         if (test_bit(B_Dirty, &ib->b.flags))
1248                 lafs_incorporate(fs, ib);
1249         if (!list_empty(&ib->children))
1250                 lafs_print_tree(&ib->b, 2);
1251         LAFS_BUG(!list_empty(&ib->children), &ib->b);
1252         err = -ERESTARTSYS;
1253 out:
1254         lafs_iounlock_block(&ib->b);
1255 out_unlocked:
1256         lafs_checkpoint_unlock(fs);
1257 out_put:
1258         putiref(ib, MKREF(inode_handle_orphan3));
1259         return err;
1260 }
1261
1262 void lafs_dirty_inode(struct inode *ino)
1263 {
1264         /* this is called in one of three cases:
1265          * 1/ by lafs internally when dblock or iblock is pinned and
1266          *    ready to be dirtied
1267          * 2/ by writeout before requesting a write - to update mtime
1268          * 3/ by read to update atime
1269          *
1270          * We want to handle atime updates carefully as they may not change
1271          * the stored inode itself.
1272          * For all other updates, the inode dblock exists and is pinned.
1273          * In those cases we will be updating the inode and so can store
1274          * the atime exactly.
1275          * For an atime update, the dblock may not exists, or may not be
1276          * Pinned.  If it isn't then we don't want to make the inode dirty
1277          * but only want to update the delta stored in the atime file.
1278          * The block for that should already be pinned.
1279          *
1280          *
1281          * We mustn't update the data block as it could be in
1282          * writeout and we cannot always wait safely.
1283          * So require that anyone who really cares, dirties the datablock
1284          * or a child themselves.
1285          * When cluster_allocate eventually gets called, it will update
1286          * the datablock from the inode.
1287          * If an update has to wait for the next phase, lock_dblock
1288          * (e.g. in setattr) will do that.
1289          *
1290          * We also use this opportunity to update the filesystem modify time.
1291          */
1292         struct timespec now;
1293         struct inode *filesys;
1294         int atime_only = 1;
1295
1296         if (LAFSI(ino)->dblock) {
1297                 struct datablock *db;
1298                 spin_lock(&ino->i_data.private_lock);
1299                 db = LAFSI(ino)->dblock;
1300                 if (db && test_bit(B_Pinned, &db->b.flags))
1301                         atime_only = 0;
1302                 spin_unlock(&ino->i_data.private_lock);
1303         }
1304
1305         if (atime_only) {
1306                 if (update_atime_delta(ino))
1307                         store_atime_delta(ino);
1308                 return;
1309         }
1310
1311         set_bit(I_Dirty, &LAFSI(ino)->iflags);
1312         ino->i_sb->s_dirt = 1;
1313
1314         if (LAFSI(ino)->type < TypeBase)
1315                 return;
1316         LAFSI(ino)->md.file.i_accesstime = ino->i_atime;
1317         if (LAFSI(ino)->md.file.atime_offset) {
1318                 LAFSI(ino)->md.file.atime_offset = 0;
1319                 store_atime_delta(ino);
1320         }
1321
1322         now = current_fs_time(ino->i_sb);
1323         filesys = LAFSI(ino)->filesys;
1324         if (!timespec_equal(&filesys->i_mtime, &now)) {
1325                 filesys->i_mtime = now;
1326                 set_bit(I_Dirty, &LAFSI(filesys)->iflags);
1327         }
1328 }
1329
1330 int lafs_sync_inode(struct inode *ino, int wait)
1331 {
1332         /* fsync has been called on this file so we need
1333          * to sync any inode updates to the next cluster.
1334          *
1335          * If we cannot create an update record,
1336          * we wait for a phase change, which writes everything
1337          * out.
1338          */
1339         struct datablock *b;
1340         struct fs *fs = fs_from_inode(ino);
1341         struct update_handle uh;
1342         int err;
1343
1344         if (wait) {
1345                 if (LAFSI(ino)->update_cluster > 1)
1346                         lafs_cluster_wait(fs, LAFSI(ino)->update_cluster);
1347                 if (LAFSI(ino)->update_cluster == 1) {
1348                         lafs_checkpoint_lock(fs);
1349                         lafs_checkpoint_unlock_wait(fs);
1350                 }
1351                 return 0;
1352         }
1353
1354         LAFSI(ino)->update_cluster = 0;
1355         if (!test_bit(I_Dirty, &LAFSI(ino)->iflags))
1356                 return 0;
1357         b = lafs_inode_dblock(ino, SYNC, MKREF(write_inode));
1358         if (IS_ERR(b))
1359                 return PTR_ERR(b);
1360
1361         lafs_iolock_written(&b->b);
1362         lafs_inode_fillblock(ino);
1363         lafs_iounlock_block(&b->b);
1364
1365         err = lafs_cluster_update_prepare(&uh, fs, LAFS_INODE_LOG_SIZE);
1366         if (err)
1367                 lafs_cluster_update_abort(&uh);
1368         else {
1369                 lafs_checkpoint_lock(fs);
1370                 if (lafs_cluster_update_pin(&uh) == 0) {
1371                         if (test_and_clear_bit(B_Dirty, &b->b.flags))
1372                                 lafs_space_return(fs, 1);
1373                         LAFSI(ino)->update_cluster =
1374                                 lafs_cluster_update_commit
1375                                 (&uh, b, LAFS_INODE_LOG_START,
1376                                  LAFS_INODE_LOG_SIZE);
1377                 } else  
1378                         lafs_cluster_update_abort(&uh);
1379                 lafs_checkpoint_unlock(fs);
1380         }
1381         if (test_bit(B_Dirty, &b->b.flags)) {
1382                 /* FIXME need to write out the data block...
1383                  * Is that just lafs_cluster_allocate ?
1384                  */
1385         }
1386
1387         if (LAFSI(ino)->update_cluster == 0) {
1388                 lafs_checkpoint_lock(fs);
1389                 if (test_bit(B_Dirty, &b->b.flags))
1390                         LAFSI(ino)->update_cluster = 1;
1391                 lafs_checkpoint_start(fs);
1392                 lafs_checkpoint_unlock(fs);
1393         }
1394         putdref(b, MKREF(write_inode));
1395         return 0; /* FIXME should I return some error message??? */
1396 }
1397
1398 void lafs_inode_fillblock(struct inode *ino)
1399 {
1400         /* copy data from ino into the related data block */
1401
1402         struct lafs_inode *li = LAFSI(ino);
1403         struct datablock *db = li->dblock;
1404         struct la_inode *lai;
1405
1406         clear_bit(I_Dirty, &LAFSI(ino)->iflags);
1407
1408         lai = map_dblock(db);
1409         lai->data_blocks = cpu_to_le32(li->cblocks);
1410         lai->index_blocks = cpu_to_le32(li->ciblocks);
1411         lai->generation = cpu_to_le16(ino->i_generation);
1412         lai->trunc_gen = li->trunc_gen;
1413         lai->flags = li->flags;
1414         lai->filetype = li->type;
1415         if (lai->metadata_size != cpu_to_le16(li->metadata_size)) {
1416                 /* Changing metadata size is wierd.
1417                  * We will need to handle this somehow for xattrs
1418                  * For now we just want to cope with
1419                  * Dir -> InodeFile changes, and that guarantees us
1420                  * there is no index info - so just clear the index 
1421                  * area.
1422                  */
1423                 u16 *s = (u16*)(((char*)lai) + li->metadata_size);
1424                 BUG_ON(li->type != TypeInodeFile);
1425                 lai->metadata_size = cpu_to_le16(li->metadata_size);
1426                 memset(s, 0, ino->i_sb->s_blocksize - li->metadata_size);
1427                 *s = cpu_to_le16(IBLK_INDIRECT);
1428         }
1429         lai->depth = li->depth;
1430
1431         switch (li->type) {
1432         case TypeInodeFile:
1433         {
1434                 struct fs_md *i = &li->md.fs;
1435                 struct fs_metadata *l = &lai->metadata[0].fs;
1436                 int nlen;
1437
1438                 l->snapshot_usage_table = cpu_to_le16(i->usagetable);
1439                 l->update_time = cpu_to_le64(encode_time(&ino->i_mtime));
1440                 l->blocks_used = cpu_to_le64(i->cblocks_used);
1441                 l->blocks_allowed = cpu_to_le64(i->blocks_allowed);
1442                 l->creation_age = cpu_to_le64(i->creation_age);
1443                 l->inodes_used = cpu_to_le32(i->inodes_used);
1444                 l->quota_inodes[0] = cpu_to_le32(i->quota_inums[0]);
1445                 l->quota_inodes[1] = cpu_to_le32(i->quota_inums[1]);
1446                 l->quota_inodes[2] = cpu_to_le32(i->quota_inums[2]);
1447                 nlen = lai->metadata_size - offsetof(struct la_inode,
1448                                                      metadata[0].fs.name);
1449                 memset(l->name, 0, nlen);
1450                 if (i->name == NULL)
1451                         nlen = 0;
1452                 else if (strlen(i->name) < nlen)
1453                         nlen = strlen(i->name);
1454                 memcpy(l->name, i->name, nlen);
1455                 break;
1456         }
1457
1458         case TypeInodeMap:
1459         {
1460                 struct inodemap_md *m = &li->md.inodemap;
1461                 struct inodemap_metadata *s = &lai->metadata[0].inodemap;
1462                 s->size = cpu_to_le32(m->size);
1463                 break;
1464         }
1465
1466         case TypeSegmentMap:
1467         {
1468                 struct su_md *m = &li->md.segmentusage;
1469                 struct su_metadata *s = &lai->metadata[0].segmentusage;
1470                 s->table_size = cpu_to_le32(m->table_size);
1471                 break;
1472         }
1473
1474         case TypeQuota:
1475         {
1476                 struct quota_md *m = &li->md.quota;
1477                 struct quota_metadata *s = &lai->metadata[0].quota;
1478                 s->gracetime = cpu_to_le32(m->gracetime);
1479                 s->graceunits = cpu_to_le32(m->graceunits);
1480                 break;
1481         }
1482         case TypeOrphanList:
1483         case TypeAccessTime:
1484                 break;
1485
1486         default: /* TypeBase or larger */
1487         {
1488                 struct file_md *i = &li->md.file;
1489                 struct file_metadata *l = &lai->metadata[0].file;
1490                 struct dir_metadata *d = &lai->metadata[0].dir;
1491                 struct special_metadata *s = &lai->metadata[0].special;
1492
1493                 if (li->type < TypeBase)
1494                         break;
1495                 l->flags = cpu_to_le16(i->flags);
1496                 l->mode = cpu_to_le16(ino->i_mode);
1497                 l->userid = cpu_to_le32(ino->i_uid);
1498                 l->groupid = cpu_to_le32(ino->i_gid);
1499                 l->treeid = cpu_to_le32(i->treeid);
1500                 l->creationtime = cpu_to_le64(i->creationtime);
1501                 l->modifytime = cpu_to_le64(encode_time(&ino->i_mtime));
1502                 l->ctime = cpu_to_le64(encode_time(&ino->i_ctime));
1503                 l->accesstime = cpu_to_le64(encode_time(&i->i_accesstime));
1504                 l->size = cpu_to_le64(ino->i_size);
1505                 l->parent = cpu_to_le32(i->parent);
1506                 l->linkcount = cpu_to_le32(ino->i_nlink);
1507
1508                 switch (li->type) {
1509                 case TypeFile:
1510                         break;
1511                 case TypeDir:
1512                         d->hash_seed = cpu_to_le32(i->seed);
1513                         break;
1514                 case TypeSymlink:
1515                         break;
1516                 case TypeSpecial:
1517                         s->major = cpu_to_le32(MAJOR(ino->i_rdev));
1518                         s->minor = cpu_to_le32(MINOR(ino->i_rdev));
1519                         break;
1520                 }
1521         }
1522         }
1523         unmap_dblock(db, lai);
1524 }
1525
1526 /*-----------------------------------------------------------------------
1527  * Inode allocate map handling.
1528  * Inode 1 of each fileset is a bitmap of free inode numbers.
1529  * Whenever the file is extended in size, new bits are set to one.  They
1530  * are then cleared when the inode is allocated.  When a block becomes
1531  * full of zeros, we don't need to store it any more.
1532  *
1533  * We don't clear the bit until we are committed to creating an inode
1534  * This means we cannot clear it straight away, so two different threads
1535  * might see the same inode number as being available.  We have two
1536  * approaches to guard against this.
1537  * Firstly we have a 'current' pointer into the inodemap file and
1538  * increase that past the inode we return.  This discourages multiple
1539  * hits but as the pointer would need to be rewound occasionally it
1540  * isn't a guarantee.  The guarantee against multiple allocations is done
1541  * via a flag in the block representing an inode.  This is set
1542  * while an inode is being allocated.
1543  */
1544
1545 /* inode number allocation has the prealloc/pin/commit/abort structure
1546  * so it can be committed effectively
1547  */
1548
1549 static int
1550 choose_free_inum(struct fs *fs, struct inode *fsys, u32 *inump,
1551                  struct datablock **bp, int *restarted)
1552 {
1553         struct inode *im = lafs_iget(fsys, 1, SYNC);
1554         loff_t bnum;
1555         struct datablock *b;
1556         char *buf;
1557         int err;
1558         int bit;
1559
1560         if (*bp) {
1561                 struct inode *i = (*bp)->b.inode;
1562                 putdref(*bp, MKREF(cfi_map));
1563                 iput(i);
1564                 *bp = NULL;
1565         }
1566
1567         mutex_lock_nested(&im->i_mutex, I_MUTEX_QUOTA);
1568 retry:
1569         bnum = LAFSI(im)->md.inodemap.thisblock;
1570
1571         if (bnum == NoBlock ||
1572             LAFSI(im)->md.inodemap.nextbit >= (fs->blocksize<<3)) {
1573                 if (bnum == NoBlock)
1574                         bnum = LAFSI(im)->md.inodemap.size;
1575
1576                 if (bnum+1 < LAFSI(im)->md.inodemap.size)
1577                         bnum++;
1578                 else if (!*restarted) {
1579                         bnum = 0;
1580                         *restarted = 1;
1581                 } else {
1582                         /* Need to add a new block to the file */
1583                         bnum = LAFSI(im)->md.inodemap.size;
1584                         b = lafs_get_block(im, bnum, NULL, GFP_KERNEL,
1585                                            MKREF(cfi_map));
1586                         err = -ENOMEM;
1587                         if (!b)
1588                                 goto abort;
1589                         lafs_iolock_written(&b->b);
1590                         set_bit(B_PinPending, &b->b.flags);
1591                         lafs_iounlock_block(&b->b);
1592                 retry2:
1593                         lafs_checkpoint_lock(fs);
1594                         err = lafs_pin_dblock(b, NewSpace);
1595                         if (err == -EAGAIN) {
1596                                 lafs_checkpoint_unlock_wait(fs);
1597                                 goto retry2;
1598                         }
1599                         if (err < 0)
1600                                 goto abort_unlock;
1601
1602                         buf = map_dblock(b);
1603                         /* Set block to "all are free" */
1604                         memset(buf, 0xff, fs->blocksize);
1605                         unmap_dblock(b, buf);
1606                         set_bit(B_Valid, &b->b.flags);
1607                         LAFSI(im)->md.inodemap.size = bnum+1;
1608                         lafs_dirty_inode(im);
1609                         lafs_dirty_dblock(b);
1610                         lafs_checkpoint_unlock(fs);
1611                         putdref(b, MKREF(cfi_map));
1612                 }
1613                 b = NULL;
1614                 err = lafs_find_next(im, &bnum);
1615                 if (err < 0)
1616                         goto abort;
1617                 if (err == 0)
1618                         bnum = 0;
1619
1620                 LAFSI(im)->md.inodemap.nextbit = 0;
1621                 LAFSI(im)->md.inodemap.thisblock = bnum;
1622                 goto retry;
1623         }
1624         b = lafs_get_block(im, bnum, NULL, GFP_KERNEL, MKREF(cfi_map));
1625         err = -ENOSPC;
1626         if (!b)
1627                 goto abort;
1628         err = lafs_find_block(b, NOADOPT);
1629         if (err)
1630                 goto abort;
1631         if (b->b.physaddr == 0 && !test_bit(B_Valid, &b->b.flags)) {
1632                 LAFSI(im)->md.inodemap.nextbit =
1633                         (fs->blocksize<<3) + 1;
1634                 putdref(b,MKREF(cfi_map));
1635                 goto retry;
1636         }
1637         err = lafs_read_block(b);
1638         if (err)
1639                 goto abort;
1640
1641         bit = LAFSI(im)->md.inodemap.nextbit;
1642         LAFSI(im)->md.inodemap.thisblock = bnum;
1643         buf = map_dblock(b);
1644         while (bnum == 0 && bit < 16) {
1645                 /* Never return an inum below 16 - they are special */
1646                 if (!generic_test_le_bit(bit, (unsigned long *)buf))
1647                         generic___clear_le_bit(bit, (unsigned long *)buf);
1648                 bit++;
1649         }
1650
1651         bit = generic_find_next_le_bit((unsigned long *)buf,
1652                                        fs->blocksize<<3, bit);
1653         unmap_dblock(b, buf);
1654         LAFSI(im)->md.inodemap.nextbit = bit+1;
1655         if (bit >= fs->blocksize<<3) {
1656                 putdref(b,MKREF(cfi_map));
1657                 goto retry;
1658         }
1659         mutex_unlock(&im->i_mutex);
1660         *bp = b;
1661         *inump = bit + (bnum << (im->i_blkbits + 3));
1662         return 0;
1663
1664 abort_unlock:
1665         lafs_checkpoint_unlock(fs);
1666 abort:
1667         putdref(b, MKREF(cfi_map));
1668         *bp = NULL;
1669         mutex_unlock(&im->i_mutex);
1670         iput(im);
1671         return err;
1672 }
1673
1674 struct inode_map_new_info {
1675         struct datablock *ib, *mb;
1676 };
1677
1678 static int
1679 inode_map_new_prepare(struct fs *fs, int inum, struct inode *fsys,
1680                       struct inode_map_new_info *imni)
1681 {
1682         int choice = inum;
1683         int restarted = 0;
1684         int err = 0;
1685         struct datablock *b;
1686
1687         imni->ib = imni->mb = NULL;
1688 retry:
1689         if (inum == 0)
1690                 /* choose a possibly-free inode number */
1691                 err = choose_free_inum(fs, fsys, &choice,
1692                                        &imni->mb, &restarted);
1693         if (err)
1694                 return err;
1695
1696         b = lafs_get_block(fsys, choice, NULL, GFP_KERNEL,
1697                            MKREF(cfi_ino));
1698         if (!b)
1699                 return -ENOMEM;
1700
1701         if (test_and_set_bit(B_Claimed, &b->b.flags)) {
1702                 putdref(b, MKREF(cfi_ino));
1703                 if (inum)
1704                         return -EEXIST;
1705                 goto retry;
1706         }
1707         if (imni->mb) {
1708                 lafs_iolock_written(&imni->mb->b);
1709                 set_bit(B_PinPending, &imni->mb->b.flags);
1710                 lafs_iounlock_block(&imni->mb->b);
1711         }
1712         set_bit(B_PinPending, &b->b.flags);
1713         b->my_inode = NULL;
1714         imni->ib = b;
1715         return 0;
1716 }
1717
1718 static int
1719 inode_map_new_pin(struct inode_map_new_info *imni)
1720 {
1721         int err = 0;
1722         if (imni->mb)
1723                 err = lafs_pin_dblock(imni->mb, NewSpace);
1724         err = err ?: lafs_pin_dblock(imni->ib, NewSpace);
1725         return err;
1726 }
1727
1728 static void
1729 inode_map_new_commit(struct inode_map_new_info *imni)
1730 {
1731         unsigned long *buf;
1732
1733         if (imni->mb) {
1734                 int blksize = imni->ib->b.inode->i_sb->s_blocksize;
1735                 int bit = imni->ib->b.fileaddr & (blksize*8 - 1);
1736                 int hole = 0;
1737                 struct inode *ino = imni->mb->b.inode;
1738
1739                 mutex_lock_nested(&ino->i_mutex, I_MUTEX_QUOTA);
1740                 buf = map_dblock(imni->mb);
1741                 generic___clear_le_bit(bit, buf);
1742                 if (buf[blksize/sizeof(*buf)-1] == 0 &&
1743                     generic_find_next_le_bit(buf, blksize*8, 0) == blksize*8)
1744                         /* block is empty, punch a hole */
1745                         hole = 1;
1746
1747                 unmap_dblock(imni->mb, buf);
1748                 if (hole)
1749                         lafs_erase_dblock(imni->mb);
1750                 else
1751                         lafs_dirty_dblock(imni->mb);
1752
1753                 putdref(imni->mb, MKREF(cfi_map));
1754                 mutex_unlock(&ino->i_mutex);
1755                 iput(ino);
1756         }
1757         putdref(imni->ib, MKREF(cfi_ino));
1758 }
1759
1760 static void
1761 inode_map_new_abort(struct inode_map_new_info *imni)
1762 {
1763         if (imni->ib) {
1764                 clear_bit(B_Claimed, &imni->ib->b.flags);
1765                 clear_bit(B_PinPending, &imni->ib->b.flags);
1766                 lafs_orphan_release(fs_from_inode(imni->ib->b.inode),
1767                                     imni->ib);
1768         }
1769         putdref(imni->ib, MKREF(cfi_ino));
1770         if (imni->mb) {
1771                 struct inode *ino = imni->mb->b.inode;
1772                 putdref(imni->mb, MKREF(cfi_map));
1773                 iput(ino);
1774         }
1775 }
1776
1777 struct inode *
1778 lafs_new_inode(struct fs *fs, struct inode *fsys, struct inode *dir,
1779                int type, int inum, int mode, struct datablock **inodbp)
1780 {
1781         /* allocate and instantiate a new inode.  If inum is non-zero,
1782          * choose any number, otherwise we are creating a special inode
1783          * and have to use the given number.
1784          * This creation is committed independently of any name that might
1785          * subsequently be given to the inode.  So we register it as an
1786          * orphan so that it will be cleaned up if the name isn't
1787          * successfully created
1788          *
1789          */
1790         struct inode *ino;
1791         struct datablock *b;
1792         struct inode_map_new_info imni;
1793         struct update_handle ui;
1794         int err;
1795
1796         err = inode_map_new_prepare(fs, inum, fsys, &imni);
1797         err = lafs_cluster_update_prepare(&ui, fs, sizeof(struct la_inode))
1798                 ?: err;
1799         if (err == 0)
1800                 err = lafs_make_orphan(fs, imni.ib);
1801         if (err)
1802                 goto abort;
1803 retry:
1804         lafs_checkpoint_lock(fs);
1805
1806         err = inode_map_new_pin(&imni);
1807
1808         if (err == -EAGAIN) {
1809                 lafs_checkpoint_unlock_wait(fs);
1810                 goto retry;
1811         }
1812         if (err < 0)
1813                 goto abort_unlock;
1814
1815         b = getdref(imni.ib, MKREF(inode_new));
1816
1817         lafs_iolock_block(&b->b); /* make sure we don't race with the cleaner
1818                                    * and zero this inode while trying to load it
1819                                    */
1820         lafs_inode_init(b, type, mode, dir);
1821         lafs_iounlock_block(&b->b);
1822
1823         inode_map_new_commit(&imni);
1824         ino = lafs_iget(fsys, b->b.fileaddr, SYNC);
1825         if (IS_ERR(ino)) {
1826                 lafs_cluster_update_abort(&ui);
1827                 LAFS_BUG(1, &b->b);
1828         } else
1829                 lafs_cluster_update_commit(&ui, b, 0,
1830                                            LAFSI(ino)->metadata_size);
1831         LAFS_BUG(LAFSI(ino)->dblock != b, &b->b);
1832         LAFS_BUG(b->my_inode != ino, &b->b);
1833         lafs_checkpoint_unlock(fs);
1834
1835         if (inodbp)
1836                 *inodbp = b;
1837         else
1838                 putdref(b, MKREF(inode_new));
1839         return ino;
1840
1841 abort_unlock:
1842         lafs_checkpoint_unlock(fs);
1843         err = -ENOSPC;
1844 abort:
1845         inode_map_new_abort(&imni);
1846         lafs_cluster_update_abort(&ui);
1847         dprintk("After abort %d: %s\n", err, strblk(&imni.ib->b));
1848         return ERR_PTR(err);
1849 }
1850
1851 static int inode_map_free(struct fs *fs, struct inode *fsys, u32 inum)
1852 {
1853         struct inode *im = lafs_iget(fsys, 1, SYNC);
1854         int bit;
1855         unsigned long *buf;
1856         struct datablock *b;
1857         u32 bnum;
1858         int err;
1859
1860         mutex_lock_nested(&im->i_mutex, I_MUTEX_QUOTA);
1861
1862         bnum = inum >> (3 + fs->blocksize_bits);
1863         bit = inum - (bnum << (3 + fs->blocksize_bits));
1864         b = lafs_get_block(im, bnum, NULL, GFP_KERNEL, MKREF(inode_map_free));
1865         if (!b) {
1866                 mutex_unlock(&im->i_mutex);
1867                 iput(im);
1868                 return -ENOMEM;
1869         }
1870         err = lafs_read_block(b);
1871         if (err) {
1872                 putdref(b, MKREF(inode_map_free));
1873                 mutex_unlock(&im->i_mutex);
1874                 iput(im);
1875                 return err;
1876         }
1877         lafs_iolock_written(&b->b);
1878         set_bit(B_PinPending, &b->b.flags);
1879         lafs_iounlock_block(&b->b);
1880 retry:
1881         lafs_checkpoint_lock(fs);
1882         err = lafs_pin_dblock(b, ReleaseSpace);
1883         if (err == -EAGAIN) {
1884                 lafs_checkpoint_unlock_wait(fs);
1885                 goto retry;
1886         }
1887         BUG_ON(err < 0);
1888         buf = map_dblock(b);
1889         generic___set_le_bit(bit, buf);
1890         unmap_dblock(b, buf);
1891         lafs_dirty_dblock(b);
1892         putdref(b, MKREF(inode_map_free));
1893         lafs_checkpoint_unlock(fs);
1894         mutex_unlock(&im->i_mutex);
1895         iput(im);
1896         return 0;
1897 }
1898
1899 int lafs_inode_inuse(struct fs *fs, struct inode *fsys, u32 inum)
1900 {
1901         /* This is used during roll-forward to register a newly created
1902          * inode in the inode map
1903          */
1904         struct inode *im = lafs_iget(fsys, 1, SYNC);
1905         int bit;
1906         unsigned long *buf;
1907         struct datablock *b;
1908         u32 bnum;
1909         int err;
1910
1911         mutex_lock_nested(&im->i_mutex, I_MUTEX_QUOTA);
1912
1913         bnum = inum >> (3 + fs->blocksize_bits);
1914         bit = inum - (bnum << (3 + fs->blocksize_bits));
1915         if (bnum > LAFSI(im)->md.inodemap.size) {
1916                 /* inum to unbelievably big */
1917                 mutex_unlock(&im->i_mutex);
1918                 iput(im);
1919                 return -EINVAL;
1920         }
1921         b = lafs_get_block(im, bnum, NULL, GFP_KERNEL, MKREF(inode_map_free));
1922         if (!b) {
1923                 mutex_unlock(&im->i_mutex);
1924                 iput(im);
1925                 return -ENOMEM;
1926         }
1927
1928         err = lafs_read_block(b);
1929         if (err) {
1930                 putdref(b, MKREF(inode_map_free));
1931                 mutex_unlock(&im->i_mutex);
1932                 iput(im);
1933                 return err;
1934         }
1935
1936         lafs_iolock_written(&b->b);
1937         set_bit(B_PinPending, &b->b.flags);
1938         lafs_iounlock_block(&b->b);
1939 retry:
1940         lafs_checkpoint_lock(fs);
1941         err = lafs_pin_dblock(b, CleanSpace);
1942         if (err == -EAGAIN) {
1943                 lafs_checkpoint_unlock_wait(fs);
1944                 goto retry;
1945         }
1946         BUG_ON(err < 0);
1947         buf = map_dblock(b);
1948         if (bnum == LAFSI(im)->md.inodemap.size) {
1949                 /* need to add a new block to the file */
1950                 memset(buf, 0xff, fs->blocksize);
1951                 LAFSI(im)->md.inodemap.size = bnum + 1;
1952                 lafs_dirty_inode(im);
1953         }
1954         generic___clear_le_bit(bit, buf);
1955         unmap_dblock(b, buf);
1956         lafs_dirty_dblock(b);
1957         putdref(b, MKREF(inode_map_free));
1958         lafs_checkpoint_unlock(fs);
1959         mutex_unlock(&im->i_mutex);
1960         iput(im);
1961         return 0;
1962 }
1963
1964
1965
1966 int lafs_setattr(struct dentry *dentry, struct iattr *attr)
1967 {
1968         int err;
1969         struct inode *ino = dentry->d_inode;
1970         struct fs *fs = fs_from_inode(ino);
1971         struct datablock *db;
1972
1973         err = inode_change_ok(ino, attr);
1974         db = lafs_inode_dblock(ino, SYNC, MKREF(setattr));
1975         if (IS_ERR(db))
1976                 err = PTR_ERR(db);
1977         if (err)
1978                 return err;
1979
1980         /* We don't need iolock_written here as we don't
1981          * actually change the inode block yet
1982          */
1983         lafs_iolock_block(&db->b);
1984         set_bit(B_PinPending, &db->b.flags);
1985         lafs_iounlock_block(&db->b);
1986
1987         /* FIXME quota stuff */
1988
1989 again:
1990         lafs_checkpoint_lock(fs);
1991         err = lafs_pin_dblock(db, ReleaseSpace);
1992         if (err == -EAGAIN) {
1993                 lafs_checkpoint_unlock_wait(fs);
1994                 goto again;
1995         }
1996
1997         if (!err) {
1998                 if ((attr->ia_valid & ATTR_SIZE) &&
1999                     attr->ia_size != i_size_read(ino))
2000                         truncate_setsize(ino, attr->ia_size);
2001                 setattr_copy(ino, attr);
2002                 mark_inode_dirty(ino);
2003
2004                 lafs_dirty_dblock(db);
2005         }
2006         clear_bit(B_PinPending, &db->b.flags);
2007         putdref(db, MKREF(setattr));
2008         lafs_checkpoint_unlock(fs);
2009
2010         return err;
2011 }
2012
2013 void lafs_truncate(struct inode *ino)
2014 {
2015         /* Want to truncate this file.
2016          * i_size has already been changed, and the address space
2017          * has been cleaned up.
2018          * So just start the background truncate
2019          */
2020         struct fs *fs = fs_from_inode(ino);
2021         struct datablock *db = lafs_inode_dblock(ino, SYNC, MKREF(trunc));
2022         loff_t trunc_block;
2023         DEFINE_WAIT(wq);
2024
2025         if (IS_ERR(db))
2026                 return;
2027
2028         trunc_block = ((i_size_read(ino) + fs->blocksize - 1)
2029                        >> fs->blocksize_bits);
2030         /* We hold i_mutex, so regular orphan processing cannot
2031          * contine - we have to push it forward ourselves.
2032          */
2033         while (test_bit(I_Trunc, &LAFSI(ino)->iflags) &&
2034                LAFSI(ino)->trunc_next < trunc_block) {
2035                 prepare_to_wait(&fs->async_complete, &wq,
2036                                 TASK_UNINTERRUPTIBLE);
2037                 lafs_inode_handle_orphan(db);
2038                 if (test_bit(B_Orphan, &db->b.flags))
2039                         schedule();
2040         }
2041         finish_wait(&fs->async_complete, &wq);
2042
2043         /* There is nothing we can do about errors here.  The
2044          * most likely are ENOMEM which itself is very unlikely.
2045          * If this doesn't get registered as an orphan .... maybe
2046          * it will have to wait until something else truncates it.
2047          */
2048         lafs_make_orphan(fs, db);
2049
2050         if (!test_and_set_bit(I_Trunc, &LAFSI(ino)->iflags))
2051                 lafs_igrab_fs(ino);
2052         if (trunc_block == 0)
2053                 LAFSI(ino)->trunc_gen++;
2054         LAFSI(ino)->trunc_next = trunc_block;
2055         putdref(db, MKREF(trunc));
2056 }
2057
2058 const struct inode_operations lafs_special_ino_operations = {
2059         .setattr        = lafs_setattr,
2060         .getattr        = lafs_getattr,
2061         .truncate       = lafs_truncate,
2062 };