]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - fs/xfs/xfs_iops.c
xfs: tiny xfs_setattr_mode cleanup
[mirror_ubuntu-focal-kernel.git] / fs / xfs / xfs_iops.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
1da177e4
LT
18#include "xfs.h"
19#include "xfs_fs.h"
70a9883c 20#include "xfs_shared.h"
239880ef
DC
21#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
1da177e4
LT
24#include "xfs_sb.h"
25#include "xfs_ag.h"
1da177e4 26#include "xfs_mount.h"
57062787 27#include "xfs_da_format.h"
1da177e4
LT
28#include "xfs_inode.h"
29#include "xfs_bmap.h"
68988114 30#include "xfs_bmap_util.h"
239880ef 31#include "xfs_acl.h"
239880ef 32#include "xfs_quota.h"
1da177e4 33#include "xfs_error.h"
1da177e4 34#include "xfs_attr.h"
239880ef 35#include "xfs_trans.h"
0b1b213f 36#include "xfs_trace.h"
27b52867 37#include "xfs_icache.h"
c24b5dfa 38#include "xfs_symlink.h"
0cb97766 39#include "xfs_da_btree.h"
0cb97766 40#include "xfs_dir2_priv.h"
a4fbe6ab 41#include "xfs_dinode.h"
1da177e4 42
16f7e0fe 43#include <linux/capability.h>
1da177e4
LT
44#include <linux/xattr.h>
45#include <linux/namei.h>
ef14f0c1 46#include <linux/posix_acl.h>
446ada4a 47#include <linux/security.h>
f35642e2 48#include <linux/fiemap.h>
5a0e3ad6 49#include <linux/slab.h>
1da177e4 50
8d2a5e6e
DC
51static int
52xfs_initxattrs(
53 struct inode *inode,
54 const struct xattr *xattr_array,
55 void *fs_info)
9d8f13ba 56{
8d2a5e6e
DC
57 const struct xattr *xattr;
58 struct xfs_inode *ip = XFS_I(inode);
59 int error = 0;
9d8f13ba
MZ
60
61 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
62 error = xfs_attr_set(ip, xattr->name, xattr->value,
63 xattr->value_len, ATTR_SECURE);
64 if (error < 0)
65 break;
66 }
67 return error;
68}
69
446ada4a
NS
70/*
71 * Hook in SELinux. This is not quite correct yet, what we really need
72 * here (as we do for default ACLs) is a mechanism by which creation of
73 * these attrs can be journalled at inode creation time (along with the
74 * inode, of course, such that log replay can't cause these to be lost).
75 */
9d8f13ba 76
446ada4a 77STATIC int
416c6d5b 78xfs_init_security(
af048193 79 struct inode *inode,
2a7dba39
EP
80 struct inode *dir,
81 const struct qstr *qstr)
446ada4a 82{
9d8f13ba
MZ
83 return security_inode_init_security(inode, dir, qstr,
84 &xfs_initxattrs, NULL);
446ada4a
NS
85}
86
556b8b16
BN
87static void
88xfs_dentry_to_name(
89 struct xfs_name *namep,
0cb97766
DC
90 struct dentry *dentry,
91 int mode)
556b8b16
BN
92{
93 namep->name = dentry->d_name.name;
94 namep->len = dentry->d_name.len;
0cb97766 95 namep->type = xfs_mode_to_ftype[(mode & S_IFMT) >> S_SHIFT];
556b8b16
BN
96}
97
7989cb8e 98STATIC void
416c6d5b 99xfs_cleanup_inode(
739bfb2a 100 struct inode *dir,
af048193 101 struct inode *inode,
8f112e3b 102 struct dentry *dentry)
3a69c7dc 103{
556b8b16 104 struct xfs_name teardown;
3a69c7dc
YL
105
106 /* Oh, the horror.
220b5284 107 * If we can't add the ACL or we fail in
416c6d5b 108 * xfs_init_security we must back out.
3a69c7dc
YL
109 * ENOSPC can hit here, among other things.
110 */
0cb97766 111 xfs_dentry_to_name(&teardown, dentry, 0);
3a69c7dc 112
8f112e3b 113 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
af048193 114 iput(inode);
3a69c7dc
YL
115}
116
1da177e4 117STATIC int
416c6d5b 118xfs_vn_mknod(
1da177e4
LT
119 struct inode *dir,
120 struct dentry *dentry,
1a67aafb 121 umode_t mode,
1da177e4
LT
122 dev_t rdev)
123{
db0bb7ba 124 struct inode *inode;
979ebab1 125 struct xfs_inode *ip = NULL;
ef14f0c1 126 struct posix_acl *default_acl = NULL;
556b8b16 127 struct xfs_name name;
1da177e4
LT
128 int error;
129
130 /*
131 * Irix uses Missed'em'V split, but doesn't want to see
132 * the upper 5 bits of (14bit) major.
133 */
517b5e8c
CH
134 if (S_ISCHR(mode) || S_ISBLK(mode)) {
135 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
136 return -EINVAL;
137 rdev = sysv_encode_dev(rdev);
138 } else {
139 rdev = 0;
140 }
1da177e4 141
ef14f0c1
CH
142 if (IS_POSIXACL(dir)) {
143 default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
144 if (IS_ERR(default_acl))
c46a131c 145 return PTR_ERR(default_acl);
1da177e4 146
e83f1eb6
CH
147 if (!default_acl)
148 mode &= ~current_umask();
ef14f0c1 149 }
1da177e4 150
0cb97766 151 xfs_dentry_to_name(&name, dentry, mode);
6c77b0ea 152 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
db0bb7ba
CH
153 if (unlikely(error))
154 goto out_free_acl;
446ada4a 155
01651646 156 inode = VFS_I(ip);
979ebab1 157
2a7dba39 158 error = xfs_init_security(inode, dir, &dentry->d_name);
db0bb7ba
CH
159 if (unlikely(error))
160 goto out_cleanup_inode;
161
162 if (default_acl) {
ef14f0c1 163 error = -xfs_inherit_acl(inode, default_acl);
826cae2f 164 default_acl = NULL;
db0bb7ba
CH
165 if (unlikely(error))
166 goto out_cleanup_inode;
1da177e4
LT
167 }
168
1da177e4 169
db0bb7ba 170 d_instantiate(dentry, inode);
db0bb7ba
CH
171 return -error;
172
173 out_cleanup_inode:
8f112e3b 174 xfs_cleanup_inode(dir, inode, dentry);
db0bb7ba 175 out_free_acl:
ef14f0c1 176 posix_acl_release(default_acl);
1da177e4
LT
177 return -error;
178}
179
180STATIC int
416c6d5b 181xfs_vn_create(
1da177e4
LT
182 struct inode *dir,
183 struct dentry *dentry,
4acdaf27 184 umode_t mode,
ebfc3b49 185 bool flags)
1da177e4 186{
416c6d5b 187 return xfs_vn_mknod(dir, dentry, mode, 0);
1da177e4
LT
188}
189
190STATIC int
416c6d5b 191xfs_vn_mkdir(
1da177e4
LT
192 struct inode *dir,
193 struct dentry *dentry,
18bb1db3 194 umode_t mode)
1da177e4 195{
416c6d5b 196 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
1da177e4
LT
197}
198
199STATIC struct dentry *
416c6d5b 200xfs_vn_lookup(
1da177e4
LT
201 struct inode *dir,
202 struct dentry *dentry,
00cd8dd3 203 unsigned int flags)
1da177e4 204{
ef1f5e7a 205 struct xfs_inode *cip;
556b8b16 206 struct xfs_name name;
1da177e4
LT
207 int error;
208
209 if (dentry->d_name.len >= MAXNAMELEN)
210 return ERR_PTR(-ENAMETOOLONG);
211
0cb97766 212 xfs_dentry_to_name(&name, dentry, 0);
384f3ced 213 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
67fcaa73 214 if (unlikely(error)) {
1da177e4
LT
215 if (unlikely(error != ENOENT))
216 return ERR_PTR(-error);
217 d_add(dentry, NULL);
218 return NULL;
219 }
220
01651646 221 return d_splice_alias(VFS_I(cip), dentry);
1da177e4
LT
222}
223
384f3ced
BN
224STATIC struct dentry *
225xfs_vn_ci_lookup(
226 struct inode *dir,
227 struct dentry *dentry,
00cd8dd3 228 unsigned int flags)
384f3ced
BN
229{
230 struct xfs_inode *ip;
231 struct xfs_name xname;
232 struct xfs_name ci_name;
233 struct qstr dname;
234 int error;
235
236 if (dentry->d_name.len >= MAXNAMELEN)
237 return ERR_PTR(-ENAMETOOLONG);
238
0cb97766 239 xfs_dentry_to_name(&xname, dentry, 0);
384f3ced
BN
240 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
241 if (unlikely(error)) {
242 if (unlikely(error != ENOENT))
243 return ERR_PTR(-error);
866d5dc9
BN
244 /*
245 * call d_add(dentry, NULL) here when d_drop_negative_children
246 * is called in xfs_vn_mknod (ie. allow negative dentries
247 * with CI filesystems).
248 */
384f3ced
BN
249 return NULL;
250 }
251
252 /* if exact match, just splice and exit */
253 if (!ci_name.name)
01651646 254 return d_splice_alias(VFS_I(ip), dentry);
384f3ced
BN
255
256 /* else case-insensitive match... */
257 dname.name = ci_name.name;
258 dname.len = ci_name.len;
e45b590b 259 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
384f3ced
BN
260 kmem_free(ci_name.name);
261 return dentry;
262}
263
1da177e4 264STATIC int
416c6d5b 265xfs_vn_link(
1da177e4
LT
266 struct dentry *old_dentry,
267 struct inode *dir,
268 struct dentry *dentry)
269{
d9424b3c 270 struct inode *inode = old_dentry->d_inode;
556b8b16 271 struct xfs_name name;
1da177e4
LT
272 int error;
273
0cb97766 274 xfs_dentry_to_name(&name, dentry, inode->i_mode);
1da177e4 275
556b8b16 276 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
d9424b3c 277 if (unlikely(error))
a3da7896 278 return -error;
a3da7896 279
7de9c6ee 280 ihold(inode);
a3da7896
CH
281 d_instantiate(dentry, inode);
282 return 0;
1da177e4
LT
283}
284
285STATIC int
416c6d5b 286xfs_vn_unlink(
1da177e4
LT
287 struct inode *dir,
288 struct dentry *dentry)
289{
556b8b16 290 struct xfs_name name;
1da177e4
LT
291 int error;
292
0cb97766 293 xfs_dentry_to_name(&name, dentry, 0);
1da177e4 294
e5700704
CH
295 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
296 if (error)
297 return error;
298
299 /*
300 * With unlink, the VFS makes the dentry "negative": no inode,
301 * but still hashed. This is incompatible with case-insensitive
302 * mode, so invalidate (unhash) the dentry in CI-mode.
303 */
304 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
305 d_invalidate(dentry);
306 return 0;
1da177e4
LT
307}
308
309STATIC int
416c6d5b 310xfs_vn_symlink(
1da177e4
LT
311 struct inode *dir,
312 struct dentry *dentry,
313 const char *symname)
314{
3937be5b
CH
315 struct inode *inode;
316 struct xfs_inode *cip = NULL;
556b8b16 317 struct xfs_name name;
1da177e4 318 int error;
576b1d67 319 umode_t mode;
1da177e4 320
3e5daf05 321 mode = S_IFLNK |
ce3b0f8d 322 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
0cb97766 323 xfs_dentry_to_name(&name, dentry, mode);
1da177e4 324
6c77b0ea 325 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
3937be5b
CH
326 if (unlikely(error))
327 goto out;
328
01651646 329 inode = VFS_I(cip);
3937be5b 330
2a7dba39 331 error = xfs_init_security(inode, dir, &dentry->d_name);
3937be5b
CH
332 if (unlikely(error))
333 goto out_cleanup_inode;
334
335 d_instantiate(dentry, inode);
3937be5b
CH
336 return 0;
337
338 out_cleanup_inode:
8f112e3b 339 xfs_cleanup_inode(dir, inode, dentry);
3937be5b 340 out:
1da177e4
LT
341 return -error;
342}
343
1da177e4 344STATIC int
416c6d5b 345xfs_vn_rename(
1da177e4
LT
346 struct inode *odir,
347 struct dentry *odentry,
348 struct inode *ndir,
349 struct dentry *ndentry)
350{
351 struct inode *new_inode = ndentry->d_inode;
556b8b16
BN
352 struct xfs_name oname;
353 struct xfs_name nname;
1da177e4 354
0cb97766
DC
355 xfs_dentry_to_name(&oname, odentry, 0);
356 xfs_dentry_to_name(&nname, ndentry, odentry->d_inode->i_mode);
556b8b16 357
e5700704 358 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
cfa853e4 359 XFS_I(ndir), &nname, new_inode ?
0cb97766 360 XFS_I(new_inode) : NULL);
1da177e4
LT
361}
362
363/*
364 * careful here - this function can get called recursively, so
365 * we need to be very careful about how much stack we use.
366 * uio is kmalloced for this reason...
367 */
008b150a 368STATIC void *
416c6d5b 369xfs_vn_follow_link(
1da177e4
LT
370 struct dentry *dentry,
371 struct nameidata *nd)
372{
1da177e4 373 char *link;
804c83c3 374 int error = -ENOMEM;
1da177e4 375
f52720ca 376 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
804c83c3
CH
377 if (!link)
378 goto out_err;
1da177e4 379
739bfb2a 380 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
804c83c3
CH
381 if (unlikely(error))
382 goto out_kfree;
1da177e4
LT
383
384 nd_set_link(nd, link);
008b150a 385 return NULL;
804c83c3
CH
386
387 out_kfree:
388 kfree(link);
389 out_err:
390 nd_set_link(nd, ERR_PTR(error));
391 return NULL;
1da177e4
LT
392}
393
cde410a9 394STATIC void
416c6d5b 395xfs_vn_put_link(
cde410a9
NS
396 struct dentry *dentry,
397 struct nameidata *nd,
398 void *p)
1da177e4 399{
cde410a9
NS
400 char *s = nd_get_link(nd);
401
1da177e4
LT
402 if (!IS_ERR(s))
403 kfree(s);
404}
405
1da177e4 406STATIC int
416c6d5b 407xfs_vn_getattr(
c43f4087
CH
408 struct vfsmount *mnt,
409 struct dentry *dentry,
410 struct kstat *stat)
1da177e4 411{
c43f4087
CH
412 struct inode *inode = dentry->d_inode;
413 struct xfs_inode *ip = XFS_I(inode);
414 struct xfs_mount *mp = ip->i_mount;
415
cca28fb8 416 trace_xfs_getattr(ip);
c43f4087
CH
417
418 if (XFS_FORCED_SHUTDOWN(mp))
ed32201e 419 return -XFS_ERROR(EIO);
c43f4087
CH
420
421 stat->size = XFS_ISIZE(ip);
422 stat->dev = inode->i_sb->s_dev;
423 stat->mode = ip->i_d.di_mode;
424 stat->nlink = ip->i_d.di_nlink;
7aab1b28
DE
425 stat->uid = inode->i_uid;
426 stat->gid = inode->i_gid;
c43f4087 427 stat->ino = ip->i_ino;
c43f4087 428 stat->atime = inode->i_atime;
f9581b14
CH
429 stat->mtime = inode->i_mtime;
430 stat->ctime = inode->i_ctime;
c43f4087
CH
431 stat->blocks =
432 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
433
434
435 switch (inode->i_mode & S_IFMT) {
436 case S_IFBLK:
437 case S_IFCHR:
438 stat->blksize = BLKDEV_IOSIZE;
439 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
440 sysv_minor(ip->i_df.if_u2.if_rdev));
441 break;
442 default:
71ddabb9 443 if (XFS_IS_REALTIME_INODE(ip)) {
c43f4087
CH
444 /*
445 * If the file blocks are being allocated from a
446 * realtime volume, then return the inode's realtime
447 * extent size or the realtime volume's extent size.
448 */
449 stat->blksize =
450 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
451 } else
452 stat->blksize = xfs_preferred_iosize(mp);
453 stat->rdev = 0;
454 break;
69e23b9a 455 }
c43f4087
CH
456
457 return 0;
1da177e4
LT
458}
459
56c19e89
DC
460static void
461xfs_setattr_mode(
56c19e89
DC
462 struct xfs_inode *ip,
463 struct iattr *iattr)
464{
0c3d88df
CH
465 struct inode *inode = VFS_I(ip);
466 umode_t mode = iattr->ia_mode;
56c19e89 467
56c19e89
DC
468 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
469
56c19e89
DC
470 ip->i_d.di_mode &= S_IFMT;
471 ip->i_d.di_mode |= mode & ~S_IFMT;
472
473 inode->i_mode &= S_IFMT;
474 inode->i_mode |= mode & ~S_IFMT;
475}
476
c4ed4243
CH
477int
478xfs_setattr_nonsize(
479 struct xfs_inode *ip,
480 struct iattr *iattr,
481 int flags)
482{
483 xfs_mount_t *mp = ip->i_mount;
484 struct inode *inode = VFS_I(ip);
485 int mask = iattr->ia_valid;
486 xfs_trans_t *tp;
487 int error;
7aab1b28
DE
488 kuid_t uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
489 kgid_t gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
c4ed4243
CH
490 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
491 struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
492
493 trace_xfs_setattr(ip);
494
42c49d7f
CM
495 /* If acls are being inherited, we already have this checked */
496 if (!(flags & XFS_ATTR_NOACL)) {
497 if (mp->m_flags & XFS_MOUNT_RDONLY)
498 return XFS_ERROR(EROFS);
c4ed4243 499
42c49d7f
CM
500 if (XFS_FORCED_SHUTDOWN(mp))
501 return XFS_ERROR(EIO);
c4ed4243 502
42c49d7f
CM
503 error = -inode_change_ok(inode, iattr);
504 if (error)
505 return XFS_ERROR(error);
506 }
c4ed4243
CH
507
508 ASSERT((mask & ATTR_SIZE) == 0);
509
510 /*
511 * If disk quotas is on, we make sure that the dquots do exist on disk,
512 * before we start any other transactions. Trying to do this later
513 * is messy. We don't care to take a readlock to look at the ids
514 * in inode here, because we can't hold it across the trans_reserve.
515 * If the IDs do change before we take the ilock, we're covered
516 * because the i_*dquot fields will get updated anyway.
517 */
518 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
519 uint qflags = 0;
520
521 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
522 uid = iattr->ia_uid;
523 qflags |= XFS_QMOPT_UQUOTA;
524 } else {
7aab1b28 525 uid = inode->i_uid;
c4ed4243
CH
526 }
527 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
528 gid = iattr->ia_gid;
529 qflags |= XFS_QMOPT_GQUOTA;
530 } else {
7aab1b28 531 gid = inode->i_gid;
c4ed4243
CH
532 }
533
534 /*
535 * We take a reference when we initialize udqp and gdqp,
536 * so it is important that we never blindly double trip on
537 * the same variable. See xfs_create() for an example.
538 */
539 ASSERT(udqp == NULL);
540 ASSERT(gdqp == NULL);
7aab1b28
DE
541 error = xfs_qm_vop_dqalloc(ip, xfs_kuid_to_uid(uid),
542 xfs_kgid_to_gid(gid),
543 xfs_get_projid(ip),
544 qflags, &udqp, &gdqp, NULL);
c4ed4243
CH
545 if (error)
546 return error;
547 }
548
549 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
3d3c8b52 550 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
c4ed4243
CH
551 if (error)
552 goto out_dqrele;
553
554 xfs_ilock(ip, XFS_ILOCK_EXCL);
555
556 /*
557 * Change file ownership. Must be the owner or privileged.
558 */
559 if (mask & (ATTR_UID|ATTR_GID)) {
560 /*
561 * These IDs could have changed since we last looked at them.
562 * But, we're assured that if the ownership did change
563 * while we didn't have the inode locked, inode's dquot(s)
564 * would have changed also.
565 */
7aab1b28
DE
566 iuid = inode->i_uid;
567 igid = inode->i_gid;
c4ed4243
CH
568 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
569 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
570
571 /*
572 * Do a quota reservation only if uid/gid is actually
573 * going to change.
574 */
575 if (XFS_IS_QUOTA_RUNNING(mp) &&
7aab1b28
DE
576 ((XFS_IS_UQUOTA_ON(mp) && !uid_eq(iuid, uid)) ||
577 (XFS_IS_GQUOTA_ON(mp) && !gid_eq(igid, gid)))) {
c4ed4243
CH
578 ASSERT(tp);
579 error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
92f8ff73 580 NULL, capable(CAP_FOWNER) ?
c4ed4243
CH
581 XFS_QMOPT_FORCE_RES : 0);
582 if (error) /* out of quota */
583 goto out_trans_cancel;
584 }
585 }
586
ddc3415a 587 xfs_trans_ijoin(tp, ip, 0);
c4ed4243
CH
588
589 /*
590 * Change file ownership. Must be the owner or privileged.
591 */
592 if (mask & (ATTR_UID|ATTR_GID)) {
593 /*
594 * CAP_FSETID overrides the following restrictions:
595 *
596 * The set-user-ID and set-group-ID bits of a file will be
597 * cleared upon successful return from chown()
598 */
599 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
600 !capable(CAP_FSETID))
601 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
602
603 /*
604 * Change the ownerships and register quota modifications
605 * in the transaction.
606 */
7aab1b28 607 if (!uid_eq(iuid, uid)) {
c4ed4243
CH
608 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
609 ASSERT(mask & ATTR_UID);
610 ASSERT(udqp);
611 olddquot1 = xfs_qm_vop_chown(tp, ip,
612 &ip->i_udquot, udqp);
613 }
7aab1b28 614 ip->i_d.di_uid = xfs_kuid_to_uid(uid);
c4ed4243
CH
615 inode->i_uid = uid;
616 }
7aab1b28 617 if (!gid_eq(igid, gid)) {
c4ed4243
CH
618 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
619 ASSERT(!XFS_IS_PQUOTA_ON(mp));
620 ASSERT(mask & ATTR_GID);
621 ASSERT(gdqp);
622 olddquot2 = xfs_qm_vop_chown(tp, ip,
623 &ip->i_gdquot, gdqp);
624 }
7aab1b28 625 ip->i_d.di_gid = xfs_kgid_to_gid(gid);
c4ed4243
CH
626 inode->i_gid = gid;
627 }
628 }
629
630 /*
631 * Change file access modes.
632 */
56c19e89 633 if (mask & ATTR_MODE)
0c3d88df 634 xfs_setattr_mode(ip, iattr);
c4ed4243
CH
635
636 /*
637 * Change file access or modified times.
638 */
639 if (mask & ATTR_ATIME) {
640 inode->i_atime = iattr->ia_atime;
641 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
642 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
c4ed4243
CH
643 }
644 if (mask & ATTR_CTIME) {
645 inode->i_ctime = iattr->ia_ctime;
646 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
647 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
c4ed4243
CH
648 }
649 if (mask & ATTR_MTIME) {
650 inode->i_mtime = iattr->ia_mtime;
651 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
652 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
c4ed4243
CH
653 }
654
655 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
656
657 XFS_STATS_INC(xs_ig_attrchg);
658
659 if (mp->m_flags & XFS_MOUNT_WSYNC)
660 xfs_trans_set_sync(tp);
661 error = xfs_trans_commit(tp, 0);
662
663 xfs_iunlock(ip, XFS_ILOCK_EXCL);
664
665 /*
666 * Release any dquot(s) the inode had kept before chown.
667 */
668 xfs_qm_dqrele(olddquot1);
669 xfs_qm_dqrele(olddquot2);
670 xfs_qm_dqrele(udqp);
671 xfs_qm_dqrele(gdqp);
672
673 if (error)
674 return XFS_ERROR(error);
675
676 /*
677 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
678 * update. We could avoid this with linked transactions
679 * and passing down the transaction pointer all the way
680 * to attr_set. No previous user of the generic
681 * Posix ACL code seems to care about this issue either.
682 */
683 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
684 error = -xfs_acl_chmod(inode);
685 if (error)
686 return XFS_ERROR(error);
687 }
688
689 return 0;
690
691out_trans_cancel:
692 xfs_trans_cancel(tp, 0);
693 xfs_iunlock(ip, XFS_ILOCK_EXCL);
694out_dqrele:
695 xfs_qm_dqrele(udqp);
696 xfs_qm_dqrele(gdqp);
697 return error;
698}
699
700/*
701 * Truncate file. Must have write permission and not be a directory.
702 */
703int
704xfs_setattr_size(
705 struct xfs_inode *ip,
76ca4c23 706 struct iattr *iattr)
c4ed4243
CH
707{
708 struct xfs_mount *mp = ip->i_mount;
709 struct inode *inode = VFS_I(ip);
710 int mask = iattr->ia_valid;
673e8e59 711 xfs_off_t oldsize, newsize;
c4ed4243
CH
712 struct xfs_trans *tp;
713 int error;
f38996f5 714 uint lock_flags = 0;
c4ed4243
CH
715 uint commit_flags = 0;
716
717 trace_xfs_setattr(ip);
718
719 if (mp->m_flags & XFS_MOUNT_RDONLY)
720 return XFS_ERROR(EROFS);
721
722 if (XFS_FORCED_SHUTDOWN(mp))
723 return XFS_ERROR(EIO);
724
725 error = -inode_change_ok(inode, iattr);
726 if (error)
727 return XFS_ERROR(error);
728
76ca4c23 729 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
c4ed4243 730 ASSERT(S_ISREG(ip->i_d.di_mode));
56c19e89
DC
731 ASSERT((mask & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
732 ATTR_MTIME_SET|ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
c4ed4243 733
ce7ae151 734 oldsize = inode->i_size;
673e8e59
CH
735 newsize = iattr->ia_size;
736
c4ed4243
CH
737 /*
738 * Short circuit the truncate case for zero length files.
739 */
673e8e59 740 if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
681b1200 741 if (!(mask & (ATTR_CTIME|ATTR_MTIME)))
76ca4c23 742 return 0;
681b1200
CH
743
744 /*
745 * Use the regular setattr path to update the timestamps.
746 */
681b1200
CH
747 iattr->ia_valid &= ~ATTR_SIZE;
748 return xfs_setattr_nonsize(ip, iattr, 0);
c4ed4243
CH
749 }
750
751 /*
752 * Make sure that the dquots are attached to the inode.
753 */
f38996f5 754 error = xfs_qm_dqattach(ip, 0);
c4ed4243 755 if (error)
76ca4c23 756 return error;
c4ed4243
CH
757
758 /*
759 * Now we can make the changes. Before we join the inode to the
760 * transaction, take care of the part of the truncation that must be
761 * done without the inode lock. This needs to be done before joining
762 * the inode to the transaction, because the inode cannot be unlocked
763 * once it is a part of the transaction.
764 */
673e8e59 765 if (newsize > oldsize) {
c4ed4243
CH
766 /*
767 * Do the first part of growing a file: zero any data in the
768 * last block that is beyond the old EOF. We need to do this
769 * before the inode is joined to the transaction to modify
770 * i_size.
771 */
673e8e59 772 error = xfs_zero_eof(ip, newsize, oldsize);
c4ed4243 773 if (error)
76ca4c23 774 return error;
c4ed4243 775 }
c4ed4243
CH
776
777 /*
778 * We are going to log the inode size change in this transaction so
779 * any previous writes that are beyond the on disk EOF and the new
780 * EOF that have not been written out need to be written here. If we
781 * do not write the data out, we expose ourselves to the null files
782 * problem.
783 *
784 * Only flush from the on disk size to the smaller of the in memory
785 * file size or the new size as that's the range we really care about
786 * here and prevents waiting for other data not within the range we
787 * care about here.
788 */
673e8e59 789 if (oldsize != ip->i_d.di_size && newsize > ip->i_d.di_size) {
4bc1ea6b
DC
790 error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
791 ip->i_d.di_size, newsize);
c4ed4243 792 if (error)
76ca4c23 793 return error;
c4ed4243
CH
794 }
795
796 /*
4a06fd26 797 * Wait for all direct I/O to complete.
c4ed4243 798 */
4a06fd26 799 inode_dio_wait(inode);
c4ed4243 800
673e8e59 801 error = -block_truncate_page(inode->i_mapping, newsize, xfs_get_blocks);
c4ed4243 802 if (error)
76ca4c23 803 return error;
c4ed4243
CH
804
805 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
3d3c8b52 806 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
c4ed4243
CH
807 if (error)
808 goto out_trans_cancel;
809
673e8e59 810 truncate_setsize(inode, newsize);
c4ed4243
CH
811
812 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
813 lock_flags |= XFS_ILOCK_EXCL;
814
815 xfs_ilock(ip, XFS_ILOCK_EXCL);
816
ddc3415a 817 xfs_trans_ijoin(tp, ip, 0);
c4ed4243
CH
818
819 /*
820 * Only change the c/mtime if we are changing the size or we are
821 * explicitly asked to change it. This handles the semantic difference
822 * between truncate() and ftruncate() as implemented in the VFS.
823 *
824 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
825 * special case where we need to update the times despite not having
826 * these flags set. For all other operations the VFS set these flags
827 * explicitly if it wants a timestamp update.
828 */
673e8e59 829 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) {
c4ed4243
CH
830 iattr->ia_ctime = iattr->ia_mtime =
831 current_fs_time(inode->i_sb);
832 mask |= ATTR_CTIME | ATTR_MTIME;
833 }
834
673e8e59
CH
835 /*
836 * The first thing we do is set the size to new_size permanently on
837 * disk. This way we don't have to worry about anyone ever being able
838 * to look at the data being freed even in the face of a crash.
839 * What we're getting around here is the case where we free a block, it
840 * is allocated to another file, it is written to, and then we crash.
841 * If the new data gets written to the file but the log buffers
842 * containing the free and reallocation don't, then we'd end up with
843 * garbage in the blocks being freed. As long as we make the new size
844 * permanent before actually freeing any blocks it doesn't matter if
845 * they get written to.
846 */
847 ip->i_d.di_size = newsize;
673e8e59
CH
848 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
849
850 if (newsize <= oldsize) {
851 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
c4ed4243
CH
852 if (error)
853 goto out_trans_abort;
854
855 /*
856 * Truncated "down", so we're removing references to old data
857 * here - if we delay flushing for a long time, we expose
858 * ourselves unduly to the notorious NULL files problem. So,
859 * we mark this inode and flush it when the file is closed,
860 * and do not wait the usual (long) time for writeout.
861 */
862 xfs_iflags_set(ip, XFS_ITRUNCATED);
27b52867
BF
863
864 /* A truncate down always removes post-EOF blocks. */
865 xfs_inode_clear_eofblocks_tag(ip);
c4ed4243
CH
866 }
867
56c19e89
DC
868 /*
869 * Change file access modes.
870 */
871 if (mask & ATTR_MODE)
0c3d88df 872 xfs_setattr_mode(ip, iattr);
56c19e89 873
c4ed4243
CH
874 if (mask & ATTR_CTIME) {
875 inode->i_ctime = iattr->ia_ctime;
876 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
877 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
c4ed4243
CH
878 }
879 if (mask & ATTR_MTIME) {
880 inode->i_mtime = iattr->ia_mtime;
881 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
882 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
c4ed4243
CH
883 }
884
885 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
886
887 XFS_STATS_INC(xs_ig_attrchg);
888
889 if (mp->m_flags & XFS_MOUNT_WSYNC)
890 xfs_trans_set_sync(tp);
891
892 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
893out_unlock:
894 if (lock_flags)
895 xfs_iunlock(ip, lock_flags);
896 return error;
897
898out_trans_abort:
899 commit_flags |= XFS_TRANS_ABORT;
900out_trans_cancel:
901 xfs_trans_cancel(tp, commit_flags);
902 goto out_unlock;
903}
904
1da177e4 905STATIC int
416c6d5b 906xfs_vn_setattr(
76ca4c23
CH
907 struct dentry *dentry,
908 struct iattr *iattr)
1da177e4 909{
76ca4c23
CH
910 struct xfs_inode *ip = XFS_I(dentry->d_inode);
911 int error;
912
913 if (iattr->ia_valid & ATTR_SIZE) {
914 xfs_ilock(ip, XFS_IOLOCK_EXCL);
915 error = xfs_setattr_size(ip, iattr);
916 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
917 } else {
918 error = xfs_setattr_nonsize(ip, iattr, 0);
919 }
920
921 return -error;
1da177e4
LT
922}
923
69ff2826
CH
924STATIC int
925xfs_vn_update_time(
926 struct inode *inode,
927 struct timespec *now,
928 int flags)
929{
930 struct xfs_inode *ip = XFS_I(inode);
931 struct xfs_mount *mp = ip->i_mount;
932 struct xfs_trans *tp;
933 int error;
934
935 trace_xfs_update_time(ip);
936
937 tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
3d3c8b52 938 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_fsyncts, 0, 0);
69ff2826
CH
939 if (error) {
940 xfs_trans_cancel(tp, 0);
941 return -error;
942 }
943
944 xfs_ilock(ip, XFS_ILOCK_EXCL);
945 if (flags & S_CTIME) {
946 inode->i_ctime = *now;
947 ip->i_d.di_ctime.t_sec = (__int32_t)now->tv_sec;
948 ip->i_d.di_ctime.t_nsec = (__int32_t)now->tv_nsec;
949 }
950 if (flags & S_MTIME) {
951 inode->i_mtime = *now;
952 ip->i_d.di_mtime.t_sec = (__int32_t)now->tv_sec;
953 ip->i_d.di_mtime.t_nsec = (__int32_t)now->tv_nsec;
954 }
955 if (flags & S_ATIME) {
956 inode->i_atime = *now;
957 ip->i_d.di_atime.t_sec = (__int32_t)now->tv_sec;
958 ip->i_d.di_atime.t_nsec = (__int32_t)now->tv_nsec;
959 }
960 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
961 xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
962 return -xfs_trans_commit(tp, 0);
963}
964
f35642e2
ES
965#define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
966
967/*
968 * Call fiemap helper to fill in user data.
969 * Returns positive errors to xfs_getbmap.
970 */
971STATIC int
972xfs_fiemap_format(
973 void **arg,
974 struct getbmapx *bmv,
975 int *full)
976{
977 int error;
978 struct fiemap_extent_info *fieinfo = *arg;
979 u32 fiemap_flags = 0;
980 u64 logical, physical, length;
981
982 /* Do nothing for a hole */
983 if (bmv->bmv_block == -1LL)
984 return 0;
985
986 logical = BBTOB(bmv->bmv_offset);
987 physical = BBTOB(bmv->bmv_block);
988 length = BBTOB(bmv->bmv_length);
989
990 if (bmv->bmv_oflags & BMV_OF_PREALLOC)
991 fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
992 else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
635c4d0b
JL
993 fiemap_flags |= (FIEMAP_EXTENT_DELALLOC |
994 FIEMAP_EXTENT_UNKNOWN);
f35642e2
ES
995 physical = 0; /* no block yet */
996 }
997 if (bmv->bmv_oflags & BMV_OF_LAST)
998 fiemap_flags |= FIEMAP_EXTENT_LAST;
999
1000 error = fiemap_fill_next_extent(fieinfo, logical, physical,
1001 length, fiemap_flags);
1002 if (error > 0) {
1003 error = 0;
1004 *full = 1; /* user array now full */
1005 }
1006
1007 return -error;
1008}
1009
1010STATIC int
1011xfs_vn_fiemap(
1012 struct inode *inode,
1013 struct fiemap_extent_info *fieinfo,
1014 u64 start,
1015 u64 length)
1016{
1017 xfs_inode_t *ip = XFS_I(inode);
1018 struct getbmapx bm;
1019 int error;
1020
1021 error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
1022 if (error)
1023 return error;
1024
1025 /* Set up bmap header for xfs internal routine */
1026 bm.bmv_offset = BTOBB(start);
1027 /* Special case for whole file */
1028 if (length == FIEMAP_MAX_OFFSET)
1029 bm.bmv_length = -1LL;
1030 else
1031 bm.bmv_length = BTOBB(length);
1032
97db39a1 1033 /* We add one because in getbmap world count includes the header */
2d1ff3c7
TM
1034 bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
1035 fieinfo->fi_extents_max + 1;
1036 bm.bmv_count = min_t(__s32, bm.bmv_count,
1037 (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
9af25465 1038 bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
f35642e2
ES
1039 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
1040 bm.bmv_iflags |= BMV_IF_ATTRFORK;
1041 if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
1042 bm.bmv_iflags |= BMV_IF_DELALLOC;
1043
1044 error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
1045 if (error)
1046 return -error;
1047
1048 return 0;
1049}
1050
41be8bed 1051static const struct inode_operations xfs_inode_operations = {
4e34e719 1052 .get_acl = xfs_get_acl,
416c6d5b
NS
1053 .getattr = xfs_vn_getattr,
1054 .setattr = xfs_vn_setattr,
0ec58516
LM
1055 .setxattr = generic_setxattr,
1056 .getxattr = generic_getxattr,
1057 .removexattr = generic_removexattr,
416c6d5b 1058 .listxattr = xfs_vn_listxattr,
f35642e2 1059 .fiemap = xfs_vn_fiemap,
69ff2826 1060 .update_time = xfs_vn_update_time,
1da177e4
LT
1061};
1062
41be8bed 1063static const struct inode_operations xfs_dir_inode_operations = {
416c6d5b
NS
1064 .create = xfs_vn_create,
1065 .lookup = xfs_vn_lookup,
1066 .link = xfs_vn_link,
1067 .unlink = xfs_vn_unlink,
1068 .symlink = xfs_vn_symlink,
1069 .mkdir = xfs_vn_mkdir,
8f112e3b
CH
1070 /*
1071 * Yes, XFS uses the same method for rmdir and unlink.
1072 *
1073 * There are some subtile differences deeper in the code,
1074 * but we use S_ISDIR to check for those.
1075 */
1076 .rmdir = xfs_vn_unlink,
416c6d5b
NS
1077 .mknod = xfs_vn_mknod,
1078 .rename = xfs_vn_rename,
4e34e719 1079 .get_acl = xfs_get_acl,
416c6d5b
NS
1080 .getattr = xfs_vn_getattr,
1081 .setattr = xfs_vn_setattr,
0ec58516
LM
1082 .setxattr = generic_setxattr,
1083 .getxattr = generic_getxattr,
1084 .removexattr = generic_removexattr,
416c6d5b 1085 .listxattr = xfs_vn_listxattr,
69ff2826 1086 .update_time = xfs_vn_update_time,
1da177e4
LT
1087};
1088
41be8bed 1089static const struct inode_operations xfs_dir_ci_inode_operations = {
384f3ced
BN
1090 .create = xfs_vn_create,
1091 .lookup = xfs_vn_ci_lookup,
1092 .link = xfs_vn_link,
1093 .unlink = xfs_vn_unlink,
1094 .symlink = xfs_vn_symlink,
1095 .mkdir = xfs_vn_mkdir,
8f112e3b
CH
1096 /*
1097 * Yes, XFS uses the same method for rmdir and unlink.
1098 *
1099 * There are some subtile differences deeper in the code,
1100 * but we use S_ISDIR to check for those.
1101 */
1102 .rmdir = xfs_vn_unlink,
384f3ced
BN
1103 .mknod = xfs_vn_mknod,
1104 .rename = xfs_vn_rename,
4e34e719 1105 .get_acl = xfs_get_acl,
384f3ced
BN
1106 .getattr = xfs_vn_getattr,
1107 .setattr = xfs_vn_setattr,
0ec58516
LM
1108 .setxattr = generic_setxattr,
1109 .getxattr = generic_getxattr,
1110 .removexattr = generic_removexattr,
384f3ced 1111 .listxattr = xfs_vn_listxattr,
69ff2826 1112 .update_time = xfs_vn_update_time,
384f3ced
BN
1113};
1114
41be8bed 1115static const struct inode_operations xfs_symlink_inode_operations = {
1da177e4 1116 .readlink = generic_readlink,
416c6d5b
NS
1117 .follow_link = xfs_vn_follow_link,
1118 .put_link = xfs_vn_put_link,
4e34e719 1119 .get_acl = xfs_get_acl,
416c6d5b
NS
1120 .getattr = xfs_vn_getattr,
1121 .setattr = xfs_vn_setattr,
0ec58516
LM
1122 .setxattr = generic_setxattr,
1123 .getxattr = generic_getxattr,
1124 .removexattr = generic_removexattr,
416c6d5b 1125 .listxattr = xfs_vn_listxattr,
69ff2826 1126 .update_time = xfs_vn_update_time,
1da177e4 1127};
41be8bed
CH
1128
1129STATIC void
1130xfs_diflags_to_iflags(
1131 struct inode *inode,
1132 struct xfs_inode *ip)
1133{
1134 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
1135 inode->i_flags |= S_IMMUTABLE;
1136 else
1137 inode->i_flags &= ~S_IMMUTABLE;
1138 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
1139 inode->i_flags |= S_APPEND;
1140 else
1141 inode->i_flags &= ~S_APPEND;
1142 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
1143 inode->i_flags |= S_SYNC;
1144 else
1145 inode->i_flags &= ~S_SYNC;
1146 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
1147 inode->i_flags |= S_NOATIME;
1148 else
1149 inode->i_flags &= ~S_NOATIME;
1150}
1151
1152/*
1153 * Initialize the Linux inode, set up the operation vectors and
1154 * unlock the inode.
1155 *
1156 * When reading existing inodes from disk this is called directly
1157 * from xfs_iget, when creating a new inode it is called from
1158 * xfs_ialloc after setting up the inode.
bf904248
DC
1159 *
1160 * We are always called with an uninitialised linux inode here.
1161 * We need to initialise the necessary fields and take a reference
1162 * on it.
41be8bed
CH
1163 */
1164void
1165xfs_setup_inode(
1166 struct xfs_inode *ip)
1167{
bf904248 1168 struct inode *inode = &ip->i_vnode;
ad22c7a0 1169 gfp_t gfp_mask;
bf904248
DC
1170
1171 inode->i_ino = ip->i_ino;
eaff8079 1172 inode->i_state = I_NEW;
646ec461
CH
1173
1174 inode_sb_list_add(inode);
c6f6cd06
CH
1175 /* make the inode look hashed for the writeback code */
1176 hlist_add_fake(&inode->i_hash);
41be8bed
CH
1177
1178 inode->i_mode = ip->i_d.di_mode;
bfe86848 1179 set_nlink(inode, ip->i_d.di_nlink);
7aab1b28
DE
1180 inode->i_uid = xfs_uid_to_kuid(ip->i_d.di_uid);
1181 inode->i_gid = xfs_gid_to_kgid(ip->i_d.di_gid);
41be8bed
CH
1182
1183 switch (inode->i_mode & S_IFMT) {
1184 case S_IFBLK:
1185 case S_IFCHR:
1186 inode->i_rdev =
1187 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
1188 sysv_minor(ip->i_df.if_u2.if_rdev));
1189 break;
1190 default:
1191 inode->i_rdev = 0;
1192 break;
1193 }
1194
1195 inode->i_generation = ip->i_d.di_gen;
1196 i_size_write(inode, ip->i_d.di_size);
1197 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
1198 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
1199 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
1200 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
1201 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
1202 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
1203 xfs_diflags_to_iflags(inode, ip);
41be8bed 1204
4bceb18f 1205 ip->d_ops = ip->i_mount->m_nondir_inode_ops;
41be8bed
CH
1206 switch (inode->i_mode & S_IFMT) {
1207 case S_IFREG:
1208 inode->i_op = &xfs_inode_operations;
1209 inode->i_fop = &xfs_file_operations;
1210 inode->i_mapping->a_ops = &xfs_address_space_operations;
1211 break;
1212 case S_IFDIR:
1213 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
1214 inode->i_op = &xfs_dir_ci_inode_operations;
1215 else
1216 inode->i_op = &xfs_dir_inode_operations;
1217 inode->i_fop = &xfs_dir_file_operations;
32c5483a 1218 ip->d_ops = ip->i_mount->m_dir_inode_ops;
41be8bed
CH
1219 break;
1220 case S_IFLNK:
1221 inode->i_op = &xfs_symlink_inode_operations;
1222 if (!(ip->i_df.if_flags & XFS_IFINLINE))
1223 inode->i_mapping->a_ops = &xfs_address_space_operations;
1224 break;
1225 default:
1226 inode->i_op = &xfs_inode_operations;
1227 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1228 break;
1229 }
1230
ad22c7a0
DC
1231 /*
1232 * Ensure all page cache allocations are done from GFP_NOFS context to
1233 * prevent direct reclaim recursion back into the filesystem and blowing
1234 * stacks or deadlocking.
1235 */
1236 gfp_mask = mapping_gfp_mask(inode->i_mapping);
1237 mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
1238
510792ee
CH
1239 /*
1240 * If there is no attribute fork no ACL can exist on this inode,
1241 * and it can't have any file capabilities attached to it either.
1242 */
1243 if (!XFS_IFORK_Q(ip)) {
1244 inode_has_no_xattr(inode);
6311b108 1245 cache_no_acl(inode);
510792ee 1246 }
6311b108 1247
41be8bed
CH
1248 xfs_iflags_clear(ip, XFS_INEW);
1249 barrier();
1250
1251 unlock_new_inode(inode);
1252}