This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / xfs / linux / xfs_iops.c
1 /*
2  * Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 #include "xfs.h"
34 #include "xfs_fs.h"
35 #include "xfs_inum.h"
36 #include "xfs_log.h"
37 #include "xfs_trans.h"
38 #include "xfs_sb.h"
39 #include "xfs_ag.h"
40 #include "xfs_dir.h"
41 #include "xfs_dir2.h"
42 #include "xfs_alloc.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_quota.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode.h"
56 #include "xfs_bmap.h"
57 #include "xfs_bit.h"
58 #include "xfs_rtalloc.h"
59 #include "xfs_error.h"
60 #include "xfs_itable.h"
61 #include "xfs_rw.h"
62 #include "xfs_acl.h"
63 #include "xfs_cap.h"
64 #include "xfs_mac.h"
65 #include "xfs_attr.h"
66 #include "xfs_buf_item.h"
67 #include "xfs_utils.h"
68
69 #include <linux/xattr.h>
70
71
72 /*
73  * Pull the link count and size up from the xfs inode to the linux inode
74  */
75 STATIC void
76 validate_fields(
77         struct inode    *ip)
78 {
79         vnode_t         *vp = LINVFS_GET_VP(ip);
80         vattr_t         va;
81         int             error;
82
83         va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
84         VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
85         if (likely(!error)) {
86                 ip->i_nlink = va.va_nlink;
87                 ip->i_blocks = va.va_nblocks;
88
89                 /* we're under i_sem so i_size can't change under us */
90                 if (i_size_read(ip) != va.va_size)
91                         i_size_write(ip, va.va_size);
92         }
93 }
94
95 /*
96  * Determine whether a process has a valid fs_struct (kernel daemons
97  * like knfsd don't have an fs_struct).
98  *
99  * XXX(hch):  nfsd is broken, better fix it instead.
100  */
101 STATIC inline int
102 has_fs_struct(struct task_struct *task)
103 {
104         return (task->fs != init_task.fs);
105 }
106
107 STATIC int
108 linvfs_mknod(
109         struct inode    *dir,
110         struct dentry   *dentry,
111         int             mode,
112         dev_t           rdev)
113 {
114         struct inode    *ip;
115         vattr_t         va;
116         vnode_t         *vp = NULL, *dvp = LINVFS_GET_VP(dir);
117         xfs_acl_t       *default_acl = NULL;
118         attrexists_t    test_default_acl = _ACL_DEFAULT_EXISTS;
119         int             error;
120
121         /*
122          * Irix uses Missed'em'V split, but doesn't want to see
123          * the upper 5 bits of (14bit) major.
124          */
125         if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
126                 return -EINVAL;
127
128         if (test_default_acl && test_default_acl(dvp)) {
129                 if (!_ACL_ALLOC(default_acl))
130                         return -ENOMEM;
131                 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
132                         _ACL_FREE(default_acl);
133                         default_acl = NULL;
134                 }
135         }
136
137         if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
138                 mode &= ~current->fs->umask;
139
140         memset(&va, 0, sizeof(va));
141         va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
142         va.va_type = IFTOVT(mode);
143         va.va_mode = mode;
144
145         switch (mode & S_IFMT) {
146         case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
147                 va.va_rdev = sysv_encode_dev(rdev);
148                 va.va_mask |= XFS_AT_RDEV;
149                 /*FALLTHROUGH*/
150         case S_IFREG:
151                 VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
152                 break;
153         case S_IFDIR:
154                 VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
155                 break;
156         default:
157                 error = EINVAL;
158                 break;
159         }
160
161         if (default_acl) {
162                 if (!error) {
163                         error = _ACL_INHERIT(vp, &va, default_acl);
164                         if (!error) {
165                                 VMODIFY(vp);
166                         } else {
167                                 struct dentry   teardown = {};
168                                 int             err2;
169
170                                 /* Oh, the horror.
171                                  * If we can't add the ACL we must back out.
172                                  * ENOSPC can hit here, among other things.
173                                  */
174                                 teardown.d_inode = ip = LINVFS_GET_IP(vp);
175                                 teardown.d_name = dentry->d_name;
176                                 remove_inode_hash(ip);
177                                 make_bad_inode(ip);
178                                 if (S_ISDIR(mode))
179                                         VOP_RMDIR(dvp, &teardown, NULL, err2);
180                                 else
181                                         VOP_REMOVE(dvp, &teardown, NULL, err2);
182                                 VN_RELE(vp);
183                         }
184                 }
185                 _ACL_FREE(default_acl);
186         }
187
188         if (!error) {
189                 ASSERT(vp);
190                 ip = LINVFS_GET_IP(vp);
191
192                 if (S_ISCHR(mode) || S_ISBLK(mode))
193                         ip->i_rdev = rdev;
194                 else if (S_ISDIR(mode))
195                         validate_fields(ip);
196                 d_instantiate(dentry, ip);
197                 validate_fields(dir);
198         }
199         return -error;
200 }
201
202 STATIC int
203 linvfs_create(
204         struct inode    *dir,
205         struct dentry   *dentry,
206         int             mode,
207         struct nameidata *nd)
208 {
209         return linvfs_mknod(dir, dentry, mode, 0);
210 }
211
212 STATIC int
213 linvfs_mkdir(
214         struct inode    *dir,
215         struct dentry   *dentry,
216         int             mode)
217 {
218         return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
219 }
220
221 STATIC struct dentry *
222 linvfs_lookup(
223         struct inode    *dir,
224         struct dentry   *dentry,
225         struct nameidata *nd)
226 {
227         struct inode    *ip = NULL;
228         vnode_t         *vp, *cvp = NULL;
229         int             error;
230
231         if (dentry->d_name.len >= MAXNAMELEN)
232                 return ERR_PTR(-ENAMETOOLONG);
233
234         vp = LINVFS_GET_VP(dir);
235         VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
236         if (!error) {
237                 ASSERT(cvp);
238                 ip = LINVFS_GET_IP(cvp);
239                 if (!ip) {
240                         VN_RELE(cvp);
241                         return ERR_PTR(-EACCES);
242                 }
243         }
244         if (error && (error != ENOENT))
245                 return ERR_PTR(-error);
246         return d_splice_alias(ip, dentry);
247 }
248
249 STATIC int
250 linvfs_link(
251         struct dentry   *old_dentry,
252         struct inode    *dir,
253         struct dentry   *dentry)
254 {
255         struct inode    *ip;    /* inode of guy being linked to */
256         vnode_t         *tdvp;  /* target directory for new name/link */
257         vnode_t         *vp;    /* vp of name being linked */
258         int             error;
259
260         ip = old_dentry->d_inode;       /* inode being linked to */
261         if (S_ISDIR(ip->i_mode))
262                 return -EPERM;
263
264         tdvp = LINVFS_GET_VP(dir);
265         vp = LINVFS_GET_VP(ip);
266
267         VOP_LINK(tdvp, vp, dentry, NULL, error);
268         if (!error) {
269                 VMODIFY(tdvp);
270                 VN_HOLD(vp);
271                 validate_fields(ip);
272                 d_instantiate(dentry, ip);
273         }
274         return -error;
275 }
276
277 STATIC int
278 linvfs_unlink(
279         struct inode    *dir,
280         struct dentry   *dentry)
281 {
282         struct inode    *inode;
283         vnode_t         *dvp;   /* directory containing name to remove */
284         int             error;
285
286         inode = dentry->d_inode;
287         dvp = LINVFS_GET_VP(dir);
288
289         VOP_REMOVE(dvp, dentry, NULL, error);
290         if (!error) {
291                 validate_fields(dir);   /* For size only */
292                 validate_fields(inode);
293         }
294
295         return -error;
296 }
297
298 STATIC int
299 linvfs_symlink(
300         struct inode    *dir,
301         struct dentry   *dentry,
302         const char      *symname)
303 {
304         struct inode    *ip;
305         vattr_t         va;
306         vnode_t         *dvp;   /* directory containing name to remove */
307         vnode_t         *cvp;   /* used to lookup symlink to put in dentry */
308         int             error;
309
310         dvp = LINVFS_GET_VP(dir);
311         cvp = NULL;
312
313         memset(&va, 0, sizeof(va));
314         va.va_type = VLNK;
315         va.va_mode = irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO;
316         va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
317
318         error = 0;
319         VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
320         if (!error && cvp) {
321                 ASSERT(cvp->v_type == VLNK);
322                 ip = LINVFS_GET_IP(cvp);
323                 d_instantiate(dentry, ip);
324                 validate_fields(dir);
325                 validate_fields(ip); /* size needs update */
326         }
327         return -error;
328 }
329
330 STATIC int
331 linvfs_rmdir(
332         struct inode    *dir,
333         struct dentry   *dentry)
334 {
335         struct inode    *inode = dentry->d_inode;
336         vnode_t         *dvp = LINVFS_GET_VP(dir);
337         int             error;
338
339         VOP_RMDIR(dvp, dentry, NULL, error);
340         if (!error) {
341                 validate_fields(inode);
342                 validate_fields(dir);
343         }
344         return -error;
345 }
346
347 STATIC int
348 linvfs_rename(
349         struct inode    *odir,
350         struct dentry   *odentry,
351         struct inode    *ndir,
352         struct dentry   *ndentry)
353 {
354         struct inode    *new_inode = ndentry->d_inode;
355         vnode_t         *fvp;   /* from directory */
356         vnode_t         *tvp;   /* target directory */
357         int             error;
358
359         fvp = LINVFS_GET_VP(odir);
360         tvp = LINVFS_GET_VP(ndir);
361
362         VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
363         if (error)
364                 return -error;
365
366         if (new_inode)
367                 validate_fields(new_inode);
368
369         validate_fields(odir);
370         if (ndir != odir)
371                 validate_fields(ndir);
372         return 0;
373 }
374
375 STATIC int
376 linvfs_readlink(
377         struct dentry   *dentry,
378         char            *buf,
379         int             size)
380 {
381         vnode_t         *vp = LINVFS_GET_VP(dentry->d_inode);
382         uio_t           uio;
383         iovec_t         iov;
384         int             error;
385
386         iov.iov_base = buf;
387         iov.iov_len = size;
388
389         uio.uio_iov = &iov;
390         uio.uio_offset = 0;
391         uio.uio_segflg = UIO_USERSPACE;
392         uio.uio_resid = size;
393         uio.uio_iovcnt = 1;
394
395         VOP_READLINK(vp, &uio, 0, NULL, error);
396         if (error)
397                 return -error;
398
399         return (size - uio.uio_resid);
400 }
401
402 /*
403  * careful here - this function can get called recursively, so
404  * we need to be very careful about how much stack we use.
405  * uio is kmalloced for this reason...
406  */
407 STATIC int
408 linvfs_follow_link(
409         struct dentry           *dentry,
410         struct nameidata        *nd)
411 {
412         vnode_t                 *vp;
413         uio_t                   *uio;
414         iovec_t                 iov;
415         int                     error;
416         char                    *link;
417
418         ASSERT(dentry);
419         ASSERT(nd);
420
421         link = (char *)kmalloc(MAXNAMELEN+1, GFP_KERNEL);
422         if (!link)
423                 return -ENOMEM;
424
425         uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
426         if (!uio) {
427                 kfree(link);
428                 return -ENOMEM;
429         }
430
431         vp = LINVFS_GET_VP(dentry->d_inode);
432
433         iov.iov_base = link;
434         iov.iov_len = MAXNAMELEN;
435
436         uio->uio_iov = &iov;
437         uio->uio_offset = 0;
438         uio->uio_segflg = UIO_SYSSPACE;
439         uio->uio_resid = MAXNAMELEN;
440         uio->uio_iovcnt = 1;
441
442         VOP_READLINK(vp, uio, 0, NULL, error);
443         if (error) {
444                 kfree(uio);
445                 kfree(link);
446                 return -error;
447         }
448
449         link[MAXNAMELEN - uio->uio_resid] = '\0';
450         kfree(uio);
451
452         /* vfs_follow_link returns (-) errors */
453         error = vfs_follow_link(nd, link);
454         kfree(link);
455         return error;
456 }
457
458 #ifdef CONFIG_XFS_POSIX_ACL
459 STATIC int
460 linvfs_permission(
461         struct inode    *inode,
462         int             mode,
463         struct nameidata *nd)
464 {
465         vnode_t         *vp = LINVFS_GET_VP(inode);
466         int             error;
467
468         mode <<= 6;             /* convert from linux to vnode access bits */
469         VOP_ACCESS(vp, mode, NULL, error);
470         return -error;
471 }
472 #else
473 #define linvfs_permission NULL
474 #endif
475
476 STATIC int
477 linvfs_getattr(
478         struct vfsmount *mnt,
479         struct dentry   *dentry,
480         struct kstat    *stat)
481 {
482         struct inode    *inode = dentry->d_inode;
483         vnode_t         *vp = LINVFS_GET_VP(inode);
484         int             error = 0;
485
486         if (unlikely(vp->v_flag & VMODIFIED))
487                 error = vn_revalidate(vp);
488         if (!error)
489                 generic_fillattr(inode, stat);
490         return 0;
491 }
492
493 STATIC int
494 linvfs_setattr(
495         struct dentry   *dentry,
496         struct iattr    *attr)
497 {
498         struct inode    *inode = dentry->d_inode;
499         unsigned int    ia_valid = attr->ia_valid;
500         vnode_t         *vp = LINVFS_GET_VP(inode);
501         vattr_t         vattr;
502         int             flags = 0;
503         int             error;
504
505         memset(&vattr, 0, sizeof(vattr_t));
506         if (ia_valid & ATTR_UID) {
507                 vattr.va_mask |= XFS_AT_UID;
508                 vattr.va_uid = attr->ia_uid;
509         }
510         if (ia_valid & ATTR_GID) {
511                 vattr.va_mask |= XFS_AT_GID;
512                 vattr.va_gid = attr->ia_gid;
513         }
514         if (ia_valid & ATTR_SIZE) {
515                 vattr.va_mask |= XFS_AT_SIZE;
516                 vattr.va_size = attr->ia_size;
517         }
518         if (ia_valid & ATTR_ATIME) {
519                 vattr.va_mask |= XFS_AT_ATIME;
520                 vattr.va_atime = attr->ia_atime;
521         }
522         if (ia_valid & ATTR_MTIME) {
523                 vattr.va_mask |= XFS_AT_MTIME;
524                 vattr.va_mtime = attr->ia_mtime;
525         }
526         if (ia_valid & ATTR_CTIME) {
527                 vattr.va_mask |= XFS_AT_CTIME;
528                 vattr.va_ctime = attr->ia_ctime;
529         }
530         if (ia_valid & ATTR_MODE) {
531                 vattr.va_mask |= XFS_AT_MODE;
532                 vattr.va_mode = attr->ia_mode;
533                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
534                         inode->i_mode &= ~S_ISGID;
535         }
536
537         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
538                 flags = ATTR_UTIME;
539 #ifdef ATTR_NO_BLOCK
540         if ((ia_valid & ATTR_NO_BLOCK))
541                 flags |= ATTR_NONBLOCK;
542 #endif
543
544         VOP_SETATTR(vp, &vattr, flags, NULL, error);
545         if (error)
546                 return(-error); /* Positive error up from XFS */
547         if (ia_valid & ATTR_SIZE) {
548                 error = vmtruncate(inode, attr->ia_size);
549         }
550
551         if (!error) {
552                 vn_revalidate(vp);
553         }
554         return error;
555 }
556
557 STATIC void
558 linvfs_truncate(
559         struct inode    *inode)
560 {
561         block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
562 }
563
564 STATIC int
565 linvfs_setxattr(
566         struct dentry   *dentry,
567         const char      *name,
568         const void      *data,
569         size_t          size,
570         int             flags)
571 {
572         vnode_t         *vp = LINVFS_GET_VP(dentry->d_inode);
573         char            *attr = (char *)name;
574         attrnames_t     *namesp;
575         int             xflags = 0;
576         int             error;
577
578         namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
579         if (!namesp)
580                 return -EOPNOTSUPP;
581         attr += namesp->attr_namelen;
582         error = namesp->attr_capable(vp, NULL);
583         if (error)
584                 return error;
585
586         /* Convert Linux syscall to XFS internal ATTR flags */
587         if (flags & XATTR_CREATE)
588                 xflags |= ATTR_CREATE;
589         if (flags & XATTR_REPLACE)
590                 xflags |= ATTR_REPLACE;
591         xflags |= namesp->attr_flag;
592         return namesp->attr_set(vp, attr, (void *)data, size, xflags);
593 }
594
595 STATIC ssize_t
596 linvfs_getxattr(
597         struct dentry   *dentry,
598         const char      *name,
599         void            *data,
600         size_t          size)
601 {
602         vnode_t         *vp = LINVFS_GET_VP(dentry->d_inode);
603         char            *attr = (char *)name;
604         attrnames_t     *namesp;
605         int             xflags = 0;
606         ssize_t         error;
607
608         namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
609         if (!namesp)
610                 return -EOPNOTSUPP;
611         attr += namesp->attr_namelen;
612         error = namesp->attr_capable(vp, NULL);
613         if (error)
614                 return error;
615
616         /* Convert Linux syscall to XFS internal ATTR flags */
617         if (!size) {
618                 xflags |= ATTR_KERNOVAL;
619                 data = NULL;
620         }
621         xflags |= namesp->attr_flag;
622         return namesp->attr_get(vp, attr, (void *)data, size, xflags);
623 }
624
625 STATIC ssize_t
626 linvfs_listxattr(
627         struct dentry           *dentry,
628         char                    *data,
629         size_t                  size)
630 {
631         vnode_t                 *vp = LINVFS_GET_VP(dentry->d_inode);
632         int                     error, xflags = ATTR_KERNAMELS;
633         ssize_t                 result;
634
635         if (!size)
636                 xflags |= ATTR_KERNOVAL;
637         xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
638
639         error = attr_generic_list(vp, data, size, xflags, &result);
640         if (error < 0)
641                 return error;
642         return result;
643 }
644
645 STATIC int
646 linvfs_removexattr(
647         struct dentry   *dentry,
648         const char      *name)
649 {
650         vnode_t         *vp = LINVFS_GET_VP(dentry->d_inode);
651         char            *attr = (char *)name;
652         attrnames_t     *namesp;
653         int             xflags = 0;
654         int             error;
655
656         namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
657         if (!namesp)
658                 return -EOPNOTSUPP;
659         attr += namesp->attr_namelen;
660         error = namesp->attr_capable(vp, NULL);
661         if (error)
662                 return error;
663         xflags |= namesp->attr_flag;
664         return namesp->attr_remove(vp, attr, xflags);
665 }
666
667
668 struct inode_operations linvfs_file_inode_operations = {
669         .permission             = linvfs_permission,
670         .truncate               = linvfs_truncate,
671         .getattr                = linvfs_getattr,
672         .setattr                = linvfs_setattr,
673         .setxattr               = linvfs_setxattr,
674         .getxattr               = linvfs_getxattr,
675         .listxattr              = linvfs_listxattr,
676         .removexattr            = linvfs_removexattr,
677 };
678
679 struct inode_operations linvfs_dir_inode_operations = {
680         .create                 = linvfs_create,
681         .lookup                 = linvfs_lookup,
682         .link                   = linvfs_link,
683         .unlink                 = linvfs_unlink,
684         .symlink                = linvfs_symlink,
685         .mkdir                  = linvfs_mkdir,
686         .rmdir                  = linvfs_rmdir,
687         .mknod                  = linvfs_mknod,
688         .rename                 = linvfs_rename,
689         .permission             = linvfs_permission,
690         .getattr                = linvfs_getattr,
691         .setattr                = linvfs_setattr,
692         .setxattr               = linvfs_setxattr,
693         .getxattr               = linvfs_getxattr,
694         .listxattr              = linvfs_listxattr,
695         .removexattr            = linvfs_removexattr,
696 };
697
698 struct inode_operations linvfs_symlink_inode_operations = {
699         .readlink               = linvfs_readlink,
700         .follow_link            = linvfs_follow_link,
701         .permission             = linvfs_permission,
702         .getattr                = linvfs_getattr,
703         .setattr                = linvfs_setattr,
704         .setxattr               = linvfs_setxattr,
705         .getxattr               = linvfs_getxattr,
706         .listxattr              = linvfs_listxattr,
707         .removexattr            = linvfs_removexattr,
708 };