blob: 930d2701f2062bc6801a6fdaf978742c679dc09e [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Dave Kleikamp1868f4a2005-05-04 15:29:35 -05003 * Copyright (C) International Business Machines Corp., 2000-2002
4 * Portions Copyright (C) Christoph Hellwig, 2001-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Christoph Hellwig10257742010-06-04 11:30:02 +02007#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/fs.h>
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -08009#include <linux/posix_acl.h>
Christoph Hellwig759bfee2010-03-03 09:05:02 -050010#include <linux/quotaops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "jfs_incore.h"
Dave Kleikamp1868f4a2005-05-04 15:29:35 -050012#include "jfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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 Bacik02c24a82011-07-16 20:44:56 -040019int jfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
Christoph Hellwig7ea80852010-05-26 17:53:25 +020021 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 int rc = 0;
23
Jeff Layton3b49c9a2017-07-07 15:20:52 -040024 rc = file_write_and_wait_range(file, start, end);
Josef Bacik02c24a82011-07-16 20:44:56 -040025 if (rc)
26 return rc;
27
Al Viro59551022016-01-22 15:40:57 -050028 inode_lock(inode);
Theodore Ts'o0ae45f62015-02-02 00:37:00 -050029 if (!(inode->i_state & I_DIRTY_ALL) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 (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 Viro59551022016-01-22 15:40:57 -050033 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return rc;
35 }
36
37 rc |= jfs_commit_inode(inode, 1);
Al Viro59551022016-01-22 15:40:57 -050038 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40 return rc ? -EIO : 0;
41}
42
43static int jfs_open(struct inode *inode, struct file *file)
44{
45 int rc;
46
Christoph Hellwig907f4552010-03-03 09:05:06 -050047 if ((rc = dquot_file_open(inode, file)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 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 Kleikampd31b53e2011-06-20 10:53:46 -050064 struct jfs_sb_info *jfs_sb = JFS_SBI(inode->i_sb);
65 ji->active_ag = BLKTOAG(addressPXD(&ji->ixpxd), jfs_sb);
Nan Jia55a8d022015-05-31 19:53:28 +100066 atomic_inc(&jfs_sb->bmap->db_active[ji->active_ag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
68 spin_unlock_irq(&ji->ag_lock);
69 }
70
71 return 0;
72}
73static 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
Christoph Hellwig759bfee2010-03-03 09:05:02 -050088int jfs_setattr(struct dentry *dentry, struct iattr *iattr)
89{
David Howells2b0143b2015-03-17 22:25:59 +000090 struct inode *inode = d_inode(dentry);
Christoph Hellwig759bfee2010-03-03 09:05:02 -050091 int rc;
92
Jan Kara31051c82016-05-26 16:55:18 +020093 rc = setattr_prepare(dentry, iattr);
Christoph Hellwig759bfee2010-03-03 09:05:02 -050094 if (rc)
95 return rc;
96
Dave Kleikampacc84b02015-07-15 13:53:19 -050097 if (is_quota_modification(inode, iattr)) {
98 rc = dquot_initialize(inode);
99 if (rc)
100 return rc;
101 }
Eric W. Biedermanc18cdc12012-02-10 11:40:34 -0800102 if ((iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)) ||
103 (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid))) {
Christoph Hellwigb43fa822010-03-03 09:05:03 -0500104 rc = dquot_transfer(inode, iattr);
105 if (rc)
106 return rc;
Christoph Hellwig759bfee2010-03-03 09:05:02 -0500107 }
108
Christoph Hellwig10257742010-06-04 11:30:02 +0200109 if ((iattr->ia_valid & ATTR_SIZE) &&
110 iattr->ia_size != i_size_read(inode)) {
Christoph Hellwig562c72aa52011-06-24 14:29:45 -0400111 inode_dio_wait(inode);
112
Marco Stornelli86dd07d2012-12-15 11:54:25 +0100113 rc = inode_newsize_ok(inode, iattr->ia_size);
Christoph Hellwig10257742010-06-04 11:30:02 +0200114 if (rc)
115 return rc;
Marco Stornelli86dd07d2012-12-15 11:54:25 +0100116
117 truncate_setsize(inode, iattr->ia_size);
118 jfs_truncate(inode);
Christoph Hellwig10257742010-06-04 11:30:02 +0200119 }
Christoph Hellwig759bfee2010-03-03 09:05:02 -0500120
Christoph Hellwig10257742010-06-04 11:30:02 +0200121 setattr_copy(inode, iattr);
122 mark_inode_dirty(inode);
123
124 if (iattr->ia_valid & ATTR_MODE)
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800125 rc = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig759bfee2010-03-03 09:05:02 -0500126 return rc;
127}
128
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800129const struct inode_operations jfs_file_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .listxattr = jfs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .setattr = jfs_setattr,
Christoph Hellwig759bfee2010-03-03 09:05:02 -0500132#ifdef CONFIG_JFS_POSIX_ACL
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200133 .get_acl = jfs_get_acl,
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800134 .set_acl = jfs_set_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,
Al Viroaad4f8b2014-04-02 14:33:16 -0400141 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400142 .write_iter = generic_file_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .mmap = generic_file_mmap,
Daniel Drake89f68222006-10-30 11:47:02 -0600144 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -0400145 .splice_write = iter_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .fsync = jfs_fsync,
147 .release = jfs_release,
Andi Kleenbaab81f2008-01-27 16:58:51 -0600148 .unlocked_ioctl = jfs_ioctl,
Andi Kleenef1fc2f2008-01-27 17:02:02 -0600149#ifdef CONFIG_COMPAT
150 .compat_ioctl = jfs_compat_ioctl,
151#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};