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