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