]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/xfs/linux-2.6/xfs_iops.c
[XFS] avoid xfs_getattr in XFS_IOC_FSGETXATTR ioctl
[mirror_ubuntu-zesty-kernel.git] / fs / xfs / linux-2.6 / 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"
a844f451 20#include "xfs_bit.h"
1da177e4 21#include "xfs_log.h"
a844f451 22#include "xfs_inum.h"
1da177e4
LT
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
1da177e4
LT
26#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_quota.h"
30#include "xfs_mount.h"
1da177e4 31#include "xfs_bmap_btree.h"
a844f451 32#include "xfs_alloc_btree.h"
1da177e4 33#include "xfs_ialloc_btree.h"
1da177e4 34#include "xfs_dir2_sf.h"
a844f451 35#include "xfs_attr_sf.h"
1da177e4
LT
36#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_bmap.h"
a844f451
NS
39#include "xfs_btree.h"
40#include "xfs_ialloc.h"
1da177e4
LT
41#include "xfs_rtalloc.h"
42#include "xfs_error.h"
43#include "xfs_itable.h"
44#include "xfs_rw.h"
45#include "xfs_acl.h"
1da177e4
LT
46#include "xfs_attr.h"
47#include "xfs_buf_item.h"
48#include "xfs_utils.h"
739bfb2a 49#include "xfs_vnodeops.h"
1da177e4 50
16f7e0fe 51#include <linux/capability.h>
1da177e4
LT
52#include <linux/xattr.h>
53#include <linux/namei.h>
446ada4a 54#include <linux/security.h>
1da177e4 55
42fe2b1f
CH
56/*
57 * Bring the atime in the XFS inode uptodate.
58 * Used before logging the inode to disk or when the Linux inode goes away.
59 */
60void
61xfs_synchronize_atime(
62 xfs_inode_t *ip)
63{
67fcaa73 64 bhv_vnode_t *vp;
42fe2b1f
CH
65
66 vp = XFS_ITOV_NULL(ip);
67 if (vp) {
0a74cd19
CH
68 ip->i_d.di_atime.t_sec = (__int32_t)vp->i_atime.tv_sec;
69 ip->i_d.di_atime.t_nsec = (__int32_t)vp->i_atime.tv_nsec;
42fe2b1f
CH
70 }
71}
72
4aeb664c
NS
73/*
74 * Change the requested timestamp in the given inode.
75 * We don't lock across timestamp updates, and we don't log them but
76 * we do record the fact that there is dirty information in core.
77 *
78 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
79 * with XFS_ICHGTIME_ACC to be sure that access time
80 * update will take. Calling first with XFS_ICHGTIME_ACC
81 * and then XFS_ICHGTIME_MOD may fail to modify the access
82 * timestamp if the filesystem is mounted noacctm.
83 */
84void
85xfs_ichgtime(
86 xfs_inode_t *ip,
87 int flags)
88{
ec86dc02 89 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
4aeb664c
NS
90 timespec_t tv;
91
4aeb664c
NS
92 nanotime(&tv);
93 if (flags & XFS_ICHGTIME_MOD) {
94 inode->i_mtime = tv;
95 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
96 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
97 }
98 if (flags & XFS_ICHGTIME_ACC) {
99 inode->i_atime = tv;
100 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
101 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
102 }
103 if (flags & XFS_ICHGTIME_CHG) {
104 inode->i_ctime = tv;
105 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
106 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
107 }
108
109 /*
110 * We update the i_update_core field _after_ changing
111 * the timestamps in order to coordinate properly with
112 * xfs_iflush() so that we don't lose timestamp updates.
113 * This keeps us from having to hold the inode lock
114 * while doing this. We use the SYNCHRONIZE macro to
115 * ensure that the compiler does not reorder the update
116 * of i_update_core above the timestamp updates above.
117 */
118 SYNCHRONIZE();
119 ip->i_update_core = 1;
120 if (!(inode->i_state & I_LOCK))
121 mark_inode_dirty_sync(inode);
122}
123
124/*
125 * Variant on the above which avoids querying the system clock
126 * in situations where we know the Linux inode timestamps have
127 * just been updated (and so we can update our inode cheaply).
4aeb664c
NS
128 */
129void
130xfs_ichgtime_fast(
131 xfs_inode_t *ip,
132 struct inode *inode,
133 int flags)
134{
135 timespec_t *tvp;
136
137 /*
42fe2b1f
CH
138 * Atime updates for read() & friends are handled lazily now, and
139 * explicit updates must go through xfs_ichgtime()
4aeb664c 140 */
42fe2b1f 141 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
4aeb664c
NS
142
143 /*
42fe2b1f
CH
144 * We're not supposed to change timestamps in readonly-mounted
145 * filesystems. Throw it away if anyone asks us.
4aeb664c 146 */
42fe2b1f 147 if (unlikely(IS_RDONLY(inode)))
4aeb664c
NS
148 return;
149
150 if (flags & XFS_ICHGTIME_MOD) {
151 tvp = &inode->i_mtime;
152 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
153 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
154 }
4aeb664c
NS
155 if (flags & XFS_ICHGTIME_CHG) {
156 tvp = &inode->i_ctime;
157 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
158 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
159 }
160
161 /*
162 * We update the i_update_core field _after_ changing
163 * the timestamps in order to coordinate properly with
164 * xfs_iflush() so that we don't lose timestamp updates.
165 * This keeps us from having to hold the inode lock
166 * while doing this. We use the SYNCHRONIZE macro to
167 * ensure that the compiler does not reorder the update
168 * of i_update_core above the timestamp updates above.
169 */
170 SYNCHRONIZE();
171 ip->i_update_core = 1;
172 if (!(inode->i_state & I_LOCK))
173 mark_inode_dirty_sync(inode);
174}
175
1da177e4
LT
176
177/*
178 * Pull the link count and size up from the xfs inode to the linux inode
179 */
180STATIC void
416c6d5b 181xfs_validate_fields(
220b5284 182 struct inode *ip,
8285fb58 183 bhv_vattr_t *vattr)
1da177e4 184{
220b5284 185 vattr->va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
739bfb2a 186 if (!xfs_getattr(XFS_I(ip), vattr, ATTR_LAZY)) {
220b5284
NS
187 ip->i_nlink = vattr->va_nlink;
188 ip->i_blocks = vattr->va_nblocks;
1da177e4 189
220b5284
NS
190 /* we're under i_sem so i_size can't change under us */
191 if (i_size_read(ip) != vattr->va_size)
192 i_size_write(ip, vattr->va_size);
1da177e4
LT
193 }
194}
195
446ada4a
NS
196/*
197 * Hook in SELinux. This is not quite correct yet, what we really need
198 * here (as we do for default ACLs) is a mechanism by which creation of
199 * these attrs can be journalled at inode creation time (along with the
200 * inode, of course, such that log replay can't cause these to be lost).
201 */
202STATIC int
416c6d5b 203xfs_init_security(
67fcaa73 204 bhv_vnode_t *vp,
446ada4a
NS
205 struct inode *dir)
206{
ec86dc02 207 struct inode *ip = vn_to_inode(vp);
446ada4a
NS
208 size_t length;
209 void *value;
210 char *name;
211 int error;
212
213 error = security_inode_init_security(ip, dir, &name, &value, &length);
214 if (error) {
215 if (error == -EOPNOTSUPP)
216 return 0;
217 return -error;
218 }
219
739bfb2a
CH
220 error = xfs_attr_set(XFS_I(ip), name, value,
221 length, ATTR_SECURE);
446ada4a 222 if (!error)
b3aea4ed 223 xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED);
446ada4a
NS
224
225 kfree(name);
226 kfree(value);
227 return error;
228}
229
1da177e4
LT
230/*
231 * Determine whether a process has a valid fs_struct (kernel daemons
232 * like knfsd don't have an fs_struct).
233 *
234 * XXX(hch): nfsd is broken, better fix it instead.
235 */
7989cb8e 236STATIC_INLINE int
416c6d5b 237xfs_has_fs_struct(struct task_struct *task)
1da177e4
LT
238{
239 return (task->fs != init_task.fs);
240}
241
7989cb8e 242STATIC void
416c6d5b 243xfs_cleanup_inode(
739bfb2a 244 struct inode *dir,
67fcaa73 245 bhv_vnode_t *vp,
220b5284 246 struct dentry *dentry,
3a69c7dc
YL
247 int mode)
248{
249 struct dentry teardown = {};
3a69c7dc
YL
250
251 /* Oh, the horror.
220b5284 252 * If we can't add the ACL or we fail in
416c6d5b 253 * xfs_init_security we must back out.
3a69c7dc
YL
254 * ENOSPC can hit here, among other things.
255 */
ec86dc02 256 teardown.d_inode = vn_to_inode(vp);
3a69c7dc
YL
257 teardown.d_name = dentry->d_name;
258
259 if (S_ISDIR(mode))
739bfb2a 260 xfs_rmdir(XFS_I(dir), &teardown);
3a69c7dc 261 else
739bfb2a 262 xfs_remove(XFS_I(dir), &teardown);
3a69c7dc
YL
263 VN_RELE(vp);
264}
265
1da177e4 266STATIC int
416c6d5b 267xfs_vn_mknod(
1da177e4
LT
268 struct inode *dir,
269 struct dentry *dentry,
270 int mode,
271 dev_t rdev)
272{
273 struct inode *ip;
8285fb58 274 bhv_vattr_t vattr = { 0 };
67fcaa73 275 bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
1da177e4
LT
276 xfs_acl_t *default_acl = NULL;
277 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
278 int error;
279
280 /*
281 * Irix uses Missed'em'V split, but doesn't want to see
282 * the upper 5 bits of (14bit) major.
283 */
220b5284 284 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
1da177e4
LT
285 return -EINVAL;
286
220b5284
NS
287 if (unlikely(test_default_acl && test_default_acl(dvp))) {
288 if (!_ACL_ALLOC(default_acl)) {
1da177e4 289 return -ENOMEM;
220b5284 290 }
1da177e4
LT
291 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
292 _ACL_FREE(default_acl);
293 default_acl = NULL;
294 }
295 }
296
416c6d5b 297 if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
1da177e4
LT
298 mode &= ~current->fs->umask;
299
524fbf5d
NS
300 vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
301 vattr.va_mode = mode;
1da177e4
LT
302
303 switch (mode & S_IFMT) {
304 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
524fbf5d
NS
305 vattr.va_rdev = sysv_encode_dev(rdev);
306 vattr.va_mask |= XFS_AT_RDEV;
1da177e4
LT
307 /*FALLTHROUGH*/
308 case S_IFREG:
739bfb2a 309 error = xfs_create(XFS_I(dir), dentry, &vattr, &vp, NULL);
1da177e4
LT
310 break;
311 case S_IFDIR:
739bfb2a 312 error = xfs_mkdir(XFS_I(dir), dentry, &vattr, &vp, NULL);
1da177e4
LT
313 break;
314 default:
315 error = EINVAL;
316 break;
317 }
318
220b5284 319 if (unlikely(!error)) {
416c6d5b 320 error = xfs_init_security(vp, dir);
3a69c7dc 321 if (error)
739bfb2a 322 xfs_cleanup_inode(dir, vp, dentry, mode);
3a69c7dc 323 }
446ada4a 324
220b5284 325 if (unlikely(default_acl)) {
1da177e4 326 if (!error) {
524fbf5d 327 error = _ACL_INHERIT(vp, &vattr, default_acl);
220b5284 328 if (!error)
0a74cd19 329 xfs_iflags_set(XFS_I(vp), XFS_IMODIFIED);
3a69c7dc 330 else
739bfb2a 331 xfs_cleanup_inode(dir, vp, dentry, mode);
1da177e4
LT
332 }
333 _ACL_FREE(default_acl);
334 }
335
220b5284 336 if (likely(!error)) {
1da177e4 337 ASSERT(vp);
ec86dc02 338 ip = vn_to_inode(vp);
1da177e4
LT
339
340 if (S_ISCHR(mode) || S_ISBLK(mode))
341 ip->i_rdev = rdev;
342 else if (S_ISDIR(mode))
524fbf5d 343 xfs_validate_fields(ip, &vattr);
1da177e4 344 d_instantiate(dentry, ip);
524fbf5d 345 xfs_validate_fields(dir, &vattr);
1da177e4
LT
346 }
347 return -error;
348}
349
350STATIC int
416c6d5b 351xfs_vn_create(
1da177e4
LT
352 struct inode *dir,
353 struct dentry *dentry,
354 int mode,
355 struct nameidata *nd)
356{
416c6d5b 357 return xfs_vn_mknod(dir, dentry, mode, 0);
1da177e4
LT
358}
359
360STATIC int
416c6d5b 361xfs_vn_mkdir(
1da177e4
LT
362 struct inode *dir,
363 struct dentry *dentry,
364 int mode)
365{
416c6d5b 366 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
1da177e4
LT
367}
368
369STATIC struct dentry *
416c6d5b 370xfs_vn_lookup(
1da177e4
LT
371 struct inode *dir,
372 struct dentry *dentry,
373 struct nameidata *nd)
374{
739bfb2a 375 bhv_vnode_t *cvp;
1da177e4
LT
376 int error;
377
378 if (dentry->d_name.len >= MAXNAMELEN)
379 return ERR_PTR(-ENAMETOOLONG);
380
739bfb2a 381 error = xfs_lookup(XFS_I(dir), dentry, &cvp);
67fcaa73 382 if (unlikely(error)) {
1da177e4
LT
383 if (unlikely(error != ENOENT))
384 return ERR_PTR(-error);
385 d_add(dentry, NULL);
386 return NULL;
387 }
388
ec86dc02 389 return d_splice_alias(vn_to_inode(cvp), dentry);
1da177e4
LT
390}
391
392STATIC int
416c6d5b 393xfs_vn_link(
1da177e4
LT
394 struct dentry *old_dentry,
395 struct inode *dir,
396 struct dentry *dentry)
397{
398 struct inode *ip; /* inode of guy being linked to */
67fcaa73 399 bhv_vnode_t *vp; /* vp of name being linked */
8285fb58 400 bhv_vattr_t vattr;
1da177e4
LT
401 int error;
402
403 ip = old_dentry->d_inode; /* inode being linked to */
ec86dc02 404 vp = vn_from_inode(ip);
1da177e4 405
97dfd70c 406 VN_HOLD(vp);
739bfb2a 407 error = xfs_link(XFS_I(dir), vp, dentry);
97dfd70c
NS
408 if (unlikely(error)) {
409 VN_RELE(vp);
410 } else {
b3aea4ed 411 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
524fbf5d 412 xfs_validate_fields(ip, &vattr);
1da177e4
LT
413 d_instantiate(dentry, ip);
414 }
415 return -error;
416}
417
418STATIC int
416c6d5b 419xfs_vn_unlink(
1da177e4
LT
420 struct inode *dir,
421 struct dentry *dentry)
422{
423 struct inode *inode;
8285fb58 424 bhv_vattr_t vattr;
1da177e4
LT
425 int error;
426
427 inode = dentry->d_inode;
1da177e4 428
739bfb2a 429 error = xfs_remove(XFS_I(dir), dentry);
220b5284 430 if (likely(!error)) {
524fbf5d
NS
431 xfs_validate_fields(dir, &vattr); /* size needs update */
432 xfs_validate_fields(inode, &vattr);
1da177e4 433 }
1da177e4
LT
434 return -error;
435}
436
437STATIC int
416c6d5b 438xfs_vn_symlink(
1da177e4
LT
439 struct inode *dir,
440 struct dentry *dentry,
441 const char *symname)
442{
443 struct inode *ip;
8285fb58 444 bhv_vattr_t va = { 0 };
67fcaa73 445 bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
1da177e4
LT
446 int error;
447
1da177e4
LT
448 cvp = NULL;
449
67fcaa73 450 va.va_mode = S_IFLNK |
0432dab2 451 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
67fcaa73 452 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
1da177e4 453
739bfb2a
CH
454 error = xfs_symlink(XFS_I(dir), dentry, &va,
455 (char *)symname, &cvp, NULL);
54245702 456 if (likely(!error && cvp)) {
416c6d5b 457 error = xfs_init_security(cvp, dir);
54245702 458 if (likely(!error)) {
ec86dc02 459 ip = vn_to_inode(cvp);
54245702 460 d_instantiate(dentry, ip);
67fcaa73
NS
461 xfs_validate_fields(dir, &va);
462 xfs_validate_fields(ip, &va);
ce9d37c2 463 } else {
739bfb2a 464 xfs_cleanup_inode(dir, cvp, dentry, 0);
54245702 465 }
1da177e4
LT
466 }
467 return -error;
468}
469
470STATIC int
416c6d5b 471xfs_vn_rmdir(
1da177e4
LT
472 struct inode *dir,
473 struct dentry *dentry)
474{
475 struct inode *inode = dentry->d_inode;
8285fb58 476 bhv_vattr_t vattr;
1da177e4
LT
477 int error;
478
739bfb2a 479 error = xfs_rmdir(XFS_I(dir), dentry);
220b5284 480 if (likely(!error)) {
524fbf5d
NS
481 xfs_validate_fields(inode, &vattr);
482 xfs_validate_fields(dir, &vattr);
1da177e4
LT
483 }
484 return -error;
485}
486
487STATIC int
416c6d5b 488xfs_vn_rename(
1da177e4
LT
489 struct inode *odir,
490 struct dentry *odentry,
491 struct inode *ndir,
492 struct dentry *ndentry)
493{
494 struct inode *new_inode = ndentry->d_inode;
67fcaa73 495 bhv_vnode_t *tvp; /* target directory */
8285fb58 496 bhv_vattr_t vattr;
1da177e4
LT
497 int error;
498
ec86dc02 499 tvp = vn_from_inode(ndir);
1da177e4 500
739bfb2a 501 error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry);
220b5284
NS
502 if (likely(!error)) {
503 if (new_inode)
524fbf5d
NS
504 xfs_validate_fields(new_inode, &vattr);
505 xfs_validate_fields(odir, &vattr);
220b5284 506 if (ndir != odir)
524fbf5d 507 xfs_validate_fields(ndir, &vattr);
220b5284 508 }
220b5284 509 return -error;
1da177e4
LT
510}
511
512/*
513 * careful here - this function can get called recursively, so
514 * we need to be very careful about how much stack we use.
515 * uio is kmalloced for this reason...
516 */
008b150a 517STATIC void *
416c6d5b 518xfs_vn_follow_link(
1da177e4
LT
519 struct dentry *dentry,
520 struct nameidata *nd)
521{
1da177e4 522 char *link;
804c83c3 523 int error = -ENOMEM;
1da177e4 524
f52720ca 525 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
804c83c3
CH
526 if (!link)
527 goto out_err;
1da177e4 528
739bfb2a 529 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
804c83c3
CH
530 if (unlikely(error))
531 goto out_kfree;
1da177e4
LT
532
533 nd_set_link(nd, link);
008b150a 534 return NULL;
804c83c3
CH
535
536 out_kfree:
537 kfree(link);
538 out_err:
539 nd_set_link(nd, ERR_PTR(error));
540 return NULL;
1da177e4
LT
541}
542
cde410a9 543STATIC void
416c6d5b 544xfs_vn_put_link(
cde410a9
NS
545 struct dentry *dentry,
546 struct nameidata *nd,
547 void *p)
1da177e4 548{
cde410a9
NS
549 char *s = nd_get_link(nd);
550
1da177e4
LT
551 if (!IS_ERR(s))
552 kfree(s);
553}
554
555#ifdef CONFIG_XFS_POSIX_ACL
556STATIC int
416c6d5b 557xfs_vn_permission(
1da177e4
LT
558 struct inode *inode,
559 int mode,
560 struct nameidata *nd)
561{
739bfb2a 562 return -xfs_access(XFS_I(inode), mode << 6, NULL);
1da177e4
LT
563}
564#else
416c6d5b 565#define xfs_vn_permission NULL
1da177e4
LT
566#endif
567
568STATIC int
416c6d5b 569xfs_vn_getattr(
1da177e4
LT
570 struct vfsmount *mnt,
571 struct dentry *dentry,
572 struct kstat *stat)
573{
574 struct inode *inode = dentry->d_inode;
69e23b9a
NS
575 bhv_vattr_t vattr = { .va_mask = XFS_AT_STAT };
576 int error;
1da177e4 577
739bfb2a 578 error = xfs_getattr(XFS_I(inode), &vattr, ATTR_LAZY);
69e23b9a
NS
579 if (likely(!error)) {
580 stat->size = i_size_read(inode);
581 stat->dev = inode->i_sb->s_dev;
582 stat->rdev = (vattr.va_rdev == 0) ? 0 :
583 MKDEV(sysv_major(vattr.va_rdev) & 0x1ff,
584 sysv_minor(vattr.va_rdev));
585 stat->mode = vattr.va_mode;
586 stat->nlink = vattr.va_nlink;
587 stat->uid = vattr.va_uid;
588 stat->gid = vattr.va_gid;
589 stat->ino = vattr.va_nodeid;
590 stat->atime = vattr.va_atime;
591 stat->mtime = vattr.va_mtime;
592 stat->ctime = vattr.va_ctime;
593 stat->blocks = vattr.va_nblocks;
594 stat->blksize = vattr.va_blocksize;
595 }
b76963fa 596 return -error;
1da177e4
LT
597}
598
599STATIC int
416c6d5b 600xfs_vn_setattr(
1da177e4
LT
601 struct dentry *dentry,
602 struct iattr *attr)
603{
604 struct inode *inode = dentry->d_inode;
605 unsigned int ia_valid = attr->ia_valid;
8285fb58 606 bhv_vattr_t vattr = { 0 };
1da177e4
LT
607 int flags = 0;
608 int error;
609
1da177e4
LT
610 if (ia_valid & ATTR_UID) {
611 vattr.va_mask |= XFS_AT_UID;
612 vattr.va_uid = attr->ia_uid;
613 }
614 if (ia_valid & ATTR_GID) {
615 vattr.va_mask |= XFS_AT_GID;
616 vattr.va_gid = attr->ia_gid;
617 }
618 if (ia_valid & ATTR_SIZE) {
619 vattr.va_mask |= XFS_AT_SIZE;
620 vattr.va_size = attr->ia_size;
621 }
622 if (ia_valid & ATTR_ATIME) {
623 vattr.va_mask |= XFS_AT_ATIME;
624 vattr.va_atime = attr->ia_atime;
8c0b5113 625 inode->i_atime = attr->ia_atime;
1da177e4
LT
626 }
627 if (ia_valid & ATTR_MTIME) {
628 vattr.va_mask |= XFS_AT_MTIME;
629 vattr.va_mtime = attr->ia_mtime;
630 }
631 if (ia_valid & ATTR_CTIME) {
632 vattr.va_mask |= XFS_AT_CTIME;
633 vattr.va_ctime = attr->ia_ctime;
634 }
635 if (ia_valid & ATTR_MODE) {
636 vattr.va_mask |= XFS_AT_MODE;
637 vattr.va_mode = attr->ia_mode;
638 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
639 inode->i_mode &= ~S_ISGID;
640 }
641
642 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
643 flags |= ATTR_UTIME;
644#ifdef ATTR_NO_BLOCK
645 if ((ia_valid & ATTR_NO_BLOCK))
646 flags |= ATTR_NONBLOCK;
647#endif
648
739bfb2a 649 error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
220b5284 650 if (likely(!error))
739bfb2a 651 __vn_revalidate(vn_from_inode(inode), &vattr);
220b5284 652 return -error;
1da177e4
LT
653}
654
655STATIC void
416c6d5b 656xfs_vn_truncate(
1da177e4
LT
657 struct inode *inode)
658{
c2536668 659 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
1da177e4
LT
660}
661
662STATIC int
416c6d5b 663xfs_vn_setxattr(
1da177e4
LT
664 struct dentry *dentry,
665 const char *name,
666 const void *data,
667 size_t size,
668 int flags)
669{
67fcaa73 670 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
1da177e4
LT
671 char *attr = (char *)name;
672 attrnames_t *namesp;
673 int xflags = 0;
674 int error;
675
676 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
677 if (!namesp)
678 return -EOPNOTSUPP;
679 attr += namesp->attr_namelen;
680 error = namesp->attr_capable(vp, NULL);
681 if (error)
682 return error;
683
684 /* Convert Linux syscall to XFS internal ATTR flags */
685 if (flags & XATTR_CREATE)
686 xflags |= ATTR_CREATE;
687 if (flags & XATTR_REPLACE)
688 xflags |= ATTR_REPLACE;
689 xflags |= namesp->attr_flag;
690 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
691}
692
693STATIC ssize_t
416c6d5b 694xfs_vn_getxattr(
1da177e4
LT
695 struct dentry *dentry,
696 const char *name,
697 void *data,
698 size_t size)
699{
67fcaa73 700 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
1da177e4
LT
701 char *attr = (char *)name;
702 attrnames_t *namesp;
703 int xflags = 0;
704 ssize_t error;
705
706 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
707 if (!namesp)
708 return -EOPNOTSUPP;
709 attr += namesp->attr_namelen;
710 error = namesp->attr_capable(vp, NULL);
711 if (error)
712 return error;
713
714 /* Convert Linux syscall to XFS internal ATTR flags */
715 if (!size) {
716 xflags |= ATTR_KERNOVAL;
717 data = NULL;
718 }
719 xflags |= namesp->attr_flag;
720 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
721}
722
723STATIC ssize_t
416c6d5b 724xfs_vn_listxattr(
1da177e4
LT
725 struct dentry *dentry,
726 char *data,
727 size_t size)
728{
67fcaa73 729 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
1da177e4
LT
730 int error, xflags = ATTR_KERNAMELS;
731 ssize_t result;
732
733 if (!size)
734 xflags |= ATTR_KERNOVAL;
735 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
736
737 error = attr_generic_list(vp, data, size, xflags, &result);
738 if (error < 0)
739 return error;
740 return result;
741}
742
743STATIC int
416c6d5b 744xfs_vn_removexattr(
1da177e4
LT
745 struct dentry *dentry,
746 const char *name)
747{
67fcaa73 748 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
1da177e4
LT
749 char *attr = (char *)name;
750 attrnames_t *namesp;
751 int xflags = 0;
752 int error;
753
754 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
755 if (!namesp)
756 return -EOPNOTSUPP;
757 attr += namesp->attr_namelen;
758 error = namesp->attr_capable(vp, NULL);
759 if (error)
760 return error;
761 xflags |= namesp->attr_flag;
762 return namesp->attr_remove(vp, attr, xflags);
763}
764
765
c5ef1c42 766const struct inode_operations xfs_inode_operations = {
416c6d5b
NS
767 .permission = xfs_vn_permission,
768 .truncate = xfs_vn_truncate,
769 .getattr = xfs_vn_getattr,
770 .setattr = xfs_vn_setattr,
771 .setxattr = xfs_vn_setxattr,
772 .getxattr = xfs_vn_getxattr,
773 .listxattr = xfs_vn_listxattr,
774 .removexattr = xfs_vn_removexattr,
1da177e4
LT
775};
776
c5ef1c42 777const struct inode_operations xfs_dir_inode_operations = {
416c6d5b
NS
778 .create = xfs_vn_create,
779 .lookup = xfs_vn_lookup,
780 .link = xfs_vn_link,
781 .unlink = xfs_vn_unlink,
782 .symlink = xfs_vn_symlink,
783 .mkdir = xfs_vn_mkdir,
784 .rmdir = xfs_vn_rmdir,
785 .mknod = xfs_vn_mknod,
786 .rename = xfs_vn_rename,
787 .permission = xfs_vn_permission,
788 .getattr = xfs_vn_getattr,
789 .setattr = xfs_vn_setattr,
790 .setxattr = xfs_vn_setxattr,
791 .getxattr = xfs_vn_getxattr,
792 .listxattr = xfs_vn_listxattr,
793 .removexattr = xfs_vn_removexattr,
1da177e4
LT
794};
795
c5ef1c42 796const struct inode_operations xfs_symlink_inode_operations = {
1da177e4 797 .readlink = generic_readlink,
416c6d5b
NS
798 .follow_link = xfs_vn_follow_link,
799 .put_link = xfs_vn_put_link,
800 .permission = xfs_vn_permission,
801 .getattr = xfs_vn_getattr,
802 .setattr = xfs_vn_setattr,
803 .setxattr = xfs_vn_setxattr,
804 .getxattr = xfs_vn_getxattr,
805 .listxattr = xfs_vn_listxattr,
806 .removexattr = xfs_vn_removexattr,
1da177e4 807};