3 * Coda: an Experimental Distributed File System
6 * Copyright (c) 1987-1998 Carnegie Mellon University
9 * Permission to use, copy, modify and distribute this software and its
10 * documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation, and
14 * that credit is given to Carnegie Mellon University in all documents
15 * and publicity pertaining to direct or indirect use of this code or its
18 * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS KNOWN TO HAVE BUGS,
19 * SOME OF WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON ALLOWS
20 * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION. CARNEGIE MELLON
21 * DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
22 * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE OR OF
23 * ANY DERIVATIVE WORK.
25 * Carnegie Mellon encourages users of this software to return any
26 * improvements or extensions that they make, and to grant Carnegie
27 * Mellon the rights to redistribute these changes without encumbrance.
29 * @(#) src/sys/cfs/coda_venus.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
30 * $FreeBSD: src/sys/coda/coda_venus.c,v 1.9 1999/08/28 00:40:56 peter Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
38 #include <sys/select.h>
39 #include <sys/ioccom.h>
40 #include <sys/fcntl.h>
42 #include <coda/coda.h>
43 #include <coda/cnode.h>
44 #include <coda/coda_venus.h>
45 #include <coda/coda_pioctl.h>
47 #define DECL_NO_IN(name) \
48 struct coda_in_hdr *inp; \
49 struct name ## _out *outp; \
50 int name ## _size = sizeof (struct coda_in_hdr); \
51 int Isize = sizeof (struct coda_in_hdr); \
52 int Osize = sizeof (struct name ## _out); \
56 struct name ## _in *inp; \
57 struct name ## _out *outp; \
58 int name ## _size = sizeof (struct name ## _in); \
59 int Isize = sizeof (struct name ## _in); \
60 int Osize = sizeof (struct name ## _out); \
63 #define DECL_NO_OUT(name) \
64 struct name ## _in *inp; \
65 struct coda_out_hdr *outp; \
66 int name ## _size = sizeof (struct name ## _in); \
67 int Isize = sizeof (struct name ## _in); \
68 int Osize = sizeof (struct coda_out_hdr); \
71 #define ALLOC_NO_IN(name) \
72 if (Osize > name ## _size) \
73 name ## _size = Osize; \
74 CODA_ALLOC(inp, struct coda_in_hdr *, name ## _size);\
75 outp = (struct name ## _out *) inp
78 if (Osize > name ## _size) \
79 name ## _size = Osize; \
80 CODA_ALLOC(inp, struct name ## _in *, name ## _size);\
81 outp = (struct name ## _out *) inp
83 #define ALLOC_NO_OUT(name) \
84 if (Osize > name ## _size) \
85 name ## _size = Osize; \
86 CODA_ALLOC(inp, struct name ## _in *, name ## _size);\
87 outp = (struct coda_out_hdr *) inp
89 #define STRCPY(struc, name, len) \
90 bcopy(name, (char *)inp + (int)inp->struc, len); \
91 ((char*)inp + (int)inp->struc)[len++] = 0; \
94 #define INIT_IN(in, op, ident, p) \
95 (in)->opcode = (op); \
96 (in)->pid = p ? p->p_pid : -1; \
97 (in)->pgid = p ? p->p_pgid : -1; \
98 (in)->sid = (p && p->p_session && p->p_session->s_leader) ? (p->p_session->s_leader->p_pid) : -1; \
99 if (ident != NOCRED) { \
100 (in)->cred.cr_uid = ident->cr_uid; \
101 (in)->cred.cr_groupid = ident->cr_gid; \
103 bzero(&((in)->cred),sizeof(struct coda_cred)); \
104 (in)->cred.cr_uid = -1; \
105 (in)->cred.cr_groupid = -1; \
108 #define CNV_OFLAG(to, from) \
111 if (from & FREAD) to |= C_O_READ; \
112 if (from & FWRITE) to |= C_O_WRITE; \
113 if (from & O_TRUNC) to |= C_O_TRUNC; \
114 if (from & O_EXCL) to |= C_O_EXCL; \
115 if (from & O_CREAT) to |= C_O_CREAT; \
118 #define CNV_VV2V_ATTR(top, fromp) \
120 (top)->va_type = (fromp)->va_type; \
121 (top)->va_mode = (fromp)->va_mode; \
122 (top)->va_nlink = (fromp)->va_nlink; \
123 (top)->va_uid = (fromp)->va_uid; \
124 (top)->va_gid = (fromp)->va_gid; \
125 (top)->va_fsid = VNOVAL; \
126 (top)->va_fileid = (fromp)->va_fileid; \
127 (top)->va_size = (fromp)->va_size; \
128 (top)->va_blocksize = (fromp)->va_blocksize; \
129 (top)->va_atime = (fromp)->va_atime; \
130 (top)->va_mtime = (fromp)->va_mtime; \
131 (top)->va_ctime = (fromp)->va_ctime; \
132 (top)->va_gen = (fromp)->va_gen; \
133 (top)->va_flags = (fromp)->va_flags; \
134 (top)->va_rdev = (fromp)->va_rdev; \
135 (top)->va_bytes = (fromp)->va_bytes; \
136 (top)->va_filerev = (fromp)->va_filerev; \
137 (top)->va_vaflags = VNOVAL; \
138 (top)->va_spare = VNOVAL; \
141 #define CNV_V2VV_ATTR(top, fromp) \
143 (top)->va_type = (fromp)->va_type; \
144 (top)->va_mode = (fromp)->va_mode; \
145 (top)->va_nlink = (fromp)->va_nlink; \
146 (top)->va_uid = (fromp)->va_uid; \
147 (top)->va_gid = (fromp)->va_gid; \
148 (top)->va_fileid = (fromp)->va_fileid; \
149 (top)->va_size = (fromp)->va_size; \
150 (top)->va_blocksize = (fromp)->va_blocksize; \
151 (top)->va_atime = (fromp)->va_atime; \
152 (top)->va_mtime = (fromp)->va_mtime; \
153 (top)->va_ctime = (fromp)->va_ctime; \
154 (top)->va_gen = (fromp)->va_gen; \
155 (top)->va_flags = (fromp)->va_flags; \
156 (top)->va_rdev = (fromp)->va_rdev; \
157 (top)->va_bytes = (fromp)->va_bytes; \
158 (top)->va_filerev = (fromp)->va_filerev; \
162 int coda_kernel_version = CODA_KERNEL_VERSION;
165 venus_root(void *mdp,
166 struct ucred *cred, struct proc *p,
167 /*out*/ ViceFid *VFid)
169 DECL_NO_IN(coda_root); /* sets Isize & Osize */
170 ALLOC_NO_IN(coda_root); /* sets inp & outp */
172 /* send the open to venus. */
173 INIT_IN(inp, CODA_ROOT, cred, p);
175 error = coda_call(mdp, Isize, &Osize, (char *)inp);
179 CODA_FREE(inp, coda_root_size);
184 venus_open(void *mdp, ViceFid *fid, int flag,
185 struct ucred *cred, struct proc *p,
186 /*out*/ dev_t *dev, ino_t *inode)
189 DECL(coda_open); /* sets Isize & Osize */
190 ALLOC(coda_open); /* sets inp & outp */
192 /* send the open to venus. */
193 INIT_IN(&inp->ih, CODA_OPEN, cred, p);
195 CNV_OFLAG(cflag, flag);
198 error = coda_call(mdp, Isize, &Osize, (char *)inp);
200 /* 5/11 *dev = udev2dev(outp->dev, 2); */
201 *dev = udev2dev(outp->dev, 0);
202 *inode = outp->inode;
205 CODA_FREE(inp, coda_open_size);
210 venus_close(void *mdp, ViceFid *fid, int flag,
211 struct ucred *cred, struct proc *p)
214 DECL_NO_OUT(coda_close); /* sets Isize & Osize */
215 ALLOC_NO_OUT(coda_close); /* sets inp & outp */
217 INIT_IN(&inp->ih, CODA_CLOSE, cred, p);
219 CNV_OFLAG(cflag, flag);
222 error = coda_call(mdp, Isize, &Osize, (char *)inp);
224 CODA_FREE(inp, coda_close_size);
229 * these two calls will not exist!!! the container file is read/written
243 * this is a bit sad too. the ioctl's are for the control file, not for
247 venus_ioctl(void *mdp, ViceFid *fid,
248 int com, int flag, caddr_t data,
249 struct ucred *cred, struct proc *p)
251 DECL(coda_ioctl); /* sets Isize & Osize */
252 struct PioctlData *iap = (struct PioctlData *)data;
255 coda_ioctl_size = VC_MAXMSGSIZE;
256 ALLOC(coda_ioctl); /* sets inp & outp */
258 INIT_IN(&inp->ih, CODA_IOCTL, cred, p);
261 /* command was mutated by increasing its size field to reflect the
262 * path and follow args. we need to subtract that out before sending
263 * the command to venus.
265 inp->cmd = (com & ~(IOCPARM_MASK << 16));
266 tmp = ((com >> 16) & IOCPARM_MASK) - sizeof (char *) - sizeof (int);
267 inp->cmd |= (tmp & IOCPARM_MASK) << 16;
270 inp->len = iap->vi.in_size;
271 inp->data = (char *)(sizeof (struct coda_ioctl_in));
273 error = copyin(iap->vi.in, (char*)inp + (int)inp->data,
276 CODA_FREE(inp, coda_ioctl_size);
280 Osize = VC_MAXMSGSIZE;
281 error = coda_call(mdp, Isize + iap->vi.in_size, &Osize, (char *)inp);
283 /* copy out the out buffer. */
285 if (outp->len > iap->vi.out_size) {
288 error = copyout((char *)outp + (int)outp->data,
289 iap->vi.out, iap->vi.out_size);
293 CODA_FREE(inp, coda_ioctl_size);
298 venus_getattr(void *mdp, ViceFid *fid,
299 struct ucred *cred, struct proc *p,
300 /*out*/ struct vattr *vap)
302 DECL(coda_getattr); /* sets Isize & Osize */
303 ALLOC(coda_getattr); /* sets inp & outp */
305 /* send the open to venus. */
306 INIT_IN(&inp->ih, CODA_GETATTR, cred, p);
309 error = coda_call(mdp, Isize, &Osize, (char *)inp);
311 CNV_VV2V_ATTR(vap, &outp->attr);
314 CODA_FREE(inp, coda_getattr_size);
319 venus_setattr(void *mdp, ViceFid *fid, struct vattr *vap,
320 struct ucred *cred, struct proc *p)
322 DECL_NO_OUT(coda_setattr); /* sets Isize & Osize */
323 ALLOC_NO_OUT(coda_setattr); /* sets inp & outp */
325 /* send the open to venus. */
326 INIT_IN(&inp->ih, CODA_SETATTR, cred, p);
328 CNV_V2VV_ATTR(&inp->attr, vap);
330 error = coda_call(mdp, Isize, &Osize, (char *)inp);
332 CODA_FREE(inp, coda_setattr_size);
337 venus_access(void *mdp, ViceFid *fid, int mode,
338 struct ucred *cred, struct proc *p)
340 DECL_NO_OUT(coda_access); /* sets Isize & Osize */
341 ALLOC_NO_OUT(coda_access); /* sets inp & outp */
343 /* send the open to venus. */
344 INIT_IN(&inp->ih, CODA_ACCESS, cred, p);
347 * NetBSD and Venus internals use the "data" in the low 3 bits.
348 * Hence, the conversion.
350 inp->flags = mode>>6;
352 error = coda_call(mdp, Isize, &Osize, (char *)inp);
354 CODA_FREE(inp, coda_access_size);
359 venus_readlink(void *mdp, ViceFid *fid,
360 struct ucred *cred, struct proc *p,
361 /*out*/ char **str, int *len)
363 DECL(coda_readlink); /* sets Isize & Osize */
364 coda_readlink_size += CODA_MAXPATHLEN;
365 ALLOC(coda_readlink); /* sets inp & outp */
367 /* send the open to venus. */
368 INIT_IN(&inp->ih, CODA_READLINK, cred, p);
371 Osize += CODA_MAXPATHLEN;
372 error = coda_call(mdp, Isize, &Osize, (char *)inp);
374 CODA_ALLOC(*str, char *, outp->count);
376 bcopy((char *)outp + (int)outp->data, *str, *len);
379 CODA_FREE(inp, coda_readlink_size);
384 venus_fsync(void *mdp, ViceFid *fid,
385 struct ucred *cred, struct proc *p)
387 DECL_NO_OUT(coda_fsync); /* sets Isize & Osize */
388 ALLOC_NO_OUT(coda_fsync); /* sets inp & outp */
390 /* send the open to venus. */
391 INIT_IN(&inp->ih, CODA_FSYNC, cred, p);
394 error = coda_call(mdp, Isize, &Osize, (char *)inp);
396 CODA_FREE(inp, coda_fsync_size);
401 venus_lookup(void *mdp, ViceFid *fid,
402 const char *nm, int len,
403 struct ucred *cred, struct proc *p,
404 /*out*/ ViceFid *VFid, int *vtype)
406 DECL(coda_lookup); /* sets Isize & Osize */
407 coda_lookup_size += len + 1;
408 ALLOC(coda_lookup); /* sets inp & outp */
410 /* send the open to venus. */
411 INIT_IN(&inp->ih, CODA_LOOKUP, cred, p);
415 * Between version 1 and version 2 we have added an extra flag field
416 * to this structure. But because the string was at the end and because
417 * of the wierd way we represent strings by having the slot point to
418 * where the string characters are in the "heap", we can just slip the
419 * flag parameter in after the string slot pointer and veni that don't
420 * know better won't see this new flag field ...
421 * Otherwise we'd need two different venus_lookup functions.
424 inp->flags = CLU_CASE_SENSITIVE; /* doesn't really matter for BSD */
425 STRCPY(name, nm, len); /* increments Isize */
427 error = coda_call(mdp, Isize, &Osize, (char *)inp);
430 *vtype = outp->vtype;
433 CODA_FREE(inp, coda_lookup_size);
438 venus_create(void *mdp, ViceFid *fid,
439 const char *nm, int len, int exclusive, int mode, struct vattr *va,
440 struct ucred *cred, struct proc *p,
441 /*out*/ ViceFid *VFid, struct vattr *attr)
443 DECL(coda_create); /* sets Isize & Osize */
444 coda_create_size += len + 1;
445 ALLOC(coda_create); /* sets inp & outp */
447 /* send the open to venus. */
448 INIT_IN(&inp->ih, CODA_CREATE, cred, p);
450 inp->excl = exclusive ? C_O_EXCL : 0;
452 CNV_V2VV_ATTR(&inp->attr, va);
455 STRCPY(name, nm, len); /* increments Isize */
457 error = coda_call(mdp, Isize, &Osize, (char *)inp);
460 CNV_VV2V_ATTR(attr, &outp->attr);
463 CODA_FREE(inp, coda_create_size);
468 venus_remove(void *mdp, ViceFid *fid,
469 const char *nm, int len,
470 struct ucred *cred, struct proc *p)
472 DECL_NO_OUT(coda_remove); /* sets Isize & Osize */
473 coda_remove_size += len + 1;
474 ALLOC_NO_OUT(coda_remove); /* sets inp & outp */
476 /* send the open to venus. */
477 INIT_IN(&inp->ih, CODA_REMOVE, cred, p);
481 STRCPY(name, nm, len); /* increments Isize */
483 error = coda_call(mdp, Isize, &Osize, (char *)inp);
485 CODA_FREE(inp, coda_remove_size);
490 venus_link(void *mdp, ViceFid *fid, ViceFid *tfid,
491 const char *nm, int len,
492 struct ucred *cred, struct proc *p)
494 DECL_NO_OUT(coda_link); /* sets Isize & Osize */
495 coda_link_size += len + 1;
496 ALLOC_NO_OUT(coda_link); /* sets inp & outp */
498 /* send the open to venus. */
499 INIT_IN(&inp->ih, CODA_LINK, cred, p);
500 inp->sourceFid = *fid;
501 inp->destFid = *tfid;
504 STRCPY(tname, nm, len); /* increments Isize */
506 error = coda_call(mdp, Isize, &Osize, (char *)inp);
508 CODA_FREE(inp, coda_link_size);
513 venus_rename(void *mdp, ViceFid *fid, ViceFid *tfid,
514 const char *nm, int len, const char *tnm, int tlen,
515 struct ucred *cred, struct proc *p)
517 DECL_NO_OUT(coda_rename); /* sets Isize & Osize */
518 coda_rename_size += len + 1 + tlen + 1;
519 ALLOC_NO_OUT(coda_rename); /* sets inp & outp */
521 /* send the open to venus. */
522 INIT_IN(&inp->ih, CODA_RENAME, cred, p);
523 inp->sourceFid = *fid;
524 inp->destFid = *tfid;
526 inp->srcname = Isize;
527 STRCPY(srcname, nm, len); /* increments Isize */
529 inp->destname = Isize;
530 STRCPY(destname, tnm, tlen); /* increments Isize */
532 error = coda_call(mdp, Isize, &Osize, (char *)inp);
534 CODA_FREE(inp, coda_rename_size);
539 venus_mkdir(void *mdp, ViceFid *fid,
540 const char *nm, int len, struct vattr *va,
541 struct ucred *cred, struct proc *p,
542 /*out*/ ViceFid *VFid, struct vattr *ova)
544 DECL(coda_mkdir); /* sets Isize & Osize */
545 coda_mkdir_size += len + 1;
546 ALLOC(coda_mkdir); /* sets inp & outp */
548 /* send the open to venus. */
549 INIT_IN(&inp->ih, CODA_MKDIR, cred, p);
551 CNV_V2VV_ATTR(&inp->attr, va);
554 STRCPY(name, nm, len); /* increments Isize */
556 error = coda_call(mdp, Isize, &Osize, (char *)inp);
559 CNV_VV2V_ATTR(ova, &outp->attr);
562 CODA_FREE(inp, coda_mkdir_size);
567 venus_rmdir(void *mdp, ViceFid *fid,
568 const char *nm, int len,
569 struct ucred *cred, struct proc *p)
571 DECL_NO_OUT(coda_rmdir); /* sets Isize & Osize */
572 coda_rmdir_size += len + 1;
573 ALLOC_NO_OUT(coda_rmdir); /* sets inp & outp */
575 /* send the open to venus. */
576 INIT_IN(&inp->ih, CODA_RMDIR, cred, p);
580 STRCPY(name, nm, len); /* increments Isize */
582 error = coda_call(mdp, Isize, &Osize, (char *)inp);
584 CODA_FREE(inp, coda_rmdir_size);
589 venus_symlink(void *mdp, ViceFid *fid,
590 const char *lnm, int llen, const char *nm, int len, struct vattr *va,
591 struct ucred *cred, struct proc *p)
593 DECL_NO_OUT(coda_symlink); /* sets Isize & Osize */
594 coda_symlink_size += llen + 1 + len + 1;
595 ALLOC_NO_OUT(coda_symlink); /* sets inp & outp */
597 /* send the open to venus. */
598 INIT_IN(&inp->ih, CODA_SYMLINK, cred, p);
600 CNV_V2VV_ATTR(&inp->attr, va);
602 inp->srcname = Isize;
603 STRCPY(srcname, lnm, llen); /* increments Isize */
606 STRCPY(tname, nm, len); /* increments Isize */
608 error = coda_call(mdp, Isize, &Osize, (char *)inp);
610 CODA_FREE(inp, coda_symlink_size);
615 venus_readdir(void *mdp, ViceFid *fid,
616 int count, int offset,
617 struct ucred *cred, struct proc *p,
618 /*out*/ char *buffer, int *len)
620 DECL(coda_readdir); /* sets Isize & Osize */
621 coda_readdir_size = VC_MAXMSGSIZE;
622 ALLOC(coda_readdir); /* sets inp & outp */
624 /* send the open to venus. */
625 INIT_IN(&inp->ih, CODA_READDIR, cred, p);
628 inp->offset = offset;
630 Osize = VC_MAXMSGSIZE;
631 error = coda_call(mdp, Isize, &Osize, (char *)inp);
633 bcopy((char *)outp + (int)outp->data, buffer, outp->size);
637 CODA_FREE(inp, coda_readdir_size);
642 venus_fhtovp(void *mdp, ViceFid *fid,
643 struct ucred *cred, struct proc *p,
644 /*out*/ ViceFid *VFid, int *vtype)
646 DECL(coda_vget); /* sets Isize & Osize */
647 ALLOC(coda_vget); /* sets inp & outp */
649 /* Send the open to Venus. */
650 INIT_IN(&inp->ih, CODA_VGET, cred, p);
653 error = coda_call(mdp, Isize, &Osize, (char *)inp);
656 *vtype = outp->vtype;
659 CODA_FREE(inp, coda_vget_size);