4 * Copyright (C) 2005-2009
5 * Neil Brown <neilb@suse.de>
6 * Released under the GPL, version 2
8 * generic inode handling
13 #include <linux/random.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
17 static void check_atime_ref(struct inode *ino, int async);
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
45 static int sync_itest(struct inode *inode, void *data)
47 struct ikey *ik = data;
49 if (inode->i_ino != ik->inum ||
50 LAFSI(inode)->filesys != ik->fsys)
55 static int async_itest(struct inode *inode, void *data)
57 struct ikey *ik = data;
60 /* found and is freeing */
62 if (!sync_itest(inode, data))
64 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW)) {
71 static int iset(struct inode *inode, void *data)
73 struct ikey *ik = data;
76 inode->i_ino = ik->inum;
77 LAFSI(inode)->filesys = ik->fsys;
82 lafs_iget(struct inode *fsys, ino_t inum, int async)
84 /* find, and load if needed, this inum */
85 struct inode *ino = NULL;
87 struct datablock *b = NULL;
88 struct ikey ik = { .inum = inum, .fsys = fsys, };
90 struct super_block *sb = fsys->i_sb;
93 /* We cannot afford to block on 'freeing_inode'
94 * So use iget5_locked and refuse to match such
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.
103 ino = ilookup5(sb, inum, async_itest, &ik);
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.
115 b = lafs_get_block(fsys, inum, NULL,
116 GFP_NOFS, MKREF(iget));
118 return ERR_PTR(-ENOMEM);
121 err = lafs_read_block_async(b);
124 /* Have the block, so safe to iget */
125 ino = iget5_locked(sb, inum,
136 if (test_and_set_bit(B_Async, &b->b.flags)) {
137 putdref(b, MKREF(iget));
140 getdref(b, MKREF(async));
144 ino = iget5_locked(sb, inum, sync_itest, iset, &ik);
147 putdref(b, MKREF(iget));
148 return ERR_PTR(-ENOMEM);
151 if (!(ino->i_state & I_NEW)) {
152 putdref(b, MKREF(iget));
154 check_atime_ref(ino, async);
158 return ERR_PTR(-ENOENT);
161 igrab(LAFSI(ino)->filesys);
163 /* surprisingly the inode bdi does not default to the
164 * super_blocks bdi...
166 ino->i_data.backing_dev_info = sb->s_bdi;
167 /* Need to load block 'inum' from an inode file...
170 b = lafs_get_block(fsys, inum, NULL, GFP_KERNEL, MKREF(iget));
174 err = lafs_read_block(b);
179 oldino = rcu_my_inode(b);
181 /* The inode is new, but the block thinks it has an
182 * old inode, so we must be in the process of destroying
184 * So fail the lookup without even looking at the content
185 * of the block (Which might not be clear yet).
187 spin_lock(&oldino->i_data.private_lock);
188 if (!test_bit(I_Deleting, &LAFSI(oldino)->iflags)) {
190 LAFSI(oldino)->dblock = NULL;
191 LAFSI(oldino)->iblock = NULL;
193 spin_unlock(&oldino->i_data.private_lock);
201 err = lafs_import_inode(ino, b);
204 printk("lafs_import_inode failed %d\n", err);
207 check_atime_ref(ino, async);
208 unlock_new_inode(ino);
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));
214 putdref(b, MKREF(iget));
218 unlock_new_inode(ino);
225 lafs_iget_fs(struct fs *fs, int fsnum, int inum, int async)
227 struct super_block *sb;
233 /* Need to locate or load the superblock for this
234 * subordinate filesystem
236 struct inode *filesys;
238 filesys = lafs_iget(fs->ss[0].root, fsnum, async);
241 if (LAFSI(filesys)->type != TypeInodeFile) {
243 return ERR_PTR(-ENOENT);
245 rv = lafs_iget(filesys, inum, async);
247 rv = lafs_iget(fs->ss[0].root, inum, async);
249 atomic_inc(&sb->s_active);
251 rv = igrab(fs->ss[0].root);
252 atomic_inc(&sb->s_active);
258 lafs_import_inode(struct inode *ino, struct datablock *b)
260 struct la_inode *lai = map_dblock(b);
261 struct lafs_inode *li = LAFSI(ino);
264 if (lai->filetype == 0) {
271 ino->i_mode = S_IFREG;
272 ino->i_nlink = 1; /* For special file, set nlink so they
273 * never appear unlinked */
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);
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;
293 dprintk("inode %lu type is %d\n", (unsigned long)ino->i_ino, li->type);
295 ino->i_data.a_ops = &lafs_file_aops;
301 struct fs_md *i = &li->md.fs;
302 struct fs_metadata *l = &lai->metadata[0].fs;
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;
326 /* Need to unmap the dblock to kmalloc because
327 * the mapping makes us 'atomic'
329 unmap_dblock(b, lai);
330 i->name = kmalloc(nlen+1, GFP_KERNEL);
332 l = &lai->metadata[0].fs;
337 memcpy(i->name, l->name, nlen);
340 /* Make this look like a directory */
341 ino->i_mode = S_IFDIR;
345 ino->i_op = &lafs_subset_ino_operations;
346 ino->i_fop = &lafs_subset_file_operations;
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;
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);
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);
378 struct orphan_md *m = &li->md.orphan;
379 /* This will be set via lafs_count_orphans */
387 default: /* TypeBase or larger */
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;
394 if (li->type < TypeBase)
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
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
424 /* FIXME set a superblock flag requesting
425 * directory linkage checking
430 dprintk(" mode = 0%o uid %d size %lld\n",
431 ino->i_mode, ino->i_uid, ino->i_size);
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;
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;
445 dprintk("Hmm. %d %d %d\n",
452 ino->i_op = &lafs_link_ino_operations;
453 ino->i_mode = (ino->i_mode & 07777) | S_IFLNK;
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);
467 ino->i_blkbits = ino->i_sb->s_blocksize_bits;
468 /* FIXME i_blocks and i_byte - used for quota?? */
471 /* Note: no refcount yet. Either will remove the reference to the
475 rcu_assign_pointer(b->my_inode, ino);
479 printk("inode %lu type is %d\n",
480 (unsigned long)ino->i_ino, li->type);
481 unmap_dblock(b, lai);
485 static void check_atime_ref(struct inode *ino, int async)
487 /* If there is an atime file in this filesystem the inode
488 * should hold a reference to the relevant block in
491 struct inode *root, *at;
495 /* Never bother for async lookups */
497 if (LAFSI(ino)->type < TypeBase)
499 if (test_bit(I_AccessTime, &LAFSI(ino)->iflags))
501 root = LAFSI(ino)->filesys;
502 at = LAFSI(root)->md.fs.accesstime;
506 if (LAFSI(ino)->md.file.atime_offset)
507 LAFSI(ino)->md.file.atime_offset = 0;
509 /* "* 2" to get byte number, then shift to get block
510 * number. So just shift by 1 less than blkbits.
512 bnum = ino->i_ino >> (at->i_blkbits-1);
513 b = lafs_get_block(at, bnum, NULL, GFP_NOFS, MKREF(atime));
515 if (lafs_read_block(b) == 0) {
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);
526 putdref(b, MKREF(atime));
530 void lafs_add_atime_offset(struct timespec *atime, int offset)
537 expon = offset & 0x1f;
539 mantissa = (offset >> 5) | 0x800;
541 mantissa = (offset >> 5);
544 mantissa <<= expon-11;
545 atime->tv_sec += mantissa;
549 mantissa <<= expon-1;
550 timespec_add_ns(atime, (s64)mantissa * 1000000);
554 static int normalise(int *mantissa)
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.
561 while (*mantissa >= 4096) {
568 static int update_atime_delta(struct inode *ino)
570 /* calculate new delta to show the difference between
571 * i_atime and i_accesstime
574 if (LAFSI(ino)->type < TypeBase)
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
583 struct timespec diff;
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 */
591 shift = normalise(&rv) + 11;
593 /* Store the milliseconds */
594 int rv = diff.tv_nsec / 1000000;
595 rv += diff.tv_sec * 1000;
597 shift = normalise(&rv) + 1;
609 if (LAFSI(ino)->md.file.atime_offset == rv)
612 LAFSI(ino)->md.file.atime_offset = rv;
616 static void store_atime_delta(struct inode *ino)
624 if (!test_bit(I_AccessTime, &LAFSI(ino)->iflags))
625 /* sorry, nothing we can do here */
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));
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));
644 unmap_dblock(b, atp);
645 putdref(b, MKREF(store_atime));
648 void lafs_inode_checkpin(struct inode *ino)
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.
655 * This needs to be called whenever we change
656 * i_nlink, and whenever we pin or unpin an InoIdx
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)
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))
675 spin_unlock(&ino->i_data.private_lock);
678 if (!test_and_set_bit(I_Pinned, &LAFSI(ino)->iflags)) {
679 if (ino->i_sb->s_type == &lafs_fs_type)
685 if (test_and_clear_bit(I_Pinned, &LAFSI(ino)->iflags)) {
686 if (ino->i_sb->s_type == &lafs_fs_type)
695 struct datablock *lafs_inode_get_dblock(struct inode *ino, REFARG)
697 struct datablock *db;
699 spin_lock(&ino->i_data.private_lock);
700 db = LAFSI(ino)->dblock;
702 if (db->b.inode == ino)
703 getdref_locked(db, REF);
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);
710 spin_unlock(&ino->i_data.private_lock);
714 struct datablock *lafs_inode_dblock(struct inode *ino, int async, REFARG)
716 struct datablock *db;
719 db = lafs_inode_get_dblock(ino, REF);
722 db = lafs_get_block(LAFSI(ino)->filesys, ino->i_ino, NULL,
725 return ERR_PTR(-ENOMEM);
727 LAFSI(ino)->dblock = db;
728 rcu_assign_pointer(db->my_inode, ino);
730 err = lafs_read_block_async(db);
732 err = lafs_read_block(db);
740 void lafs_inode_init(struct datablock *b, int type, int mode, struct inode *dir)
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
747 struct fs *fs = fs_from_inode(b->b.inode);
748 struct la_inode *lai = map_dblock(b);
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));
756 lai->filetype = type;
762 struct fs_metadata *l = &lai->metadata[0].fs;
763 size = sizeof(struct fs_metadata);
766 l->blocks_allowed = 0;
767 l->creation_age = fs->wc[0].cluster_seq;
769 l->quota_inodes[0] = 0;
770 l->quota_inodes[1] = 0;
771 l->quota_inodes[2] = 0;
772 l->snapshot_usage_table = 0;
774 /* name will be zero length and not used */
779 struct inodemap_metadata *l = &lai->metadata[0].inodemap;
781 size = sizeof(struct inodemap_metadata);
785 size = sizeof(struct su_metadata);
788 size = sizeof(struct quota_metadata);
798 struct file_metadata *l = &lai->metadata[0].file;
799 struct timespec now = CURRENT_TIME;
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);
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);
812 l->treeid = l->userid;
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;
820 l->parent = dir ? cpu_to_le32(dir->i_ino) : 0;
823 if (type == TypeDir) {
824 struct dir_metadata *l = &lai->metadata[0].dir;
826 get_random_bytes(&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);
836 size = sizeof(struct file_metadata);
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);
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);
850 static int inode_map_free(struct fs *fs, struct inode *fsys, u32 inum);
852 void lafs_evict_inode(struct inode *ino)
854 struct fs *fs = fs_from_inode(ino);
855 struct lafs_inode *li = LAFSI(ino);
857 if (ino->i_mode == 0) {
858 /* There never was an inode here,
860 * We just call end_writeback to get the
861 * flags set properly.
867 dprintk("EVICT INODE %d\n", (int)ino->i_ino);
870 /* Normal truncation holds an igrab, so we cannot be
871 * deleted until any truncation finishes
873 BUG_ON(test_bit(I_Trunc, &LAFSI(ino)->iflags));
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);
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));
890 inode_map_free(fs, LAFSI(ino)->filesys, ino->i_ino);
892 truncate_inode_pages(&ino->i_data, 0);
895 dprintk("CLEAR INODE %d\n", (int)ino->i_ino);
899 /* Now is a good time to break the linkage between
900 * inode and dblock - but not if the file is
903 if (!test_bit(I_Deleting, &li->iflags)) {
904 struct datablock *db;
905 spin_lock(&ino->i_data.private_lock);
908 struct indexblock *ib = li->iblock;
909 LAFS_BUG(ib && atomic_read(&ib->b.refcnt), &db->b);
914 spin_unlock(&ino->i_data.private_lock);
917 /* FIXME release quota inodes if filesystem */
920 static int prune(void *data, u32 addr, u64 paddr, int len)
922 /* This whole index block is being pruned, just account
923 * for everything and it will be cleared afterwards
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);
930 dprintk("PRUNE %d for %d at %lld\n", addr, len, (long long)paddr);
931 if (paddr == 0 || len == 0)
933 for (i = 0 ; i < len ; i++)
934 lafs_summary_update(fs, ino, paddr+i, 0, 0, ph, 0);
938 static int prune_some(void *data, u32 addr, u64 paddr, int len)
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??
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);
952 if (paddr == 0 || len == 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.
960 if (addr + i < LAFSI(ino)->trunc_next)
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)) {
968 a->fileaddr = addr + i;
971 LAFS_BUG(!test_bit(B_Pinned, &ib->b.flags), &ib->b);
972 ib->uninc_table.pending_cnt++;
974 spin_unlock(&ino->i_data.private_lock);
977 spin_unlock(&ino->i_data.private_lock);
978 lafs_summary_update(fs, ino, paddr+i, 0, 0, ph, 0);
983 int lafs_inode_handle_orphan(struct datablock *b)
985 /* Don't need rcu protection for my_inode run_orphan
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;
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",
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);
1020 lafs_orphan_forget(fs, b);
1024 ib = lafs_make_iblock(ino, ADOPT, SYNC, MKREF(inode_handle_orphan));
1028 /* Here is the guts of 'truncate'. We find the next leaf index
1029 * block and discard all the addresses there-in.
1031 trunc_next = LAFSI(ino)->trunc_next;
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
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
1044 struct indexblock *tmp;
1045 struct indexblock *next;
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);
1053 wake_up(&fs->trunc_wait);
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
1062 err = -EBUSY; /* Try again after the checkpoint */
1066 lastaddr = (i_size_read(ino) +
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.
1077 spin_lock(&ib->b.inode->i_data.private_lock);
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))
1086 tmp->b.fileaddr > next->b.fileaddr)
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);
1095 wake_up(&fs->trunc_wait);
1099 getiref(ib2, MKREF(inode_handle_orphan2));
1100 spin_unlock(&ib->b.inode->i_data.private_lock);
1102 /* ib2 is an index block beyond EOF with no
1104 * Incorporating it should unpin it.
1106 if (!list_empty(&ib2->children)) {
1107 lafs_print_tree(&ib2->b, 3);
1108 LAFS_BUG(1, &ib2->b);
1111 if (!lafs_iolock_written_async(&ib2->b)) {
1112 putiref(ib2, MKREF(inode_handle_orphan2));
1116 while (ib2->uninc_table.pending_cnt || ib2->uninc)
1117 lafs_incorporate(fs, ib2);
1119 if (test_bit(B_Dirty, &ib2->b.flags) ||
1120 test_bit(B_Realloc, &ib2->b.flags))
1121 lafs_cluster_allocate(&ib2->b, 0);
1123 lafs_iounlock_block(&ib2->b);
1125 if (!list_empty(&ib2->b.siblings)) {
1126 printk("looping on %s\n", strblk(&ib2->b));
1131 putiref(ib2, MKREF(inode_handle_orphan2));
1134 if (lafs_iolock_written_async(&ib->b)) {
1136 lafs_incorporate(fs, ib);
1137 lafs_iounlock_block(&ib->b);
1142 putiref(ib, MKREF(inode_handle_orphan));
1146 putiref(ib, MKREF(inode_handle_orphan));
1148 ib = lafs_leaf_find(ino, trunc_next, ADOPT, &next_trunc,
1149 ASYNC, MKREF(inode_handle_orphan3));
1152 /* now hold an iolock on ib */
1154 /* Ok, trunc_next seems to refer to a block that exists.
1155 * We need to erase it..
1157 * So we open up the index block ourselves, call
1158 * lafs_summary_update with each block address, and then
1162 if (LAFSI(ino)->depth == 0) {
1163 /* Nothing to truncate */
1164 clear_bit(I_Trunc, &LAFSI(ino)->iflags);
1166 if (test_bit(B_Pinned, &ib->b.flags))
1167 /* Need to move the dirtiness which keeps this
1168 * pinned to the data block.
1170 lafs_cluster_allocate(&ib->b, 0);
1172 lafs_iounlock_block(&ib->b);
1177 lafs_checkpoint_lock(fs);
1178 err = lafs_reserve_block(&ib->b, ReleaseSpace);
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
1186 LAFSI(ino)->trunc_next = next_trunc;
1187 lafs_cluster_allocate(&ib->b, 0);
1192 lafs_pin_block(&ib->b);
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
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));
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
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
1223 lafs_walk_leaf_index(ib, prune_some, ib);
1225 if (test_bit(B_Dirty, &ib->b.flags))
1226 lafs_incorporate(fs, ib);
1230 LAFSI(ino)->trunc_next = next_trunc;
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.
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);
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);
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);
1254 lafs_iounlock_block(&ib->b);
1256 lafs_checkpoint_unlock(fs);
1258 putiref(ib, MKREF(inode_handle_orphan3));
1262 void lafs_dirty_inode(struct inode *ino)
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
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.
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.
1290 * We also use this opportunity to update the filesystem modify time.
1292 struct timespec now;
1293 struct inode *filesys;
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))
1302 spin_unlock(&ino->i_data.private_lock);
1306 if (update_atime_delta(ino))
1307 store_atime_delta(ino);
1311 set_bit(I_Dirty, &LAFSI(ino)->iflags);
1312 ino->i_sb->s_dirt = 1;
1314 if (LAFSI(ino)->type < TypeBase)
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);
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);
1330 int lafs_sync_inode(struct inode *ino, int wait)
1332 /* fsync has been called on this file so we need
1333 * to sync any inode updates to the next cluster.
1335 * If we cannot create an update record,
1336 * we wait for a phase change, which writes everything
1339 struct datablock *b;
1340 struct fs *fs = fs_from_inode(ino);
1341 struct update_handle uh;
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);
1354 LAFSI(ino)->update_cluster = 0;
1355 if (!test_bit(I_Dirty, &LAFSI(ino)->iflags))
1357 b = lafs_inode_dblock(ino, SYNC, MKREF(write_inode));
1361 lafs_iolock_written(&b->b);
1362 lafs_inode_fillblock(ino);
1363 lafs_iounlock_block(&b->b);
1365 err = lafs_cluster_update_prepare(&uh, fs, LAFS_INODE_LOG_SIZE);
1367 lafs_cluster_update_abort(&uh);
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);
1378 lafs_cluster_update_abort(&uh);
1379 lafs_checkpoint_unlock(fs);
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 ?
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);
1394 putdref(b, MKREF(write_inode));
1395 return 0; /* FIXME should I return some error message??? */
1398 void lafs_inode_fillblock(struct inode *ino)
1400 /* copy data from ino into the related data block */
1402 struct lafs_inode *li = LAFSI(ino);
1403 struct datablock *db = li->dblock;
1404 struct la_inode *lai;
1406 clear_bit(I_Dirty, &LAFSI(ino)->iflags);
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
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);
1429 lai->depth = li->depth;
1434 struct fs_md *i = &li->md.fs;
1435 struct fs_metadata *l = &lai->metadata[0].fs;
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)
1452 else if (strlen(i->name) < nlen)
1453 nlen = strlen(i->name);
1454 memcpy(l->name, i->name, nlen);
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);
1466 case TypeSegmentMap:
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);
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);
1482 case TypeOrphanList:
1483 case TypeAccessTime:
1486 default: /* TypeBase or larger */
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;
1493 if (li->type < TypeBase)
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);
1512 d->hash_seed = cpu_to_le32(i->seed);
1517 s->major = cpu_to_le32(MAJOR(ino->i_rdev));
1518 s->minor = cpu_to_le32(MINOR(ino->i_rdev));
1523 unmap_dblock(db, lai);
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.
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.
1545 /* inode number allocation has the prealloc/pin/commit/abort structure
1546 * so it can be committed effectively
1550 choose_free_inum(struct fs *fs, struct inode *fsys, u32 *inump,
1551 struct datablock **bp, int *restarted)
1553 struct inode *im = lafs_iget(fsys, 1, SYNC);
1555 struct datablock *b;
1561 struct inode *i = (*bp)->b.inode;
1562 putdref(*bp, MKREF(cfi_map));
1567 mutex_lock_nested(&im->i_mutex, I_MUTEX_QUOTA);
1569 bnum = LAFSI(im)->md.inodemap.thisblock;
1571 if (bnum == NoBlock ||
1572 LAFSI(im)->md.inodemap.nextbit >= (fs->blocksize<<3)) {
1573 if (bnum == NoBlock)
1574 bnum = LAFSI(im)->md.inodemap.size;
1576 if (bnum+1 < LAFSI(im)->md.inodemap.size)
1578 else if (!*restarted) {
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,
1589 lafs_iolock_written(&b->b);
1590 set_bit(B_PinPending, &b->b.flags);
1591 lafs_iounlock_block(&b->b);
1593 lafs_checkpoint_lock(fs);
1594 err = lafs_pin_dblock(b, NewSpace);
1595 if (err == -EAGAIN) {
1596 lafs_checkpoint_unlock_wait(fs);
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));
1614 err = lafs_find_next(im, &bnum);
1620 LAFSI(im)->md.inodemap.nextbit = 0;
1621 LAFSI(im)->md.inodemap.thisblock = bnum;
1624 b = lafs_get_block(im, bnum, NULL, GFP_KERNEL, MKREF(cfi_map));
1628 err = lafs_find_block(b, NOADOPT);
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));
1637 err = lafs_read_block(b);
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);
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));
1659 mutex_unlock(&im->i_mutex);
1661 *inump = bit + (bnum << (im->i_blkbits + 3));
1665 lafs_checkpoint_unlock(fs);
1667 putdref(b, MKREF(cfi_map));
1669 mutex_unlock(&im->i_mutex);
1674 struct inode_map_new_info {
1675 struct datablock *ib, *mb;
1679 inode_map_new_prepare(struct fs *fs, int inum, struct inode *fsys,
1680 struct inode_map_new_info *imni)
1685 struct datablock *b;
1687 imni->ib = imni->mb = NULL;
1690 /* choose a possibly-free inode number */
1691 err = choose_free_inum(fs, fsys, &choice,
1692 &imni->mb, &restarted);
1696 b = lafs_get_block(fsys, choice, NULL, GFP_KERNEL,
1701 if (test_and_set_bit(B_Claimed, &b->b.flags)) {
1702 putdref(b, MKREF(cfi_ino));
1708 lafs_iolock_written(&imni->mb->b);
1709 set_bit(B_PinPending, &imni->mb->b.flags);
1710 lafs_iounlock_block(&imni->mb->b);
1712 set_bit(B_PinPending, &b->b.flags);
1719 inode_map_new_pin(struct inode_map_new_info *imni)
1723 err = lafs_pin_dblock(imni->mb, NewSpace);
1724 err = err ?: lafs_pin_dblock(imni->ib, NewSpace);
1729 inode_map_new_commit(struct inode_map_new_info *imni)
1734 int blksize = imni->ib->b.inode->i_sb->s_blocksize;
1735 int bit = imni->ib->b.fileaddr & (blksize*8 - 1);
1737 struct inode *ino = imni->mb->b.inode;
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 */
1747 unmap_dblock(imni->mb, buf);
1749 lafs_erase_dblock(imni->mb);
1751 lafs_dirty_dblock(imni->mb);
1753 putdref(imni->mb, MKREF(cfi_map));
1754 mutex_unlock(&ino->i_mutex);
1757 putdref(imni->ib, MKREF(cfi_ino));
1761 inode_map_new_abort(struct inode_map_new_info *imni)
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),
1769 putdref(imni->ib, MKREF(cfi_ino));
1771 struct inode *ino = imni->mb->b.inode;
1772 putdref(imni->mb, MKREF(cfi_map));
1778 lafs_new_inode(struct fs *fs, struct inode *fsys, struct inode *dir,
1779 int type, int inum, int mode, struct datablock **inodbp)
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
1791 struct datablock *b;
1792 struct inode_map_new_info imni;
1793 struct update_handle ui;
1796 err = inode_map_new_prepare(fs, inum, fsys, &imni);
1797 err = lafs_cluster_update_prepare(&ui, fs, sizeof(struct la_inode))
1800 err = lafs_make_orphan(fs, imni.ib);
1804 lafs_checkpoint_lock(fs);
1806 err = inode_map_new_pin(&imni);
1808 if (err == -EAGAIN) {
1809 lafs_checkpoint_unlock_wait(fs);
1815 b = getdref(imni.ib, MKREF(inode_new));
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
1820 lafs_inode_init(b, type, mode, dir);
1821 lafs_iounlock_block(&b->b);
1823 inode_map_new_commit(&imni);
1824 ino = lafs_iget(fsys, b->b.fileaddr, SYNC);
1826 lafs_cluster_update_abort(&ui);
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);
1838 putdref(b, MKREF(inode_new));
1842 lafs_checkpoint_unlock(fs);
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);
1851 static int inode_map_free(struct fs *fs, struct inode *fsys, u32 inum)
1853 struct inode *im = lafs_iget(fsys, 1, SYNC);
1856 struct datablock *b;
1860 mutex_lock_nested(&im->i_mutex, I_MUTEX_QUOTA);
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));
1866 mutex_unlock(&im->i_mutex);
1870 err = lafs_read_block(b);
1872 putdref(b, MKREF(inode_map_free));
1873 mutex_unlock(&im->i_mutex);
1877 lafs_iolock_written(&b->b);
1878 set_bit(B_PinPending, &b->b.flags);
1879 lafs_iounlock_block(&b->b);
1881 lafs_checkpoint_lock(fs);
1882 err = lafs_pin_dblock(b, ReleaseSpace);
1883 if (err == -EAGAIN) {
1884 lafs_checkpoint_unlock_wait(fs);
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);
1899 int lafs_inode_inuse(struct fs *fs, struct inode *fsys, u32 inum)
1901 /* This is used during roll-forward to register a newly created
1902 * inode in the inode map
1904 struct inode *im = lafs_iget(fsys, 1, SYNC);
1907 struct datablock *b;
1911 mutex_lock_nested(&im->i_mutex, I_MUTEX_QUOTA);
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);
1921 b = lafs_get_block(im, bnum, NULL, GFP_KERNEL, MKREF(inode_map_free));
1923 mutex_unlock(&im->i_mutex);
1928 err = lafs_read_block(b);
1930 putdref(b, MKREF(inode_map_free));
1931 mutex_unlock(&im->i_mutex);
1936 lafs_iolock_written(&b->b);
1937 set_bit(B_PinPending, &b->b.flags);
1938 lafs_iounlock_block(&b->b);
1940 lafs_checkpoint_lock(fs);
1941 err = lafs_pin_dblock(b, CleanSpace);
1942 if (err == -EAGAIN) {
1943 lafs_checkpoint_unlock_wait(fs);
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);
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);
1966 int lafs_setattr(struct dentry *dentry, struct iattr *attr)
1969 struct inode *ino = dentry->d_inode;
1970 struct fs *fs = fs_from_inode(ino);
1971 struct datablock *db;
1973 err = inode_change_ok(ino, attr);
1974 db = lafs_inode_dblock(ino, SYNC, MKREF(setattr));
1980 /* We don't need iolock_written here as we don't
1981 * actually change the inode block yet
1983 lafs_iolock_block(&db->b);
1984 set_bit(B_PinPending, &db->b.flags);
1985 lafs_iounlock_block(&db->b);
1987 /* FIXME quota stuff */
1990 lafs_checkpoint_lock(fs);
1991 err = lafs_pin_dblock(db, ReleaseSpace);
1992 if (err == -EAGAIN) {
1993 lafs_checkpoint_unlock_wait(fs);
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);
2004 lafs_dirty_dblock(db);
2006 clear_bit(B_PinPending, &db->b.flags);
2007 putdref(db, MKREF(setattr));
2008 lafs_checkpoint_unlock(fs);
2013 void lafs_truncate(struct inode *ino)
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
2020 struct fs *fs = fs_from_inode(ino);
2021 struct datablock *db = lafs_inode_dblock(ino, SYNC, MKREF(trunc));
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.
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))
2041 finish_wait(&fs->async_complete, &wq);
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.
2048 lafs_make_orphan(fs, db);
2050 if (!test_and_set_bit(I_Trunc, &LAFSI(ino)->iflags))
2052 if (trunc_block == 0)
2053 LAFSI(ino)->trunc_gen++;
2054 LAFSI(ino)->trunc_next = trunc_block;
2055 putdref(db, MKREF(trunc));
2058 const struct inode_operations lafs_special_ino_operations = {
2059 .setattr = lafs_setattr,
2060 .getattr = lafs_getattr,
2061 .truncate = lafs_truncate,