blob: 3f2ae19c8ca0a48066de935a09f6099f07b77611 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
19#include "xfs_fs.h"
Dave Chinner70a9883c2013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110027#include "xfs_da_format.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_dinode.h"
30#include "xfs_inode.h"
31#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100032#include "xfs_bmap_util.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_acl.h"
34#include "xfs_alloc.h"
35#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_rtalloc.h"
37#include "xfs_error.h"
38#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_attr.h"
Dave Chinner239880e2013-10-23 10:50:10 +110040#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_buf_item.h"
Christoph Hellwigc4ed4242011-07-08 14:34:23 +020042#include "xfs_inode_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Brian Foster27b52862012-11-06 09:50:38 -050044#include "xfs_icache.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100045#include "xfs_symlink.h"
Dave Chinner0cb97762013-08-12 20:50:09 +100046#include "xfs_da_btree.h"
Dave Chinner0cb97762013-08-12 20:50:09 +100047#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080049#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/xattr.h>
51#include <linux/namei.h>
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020052#include <linux/posix_acl.h>
Nathan Scott446ada42006-01-11 15:35:44 +110053#include <linux/security.h>
Eric Sandeenf35642e2008-11-28 14:23:35 +110054#include <linux/fiemap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090055#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Dave Chinner8d2a5e62012-03-07 04:50:19 +000057static int
58xfs_initxattrs(
59 struct inode *inode,
60 const struct xattr *xattr_array,
61 void *fs_info)
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040062{
Dave Chinner8d2a5e62012-03-07 04:50:19 +000063 const struct xattr *xattr;
64 struct xfs_inode *ip = XFS_I(inode);
65 int error = 0;
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040066
67 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
68 error = xfs_attr_set(ip, xattr->name, xattr->value,
69 xattr->value_len, ATTR_SECURE);
70 if (error < 0)
71 break;
72 }
73 return error;
74}
75
David Chinner5d51eff2007-11-23 16:29:18 +110076/*
Nathan Scott446ada42006-01-11 15:35:44 +110077 * Hook in SELinux. This is not quite correct yet, what we really need
78 * here (as we do for default ACLs) is a mechanism by which creation of
79 * these attrs can be journalled at inode creation time (along with the
80 * inode, of course, such that log replay can't cause these to be lost).
81 */
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040082
Nathan Scott446ada42006-01-11 15:35:44 +110083STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +110084xfs_init_security(
Christoph Hellwigaf048192008-03-06 13:46:43 +110085 struct inode *inode,
Eric Paris2a7dba32011-02-01 11:05:39 -050086 struct inode *dir,
87 const struct qstr *qstr)
Nathan Scott446ada42006-01-11 15:35:44 +110088{
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040089 return security_inode_init_security(inode, dir, qstr,
90 &xfs_initxattrs, NULL);
Nathan Scott446ada42006-01-11 15:35:44 +110091}
92
Barry Naujok556b8b12008-04-10 12:22:07 +100093static void
94xfs_dentry_to_name(
95 struct xfs_name *namep,
Dave Chinner0cb97762013-08-12 20:50:09 +100096 struct dentry *dentry,
97 int mode)
Barry Naujok556b8b12008-04-10 12:22:07 +100098{
99 namep->name = dentry->d_name.name;
100 namep->len = dentry->d_name.len;
Dave Chinner0cb97762013-08-12 20:50:09 +1000101 namep->type = xfs_mode_to_ftype[(mode & S_IFMT) >> S_SHIFT];
Barry Naujok556b8b12008-04-10 12:22:07 +1000102}
103
David Chinner7989cb82007-02-10 18:34:56 +1100104STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100105xfs_cleanup_inode(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000106 struct inode *dir,
Christoph Hellwigaf048192008-03-06 13:46:43 +1100107 struct inode *inode,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000108 struct dentry *dentry)
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100109{
Barry Naujok556b8b12008-04-10 12:22:07 +1000110 struct xfs_name teardown;
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100111
112 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100113 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100114 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100115 * ENOSPC can hit here, among other things.
116 */
Dave Chinner0cb97762013-08-12 20:50:09 +1000117 xfs_dentry_to_name(&teardown, dentry, 0);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100118
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000119 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
Christoph Hellwigaf048192008-03-06 13:46:43 +1100120 iput(inode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100124xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 struct inode *dir,
126 struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400127 umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 dev_t rdev)
129{
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100130 struct inode *inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100131 struct xfs_inode *ip = NULL;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200132 struct posix_acl *default_acl = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000133 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int error;
135
136 /*
137 * Irix uses Missed'em'V split, but doesn't want to see
138 * the upper 5 bits of (14bit) major.
139 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100140 if (S_ISCHR(mode) || S_ISBLK(mode)) {
141 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
142 return -EINVAL;
143 rdev = sysv_encode_dev(rdev);
144 } else {
145 rdev = 0;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200148 if (IS_POSIXACL(dir)) {
149 default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
150 if (IS_ERR(default_acl))
Al Viroc46a1312011-06-05 11:12:31 +0000151 return PTR_ERR(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Christoph Hellwige83f1eb2009-06-12 11:19:11 -0400153 if (!default_acl)
154 mode &= ~current_umask();
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Dave Chinner0cb97762013-08-12 20:50:09 +1000157 xfs_dentry_to_name(&name, dentry, mode);
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000158 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100159 if (unlikely(error))
160 goto out_free_acl;
Nathan Scott446ada42006-01-11 15:35:44 +1100161
David Chinner01651642008-08-13 15:45:15 +1000162 inode = VFS_I(ip);
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100163
Eric Paris2a7dba32011-02-01 11:05:39 -0500164 error = xfs_init_security(inode, dir, &dentry->d_name);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100165 if (unlikely(error))
166 goto out_cleanup_inode;
167
168 if (default_acl) {
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200169 error = -xfs_inherit_acl(inode, default_acl);
Al Viro826cae22011-07-23 03:10:32 -0400170 default_acl = NULL;
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100171 if (unlikely(error))
172 goto out_cleanup_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100176 d_instantiate(dentry, inode);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100177 return -error;
178
179 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000180 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100181 out_free_acl:
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200182 posix_acl_release(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return -error;
184}
185
186STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100187xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct inode *dir,
189 struct dentry *dentry,
Al Viro4acdaf22011-07-26 01:42:34 -0400190 umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400191 bool flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Nathan Scott416c6d52006-03-14 14:00:51 +1100193 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100197xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct inode *dir,
199 struct dentry *dentry,
Al Viro18bb1db2011-07-26 01:41:39 -0400200 umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Nathan Scott416c6d52006-03-14 14:00:51 +1100202 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100206xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct inode *dir,
208 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400209 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100211 struct xfs_inode *cip;
Barry Naujok556b8b12008-04-10 12:22:07 +1000212 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 int error;
214
215 if (dentry->d_name.len >= MAXNAMELEN)
216 return ERR_PTR(-ENAMETOOLONG);
217
Dave Chinner0cb97762013-08-12 20:50:09 +1000218 xfs_dentry_to_name(&name, dentry, 0);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000219 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000220 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (unlikely(error != ENOENT))
222 return ERR_PTR(-error);
223 d_add(dentry, NULL);
224 return NULL;
225 }
226
David Chinner01651642008-08-13 15:45:15 +1000227 return d_splice_alias(VFS_I(cip), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Barry Naujok384f3ce2008-05-21 16:58:22 +1000230STATIC struct dentry *
231xfs_vn_ci_lookup(
232 struct inode *dir,
233 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400234 unsigned int flags)
Barry Naujok384f3ce2008-05-21 16:58:22 +1000235{
236 struct xfs_inode *ip;
237 struct xfs_name xname;
238 struct xfs_name ci_name;
239 struct qstr dname;
240 int error;
241
242 if (dentry->d_name.len >= MAXNAMELEN)
243 return ERR_PTR(-ENAMETOOLONG);
244
Dave Chinner0cb97762013-08-12 20:50:09 +1000245 xfs_dentry_to_name(&xname, dentry, 0);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000246 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
247 if (unlikely(error)) {
248 if (unlikely(error != ENOENT))
249 return ERR_PTR(-error);
Barry Naujok866d5dc2008-05-22 17:21:40 +1000250 /*
251 * call d_add(dentry, NULL) here when d_drop_negative_children
252 * is called in xfs_vn_mknod (ie. allow negative dentries
253 * with CI filesystems).
254 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000255 return NULL;
256 }
257
258 /* if exact match, just splice and exit */
259 if (!ci_name.name)
David Chinner01651642008-08-13 15:45:15 +1000260 return d_splice_alias(VFS_I(ip), dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000261
262 /* else case-insensitive match... */
263 dname.name = ci_name.name;
264 dname.len = ci_name.len;
Christoph Hellwige45b5902008-08-07 23:49:07 +0200265 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000266 kmem_free(ci_name.name);
267 return dentry;
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100271xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct dentry *old_dentry,
273 struct inode *dir,
274 struct dentry *dentry)
275{
Christoph Hellwigd9424b32008-12-03 12:20:27 +0100276 struct inode *inode = old_dentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000277 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int error;
279
Dave Chinner0cb97762013-08-12 20:50:09 +1000280 xfs_dentry_to_name(&name, dentry, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Barry Naujok556b8b12008-04-10 12:22:07 +1000282 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
Christoph Hellwigd9424b32008-12-03 12:20:27 +0100283 if (unlikely(error))
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100284 return -error;
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100285
Al Viro7de9c6ee2010-10-23 11:11:40 -0400286 ihold(inode);
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100287 d_instantiate(dentry, inode);
288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100292xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct inode *dir,
294 struct dentry *dentry)
295{
Barry Naujok556b8b12008-04-10 12:22:07 +1000296 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int error;
298
Dave Chinner0cb97762013-08-12 20:50:09 +1000299 xfs_dentry_to_name(&name, dentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Christoph Hellwige5700702008-06-23 13:25:25 +1000301 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
302 if (error)
303 return error;
304
305 /*
306 * With unlink, the VFS makes the dentry "negative": no inode,
307 * but still hashed. This is incompatible with case-insensitive
308 * mode, so invalidate (unhash) the dentry in CI-mode.
309 */
310 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
311 d_invalidate(dentry);
312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100316xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct inode *dir,
318 struct dentry *dentry,
319 const char *symname)
320{
Christoph Hellwig3937be52008-03-06 13:46:19 +1100321 struct inode *inode;
322 struct xfs_inode *cip = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000323 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 int error;
Al Viro576b1d62011-07-26 02:50:15 -0400325 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000327 mode = S_IFLNK |
Al Viroce3b0f82009-03-29 19:08:22 -0400328 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
Dave Chinner0cb97762013-08-12 20:50:09 +1000329 xfs_dentry_to_name(&name, dentry, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000331 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100332 if (unlikely(error))
333 goto out;
334
David Chinner01651642008-08-13 15:45:15 +1000335 inode = VFS_I(cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100336
Eric Paris2a7dba32011-02-01 11:05:39 -0500337 error = xfs_init_security(inode, dir, &dentry->d_name);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100338 if (unlikely(error))
339 goto out_cleanup_inode;
340
341 d_instantiate(dentry, inode);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100342 return 0;
343
344 out_cleanup_inode:
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000345 xfs_cleanup_inode(dir, inode, dentry);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100346 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return -error;
348}
349
350STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100351xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct inode *odir,
353 struct dentry *odentry,
354 struct inode *ndir,
355 struct dentry *ndentry)
356{
357 struct inode *new_inode = ndentry->d_inode;
Barry Naujok556b8b12008-04-10 12:22:07 +1000358 struct xfs_name oname;
359 struct xfs_name nname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Dave Chinner0cb97762013-08-12 20:50:09 +1000361 xfs_dentry_to_name(&oname, odentry, 0);
362 xfs_dentry_to_name(&nname, ndentry, odentry->d_inode->i_mode);
Barry Naujok556b8b12008-04-10 12:22:07 +1000363
Christoph Hellwige5700702008-06-23 13:25:25 +1000364 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
Christoph Hellwigcfa853e2008-04-22 17:34:06 +1000365 XFS_I(ndir), &nname, new_inode ?
Dave Chinner0cb97762013-08-12 20:50:09 +1000366 XFS_I(new_inode) : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369/*
370 * careful here - this function can get called recursively, so
371 * we need to be very careful about how much stack we use.
372 * uio is kmalloced for this reason...
373 */
Al Viro008b1502005-08-20 00:17:39 +0100374STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100375xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct dentry *dentry,
377 struct nameidata *nd)
378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000380 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700382 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000383 if (!link)
384 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000386 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000387 if (unlikely(error))
388 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100391 return NULL;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000392
393 out_kfree:
394 kfree(link);
395 out_err:
396 nd_set_link(nd, ERR_PTR(error));
397 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Nathan Scottcde410a2005-09-05 11:47:01 +1000400STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100401xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000402 struct dentry *dentry,
403 struct nameidata *nd,
404 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Nathan Scottcde410a2005-09-05 11:47:01 +1000406 char *s = nd_get_link(nd);
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (!IS_ERR(s))
409 kfree(s);
410}
411
Christoph Hellwig45767582008-02-05 12:13:24 +1100412STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100413xfs_vn_getattr(
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000414 struct vfsmount *mnt,
415 struct dentry *dentry,
416 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000418 struct inode *inode = dentry->d_inode;
419 struct xfs_inode *ip = XFS_I(inode);
420 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000422 trace_xfs_getattr(ip);
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000423
424 if (XFS_FORCED_SHUTDOWN(mp))
Mitsuo Hayasakaed322012011-09-17 13:38:38 +0000425 return -XFS_ERROR(EIO);
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000426
427 stat->size = XFS_ISIZE(ip);
428 stat->dev = inode->i_sb->s_dev;
429 stat->mode = ip->i_d.di_mode;
430 stat->nlink = ip->i_d.di_nlink;
Dwight Engen7aab1b22013-08-15 14:08:01 -0400431 stat->uid = inode->i_uid;
432 stat->gid = inode->i_gid;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000433 stat->ino = ip->i_ino;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000434 stat->atime = inode->i_atime;
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000435 stat->mtime = inode->i_mtime;
436 stat->ctime = inode->i_ctime;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000437 stat->blocks =
438 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
439
440
441 switch (inode->i_mode & S_IFMT) {
442 case S_IFBLK:
443 case S_IFCHR:
444 stat->blksize = BLKDEV_IOSIZE;
445 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
446 sysv_minor(ip->i_df.if_u2.if_rdev));
447 break;
448 default:
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100449 if (XFS_IS_REALTIME_INODE(ip)) {
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000450 /*
451 * If the file blocks are being allocated from a
452 * realtime volume, then return the inode's realtime
453 * extent size or the realtime volume's extent size.
454 */
455 stat->blksize =
456 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
457 } else
458 stat->blksize = xfs_preferred_iosize(mp);
459 stat->rdev = 0;
460 break;
Nathan Scott69e23b92006-09-28 11:01:22 +1000461 }
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000462
463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Dave Chinner56c19e82013-05-27 16:38:25 +1000466static void
467xfs_setattr_mode(
468 struct xfs_trans *tp,
469 struct xfs_inode *ip,
470 struct iattr *iattr)
471{
472 struct inode *inode = VFS_I(ip);
473 umode_t mode = iattr->ia_mode;
474
475 ASSERT(tp);
476 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
477
Dave Chinner56c19e82013-05-27 16:38:25 +1000478 ip->i_d.di_mode &= S_IFMT;
479 ip->i_d.di_mode |= mode & ~S_IFMT;
480
481 inode->i_mode &= S_IFMT;
482 inode->i_mode |= mode & ~S_IFMT;
483}
484
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200485int
486xfs_setattr_nonsize(
487 struct xfs_inode *ip,
488 struct iattr *iattr,
489 int flags)
490{
491 xfs_mount_t *mp = ip->i_mount;
492 struct inode *inode = VFS_I(ip);
493 int mask = iattr->ia_valid;
494 xfs_trans_t *tp;
495 int error;
Dwight Engen7aab1b22013-08-15 14:08:01 -0400496 kuid_t uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
497 kgid_t gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200498 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
499 struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
500
501 trace_xfs_setattr(ip);
502
Carlos Maiolino42c49d72013-06-21 14:45:53 -0300503 /* If acls are being inherited, we already have this checked */
504 if (!(flags & XFS_ATTR_NOACL)) {
505 if (mp->m_flags & XFS_MOUNT_RDONLY)
506 return XFS_ERROR(EROFS);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200507
Carlos Maiolino42c49d72013-06-21 14:45:53 -0300508 if (XFS_FORCED_SHUTDOWN(mp))
509 return XFS_ERROR(EIO);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200510
Carlos Maiolino42c49d72013-06-21 14:45:53 -0300511 error = -inode_change_ok(inode, iattr);
512 if (error)
513 return XFS_ERROR(error);
514 }
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200515
516 ASSERT((mask & ATTR_SIZE) == 0);
517
518 /*
519 * If disk quotas is on, we make sure that the dquots do exist on disk,
520 * before we start any other transactions. Trying to do this later
521 * is messy. We don't care to take a readlock to look at the ids
522 * in inode here, because we can't hold it across the trans_reserve.
523 * If the IDs do change before we take the ilock, we're covered
524 * because the i_*dquot fields will get updated anyway.
525 */
526 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
527 uint qflags = 0;
528
529 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
530 uid = iattr->ia_uid;
531 qflags |= XFS_QMOPT_UQUOTA;
532 } else {
Dwight Engen7aab1b22013-08-15 14:08:01 -0400533 uid = inode->i_uid;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200534 }
535 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
536 gid = iattr->ia_gid;
537 qflags |= XFS_QMOPT_GQUOTA;
538 } else {
Dwight Engen7aab1b22013-08-15 14:08:01 -0400539 gid = inode->i_gid;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200540 }
541
542 /*
543 * We take a reference when we initialize udqp and gdqp,
544 * so it is important that we never blindly double trip on
545 * the same variable. See xfs_create() for an example.
546 */
547 ASSERT(udqp == NULL);
548 ASSERT(gdqp == NULL);
Dwight Engen7aab1b22013-08-15 14:08:01 -0400549 error = xfs_qm_vop_dqalloc(ip, xfs_kuid_to_uid(uid),
550 xfs_kgid_to_gid(gid),
551 xfs_get_projid(ip),
552 qflags, &udqp, &gdqp, NULL);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200553 if (error)
554 return error;
555 }
556
557 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
Jie Liu3d3c8b52013-08-12 20:49:59 +1000558 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200559 if (error)
560 goto out_dqrele;
561
562 xfs_ilock(ip, XFS_ILOCK_EXCL);
563
564 /*
565 * Change file ownership. Must be the owner or privileged.
566 */
567 if (mask & (ATTR_UID|ATTR_GID)) {
568 /*
569 * These IDs could have changed since we last looked at them.
570 * But, we're assured that if the ownership did change
571 * while we didn't have the inode locked, inode's dquot(s)
572 * would have changed also.
573 */
Dwight Engen7aab1b22013-08-15 14:08:01 -0400574 iuid = inode->i_uid;
575 igid = inode->i_gid;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200576 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
577 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
578
579 /*
580 * Do a quota reservation only if uid/gid is actually
581 * going to change.
582 */
583 if (XFS_IS_QUOTA_RUNNING(mp) &&
Dwight Engen7aab1b22013-08-15 14:08:01 -0400584 ((XFS_IS_UQUOTA_ON(mp) && !uid_eq(iuid, uid)) ||
585 (XFS_IS_GQUOTA_ON(mp) && !gid_eq(igid, gid)))) {
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200586 ASSERT(tp);
587 error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500588 NULL, capable(CAP_FOWNER) ?
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200589 XFS_QMOPT_FORCE_RES : 0);
590 if (error) /* out of quota */
591 goto out_trans_cancel;
592 }
593 }
594
Christoph Hellwigddc34152011-09-19 15:00:54 +0000595 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200596
597 /*
598 * Change file ownership. Must be the owner or privileged.
599 */
600 if (mask & (ATTR_UID|ATTR_GID)) {
601 /*
602 * CAP_FSETID overrides the following restrictions:
603 *
604 * The set-user-ID and set-group-ID bits of a file will be
605 * cleared upon successful return from chown()
606 */
607 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
608 !capable(CAP_FSETID))
609 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
610
611 /*
612 * Change the ownerships and register quota modifications
613 * in the transaction.
614 */
Dwight Engen7aab1b22013-08-15 14:08:01 -0400615 if (!uid_eq(iuid, uid)) {
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200616 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
617 ASSERT(mask & ATTR_UID);
618 ASSERT(udqp);
619 olddquot1 = xfs_qm_vop_chown(tp, ip,
620 &ip->i_udquot, udqp);
621 }
Dwight Engen7aab1b22013-08-15 14:08:01 -0400622 ip->i_d.di_uid = xfs_kuid_to_uid(uid);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200623 inode->i_uid = uid;
624 }
Dwight Engen7aab1b22013-08-15 14:08:01 -0400625 if (!gid_eq(igid, gid)) {
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200626 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
627 ASSERT(!XFS_IS_PQUOTA_ON(mp));
628 ASSERT(mask & ATTR_GID);
629 ASSERT(gdqp);
630 olddquot2 = xfs_qm_vop_chown(tp, ip,
631 &ip->i_gdquot, gdqp);
632 }
Dwight Engen7aab1b22013-08-15 14:08:01 -0400633 ip->i_d.di_gid = xfs_kgid_to_gid(gid);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200634 inode->i_gid = gid;
635 }
636 }
637
638 /*
639 * Change file access modes.
640 */
Dave Chinner56c19e82013-05-27 16:38:25 +1000641 if (mask & ATTR_MODE)
642 xfs_setattr_mode(tp, ip, iattr);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200643
644 /*
645 * Change file access or modified times.
646 */
647 if (mask & ATTR_ATIME) {
648 inode->i_atime = iattr->ia_atime;
649 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
650 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200651 }
652 if (mask & ATTR_CTIME) {
653 inode->i_ctime = iattr->ia_ctime;
654 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
655 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200656 }
657 if (mask & ATTR_MTIME) {
658 inode->i_mtime = iattr->ia_mtime;
659 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
660 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200661 }
662
663 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
664
665 XFS_STATS_INC(xs_ig_attrchg);
666
667 if (mp->m_flags & XFS_MOUNT_WSYNC)
668 xfs_trans_set_sync(tp);
669 error = xfs_trans_commit(tp, 0);
670
671 xfs_iunlock(ip, XFS_ILOCK_EXCL);
672
673 /*
674 * Release any dquot(s) the inode had kept before chown.
675 */
676 xfs_qm_dqrele(olddquot1);
677 xfs_qm_dqrele(olddquot2);
678 xfs_qm_dqrele(udqp);
679 xfs_qm_dqrele(gdqp);
680
681 if (error)
682 return XFS_ERROR(error);
683
684 /*
685 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
686 * update. We could avoid this with linked transactions
687 * and passing down the transaction pointer all the way
688 * to attr_set. No previous user of the generic
689 * Posix ACL code seems to care about this issue either.
690 */
691 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
692 error = -xfs_acl_chmod(inode);
693 if (error)
694 return XFS_ERROR(error);
695 }
696
697 return 0;
698
699out_trans_cancel:
700 xfs_trans_cancel(tp, 0);
701 xfs_iunlock(ip, XFS_ILOCK_EXCL);
702out_dqrele:
703 xfs_qm_dqrele(udqp);
704 xfs_qm_dqrele(gdqp);
705 return error;
706}
707
708/*
709 * Truncate file. Must have write permission and not be a directory.
710 */
711int
712xfs_setattr_size(
713 struct xfs_inode *ip,
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700714 struct iattr *iattr)
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200715{
716 struct xfs_mount *mp = ip->i_mount;
717 struct inode *inode = VFS_I(ip);
718 int mask = iattr->ia_valid;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000719 xfs_off_t oldsize, newsize;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200720 struct xfs_trans *tp;
721 int error;
Christoph Hellwigf38996f2012-03-27 10:34:48 -0400722 uint lock_flags = 0;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200723 uint commit_flags = 0;
724
725 trace_xfs_setattr(ip);
726
727 if (mp->m_flags & XFS_MOUNT_RDONLY)
728 return XFS_ERROR(EROFS);
729
730 if (XFS_FORCED_SHUTDOWN(mp))
731 return XFS_ERROR(EIO);
732
733 error = -inode_change_ok(inode, iattr);
734 if (error)
735 return XFS_ERROR(error);
736
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700737 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200738 ASSERT(S_ISREG(ip->i_d.di_mode));
Dave Chinner56c19e82013-05-27 16:38:25 +1000739 ASSERT((mask & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
740 ATTR_MTIME_SET|ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200741
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000742 oldsize = inode->i_size;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000743 newsize = iattr->ia_size;
744
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200745 /*
746 * Short circuit the truncate case for zero length files.
747 */
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000748 if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
Christoph Hellwig681b1202011-07-08 14:34:26 +0200749 if (!(mask & (ATTR_CTIME|ATTR_MTIME)))
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700750 return 0;
Christoph Hellwig681b1202011-07-08 14:34:26 +0200751
752 /*
753 * Use the regular setattr path to update the timestamps.
754 */
Christoph Hellwig681b1202011-07-08 14:34:26 +0200755 iattr->ia_valid &= ~ATTR_SIZE;
756 return xfs_setattr_nonsize(ip, iattr, 0);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200757 }
758
759 /*
760 * Make sure that the dquots are attached to the inode.
761 */
Christoph Hellwigf38996f2012-03-27 10:34:48 -0400762 error = xfs_qm_dqattach(ip, 0);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200763 if (error)
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700764 return error;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200765
766 /*
767 * Now we can make the changes. Before we join the inode to the
768 * transaction, take care of the part of the truncation that must be
769 * done without the inode lock. This needs to be done before joining
770 * the inode to the transaction, because the inode cannot be unlocked
771 * once it is a part of the transaction.
772 */
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000773 if (newsize > oldsize) {
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200774 /*
775 * Do the first part of growing a file: zero any data in the
776 * last block that is beyond the old EOF. We need to do this
777 * before the inode is joined to the transaction to modify
778 * i_size.
779 */
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000780 error = xfs_zero_eof(ip, newsize, oldsize);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200781 if (error)
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700782 return error;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200783 }
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200784
785 /*
786 * We are going to log the inode size change in this transaction so
787 * any previous writes that are beyond the on disk EOF and the new
788 * EOF that have not been written out need to be written here. If we
789 * do not write the data out, we expose ourselves to the null files
790 * problem.
791 *
792 * Only flush from the on disk size to the smaller of the in memory
793 * file size or the new size as that's the range we really care about
794 * here and prevents waiting for other data not within the range we
795 * care about here.
796 */
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000797 if (oldsize != ip->i_d.di_size && newsize > ip->i_d.di_size) {
Dave Chinner4bc1ea62012-11-12 22:53:56 +1100798 error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
799 ip->i_d.di_size, newsize);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200800 if (error)
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700801 return error;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200802 }
803
804 /*
Christoph Hellwig4a06fd22011-08-23 08:28:13 +0000805 * Wait for all direct I/O to complete.
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200806 */
Christoph Hellwig4a06fd22011-08-23 08:28:13 +0000807 inode_dio_wait(inode);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200808
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000809 error = -block_truncate_page(inode->i_mapping, newsize, xfs_get_blocks);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200810 if (error)
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700811 return error;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200812
813 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
Jie Liu3d3c8b52013-08-12 20:49:59 +1000814 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200815 if (error)
816 goto out_trans_cancel;
817
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000818 truncate_setsize(inode, newsize);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200819
820 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
821 lock_flags |= XFS_ILOCK_EXCL;
822
823 xfs_ilock(ip, XFS_ILOCK_EXCL);
824
Christoph Hellwigddc34152011-09-19 15:00:54 +0000825 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200826
827 /*
828 * Only change the c/mtime if we are changing the size or we are
829 * explicitly asked to change it. This handles the semantic difference
830 * between truncate() and ftruncate() as implemented in the VFS.
831 *
832 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
833 * special case where we need to update the times despite not having
834 * these flags set. For all other operations the VFS set these flags
835 * explicitly if it wants a timestamp update.
836 */
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000837 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) {
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200838 iattr->ia_ctime = iattr->ia_mtime =
839 current_fs_time(inode->i_sb);
840 mask |= ATTR_CTIME | ATTR_MTIME;
841 }
842
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000843 /*
844 * The first thing we do is set the size to new_size permanently on
845 * disk. This way we don't have to worry about anyone ever being able
846 * to look at the data being freed even in the face of a crash.
847 * What we're getting around here is the case where we free a block, it
848 * is allocated to another file, it is written to, and then we crash.
849 * If the new data gets written to the file but the log buffers
850 * containing the free and reallocation don't, then we'd end up with
851 * garbage in the blocks being freed. As long as we make the new size
852 * permanent before actually freeing any blocks it doesn't matter if
853 * they get written to.
854 */
855 ip->i_d.di_size = newsize;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000856 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
857
858 if (newsize <= oldsize) {
859 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200860 if (error)
861 goto out_trans_abort;
862
863 /*
864 * Truncated "down", so we're removing references to old data
865 * here - if we delay flushing for a long time, we expose
866 * ourselves unduly to the notorious NULL files problem. So,
867 * we mark this inode and flush it when the file is closed,
868 * and do not wait the usual (long) time for writeout.
869 */
870 xfs_iflags_set(ip, XFS_ITRUNCATED);
Brian Foster27b52862012-11-06 09:50:38 -0500871
872 /* A truncate down always removes post-EOF blocks. */
873 xfs_inode_clear_eofblocks_tag(ip);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200874 }
875
Dave Chinner56c19e82013-05-27 16:38:25 +1000876 /*
877 * Change file access modes.
878 */
879 if (mask & ATTR_MODE)
880 xfs_setattr_mode(tp, ip, iattr);
881
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200882 if (mask & ATTR_CTIME) {
883 inode->i_ctime = iattr->ia_ctime;
884 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
885 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200886 }
887 if (mask & ATTR_MTIME) {
888 inode->i_mtime = iattr->ia_mtime;
889 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
890 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200891 }
892
893 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
894
895 XFS_STATS_INC(xs_ig_attrchg);
896
897 if (mp->m_flags & XFS_MOUNT_WSYNC)
898 xfs_trans_set_sync(tp);
899
900 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
901out_unlock:
902 if (lock_flags)
903 xfs_iunlock(ip, lock_flags);
904 return error;
905
906out_trans_abort:
907 commit_flags |= XFS_TRANS_ABORT;
908out_trans_cancel:
909 xfs_trans_cancel(tp, commit_flags);
910 goto out_unlock;
911}
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100914xfs_vn_setattr(
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700915 struct dentry *dentry,
916 struct iattr *iattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700918 struct xfs_inode *ip = XFS_I(dentry->d_inode);
919 int error;
920
921 if (iattr->ia_valid & ATTR_SIZE) {
922 xfs_ilock(ip, XFS_IOLOCK_EXCL);
923 error = xfs_setattr_size(ip, iattr);
924 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
925 } else {
926 error = xfs_setattr_nonsize(ip, iattr, 0);
927 }
928
929 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Christoph Hellwig69ff2822012-06-06 17:01:28 -0400932STATIC int
933xfs_vn_update_time(
934 struct inode *inode,
935 struct timespec *now,
936 int flags)
937{
938 struct xfs_inode *ip = XFS_I(inode);
939 struct xfs_mount *mp = ip->i_mount;
940 struct xfs_trans *tp;
941 int error;
942
943 trace_xfs_update_time(ip);
944
945 tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
Jie Liu3d3c8b52013-08-12 20:49:59 +1000946 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_fsyncts, 0, 0);
Christoph Hellwig69ff2822012-06-06 17:01:28 -0400947 if (error) {
948 xfs_trans_cancel(tp, 0);
949 return -error;
950 }
951
952 xfs_ilock(ip, XFS_ILOCK_EXCL);
953 if (flags & S_CTIME) {
954 inode->i_ctime = *now;
955 ip->i_d.di_ctime.t_sec = (__int32_t)now->tv_sec;
956 ip->i_d.di_ctime.t_nsec = (__int32_t)now->tv_nsec;
957 }
958 if (flags & S_MTIME) {
959 inode->i_mtime = *now;
960 ip->i_d.di_mtime.t_sec = (__int32_t)now->tv_sec;
961 ip->i_d.di_mtime.t_nsec = (__int32_t)now->tv_nsec;
962 }
963 if (flags & S_ATIME) {
964 inode->i_atime = *now;
965 ip->i_d.di_atime.t_sec = (__int32_t)now->tv_sec;
966 ip->i_d.di_atime.t_nsec = (__int32_t)now->tv_nsec;
967 }
968 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
969 xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
970 return -xfs_trans_commit(tp, 0);
971}
972
Eric Sandeenf35642e2008-11-28 14:23:35 +1100973#define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
974
975/*
976 * Call fiemap helper to fill in user data.
977 * Returns positive errors to xfs_getbmap.
978 */
979STATIC int
980xfs_fiemap_format(
981 void **arg,
982 struct getbmapx *bmv,
983 int *full)
984{
985 int error;
986 struct fiemap_extent_info *fieinfo = *arg;
987 u32 fiemap_flags = 0;
988 u64 logical, physical, length;
989
990 /* Do nothing for a hole */
991 if (bmv->bmv_block == -1LL)
992 return 0;
993
994 logical = BBTOB(bmv->bmv_offset);
995 physical = BBTOB(bmv->bmv_block);
996 length = BBTOB(bmv->bmv_length);
997
998 if (bmv->bmv_oflags & BMV_OF_PREALLOC)
999 fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
1000 else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
Jie Liu635c4d02013-06-06 06:24:01 -07001001 fiemap_flags |= (FIEMAP_EXTENT_DELALLOC |
1002 FIEMAP_EXTENT_UNKNOWN);
Eric Sandeenf35642e2008-11-28 14:23:35 +11001003 physical = 0; /* no block yet */
1004 }
1005 if (bmv->bmv_oflags & BMV_OF_LAST)
1006 fiemap_flags |= FIEMAP_EXTENT_LAST;
1007
1008 error = fiemap_fill_next_extent(fieinfo, logical, physical,
1009 length, fiemap_flags);
1010 if (error > 0) {
1011 error = 0;
1012 *full = 1; /* user array now full */
1013 }
1014
1015 return -error;
1016}
1017
1018STATIC int
1019xfs_vn_fiemap(
1020 struct inode *inode,
1021 struct fiemap_extent_info *fieinfo,
1022 u64 start,
1023 u64 length)
1024{
1025 xfs_inode_t *ip = XFS_I(inode);
1026 struct getbmapx bm;
1027 int error;
1028
1029 error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
1030 if (error)
1031 return error;
1032
1033 /* Set up bmap header for xfs internal routine */
1034 bm.bmv_offset = BTOBB(start);
1035 /* Special case for whole file */
1036 if (length == FIEMAP_MAX_OFFSET)
1037 bm.bmv_length = -1LL;
1038 else
1039 bm.bmv_length = BTOBB(length);
1040
Eric Sandeen97db39a2009-07-26 21:52:01 -05001041 /* We add one because in getbmap world count includes the header */
Tao Ma2d1ff3c2010-04-29 15:13:56 +10001042 bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
1043 fieinfo->fi_extents_max + 1;
1044 bm.bmv_count = min_t(__s32, bm.bmv_count,
1045 (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
Tao Ma9af25462010-08-30 02:44:03 +00001046 bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
Eric Sandeenf35642e2008-11-28 14:23:35 +11001047 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
1048 bm.bmv_iflags |= BMV_IF_ATTRFORK;
1049 if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
1050 bm.bmv_iflags |= BMV_IF_DELALLOC;
1051
1052 error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
1053 if (error)
1054 return -error;
1055
1056 return 0;
1057}
1058
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001059static const struct inode_operations xfs_inode_operations = {
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001060 .get_acl = xfs_get_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +11001061 .getattr = xfs_vn_getattr,
1062 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +10001063 .setxattr = generic_setxattr,
1064 .getxattr = generic_getxattr,
1065 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +11001066 .listxattr = xfs_vn_listxattr,
Eric Sandeenf35642e2008-11-28 14:23:35 +11001067 .fiemap = xfs_vn_fiemap,
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001068 .update_time = xfs_vn_update_time,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069};
1070
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001071static const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +11001072 .create = xfs_vn_create,
1073 .lookup = xfs_vn_lookup,
1074 .link = xfs_vn_link,
1075 .unlink = xfs_vn_unlink,
1076 .symlink = xfs_vn_symlink,
1077 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001078 /*
1079 * Yes, XFS uses the same method for rmdir and unlink.
1080 *
1081 * There are some subtile differences deeper in the code,
1082 * but we use S_ISDIR to check for those.
1083 */
1084 .rmdir = xfs_vn_unlink,
Nathan Scott416c6d52006-03-14 14:00:51 +11001085 .mknod = xfs_vn_mknod,
1086 .rename = xfs_vn_rename,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001087 .get_acl = xfs_get_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +11001088 .getattr = xfs_vn_getattr,
1089 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +10001090 .setxattr = generic_setxattr,
1091 .getxattr = generic_getxattr,
1092 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +11001093 .listxattr = xfs_vn_listxattr,
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001094 .update_time = xfs_vn_update_time,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095};
1096
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001097static const struct inode_operations xfs_dir_ci_inode_operations = {
Barry Naujok384f3ce2008-05-21 16:58:22 +10001098 .create = xfs_vn_create,
1099 .lookup = xfs_vn_ci_lookup,
1100 .link = xfs_vn_link,
1101 .unlink = xfs_vn_unlink,
1102 .symlink = xfs_vn_symlink,
1103 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001104 /*
1105 * Yes, XFS uses the same method for rmdir and unlink.
1106 *
1107 * There are some subtile differences deeper in the code,
1108 * but we use S_ISDIR to check for those.
1109 */
1110 .rmdir = xfs_vn_unlink,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001111 .mknod = xfs_vn_mknod,
1112 .rename = xfs_vn_rename,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001113 .get_acl = xfs_get_acl,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001114 .getattr = xfs_vn_getattr,
1115 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +10001116 .setxattr = generic_setxattr,
1117 .getxattr = generic_getxattr,
1118 .removexattr = generic_removexattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001119 .listxattr = xfs_vn_listxattr,
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001120 .update_time = xfs_vn_update_time,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001121};
1122
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001123static const struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +11001125 .follow_link = xfs_vn_follow_link,
1126 .put_link = xfs_vn_put_link,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001127 .get_acl = xfs_get_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +11001128 .getattr = xfs_vn_getattr,
1129 .setattr = xfs_vn_setattr,
Lachlan McIlroy0ec58512008-06-23 13:23:01 +10001130 .setxattr = generic_setxattr,
1131 .getxattr = generic_getxattr,
1132 .removexattr = generic_removexattr,
Nathan Scott416c6d52006-03-14 14:00:51 +11001133 .listxattr = xfs_vn_listxattr,
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001134 .update_time = xfs_vn_update_time,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135};
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001136
1137STATIC void
1138xfs_diflags_to_iflags(
1139 struct inode *inode,
1140 struct xfs_inode *ip)
1141{
1142 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
1143 inode->i_flags |= S_IMMUTABLE;
1144 else
1145 inode->i_flags &= ~S_IMMUTABLE;
1146 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
1147 inode->i_flags |= S_APPEND;
1148 else
1149 inode->i_flags &= ~S_APPEND;
1150 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
1151 inode->i_flags |= S_SYNC;
1152 else
1153 inode->i_flags &= ~S_SYNC;
1154 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
1155 inode->i_flags |= S_NOATIME;
1156 else
1157 inode->i_flags &= ~S_NOATIME;
1158}
1159
1160/*
1161 * Initialize the Linux inode, set up the operation vectors and
1162 * unlock the inode.
1163 *
1164 * When reading existing inodes from disk this is called directly
1165 * from xfs_iget, when creating a new inode it is called from
1166 * xfs_ialloc after setting up the inode.
David Chinnerbf904242008-10-30 17:36:14 +11001167 *
1168 * We are always called with an uninitialised linux inode here.
1169 * We need to initialise the necessary fields and take a reference
1170 * on it.
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001171 */
1172void
1173xfs_setup_inode(
1174 struct xfs_inode *ip)
1175{
David Chinnerbf904242008-10-30 17:36:14 +11001176 struct inode *inode = &ip->i_vnode;
1177
1178 inode->i_ino = ip->i_ino;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001179 inode->i_state = I_NEW;
Christoph Hellwig646ec462010-10-23 07:15:32 -04001180
1181 inode_sb_list_add(inode);
Christoph Hellwigc6f6cd02010-11-06 11:43:08 +00001182 /* make the inode look hashed for the writeback code */
1183 hlist_add_fake(&inode->i_hash);
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001184
1185 inode->i_mode = ip->i_d.di_mode;
Miklos Szeredibfe86842011-10-28 14:13:29 +02001186 set_nlink(inode, ip->i_d.di_nlink);
Dwight Engen7aab1b22013-08-15 14:08:01 -04001187 inode->i_uid = xfs_uid_to_kuid(ip->i_d.di_uid);
1188 inode->i_gid = xfs_gid_to_kgid(ip->i_d.di_gid);
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001189
1190 switch (inode->i_mode & S_IFMT) {
1191 case S_IFBLK:
1192 case S_IFCHR:
1193 inode->i_rdev =
1194 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
1195 sysv_minor(ip->i_df.if_u2.if_rdev));
1196 break;
1197 default:
1198 inode->i_rdev = 0;
1199 break;
1200 }
1201
1202 inode->i_generation = ip->i_d.di_gen;
1203 i_size_write(inode, ip->i_d.di_size);
1204 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
1205 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
1206 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
1207 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
1208 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
1209 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
1210 xfs_diflags_to_iflags(inode, ip);
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001211
1212 switch (inode->i_mode & S_IFMT) {
1213 case S_IFREG:
1214 inode->i_op = &xfs_inode_operations;
1215 inode->i_fop = &xfs_file_operations;
1216 inode->i_mapping->a_ops = &xfs_address_space_operations;
1217 break;
1218 case S_IFDIR:
1219 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
1220 inode->i_op = &xfs_dir_ci_inode_operations;
1221 else
1222 inode->i_op = &xfs_dir_inode_operations;
1223 inode->i_fop = &xfs_dir_file_operations;
1224 break;
1225 case S_IFLNK:
1226 inode->i_op = &xfs_symlink_inode_operations;
1227 if (!(ip->i_df.if_flags & XFS_IFINLINE))
1228 inode->i_mapping->a_ops = &xfs_address_space_operations;
1229 break;
1230 default:
1231 inode->i_op = &xfs_inode_operations;
1232 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1233 break;
1234 }
1235
Christoph Hellwig510792e2011-07-26 15:07:29 +00001236 /*
1237 * If there is no attribute fork no ACL can exist on this inode,
1238 * and it can't have any file capabilities attached to it either.
1239 */
1240 if (!XFS_IFORK_Q(ip)) {
1241 inode_has_no_xattr(inode);
Christoph Hellwig6311b102011-07-23 17:36:50 +02001242 cache_no_acl(inode);
Christoph Hellwig510792e2011-07-26 15:07:29 +00001243 }
Christoph Hellwig6311b102011-07-23 17:36:50 +02001244
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001245 xfs_iflags_clear(ip, XFS_INEW);
1246 barrier();
1247
1248 unlock_new_inode(inode);
1249}