vfs: push dentry_unhash on rmdir into file systems
[linux-2.6.git] / fs / hfs / inode.c
index 39fd85b..fff16c9 100644 (file)
 
 #include <linux/pagemap.h>
 #include <linux/mpage.h>
+#include <linux/sched.h>
 
 #include "hfs_fs.h"
 #include "btree.h"
 
-static struct file_operations hfs_file_operations;
-static struct inode_operations hfs_file_inode_operations;
+static const struct file_operations hfs_file_operations;
+static const struct inode_operations hfs_file_inode_operations;
 
 /*================ Variable-like macros ================*/
 
@@ -34,10 +35,23 @@ static int hfs_readpage(struct file *file, struct page *page)
        return block_read_full_page(page, hfs_get_block);
 }
 
-static int hfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
+static int hfs_write_begin(struct file *file, struct address_space *mapping,
+                       loff_t pos, unsigned len, unsigned flags,
+                       struct page **pagep, void **fsdata)
 {
-       return cont_prepare_write(page, from, to, hfs_get_block,
-                                 &HFS_I(page->mapping->host)->phys_size);
+       int ret;
+
+       *pagep = NULL;
+       ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
+                               hfs_get_block,
+                               &HFS_I(mapping->host)->phys_size);
+       if (unlikely(ret)) {
+               loff_t isize = mapping->host->i_size;
+               if (pos + len > isize)
+                       vmtruncate(mapping->host, isize);
+       }
+
+       return ret;
 }
 
 static sector_t hfs_bmap(struct address_space *mapping, sector_t block)
@@ -65,6 +79,10 @@ static int hfs_releasepage(struct page *page, gfp_t mask)
                BUG();
                return 0;
        }
+
+       if (!tree)
+               return 0;
+
        if (tree->node_size >= PAGE_CACHE_SIZE) {
                nidx = page->index >> (tree->node_size_shift - PAGE_CACHE_SHIFT);
                spin_lock(&tree->hash_lock);
@@ -98,25 +116,29 @@ static int hfs_releasepage(struct page *page, gfp_t mask)
        return res ? try_to_free_buffers(page) : 0;
 }
 
-static int hfs_get_blocks(struct inode *inode, sector_t iblock, unsigned long max_blocks,
-                         struct buffer_head *bh_result, int create)
-{
-       int ret;
-
-       ret = hfs_get_block(inode, iblock, bh_result, create);
-       if (!ret)
-               bh_result->b_size = (1 << inode->i_blkbits);
-       return ret;
-}
-
 static ssize_t hfs_direct_IO(int rw, struct kiocb *iocb,
                const struct iovec *iov, loff_t offset, unsigned long nr_segs)
 {
        struct file *file = iocb->ki_filp;
-       struct inode *inode = file->f_dentry->d_inode->i_mapping->host;
+       struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host;
+       ssize_t ret;
+
+       ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
+                                 offset, nr_segs, hfs_get_block, NULL);
+
+       /*
+        * In case of error extending write may have instantiated a few
+        * blocks outside i_size. Trim these off again.
+        */
+       if (unlikely((rw & WRITE) && ret < 0)) {
+               loff_t isize = i_size_read(inode);
+               loff_t end = offset + iov_length(iov, nr_segs);
+
+               if (end > isize)
+                       vmtruncate(inode, isize);
+       }
 
