Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Dave Kleikamp | 1868f4a | 2005-05-04 15:29:35 -0500 | [diff] [blame] | 3 | * Copyright (C) International Business Machines Corp., 2000-2002 |
| 4 | * Portions Copyright (C) Christoph Hellwig, 2001-2002 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 7 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/fs.h> |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 9 | #include <linux/posix_acl.h> |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 10 | #include <linux/quotaops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include "jfs_incore.h" |
Dave Kleikamp | 1868f4a | 2005-05-04 15:29:35 -0500 | [diff] [blame] | 12 | #include "jfs_inode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "jfs_dmap.h" |
| 14 | #include "jfs_txnmgr.h" |
| 15 | #include "jfs_xattr.h" |
| 16 | #include "jfs_acl.h" |
| 17 | #include "jfs_debug.h" |
| 18 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 19 | 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] | 20 | { |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 21 | struct inode *inode = file->f_mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | int rc = 0; |
| 23 | |
Jeff Layton | 3b49c9a | 2017-07-07 15:20:52 -0400 | [diff] [blame] | 24 | rc = file_write_and_wait_range(file, start, end); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 25 | if (rc) |
| 26 | return rc; |
| 27 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 28 | inode_lock(inode); |
Theodore Ts'o | 0ae45f6 | 2015-02-02 00:37:00 -0500 | [diff] [blame] | 29 | if (!(inode->i_state & I_DIRTY_ALL) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | (datasync && !(inode->i_state & I_DIRTY_DATASYNC))) { |
| 31 | /* Make sure committed changes hit the disk */ |
| 32 | jfs_flush_journal(JFS_SBI(inode->i_sb)->log, 1); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 33 | inode_unlock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | return rc; |
| 35 | } |
| 36 | |
| 37 | rc |= jfs_commit_inode(inode, 1); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 38 | inode_unlock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | return rc ? -EIO : 0; |
| 41 | } |
| 42 | |
| 43 | static int jfs_open(struct inode *inode, struct file *file) |
| 44 | { |
| 45 | int rc; |
| 46 | |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 47 | if ((rc = dquot_file_open(inode, file))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | return rc; |
| 49 | |
| 50 | /* |
| 51 | * We attempt to allow only one "active" file open per aggregate |
| 52 | * group. Otherwise, appending to files in parallel can cause |
| 53 | * fragmentation within the files. |
| 54 | * |
| 55 | * If the file is empty, it was probably just created and going |
| 56 | * to be written to. If it has a size, we'll hold off until the |
| 57 | * file is actually grown. |
| 58 | */ |
| 59 | if (S_ISREG(inode->i_mode) && file->f_mode & FMODE_WRITE && |
| 60 | (inode->i_size == 0)) { |
| 61 | struct jfs_inode_info *ji = JFS_IP(inode); |
| 62 | spin_lock_irq(&ji->ag_lock); |
| 63 | if (ji->active_ag == -1) { |
Dave Kleikamp | d31b53e | 2011-06-20 10:53:46 -0500 | [diff] [blame] | 64 | struct jfs_sb_info *jfs_sb = JFS_SBI(inode->i_sb); |
| 65 | ji->active_ag = BLKTOAG(addressPXD(&ji->ixpxd), jfs_sb); |
Nan Jia | 55a8d02 | 2015-05-31 19:53:28 +1000 | [diff] [blame] | 66 | atomic_inc(&jfs_sb->bmap->db_active[ji->active_ag]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | spin_unlock_irq(&ji->ag_lock); |
| 69 | } |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | static int jfs_release(struct inode *inode, struct file *file) |
| 74 | { |
| 75 | struct jfs_inode_info *ji = JFS_IP(inode); |
| 76 | |
| 77 | spin_lock_irq(&ji->ag_lock); |
| 78 | if (ji->active_ag != -1) { |
| 79 | struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap; |
| 80 | atomic_dec(&bmap->db_active[ji->active_ag]); |
| 81 | ji->active_ag = -1; |
| 82 | } |
| 83 | spin_unlock_irq(&ji->ag_lock); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 88 | int jfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, |
| 89 | struct iattr *iattr) |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 90 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 91 | struct inode *inode = d_inode(dentry); |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 92 | int rc; |
| 93 | |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 94 | rc = setattr_prepare(&init_user_ns, dentry, iattr); |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 95 | if (rc) |
| 96 | return rc; |
| 97 | |
Dave Kleikamp | acc84b0 | 2015-07-15 13:53:19 -0500 | [diff] [blame] | 98 | if (is_quota_modification(inode, iattr)) { |
| 99 | rc = dquot_initialize(inode); |
| 100 | if (rc) |
| 101 | return rc; |
| 102 | } |
Eric W. Biederman | c18cdc1 | 2012-02-10 11:40:34 -0800 | [diff] [blame] | 103 | if ((iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)) || |
| 104 | (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] | 105 | rc = dquot_transfer(inode, iattr); |
| 106 | if (rc) |
| 107 | return rc; |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 110 | if ((iattr->ia_valid & ATTR_SIZE) && |
| 111 | iattr->ia_size != i_size_read(inode)) { |
Christoph Hellwig | 562c72aa5 | 2011-06-24 14:29:45 -0400 | [diff] [blame] | 112 | inode_dio_wait(inode); |
| 113 | |
Marco Stornelli | 86dd07d | 2012-12-15 11:54:25 +0100 | [diff] [blame] | 114 | rc = inode_newsize_ok(inode, iattr->ia_size); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 115 | if (rc) |
| 116 | return rc; |
Marco Stornelli | 86dd07d | 2012-12-15 11:54:25 +0100 | [diff] [blame] | 117 | |
| 118 | truncate_setsize(inode, iattr->ia_size); |
| 119 | jfs_truncate(inode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 120 | } |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 121 | |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 122 | setattr_copy(&init_user_ns, inode, iattr); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 123 | mark_inode_dirty(inode); |
| 124 | |
| 125 | if (iattr->ia_valid & ATTR_MODE) |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 126 | rc = posix_acl_chmod(&init_user_ns, inode, inode->i_mode); |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 127 | return rc; |
| 128 | } |
| 129 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 130 | const struct inode_operations jfs_file_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | .listxattr = jfs_listxattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | .setattr = jfs_setattr, |
Miklos Szeredi | 2ca58e3 | 2021-04-07 14:36:44 +0200 | [diff] [blame] | 133 | .fileattr_get = jfs_fileattr_get, |
| 134 | .fileattr_set = jfs_fileattr_set, |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 135 | #ifdef CONFIG_JFS_POSIX_ACL |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 136 | .get_acl = jfs_get_acl, |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 137 | .set_acl = jfs_set_acl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | #endif |
| 139 | }; |
| 140 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 141 | const struct file_operations jfs_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | .open = jfs_open, |
| 143 | .llseek = generic_file_llseek, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 144 | .read_iter = generic_file_read_iter, |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 145 | .write_iter = generic_file_write_iter, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | .mmap = generic_file_mmap, |
Daniel Drake | 89f6822 | 2006-10-30 11:47:02 -0600 | [diff] [blame] | 147 | .splice_read = generic_file_splice_read, |
Al Viro | 8d02076 | 2014-04-05 04:27:08 -0400 | [diff] [blame] | 148 | .splice_write = iter_file_splice_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | .fsync = jfs_fsync, |
| 150 | .release = jfs_release, |
Andi Kleen | baab81f | 2008-01-27 16:58:51 -0600 | [diff] [blame] | 151 | .unlocked_ioctl = jfs_ioctl, |
Miklos Szeredi | 2ca58e3 | 2021-04-07 14:36:44 +0200 | [diff] [blame] | 152 | .compat_ioctl = compat_ptr_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | }; |