Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Dave Kleikamp | 1868f4a | 2005-05-04 15:29:35 -0500 | [diff] [blame] | 2 | * Copyright (C) International Business Machines Corp., 2000-2002 |
| 3 | * Portions Copyright (C) Christoph Hellwig, 2001-2002 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 7 | * the Free Software Foundation; either version 2 of the License, or |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * (at your option) any later version. |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 16 | * along with this program; if not, write to the Free Software |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 20 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/fs.h> |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 22 | #include <linux/posix_acl.h> |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 23 | #include <linux/quotaops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "jfs_incore.h" |
Dave Kleikamp | 1868f4a | 2005-05-04 15:29:35 -0500 | [diff] [blame] | 25 | #include "jfs_inode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "jfs_dmap.h" |
| 27 | #include "jfs_txnmgr.h" |
| 28 | #include "jfs_xattr.h" |
| 29 | #include "jfs_acl.h" |
| 30 | #include "jfs_debug.h" |
| 31 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 32 | int jfs_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 34 | struct inode *inode = file->f_mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | int rc = 0; |
| 36 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 37 | rc = filemap_write_and_wait_range(inode->i_mapping, start, end); |
| 38 | if (rc) |
| 39 | return rc; |
| 40 | |
| 41 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | if (!(inode->i_state & I_DIRTY) || |
| 43 | (datasync && !(inode->i_state & I_DIRTY_DATASYNC))) { |
| 44 | /* Make sure committed changes hit the disk */ |
| 45 | jfs_flush_journal(JFS_SBI(inode->i_sb)->log, 1); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 46 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | return rc; |
| 48 | } |
| 49 | |
| 50 | rc |= jfs_commit_inode(inode, 1); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 51 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | return rc ? -EIO : 0; |
| 54 | } |
| 55 | |
| 56 | static int jfs_open(struct inode *inode, struct file *file) |
| 57 | { |
| 58 | int rc; |
| 59 | |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 60 | if ((rc = dquot_file_open(inode, file))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | return rc; |
| 62 | |
| 63 | /* |
| 64 | * We attempt to allow only one "active" file open per aggregate |
| 65 | * group. Otherwise, appending to files in parallel can cause |
| 66 | * fragmentation within the files. |
| 67 | * |
| 68 | * If the file is empty, it was probably just created and going |
| 69 | * to be written to. If it has a size, we'll hold off until the |
| 70 | * file is actually grown. |
| 71 | */ |
| 72 | if (S_ISREG(inode->i_mode) && file->f_mode & FMODE_WRITE && |
| 73 | (inode->i_size == 0)) { |
| 74 | struct jfs_inode_info *ji = JFS_IP(inode); |
| 75 | spin_lock_irq(&ji->ag_lock); |
| 76 | if (ji->active_ag == -1) { |
Dave Kleikamp | d31b53e | 2011-06-20 10:53:46 -0500 | [diff] [blame] | 77 | struct jfs_sb_info *jfs_sb = JFS_SBI(inode->i_sb); |
| 78 | ji->active_ag = BLKTOAG(addressPXD(&ji->ixpxd), jfs_sb); |
| 79 | atomic_inc( &jfs_sb->bmap->db_active[ji->active_ag]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | spin_unlock_irq(&ji->ag_lock); |
| 82 | } |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | static int jfs_release(struct inode *inode, struct file *file) |
| 87 | { |
| 88 | struct jfs_inode_info *ji = JFS_IP(inode); |
| 89 | |
| 90 | spin_lock_irq(&ji->ag_lock); |
| 91 | if (ji->active_ag != -1) { |
| 92 | struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap; |
| 93 | atomic_dec(&bmap->db_active[ji->active_ag]); |
| 94 | ji->active_ag = -1; |
| 95 | } |
| 96 | spin_unlock_irq(&ji->ag_lock); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 101 | int jfs_setattr(struct dentry *dentry, struct iattr *iattr) |
| 102 | { |
| 103 | struct inode *inode = dentry->d_inode; |
| 104 | int rc; |
| 105 | |
| 106 | rc = inode_change_ok(inode, iattr); |
| 107 | if (rc) |
| 108 | return rc; |
| 109 | |
Dmitry Monakhov | 1275562 | 2010-04-08 22:04:20 +0400 | [diff] [blame] | 110 | if (is_quota_modification(inode, iattr)) |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 111 | dquot_initialize(inode); |
Eric W. Biederman | c18cdc1 | 2012-02-10 11:40:34 -0800 | [diff] [blame] | 112 | if ((iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)) || |
| 113 | (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid))) { |
Christoph Hellwig | b43fa82 | 2010-03-03 09:05:03 -0500 | [diff] [blame] | 114 | rc = dquot_transfer(inode, iattr); |
| 115 | if (rc) |
| 116 | return rc; |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 119 | if ((iattr->ia_valid & ATTR_SIZE) && |
| 120 | iattr->ia_size != i_size_read(inode)) { |
Christoph Hellwig | 562c72a | 2011-06-24 14:29:45 -0400 | [diff] [blame] | 121 | inode_dio_wait(inode); |
| 122 | |
Marco Stornelli | 86dd07d | 2012-12-15 11:54:25 +0100 | [diff] [blame] | 123 | rc = inode_newsize_ok(inode, iattr->ia_size); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 124 | if (rc) |
| 125 | return rc; |
Marco Stornelli | 86dd07d | 2012-12-15 11:54:25 +0100 | [diff] [blame] | 126 | |
| 127 | truncate_setsize(inode, iattr->ia_size); |
| 128 | jfs_truncate(inode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 129 | } |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 130 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 131 | setattr_copy(inode, iattr); |
| 132 | mark_inode_dirty(inode); |
| 133 | |
| 134 | if (iattr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 135 | rc = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 136 | return rc; |
| 137 | } |
| 138 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 139 | const struct inode_operations jfs_file_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | .setxattr = jfs_setxattr, |
| 141 | .getxattr = jfs_getxattr, |
| 142 | .listxattr = jfs_listxattr, |
| 143 | .removexattr = jfs_removexattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | .setattr = jfs_setattr, |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 145 | #ifdef CONFIG_JFS_POSIX_ACL |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 146 | .get_acl = jfs_get_acl, |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 147 | .set_acl = jfs_set_acl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | #endif |
| 149 | }; |
| 150 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 151 | const struct file_operations jfs_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | .open = jfs_open, |
| 153 | .llseek = generic_file_llseek, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 154 | .write = do_sync_write, |
| 155 | .read = do_sync_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | .aio_read = generic_file_aio_read, |
| 157 | .aio_write = generic_file_aio_write, |
| 158 | .mmap = generic_file_mmap, |
Daniel Drake | 89f6822 | 2006-10-30 11:47:02 -0600 | [diff] [blame] | 159 | .splice_read = generic_file_splice_read, |
| 160 | .splice_write = generic_file_splice_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | .fsync = jfs_fsync, |
| 162 | .release = jfs_release, |
Andi Kleen | baab81f | 2008-01-27 16:58:51 -0600 | [diff] [blame] | 163 | .unlocked_ioctl = jfs_ioctl, |
Andi Kleen | ef1fc2f | 2008-01-27 17:02:02 -0600 | [diff] [blame] | 164 | #ifdef CONFIG_COMPAT |
| 165 | .compat_ioctl = jfs_compat_ioctl, |
| 166 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | }; |