9f32315acef1584877e40d9bbbf59645dcbba6ac
[linux-2.6-microblaze.git] / fs / jfs / file.c
1 /*
2  *   Copyright (C) International Business Machines Corp., 2000-2002
3  *   Portions Copyright (C) Christoph Hellwig, 2001-2002
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19
20 #include <linux/mm.h>
21 #include <linux/fs.h>
22 #include <linux/quotaops.h>
23 #include "jfs_incore.h"
24 #include "jfs_inode.h"
25 #include "jfs_dmap.h"
26 #include "jfs_txnmgr.h"
27 #include "jfs_xattr.h"
28 #include "jfs_acl.h"
29 #include "jfs_debug.h"
30
31 int jfs_fsync(struct file *file, int datasync)
32 {
33         struct inode *inode = file->f_mapping->host;
34         int rc = 0;
35
36         if (!(inode->i_state & I_DIRTY) ||
37             (datasync && !(inode->i_state & I_DIRTY_DATASYNC))) {
38                 /* Make sure committed changes hit the disk */
39                 jfs_flush_journal(JFS_SBI(inode->i_sb)->log, 1);
40                 return rc;
41         }
42
43         rc |= jfs_commit_inode(inode, 1);
44
45         return rc ? -EIO : 0;
46 }
47
48 static int jfs_open(struct inode *inode, struct file *file)
49 {
50         int rc;
51
52         if ((rc = dquot_file_open(inode, file)))
53                 return rc;
54
55         /*
56          * We attempt to allow only one "active" file open per aggregate
57          * group.  Otherwise, appending to files in parallel can cause
58          * fragmentation within the files.
59          *
60          * If the file is empty, it was probably just created and going
61          * to be written to.  If it has a size, we'll hold off until the
62          * file is actually grown.
63          */
64         if (S_ISREG(inode->i_mode) && file->f_mode & FMODE_WRITE &&
65             (inode->i_size == 0)) {
66                 struct jfs_inode_info *ji = JFS_IP(inode);
67                 spin_lock_irq(&ji->ag_lock);
68                 if (ji->active_ag == -1) {
69                         struct jfs_sb_info *jfs_sb = JFS_SBI(inode->i_sb);
70                         ji->active_ag = BLKTOAG(addressPXD(&ji->ixpxd), jfs_sb);
71                         atomic_inc( &jfs_sb->bmap->db_active[ji->active_ag]);
72                 }
73                 spin_unlock_irq(&ji->ag_lock);
74         }
75
76         return 0;
77 }
78 static int jfs_release(struct inode *inode, struct file *file)
79 {
80         struct jfs_inode_info *ji = JFS_IP(inode);
81
82         spin_lock_irq(&ji->ag_lock);
83         if (ji->active_ag != -1) {
84                 struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
85                 atomic_dec(&bmap->db_active[ji->active_ag]);
86                 ji->active_ag = -1;
87         }
88         spin_unlock_irq(&ji->ag_lock);
89
90         return 0;
91 }
92
93 int jfs_setattr(struct dentry *dentry, struct iattr *iattr)
94 {
95         struct inode *inode = dentry->d_inode;
96         int rc;
97
98         rc = inode_change_ok(inode, iattr);
99         if (rc)
100                 return rc;
101
102         if (is_quota_modification(inode, iattr))
103                 dquot_initialize(inode);
104         if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
105             (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
106                 rc = dquot_transfer(inode, iattr);
107                 if (rc)
108                         return rc;
109         }
110
111         if ((iattr->ia_valid & ATTR_SIZE) &&
112             iattr->ia_size != i_size_read(inode)) {
113                 inode_dio_wait(inode);
114
115                 rc = vmtruncate(inode, iattr->ia_size);
116                 if (rc)
117                         return rc;
118         }
119
120         setattr_copy(inode, iattr);
121         mark_inode_dirty(inode);
122
123         if (iattr->ia_valid & ATTR_MODE)
124                 rc = jfs_acl_chmod(inode);
125         return rc;
126 }
127
128 const struct inode_operations jfs_file_inode_operations = {
129         .truncate       = jfs_truncate,
130         .setxattr       = jfs_setxattr,
131         .getxattr       = jfs_getxattr,
132         .listxattr      = jfs_listxattr,
133         .removexattr    = jfs_removexattr,
134         .setattr        = jfs_setattr,
135 #ifdef CONFIG_JFS_POSIX_ACL
136         .check_acl      = jfs_check_acl,
137 #endif
138 };
139
140 const struct file_operations jfs_file_operations = {
141         .open           = jfs_open,
142         .llseek         = generic_file_llseek,
143         .write          = do_sync_write,
144         .read           = do_sync_read,
145         .aio_read       = generic_file_aio_read,
146         .aio_write      = generic_file_aio_write,
147         .mmap           = generic_file_mmap,
148         .splice_read    = generic_file_splice_read,
149         .splice_write   = generic_file_splice_write,
150         .fsync          = jfs_fsync,
151         .release        = jfs_release,
152         .unlocked_ioctl = jfs_ioctl,
153 #ifdef CONFIG_COMPAT
154         .compat_ioctl   = jfs_compat_ioctl,
155 #endif
156 };