-       return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
-                                 offset, nr_segs, hfs_get_blocks, NULL);
+       return ret;
 }
 
 static int hfs_writepages(struct address_space *mapping,
@@ -125,22 +147,20 @@ static int hfs_writepages(struct address_space *mapping,
        return mpage_writepages(mapping, wbc, hfs_get_block);
 }
 
-struct address_space_operations hfs_btree_aops = {
+const struct address_space_operations hfs_btree_aops = {
        .readpage       = hfs_readpage,
        .writepage      = hfs_writepage,
-       .sync_page      = block_sync_page,
-       .prepare_write  = hfs_prepare_write,
-       .commit_write   = generic_commit_write,
+       .write_begin    = hfs_write_begin,
+       .write_end      = generic_write_end,
        .bmap           = hfs_bmap,
        .releasepage    = hfs_releasepage,
 };
 
-struct address_space_operations hfs_aops = {
+const struct address_space_operations hfs_aops = {
        .readpage       = hfs_readpage,
        .writepage      = hfs_writepage,
-       .sync_page      = block_sync_page,
-       .prepare_write  = hfs_prepare_write,
-       .commit_write   = generic_commit_write,
+       .write_begin    = hfs_write_begin,
+       .write_end      = generic_write_end,
        .bmap           = hfs_bmap,
        .direct_IO      = hfs_direct_IO,
        .writepages     = hfs_writepages,
@@ -156,16 +176,15 @@ struct inode *hfs_new_inode(struct inode *dir, struct qstr *name, int mode)
        if (!inode)
                return NULL;
 
-       init_MUTEX(&HFS_I(inode)->extents_lock);
+       mutex_init(&HFS_I(inode)->extents_lock);
        INIT_LIST_HEAD(&HFS_I(inode)->open_dir_list);
        hfs_cat_build_key(sb, (btree_key *)&HFS_I(inode)->cat_key, dir->i_ino, name);
        inode->i_ino = HFS_SB(sb)->next_id++;
        inode->i_mode = mode;
-       inode->i_uid = current->fsuid;
-       inode->i_gid = current->fsgid;
+       inode->i_uid = current_fsuid();
+       inode->i_gid = current_fsgid();
        inode->i_nlink = 1;
        inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
-       inode->i_blksize = HFS_SB(sb)->alloc_blksz;
        HFS_I(inode)->flags = 0;
        HFS_I(inode)->rsrc_inode = NULL;
        HFS_I(inode)->fs_blocks = 0;
@@ -288,14 +307,13 @@ static int hfs_read_inode(struct inode *inode, void *data)
 
        HFS_I(inode)->flags = 0;
        HFS_I(inode)->rsrc_inode = NULL;
-       init_MUTEX(&HFS_I(inode)->extents_lock);
+       mutex_init(&HFS_I(inode)->extents_lock);
        INIT_LIST_HEAD(&HFS_I(inode)->open_dir_list);
 
        /* Initialize the inode */
        inode->i_uid = hsb->s_uid;
        inode->i_gid = hsb->s_gid;
        inode->i_nlink = 1;
-       inode->i_blksize = HFS_SB(inode->i_sb)->alloc_blksz;
 
        if (idata->key)
                HFS_I(inode)->cat_key = *idata->key;
@@ -385,7 +403,7 @@ void hfs_inode_write_fork(struct inode *inode, struct hfs_extent *ext,
                                         HFS_SB(inode->i_sb)->alloc_blksz);
 }
 
-int hfs_write_inode(struct inode *inode, int unused)
+int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)
 {
        struct inode *main_inode = inode;
        struct hfs_find_data fd;
@@ -504,35 +522,27 @@ static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry,
        HFS_I(inode)->rsrc_inode = dir;
        HFS_I(dir)->rsrc_inode = inode;
        igrab(dir);
-       hlist_add_head(&inode->i_hash, &HFS_SB(dir->i_sb)->rsrc_inodes);
+       hlist_add_fake(&inode->i_hash);
        mark_inode_dirty(inode);
 out:
        d_add(dentry, inode);
        return NULL;
 }
 
-void hfs_clear_inode(struct inode *inode)
+void hfs_evict_inode(struct inode *inode)
 {
+       truncate_inode_pages(&inode->i_data, 0);
+       end_writeback(inode);
        if (HFS_IS_RSRC(inode) && HFS_I(inode)->rsrc_inode) {
                HFS_I(HFS_I(inode)->rsrc_inode)->rsrc_inode = NULL;
                iput(HFS_I(inode)->rsrc_inode);
        }
 }
 
-static int hfs_permission(struct inode *inode, int mask,
-                         struct nameidata *nd)
-{
-       if (S_ISREG(inode->i_mode) && mask & MAY_EXEC)
-               return 0;
-       return generic_permission(inode, mask, NULL);
-}
-
 static int hfs_file_open(struct inode *inode, struct file *file)
 {
        if (HFS_IS_RSRC(inode))
                inode = HFS_I(inode)->rsrc_inode;
-       if (atomic_read(&file->f_count) != 1)
-               return 0;
        atomic_inc(&HFS_I(inode)->opencnt);
        return 0;
 }
@@ -543,8 +553,6 @@ static int hfs_file_release(struct inode *inode, struct file *file)
 
        if (HFS_IS_RSRC(inode))
                inode = HFS_I(inode)->rsrc_inode;
-       if (atomic_read(&file->f_count) != 0)
-               return 0;
        if (atomic_dec_and_test(&HFS_I(inode)->opencnt)) {
                mutex_lock(&inode->i_mutex);
                hfs_file_truncate(inode);
@@ -604,30 +612,61 @@ int hfs_inode_setattr(struct dentry *dentry, struct iattr * attr)
                        attr->ia_mode = inode->i_mode & ~S_IWUGO;
                attr->ia_mode &= S_ISDIR(inode->i_mode) ? ~hsb->s_dir_umask: ~hsb->s_file_umask;
        }
-       error = inode_setattr(inode, attr);
-       if (error)
-               return error;
 
+       if ((attr->ia_valid & ATTR_SIZE) &&
+           attr->ia_size != i_size_read(inode)) {
+               error = vmtruncate(inode, attr->ia_size);
+               if (error)
+                       return error;
+       }
+
+       setattr_copy(inode, attr);
+       mark_inode_dirty(inode);
        return 0;
 }
 
+static int hfs_file_fsync(struct file *filp, int datasync)
+{
+       struct inode *inode = filp->f_mapping->host;
+       struct super_block * sb;
+       int ret, err;
+
+       /* sync the inode to buffers */
+       ret = write_inode_now(inode, 0);
+
+       /* sync the superblock to buffers */
+       sb = inode->i_sb;
+       if (sb->s_dirt) {
+               lock_super(sb);
+               sb->s_dirt = 0;
+               if (!(sb->s_flags & MS_RDONLY))
+                       hfs_mdb_commit(sb);
+               unlock_super(sb);
+       }
+       /* .. finally sync the buffers to disk */
+       err = sync_blockdev(sb->s_bdev);
+       if (!ret)
+               ret = err;
+       return ret;
+}
 
-static struct file_operations hfs_file_operations = {
+static const struct file_operations hfs_file_operations = {
        .llseek         = generic_file_llseek,
-       .read           = generic_file_read,
-       .write          = generic_file_write,
+       .read           = do_sync_read,
+       .aio_read       = generic_file_aio_read,
+       .write          = do_sync_write,
+       .aio_write      = generic_file_aio_write,
        .mmap           = generic_file_mmap,
-       .sendfile       = generic_file_sendfile,
-       .fsync          = file_fsync,
+       .splice_read    = generic_file_splice_read,
+       .fsync          = hfs_file_fsync,
        .open           = hfs_file_open,
        .release        = hfs_file_release,
 };
 
-static struct inode_operations hfs_file_inode_operations = {
+static const struct inode_operations hfs_file_inode_operations = {
        .lookup         = hfs_file_lookup,
        .truncate       = hfs_file_truncate,
        .setattr        = hfs_inode_setattr,
-       .permission     = hfs_permission,
        .setxattr       = hfs_setxattr,
        .getxattr       = hfs_getxattr,
        .listxattr      = hfs_listxattr,