[XFS] Warn if errors come from block_truncate_page().
[safe/jmp/linux-2.6] / 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         /*
159          * We're not supposed to change timestamps in readonly-mounted
160          * filesystems.  Throw it away if anyone asks us.
161          */
162         if (unlikely(IS_RDONLY(inode)))
163                 return;
164
165         if (flags & XFS_ICHGTIME_MOD) {
166                 tvp = &inode->i_mtime;
167                 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
168                 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
169         }
170         if (flags & XFS_ICHGTIME_CHG) {
171                 tvp = &inode->i_ctime;
172                 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
173                 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
174         }
175
176         /*
177          * We update the i_update_core field _after_ changing
178          * the timestamps in order to coordinate properly with
179          * xfs_iflush() so that we don't lose timestamp updates.
180          * This keeps us from having to hold the inode lock
181          * while doing this.  We use the SYNCHRONIZE macro to
182          * ensure that the compiler does not reorder the update
183          * of i_update_core above the timestamp updates above.
184          */
185         SYNCHRONIZE();
186         ip->i_update_core = 1;
187         if (!(inode->i_state & I_NEW))
188                 mark_inode_dirty_sync(inode);
189 }
190
191
192 /*
193  * Pull the link count and size up from the xfs inode to the linux inode
194  */
195 STATIC void
196 xfs_validate_fields(
197         struct inode            *inode)
198 {
199         struct xfs_inode        *ip = XFS_I(inode);
200         loff_t size;
201
202         /* we're under i_sem so i_size can't change under us */
203         size = XFS_ISIZE(ip);
204         if (i_size_read(inode) != size)
205                 i_size_write(inode, size);
206 }
207
208 /*
209  * Hook in SELinux.  This is not quite correct yet, what we really need
210  * here (as we do for default ACLs) is a mechanism by which creation of
211  * these attrs can be journalled at inode creation time (along with the
212  * inode, of course, such that log replay can't cause these to be lost).
213  */
214 STATIC int
215 xfs_init_security(
216         struct inode    *inode,
217         struct inode    *dir)
218 {
219         struct xfs_inode *ip = XFS_I(inode);
220         size_t          length;
221         void            *value;
222         char            *name;
223         int             error;
224
225         error = security_inode_init_security(inode, dir, &name,
226                                              &value, &length);
227         if (error) {
228                 if (error == -EOPNOTSUPP)
229                         return 0;
230                 return -error;
231         }
232
233         error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
234         if (!error)
235                 xfs_iflags_set(ip, XFS_IMODIFIED);
236
237         kfree(name);
238         kfree(value);
239         return error;
240 }
241
242 STATIC void
243 xfs_cleanup_inode(
244         struct inode    *dir,
245         struct inode    *inode,
246         struct dentry   *dentry,
247         int             mode)
248 {
249         struct dentry   teardown = {};
250
251         /* Oh, the horror.
252          * If we can't add the ACL or we fail in
253          * xfs_init_security we must back out.
254          * ENOSPC can hit here, among other things.
255          */
256         teardown.d_inode = inode;
257         teardown.d_name = dentry->d_name;
258
259         if (S_ISDIR(mode))
260                 xfs_rmdir(XFS_I(dir), &teardown);
261         else
262                 xfs_remove(XFS_I(dir), &teardown);
263         iput(inode);
264 }
265
266 STATIC int
267 xfs_vn_mknod(
268         struct inode    *dir,
269         struct dentry   *dentry,
270         int             mode,
271         dev_t           rdev)
272 {
273         struct inode    *inode;
274         struct xfs_inode *ip = NULL;
275         xfs_acl_t       *default_acl = NULL;
276         attrexists_t    test_default_acl = _ACL_DEFAULT_EXISTS;
277         int             error;
278
279         /*
280          * Irix uses Missed'em'V split, but doesn't want to see
281          * the upper 5 bits of (14bit) major.
282          */
283         if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
284                 return -EINVAL;
285
286         if (test_default_acl && test_default_acl(dir)) {
287                 if (!_ACL_ALLOC(default_acl)) {
288                         return -ENOMEM;
289                 }
290                 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
291                         _ACL_FREE(default_acl);
292                         default_acl = NULL;
293                 }
294         }
295
296         if (IS_POSIXACL(dir) && !default_acl)
297                 mode &= ~current->fs->umask;
298
299         switch (mode & S_IFMT) {
300         case S_IFCHR:
301         case S_IFBLK:
302         case S_IFIFO:
303         case S_IFSOCK:
304                 rdev = sysv_encode_dev(rdev);
305         case S_IFREG:
306                 error = xfs_create(XFS_I(dir), dentry, mode, rdev, &ip, NULL);
307                 break;
308         case S_IFDIR:
309                 error = xfs_mkdir(XFS_I(dir), dentry, mode, &ip, NULL);
310                 break;
311         default:
312                 error = EINVAL;
313                 break;
314         }
315
316         if (unlikely(error))
317                 goto out_free_acl;
318
319         inode = ip->i_vnode;
320
321         error = xfs_init_security(inode, dir);
322         if (unlikely(error))
323                 goto out_cleanup_inode;
324
325         if (default_acl) {
326                 error = _ACL_INHERIT(inode, mode, default_acl);
327                 if (unlikely(error))
328                         goto out_cleanup_inode;
329                 xfs_iflags_set(ip, XFS_IMODIFIED);
330                 _ACL_FREE(default_acl);
331         }
332
333
334         if (S_ISDIR(mode))
335                 xfs_validate_fields(inode);
336         d_instantiate(dentry, inode);
337         xfs_validate_fields(dir);
338         return -error;
339
340  out_cleanup_inode:
341         xfs_cleanup_inode(dir, inode, dentry, mode);
342  out_free_acl:
343         if (default_acl)
344                 _ACL_FREE(default_acl);
345         return -error;
346 }
347
348 STATIC int
349 xfs_vn_create(
350         struct inode    *dir,
351         struct dentry   *dentry,
352         int             mode,
353         struct nameidata *nd)
354 {
355         return xfs_vn_mknod(dir, dentry, mode, 0);
356 }
357
358 STATIC int
359 xfs_vn_mkdir(
360         struct inode    *dir,
361         struct dentry   *dentry,
362         int             mode)
363 {
364         return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
365 }
366
367 STATIC struct dentry *
368 xfs_vn_lookup(
369         struct inode    *dir,
370         struct dentry   *dentry,
371         struct nameidata *nd)
372 {
373         struct xfs_inode *cip;
374         int             error;
375
376         if (dentry->d_name.len >= MAXNAMELEN)
377                 return ERR_PTR(-ENAMETOOLONG);
378
379         error = xfs_lookup(XFS_I(dir), dentry, &cip);
380         if (unlikely(error)) {
381                 if (unlikely(error != ENOENT))
382                         return ERR_PTR(-error);
383                 d_add(dentry, NULL);
384                 return NULL;
385         }
386
387         return d_splice_alias(cip->i_vnode, dentry);
388 }
389
390 STATIC int
391 xfs_vn_link(
392         struct dentry   *old_dentry,
393         struct inode    *dir,
394         struct dentry   *dentry)
395 {
396         struct inode    *inode; /* inode of guy being linked to */
397         int             error;
398
399         inode = old_dentry->d_inode;
400
401         igrab(inode);
402         error = xfs_link(XFS_I(dir), XFS_I(inode), dentry);
403         if (unlikely(error)) {
404                 iput(inode);
405                 return -error;
406         }
407
408         xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
409         xfs_validate_fields(inode);
410         d_instantiate(dentry, inode);
411         return 0;
412 }
413
414 STATIC int
415 xfs_vn_unlink(
416         struct inode    *dir,
417         struct dentry   *dentry)
418 {
419         struct inode    *inode;
420         int             error;
421
422         inode = dentry->d_inode;
423
424         error = xfs_remove(XFS_I(dir), dentry);
425         if (likely(!error)) {
426                 xfs_validate_fields(dir);       /* size needs update */
427                 xfs_validate_fields(inode);
428         }
429         return -error;
430 }
431
432 STATIC int
433 xfs_vn_symlink(
434         struct inode    *dir,
435         struct dentry   *dentry,
436         const char      *symname)
437 {
438         struct inode    *inode;
439         struct xfs_inode *cip = NULL;
440         int             error;
441         mode_t          mode;
442
443         mode = S_IFLNK |
444                 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
445
446         error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode,
447                             &cip, NULL);
448         if (unlikely(error))
449                 goto out;
450
451         inode = cip->i_vnode;
452
453         error = xfs_init_security(inode, dir);
454         if (unlikely(error))
455                 goto out_cleanup_inode;
456
457         d_instantiate(dentry, inode);
458         xfs_validate_fields(dir);
459         xfs_validate_fields(inode);
460         return 0;
461
462  out_cleanup_inode:
463         xfs_cleanup_inode(dir, inode, dentry, 0);
464  out:
465         return -error;
466 }
467
468 STATIC int
469 xfs_vn_rmdir(
470         struct inode    *dir,
471         struct dentry   *dentry)
472 {
473         struct inode    *inode = dentry->d_inode;
474         int             error;
475
476         error = xfs_rmdir(XFS_I(dir), dentry);
477         if (likely(!error)) {
478                 xfs_validate_fields(inode);
479                 xfs_validate_fields(dir);
480         }
481         return -error;
482 }
483
484 STATIC int
485 xfs_vn_rename(
486         struct inode    *odir,
487         struct dentry   *odentry,
488         struct inode    *ndir,
489         struct dentry   *ndentry)
490 {
491         struct inode    *new_inode = ndentry->d_inode;
492         int             error;
493
494         error = xfs_rename(XFS_I(odir), odentry, XFS_I(ndir), ndentry);
495         if (likely(!error)) {
496                 if (new_inode)
497                         xfs_validate_fields(new_inode);
498                 xfs_validate_fields(odir);
499                 if (ndir != odir)
500                         xfs_validate_fields(ndir);
501         }
502         return -error;
503 }
504
505 /*
506  * careful here - this function can get called recursively, so
507  * we need to be very careful about how much stack we use.
508  * uio is kmalloced for this reason...
509  */
510 STATIC void *
511 xfs_vn_follow_link(
512         struct dentry           *dentry,
513         struct nameidata        *nd)
514 {
515         char                    *link;
516         int                     error = -ENOMEM;
517
518         link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
519         if (!link)
520                 goto out_err;
521
522         error = -xfs_readlink(XFS_I(dentry->d_inode), link);
523         if (unlikely(error))
524                 goto out_kfree;
525
526         nd_set_link(nd, link);
527         return NULL;
528
529  out_kfree:
530         kfree(link);
531  out_err:
532         nd_set_link(nd, ERR_PTR(error));
533         return NULL;
534 }
535
536 STATIC void
537 xfs_vn_put_link(
538         struct dentry   *dentry,
539         struct nameidata *nd,
540         void            *p)
541 {
542         char            *s = nd_get_link(nd);
543
544         if (!IS_ERR(s))
545                 kfree(s);
546 }
547
548 #ifdef CONFIG_XFS_POSIX_ACL
549 STATIC int
550 xfs_check_acl(
551         struct inode            *inode,
552         int                     mask)
553 {
554         struct xfs_inode        *ip = XFS_I(inode);
555         int                     error;
556
557         xfs_itrace_entry(ip);
558
559         if (XFS_IFORK_Q(ip)) {
560                 error = xfs_acl_iaccess(ip, mask, NULL);
561                 if (error != -1)
562                         return -error;
563         }
564
565         return -EAGAIN;
566 }
567
568 STATIC int
569 xfs_vn_permission(
570         struct inode            *inode,
571         int                     mask,
572         struct nameidata        *nd)
573 {
574         return generic_permission(inode, mask, xfs_check_acl);
575 }
576 #else
577 #define xfs_vn_permission NULL
578 #endif
579
580 STATIC int
581 xfs_vn_getattr(
582         struct vfsmount         *mnt,
583         struct dentry           *dentry,
584         struct kstat            *stat)
585 {
586         struct inode            *inode = dentry->d_inode;
587         struct xfs_inode        *ip = XFS_I(inode);
588         struct xfs_mount        *mp = ip->i_mount;
589
590         xfs_itrace_entry(ip);
591
592         if (XFS_FORCED_SHUTDOWN(mp))
593                 return XFS_ERROR(EIO);
594
595         stat->size = XFS_ISIZE(ip);
596         stat->dev = inode->i_sb->s_dev;
597         stat->mode = ip->i_d.di_mode;
598         stat->nlink = ip->i_d.di_nlink;
599         stat->uid = ip->i_d.di_uid;
600         stat->gid = ip->i_d.di_gid;
601         stat->ino = ip->i_ino;
602 #if XFS_BIG_INUMS
603         stat->ino += mp->m_inoadd;
604 #endif
605         stat->atime = inode->i_atime;
606         stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
607         stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
608         stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
609         stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
610         stat->blocks =
611                 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
612
613
614         switch (inode->i_mode & S_IFMT) {
615         case S_IFBLK:
616         case S_IFCHR:
617                 stat->blksize = BLKDEV_IOSIZE;
618                 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
619                                    sysv_minor(ip->i_df.if_u2.if_rdev));
620                 break;
621         default:
622                 if (XFS_IS_REALTIME_INODE(ip)) {
623                         /*
624                          * If the file blocks are being allocated from a
625                          * realtime volume, then return the inode's realtime
626                          * extent size or the realtime volume's extent size.
627                          */
628                         stat->blksize =
629                                 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
630                 } else
631                         stat->blksize = xfs_preferred_iosize(mp);
632                 stat->rdev = 0;
633                 break;
634         }
635
636         return 0;
637 }
638
639 STATIC int
640 xfs_vn_setattr(
641         struct dentry   *dentry,
642         struct iattr    *attr)
643 {
644         struct inode    *inode = dentry->d_inode;
645         unsigned int    ia_valid = attr->ia_valid;
646         bhv_vattr_t     vattr = { 0 };
647         int             flags = 0;
648         int             error;
649
650         if (ia_valid & ATTR_UID) {
651                 vattr.va_mask |= XFS_AT_UID;
652                 vattr.va_uid = attr->ia_uid;
653         }
654         if (ia_valid & ATTR_GID) {
655                 vattr.va_mask |= XFS_AT_GID;
656                 vattr.va_gid = attr->ia_gid;
657         }
658         if (ia_valid & ATTR_SIZE) {
659                 vattr.va_mask |= XFS_AT_SIZE;
660                 vattr.va_size = attr->ia_size;
661         }
662         if (ia_valid & ATTR_ATIME) {
663                 vattr.va_mask |= XFS_AT_ATIME;
664                 vattr.va_atime = attr->ia_atime;
665                 inode->i_atime = attr->ia_atime;
666         }
667         if (ia_valid & ATTR_MTIME) {
668                 vattr.va_mask |= XFS_AT_MTIME;
669                 vattr.va_mtime = attr->ia_mtime;
670         }
671         if (ia_valid & ATTR_CTIME) {
672                 vattr.va_mask |= XFS_AT_CTIME;
673                 vattr.va_ctime = attr->ia_ctime;
674         }
675         if (ia_valid & ATTR_MODE) {
676                 vattr.va_mask |= XFS_AT_MODE;
677                 vattr.va_mode = attr->ia_mode;
678                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
679                         inode->i_mode &= ~S_ISGID;
680         }
681
682         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
683                 flags |= ATTR_UTIME;
684 #ifdef ATTR_NO_BLOCK
685         if ((ia_valid & ATTR_NO_BLOCK))
686                 flags |= ATTR_NONBLOCK;
687 #endif
688
689         error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
690         if (likely(!error))
691                 vn_revalidate(vn_from_inode(inode));
692         return -error;
693 }
694
695 /*
696  * block_truncate_page can return an error, but we can't propagate it
697  * at all here. Leave a complaint + stack trace in the syslog because
698  * this could be bad. If it is bad, we need to propagate the error further.
699  */
700 STATIC void
701 xfs_vn_truncate(
702         struct inode    *inode)
703 {
704         int     error;
705         error = block_truncate_page(inode->i_mapping, inode->i_size,
706                                                         xfs_get_blocks);
707         WARN_ON(error);
708 }
709
710 STATIC int
711 xfs_vn_setxattr(
712         struct dentry   *dentry,
713         const char      *name,
714         const void      *data,
715         size_t          size,
716         int             flags)
717 {
718         bhv_vnode_t     *vp = vn_from_inode(dentry->d_inode);
719         char            *attr = (char *)name;
720         attrnames_t     *namesp;
721         int             xflags = 0;
722         int             error;
723
724         namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
725         if (!namesp)
726                 return -EOPNOTSUPP;
727         attr += namesp->attr_namelen;
728         error = namesp->attr_capable(vp, NULL);
729         if (error)
730                 return error;
731
732         /* Convert Linux syscall to XFS internal ATTR flags */
733         if (flags & XATTR_CREATE)
734                 xflags |= ATTR_CREATE;
735         if (flags & XATTR_REPLACE)
736                 xflags |= ATTR_REPLACE;
737         xflags |= namesp->attr_flag;
738         return namesp->attr_set(vp, attr, (void *)data, size, xflags);
739 }
740
741 STATIC ssize_t
742 xfs_vn_getxattr(
743         struct dentry   *dentry,
744         const char      *name,
745         void            *data,
746         size_t          size)
747 {
748         bhv_vnode_t     *vp = vn_from_inode(dentry->d_inode);
749         char            *attr = (char *)name;
750         attrnames_t     *namesp;
751         int             xflags = 0;
752         ssize_t         error;
753
754         namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
755         if (!namesp)
756                 return -EOPNOTSUPP;
757         attr += namesp->attr_namelen;
758         error = namesp->attr_capable(vp, NULL);
759         if (error)
760                 return error;
761
762         /* Convert Linux syscall to XFS internal ATTR flags */
763         if (!size) {
764                 xflags |= ATTR_KERNOVAL;
765                 data = NULL;
766         }
767         xflags |= namesp->attr_flag;
768         return namesp->attr_get(vp, attr, (void *)data, size, xflags);
769 }
770
771 STATIC ssize_t
772 xfs_vn_listxattr(
773         struct dentry           *dentry,
774         char                    *data,
775         size_t                  size)
776 {
777         bhv_vnode_t             *vp = vn_from_inode(dentry->d_inode);
778         int                     error, xflags = ATTR_KERNAMELS;
779         ssize_t                 result;
780
781         if (!size)
782                 xflags |= ATTR_KERNOVAL;
783         xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
784
785         error = attr_generic_list(vp, data, size, xflags, &result);
786         if (error < 0)
787                 return error;
788         return result;
789 }
790
791 STATIC int
792 xfs_vn_removexattr(
793         struct dentry   *dentry,
794         const char      *name)
795 {
796         bhv_vnode_t     *vp = vn_from_inode(dentry->d_inode);
797         char            *attr = (char *)name;
798         attrnames_t     *namesp;
799         int             xflags = 0;
800         int             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         xflags |= namesp->attr_flag;
810         return namesp->attr_remove(vp, attr, xflags);
811 }
812
813 STATIC long
814 xfs_vn_fallocate(
815         struct inode    *inode,
816         int             mode,
817         loff_t          offset,
818         loff_t          len)
819 {
820         long            error;
821         loff_t          new_size = 0;
822         xfs_flock64_t   bf;
823         xfs_inode_t     *ip = XFS_I(inode);
824
825         /* preallocation on directories not yet supported */
826         error = -ENODEV;
827         if (S_ISDIR(inode->i_mode))
828                 goto out_error;
829
830         bf.l_whence = 0;
831         bf.l_start = offset;
832         bf.l_len = len;
833
834         xfs_ilock(ip, XFS_IOLOCK_EXCL);
835         error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
836                                                 0, NULL, ATTR_NOLOCK);
837         if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
838             offset + len > i_size_read(inode))
839                 new_size = offset + len;
840
841         /* Change file size if needed */
842         if (new_size) {
843                 bhv_vattr_t     va;
844
845                 va.va_mask = XFS_AT_SIZE;
846                 va.va_size = new_size;
847                 error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL);
848         }
849
850         xfs_iunlock(ip, XFS_IOLOCK_EXCL);
851 out_error:
852         return error;
853 }
854
855 const struct inode_operations xfs_inode_operations = {
856         .permission             = xfs_vn_permission,
857         .truncate               = xfs_vn_truncate,
858         .getattr                = xfs_vn_getattr,
859         .setattr                = xfs_vn_setattr,
860         .setxattr               = xfs_vn_setxattr,
861         .getxattr               = xfs_vn_getxattr,
862         .listxattr              = xfs_vn_listxattr,
863         .removexattr            = xfs_vn_removexattr,
864         .fallocate              = xfs_vn_fallocate,
865 };
866
867 const struct inode_operations xfs_dir_inode_operations = {
868         .create                 = xfs_vn_create,
869         .lookup                 = xfs_vn_lookup,
870         .link                   = xfs_vn_link,
871         .unlink                 = xfs_vn_unlink,
872         .symlink                = xfs_vn_symlink,
873         .mkdir                  = xfs_vn_mkdir,
874         .rmdir                  = xfs_vn_rmdir,
875         .mknod                  = xfs_vn_mknod,
876         .rename                 = xfs_vn_rename,
877         .permission             = xfs_vn_permission,
878         .getattr                = xfs_vn_getattr,
879         .setattr                = xfs_vn_setattr,
880         .setxattr               = xfs_vn_setxattr,
881         .getxattr               = xfs_vn_getxattr,
882         .listxattr              = xfs_vn_listxattr,
883         .removexattr            = xfs_vn_removexattr,
884 };
885
886 const struct inode_operations xfs_symlink_inode_operations = {
887         .readlink               = generic_readlink,
888         .follow_link            = xfs_vn_follow_link,
889         .put_link               = xfs_vn_put_link,
890         .permission             = xfs_vn_permission,
891         .getattr                = xfs_vn_getattr,
892         .setattr                = xfs_vn_setattr,
893         .setxattr               = xfs_vn_setxattr,
894         .getxattr               = xfs_vn_getxattr,
895         .listxattr              = xfs_vn_listxattr,
896         .removexattr            = xfs_vn_removexattr,
897 };