blob: c5ce6c1d1ff406c567d0b59d0ffe03ed11b8daa5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Dave Kleikamp1868f4a2005-05-04 15:29:35 -05002 * Copyright (C) International Business Machines Corp., 2000-2002
3 * Portions Copyright (C) Christoph Hellwig, 2001-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Dave Kleikamp63f83c92006-10-02 09:55:27 -05007 * the Free Software Foundation; either version 2 of the License, or
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * (at your option) any later version.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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
Dave Kleikamp63f83c92006-10-02 09:55:27 -050016 * along with this program; if not, write to the Free Software
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Christoph Hellwig10257742010-06-04 11:30:02 +020020#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/fs.h>
Christoph Hellwig759bfee2010-03-03 09:05:02 -050022#include <linux/quotaops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "jfs_incore.h"
Dave Kleikamp1868f4a2005-05-04 15:29:35 -050024#include "jfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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
Christoph Hellwig7ea80852010-05-26 17:53:25 +020031int jfs_fsync(struct file *file, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Christoph Hellwig7ea80852010-05-26 17:53:25 +020033 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 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
48static int jfs_open(struct inode *inode, struct file *file)
49{
50 int rc;
51
Christoph Hellwig907f4552010-03-03 09:05:06 -050052 if ((rc = dquot_file_open(inode, file)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 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 ji->active_ag = ji->agno;
70 atomic_inc(
71 &JFS_SBI(inode->i_sb)->bmap->db_active[ji->agno]);
72 }
73 spin_unlock_irq(&ji->ag_lock);
74 }
75
76 return 0;
77}
78static 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
Christoph Hellwig759bfee2010-03-03 09:05:02 -050093int 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
Dmitry Monakhov12755622010-04-08 22:04:20 +0400102 if (is_quota_modification(inode, iattr))
Christoph Hellwig871a2932010-03-03 09:05:07 -0500103 dquot_initialize(inode);
Christoph Hellwig759bfee2010-03-03 09:05:02 -0500104 if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
105 (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
Christoph Hellwigb43fa822010-03-03 09:05:03 -0500106 rc = dquot_transfer(inode, iattr);
107 if (rc)
108 return rc;
Christoph Hellwig759bfee2010-03-03 09:05:02 -0500109 }
110
Christoph Hellwig10257742010-06-04 11:30:02 +0200111 if ((iattr->ia_valid & ATTR_SIZE) &&
112 iattr->ia_size != i_size_read(inode)) {
113 rc = vmtruncate(inode, iattr->ia_size);
114 if (rc)
115 return rc;
116 }
Christoph Hellwig759bfee2010-03-03 09:05:02 -0500117
Christoph Hellwig10257742010-06-04 11:30:02 +0200118 setattr_copy(inode, iattr);
119 mark_inode_dirty(inode);
120
121 if (iattr->ia_valid & ATTR_MODE)
Christoph Hellwig759bfee2010-03-03 09:05:02 -0500122 rc = jfs_acl_chmod(inode);
Christoph Hellwig759bfee2010-03-03 09:05:02 -0500123 return rc;
124}
125
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800126const struct inode_operations jfs_file_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .truncate = jfs_truncate,
128 .setxattr = jfs_setxattr,
129 .getxattr = jfs_getxattr,
130 .listxattr = jfs_listxattr,
131 .removexattr = jfs_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .setattr = jfs_setattr,
Christoph Hellwig759bfee2010-03-03 09:05:02 -0500133#ifdef CONFIG_JFS_POSIX_ACL
Linus Torvalds18f4c642009-08-28 12:29:03 -0700134 .check_acl = jfs_check_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#endif
136};
137
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800138const struct file_operations jfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .open = jfs_open,
140 .llseek = generic_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -0700141 .write = do_sync_write,
142 .read = do_sync_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .aio_read = generic_file_aio_read,
144 .aio_write = generic_file_aio_write,
145 .mmap = generic_file_mmap,
Daniel Drake89f68222006-10-30 11:47:02 -0600146 .splice_read = generic_file_splice_read,
147 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .fsync = jfs_fsync,
149 .release = jfs_release,
Andi Kleenbaab81f2008-01-27 16:58:51 -0600150 .unlocked_ioctl = jfs_ioctl,
Andi Kleenef1fc2f2008-01-27 17:02:02 -0600151#ifdef CONFIG_COMPAT
152 .compat_ioctl = jfs_compat_ioctl,
153#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};