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