]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/xfs/linux-2.6/xfs_iops.c
[XFS] Remove d_add call for an ENOENT lookup return code
[linux-2.6-omap-h63xx.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_attr.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_utils.h"
49 #include "xfs_vnodeops.h"
50
51 #include <linux/capability.h>
52 #include <linux/xattr.h>
53 #include <linux/namei.h>
54 #include <linux/security.h>
55 #include <linux/falloc.h>
56
57 /*
58  * Bring the atime in the XFS inode uptodate.
59  * Used before logging the inode to disk or when the Linux inode goes away.
60  */
61 void
62 xfs_synchronize_atime(
63         xfs_inode_t     *ip)
64 {
65         struct inode    *inode = ip->i_vnode;
66
67         if (inode) {
68                 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
69                 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
70         }
71 }
72
73 /*
74  * If the linux inode exists, mark it dirty.
75  * Used when commiting a dirty inode into a transaction so that
76  * the inode will get written back by the linux code
77  */
78 void
79 xfs_mark_inode_dirty_sync(
80         xfs_inode_t     *ip)
81 {
82         struct inode    *inode = ip->i_vnode;
83
84         if (inode)
85                 mark_inode_dirty_sync(inode);
86 }
87
88 /*
89  * Change the requested timestamp in the given inode.
90  * We don't lock across timestamp updates, and we don't log them but
91  * we do record the fact that there is dirty information in core.
92  *
93  * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
94  *              with XFS_ICHGTIME_ACC to be sure that access time
95  *              update will take.  Calling first with XFS_ICHGTIME_ACC
96  *              and then XFS_ICHGTIME_MOD may fail to modify the access
97  *              timestamp if the filesystem is mounted noacctm.
98  */
99 void
100 xfs_ichgtime(
101         xfs_inode_t     *ip,
102         int             flags)
103 {
104         struct inode    *inode = vn_to_inode(XFS_ITOV(ip));
105         timespec_t      tv;
106
107         nanotime(&tv);
108         if (flags & XFS_ICHGTIME_MOD) {
109                 inode->i_mtime = tv;
110                 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
111                 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
112         }
113         if (flags & XFS_ICHGTIME_ACC) {
114                 inode->i_atime = tv;
115                 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
116                 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
117         }
118         if (flags & XFS_ICHGTIME_CHG) {
119                 inode->i_ctime = tv;
120                 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
121                 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
122         }
123
124         /*
125          * We update the i_update_core field _after_ changing
126          * the timestamps in order to coordinate properly with
127          * xfs_iflush() so that we don't lose timestamp updates.
128          * This keeps us from having to hold the inode lock
129          * while doing this.  We use the SYNCHRONIZE macro to
130          * ensure that the compiler does not reorder the update
131          * of i_update_core above the timestamp updates above.
132          */
133         SYNCHRONIZE();
134         ip->i_update_core = 1;
135         if (!(inode->i_state & I_NEW))
136                 mark_inode_dirty_sync(inode);
137 }
138
139 /*
140  * Variant on the above which avoids querying the system clock
141  * in situations where we know the Linux inode timestamps have
142  * just been updated (and so we can update our inode cheaply).
143  */
144 void
145 xfs_ichgtime_fast(
146         xfs_inode_t     *ip,
147         struct inode    *inode,
148         int             flags)
149 {
150         timespec_t      *tvp;
151
152         /*
153          * Atime updates for read() & friends are handled lazily now, and
154          * explicit updates must go through xfs_ichgtime()
155          */
156         ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
157
158         if (flags & XFS_ICHGTIME_MOD) {
159                 tvp = &inode->i_mtime;
160                 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
161                 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
162         }
163         if (flags & XFS_ICHGTIME_CHG) {
164                 tvp = &inode->i_ctime;
165                 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
166                 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
167         }
168
169         /*
170          * We update the i_update_core field _after_ changing
171          * the timestamps in order to coordinate properly with
172          * xfs_iflush() so that we don't lose timestamp updates.
173          * This keeps us from having to hold the inode lock
174          * while doing this.  We use the SYNCHRONIZE macro to
175          * ensure that the compiler does not reorder the update
176          * of i_update_core above the timestamp updates above.
177          */
178         SYNCHRONIZE();
179         ip->i_update_core = 1;
180         if (!(inode->i_state & I_NEW))
181                 mark_inode_dirty_sync(inode);
182 }
183
184
185 /*
186  * Pull the link count and size up from the xfs inode to the linux inode
187  */
188 STATIC void
189 xfs_validate_fields(
190         struct inode            *inode)
191 {
192         struct xfs_inode        *ip = XFS_I(inode);
193         loff_t size;
194
195         /* we're under i_sem so i_size can't change under us */
196         size = XFS_ISIZE(ip);
197         if (i_size_read(inode) != size)
198                 i_size_write(inode, size);
199 }
200
201 /*
202  * Hook in SELinux.  This is not quite correct yet, what we really need
203  * here (as we do for default ACLs) is a mechanism by which creation of
204  * these attrs can be journalled at inode creation time (along with the
205  * inode, of course, such that log replay can't cause these to be lost).
206  */
207 STATIC int
208 xfs_init_security(
209         struct inode    *inode,
210         struct inode    *dir)
211 {
212         struct xfs_inode *ip = XFS_I(inode);
213         size_t          length;
214         void            *value;
215         char            *name;
216         int             error;
217
218         error = security_inode_init_security(inode, dir, &name,
219                                              &value, &length);
220         if (error) {
221                 if (error == -EOPNOTSUPP)
222                         return 0;
223                 return -error;
224         }
225
226         error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
227         if (!error)
228                 xfs_iflags_set(ip, XFS_IMODIFIED);
229
230         kfree(name);
231         kfree(value);
232         return error;
233 }
234
235 static void
236 xfs_dentry_to_name(
237         struct xfs_name *namep,
238         struct dentry   *dentry)
239 {
240         namep->name = dentry->d_name.name;
241         namep->len = dentry->d_name.len;
242 }
243
244 STATIC void
245 xfs_cleanup_inode(
246         struct inode    *dir,
247         struct inode    *inode,
248         struct dentry   *dentry,
249         int             mode)
250 {
251         struct xfs_name teardown;
252
253         /* Oh, the horror.
254          * If we can't add the ACL or we fail in
255          * xfs_init_security we must back out.
256          * ENOSPC can hit here, among other things.
257          */
258         xfs_dentry_to_name(&teardown, dentry);
259
260         if (S_ISDIR(mode))
261                 xfs_rmdir(XFS_I(dir), &teardown, XFS_I(inode));
262         else
263                 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
264         iput(inode);
265 }
266
267 STATIC int
268 xfs_vn_mknod(
269         struct inode    *dir,
270         struct dentry   *dentry,
271         int             mode,
272         dev_t           rdev)
273 {
274         struct inode    *inode;
275         struct xfs_inode *ip = NULL;
276         xfs_acl_t       *default_acl = NULL;
277         struct xfs_name name;
278         attrexists_t    test_default_acl = _ACL_DEFAULT_EXISTS;
279         int             error;
280
281         /*
282          * Irix uses Missed'em'V split, but doesn't want to see
283          * the upper 5 bits of (14bit) major.
284          */
285         if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
286                 return -EINVAL;
287
288         if (test_default_acl && test_default_acl(dir)) {
289                 if (!_ACL_ALLOC(default_acl)) {
290                         return -ENOMEM;
291                 }
292                 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
293                         _ACL_FREE(default_acl);
294                         default_acl = NULL;
295                 }
296         }
297
298         xfs_dentry_to_name(&name, dentry);
299
300         if (IS_POSIXACL(dir) && !default_acl)
301                 mode &= ~current->fs->umask;
302
303         switch (mode & S_IFMT) {
304         case S_IFCHR:
305         case S_IFBLK:
306         case S_IFIFO:
307         case S_IFSOCK:
308                 rdev = sysv_encode_dev(rdev);
309         case S_IFREG:
310                 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
311                 break;
312         case S_IFDIR:
313                 error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL);
314                 break;
315         default:
316                 error = EINVAL;
317                 break;
318         }
319
320         if (unlikely(error))
321                 goto out_free_acl;
322
323         inode = ip->i_vnode;
324
325         error = xfs_init_security(inode, dir);
326         if (unlikely(error))
327                 goto out_cleanup_inode;
328
329         if (default_acl) {
330                 error = _ACL_INHERIT(inode, mode, default_acl);
331                 if (unlikely(error))
332                         goto out_cleanup_inode;
333                 xfs_iflags_set(ip, XFS_IMODIFIED);
334                 _ACL_FREE(default_acl);
335         }
336
337
338         if (S_ISDIR(mode))
339                 xfs_validate_fields(inode);
340         d_instantiate(dentry, inode);
341         xfs_validate_fields(dir);
342         return -error;
343
344  out_cleanup_inode:
345         xfs_cleanup_inode(dir, inode, dentry, mode);
346  out_free_acl:
347         if (default_acl)
348                 _ACL_FREE(default_acl);
349         return -error;
350 }
351
352 STATIC int
353 xfs_vn_create(
354         struct inode    *dir,
355         struct dentry   *dentry,
356         int             mode,
357         struct nameidata *nd)
358 {
359         return xfs_vn_mknod(dir, dentry, mode, 0);
360 }
361
362 STATIC int
363 xfs_vn_mkdir(
364         struct inode    *dir,
365         struct dentry   *dentry,
366         int             mode)
367 {
368         return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
369 }
370
371 STATIC struct dentry *
372 xfs_vn_lookup(
373         struct inode    *dir,
374         struct dentry   *dentry,
375         struct nameidata *nd)
376 {
377         struct xfs_inode *cip;
378         struct xfs_name name;
379         int             error;
380
381         if (dentry->d_name.len >= MAXNAMELEN)
382                 return ERR_PTR(-ENAMETOOLONG);
383
384         xfs_dentry_to_name(&name, dentry);
385         error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
386         if (unlikely(error)) {
387                 if (unlikely(error != ENOENT))
388                         return ERR_PTR(-error);
389                 d_add(dentry, NULL);
390                 return NULL;
391         }
392
393         return d_splice_alias(cip->i_vnode, dentry);
394 }
395
396 STATIC struct dentry *
397 xfs_vn_ci_lookup(
398         struct inode    *dir,
399         struct dentry   *dentry,
400         struct nameidata *nd)
401 {
402         struct xfs_inode *ip;
403         struct xfs_name xname;
404         struct xfs_name ci_name;
405         struct qstr     dname;
406         int             error;
407
408         if (dentry->d_name.len >= MAXNAMELEN)
409                 return ERR_PTR(-ENAMETOOLONG);
410
411         xfs_dentry_to_name(&xname, dentry);
412         error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
413         if (unlikely(error)) {
414                 if (unlikely(error != ENOENT))
415                         return ERR_PTR(-error);
416                 /*
417                  * call d_add(dentry, NULL) here when d_drop_negative_children
418                  * is called in xfs_vn_mknod (ie. allow negative dentries
419                  * with CI filesystems).
420                  */
421                 return NULL;
422         }
423
424         /* if exact match, just splice and exit */
425         if (!ci_name.name)
426                 return d_splice_alias(ip->i_vnode, dentry);
427
428         /* else case-insensitive match... */
429         dname.name = ci_name.name;
430         dname.len = ci_name.len;
431         dentry = d_add_ci(ip->i_vnode, dentry, &dname);
432         kmem_free(ci_name.name);
433         return dentry;
434 }
435
436 STATIC int
437 xfs_vn_link(
438         struct dentry   *old_dentry,
439         struct inode    *dir,
440         struct dentry   *dentry)
441 {
442         struct inode    *inode; /* inode of guy being linked to */
443         struct xfs_name name;
444         int             error;
445
446         inode = old_dentry->d_inode;
447         xfs_dentry_to_name(&name, dentry);
448
449         igrab(inode);
450         error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
451         if (unlikely(error)) {
452                 iput(inode);
453                 return -error;
454         }
455
456         xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
457         xfs_validate_fields(inode);
458         d_instantiate(dentry, inode);
459         return 0;
460 }
461
462 STATIC int
463 xfs_vn_unlink(
464         struct inode    *dir,
465         struct dentry   *dentry)
466 {
467         struct inode    *inode;
468         struct xfs_name name;
469         int             error;
470
471         inode = dentry->d_inode;
472         xfs_dentry_to_name(&name, dentry);
473
474         error = xfs_remove(XFS_I(dir), &name, XFS_I(inode));
475         if (likely(!error)) {
476                 xfs_validate_fields(dir);       /* size needs update */
477                 xfs_validate_fields(inode);
478         }
479         return -error;
480 }
481
482 STATIC int
483 xfs_vn_symlink(
484         struct inode    *dir,
485         struct dentry   *dentry,
486         const char      *symname)
487 {
488         struct inode    *inode;
489         struct xfs_inode *cip = NULL;
490         struct xfs_name name;
491         int             error;
492         mode_t          mode;
493
494         mode = S_IFLNK |
495                 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
496         xfs_dentry_to_name(&name, dentry);
497
498         error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
499         if (unlikely(error))
500                 goto out;
501
502         inode = cip->i_vnode;
503
504         error = xfs_init_security(inode, dir);
505         if (unlikely(error))
506                 goto out_cleanup_inode;
507
508         d_instantiate(dentry, inode);
509         xfs_validate_fields(dir);
510         xfs_validate_fields(inode);
511         return 0;
512
513  out_cleanup_inode:
514         xfs_cleanup_inode(dir, inode, dentry, 0);
515  out:
516         return -error;
517 }
518
519 STATIC int
520 xfs_vn_rmdir(
521         struct inode    *dir,
522         struct dentry   *dentry)
523 {
524         struct inode    *inode = dentry->d_inode;
525         struct xfs_name name;
526         int             error;
527
528         xfs_dentry_to_name(&name, dentry);
529
530         error = xfs_rmdir(XFS_I(dir), &name, XFS_I(inode));
531         if (likely(!error)) {
532                 xfs_validate_fields(inode);
533                 xfs_validate_fields(dir);
534         }
535         return -error;
536 }
537
538 STATIC int
539 xfs_vn_rename(
540         struct inode    *odir,
541         struct dentry   *odentry,
542         struct inode    *ndir,
543         struct dentry   *ndentry)
544 {
545         struct inode    *new_inode = ndentry->d_inode;
546         struct xfs_name oname;
547         struct xfs_name nname;
548         int             error;
549
550         xfs_dentry_to_name(&oname, odentry);
551         xfs_dentry_to_name(&nname, ndentry);
552
553         error = xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
554                            XFS_I(ndir), &nname, new_inode ?
555                                                 XFS_I(new_inode) : NULL);
556         if (likely(!error)) {
557                 if (new_inode)
558                         xfs_validate_fields(new_inode);
559                 xfs_validate_fields(odir);
560                 if (ndir != odir)
561                         xfs_validate_fields(ndir);
562         }
563         return -error;
564 }
565
566 /*
567  * careful here - this function can get called recursively, so
568  * we need to be very careful about how much stack we use.
569  * uio is kmalloced for this reason...
570  */
571 STATIC void *
572 xfs_vn_follow_link(
573         struct dentry           *dentry,
574         struct nameidata        *nd)
575 {
576         char                    *link;
577         int                     error = -ENOMEM;
578
579         link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
580         if (!link)
581                 goto out_err;
582
583         error = -xfs_readlink(XFS_I(dentry->d_inode), link);
584         if (unlikely(error))
585                 goto out_kfree;
586
587         nd_set_link(nd, link);
588         return NULL;
589
590  out_kfree:
591         kfree(link);
592  out_err:
593         nd_set_link(nd, ERR_PTR(error));
594         return NULL;
595 }
596
597 STATIC void
598 xfs_vn_put_link(
599         struct dentry   *dentry,
600         struct nameidata *nd,
601         void            *p)
602 {
603         char            *s = nd_get_link(nd);
604
605         if (!IS_ERR(s))
606                 kfree(s);
607 }
608
609 #ifdef CONFIG_XFS_POSIX_ACL
610 STATIC int
611 xfs_check_acl(
612         struct inode            *inode,
613         int                     mask)
614 {
615         struct xfs_inode        *ip = XFS_I(inode);
616         int                     error;
617
618         xfs_itrace_entry(ip);
619
620         if (XFS_IFORK_Q(ip)) {
621                 error = xfs_acl_iaccess(ip, mask, NULL);
622                 if (error != -1)
623                         return -error;
624         }
625
626         return -EAGAIN;
627 }
628
629 STATIC int
630 xfs_vn_permission(
631         struct inode            *inode,
632         int                     mask)
633 {
634         return generic_permission(inode, mask, xfs_check_acl);
635 }
636 #else
637 #define xfs_vn_permission NULL
638 #endif
639
640 STATIC int
641 xfs_vn_getattr(
642         struct vfsmount         *mnt,
643         struct dentry           *dentry,
644         struct kstat            *stat)
645 {
646         struct inode            *inode = dentry->d_inode;
647         struct xfs_inode        *ip = XFS_I(inode);
648         struct xfs_mount        *mp = ip->i_mount;
649
650         xfs_itrace_entry(ip);
651
652         if (XFS_FORCED_SHUTDOWN(mp))
653                 return XFS_ERROR(EIO);
654
655         stat->size = XFS_ISIZE(ip);
656         stat->dev = inode->i_sb->s_dev;
657         stat->mode = ip->i_d.di_mode;
658         stat->nlink = ip->i_d.di_nlink;
659         stat->uid = ip->i_d.di_uid;
660         stat->gid = ip->i_d.di_gid;
661         stat->ino = ip->i_ino;
662 #if XFS_BIG_INUMS
663         stat->ino += mp->m_inoadd;
664 #endif
665         stat->atime = inode->i_atime;
666         stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
667         stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
668         stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
669         stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
670         stat->blocks =
671                 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
672
673
674         switch (inode->i_mode & S_IFMT) {
675         case S_IFBLK:
676         case S_IFCHR:
677                 stat->blksize = BLKDEV_IOSIZE;
678                 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
679                                    sysv_minor(ip->i_df.if_u2.if_rdev));
680                 break;
681         default:
682                 if (XFS_IS_REALTIME_INODE(ip)) {
683                         /*
684                          * If the file blocks are being allocated from a
685                          * realtime volume, then return the inode's realtime
686                          * extent size or the realtime volume's extent size.
687                          */
688                         stat->blksize =
689                                 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
690                 } else
691                         stat->blksize = xfs_preferred_iosize(mp);
692                 stat->rdev = 0;
693                 break;
694         }
695
696         return 0;
697 }
698
699 STATIC int
700 xfs_vn_setattr(
701         struct dentry   *dentry,
702         struct iattr    *attr)
703 {
704         struct inode    *inode = dentry->d_inode;
705         unsigned int    ia_valid = attr->ia_valid;
706         bhv_vattr_t     vattr = { 0 };
707         int             flags = 0;
708         int             error;
709
710         if (ia_valid & ATTR_UID) {
711                 vattr.va_mask |= XFS_AT_UID;
712                 vattr.va_uid = attr->ia_uid;
713         }
714         if (ia_valid & ATTR_GID) {
715                 vattr.va_mask |= XFS_AT_GID;
716                 vattr.va_gid = attr->ia_gid;
717         }
718         if (ia_valid & ATTR_SIZE) {
719                 vattr.va_mask |= XFS_AT_SIZE;
720                 vattr.va_size = attr->ia_size;
721         }
722         if (ia_valid & ATTR_ATIME) {
723                 vattr.va_mask |= XFS_AT_ATIME;
724                 vattr.va_atime = attr->ia_atime;
725                 inode->i_atime = attr->ia_atime;
726         }
727         if (ia_valid & ATTR_MTIME) {
728                 vattr.va_mask |= XFS_AT_MTIME;
729                 vattr.va_mtime = attr->ia_mtime;
730         }
731         if (ia_valid & ATTR_CTIME) {
732                 vattr.va_mask |= XFS_AT_CTIME;
733                 vattr.va_ctime = attr->ia_ctime;
734         }
735         if (ia_valid & ATTR_MODE) {
736                 vattr.va_mask |= XFS_AT_MODE;
737                 vattr.va_mode = attr->ia_mode;
738                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
739                         inode->i_mode &= ~S_ISGID;
740         }
741
742         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
743                 flags |= ATTR_UTIME;
744 #ifdef ATTR_NO_BLOCK
745         if ((ia_valid & ATTR_NO_BLOCK))
746                 flags |= ATTR_NONBLOCK;
747 #endif
748
749         error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
750         if (likely(!error))
751                 vn_revalidate(vn_from_inode(inode));
752         return -error;
753 }
754
755 /*
756  * block_truncate_page can return an error, but we can't propagate it
757  * at all here. Leave a complaint + stack trace in the syslog because
758  * this could be bad. If it is bad, we need to propagate the error further.
759  */
760 STATIC void
761 xfs_vn_truncate(
762         struct inode    *inode)
763 {
764         int     error;
765         error = block_truncate_page(inode->i_mapping, inode->i_size,
766                                                         xfs_get_blocks);
767         WARN_ON(error);
768 }
769
770 STATIC int
771 xfs_vn_setxattr(
772         struct dentry   *dentry,
773         const char      *name,
774         const void      *data,
775         size_t          size,
776         int             flags)
777 {
778         bhv_vnode_t     *vp = vn_from_inode(dentry->d_inode);
779         char            *attr = (char *)name;
780         attrnames_t     *namesp;
781         int             xflags = 0;
782
783         namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
784         if (!namesp)
785                 return -EOPNOTSUPP;
786         attr += namesp->attr_namelen;
787
788         /* Convert Linux syscall to XFS internal ATTR flags */
789         if (flags & XATTR_CREATE)
790                 xflags |= ATTR_CREATE;
791         if (flags & XATTR_REPLACE)
792                 xflags |= ATTR_REPLACE;
793         xflags |= namesp->attr_flag;
794         return namesp->attr_set(vp, attr, (void *)data, size, xflags);
795 }
796
797 STATIC ssize_t
798 xfs_vn_getxattr(
799         struct dentry   *dentry,
800         const char      *name,
801         void            *data,
802         size_t          size)
803 {
804         bhv_vnode_t     *vp = vn_from_inode(dentry->d_inode);
805         char            *attr = (char *)name;
806         attrnames_t     *namesp;
807         int             xflags = 0;
808
809         namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
810         if (!namesp)
811                 return -EOPNOTSUPP;
812         attr += namesp->attr_namelen;
813
814         /* Convert Linux syscall to XFS internal ATTR flags */
815         if (!size) {
816                 xflags |= ATTR_KERNOVAL;
817                 data = NULL;
818         }
819         xflags |= namesp->attr_flag;
820         return namesp->attr_get(vp, attr, (void *)data, size, xflags);
821 }
822
823 STATIC ssize_t
824 xfs_vn_listxattr(
825         struct dentry           *dentry,
826         char                    *data,
827         size_t                  size)
828 {
829         bhv_vnode_t             *vp = vn_from_inode(dentry->d_inode);
830         int                     error, xflags = ATTR_KERNAMELS;
831         ssize_t                 result;
832
833         if (!size)
834                 xflags |= ATTR_KERNOVAL;
835         xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
836
837         error = attr_generic_list(vp, data, size, xflags, &result);
838         if (error < 0)
839                 return error;
840         return result;
841 }
842
843 STATIC int
844 xfs_vn_removexattr(
845         struct dentry   *dentry,
846         const char      *name)
847 {
848         bhv_vnode_t     *vp = vn_from_inode(dentry->d_inode);
849         char            *attr = (char *)name;
850         attrnames_t     *namesp;
851         int             xflags = 0;
852
853         namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
854         if (!namesp)
855                 return -EOPNOTSUPP;
856         attr += namesp->attr_namelen;
857
858         xflags |= namesp->attr_flag;
859         return namesp->attr_remove(vp, attr, xflags);
860 }
861
862 STATIC long
863 xfs_vn_fallocate(
864         struct inode    *inode,
865         int             mode,
866         loff_t          offset,
867         loff_t          len)
868 {
869         long            error;
870         loff_t          new_size = 0;
871         xfs_flock64_t   bf;
872         xfs_inode_t     *ip = XFS_I(inode);
873
874         /* preallocation on directories not yet supported */
875         error = -ENODEV;
876         if (S_ISDIR(inode->i_mode))
877                 goto out_error;
878
879         bf.l_whence = 0;
880         bf.l_start = offset;
881         bf.l_len = len;
882
883         xfs_ilock(ip, XFS_IOLOCK_EXCL);
884         error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
885                                                 0, NULL, ATTR_NOLOCK);
886         if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
887             offset + len > i_size_read(inode))
888                 new_size = offset + len;
889
890         /* Change file size if needed */
891         if (new_size) {
892                 bhv_vattr_t     va;
893
894                 va.va_mask = XFS_AT_SIZE;
895                 va.va_size = new_size;
896                 error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL);
897         }
898
899         xfs_iunlock(ip, XFS_IOLOCK_EXCL);
900 out_error:
901         return error;
902 }
903
904 const struct inode_operations xfs_inode_operations = {
905         .permission             = xfs_vn_permission,
906         .truncate               = xfs_vn_truncate,
907         .getattr                = xfs_vn_getattr,
908         .setattr                = xfs_vn_setattr,
909         .setxattr               = xfs_vn_setxattr,
910         .getxattr               = xfs_vn_getxattr,
911         .listxattr              = xfs_vn_listxattr,
912         .removexattr            = xfs_vn_removexattr,
913         .fallocate              = xfs_vn_fallocate,
914 };
915
916 const struct inode_operations xfs_dir_inode_operations = {
917         .create                 = xfs_vn_create,
918         .lookup                 = xfs_vn_lookup,
919         .link                   = xfs_vn_link,
920         .unlink                 = xfs_vn_unlink,
921         .symlink                = xfs_vn_symlink,
922         .mkdir                  = xfs_vn_mkdir,
923         .rmdir                  = xfs_vn_rmdir,
924         .mknod                  = xfs_vn_mknod,
925         .rename                 = xfs_vn_rename,
926         .permission             = xfs_vn_permission,
927         .getattr                = xfs_vn_getattr,
928         .setattr                = xfs_vn_setattr,
929         .setxattr               = xfs_vn_setxattr,
930         .getxattr               = xfs_vn_getxattr,
931         .listxattr              = xfs_vn_listxattr,
932         .removexattr            = xfs_vn_removexattr,
933 };
934
935 const struct inode_operations xfs_dir_ci_inode_operations = {
936         .create                 = xfs_vn_create,
937         .lookup                 = xfs_vn_ci_lookup,
938         .link                   = xfs_vn_link,
939         .unlink                 = xfs_vn_unlink,
940         .symlink                = xfs_vn_symlink,
941         .mkdir                  = xfs_vn_mkdir,
942         .rmdir                  = xfs_vn_rmdir,
943         .mknod                  = xfs_vn_mknod,
944         .rename                 = xfs_vn_rename,
945         .permission             = xfs_vn_permission,
946         .getattr                = xfs_vn_getattr,
947         .setattr                = xfs_vn_setattr,
948         .setxattr               = xfs_vn_setxattr,
949         .getxattr               = xfs_vn_getxattr,
950         .listxattr              = xfs_vn_listxattr,
951         .removexattr            = xfs_vn_removexattr,
952 };
953
954 const struct inode_operations xfs_symlink_inode_operations = {
955         .readlink               = generic_readlink,
956         .follow_link            = xfs_vn_follow_link,
957         .put_link               = xfs_vn_put_link,
958         .permission             = xfs_vn_permission,
959         .getattr                = xfs_vn_getattr,
960         .setattr                = xfs_vn_setattr,
961         .setxattr               = xfs_vn_setxattr,
962         .getxattr               = xfs_vn_getxattr,
963         .listxattr              = xfs_vn_listxattr,
964         .removexattr            = xfs_vn_removexattr,
965 };