[XFS] remove struct vnode::v_type
[safe/jmp/linux-2.6] / 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 #include <linux/namei.h>
71
72
73 /*
74  * Pull the link count and size up from the xfs inode to the linux inode
75  */
76 STATIC void
77 validate_fields(
78         struct inode    *ip)
79 {
80         vnode_t         *vp = LINVFS_GET_VP(ip);
81         vattr_t         va;
82         int             error;
83
84         va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
85         VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
86         if (likely(!error)) {
87                 ip->i_nlink = va.va_nlink;
88                 ip->i_blocks = va.va_nblocks;
89
90                 /* we're under i_sem so i_size can't change under us */
91                 if (i_size_read(ip) != va.va_size)
92                         i_size_write(ip, va.va_size);
93         }
94 }
95
96 /*
97  * Determine whether a process has a valid fs_struct (kernel daemons
98  * like knfsd don't have an fs_struct).
99  *
100  * XXX(hch):  nfsd is broken, better fix it instead.
101  */
102 STATIC inline int
103 has_fs_struct(struct task_struct *task)
104 {
105         return (task->fs != init_task.fs);
106 }
107
108 STATIC int
109 linvfs_mknod(
110         struct inode    *dir,
111         struct dentry   *dentry,
112         int             mode,
113         dev_t           rdev)
114 {
115         struct inode    *ip;
116         vattr_t         va;
117         vnode_t         *vp = NULL, *dvp = LINVFS_GET_VP(dir);
118         xfs_acl_t       *default_acl = NULL;
119         attrexists_t    test_default_acl = _ACL_DEFAULT_EXISTS;
120         int             error;
121
122         /*
123          * Irix uses Missed'em'V split, but doesn't want to see
124          * the upper 5 bits of (14bit) major.
125          */
126         if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
127                 return -EINVAL;
128
129         if (test_default_acl && test_default_acl(dvp)) {
130                 if (!_ACL_ALLOC(default_acl))
131                         return -ENOMEM;
132                 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
133                         _ACL_FREE(default_acl);
134                         default_acl = NULL;
135                 }
136         }
137
138         if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
139                 mode &= ~current->fs->umask;
140
141         memset(&va, 0, sizeof(va));
142         va.va_mask = XFS_AT_TYPE|XFS_AT_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
177                                 vn_mark_bad(vp);
178                                 
179                                 if (S_ISDIR(mode))
180                                         VOP_RMDIR(dvp, &teardown, NULL, err2);
181                                 else
182                                         VOP_REMOVE(dvp, &teardown, NULL, err2);
183                                 VN_RELE(vp);
184                         }
185                 }
186                 _ACL_FREE(default_acl);
187         }
188
189         if (!error) {
190                 ASSERT(vp);
191                 ip = LINVFS_GET_IP(vp);
192
193                 if (S_ISCHR(mode) || S_ISBLK(mode))
194                         ip->i_rdev = rdev;
195                 else if (S_ISDIR(mode))
196                         validate_fields(ip);
197                 d_instantiate(dentry, ip);
198                 validate_fields(dir);
199         }
200         return -error;
201 }
202
203 STATIC int
204 linvfs_create(
205         struct inode    *dir,
206         struct dentry   *dentry,
207         int             mode,
208         struct nameidata *nd)
209 {
210         return linvfs_mknod(dir, dentry, mode, 0);
211 }
212
213 STATIC int
214 linvfs_mkdir(
215         struct inode    *dir,
216         struct dentry   *dentry,
217         int             mode)
218 {
219         return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
220 }
221
222 STATIC struct dentry *
223 linvfs_lookup(
224         struct inode    *dir,
225         struct dentry   *dentry,
226         struct nameidata *nd)
227 {
228         struct vnode    *vp = LINVFS_GET_VP(dir), *cvp;
229         int             error;
230
231         if (dentry->d_name.len >= MAXNAMELEN)
232                 return ERR_PTR(-ENAMETOOLONG);
233
234         VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
235         if (error) {
236                 if (unlikely(error != ENOENT))
237                         return ERR_PTR(-error);
238                 d_add(dentry, NULL);
239                 return NULL;
240         }
241
242         return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
243 }
244
245 STATIC int
246 linvfs_link(
247         struct dentry   *old_dentry,
248         struct inode    *dir,
249         struct dentry   *dentry)
250 {
251         struct inode    *ip;    /* inode of guy being linked to */
252         vnode_t         *tdvp;  /* target directory for new name/link */
253         vnode_t         *vp;    /* vp of name being linked */
254         int             error;
255
256         ip = old_dentry->d_inode;       /* inode being linked to */
257         if (S_ISDIR(ip->i_mode))
258                 return -EPERM;
259
260         tdvp = LINVFS_GET_VP(dir);
261         vp = LINVFS_GET_VP(ip);
262
263         VOP_LINK(tdvp, vp, dentry, NULL, error);
264         if (!error) {
265                 VMODIFY(tdvp);
266                 VN_HOLD(vp);
267                 validate_fields(ip);
268                 d_instantiate(dentry, ip);
269         }
270         return -error;
271 }
272
273 STATIC int
274 linvfs_unlink(
275         struct inode    *dir,
276         struct dentry   *dentry)
277 {
278         struct inode    *inode;
279         vnode_t         *dvp;   /* directory containing name to remove */
280         int             error;
281
282         inode = dentry->d_inode;
283         dvp = LINVFS_GET_VP(dir);
284
285         VOP_REMOVE(dvp, dentry, NULL, error);
286         if (!error) {
287                 validate_fields(dir);   /* For size only */
288                 validate_fields(inode);
289         }
290
291         return -error;
292 }
293
294 STATIC int
295 linvfs_symlink(
296         struct inode    *dir,
297         struct dentry   *dentry,
298         const char      *symname)
299 {
300         struct inode    *ip;
301         vattr_t         va;
302         vnode_t         *dvp;   /* directory containing name of symlink */
303         vnode_t         *cvp;   /* used to lookup symlink to put in dentry */
304         int             error;
305
306         dvp = LINVFS_GET_VP(dir);
307         cvp = NULL;
308
309         memset(&va, 0, sizeof(va));
310         va.va_mode = S_IFLNK |
311                 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
312         va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
313
314         error = 0;
315         VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
316         if (!error && cvp) {
317                 ip = LINVFS_GET_IP(cvp);
318                 d_instantiate(dentry, ip);
319                 validate_fields(dir);
320                 validate_fields(ip); /* size needs update */
321         }
322         return -error;
323 }
324
325 STATIC int
326 linvfs_rmdir(
327         struct inode    *dir,
328         struct dentry   *dentry)
329 {
330         struct inode    *inode = dentry->d_inode;
331         vnode_t         *dvp = LINVFS_GET_VP(dir);
332         int             error;
333
334         VOP_RMDIR(dvp, dentry, NULL, error);
335         if (!error) {
336                 validate_fields(inode);
337                 validate_fields(dir);
338         }
339         return -error;
340 }
341
342 STATIC int
343 linvfs_rename(
344         struct inode    *odir,
345         struct dentry   *odentry,
346         struct inode    *ndir,
347         struct dentry   *ndentry)
348 {
349         struct inode    *new_inode = ndentry->d_inode;
350         vnode_t         *fvp;   /* from directory */
351         vnode_t         *tvp;   /* target directory */
352         int             error;
353
354         fvp = LINVFS_GET_VP(odir);
355         tvp = LINVFS_GET_VP(ndir);
356
357         VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
358         if (error)
359                 return -error;
360
361         if (new_inode)
362                 validate_fields(new_inode);
363
364         validate_fields(odir);
365         if (ndir != odir)
366                 validate_fields(ndir);
367         return 0;
368 }
369
370 /*
371  * careful here - this function can get called recursively, so
372  * we need to be very careful about how much stack we use.
373  * uio is kmalloced for this reason...
374  */
375 STATIC void *
376 linvfs_follow_link(
377         struct dentry           *dentry,
378         struct nameidata        *nd)
379 {
380         vnode_t                 *vp;
381         uio_t                   *uio;
382         iovec_t                 iov;
383         int                     error;
384         char                    *link;
385
386         ASSERT(dentry);
387         ASSERT(nd);
388
389         link = (char *)kmalloc(MAXNAMELEN+1, GFP_KERNEL);
390         if (!link) {
391                 nd_set_link(nd, ERR_PTR(-ENOMEM));
392                 return NULL;
393         }
394
395         uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
396         if (!uio) {
397                 kfree(link);
398                 nd_set_link(nd, ERR_PTR(-ENOMEM));
399                 return NULL;
400         }
401
402         vp = LINVFS_GET_VP(dentry->d_inode);
403
404         iov.iov_base = link;
405         iov.iov_len = MAXNAMELEN;
406
407         uio->uio_iov = &iov;
408         uio->uio_offset = 0;
409         uio->uio_segflg = UIO_SYSSPACE;
410         uio->uio_resid = MAXNAMELEN;
411         uio->uio_iovcnt = 1;
412
413         VOP_READLINK(vp, uio, 0, NULL, error);
414         if (error) {
415                 kfree(link);
416                 link = ERR_PTR(-error);
417         } else {
418                 link[MAXNAMELEN - uio->uio_resid] = '\0';
419         }
420         kfree(uio);
421
422         nd_set_link(nd, link);
423         return NULL;
424 }
425
426 static void linvfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
427 {
428         char *s = nd_get_link(nd);
429         if (!IS_ERR(s))
430                 kfree(s);
431 }
432
433 #ifdef CONFIG_XFS_POSIX_ACL
434 STATIC int
435 linvfs_permission(
436         struct inode    *inode,
437         int             mode,
438         struct nameidata *nd)
439 {
440         vnode_t         *vp = LINVFS_GET_VP(inode);
441         int             error;
442
443         mode <<= 6;             /* convert from linux to vnode access bits */
444         VOP_ACCESS(vp, mode, NULL, error);
445         return -error;
446 }
447 #else
448 #define linvfs_permission NULL
449 #endif
450
451 STATIC int
452 linvfs_getattr(
453         struct vfsmount *mnt,
454         struct dentry   *dentry,
455         struct kstat    *stat)
456 {
457         struct inode    *inode = dentry->d_inode;
458         vnode_t         *vp = LINVFS_GET_VP(inode);
459         int             error = 0;
460
461         if (unlikely(vp->v_flag & VMODIFIED))
462                 error = vn_revalidate(vp);
463         if (!error)
464                 generic_fillattr(inode, stat);
465         return 0;
466 }
467
468 STATIC int
469 linvfs_setattr(
470         struct dentry   *dentry,
471         struct iattr    *attr)
472 {
473         struct inode    *inode = dentry->d_inode;
474         unsigned int    ia_valid = attr->ia_valid;
475         vnode_t         *vp = LINVFS_GET_VP(inode);
476         vattr_t         vattr;
477         int             flags = 0;
478         int             error;
479
480         memset(&vattr, 0, sizeof(vattr_t));
481         if (ia_valid & ATTR_UID) {
482                 vattr.va_mask |= XFS_AT_UID;
483                 vattr.va_uid = attr->ia_uid;
484         }
485         if (ia_valid & ATTR_GID) {
486                 vattr.va_mask |= XFS_AT_GID;
487                 vattr.va_gid = attr->ia_gid;
488         }
489         if (ia_valid & ATTR_SIZE) {
490                 vattr.va_mask |= XFS_AT_SIZE;
491                 vattr.va_size = attr->ia_size;
492         }
493         if (ia_valid & ATTR_ATIME) {
494                 vattr.va_mask |= XFS_AT_ATIME;
495                 vattr.va_atime = attr->ia_atime;
496         }
497         if (ia_valid & ATTR_MTIME) {
498                 vattr.va_mask |= XFS_AT_MTIME;
499                 vattr.va_mtime = attr->ia_mtime;
500         }
501         if (ia_valid & ATTR_CTIME) {
502                 vattr.va_mask |= XFS_AT_CTIME;
503                 vattr.va_ctime = attr->ia_ctime;
504         }
505         if (ia_valid & ATTR_MODE) {
506                 vattr.va_mask |= XFS_AT_MODE;
507                 vattr.va_mode = attr->ia_mode;
508                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
509                         inode->i_mode &= ~S_ISGID;
510         }
511
512         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
513                 flags |= ATTR_UTIME;
514 #ifdef ATTR_NO_BLOCK
515         if ((ia_valid & ATTR_NO_BLOCK))
516                 flags |= ATTR_NONBLOCK;
517 #endif
518
519         VOP_SETATTR(vp, &vattr, flags, NULL, error);
520         if (error)
521                 return -error;
522         vn_revalidate(vp);
523         return error;
524 }
525
526 STATIC void
527 linvfs_truncate(
528         struct inode    *inode)
529 {
530         block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
531 }
532
533 STATIC int
534 linvfs_setxattr(
535         struct dentry   *dentry,
536         const char      *name,
537         const void      *data,
538         size_t          size,
539         int             flags)
540 {
541         vnode_t         *vp = LINVFS_GET_VP(dentry->d_inode);
542         char            *attr = (char *)name;
543         attrnames_t     *namesp;
544         int             xflags = 0;
545         int             error;
546
547         namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
548         if (!namesp)
549                 return -EOPNOTSUPP;
550         attr += namesp->attr_namelen;
551         error = namesp->attr_capable(vp, NULL);
552         if (error)
553                 return error;
554
555         /* Convert Linux syscall to XFS internal ATTR flags */
556         if (flags & XATTR_CREATE)
557                 xflags |= ATTR_CREATE;
558         if (flags & XATTR_REPLACE)
559                 xflags |= ATTR_REPLACE;
560         xflags |= namesp->attr_flag;
561         return namesp->attr_set(vp, attr, (void *)data, size, xflags);
562 }
563
564 STATIC ssize_t
565 linvfs_getxattr(
566         struct dentry   *dentry,
567         const char      *name,
568         void            *data,
569         size_t          size)
570 {
571         vnode_t         *vp = LINVFS_GET_VP(dentry->d_inode);
572         char            *attr = (char *)name;
573         attrnames_t     *namesp;
574         int             xflags = 0;
575         ssize_t         error;
576
577         namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
578         if (!namesp)
579                 return -EOPNOTSUPP;
580         attr += namesp->attr_namelen;
581         error = namesp->attr_capable(vp, NULL);
582         if (error)
583                 return error;
584
585         /* Convert Linux syscall to XFS internal ATTR flags */
586         if (!size) {
587                 xflags |= ATTR_KERNOVAL;
588                 data = NULL;
589         }
590         xflags |= namesp->attr_flag;
591         return namesp->attr_get(vp, attr, (void *)data, size, xflags);
592 }
593
594 STATIC ssize_t
595 linvfs_listxattr(
596         struct dentry           *dentry,
597         char                    *data,
598         size_t                  size)
599 {
600         vnode_t                 *vp = LINVFS_GET_VP(dentry->d_inode);
601         int                     error, xflags = ATTR_KERNAMELS;
602         ssize_t                 result;
603
604         if (!size)
605                 xflags |= ATTR_KERNOVAL;
606         xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
607
608         error = attr_generic_list(vp, data, size, xflags, &result);
609         if (error < 0)
610                 return error;
611         return result;
612 }
613
614 STATIC int
615 linvfs_removexattr(
616         struct dentry   *dentry,
617         const char      *name)
618 {
619         vnode_t         *vp = LINVFS_GET_VP(dentry->d_inode);
620         char            *attr = (char *)name;
621         attrnames_t     *namesp;
622         int             xflags = 0;
623         int             error;
624
625         namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
626         if (!namesp)
627                 return -EOPNOTSUPP;
628         attr += namesp->attr_namelen;
629         error = namesp->attr_capable(vp, NULL);
630         if (error)
631                 return error;
632         xflags |= namesp->attr_flag;
633         return namesp->attr_remove(vp, attr, xflags);
634 }
635
636
637 struct inode_operations linvfs_file_inode_operations = {
638         .permission             = linvfs_permission,
639         .truncate               = linvfs_truncate,
640         .getattr                = linvfs_getattr,
641         .setattr                = linvfs_setattr,
642         .setxattr               = linvfs_setxattr,
643         .getxattr               = linvfs_getxattr,
644         .listxattr              = linvfs_listxattr,
645         .removexattr            = linvfs_removexattr,
646 };
647
648 struct inode_operations linvfs_dir_inode_operations = {
649         .create                 = linvfs_create,
650         .lookup                 = linvfs_lookup,
651         .link                   = linvfs_link,
652         .unlink                 = linvfs_unlink,
653         .symlink                = linvfs_symlink,
654         .mkdir                  = linvfs_mkdir,
655         .rmdir                  = linvfs_rmdir,
656         .mknod                  = linvfs_mknod,
657         .rename                 = linvfs_rename,
658         .permission             = linvfs_permission,
659         .getattr                = linvfs_getattr,
660         .setattr                = linvfs_setattr,
661         .setxattr               = linvfs_setxattr,
662         .getxattr               = linvfs_getxattr,
663         .listxattr              = linvfs_listxattr,
664         .removexattr            = linvfs_removexattr,
665 };
666
667 struct inode_operations linvfs_symlink_inode_operations = {
668         .readlink               = generic_readlink,
669         .follow_link            = linvfs_follow_link,
670         .put_link               = linvfs_put_link,
671         .permission             = linvfs_permission,
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 };