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 time 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
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 lafs_dirty_dblock(b);
639 unmap_dblock(b, atp);
640 putdref(b, MKREF(store_atime));
643 void lafs_inode_checkpin(struct inode *ino)
645 /* Make sure I_Pinned is set correctly.
646 * It should be set precisely if i_nlink is non-zero,
647 * and ->iblock is B_Pinned.
648 * When it is set, we own a reference to the inode.
650 * This needs to be called whenever we change
651 * i_nlink, and whenever we pin or unpin an InoIdx
654 if (ino->i_nlink == 0) {
655 /* I_Pinned should not be set */
656 if (test_and_clear_bit(I_Pinned, &LAFSI(ino)->iflags)) {
657 if (ino->i_sb->s_type == &lafs_fs_type)
663 /* Need to check if iblock is Pinned. */
664 struct indexblock *ib = NULL;
665 if (LAFSI(ino)->iblock) {
666 spin_lock(&ino->i_data.private_lock);
667 ib = LAFSI(ino)->iblock;
668 if (ib && !test_bit(B_Pinned, &ib->b.flags))
670 spin_unlock(&ino->i_data.private_lock);
673 if (!test_and_set_bit(I_Pinned, &LAFSI(ino)->iflags)) {
674 if (ino->i_sb->s_type == &lafs_fs_type)
680 if (test_and_clear_bit(I_Pinned, &LAFSI(ino)->iflags)) {
681 if (ino->i_sb->s_type == &lafs_fs_type)
690 struct datablock *lafs_inode_get_dblock(struct inode *ino, REFARG)
692 struct datablock *db;
694 spin_lock(&ino->i_data.private_lock);
695 db = LAFSI(ino)->dblock;
697 if (db->b.inode == ino)
698 getdref_locked(db, REF);
700 spin_lock_nested(&db->b.inode->i_data.private_lock, 1);
701 getdref_locked(db, REF);
702 spin_unlock(&db->b.inode->i_data.private_lock);
705 spin_unlock(&ino->i_data.private_lock);
709 struct datablock *lafs_inode_dblock(struct inode *ino, int async, REFARG)
711 struct datablock *db;
714 db = lafs_inode_get_dblock(ino, REF);
717 db = lafs_get_block(LAFSI(ino)->filesys, ino->i_ino, NULL,
720 return ERR_PTR(-ENOMEM);
722 LAFSI(ino)->dblock = db;
723 rcu_assign_pointer(db->my_inode, ino);
725 err = lafs_read_block_async(db);
727 err = lafs_read_block(db);
735 void lafs_inode_init(struct datablock *b, int type, int mode, struct inode *dir)
737 /* A new block has been allocated in an inode file to hold an
738 * inode. We get to fill in initial values so that when
739 * 'iget' calls lafs_import_inode, the correct inode is
742 struct fs *fs = fs_from_inode(b->b.inode);
743 struct la_inode *lai = map_dblock(b);
746 lai->data_blocks = cpu_to_le32(0);
747 lai->index_blocks = cpu_to_le32(0);
748 get_random_bytes(&lai->generation, sizeof(lai->generation));
751 lai->filetype = type;
757 struct fs_metadata *l = &lai->metadata[0].fs;
758 size = sizeof(struct fs_metadata);
761 l->blocks_allowed = 0;
762 l->creation_age = fs->wc[0].cluster_seq;
764 l->quota_inodes[0] = 0;
765 l->quota_inodes[1] = 0;
766 l->quota_inodes[2] = 0;
767 l->snapshot_usage_table = 0;
769 /* name will be zero length and not used */
774 struct inodemap_metadata *l = &lai->metadata[0].inodemap;
776 size = sizeof(struct inodemap_metadata);
780 size = sizeof(struct su_metadata);
783 size = sizeof(struct quota_metadata);
793 struct file_metadata *l = &lai->metadata[0].file;
794 struct timespec now = CURRENT_TIME;
796 l->flags = cpu_to_le16(0);
797 l->userid = cpu_to_le32(current->cred->fsuid);
798 if (dir && (dir->i_mode & S_ISGID)) {
799 l->groupid = cpu_to_le32(dir->i_gid);
803 l->groupid = cpu_to_le32(current->cred->fsgid);
804 if (dir && LAFSI(dir)->md.file.treeid)
805 l->treeid = cpu_to_le32(LAFSI(dir)->md.file.treeid);
807 l->treeid = l->userid;
809 l->mode = cpu_to_le16(mode);
810 l->creationtime = encode_time(&now);
811 l->modifytime = l->creationtime;
812 l->ctime = l->creationtime;
813 l->accesstime = l->creationtime;
815 l->parent = dir ? cpu_to_le32(dir->i_ino) : 0;
818 if (type == TypeDir) {
819 struct dir_metadata *l = &lai->metadata[0].dir;
821 get_random_bytes(&seed,
823 seed = (seed & ~7) | 1;
824 l->hash_seed = cpu_to_le32(seed);
825 size = sizeof(struct dir_metadata);
826 } else if (type == TypeSpecial) {
827 struct special_metadata *s = &lai->metadata[0].special;
828 s->major = s->minor = 0;
829 size = sizeof(struct special_metadata);
831 size = sizeof(struct file_metadata);
834 size += sizeof(struct la_inode);
835 lai->metadata_size = cpu_to_le32(size);
836 memset(((char *)lai)+size, 0, fs->blocksize-size);
837 *(u16 *)(((char *)lai)+size) = cpu_to_le16(IBLK_EXTENT);
839 unmap_dblock(b, lai);
840 set_bit(B_Valid, &b->b.flags);
841 LAFS_BUG(!test_bit(B_Pinned, &b->b.flags), &b->b);
842 lafs_dirty_dblock(b);
845 static int inode_map_free(struct fs *fs, struct inode *fsys, u32 inum);
847 void lafs_evict_inode(struct inode *ino)
849 struct fs *fs = fs_from_inode(ino);
850 struct lafs_inode *li = LAFSI(ino);
852 if (ino->i_mode == 0) {
853 /* There never was an inode here,
855 * We just call end_writeback to get the
856 * flags set properly.
862 dprintk("EVICT INODE %d\n", (int)ino->i_ino);
865 /* Normal truncation holds an igrab, so we cannot be
866 * deleted until any truncation finishes
868 BUG_ON(test_bit(I_Trunc, &LAFSI(ino)->iflags));
870 if (ino->i_nlink == 0) {
871 struct datablock *b =
872 lafs_inode_dblock(ino, SYNC, MKREF(delete_inode));
873 i_size_write(ino, 0);
874 truncate_inode_pages(&ino->i_data, 0);
875 LAFSI(ino)->trunc_next = 0;
876 set_bit(I_Deleting, &LAFSI(ino)->iflags);
877 set_bit(I_Trunc, &LAFSI(ino)->iflags);
880 set_bit(B_Claimed, &b->b.flags);
881 lafs_add_orphan(fs, b);
882 dprintk("PUNCH hole for %d\n", (int)b->b.fileaddr);
883 putdref(b, MKREF(delete_inode));
885 inode_map_free(fs, LAFSI(ino)->filesys, ino->i_ino);
887 truncate_inode_pages(&ino->i_data, 0);
890 dprintk("CLEAR INODE %d\n", (int)ino->i_ino);
894 /* Now is a good time to break the linkage between
895 * inode and dblock - but not if the file is
898 if (!test_bit(I_Deleting, &li->iflags)) {
899 struct datablock *db;
900 spin_lock(&ino->i_data.private_lock);
903 struct indexblock *ib = li->iblock;
904 LAFS_BUG(ib && atomic_read(&ib->b.refcnt), &db->b);
909 spin_unlock(&ino->i_data.private_lock);
912 /* FIXME release quota inodes if filesystem */
915 static int prune(void *data, u32 addr, u64 paddr, int len)
917 /* This whole index block is being pruned, just account
918 * for everything and it will be cleared afterwards
920 struct indexblock *ib = data;
921 struct inode *ino = ib->b.inode;
922 struct fs *fs = fs_from_inode(ino);
923 int ph = !!test_bit(B_Phase1, &ib->b.flags);
925 dprintk("PRUNE %d for %d at %lld\n", addr, len, (long long)paddr);
926 if (paddr == 0 || len == 0)
928 for (i = 0 ; i < len ; i++)
929 lafs_summary_update(fs, ino, paddr+i, 0, 0, ph, 0);
933 static int prune_some(void *data, u32 addr, u64 paddr, int len)
935 /* Part of this index block is being pruned. Copy
936 * what addresses we can into uninc_table so that
937 * it can be 'incorporated'
938 * We should probably share some code with
939 * lafs_allocated_block??
941 struct indexblock *ib = data;
942 struct inode *ino = ib->b.inode;
943 struct fs *fs = fs_from_inode(ino);
944 int ph = !!test_bit(B_Phase1, &ib->b.flags);
947 if (paddr == 0 || len == 0)
949 dprintk("PRUNE2 %d for %d at %lld\n", addr, len, (long long)paddr);
950 for (i = 0 ; i < len ; i++) {
951 /* FIXME should allow longer truncation ranges in uninc_table
952 * as they are easy to handle.
955 if (addr + i < LAFSI(ino)->trunc_next)
957 spin_lock(&ino->i_data.private_lock);
958 a = &ib->uninc_table.pending_addr
959 [ib->uninc_table.pending_cnt - 1];
960 if (ib->uninc_table.pending_cnt <
961 ARRAY_SIZE(ib->uninc_table.pending_addr)) {
963 a->fileaddr = addr + i;
966 LAFS_BUG(!test_bit(B_Pinned, &ib->b.flags), &ib->b);
967 ib->uninc_table.pending_cnt++;
969 spin_unlock(&ino->i_data.private_lock);
972 spin_unlock(&ino->i_data.private_lock);
973 lafs_summary_update(fs, ino, paddr+i, 0, 0, ph, 0);
978 int lafs_inode_handle_orphan(struct datablock *b)
980 /* Don't need rcu protection for my_inode run_orphan
983 struct indexblock *ib, *ib2;
984 struct inode *ino = b->my_inode;
985 struct fs *fs = fs_from_inode(ino);
986 u32 trunc_next, next_trunc;
990 if (!test_bit(I_Trunc, &LAFSI(ino)->iflags)) {
991 if (test_bit(I_Deleting, &LAFSI(ino)->iflags)) {
992 LAFS_BUG(ino->i_nlink, &b->b);
993 if (LAFSI(ino)->cblocks +
994 LAFSI(ino)->pblocks +
995 LAFSI(ino)->ablocks +
996 LAFSI(ino)->ciblocks +
997 LAFSI(ino)->piblocks)
998 printk("Deleting inode %lu: %ld+%ld+%ld %ld+%ld\n",
1000 LAFSI(ino)->cblocks,
1001 LAFSI(ino)->pblocks,
1002 LAFSI(ino)->ablocks,
1003 LAFSI(ino)->ciblocks,
1004 LAFSI(ino)->piblocks);
1005 BUG_ON(LAFSI(ino)->cblocks +
1006 LAFSI(ino)->pblocks +
1007 LAFSI(ino)->ablocks +
1008 LAFSI(ino)->ciblocks +
1009 LAFSI(ino)->piblocks);
1010 if (lafs_erase_dblock_async(b))
1011 lafs_orphan_release(fs, b);
1012 } else if (ino->i_nlink || LAFSI(ino)->type == 0)
1013 lafs_orphan_release(fs, b);
1015 lafs_orphan_forget(fs, b);
1019 ib = lafs_make_iblock(ino, ADOPT, SYNC, MKREF(inode_handle_orphan));
1023 /* Here is the guts of 'truncate'. We find the next leaf index
1024 * block and discard all the addresses there-in.
1026 trunc_next = LAFSI(ino)->trunc_next;
1028 if (trunc_next == 0xFFFFFFFF) {
1029 /* truncate has finished in that all data blocks
1030 * have been removed and all index block are either
1031 * gone or pending incorporation at which point they will
1033 * If we hit a phase change, we will need to postpone
1034 * the rest of the cleaning until it completes.
1035 * If there is a checkpoint happening, then all the work
1036 * that we can do now, it will do for us. So just
1039 struct indexblock *tmp;
1040 struct indexblock *next;
1043 if (!test_bit(B_Pinned, &ib->b.flags)) {
1044 /* must be finished */
1045 LAFS_BUG(test_bit(B_Dirty, &ib->b.flags), &ib->b);
1046 clear_bit(I_Trunc, &LAFSI(ino)->iflags);
1048 wake_up(&fs->trunc_wait);
1052 if (fs->checkpointing) {
1053 /* This cannot happen with current code,
1054 * but leave it in case we ever have
1055 * orphan handling parallel with checkpoints
1057 err = -EBUSY; /* Try again after the checkpoint */
1061 lastaddr = (i_size_read(ino) +
1063 >> fs->blocksize_bits;
1064 /* Find a Pinned descendent of ib which has no
1065 * Pinned descendents and no PrimaryRef dependent
1066 * (so take the last).
1067 * Prefer blocks that are beyond EOF (again, take the last).
1068 * If there are none, descend the last block that
1069 * is not after EOF and look at its children.
1072 spin_lock(&ib->b.inode->i_data.private_lock);
1076 list_for_each_entry(tmp, &ib2->children, b.siblings) {
1077 if (!test_bit(B_Index, &tmp->b.flags) ||
1078 !test_bit(B_Pinned, &tmp->b.flags))
1081 tmp->b.fileaddr > next->b.fileaddr)
1085 if (ib2->b.fileaddr < lastaddr) {
1086 /* Must be all done */
1087 spin_unlock(&ib->b.inode->i_data.private_lock);
1088 clear_bit(I_Trunc, &LAFSI(ino)->iflags);
1090 wake_up(&fs->trunc_wait);
1094 getiref(ib2, MKREF(inode_handle_orphan2));
1095 spin_unlock(&ib->b.inode->i_data.private_lock);
1097 /* ib2 is an index block beyond EOF with no
1099 * Incorporating it should unpin it.
1101 if (!list_empty(&ib2->children)) {
1102 lafs_print_tree(&ib2->b, 3);
1103 LAFS_BUG(1, &ib2->b);
1106 if (!lafs_iolock_written_async(&ib2->b)) {
1107 putiref(ib2, MKREF(inode_handle_orphan2));
1111 while (ib2->uninc_table.pending_cnt || ib2->uninc)
1112 lafs_incorporate(fs, ib2);
1114 if (test_bit(B_Dirty, &ib2->b.flags) ||
1115 test_bit(B_Realloc, &ib2->b.flags))
1116 lafs_cluster_allocate(&ib2->b, 0);
1118 lafs_iounlock_block(&ib2->b);
1120 if (!list_empty(&ib2->b.siblings)) {
1121 printk("looping on %s\n", strblk(&ib2->b));
1126 putiref(ib2, MKREF(inode_handle_orphan2));
1129 if (lafs_iolock_written_async(&ib->b)) {
1131 lafs_incorporate(fs, ib);
1132 lafs_iounlock_block(&ib->b);
1137 putiref(ib, MKREF(inode_handle_orphan));
1141 putiref(ib, MKREF(inode_handle_orphan));
1143 ib = lafs_leaf_find(ino, trunc_next, ADOPT, &next_trunc,
1144 ASYNC, MKREF(inode_handle_orphan3));
1147 /* now hold an iolock on ib */
1149 /* Ok, trunc_next seems to refer to a block that exists.
1150 * We need to erase it..
1152 * So we open up the index block ourselves, call
1153 * lafs_summary_update with each block address, and then
1157 if (LAFSI(ino)->depth == 0) {
1158 /* Nothing to truncate */
1159 clear_bit(I_Trunc, &LAFSI(ino)->iflags);
1161 if (test_bit(B_Pinned, &ib->b.flags))
1162 /* Need to move the dirtiness which keeps this
1163 * pinned to the data block.
1165 lafs_cluster_allocate(&ib->b, 0);
1167 lafs_iounlock_block(&ib->b);
1172 lafs_checkpoint_lock(fs);
1173 err = lafs_reserve_block(&ib->b, ReleaseSpace);
1177 if (!test_bit(B_Valid, &ib->b.flags) &&
1178 test_bit(B_InoIdx, &ib->b.flags)) {
1179 /* still invalid, just re-erase to remove
1181 LAFSI(ino)->trunc_next = next_trunc;
1182 lafs_cluster_allocate(&ib->b, 0);
1187 lafs_pin_block(&ib->b);
1189 /* It might be that this can happen, in which case
1190 * we simply update trunc_next and loop. But I'd like
1191 * to be sure before I implement that
1193 if (!test_bit(B_Valid, &ib->b.flags)) {
1194 printk("Not Valid: %s\n", strblk(&ib->b));
1195 printk("depth = %d\n", LAFSI(ino)->depth);
1196 if (test_bit(B_InoIdx, &ib->b.flags))
1197 printk("DB: %s\n", strblk(&LAFSI(ib->b.inode)->dblock->b));
1198 LAFSI(ino)->trunc_next = next_trunc;
1199 //BUG_ON(!test_bit(B_Valid, &ib->b.flags));
1204 if (ib->b.fileaddr < trunc_next &&
1205 lafs_leaf_next(ib, 0) < trunc_next) {
1206 /* We only want to truncate part of this index block.
1207 * So we copy addresses into uninc_table and then
1208 * call lafs_incorporate.
1209 * This might cause the index tree to grow, so we
1210 * cannot trust next_trunc
1212 if (ib->uninc_table.pending_cnt == 0 &&
1213 ib->uninc == NULL) {
1214 lafs_dirty_iblock(ib, 0);
1215 /* FIXME this just removes 8 blocks at a time,
1216 * which is not enough
1218 lafs_walk_leaf_index(ib, prune_some, ib);
1220 if (test_bit(B_Dirty, &ib->b.flags))
1221 lafs_incorporate(fs, ib);
1225 LAFSI(ino)->trunc_next = next_trunc;
1227 while (ib->uninc_table.pending_cnt || ib->uninc) {
1228 /* There should be no Realloc data blocks here
1229 * but index blocks might be realloc still.
1231 LAFS_BUG(!test_bit(B_Dirty, &ib->b.flags) &&
1232 !test_bit(B_Realloc, &ib->b.flags), &ib->b);
1233 lafs_incorporate(fs, ib);
1235 if (test_bit(B_InoIdx, &ib->b.flags) ||
1236 !test_bit(B_PhysValid, &ib->b.flags) ||
1237 ib->b.physaddr != 0) {
1238 lafs_walk_leaf_index(ib, prune, ib);
1239 lafs_clear_index(ib);
1240 lafs_dirty_iblock(ib, 0);
1242 if (test_bit(B_Dirty, &ib->b.flags))
1243 lafs_incorporate(fs, ib);
1244 if (!list_empty(&ib->children))
1245 lafs_print_tree(&ib->b, 2);
1246 LAFS_BUG(!list_empty(&ib->children), &ib->b);
1249 lafs_iounlock_block(&ib->b);
1251 lafs_checkpoint_unlock(fs);
1253 putiref(ib, MKREF(inode_handle_orphan3));
1257 void lafs_dirty_inode(struct inode *ino)
1259 /* this is called in one of three cases:
1260 * 1/ by lafs internally when dblock or iblock is pinned and
1261 * ready to be dirtied
1262 * 2/ by writeout before requesting a write - to update mtime
1263 * 3/ by read to update atime
1265 * We want to handle atime updates carefully as they may not change
1266 * the stored inode itself.
1267 * For all other updates, the inode dblock exists and is pinned.
1268 * In those cases we will be updating the inode and so can store
1269 * the atime exactly.
1270 * For an atime update, the dblock may not exists, or may not be
1271 * Pinned. If it isn't then we don't want to make the inode dirty
1272 * but only want to update the delta stored in the atime file.
1273 * The block for that should already be pinned.
1276 * We mustn't update the data block as it could be in
1277 * writeout and we cannot always wait safely.
1278 * So require that anyone who really cares, dirties the datablock
1279 * or a child themselves.
1280 * When cluster_allocate eventually gets called, it will update
1281 * the datablock from the inode.
1282 * If an update has to wait for the next phase, lock_dblock
1283 * (e.g. in setattr) will do that.
1285 * We also use this opportunity to update the filesystem modify time.
1287 struct timespec now;
1288 struct inode *filesys;
1291 if (LAFSI(ino)->dblock) {
1292 struct datablock *db;
1293 spin_lock(&ino->i_data.private_lock);
1294 db = LAFSI(ino)->dblock;
1295 if (db && test_bit(B_Pinned, &db->b.flags))
1297 spin_unlock(&ino->i_data.private_lock);
1301 if (update_atime_delta(ino))
1302 store_atime_delta(ino);
1306 set_bit(I_Dirty, &LAFSI(ino)->iflags);
1307 ino->i_sb->s_dirt = 1;
1309 if (LAFSI(ino)->type < TypeBase)
1311 LAFSI(ino)->md.file.i_accesstime = ino->i_atime;
1312 if (LAFSI(ino)->md.file.atime_offset) {
1313 LAFSI(ino)->md.file.atime_offset = 0;
1314 store_atime_delta(ino);
1317 now = current_fs_time(ino->i_sb);
1318 filesys = LAFSI(ino)->filesys;
1319 if (!timespec_equal(&filesys->i_mtime, &now)) {
1320 filesys->i_mtime = now;
1321 set_bit(I_Dirty, &LAFSI(filesys)->iflags);
1325 int lafs_sync_inode(struct inode *ino, int wait)
1327 /* fsync has been called on this file so we need
1328 * to sync any inode updates to the next cluster.
1330 * If we cannot create an update record,
1331 * we wait for a phase change, which writes everything
1334 struct datablock *b;
1335 struct fs *fs = fs_from_inode(ino);
1336 struct update_handle uh;
1340 if (LAFSI(ino)->update_cluster > 1)
1341 lafs_cluster_wait(fs, LAFSI(ino)->update_cluster);
1342 if (LAFSI(ino)->update_cluster == 1) {
1343 lafs_checkpoint_lock(fs);
1344 lafs_checkpoint_unlock_wait(fs);
1349 LAFSI(ino)->update_cluster = 0;
1350 if (!test_bit(I_Dirty, &LAFSI(ino)->iflags))
1352 b = lafs_inode_dblock(ino, SYNC, MKREF(write_inode));
1356 lafs_iolock_written(&b->b);
1357 lafs_inode_fillblock(ino);
1358 lafs_iounlock_block(&b->b);
1360 err = lafs_cluster_update_prepare(&uh, fs, LAFS_INODE_LOG_SIZE);
1362 lafs_cluster_update_abort(&uh);
1364 lafs_checkpoint_lock(fs);
1365 if (lafs_cluster_update_pin(&uh) == 0) {
1366 if (test_and_clear_bit(B_Dirty, &b->b.flags))
1367 lafs_space_return(fs, 1);
1368 LAFSI(ino)->update_cluster =
1369 lafs_cluster_update_commit
1370 (&uh, b, LAFS_INODE_LOG_START,
1371 LAFS_INODE_LOG_SIZE);
1373 lafs_cluster_update_abort(&uh);
1374 lafs_checkpoint_unlock(fs);
1376 if (test_bit(B_Dirty, &b->b.flags)) {
1377 /* FIXME need to write out the data block...
1378 * Is that just lafs_cluster_allocate ?
1382 if (LAFSI(ino)->update_cluster == 0) {
1383 lafs_checkpoint_lock(fs);
1384 if (test_bit(B_Dirty, &b->b.flags))
1385 LAFSI(ino)->update_cluster = 1;
1386 lafs_checkpoint_start(fs);
1387 lafs_checkpoint_unlock(fs);
1389 putdref(b, MKREF(write_inode));
1390 return 0; /* FIXME should I return some error message??? */
1393 void lafs_inode_fillblock(struct inode *ino)
1395 /* copy data from ino into the related data block */
1397 struct lafs_inode *li = LAFSI(ino);
1398 struct datablock *db = li->dblock;
1399 struct la_inode *lai;
1401 clear_bit(I_Dirty, &LAFSI(ino)->iflags);
1403 lai = map_dblock(db);
1404 lai->data_blocks = cpu_to_le32(li->cblocks);
1405 lai->index_blocks = cpu_to_le32(li->ciblocks);
1406 lai->generation = cpu_to_le16(ino->i_generation);
1407 lai->trunc_gen = li->trunc_gen;
1408 lai->flags = li->flags;
1409 lai->filetype = li->type;
1410 if (lai->metadata_size != cpu_to_le16(li->metadata_size)) {
1411 /* Changing metadata size is wierd.
1412 * We will need to handle this somehow for xattrs
1413 * For now we just want to cope with
1414 * Dir -> InodeFile changes, and that guarantees us
1415 * there is no index info - so just clear the index
1418 u16 *s = (u16*)(((char*)lai) + li->metadata_size);
1419 BUG_ON(li->type != TypeInodeFile);
1420 lai->metadata_size = cpu_to_le16(li->metadata_size);
1421 memset(s, 0, ino->i_sb->s_blocksize - li->metadata_size);
1422 *s = cpu_to_le16(IBLK_INDIRECT);
1424 lai->depth = li->depth;
1429 struct fs_md *i = &li->md.fs;
1430 struct fs_metadata *l = &lai->metadata[0].fs;
1433 l->snapshot_usage_table = cpu_to_le16(i->usagetable);
1434 l->update_time = cpu_to_le64(encode_time(&ino->i_mtime));
1435 l->blocks_used = cpu_to_le64(i->cblocks_used);
1436 l->blocks_allowed = cpu_to_le64(i->blocks_allowed);
1437 l->creation_age = cpu_to_le64(i->creation_age);
1438 l->inodes_used = cpu_to_le32(i->inodes_used);
1439 l->quota_inodes[0] = cpu_to_le32(i->quota_inums[0]);
1440 l->quota_inodes[1] = cpu_to_le32(i->quota_inums[1]);
1441 l->quota_inodes[2] = cpu_to_le32(i->quota_inums[2]);
1442 nlen = lai->metadata_size - offsetof(struct la_inode,
1443 metadata[0].fs.name);
1444 memset(l->name, 0, nlen);
1445 if (i->name == NULL)
1447 else if (strlen(i->name) < nlen)
1448 nlen = strlen(i->name);
1449 memcpy(l->name, i->name, nlen);
1455 struct inodemap_md *m = &li->md.inodemap;
1456 struct inodemap_metadata *s = &lai->metadata[0].inodemap;
1457 s->size = cpu_to_le32(m->size);
1461 case TypeSegmentMap:
1463 struct su_md *m = &li->md.segmentusage;
1464 struct su_metadata *s = &lai->metadata[0].segmentusage;
1465 s->table_size = cpu_to_le32(m->table_size);
1471 struct quota_md *m = &li->md.quota;
1472 struct quota_metadata *s = &lai->metadata[0].quota;
1473 s->gracetime = cpu_to_le32(m->gracetime);
1474 s->graceunits = cpu_to_le32(m->graceunits);
1477 case TypeOrphanList:
1478 case TypeAccessTime:
1481 default: /* TypeBase or larger */
1483 struct file_md *i = &li->md.file;
1484 struct file_metadata *l = &lai->metadata[0].file;
1485 struct dir_metadata *d = &lai->metadata[0].dir;
1486 struct special_metadata *s = &lai->metadata[0].special;
1488 if (li->type < TypeBase)
1490 l->flags = cpu_to_le16(i->flags);
1491 l->mode = cpu_to_le16(ino->i_mode);
1492 l->userid = cpu_to_le32(ino->i_uid);
1493 l->groupid = cpu_to_le32(ino->i_gid);
1494 l->treeid = cpu_to_le32(i->treeid);
1495 l->creationtime = cpu_to_le64(i->creationtime);
1496 l->modifytime = cpu_to_le64(encode_time(&ino->i_mtime));
1497 l->ctime = cpu_to_le64(encode_time(&ino->i_ctime));
1498 l->accesstime = cpu_to_le64(encode_time(&i->i_accesstime));
1499 l->size = cpu_to_le64(ino->i_size);
1500 l->parent = cpu_to_le32(i->parent);
1501 l->linkcount = cpu_to_le32(ino->i_nlink);
1507 d->hash_seed = cpu_to_le32(i->seed);
1512 s->major = cpu_to_le32(MAJOR(ino->i_rdev));
1513 s->minor = cpu_to_le32(MINOR(ino->i_rdev));
1518 unmap_dblock(db, lai);
1521 /*-----------------------------------------------------------------------
1522 * Inode allocate map handling.
1523 * Inode 1 of each fileset is a bitmap of free inode numbers.
1524 * Whenever the file is extended in size, new bits are set to one. They
1525 * are then cleared when the inode is allocated. When a block becomes
1526 * full of zeros, we don't need to store it any more.
1528 * We don't clear the bit until we are committed to creating an inode
1529 * This means we cannot clear it straight away, so two different threads
1530 * might see the same inode number as being available. We have two
1531 * approaches to guard against this.
1532 * Firstly we have a 'current' pointer into the inodemap file and
1533 * increase that past the inode we return. This discourages multiple
1534 * hits but as the pointer would need to be rewound occasionally it
1535 * isn't a guarantee. The guarantee against multiple allocations is done
1536 * via a flag in the block representing an inode. This is set
1537 * while an inode is being allocated.
1540 /* inode number allocation has the prealloc/pin/commit/abort structure
1541 * so it can be committed effectively
1545 choose_free_inum(struct fs *fs, struct inode *fsys, u32 *inump,
1546 struct datablock **bp, int *restarted)
1548 struct inode *im = lafs_iget(fsys, 1, SYNC);
1550 struct datablock *b;
1556 struct inode *i = (*bp)->b.inode;
1557 putdref(*bp, MKREF(cfi_map));
1562 mutex_lock_nested(&im->i_mutex, I_MUTEX_QUOTA);
1564 bnum = LAFSI(im)->md.inodemap.thisblock;
1566 if (bnum == NoBlock ||
1567 LAFSI(im)->md.inodemap.nextbit >= (fs->blocksize<<3)) {
1568 if (bnum == NoBlock)
1569 bnum = LAFSI(im)->md.inodemap.size;
1571 if (bnum+1 < LAFSI(im)->md.inodemap.size)
1573 else if (!*restarted) {
1577 /* Need to add a new block to the file */
1578 bnum = LAFSI(im)->md.inodemap.size;
1579 b = lafs_get_block(im, bnum, NULL, GFP_KERNEL,
1584 lafs_iolock_written(&b->b);
1585 set_bit(B_PinPending, &b->b.flags);
1586 lafs_iounlock_block(&b->b);
1588 lafs_checkpoint_lock(fs);
1589 err = lafs_pin_dblock(b, NewSpace);
1590 if (err == -EAGAIN) {
1591 lafs_checkpoint_unlock_wait(fs);
1597 buf = map_dblock(b);
1598 /* Set block to "all are free" */
1599 memset(buf, 0xff, fs->blocksize);
1600 unmap_dblock(b, buf);
1601 set_bit(B_Valid, &b->b.flags);
1602 LAFSI(im)->md.inodemap.size = bnum+1;
1603 lafs_dirty_inode(im);
1604 lafs_dirty_dblock(b);
1605 lafs_checkpoint_unlock(fs);
1606 putdref(b, MKREF(cfi_map));
1609 err = lafs_find_next(im, &bnum);
1615 LAFSI(im)->md.inodemap.nextbit = 0;
1616 LAFSI(im)->md.inodemap.thisblock = bnum;
1619 b = lafs_get_block(im, bnum, NULL, GFP_KERNEL, MKREF(cfi_map));
1623 err = lafs_find_block(b, NOADOPT);
1626 if (b->b.physaddr == 0 && !test_bit(B_Valid, &b->b.flags)) {
1627 LAFSI(im)->md.inodemap.nextbit =
1628 (fs->blocksize<<3) + 1;
1629 putdref(b,MKREF(cfi_map));
1632 err = lafs_read_block(b);
1636 bit = LAFSI(im)->md.inodemap.nextbit;
1637 LAFSI(im)->md.inodemap.thisblock = bnum;
1638 buf = map_dblock(b);
1639 while (bnum == 0 && bit < 16) {
1640 /* Never return an inum below 16 - they are special */
1641 if (!generic_test_le_bit(bit, (unsigned long *)buf))
1642 generic___clear_le_bit(bit, (unsigned long *)buf);
1646 bit = generic_find_next_le_bit((unsigned long *)buf,
1647 fs->blocksize<<3, bit);
1648 unmap_dblock(b, buf);
1649 LAFSI(im)->md.inodemap.nextbit = bit+1;
1650 if (bit >= fs->blocksize<<3) {
1651 putdref(b,MKREF(cfi_map));
1654 mutex_unlock(&im->i_mutex);
1656 *inump = bit + (bnum << (im->i_blkbits + 3));
1660 lafs_checkpoint_unlock(fs);
1662 putdref(b, MKREF(cfi_map));
1664 mutex_unlock(&im->i_mutex);
1669 struct inode_map_new_info {
1670 struct datablock *ib, *mb;
1674 inode_map_new_prepare(struct fs *fs, int inum, struct inode *fsys,
1675 struct inode_map_new_info *imni)
1680 struct datablock *b;
1682 imni->ib = imni->mb = NULL;
1685 /* choose a possibly-free inode number */
1686 err = choose_free_inum(fs, fsys, &choice,
1687 &imni->mb, &restarted);
1691 b = lafs_get_block(fsys, choice, NULL, GFP_KERNEL,
1696 if (test_and_set_bit(B_Claimed, &b->b.flags)) {
1697 putdref(b, MKREF(cfi_ino));
1703 lafs_iolock_written(&imni->mb->b);
1704 set_bit(B_PinPending, &imni->mb->b.flags);
1705 lafs_iounlock_block(&imni->mb->b);
1707 set_bit(B_PinPending, &b->b.flags);
1714 inode_map_new_pin(struct inode_map_new_info *imni)
1718 err = lafs_pin_dblock(imni->mb, NewSpace);
1719 err = err ?: lafs_pin_dblock(imni->ib, NewSpace);
1724 inode_map_new_commit(struct inode_map_new_info *imni)
1729 int blksize = imni->ib->b.inode->i_sb->s_blocksize;
1730 int bit = imni->ib->b.fileaddr & (blksize*8 - 1);
1732 struct inode *ino = imni->mb->b.inode;
1734 mutex_lock_nested(&ino->i_mutex, I_MUTEX_QUOTA);
1735 buf = map_dblock(imni->mb);
1736 generic___clear_le_bit(bit, buf);
1737 if (buf[blksize/sizeof(*buf)-1] == 0 &&
1738 generic_find_next_le_bit(buf, blksize*8, 0) == blksize*8)
1739 /* block is empty, punch a hole */
1742 unmap_dblock(imni->mb, buf);
1744 lafs_erase_dblock(imni->mb);
1746 lafs_dirty_dblock(imni->mb);
1748 putdref(imni->mb, MKREF(cfi_map));
1749 mutex_unlock(&ino->i_mutex);
1752 putdref(imni->ib, MKREF(cfi_ino));
1756 inode_map_new_abort(struct inode_map_new_info *imni)
1759 clear_bit(B_Claimed, &imni->ib->b.flags);
1760 clear_bit(B_PinPending, &imni->ib->b.flags);
1761 lafs_orphan_release(fs_from_inode(imni->ib->b.inode),
1764 putdref(imni->ib, MKREF(cfi_ino));
1766 struct inode *ino = imni->mb->b.inode;
1767 putdref(imni->mb, MKREF(cfi_map));
1773 lafs_new_inode(struct fs *fs, struct inode *fsys, struct inode *dir,
1774 int type, int inum, int mode, struct datablock **inodbp)
1776 /* allocate and instantiate a new inode. If inum is non-zero,
1777 * choose any number, otherwise we are creating a special inode
1778 * and have to use the given number.
1779 * This creation is committed independently of any name that might
1780 * subsequently be given to the inode. So we register it as an
1781 * orphan so that it will be cleaned up if the name isn't
1782 * successfully created
1786 struct datablock *b;
1787 struct inode_map_new_info imni;
1788 struct update_handle ui;
1791 err = inode_map_new_prepare(fs, inum, fsys, &imni);
1792 err = lafs_cluster_update_prepare(&ui, fs, sizeof(struct la_inode))
1795 err = lafs_make_orphan(fs, imni.ib);
1799 lafs_checkpoint_lock(fs);
1801 err = inode_map_new_pin(&imni);
1803 if (err == -EAGAIN) {
1804 lafs_checkpoint_unlock_wait(fs);
1810 b = getdref(imni.ib, MKREF(inode_new));
1812 lafs_iolock_block(&b->b); /* make sure we don't race with the cleaner
1813 * and zero this inode while trying to load it
1815 lafs_inode_init(b, type, mode, dir);
1816 lafs_iounlock_block(&b->b);
1818 inode_map_new_commit(&imni);
1819 ino = lafs_iget(fsys, b->b.fileaddr, SYNC);
1821 lafs_cluster_update_abort(&ui);
1824 lafs_cluster_update_commit(&ui, b, 0,
1825 LAFSI(ino)->metadata_size);
1826 LAFS_BUG(LAFSI(ino)->dblock != b, &b->b);
1827 LAFS_BUG(b->my_inode != ino, &b->b);
1828 lafs_checkpoint_unlock(fs);
1833 putdref(b, MKREF(inode_new));
1837 lafs_checkpoint_unlock(fs);
1840 inode_map_new_abort(&imni);
1841 lafs_cluster_update_abort(&ui);
1842 dprintk("After abort %d: %s\n", err, strblk(&imni.ib->b));
1843 return ERR_PTR(err);
1846 static int inode_map_free(struct fs *fs, struct inode *fsys, u32 inum)
1848 struct inode *im = lafs_iget(fsys, 1, SYNC);
1851 struct datablock *b;
1855 mutex_lock_nested(&im->i_mutex, I_MUTEX_QUOTA);
1857 bnum = inum >> (3 + fs->blocksize_bits);
1858 bit = inum - (bnum << (3 + fs->blocksize_bits));
1859 b = lafs_get_block(im, bnum, NULL, GFP_KERNEL, MKREF(inode_map_free));
1861 mutex_unlock(&im->i_mutex);
1865 err = lafs_read_block(b);
1867 putdref(b, MKREF(inode_map_free));
1868 mutex_unlock(&im->i_mutex);
1872 lafs_iolock_written(&b->b);
1873 set_bit(B_PinPending, &b->b.flags);
1874 lafs_iounlock_block(&b->b);
1876 lafs_checkpoint_lock(fs);
1877 err = lafs_pin_dblock(b, ReleaseSpace);
1878 if (err == -EAGAIN) {
1879 lafs_checkpoint_unlock_wait(fs);
1883 buf = map_dblock(b);
1884 generic___set_le_bit(bit, buf);
1885 unmap_dblock(b, buf);
1886 lafs_dirty_dblock(b);
1887 putdref(b, MKREF(inode_map_free));
1888 lafs_checkpoint_unlock(fs);
1889 mutex_unlock(&im->i_mutex);
1894 int lafs_inode_inuse(struct fs *fs, struct inode *fsys, u32 inum)
1896 /* This is used during roll-forward to register a newly created
1897 * inode in the inode map
1899 struct inode *im = lafs_iget(fsys, 1, SYNC);
1902 struct datablock *b;
1906 mutex_lock_nested(&im->i_mutex, I_MUTEX_QUOTA);
1908 bnum = inum >> (3 + fs->blocksize_bits);
1909 bit = inum - (bnum << (3 + fs->blocksize_bits));
1910 if (bnum > LAFSI(im)->md.inodemap.size) {
1911 /* inum to unbelievably big */
1912 mutex_unlock(&im->i_mutex);
1916 b = lafs_get_block(im, bnum, NULL, GFP_KERNEL, MKREF(inode_map_free));
1918 mutex_unlock(&im->i_mutex);
1923 err = lafs_read_block(b);
1925 putdref(b, MKREF(inode_map_free));
1926 mutex_unlock(&im->i_mutex);
1931 lafs_iolock_written(&b->b);
1932 set_bit(B_PinPending, &b->b.flags);
1933 lafs_iounlock_block(&b->b);
1935 lafs_checkpoint_lock(fs);
1936 err = lafs_pin_dblock(b, CleanSpace);
1937 if (err == -EAGAIN) {
1938 lafs_checkpoint_unlock_wait(fs);
1942 buf = map_dblock(b);
1943 if (bnum == LAFSI(im)->md.inodemap.size) {
1944 /* need to add a new block to the file */
1945 memset(buf, 0xff, fs->blocksize);
1946 LAFSI(im)->md.inodemap.size = bnum + 1;
1947 lafs_dirty_inode(im);
1949 generic___clear_le_bit(bit, buf);
1950 unmap_dblock(b, buf);
1951 lafs_dirty_dblock(b);
1952 putdref(b, MKREF(inode_map_free));
1953 lafs_checkpoint_unlock(fs);
1954 mutex_unlock(&im->i_mutex);
1961 int lafs_setattr(struct dentry *dentry, struct iattr *attr)
1964 struct inode *ino = dentry->d_inode;
1965 struct fs *fs = fs_from_inode(ino);
1966 struct datablock *db;
1968 err = inode_change_ok(ino, attr);
1969 db = lafs_inode_dblock(ino, SYNC, MKREF(setattr));
1975 /* We don't need iolock_written here as we don't
1976 * actually change the inode block yet
1978 lafs_iolock_block(&db->b);
1979 set_bit(B_PinPending, &db->b.flags);
1980 lafs_iounlock_block(&db->b);
1982 /* FIXME quota stuff */
1985 lafs_checkpoint_lock(fs);
1986 err = lafs_pin_dblock(db, ReleaseSpace);
1987 if (err == -EAGAIN) {
1988 lafs_checkpoint_unlock_wait(fs);
1993 if ((attr->ia_valid & ATTR_SIZE) &&
1994 attr->ia_size != i_size_read(ino))
1995 truncate_setsize(ino, attr->ia_size);
1996 setattr_copy(ino, attr);
1997 mark_inode_dirty(ino);
1999 lafs_dirty_dblock(db);
2001 clear_bit(B_PinPending, &db->b.flags);
2002 putdref(db, MKREF(setattr));
2003 lafs_checkpoint_unlock(fs);
2008 void lafs_truncate(struct inode *ino)
2010 /* Want to truncate this file.
2011 * i_size has already been changed, and the address space
2012 * has been cleaned up.
2013 * So just start the background truncate
2015 struct fs *fs = fs_from_inode(ino);
2016 struct datablock *db = lafs_inode_dblock(ino, SYNC, MKREF(trunc));
2023 trunc_block = ((i_size_read(ino) + fs->blocksize - 1)
2024 >> fs->blocksize_bits);
2025 /* We hold i_mutex, so regular orphan processing cannot
2026 * contine - we have to push it forward ourselves.
2028 while (test_bit(I_Trunc, &LAFSI(ino)->iflags) &&
2029 LAFSI(ino)->trunc_next < trunc_block) {
2030 prepare_to_wait(&fs->async_complete, &wq,
2031 TASK_UNINTERRUPTIBLE);
2032 lafs_inode_handle_orphan(db);
2033 if (test_bit(B_Orphan, &db->b.flags))
2036 finish_wait(&fs->async_complete, &wq);
2038 /* There is nothing we can do about errors here. The
2039 * most likely are ENOMEM which itself is very unlikely.
2040 * If this doesn't get registered as an orphan .... maybe
2041 * it will have to wait until something else truncates it.
2043 lafs_make_orphan(fs, db);
2045 if (!test_and_set_bit(I_Trunc, &LAFSI(ino)->iflags))
2047 if (trunc_block == 0)
2048 LAFSI(ino)->trunc_gen++;
2049 LAFSI(ino)->trunc_next = trunc_block;
2050 putdref(db, MKREF(trunc));
2053 const struct inode_operations lafs_special_ino_operations = {
2054 .setattr = lafs_setattr,
2055 .getattr = lafs_getattr,
2056 .truncate = lafs_truncate,