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