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 $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/fcntl.h>
37 #include <sys/ioccom.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
44 #include <coda/coda.h>
45 #include <coda/cnode.h>
46 #include <coda/coda_venus.h>
47 #include <coda/coda_pioctl.h>
49 #define DECL_NO_IN(name) \
50 struct coda_in_hdr *inp; \
51 struct name ## _out *outp; \
52 int name ## _size = sizeof (struct coda_in_hdr); \
53 int Isize = sizeof (struct coda_in_hdr); \
54 int Osize = sizeof (struct name ## _out); \
58 struct name ## _in *inp; \
59 struct name ## _out *outp; \
60 int name ## _size = sizeof (struct name ## _in); \
61 int Isize = sizeof (struct name ## _in); \
62 int Osize = sizeof (struct name ## _out); \
65 #define DECL_NO_OUT(name) \
66 struct name ## _in *inp; \
67 struct coda_out_hdr *outp; \
68 int name ## _size = sizeof (struct name ## _in); \
69 int Isize = sizeof (struct name ## _in); \
70 int Osize = sizeof (struct coda_out_hdr); \
73 #define ALLOC_NO_IN(name) \
74 if (Osize > name ## _size) \
75 name ## _size = Osize; \
76 CODA_ALLOC(inp, struct coda_in_hdr *, name ## _size);\
77 outp = (struct name ## _out *) inp
80 if (Osize > name ## _size) \
81 name ## _size = Osize; \
82 CODA_ALLOC(inp, struct name ## _in *, name ## _size);\
83 outp = (struct name ## _out *) inp
85 #define ALLOC_NO_OUT(name) \
86 if (Osize > name ## _size) \
87 name ## _size = Osize; \
88 CODA_ALLOC(inp, struct name ## _in *, name ## _size);\
89 outp = (struct coda_out_hdr *) inp
91 #define STRCPY(struc, name, len) \
92 bcopy(name, (char *)inp + (int)inp->struc, len); \
93 ((char*)inp + (int)inp->struc)[len++] = 0; \
96 #define INIT_IN(in, op, ident, p) \
97 (in)->opcode = (op); \
98 sx_slock(&proctree_lock); \
99 (in)->pid = p ? p->p_pid : -1; \
100 (in)->pgid = p ? p->p_pgid : -1; \
101 (in)->sid = (p && p->p_session && p->p_session->s_leader) ? (p->p_session->s_leader->p_pid) : -1; \
102 sx_sunlock(&proctree_lock); \
103 if (ident != NOCRED) { \
104 (in)->cred.cr_uid = ident->cr_uid; \
105 (in)->cred.cr_groupid = ident->cr_gid; \
107 bzero(&((in)->cred),sizeof(struct coda_cred)); \
108 (in)->cred.cr_uid = -1; \
109 (in)->cred.cr_groupid = -1; \
112 #define CNV_OFLAG(to, from) \
115 if (from & FREAD) to |= C_O_READ; \
116 if (from & FWRITE) to |= C_O_WRITE; \
117 if (from & O_TRUNC) to |= C_O_TRUNC; \
118 if (from & O_EXCL) to |= C_O_EXCL; \
119 if (from & O_CREAT) to |= C_O_CREAT; \
122 #define CNV_VV2V_ATTR(top, fromp) \
124 (top)->va_type = (fromp)->va_type; \
125 (top)->va_mode = (fromp)->va_mode; \
126 (top)->va_nlink = (fromp)->va_nlink; \
127 (top)->va_uid = (fromp)->va_uid; \
128 (top)->va_gid = (fromp)->va_gid; \
129 (top)->va_fsid = VNOVAL; \
130 (top)->va_fileid = (fromp)->va_fileid; \
131 (top)->va_size = (fromp)->va_size; \
132 (top)->va_blocksize = (fromp)->va_blocksize; \
133 (top)->va_atime = (fromp)->va_atime; \
134 (top)->va_mtime = (fromp)->va_mtime; \
135 (top)->va_ctime = (fromp)->va_ctime; \
136 (top)->va_gen = (fromp)->va_gen; \
137 (top)->va_flags = (fromp)->va_flags; \
138 (top)->va_rdev = (fromp)->va_rdev; \
139 (top)->va_bytes = (fromp)->va_bytes; \
140 (top)->va_filerev = (fromp)->va_filerev; \
141 (top)->va_vaflags = VNOVAL; \
142 (top)->va_spare = VNOVAL; \
145 #define CNV_V2VV_ATTR(top, fromp) \
147 (top)->va_type = (fromp)->va_type; \
148 (top)->va_mode = (fromp)->va_mode; \
149 (top)->va_nlink = (fromp)->va_nlink; \
150 (top)->va_uid = (fromp)->va_uid; \
151 (top)->va_gid = (fromp)->va_gid; \
152 (top)->va_fileid = (fromp)->va_fileid; \
153 (top)->va_size = (fromp)->va_size; \
154 (top)->va_blocksize = (fromp)->va_blocksize; \
155 (top)->va_atime = (fromp)->va_atime; \
156 (top)->va_mtime = (fromp)->va_mtime; \
157 (top)->va_ctime = (fromp)->va_ctime; \
158 (top)->va_gen = (fromp)->va_gen; \
159 (top)->va_flags = (fromp)->va_flags; \
160 (top)->va_rdev = (fromp)->va_rdev; \
161 (top)->va_bytes = (fromp)->va_bytes; \
162 (top)->va_filerev = (fromp)->va_filerev; \
166 int coda_kernel_version = CODA_KERNEL_VERSION;
169 venus_root(void *mdp,
170 struct ucred *cred, struct proc *p,
171 /*out*/ ViceFid *VFid)
173 DECL_NO_IN(coda_root); /* sets Isize & Osize */
174 ALLOC_NO_IN(coda_root); /* sets inp & outp */
176 /* send the open to venus. */
177 INIT_IN(inp, CODA_ROOT, cred, p);
179 error = coda_call(mdp, Isize, &Osize, (char *)inp);
183 CODA_FREE(inp, coda_root_size);
188 venus_open(void *mdp, ViceFid *fid, int flag,
189 struct ucred *cred, struct proc *p,
190 /*out*/ dev_t *dev, ino_t *inode)
193 DECL(coda_open); /* sets Isize & Osize */
194 ALLOC(coda_open); /* sets inp & outp */
196 /* send the open to venus. */
197 INIT_IN(&inp->ih, CODA_OPEN, cred, p);
199 CNV_OFLAG(cflag, flag);
202 error = coda_call(mdp, Isize, &Osize, (char *)inp);
204 /* 5/11 *dev = udev2dev(outp->dev, 2); */
205 *dev = udev2dev(outp->dev, 0);
206 *inode = outp->inode;
209 CODA_FREE(inp, coda_open_size);
214 venus_close(void *mdp, ViceFid *fid, int flag,
215 struct ucred *cred, struct proc *p)
218 DECL_NO_OUT(coda_close); /* sets Isize & Osize */
219 ALLOC_NO_OUT(coda_close); /* sets inp & outp */
221 INIT_IN(&inp->ih, CODA_CLOSE, cred, p);
223 CNV_OFLAG(cflag, flag);
226 error = coda_call(mdp, Isize, &Osize, (char *)inp);
228 CODA_FREE(inp, coda_close_size);
233 * these two calls will not exist!!! the container file is read/written
247 * this is a bit sad too. the ioctl's are for the control file, not for
251 venus_ioctl(void *mdp, ViceFid *fid,
252 int com, int flag, caddr_t data,
253 struct ucred *cred, struct proc *p)
255 DECL(coda_ioctl); /* sets Isize & Osize */
256 struct PioctlData *iap = (struct PioctlData *)data;
259 coda_ioctl_size = VC_MAXMSGSIZE;
260 ALLOC(coda_ioctl); /* sets inp & outp */
262 INIT_IN(&inp->ih, CODA_IOCTL, cred, p);
265 /* command was mutated by increasing its size field to reflect the
266 * path and follow args. we need to subtract that out before sending
267 * the command to venus.
269 inp->cmd = (com & ~(IOCPARM_MASK << 16));
270 tmp = ((com >> 16) & IOCPARM_MASK) - sizeof (char *) - sizeof (int);
271 inp->cmd |= (tmp & IOCPARM_MASK) << 16;
274 inp->len = iap->vi.in_size;
275 inp->data = (char *)(sizeof (struct coda_ioctl_in));
277 error = copyin(iap->vi.in, (char*)inp + (int)inp->data,
280 CODA_FREE(inp, coda_ioctl_size);
284 Osize = VC_MAXMSGSIZE;
285 error = coda_call(mdp, Isize + iap->vi.in_size, &Osize, (char *)inp);
287 /* copy out the out buffer. */
289 if (outp->len > iap->vi.out_size) {
292 error = copyout((char *)outp + (int)outp->data,
293 iap->vi.out, iap->vi.out_size);
297 CODA_FREE(inp, coda_ioctl_size);
302 venus_getattr(void *mdp, ViceFid *fid,
303 struct ucred *cred, struct proc *p,
304 /*out*/ struct vattr *vap)
306 DECL(coda_getattr); /* sets Isize & Osize */
307 ALLOC(coda_getattr); /* sets inp & outp */
309 /* send the open to venus. */
310 INIT_IN(&inp->ih, CODA_GETATTR, cred, p);
313 error = coda_call(mdp, Isize, &Osize, (char *)inp);
315 CNV_VV2V_ATTR(vap, &outp->attr);
318 CODA_FREE(inp, coda_getattr_size);
323 venus_setattr(void *mdp, ViceFid *fid, struct vattr *vap,
324 struct ucred *cred, struct proc *p)
326 DECL_NO_OUT(coda_setattr); /* sets Isize & Osize */
327 ALLOC_NO_OUT(coda_setattr); /* sets inp & outp */
329 /* send the open to venus. */
330 INIT_IN(&inp->ih, CODA_SETATTR, cred, p);
332 CNV_V2VV_ATTR(&inp->attr, vap);
334 error = coda_call(mdp, Isize, &Osize, (char *)inp);
336 CODA_FREE(inp, coda_setattr_size);
341 venus_access(void *mdp, ViceFid *fid, int mode,
342 struct ucred *cred, struct proc *p)
344 DECL_NO_OUT(coda_access); /* sets Isize & Osize */
345 ALLOC_NO_OUT(coda_access); /* sets inp & outp */
347 /* send the open to venus. */
348 INIT_IN(&inp->ih, CODA_ACCESS, cred, p);
351 * NetBSD and Venus internals use the "data" in the low 3 bits.
352 * Hence, the conversion.
354 inp->flags = mode>>6;
356 error = coda_call(mdp, Isize, &Osize, (char *)inp);
358 CODA_FREE(inp, coda_access_size);
363 venus_readlink(void *mdp, ViceFid *fid,
364 struct ucred *cred, struct proc *p,
365 /*out*/ char **str, int *len)
367 DECL(coda_readlink); /* sets Isize & Osize */
368 coda_readlink_size += CODA_MAXPATHLEN;
369 ALLOC(coda_readlink); /* sets inp & outp */
371 /* send the open to venus. */
372 INIT_IN(&inp->ih, CODA_READLINK, cred, p);
375 Osize += CODA_MAXPATHLEN;
376 error = coda_call(mdp, Isize, &Osize, (char *)inp);
378 CODA_ALLOC(*str, char *, outp->count);
380 bcopy((char *)outp + (int)outp->data, *str, *len);
383 CODA_FREE(inp, coda_readlink_size);
388 venus_fsync(void *mdp, ViceFid *fid,
389 struct ucred *cred, struct proc *p)
391 DECL_NO_OUT(coda_fsync); /* sets Isize & Osize */
392 ALLOC_NO_OUT(coda_fsync); /* sets inp & outp */
394 /* send the open to venus. */
395 INIT_IN(&inp->ih, CODA_FSYNC, cred, p);
398 error = coda_call(mdp, Isize, &Osize, (char *)inp);
400 CODA_FREE(inp, coda_fsync_size);
405 venus_lookup(void *mdp, ViceFid *fid,
406 const char *nm, int len,
407 struct ucred *cred, struct proc *p,
408 /*out*/ ViceFid *VFid, int *vtype)
410 DECL(coda_lookup); /* sets Isize & Osize */
411 coda_lookup_size += len + 1;
412 ALLOC(coda_lookup); /* sets inp & outp */
414 /* send the open to venus. */
415 INIT_IN(&inp->ih, CODA_LOOKUP, cred, p);
419 * Between version 1 and version 2 we have added an extra flag field
420 * to this structure. But because the string was at the end and because
421 * of the wierd way we represent strings by having the slot point to
422 * where the string characters are in the "heap", we can just slip the
423 * flag parameter in after the string slot pointer and veni that don't
424 * know better won't see this new flag field ...
425 * Otherwise we'd need two different venus_lookup functions.
428 inp->flags = CLU_CASE_SENSITIVE; /* doesn't really matter for BSD */
429 STRCPY(name, nm, len); /* increments Isize */
431 error = coda_call(mdp, Isize, &Osize, (char *)inp);
434 *vtype = outp->vtype;
437 CODA_FREE(inp, coda_lookup_size);
442 venus_create(void *mdp, ViceFid *fid,
443 const char *nm, int len, int exclusive, int mode, struct vattr *va,
444 struct ucred *cred, struct proc *p,
445 /*out*/ ViceFid *VFid, struct vattr *attr)
447 DECL(coda_create); /* sets Isize & Osize */
448 coda_create_size += len + 1;
449 ALLOC(coda_create); /* sets inp & outp */
451 /* send the open to venus. */
452 INIT_IN(&inp->ih, CODA_CREATE, cred, p);
454 inp->excl = exclusive ? C_O_EXCL : 0;
456 CNV_V2VV_ATTR(&inp->attr, va);
459 STRCPY(name, nm, len); /* increments Isize */
461 error = coda_call(mdp, Isize, &Osize, (char *)inp);
464 CNV_VV2V_ATTR(attr, &outp->attr);
467 CODA_FREE(inp, coda_create_size);
472 venus_remove(void *mdp, ViceFid *fid,
473 const char *nm, int len,
474 struct ucred *cred, struct proc *p)
476 DECL_NO_OUT(coda_remove); /* sets Isize & Osize */
477 coda_remove_size += len + 1;
478 ALLOC_NO_OUT(coda_remove); /* sets inp & outp */
480 /* send the open to venus. */
481 INIT_IN(&inp->ih, CODA_REMOVE, cred, p);
485 STRCPY(name, nm, len); /* increments Isize */
487 error = coda_call(mdp, Isize, &Osize, (char *)inp);
489 CODA_FREE(inp, coda_remove_size);
494 venus_link(void *mdp, ViceFid *fid, ViceFid *tfid,
495 const char *nm, int len,
496 struct ucred *cred, struct proc *p)
498 DECL_NO_OUT(coda_link); /* sets Isize & Osize */
499 coda_link_size += len + 1;
500 ALLOC_NO_OUT(coda_link); /* sets inp & outp */
502 /* send the open to venus. */
503 INIT_IN(&inp->ih, CODA_LINK, cred, p);
504 inp->sourceFid = *fid;
505 inp->destFid = *tfid;
508 STRCPY(tname, nm, len); /* increments Isize */
510 error = coda_call(mdp, Isize, &Osize, (char *)inp);
512 CODA_FREE(inp, coda_link_size);
517 venus_rename(void *mdp, ViceFid *fid, ViceFid *tfid,
518 const char *nm, int len, const char *tnm, int tlen,
519 struct ucred *cred, struct proc *p)
521 DECL_NO_OUT(coda_rename); /* sets Isize & Osize */
522 coda_rename_size += len + 1 + tlen + 1;
523 ALLOC_NO_OUT(coda_rename); /* sets inp & outp */
525 /* send the open to venus. */
526 INIT_IN(&inp->ih, CODA_RENAME, cred, p);
527 inp->sourceFid = *fid;
528 inp->destFid = *tfid;
530 inp->srcname = Isize;
531 STRCPY(srcname, nm, len); /* increments Isize */
533 inp->destname = Isize;
534 STRCPY(destname, tnm, tlen); /* increments Isize */
536 error = coda_call(mdp, Isize, &Osize, (char *)inp);
538 CODA_FREE(inp, coda_rename_size);
543 venus_mkdir(void *mdp, ViceFid *fid,
544 const char *nm, int len, struct vattr *va,
545 struct ucred *cred, struct proc *p,
546 /*out*/ ViceFid *VFid, struct vattr *ova)
548 DECL(coda_mkdir); /* sets Isize & Osize */
549 coda_mkdir_size += len + 1;
550 ALLOC(coda_mkdir); /* sets inp & outp */
552 /* send the open to venus. */
553 INIT_IN(&inp->ih, CODA_MKDIR, cred, p);
555 CNV_V2VV_ATTR(&inp->attr, va);
558 STRCPY(name, nm, len); /* increments Isize */
560 error = coda_call(mdp, Isize, &Osize, (char *)inp);
563 CNV_VV2V_ATTR(ova, &outp->attr);
566 CODA_FREE(inp, coda_mkdir_size);
571 venus_rmdir(void *mdp, ViceFid *fid,
572 const char *nm, int len,
573 struct ucred *cred, struct proc *p)
575 DECL_NO_OUT(coda_rmdir); /* sets Isize & Osize */
576 coda_rmdir_size += len + 1;
577 ALLOC_NO_OUT(coda_rmdir); /* sets inp & outp */
579 /* send the open to venus. */
580 INIT_IN(&inp->ih, CODA_RMDIR, cred, p);
584 STRCPY(name, nm, len); /* increments Isize */
586 error = coda_call(mdp, Isize, &Osize, (char *)inp);
588 CODA_FREE(inp, coda_rmdir_size);
593 venus_symlink(void *mdp, ViceFid *fid,
594 const char *lnm, int llen, const char *nm, int len, struct vattr *va,
595 struct ucred *cred, struct proc *p)
597 DECL_NO_OUT(coda_symlink); /* sets Isize & Osize */
598 coda_symlink_size += llen + 1 + len + 1;
599 ALLOC_NO_OUT(coda_symlink); /* sets inp & outp */
601 /* send the open to venus. */
602 INIT_IN(&inp->ih, CODA_SYMLINK, cred, p);
604 CNV_V2VV_ATTR(&inp->attr, va);
606 inp->srcname = Isize;
607 STRCPY(srcname, lnm, llen); /* increments Isize */
610 STRCPY(tname, nm, len); /* increments Isize */
612 error = coda_call(mdp, Isize, &Osize, (char *)inp);
614 CODA_FREE(inp, coda_symlink_size);
619 venus_readdir(void *mdp, ViceFid *fid,
620 int count, int offset,
621 struct ucred *cred, struct proc *p,
622 /*out*/ char *buffer, int *len)
624 DECL(coda_readdir); /* sets Isize & Osize */
625 coda_readdir_size = VC_MAXMSGSIZE;
626 ALLOC(coda_readdir); /* sets inp & outp */
628 /* send the open to venus. */
629 INIT_IN(&inp->ih, CODA_READDIR, cred, p);
632 inp->offset = offset;
634 Osize = VC_MAXMSGSIZE;
635 error = coda_call(mdp, Isize, &Osize, (char *)inp);
637 bcopy((char *)outp + (int)outp->data, buffer, outp->size);
641 CODA_FREE(inp, coda_readdir_size);
646 venus_fhtovp(void *mdp, ViceFid *fid,
647 struct ucred *cred, struct proc *p,
648 /*out*/ ViceFid *VFid, int *vtype)
650 DECL(coda_vget); /* sets Isize & Osize */
651 ALLOC(coda_vget); /* sets inp & outp */
653 /* Send the open to Venus. */
654 INIT_IN(&inp->ih, CODA_VGET, cred, p);
657 error = coda_call(mdp, Isize, &Osize, (char *)inp);
660 *vtype = outp->vtype;
663 CODA_FREE(inp, coda_vget_size);