^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) International Business Machines Corp., 2000-2002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Portions Copyright (C) Christoph Hellwig, 2001-2002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/posix_acl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "jfs_incore.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "jfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "jfs_dmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "jfs_txnmgr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "jfs_xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "jfs_acl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "jfs_debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int jfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct inode *inode = file->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) rc = file_write_and_wait_range(file, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (!(inode->i_state & I_DIRTY_ALL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) (datasync && !(inode->i_state & I_DIRTY_DATASYNC))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /* Make sure committed changes hit the disk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) jfs_flush_journal(JFS_SBI(inode->i_sb)->log, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) rc |= jfs_commit_inode(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return rc ? -EIO : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static int jfs_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if ((rc = dquot_file_open(inode, file)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * We attempt to allow only one "active" file open per aggregate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * group. Otherwise, appending to files in parallel can cause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * fragmentation within the files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * If the file is empty, it was probably just created and going
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * to be written to. If it has a size, we'll hold off until the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * file is actually grown.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (S_ISREG(inode->i_mode) && file->f_mode & FMODE_WRITE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) (inode->i_size == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct jfs_inode_info *ji = JFS_IP(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) spin_lock_irq(&ji->ag_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (ji->active_ag == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct jfs_sb_info *jfs_sb = JFS_SBI(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) ji->active_ag = BLKTOAG(addressPXD(&ji->ixpxd), jfs_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) atomic_inc(&jfs_sb->bmap->db_active[ji->active_ag]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) spin_unlock_irq(&ji->ag_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static int jfs_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct jfs_inode_info *ji = JFS_IP(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) spin_lock_irq(&ji->ag_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (ji->active_ag != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) atomic_dec(&bmap->db_active[ji->active_ag]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) ji->active_ag = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) spin_unlock_irq(&ji->ag_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int jfs_setattr(struct dentry *dentry, struct iattr *iattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) rc = setattr_prepare(dentry, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (is_quota_modification(inode, iattr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) rc = dquot_initialize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if ((iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) rc = dquot_transfer(inode, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if ((iattr->ia_valid & ATTR_SIZE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) iattr->ia_size != i_size_read(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) inode_dio_wait(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) rc = inode_newsize_ok(inode, iattr->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) truncate_setsize(inode, iattr->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) jfs_truncate(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) setattr_copy(inode, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (iattr->ia_valid & ATTR_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) rc = posix_acl_chmod(inode, inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) const struct inode_operations jfs_file_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) .listxattr = jfs_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .setattr = jfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #ifdef CONFIG_JFS_POSIX_ACL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .get_acl = jfs_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) .set_acl = jfs_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) const struct file_operations jfs_file_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) .open = jfs_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) .read_iter = generic_file_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .write_iter = generic_file_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .mmap = generic_file_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .splice_read = generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .splice_write = iter_file_splice_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .fsync = jfs_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .release = jfs_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .unlocked_ioctl = jfs_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) .compat_ioctl = jfs_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) };