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 /* 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
37 #define NO_INO (~(ino_t)0)
38 static int async_itest(struct inode *inode, void *data)
44 /* found and is freeing */
46 if (inode->i_ino != inum)
48 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW)) {
55 static int async_iset(struct inode *inode, void *data)
60 inode->i_ino = *inump;
65 lafs_iget(struct super_block *sb, ino_t inum, int async)
67 /* find, and load if needed, this inum */
68 struct inode *ino = NULL;
70 struct datablock *b = NULL;
71 struct inode *inodefile;
75 BUG_ON(inum == NO_INO);
81 /* We cannot afford to block on 'freeing_inode'
82 * So use iget5_locked and refuse to match such
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.
91 ino = ilookup5(sb, inum, async_itest, &inum2);
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.
103 b = lafs_get_block(inodefile, inum, NULL,
104 GFP_NOFS, MKREF(iget));
106 return ERR_PTR(-ENOMEM);
109 err = lafs_read_block_async(b);
112 /* Have the block, so safe to iget */
114 ino = iget5_locked(sb, inum,
115 async_itest, async_iset,
125 if (test_and_set_bit(B_Async, &b->b.flags)) {
126 putdref(b, MKREF(iget));
129 getdref(b, MKREF(async));
133 ino = iget_locked(sb, inum);
136 putdref(b, MKREF(iget));
137 return ERR_PTR(-ENOMEM);
140 if (!(ino->i_state & I_NEW)) {
141 putdref(b, MKREF(iget));
145 return ERR_PTR(-ENOENT);
148 /* Need to load block 'inum' from an inode file...
151 b = lafs_get_block(inodefile, inum, NULL, GFP_KERNEL, MKREF(iget));
155 err = lafs_read_block(b);
160 oldino = rcu_my_inode(b);
162 /* The inode is new, but the block thinks it has an
163 * old inode, so we must be in the process of destroying
165 * So fail the lookup without even looking at the content
166 * of the block (Which might not be clear yet).
168 spin_lock(&oldino->i_data.private_lock);
169 if (!test_bit(I_Deleting, &LAFSI(oldino)->iflags)) {
171 LAFSI(oldino)->dblock = NULL;
172 LAFSI(oldino)->iblock = NULL;
174 spin_unlock(&oldino->i_data.private_lock);
182 err = lafs_import_inode(ino, b);
185 printk("lafs_import_inode failed %d\n", err);
188 unlock_new_inode(ino);
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));
194 putdref(b, MKREF(iget));
198 unlock_new_inode(ino);
205 lafs_iget_fs(struct fs *fs, int fsnum, int inum, int async)
207 struct super_block *sb;
213 /* Need to locate or load the superblock for this
214 * subordinate filesystem
216 struct inode *filesys;
217 struct super_block *sb2;
219 filesys = lafs_iget(sb, fsnum, async);
222 if (LAFSI(filesys)->type != TypeInodeFile) {
224 return ERR_PTR(-ENOENT);
226 /* FIXME can get_subset_sb be async at all?? */
227 sb2 = lafs_get_subset_sb(filesys);
230 return ERR_PTR(PTR_ERR(sb2));
232 rv = lafs_iget(sb2, inum, async);
234 deactivate_locked_super(sb2);
236 up_write(&sb2->s_umount);
238 rv = lafs_iget(sb, inum, async);
239 atomic_inc(&sb->s_active);
245 lafs_import_inode(struct inode *ino, struct datablock *b)
247 struct la_inode *lai = map_dblock(b);
248 struct lafs_inode *li = LAFSI(ino);
251 if (lai->filetype == 0) {
258 ino->i_mode = S_IFREG;
259 ino->i_nlink = 1; /* For special file, set nlink so they
260 * never appear unlinked */
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);
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;
280 dprintk("inode %lu type is %d\n", (unsigned long)ino->i_ino, li->type);
282 ino->i_data.a_ops = &lafs_file_aops;
288 struct fs_md *i = &li->md.fs;
289 struct fs_metadata *l = &lai->metadata[0].fs;
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);
312 /* Need to unmap the dblock to kmalloc because
313 * the mapping makes us 'atomic'
315 unmap_dblock(b, lai);
316 i->name = kmalloc(nlen+1, GFP_KERNEL);
318 l = &lai->metadata[0].fs;
323 memcpy(i->name, l->name, nlen);
326 /* Make this look like a directory */
327 ino->i_mode = S_IFDIR;
331 ino->i_op = &lafs_subset_ino_operations;
332 ino->i_fop = &lafs_subset_file_operations;
338 struct inodemap_md *m = &li->md.inodemap;
339 struct inodemap_metadata *s = &lai->metadata[0].inodemap;
340 m->size = le32_to_cpu(s->size);
341 m->thisblock = NoBlock;
348 struct su_md *m = &li->md.segmentusage;
349 struct su_metadata *s = &lai->metadata[0].segmentusage;
350 m->table_size = le32_to_cpu(s->table_size);
356 struct quota_md *m = &li->md.quota;
357 struct quota_metadata *s = &lai->metadata[0].quota;
358 m->gracetime = le32_to_cpu(s->gracetime);
359 m->graceunits = le32_to_cpu(s->graceunits);
364 struct orphan_md *m = &li->md.orphan;
365 /* This will be set via lafs_count_orphans */
373 default: /* TypeBase or larger */
375 struct file_md *i = &li->md.file;
376 struct file_metadata *l = &lai->metadata[0].file;
377 struct dir_metadata *d = &lai->metadata[0].dir;
378 struct special_metadata *s = &lai->metadata[0].special;
380 if (li->type < TypeBase)
382 i->flags = le16_to_cpu(l->flags);
383 ino->i_mode = le16_to_cpu(l->mode);
384 ino->i_uid = le32_to_cpu(l->userid);
385 ino->i_gid = le32_to_cpu(l->groupid);
386 i->treeid = le32_to_cpu(l->treeid);
387 i->creationtime = le64_to_cpu(l->creationtime);
388 decode_time(&ino->i_mtime, le64_to_cpu(l->modifytime));
389 decode_time(&ino->i_ctime, le64_to_cpu(l->ctime));
390 decode_time(&i->i_accesstime, le64_to_cpu(l->accesstime));
391 ino->i_atime = i->i_accesstime; /* FIXME load from
393 ino->i_size = le64_to_cpu(l->size);
394 i->parent = le32_to_cpu(l->parent);
395 ino->i_nlink = le32_to_cpu(l->linkcount);
396 if (ino->i_nlink == 0 && list_empty(&b->orphans)) {
397 /* This block should already be on the orphan
398 * list, otherwise there is a filesystem
400 * Either the orphan file is wrong, or the
401 * linkcount is wrong.
402 * It is safest to assume the later - either
403 * way an FS check would be needed to fix it.
405 /* FIXME set a superblock flag requesting
406 * directory linkage checking
411 dprintk(" mode = 0%o uid %d size %lld\n",
412 ino->i_mode, ino->i_uid, ino->i_size);
415 ino->i_op = &lafs_file_ino_operations;
416 ino->i_fop = &lafs_file_file_operations;
417 ino->i_mode = (ino->i_mode & 07777) | S_IFREG;
420 i->seed = le32_to_cpu(d->hash_seed);
421 ino->i_op = &lafs_dir_ino_operations;
422 ino->i_fop = &lafs_dir_file_operations;
423 ino->i_mode = (ino->i_mode & 07777) | S_IFDIR;
426 dprintk("Hmm. %d %d %d\n",
433 ino->i_op = &lafs_link_ino_operations;
434 ino->i_mode = (ino->i_mode & 07777) | S_IFLNK;
437 /* the data had better be in the inode ... */
438 ino->i_rdev = MKDEV(le32_to_cpu(s->major),
439 le32_to_cpu(s->minor));
440 ino->i_op = &lafs_special_ino_operations;
441 init_special_inode(ino, ino->i_mode, ino->i_rdev);
448 ino->i_blkbits = ino->i_sb->s_blocksize_bits;
449 /* FIXME i_blocks and i_byte - used for quota?? */
452 /* Note: no refcount yet. Either will remove the reference to the
456 rcu_assign_pointer(b->my_inode, ino);
460 printk("inode %lu type is %d\n",
461 (unsigned long)ino->i_ino, li->type);
462 unmap_dblock(b, lai);
466 void lafs_inode_checkpin(struct inode *ino)
468 /* Make sure I_Pinned is set correctly.
469 * It should be set precisely if i_nlink is non-zero,
470 * and ->iblock is B_Pinned.
471 * When it is set, we own a reference to the inode.
473 * This needs to be called whenever we change
474 * i_nlink, and whenever we pin or unpin an InoIdx
477 if (ino->i_nlink == 0) {
478 /* I_Pinned should not be set */
479 if (test_and_clear_bit(I_Pinned, &LAFSI(ino)->iflags))
482 /* Need to check if iblock is Pinned. */
483 struct indexblock *ib = NULL;
484 if (LAFSI(ino)->iblock) {
485 spin_lock(&ino->i_data.private_lock);
486 ib = LAFSI(ino)->iblock;
487 if (ib && !test_bit(B_Pinned, &ib->b.flags))
489 spin_unlock(&ino->i_data.private_lock);
492 if (!test_and_set_bit(I_Pinned, &LAFSI(ino)->iflags))
495 if (test_and_clear_bit(I_Pinned, &LAFSI(ino)->iflags))
501 struct datablock *lafs_inode_get_dblock(struct inode *ino, REFARG)
503 struct datablock *db;
505 spin_lock(&ino->i_data.private_lock);
506 db = LAFSI(ino)->dblock;
508 if (db->b.inode == ino)
509 getdref_locked(db, REF);
511 spin_lock_nested(&db->b.inode->i_data.private_lock, 1);
512 getdref_locked(db, REF);
513 spin_unlock(&db->b.inode->i_data.private_lock);
516 spin_unlock(&ino->i_data.private_lock);
520 struct datablock *lafs_inode_dblock(struct inode *ino, int async, REFARG)
522 struct datablock *db;
525 db = lafs_inode_get_dblock(ino, REF);
528 db = lafs_get_block(ino_from_sb(ino->i_sb), ino->i_ino, NULL,
533 LAFSI(ino)->dblock = db;
534 rcu_assign_pointer(db->my_inode, ino);
536 err = lafs_read_block_async(db);
538 err = lafs_read_block(db);
546 void lafs_inode_init(struct datablock *b, int type, int mode, struct inode *dir)
548 /* A new block has been allocated in an inode file to hold an
549 * inode. We get to fill in initial values so that when
550 * 'iget' calls lafs_import_inode, the correct inode is
553 struct fs *fs = fs_from_inode(b->b.inode);
554 struct la_inode *lai = map_dblock(b);
557 lai->data_blocks = cpu_to_le32(0);
558 lai->index_blocks = cpu_to_le32(0);
559 get_random_bytes(&lai->generation, sizeof(lai->generation));
562 lai->filetype = type;
568 struct fs_metadata *l = &lai->metadata[0].fs;
569 size = sizeof(struct fs_metadata);
572 l->blocks_allowed = 0;
573 l->creation_age = fs->wc[0].cluster_seq;
575 l->quota_inodes[0] = 0;
576 l->quota_inodes[1] = 0;
577 l->quota_inodes[2] = 0;
578 l->snapshot_usage_table = 0;
580 /* name will be zero length and not used */
585 struct inodemap_metadata *l = &lai->metadata[0].inodemap;
587 size = sizeof(struct inodemap_metadata);
591 size = sizeof(struct su_metadata);
594 size = sizeof(struct quota_metadata);
604 struct file_metadata *l = &lai->metadata[0].file;
605 struct timespec now = CURRENT_TIME;
607 l->flags = cpu_to_le16(0);
608 l->userid = cpu_to_le32(current->cred->fsuid);
609 if (dir && (dir->i_mode & S_ISGID)) {
610 l->groupid = cpu_to_le32(dir->i_gid);
614 l->groupid = cpu_to_le32(current->cred->fsgid);
615 if (dir && LAFSI(dir)->md.file.treeid)
616 l->treeid = cpu_to_le32(LAFSI(dir)->md.file.treeid);
618 l->treeid = l->userid;
620 l->mode = cpu_to_le16(mode);
621 l->creationtime = encode_time(&now);
622 l->modifytime = l->creationtime;
623 l->ctime = l->creationtime;
624 l->accesstime = l->creationtime;
626 l->parent = dir ? cpu_to_le32(dir->i_ino) : 0;
629 if (type == TypeDir) {
630 struct dir_metadata *l = &lai->metadata[0].dir;
632 get_random_bytes(&seed,
634 seed = (seed & ~7) | 1;
635 l->hash_seed = cpu_to_le32(seed);
636 size = sizeof(struct dir_metadata);
637 } else if (type == TypeSpecial) {
638 struct special_metadata *s = &lai->metadata[0].special;
639 s->major = s->minor = 0;
640 size = sizeof(struct special_metadata);
642 size = sizeof(struct file_metadata);
645 size += sizeof(struct la_inode);
646 lai->metadata_size = cpu_to_le32(size);
647 memset(((char *)lai)+size, 0, fs->blocksize-size);
648 *(u16 *)(((char *)lai)+size) = cpu_to_le16(IBLK_EXTENT);
650 unmap_dblock(b, lai);
651 set_bit(B_Valid, &b->b.flags);
652 LAFS_BUG(!test_bit(B_Pinned, &b->b.flags), &b->b);
653 lafs_dirty_dblock(b);
656 void lafs_clear_inode(struct inode *ino)
658 struct lafs_inode *li = LAFSI(ino);
659 dprintk("CLEAR INODE %d\n", (int)ino->i_ino);
663 /* Now is a good time to break the linkage between
664 * inode and dblock - but not if the file is
667 if (!test_bit(I_Deleting, &LAFSI(ino)->iflags)) {
668 struct datablock *db;
669 spin_lock(&ino->i_data.private_lock);
670 db = LAFSI(ino)->dblock;
672 struct indexblock *ib = LAFSI(ino)->iblock;
673 LAFS_BUG(ib && atomic_read(&ib->b.refcnt), &db->b);
675 LAFSI(ino)->dblock = NULL;
676 LAFSI(ino)->iblock = NULL;
678 spin_unlock(&ino->i_data.private_lock);
681 /* FIXME release quota inodes if filesystem */
684 static int inode_map_free(struct fs *fs, struct super_block *sb, u32 inum);
686 void lafs_delete_inode(struct inode *ino)
688 struct fs *fs = fs_from_inode(ino);
691 if (ino->i_mode == 0) {
692 /* There never was an inode here,
698 dprintk("DELETE INODE %d\n", (int)ino->i_ino);
700 /* Normal truncation holds an igrab, so we cannot be
701 * deleted until any truncation finishes
703 BUG_ON(test_bit(I_Trunc, &LAFSI(ino)->iflags));
705 b = lafs_inode_dblock(ino, SYNC, MKREF(delete_inode));
707 i_size_write(ino, 0);
708 truncate_inode_pages(&ino->i_data, 0);
709 LAFSI(ino)->trunc_next = 0;
710 set_bit(I_Deleting, &LAFSI(ino)->iflags);
711 set_bit(I_Trunc, &LAFSI(ino)->iflags);
715 set_bit(B_Claimed, &b->b.flags);
716 lafs_add_orphan(fs, b);
717 dprintk("PUNCH hole for %d\n", (int)b->b.fileaddr);
718 putdref(b, MKREF(delete_inode));
720 inode_map_free(fs, ino->i_sb, ino->i_ino);
725 static int prune(void *data, u32 addr, u64 paddr, int len)
727 /* This whole index block is being pruned, just account
728 * for everything and it will be cleared afterwards
730 struct indexblock *ib = data;
731 struct inode *ino = ib->b.inode;
732 struct fs *fs = fs_from_inode(ino);
733 int ph = !!test_bit(B_Phase1, &ib->b.flags);
735 dprintk("PRUNE %d for %d at %lld\n", addr, len, (long long)paddr);
736 if (paddr == 0 || len == 0)
738 for (i = 0 ; i < len ; i++)
739 lafs_summary_update(fs, ino, paddr+i, 0, 0, ph, 0);
743 static int prune_some(void *data, u32 addr, u64 paddr, int len)
745 /* Part of this index block is being pruned. Copy
746 * what addresses we can into uninc_table so that
747 * it can be 'incorporated'
748 * We should probably share some code with
749 * lafs_allocated_block??
751 struct indexblock *ib = data;
752 struct inode *ino = ib->b.inode;
753 struct fs *fs = fs_from_inode(ino);
754 int ph = !!test_bit(B_Phase1, &ib->b.flags);
757 if (paddr == 0 || len == 0)
759 dprintk("PRUNE2 %d for %d at %lld\n", addr, len, (long long)paddr);
760 for (i = 0 ; i < len ; i++) {
761 /* FIXME should allow longer truncation ranges in uninc_table
762 * as they are easy to handle.
765 if (addr + i < LAFSI(ino)->trunc_next)
767 spin_lock(&ino->i_data.private_lock);
768 a = &ib->uninc_table.pending_addr
769 [ib->uninc_table.pending_cnt - 1];
770 if (ib->uninc_table.pending_cnt <
771 ARRAY_SIZE(ib->uninc_table.pending_addr)) {
773 a->fileaddr = addr + i;
776 LAFS_BUG(!test_bit(B_Pinned, &ib->b.flags), &ib->b);
777 ib->uninc_table.pending_cnt++;
779 spin_unlock(&ino->i_data.private_lock);
782 spin_unlock(&ino->i_data.private_lock);
783 lafs_summary_update(fs, ino, paddr+i, 0, 0, ph, 0);
788 int lafs_inode_handle_orphan(struct datablock *b)
790 /* Don't need rcu protection for my_inode run_orphan
793 struct indexblock *ib, *ib2;
794 struct inode *ino = b->my_inode;
795 struct fs *fs = fs_from_inode(ino);
796 u32 trunc_next, next_trunc;
800 if (!test_bit(I_Trunc, &LAFSI(ino)->iflags)) {
801 if (test_bit(I_Deleting, &LAFSI(ino)->iflags)) {
802 LAFS_BUG(ino->i_nlink, &b->b);
803 if (LAFSI(ino)->cblocks +
804 LAFSI(ino)->pblocks +
805 LAFSI(ino)->ablocks +
806 LAFSI(ino)->ciblocks +
807 LAFSI(ino)->piblocks)
808 printk("Deleting inode %lu: %ld+%ld+%ld %ld+%ld\n",
813 LAFSI(ino)->ciblocks,
814 LAFSI(ino)->piblocks);
815 BUG_ON(LAFSI(ino)->cblocks +
816 LAFSI(ino)->pblocks +
817 LAFSI(ino)->ablocks +
818 LAFSI(ino)->ciblocks +
819 LAFSI(ino)->piblocks);
820 if (lafs_erase_dblock_async(b))
821 lafs_orphan_release(fs, b);
822 } else if (ino->i_nlink || LAFSI(ino)->type == 0)
823 lafs_orphan_release(fs, b);
825 lafs_orphan_forget(fs, b);
829 ib = lafs_make_iblock(ino, ADOPT, SYNC, MKREF(inode_handle_orphan));
833 /* Here is the guts of 'truncate'. We find the next leaf index
834 * block and discard all the addresses there-in.
836 trunc_next = LAFSI(ino)->trunc_next;
838 if (trunc_next == 0xFFFFFFFF) {
839 /* truncate has finished in that all data blocks
840 * have been removed and all index block are either
841 * gone or pending incorporation at which point they will
843 * If we hit a phase change, we will need to postpone
844 * the rest of the cleaning until it completes.
845 * If there is a checkpoint happening, then all the work
846 * that we can do now, it will do for us. So just
849 struct indexblock *tmp;
850 struct indexblock *next;
853 if (!test_bit(B_Pinned, &ib->b.flags)) {
854 /* must be finished */
855 LAFS_BUG(test_bit(B_Dirty, &ib->b.flags), &ib->b);
856 clear_bit(I_Trunc, &LAFSI(ino)->iflags);
858 wake_up(&fs->trunc_wait);
862 if (fs->checkpointing) {
863 /* This cannot happen with current code,
864 * but leave it in case we ever have
865 * orphan handling parallel with checkpoints
867 err = -EBUSY; /* Try again after the checkpoint */
871 lastaddr = (i_size_read(ino) +
873 >> fs->blocksize_bits;
874 /* Find a Pinned descendent of ib which has no
875 * Pinned descendents and no PrimaryRef dependent
876 * (so take the last).
877 * Prefer blocks that are beyond EOF (again, take the last).
878 * If there are none, descend the last block that
879 * is not after EOF and look at its children.
882 spin_lock(&ib->b.inode->i_data.private_lock);
886 list_for_each_entry(tmp, &ib2->children, b.siblings) {
887 if (!test_bit(B_Index, &tmp->b.flags) ||
888 !test_bit(B_Pinned, &tmp->b.flags))
891 tmp->b.fileaddr > next->b.fileaddr)
895 if (ib2->b.fileaddr < lastaddr) {
896 /* Must be all done */
897 spin_unlock(&ib->b.inode->i_data.private_lock);
898 clear_bit(I_Trunc, &LAFSI(ino)->iflags);
900 wake_up(&fs->trunc_wait);
904 getiref(ib2, MKREF(inode_handle_orphan2));
905 spin_unlock(&ib->b.inode->i_data.private_lock);
907 /* ib2 is an index block beyond EOF with no
909 * Incorporating it should unpin it.
911 if (!list_empty(&ib2->children)) {
912 lafs_print_tree(&ib2->b, 3);
913 LAFS_BUG(1, &ib2->b);
916 if (!lafs_iolock_written_async(&ib2->b)) {
917 putiref(ib2, MKREF(inode_handle_orphan2));
921 while (ib2->uninc_table.pending_cnt || ib2->uninc)
922 lafs_incorporate(fs, ib2);
924 if (test_bit(B_Dirty, &ib2->b.flags) ||
925 test_bit(B_Realloc, &ib2->b.flags))
926 lafs_cluster_allocate(&ib2->b, 0);
928 lafs_iounlock_block(&ib2->b);
930 if (!list_empty(&ib2->b.siblings)) {
931 printk("looping on %s\n", strblk(&ib2->b));
936 putiref(ib2, MKREF(inode_handle_orphan2));
939 if (lafs_iolock_written_async(&ib->b)) {
941 lafs_incorporate(fs, ib);
942 lafs_iounlock_block(&ib->b);
947 putiref(ib, MKREF(inode_handle_orphan));
951 putiref(ib, MKREF(inode_handle_orphan));
953 ib = lafs_leaf_find(ino, trunc_next, ADOPT, &next_trunc,
954 ASYNC, MKREF(inode_handle_orphan3));
957 /* now hold an iolock on ib */
959 /* Ok, trunc_next seems to refer to a block that exists.
960 * We need to erase it..
962 * So we open up the index block ourselves, call
963 * lafs_summary_update with each block address, and then
967 if (LAFSI(ino)->depth == 0) {
968 /* Nothing to truncate */
969 clear_bit(I_Trunc, &LAFSI(ino)->iflags);
971 if (test_bit(B_Pinned, &ib->b.flags))
972 /* Need to move the dirtiness which keeps this
973 * pinned to the data block.
975 lafs_cluster_allocate(&ib->b, 0);
977 lafs_iounlock_block(&ib->b);
982 lafs_checkpoint_lock(fs);
983 err = lafs_reserve_block(&ib->b, ReleaseSpace);
987 if (!test_bit(B_Valid, &ib->b.flags) &&
988 test_bit(B_InoIdx, &ib->b.flags)) {
989 /* still invalid, just re-erase to remove
991 LAFSI(ino)->trunc_next = next_trunc;
992 lafs_cluster_allocate(&ib->b, 0);
997 lafs_pin_block(&ib->b);
999 /* It might be that this can happen, in which case
1000 * we simply update trunc_next and loop. But I'd like
1001 * to be sure before I implement that
1003 if (!test_bit(B_Valid, &ib->b.flags)) {
1004 printk("Not Valid: %s\n", strblk(&ib->b));
1005 printk("depth = %d\n", LAFSI(ino)->depth);
1006 if (test_bit(B_InoIdx, &ib->b.flags))
1007 printk("DB: %s\n", strblk(&LAFSI(ib->b.inode)->dblock->b));
1008 LAFSI(ino)->trunc_next = next_trunc;
1009 //BUG_ON(!test_bit(B_Valid, &ib->b.flags));
1014 if (ib->b.fileaddr < trunc_next &&
1015 lafs_leaf_next(ib, 0) < trunc_next) {
1016 /* We only want to truncate part of this index block.
1017 * So we copy addresses into uninc_table and then
1018 * call lafs_incorporate.
1019 * This might cause the index tree to grow, so we
1020 * cannot trust next_trunc
1022 if (ib->uninc_table.pending_cnt == 0 &&
1023 ib->uninc == NULL) {
1024 lafs_dirty_iblock(ib, 0);
1025 /* FIXME this just removes 8 blocks at a time,
1026 * which is not enough
1028 lafs_walk_leaf_index(ib, prune_some, ib);
1030 if (test_bit(B_Dirty, &ib->b.flags))
1031 lafs_incorporate(fs, ib);
1035 LAFSI(ino)->trunc_next = next_trunc;
1037 while (ib->uninc_table.pending_cnt || ib->uninc) {
1038 /* There should be no Realloc data blocks here
1039 * but index blocks might be realloc still.
1041 LAFS_BUG(!test_bit(B_Dirty, &ib->b.flags) &&
1042 !test_bit(B_Realloc, &ib->b.flags), &ib->b);
1043 lafs_incorporate(fs, ib);
1045 if (test_bit(B_InoIdx, &ib->b.flags) ||
1046 !test_bit(B_PhysValid, &ib->b.flags) ||
1047 ib->b.physaddr != 0) {
1048 lafs_walk_leaf_index(ib, prune, ib);
1049 lafs_clear_index(ib);
1050 lafs_dirty_iblock(ib, 0);
1052 if (test_bit(B_Dirty, &ib->b.flags))
1053 lafs_incorporate(fs, ib);
1054 if (!list_empty(&ib->children))
1055 lafs_print_tree(&ib->b, 2);
1056 LAFS_BUG(!list_empty(&ib->children), &ib->b);
1059 lafs_iounlock_block(&ib->b);
1061 lafs_checkpoint_unlock(fs);
1063 putiref(ib, MKREF(inode_handle_orphan3));
1067 void lafs_dirty_inode(struct inode *ino)
1069 /* this is called in one of three cases:
1070 * 1/ by lafs internally when dblock or iblock is pinned and
1071 * ready to be dirtied
1072 * 2/ by writeout before requesting a write - to update mtime
1073 * 3/ by read to update atime
1075 * As we don't know which, there is not much we can do.
1076 * We mustn't update the data block as it could be in
1077 * writeout and we cannot always wait safely.
1078 * So require that anyone who really cares, dirties the datablock
1079 * or a child themselves.
1080 * When cluster_allocate eventually gets called, it will update
1081 * the datablock from the inode.
1082 * If an update has to wait for the next phase, lock_dblock
1083 * (e.g. in setattr) will do that.
1085 * We also use this opportunity to update the filesystem modify time.
1087 struct timespec now;
1088 struct inode *filesys;
1089 set_bit(I_Dirty, &LAFSI(ino)->iflags);
1090 ino->i_sb->s_dirt = 1;
1092 now = current_fs_time(ino->i_sb);
1093 filesys = ino_from_sb(ino->i_sb);
1094 if (!timespec_equal(&filesys->i_mtime, &now)) {
1095 filesys->i_mtime = now;
1096 set_bit(I_Dirty, &LAFSI(filesys)->iflags);
1100 int lafs_sync_inode(struct inode *ino, int wait)
1102 /* fsync has been called on this file so we need
1103 * to sync any inode updates to the next cluster.
1105 * If we cannot create an update record,
1106 * we wait for a phase change, which writes everything
1109 struct datablock *b;
1110 struct fs *fs = fs_from_inode(ino);
1111 struct update_handle uh;
1115 if (LAFSI(ino)->update_cluster > 1)
1116 lafs_cluster_wait(fs, LAFSI(ino)->update_cluster);
1117 if (LAFSI(ino)->update_cluster == 1) {
1118 lafs_checkpoint_lock(fs);
1119 lafs_checkpoint_unlock_wait(fs);
1124 LAFSI(ino)->update_cluster = 0;
1125 if (!test_bit(I_Dirty, &LAFSI(ino)->iflags))
1127 b = lafs_inode_dblock(ino, SYNC, MKREF(write_inode));
1131 lafs_iolock_written(&b->b);
1132 lafs_inode_fillblock(ino);
1133 lafs_iounlock_block(&b->b);
1135 err = lafs_cluster_update_prepare(&uh, fs, LAFS_INODE_LOG_SIZE);
1137 lafs_cluster_update_abort(&uh);
1139 lafs_checkpoint_lock(fs);
1140 if (lafs_cluster_update_pin(&uh) == 0) {
1141 if (test_and_clear_bit(B_Dirty, &b->b.flags))
1142 lafs_space_return(fs, 1);
1143 LAFSI(ino)->update_cluster =
1144 lafs_cluster_update_commit
1145 (&uh, b, LAFS_INODE_LOG_START,
1146 LAFS_INODE_LOG_SIZE);
1148 lafs_cluster_update_abort(&uh);
1149 lafs_checkpoint_unlock(fs);
1151 if (test_bit(B_Dirty, &b->b.flags)) {
1152 /* FIXME need to write out the data block...
1153 * Is that just lafs_cluster_allocate ?
1157 if (LAFSI(ino)->update_cluster == 0) {
1158 lafs_checkpoint_lock(fs);
1159 if (test_bit(B_Dirty, &b->b.flags))
1160 LAFSI(ino)->update_cluster = 1;
1161 lafs_checkpoint_start(fs);
1162 lafs_checkpoint_unlock(fs);
1164 putdref(b, MKREF(write_inode));
1165 return 0; /* FIXME should I return some error message??? */
1168 void lafs_inode_fillblock(struct inode *ino)
1170 /* copy data from ino into the related data block */
1172 struct lafs_inode *li = LAFSI(ino);
1173 struct datablock *db = li->dblock;
1174 struct la_inode *lai;
1176 clear_bit(I_Dirty, &LAFSI(ino)->iflags);
1178 lai = map_dblock(db);
1179 lai->data_blocks = cpu_to_le32(li->cblocks);
1180 lai->index_blocks = cpu_to_le32(li->ciblocks);
1181 lai->generation = cpu_to_le16(ino->i_generation);
1182 lai->trunc_gen = li->trunc_gen;
1183 lai->flags = li->flags;
1184 lai->filetype = li->type;
1185 if (lai->metadata_size != cpu_to_le16(li->metadata_size)) {
1186 /* Changing metadata size is wierd.
1187 * We will need to handle this somehow for xattrs
1188 * For now we just want to cope with
1189 * Dir -> InodeFile changes, and that guarantees us
1190 * there is no index info - so just clear the index
1193 u16 *s = (u16*)(((char*)lai) + li->metadata_size);
1194 BUG_ON(li->type != TypeInodeFile);
1195 lai->metadata_size = cpu_to_le16(li->metadata_size);
1196 memset(s, 0, ino->i_sb->s_blocksize - li->metadata_size);
1197 *s = cpu_to_le16(IBLK_INDIRECT);
1199 lai->depth = li->depth;
1204 struct fs_md *i = &li->md.fs;
1205 struct fs_metadata *l = &lai->metadata[0].fs;
1208 l->snapshot_usage_table = cpu_to_le16(i->usagetable);
1209 l->update_time = cpu_to_le64(encode_time(&ino->i_mtime));
1210 l->blocks_used = cpu_to_le64(i->cblocks_used);
1211 l->blocks_allowed = cpu_to_le64(i->blocks_allowed);
1212 l->creation_age = cpu_to_le64(i->creation_age);
1213 l->inodes_used = cpu_to_le32(i->inodes_used);
1214 l->quota_inodes[0] = cpu_to_le32(i->quota_inums[0]);
1215 l->quota_inodes[1] = cpu_to_le32(i->quota_inums[1]);
1216 l->quota_inodes[2] = cpu_to_le32(i->quota_inums[2]);
1217 nlen = lai->metadata_size - offsetof(struct la_inode,
1218 metadata[0].fs.name);
1219 memset(l->name, 0, nlen);
1220 if (i->name == NULL)
1222 else if (strlen(i->name) < nlen)
1223 nlen = strlen(i->name);
1224 memcpy(l->name, i->name, nlen);
1230 struct inodemap_md *m = &li->md.inodemap;
1231 struct inodemap_metadata *s = &lai->metadata[0].inodemap;
1232 s->size = cpu_to_le32(m->size);
1236 case TypeSegmentMap:
1238 struct su_md *m = &li->md.segmentusage;
1239 struct su_metadata *s = &lai->metadata[0].segmentusage;
1240 s->table_size = cpu_to_le32(m->table_size);
1246 struct quota_md *m = &li->md.quota;
1247 struct quota_metadata *s = &lai->metadata[0].quota;
1248 s->gracetime = cpu_to_le32(m->gracetime);
1249 s->graceunits = cpu_to_le32(m->graceunits);
1252 case TypeOrphanList:
1253 case TypeAccessTime:
1256 default: /* TypeBase or larger */
1258 struct file_md *i = &li->md.file;
1259 struct file_metadata *l = &lai->metadata[0].file;
1260 struct dir_metadata *d = &lai->metadata[0].dir;
1261 struct special_metadata *s = &lai->metadata[0].special;
1263 if (li->type < TypeBase)
1265 l->flags = cpu_to_le16(i->flags);
1266 l->mode = cpu_to_le16(ino->i_mode);
1267 l->userid = cpu_to_le32(ino->i_uid);
1268 l->groupid = cpu_to_le32(ino->i_gid);
1269 l->treeid = cpu_to_le32(i->treeid);
1270 l->creationtime = cpu_to_le64(i->creationtime);
1271 l->modifytime = cpu_to_le64(encode_time(&ino->i_mtime));
1272 l->ctime = cpu_to_le64(encode_time(&ino->i_ctime));
1273 l->accesstime = cpu_to_le64(encode_time(&ino->i_atime));
1274 /* FIXME write 0 to accesstime file */
1275 l->size = cpu_to_le64(ino->i_size);
1276 l->parent = cpu_to_le32(i->parent);
1277 l->linkcount = cpu_to_le32(ino->i_nlink);
1283 d->hash_seed = cpu_to_le32(i->seed);
1288 s->major = cpu_to_le32(MAJOR(ino->i_rdev));
1289 s->minor = cpu_to_le32(MINOR(ino->i_rdev));
1294 unmap_dblock(db, lai);
1297 /*-----------------------------------------------------------------------
1298 * Inode allocate map handling.
1299 * Inode 1 of each fileset is a bitmap of free inode numbers.
1300 * Whenever the file is extended in size, new bits are set to one. They
1301 * are then cleared when the inode is allocated. When a block becomes
1302 * full of zeros, we don't need to store it any more.
1304 * We don't clear the bit until we are committed to creating an inode
1305 * This means we cannot clear it straight away, so two different threads
1306 * might see the same inode number as being available. We have two
1307 * approaches to guard against this.
1308 * Firstly we have a 'current' pointer into the inodemap file and
1309 * increase that past the inode we return. This discourages multiple
1310 * hits but as the pointer would need to be rewound occasionally it
1311 * isn't a guarantee. The guarantee against multiple allocations is done
1312 * via a flag in the block representing an inode. This is set
1313 * while an inode is being allocated.
1316 /* inode number allocation has the prealloc/pin/commit/abort structure
1317 * so it can be committed effectively
1321 choose_free_inum(struct fs *fs, struct super_block *sb, u32 *inump,
1322 struct datablock **bp, int *restarted)
1324 struct inode *im = lafs_iget(sb, 1, SYNC);
1326 struct datablock *b;
1332 struct inode *i = (*bp)->b.inode;
1333 putdref(*bp, MKREF(cfi_map));
1338 mutex_lock_nested(&im->i_mutex, I_MUTEX_QUOTA);
1340 bnum = LAFSI(im)->md.inodemap.thisblock;
1342 if (bnum == NoBlock ||
1343 LAFSI(im)->md.inodemap.nextbit >= (fs->blocksize<<3)) {
1344 if (bnum == NoBlock)
1345 bnum = LAFSI(im)->md.inodemap.size;
1347 if (bnum+1 < LAFSI(im)->md.inodemap.size)
1349 else if (!*restarted) {
1353 /* Need to add a new block to the file */
1354 bnum = LAFSI(im)->md.inodemap.size;
1355 b = lafs_get_block(im, bnum, NULL, GFP_KERNEL,
1360 lafs_iolock_written(&b->b);
1361 set_bit(B_PinPending, &b->b.flags);
1362 lafs_iounlock_block(&b->b);
1364 lafs_checkpoint_lock(fs);
1365 err = lafs_pin_dblock(b, NewSpace);
1366 if (err == -EAGAIN) {
1367 lafs_checkpoint_unlock_wait(fs);
1373 buf = map_dblock(b);
1374 /* Set block to "all are free" */
1375 memset(buf, 0xff, fs->blocksize);
1376 unmap_dblock(b, buf);
1377 set_bit(B_Valid, &b->b.flags);
1378 LAFSI(im)->md.inodemap.size = bnum+1;
1379 lafs_dirty_inode(im);
1380 lafs_dirty_dblock(b);
1381 lafs_checkpoint_unlock(fs);
1382 putdref(b, MKREF(cfi_map));
1385 err = lafs_find_next(im, &bnum);
1391 LAFSI(im)->md.inodemap.nextbit = 0;
1392 LAFSI(im)->md.inodemap.thisblock = bnum;
1395 b = lafs_get_block(im, bnum, NULL, GFP_KERNEL, MKREF(cfi_map));
1399 err = lafs_find_block(b, NOADOPT);
1402 if (b->b.physaddr == 0 && !test_bit(B_Valid, &b->b.flags)) {
1403 LAFSI(im)->md.inodemap.nextbit =
1404 (fs->blocksize<<3) + 1;
1405 putdref(b,MKREF(cfi_map));
1408 err = lafs_read_block(b);
1412 bit = LAFSI(im)->md.inodemap.nextbit;
1413 LAFSI(im)->md.inodemap.thisblock = bnum;
1414 buf = map_dblock(b);
1415 while (bnum == 0 && bit < 16) {
1416 /* Never return an inum below 16 - they are special */
1417 if (!generic_test_le_bit(bit, (unsigned long *)buf))
1418 generic___clear_le_bit(bit, (unsigned long *)buf);
1422 bit = generic_find_next_le_bit((unsigned long *)buf,
1423 fs->blocksize<<3, bit);
1424 unmap_dblock(b, buf);
1425 LAFSI(im)->md.inodemap.nextbit = bit+1;
1426 if (bit >= fs->blocksize<<3) {
1427 putdref(b,MKREF(cfi_map));
1430 mutex_unlock(&im->i_mutex);
1432 *inump = bit + (bnum << (im->i_blkbits + 3));
1436 lafs_checkpoint_unlock(fs);
1438 putdref(b, MKREF(cfi_map));
1440 mutex_unlock(&im->i_mutex);
1445 struct inode_map_new_info {
1446 struct datablock *ib, *mb;
1450 inode_map_new_prepare(struct fs *fs, int inum, struct super_block *sb,
1451 struct inode_map_new_info *imni)
1456 struct datablock *b;
1458 imni->ib = imni->mb = NULL;
1461 /* choose a possibly-free inode number */
1462 err = choose_free_inum(fs, sb, &choice,
1463 &imni->mb, &restarted);
1467 b = lafs_get_block(ino_from_sb(sb), choice, NULL, GFP_KERNEL,
1472 if (test_and_set_bit(B_Claimed, &b->b.flags)) {
1473 putdref(b, MKREF(cfi_ino));
1479 lafs_iolock_written(&imni->mb->b);
1480 set_bit(B_PinPending, &imni->mb->b.flags);
1481 lafs_iounlock_block(&imni->mb->b);
1483 set_bit(B_PinPending, &b->b.flags);
1490 inode_map_new_pin(struct inode_map_new_info *imni)
1494 err = lafs_pin_dblock(imni->mb, NewSpace);
1495 err = err ?: lafs_pin_dblock(imni->ib, NewSpace);
1500 inode_map_new_commit(struct inode_map_new_info *imni)
1505 int blksize = imni->ib->b.inode->i_sb->s_blocksize;
1506 int bit = imni->ib->b.fileaddr & (blksize*8 - 1);
1508 struct inode *ino = imni->mb->b.inode;
1510 mutex_lock_nested(&ino->i_mutex, I_MUTEX_QUOTA);
1511 buf = map_dblock(imni->mb);
1512 generic___clear_le_bit(bit, buf);
1513 if (buf[blksize/sizeof(*buf)-1] == 0 &&
1514 generic_find_next_le_bit(buf, blksize*8, 0) == blksize*8)
1515 /* block is empty, punch a hole */
1518 unmap_dblock(imni->mb, buf);
1520 lafs_erase_dblock(imni->mb);
1522 lafs_dirty_dblock(imni->mb);
1524 putdref(imni->mb, MKREF(cfi_map));
1525 mutex_unlock(&ino->i_mutex);
1528 putdref(imni->ib, MKREF(cfi_ino));
1532 inode_map_new_abort(struct inode_map_new_info *imni)
1535 clear_bit(B_Claimed, &imni->ib->b.flags);
1536 clear_bit(B_PinPending, &imni->ib->b.flags);
1537 lafs_orphan_release(fs_from_inode(imni->ib->b.inode),
1540 putdref(imni->ib, MKREF(cfi_ino));
1542 struct inode *ino = imni->mb->b.inode;
1543 putdref(imni->mb, MKREF(cfi_map));
1549 lafs_new_inode(struct fs *fs, struct super_block *sb, struct inode *dir,
1550 int type, int inum, int mode, struct datablock **inodbp)
1552 /* allocate and instantiate a new inode. If inum is non-zero,
1553 * choose any number, otherwise we are creating a special inode
1554 * and have to use the given number.
1555 * This creation is committed independently of any name that might
1556 * subsequently be given to the inode. So we register it as an
1557 * orphan so that it will be cleaned up if the name isn't
1558 * successfully created
1562 struct datablock *b;
1563 struct inode_map_new_info imni;
1564 struct update_handle ui;
1567 err = inode_map_new_prepare(fs, inum, sb, &imni);
1568 err = lafs_cluster_update_prepare(&ui, fs, sizeof(struct la_inode))
1571 err = lafs_make_orphan(fs, imni.ib);
1575 lafs_checkpoint_lock(fs);
1577 err = inode_map_new_pin(&imni);
1579 if (err == -EAGAIN) {
1580 lafs_checkpoint_unlock_wait(fs);
1586 b = getdref(imni.ib, MKREF(inode_new));
1588 lafs_iolock_block(&b->b); /* make sure we don't race with the cleaner
1589 * and zero this inode while trying to load it
1591 lafs_inode_init(b, type, mode, dir);
1592 lafs_iounlock_block(&b->b);
1594 inode_map_new_commit(&imni);
1595 ino = lafs_iget(sb, b->b.fileaddr, SYNC);
1597 lafs_cluster_update_abort(&ui);
1600 lafs_cluster_update_commit(&ui, b, 0,
1601 LAFSI(ino)->metadata_size);
1602 LAFS_BUG(LAFSI(ino)->dblock != b, &b->b);
1603 LAFS_BUG(b->my_inode != ino, &b->b);
1604 lafs_checkpoint_unlock(fs);
1609 putdref(b, MKREF(inode_new));
1613 lafs_checkpoint_unlock(fs);
1616 inode_map_new_abort(&imni);
1617 lafs_cluster_update_abort(&ui);
1618 dprintk("After abort %d: %s\n", err, strblk(&imni.ib->b));
1619 return ERR_PTR(err);
1622 static int inode_map_free(struct fs *fs, struct super_block *sb, u32 inum)
1624 struct inode *im = lafs_iget(sb, 1, SYNC);
1627 struct datablock *b;
1631 mutex_lock_nested(&im->i_mutex, I_MUTEX_QUOTA);
1633 bnum = inum >> (3 + sb->s_blocksize_bits);
1634 bit = inum - (bnum << (3 + sb->s_blocksize_bits));
1635 b = lafs_get_block(im, bnum, NULL, GFP_KERNEL, MKREF(inode_map_free));
1637 mutex_unlock(&im->i_mutex);
1641 err = lafs_read_block(b);
1643 putdref(b, MKREF(inode_map_free));
1644 mutex_unlock(&im->i_mutex);
1648 lafs_iolock_written(&b->b);
1649 set_bit(B_PinPending, &b->b.flags);
1650 lafs_iounlock_block(&b->b);
1652 lafs_checkpoint_lock(fs);
1653 err = lafs_pin_dblock(b, ReleaseSpace);
1654 if (err == -EAGAIN) {
1655 lafs_checkpoint_unlock_wait(fs);
1659 buf = map_dblock(b);
1660 generic___set_le_bit(bit, buf);
1661 unmap_dblock(b, buf);
1662 lafs_dirty_dblock(b);
1663 putdref(b, MKREF(inode_map_free));
1664 lafs_checkpoint_unlock(fs);
1665 mutex_unlock(&im->i_mutex);
1670 int lafs_setattr(struct dentry *dentry, struct iattr *attr)
1673 struct inode *ino = dentry->d_inode;
1674 struct fs *fs = fs_from_inode(ino);
1675 struct datablock *db;
1677 err = inode_change_ok(ino, attr);
1678 db = lafs_inode_dblock(ino, SYNC, MKREF(setattr));
1684 /* We don't need iolock_written here as we don't
1685 * actually change the inode block yet
1687 lafs_iolock_block(&db->b);
1688 set_bit(B_PinPending, &db->b.flags);
1689 lafs_iounlock_block(&db->b);
1691 /* FIXME quota stuff */
1694 lafs_checkpoint_lock(fs);
1695 err = lafs_pin_dblock(db, ReleaseSpace);
1696 if (err == -EAGAIN) {
1697 lafs_checkpoint_unlock_wait(fs);
1700 /* inode_setattr calls lafs_dirty_inode, which sets
1701 * I_Dirty so the dblock will get updated.
1703 err = err ?: inode_setattr(ino, attr);
1705 lafs_dirty_dblock(db);
1706 clear_bit(B_PinPending, &db->b.flags);
1707 putdref(db, MKREF(setattr));
1708 lafs_checkpoint_unlock(fs);
1713 void lafs_truncate(struct inode *ino)
1715 /* Want to truncate this file.
1716 * i_size has already been changed, and the address space
1717 * has been cleaned up.
1718 * So just start the background truncate
1720 struct fs *fs = fs_from_inode(ino);
1721 struct datablock *db = lafs_inode_dblock(ino, SYNC, MKREF(trunc));
1728 trunc_block = ((i_size_read(ino) + fs->blocksize - 1)
1729 >> fs->blocksize_bits);
1730 /* We hold i_mutex, so regular orphan processing cannot
1731 * contine - we have to push it forward ourselves.
1733 while (test_bit(I_Trunc, &LAFSI(ino)->iflags) &&
1734 LAFSI(ino)->trunc_next < trunc_block) {
1735 prepare_to_wait(&fs->async_complete, &wq,
1736 TASK_UNINTERRUPTIBLE);
1737 lafs_inode_handle_orphan(db);
1738 if (test_bit(B_Orphan, &db->b.flags))
1741 finish_wait(&fs->async_complete, &wq);
1743 /* There is nothing we can do about errors here. The
1744 * most likely are ENOMEM which itself is very unlikely.
1745 * If this doesn't get registered as an orphan .... maybe
1746 * it will have to wait until something else truncates it.
1748 lafs_make_orphan(fs, db);
1750 if (!test_and_set_bit(I_Trunc, &LAFSI(ino)->iflags))
1752 if (trunc_block == 0)
1753 LAFSI(ino)->trunc_gen++;
1754 LAFSI(ino)->trunc_next = trunc_block;
1755 putdref(db, MKREF(trunc));
1758 const struct inode_operations lafs_special_ino_operations = {
1759 .setattr = lafs_setattr,
1760 .truncate = lafs_truncate,