This commit was manufactured by cvs2svn to create branch 'fedora'.
[linux-2.6.git] / fs / xfs / linux-2.6 / xfs_iops.c
1 /*
2  * Copyright (c) 2000-2004 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                 nd_set_link(nd, ERR_PTR(-ENOMEM));
424                 return 0;
425         }
426
427         uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
428         if (!uio) {
429                 kfree(link);
430                 nd_set_link(nd, ERR_PTR(-ENOMEM));
431                 return 0;
432         }
433
434         vp = LINVFS_GET_VP(dentry->d_inode);
435
436         iov.iov_base = link;
437         iov.iov_len = MAXNAMELEN;
438
439         uio->uio_iov = &iov;
440         uio->uio_offset = 0;
441         uio->uio_segflg = UIO_SYSSPACE;
442         uio->uio_resid = MAXNAMELEN;
443         uio->uio_iovcnt = 1;
444
445         VOP_READLINK(vp, uio, 0, NULL, error);
446         if (error) {
447                 kfree(link);
448                 link = ERR_PTR(-error);
449         } else {
450                 link[MAXNAMELEN - uio->uio_resid] = '\0';
451         }
452         kfree(uio);
453
454         nd_set_link(nd, link);
455         return 0;
456 }
457
458 static void linvfs_put_link(struct dentry *dentry, struct nameidata *nd)
459 {
460         char *s = nd_get_link(nd);
461         if (!IS_ERR(s))
462                 kfree(s);
463 }
464
465 #ifdef CONFIG_XFS_POSIX_ACL
466 STATIC int
467 linvfs_permission(
468         struct inode    *inode,
469         int             mode,
470         struct nameidata *nd)
471 {
472         vnode_t         *vp = LINVFS_GET_VP(inode);
473         int             error;
474
475         mode <<= 6;             /* convert from linux to vnode access bits */
476         VOP_ACCESS(vp, mode, NULL, error);
477         return -error;
478 }
479 #else
480 #define linvfs_permission NULL
481 #endif
482
483 STATIC int
484 linvfs_getattr(
485         struct vfsmount *mnt,
486         struct dentry   *dentry,
487         struct kstat    *stat)
488 {
489         struct inode    *inode = dentry->d_inode;
490         vnode_t         *vp = LINVFS_GET_VP(inode);
491         int             error = 0;
492
493         if (unlikely(vp->v_flag & VMODIFIED))
494                 error = vn_revalidate(vp);
495         if (!error)
496                 generic_fillattr(inode, stat);
497         return 0;
498 }
499
500 STATIC int
501 linvfs_setattr(
502         struct dentry   *dentry,
503         struct iattr    *attr)
504 {
505         struct inode    *inode = dentry->d_inode;
506         unsigned int    ia_valid = attr->ia_valid;
507         vnode_t         *vp = LINVFS_GET_VP(inode);
508         vattr_t         vattr;
509         int             flags = 0;
510         int             error;
511
512         memset(&vattr, 0, sizeof(vattr_t));
513         if (ia_valid & ATTR_UID) {
514                 vattr.va_mask |= XFS_AT_UID;
515                 vattr.va_uid = attr->ia_uid;
516         }
517         if (ia_valid & ATTR_GID) {
518                 vattr.va_mask |= XFS_AT_GID;
519                 vattr.va_gid = attr->ia_gid;
520         }
521         if (ia_valid & ATTR_SIZE) {
522                 vattr.va_mask |= XFS_AT_SIZE;
523                 vattr.va_size = attr->ia_size;
524         }
525         if (ia_valid & ATTR_ATIME) {
526                 vattr.va_mask |= XFS_AT_ATIME;
527                 vattr.va_atime = attr->ia_atime;
528         }
529         if (ia_valid & ATTR_MTIME) {
530                 vattr.va_mask |= XFS_AT_MTIME;
531                 vattr.va_mtime = attr->ia_mtime;
532         }
533         if (ia_valid & ATTR_CTIME) {
534                 vattr.va_mask |= XFS_AT_CTIME;
535                 vattr.va_ctime = attr->ia_ctime;
536         }
537         if (ia_valid & ATTR_MODE) {
538                 vattr.va_mask |= XFS_AT_MODE;
539                 vattr.va_mode = attr->ia_mode;
540                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
541                         inode->i_mode &= ~S_ISGID;
542         }
543
544         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
545                 flags |= ATTR_UTIME;
546 #ifdef ATTR_NO_BLOCK
547         if ((ia_valid & ATTR_NO_BLOCK))
548                 flags |= ATTR_NONBLOCK;
549 #endif
550
551         VOP_SETATTR(vp, &vattr, flags, NULL, error);
552         if (error)
553                 return -error;
554         vn_revalidate(vp);
555         return error;
556 }
557
558 STATIC void
559 linvfs_truncate(
560         struct inode    *inode)
561 {
562         block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
563 }
564
565 STATIC int
566 linvfs_setxattr(
567         struct dentry   *dentry,
568         const char      *name,
569         const void      *data,
570         size_t          size,
571         int             flags)
572 {
573         vnode_t         *vp = LINVFS_GET_VP(dentry->d_inode);
574         char            *attr = (char *)name;
575         attrnames_t     *namesp;
576         int             xflags = 0;
577         int             error;
578
579         namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
580         if (!namesp)
581                 return -EOPNOTSUPP;
582         attr += namesp->attr_namelen;
583         error = namesp->attr_capable(vp, NULL);
584         if (error)
585                 return error;
586
587         /* Convert Linux syscall to XFS internal ATTR flags */
588         if (flags & XATTR_CREATE)
589                 xflags |= ATTR_CREATE;
590         if (flags & XATTR_REPLACE)
591                 xflags |= ATTR_REPLACE;
592         xflags |= namesp->attr_flag;
593         return namesp->attr_set(vp, attr, (void *)data, size, xflags);
594 }
595
596 STATIC ssize_t
597 linvfs_getxattr(
598         struct dentry   *dentry,
599         const char      *name,
600         void            *data,
601         size_t          size)
602 {
603         vnode_t         *vp = LINVFS_GET_VP(dentry->d_inode);
604         char            *attr = (char *)name;
605         attrnames_t     *namesp;
606         int             xflags = 0;
607         ssize_t         error;
608
609         namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
610         if (!namesp)
611                 return -EOPNOTSUPP;
612         attr += namesp->attr_namelen;
613         error = namesp->attr_capable(vp, NULL);
614         if (error)
615                 return error;
616
617         /* Convert Linux syscall to XFS internal ATTR flags */
618         if (!size) {
619                 xflags |= ATTR_KERNOVAL;
620                 data = NULL;
621         }
622         xflags |= namesp->attr_flag;
623         return namesp->attr_get(vp, attr, (void *)data, size, xflags);
624 }
625
626 STATIC ssize_t
627 linvfs_listxattr(
628         struct dentry           *dentry,
629         char                    *data,
630         size_t                  size)
631 {
632         vnode_t                 *vp = LINVFS_GET_VP(dentry->d_inode);
633         int                     error, xflags = ATTR_KERNAMELS;
634         ssize_t                 result;
635
636         if (!size)
637                 xflags |= ATTR_KERNOVAL;
638         xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
639
640         error = attr_generic_list(vp, data, size, xflags, &result);
641         if (error < 0)
642                 return error;
643         return result;
644 }
645
646 STATIC int
647 linvfs_removexattr(
648         struct dentry   *dentry,
649         const char      *name)
650 {
651         vnode_t         *vp = LINVFS_GET_VP(dentry->d_inode);
652         char            *attr = (char *)name;
653         attrnames_t     *namesp;
654         int             xflags = 0;
655         int             error;
656
657         namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
658         if (!namesp)
659                 return -EOPNOTSUPP;
660         attr += namesp->attr_namelen;
661         error = namesp->attr_capable(vp, NULL);
662         if (error)
663                 return error;
664         xflags |= namesp->attr_flag;
665         return namesp->attr_remove(vp, attr, xflags);
666 }
667
668
669 struct inode_operations linvfs_file_inode_operations = {
670         .permission             = linvfs_permission,
671         .truncate               = linvfs_truncate,
672         .getattr                = linvfs_getattr,
673         .setattr                = linvfs_setattr,
674         .setxattr               = linvfs_setxattr,
675         .getxattr               = linvfs_getxattr,
676         .listxattr              = linvfs_listxattr,
677         .removexattr            = linvfs_removexattr,
678 };
679
680 struct inode_operations linvfs_dir_inode_operations = {
681         .create                 = linvfs_create,
682         .lookup                 = linvfs_lookup,
683         .link                   = linvfs_link,
684         .unlink                 = linvfs_unlink,
685         .symlink                = linvfs_symlink,
686         .mkdir                  = linvfs_mkdir,
687         .rmdir                  = linvfs_rmdir,
688         .mknod                  = linvfs_mknod,
689         .rename                 = linvfs_rename,
690         .permission             = linvfs_permission,
691         .getattr                = linvfs_getattr,
692         .setattr                = linvfs_setattr,
693         .setxattr               = linvfs_setxattr,
694         .getxattr               = linvfs_getxattr,
695         .listxattr              = linvfs_listxattr,
696         .removexattr            = linvfs_removexattr,
697 };
698
699 struct inode_operations linvfs_symlink_inode_operations = {
700         .readlink               = linvfs_readlink,
701         .follow_link            = linvfs_follow_link,
702         .put_link               = linvfs_put_link,
703         .permission             = linvfs_permission,
704         .getattr                = linvfs_getattr,
705         .setattr                = linvfs_setattr,
706         .setxattr               = linvfs_setxattr,
707         .getxattr               = linvfs_getxattr,
708         .listxattr              = linvfs_listxattr,
709         .removexattr            = linvfs_removexattr,
710 };