2 * Coda: an Experimental Distributed File System
5 * Copyright (c) 1987-1998 Carnegie Mellon University
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation, and
13 * that credit is given to Carnegie Mellon University in all documents
14 * and publicity pertaining to direct or indirect use of this code or its
17 * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS KNOWN TO HAVE BUGS,
18 * SOME OF WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON ALLOWS
19 * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION. CARNEGIE MELLON
20 * DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21 * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE OR OF
22 * ANY DERIVATIVE WORK.
24 * Carnegie Mellon encourages users of this software to return any
25 * improvements or extensions that they make, and to grant Carnegie
26 * Mellon the rights to redistribute these changes without encumbrance.
28 * @(#) src/sys/cfs/coda_venus.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
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 <fs/coda/coda.h>
45 #include <fs/coda/cnode.h>
46 #include <fs/coda/coda_venus.h>
47 #include <fs/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; \
97 #define INIT_IN(in, op, ident, p) do { \
98 (in)->opcode = (op); \
99 sx_slock(&proctree_lock); \
100 (in)->pid = p ? p->p_pid : -1; \
101 (in)->pgid = p ? p->p_pgid : -1; \
102 (in)->sid = (p && p->p_session && p->p_session->s_leader) ? \
103 (p->p_session->s_leader->p_pid) : -1; \
104 sx_sunlock(&proctree_lock); \
105 if (ident != NOCRED) { \
106 (in)->cred.cr_uid = ident->cr_uid; \
107 (in)->cred.cr_groupid = ident->cr_gid; \
109 bzero(&((in)->cred),sizeof(struct coda_cred)); \
110 (in)->cred.cr_uid = -1; \
111 (in)->cred.cr_groupid = -1; \
115 #define INIT_IN(in, op, ident, p) do { \
116 (in)->opcode = (op); \
117 (in)->pid = p ? p->p_pid : -1; \
118 (in)->pgid = p ? p->p_pgid : -1; \
119 if (ident != NOCRED) \
120 (in)->uid = ident->cr_uid; \
126 #define CNV_OFLAG(to, from) do { \
128 if (from & FREAD) to |= C_O_READ; \
129 if (from & FWRITE) to |= C_O_WRITE; \
130 if (from & O_TRUNC) to |= C_O_TRUNC; \
131 if (from & O_EXCL) to |= C_O_EXCL; \
132 if (from & O_CREAT) to |= C_O_CREAT; \
135 #define CNV_VV2V_ATTR(top, fromp) do { \
136 (top)->va_type = (fromp)->va_type; \
137 (top)->va_mode = (fromp)->va_mode; \
138 (top)->va_nlink = (fromp)->va_nlink; \
139 (top)->va_uid = (fromp)->va_uid; \
140 (top)->va_gid = (fromp)->va_gid; \
141 (top)->va_fsid = VNOVAL; \
142 (top)->va_fileid = (fromp)->va_fileid; \
143 (top)->va_size = (fromp)->va_size; \
144 (top)->va_blocksize = (fromp)->va_blocksize; \
145 (top)->va_atime = (fromp)->va_atime; \
146 (top)->va_mtime = (fromp)->va_mtime; \
147 (top)->va_ctime = (fromp)->va_ctime; \
148 (top)->va_gen = (fromp)->va_gen; \
149 (top)->va_flags = (fromp)->va_flags; \
150 (top)->va_rdev = (fromp)->va_rdev; \
151 (top)->va_bytes = (fromp)->va_bytes; \
152 (top)->va_filerev = (fromp)->va_filerev; \
153 (top)->va_vaflags = VNOVAL; \
154 (top)->va_spare = VNOVAL; \
157 #define CNV_V2VV_ATTR(top, fromp) do { \
158 (top)->va_type = (fromp)->va_type; \
159 (top)->va_mode = (fromp)->va_mode; \
160 (top)->va_nlink = (fromp)->va_nlink; \
161 (top)->va_uid = (fromp)->va_uid; \
162 (top)->va_gid = (fromp)->va_gid; \
163 (top)->va_fileid = (fromp)->va_fileid; \
164 (top)->va_size = (fromp)->va_size; \
165 (top)->va_blocksize = (fromp)->va_blocksize; \
166 (top)->va_atime = (fromp)->va_atime; \
167 (top)->va_mtime = (fromp)->va_mtime; \
168 (top)->va_ctime = (fromp)->va_ctime; \
169 (top)->va_gen = (fromp)->va_gen; \
170 (top)->va_flags = (fromp)->va_flags; \
171 (top)->va_rdev = (fromp)->va_rdev; \
172 (top)->va_bytes = (fromp)->va_bytes; \
173 (top)->va_filerev = (fromp)->va_filerev; \
176 int coda_kernel_version = CODA_KERNEL_VERSION;
179 venus_root(void *mdp, struct ucred *cred, struct proc *p,
180 /*out*/ CodaFid *VFid)
182 DECL_NO_IN(coda_root); /* sets Isize & Osize */
183 ALLOC_NO_IN(coda_root); /* sets inp & outp */
185 /* send the open to venus. */
186 INIT_IN(inp, CODA_ROOT, cred, p);
188 error = coda_call(mdp, Isize, &Osize, (char *)inp);
192 CODA_FREE(inp, coda_root_size);
197 venus_open(void *mdp, CodaFid *fid, int flag, struct ucred *cred,
198 struct proc *p, /*out*/ struct vnode **vp)
201 DECL(coda_open_by_fd); /* sets Isize & Osize */
202 ALLOC(coda_open_by_fd); /* sets inp & outp */
204 /* send the open to venus. */
205 INIT_IN(&inp->ih, CODA_OPEN_BY_FD, cred, p);
207 CNV_OFLAG(cflag, flag);
210 error = coda_call(mdp, Isize, &Osize, (char *)inp);
211 *vp = error ? NULL : outp->vp;
213 CODA_FREE(inp, coda_open_by_fd_size);
218 venus_close(void *mdp, CodaFid *fid, int flag, struct ucred *cred,
222 DECL_NO_OUT(coda_close); /* sets Isize & Osize */
223 ALLOC_NO_OUT(coda_close); /* sets inp & outp */
225 INIT_IN(&inp->ih, CODA_CLOSE, cred, p);
227 CNV_OFLAG(cflag, flag);
230 error = coda_call(mdp, Isize, &Osize, (char *)inp);
232 CODA_FREE(inp, coda_close_size);
237 * These two calls will not exist!!! The container file is read/written
251 * This is a bit sad too. the ioctl's are for the control file, not for
255 venus_ioctl(void *mdp, CodaFid *fid, int com, int flag, caddr_t data,
256 struct ucred *cred, struct proc *p)
258 DECL(coda_ioctl); /* sets Isize & Osize */
259 struct PioctlData *iap = (struct PioctlData *)data;
262 coda_ioctl_size = VC_MAXMSGSIZE;
263 ALLOC(coda_ioctl); /* sets inp & outp */
265 INIT_IN(&inp->ih, CODA_IOCTL, cred, p);
269 * Command was mutated by increasing its size field to reflect the
270 * path and follow args. we need to subtract that out before sending
271 * the command to venus.
273 inp->cmd = (com & ~(IOCPARM_MASK << 16));
274 tmp = ((com >> 16) & IOCPARM_MASK) - sizeof (char *) - sizeof (int);
275 inp->cmd |= (tmp & IOCPARM_MASK) << 16;
278 inp->len = iap->vi.in_size;
279 inp->data = (char *)(sizeof (struct coda_ioctl_in));
281 error = copyin(iap->vi.in, (char*)inp + (long)inp->data,
284 CODA_FREE(inp, coda_ioctl_size);
288 Osize = VC_MAXMSGSIZE;
289 error = coda_call(mdp, Isize + iap->vi.in_size, &Osize, (char *)inp);
292 * Copy out the out buffer.
295 if (outp->len > iap->vi.out_size)
298 error = copyout((char *)outp + (long)outp->data,
299 iap->vi.out, iap->vi.out_size);
302 CODA_FREE(inp, coda_ioctl_size);
307 venus_getattr(void *mdp, CodaFid *fid, struct ucred *cred, struct vattr *vap)
310 DECL(coda_getattr); /* sets Isize & Osize */
311 ALLOC(coda_getattr); /* sets inp & outp */
314 * Send the open to venus.
316 p = curthread->td_proc;
317 INIT_IN(&inp->ih, CODA_GETATTR, cred, p);
320 error = coda_call(mdp, Isize, &Osize, (char *)inp);
322 CNV_VV2V_ATTR(vap, &outp->attr);
324 CODA_FREE(inp, coda_getattr_size);
329 venus_setattr(void *mdp, CodaFid *fid, struct vattr *vap, struct ucred *cred)
332 DECL_NO_OUT(coda_setattr); /* sets Isize & Osize */
333 ALLOC_NO_OUT(coda_setattr); /* sets inp & outp */
336 * Send the open to venus.
338 p = curthread->td_proc;
339 INIT_IN(&inp->ih, CODA_SETATTR, cred, p);
341 CNV_V2VV_ATTR(&inp->attr, vap);
343 error = coda_call(mdp, Isize, &Osize, (char *)inp);
345 CODA_FREE(inp, coda_setattr_size);
350 venus_access(void *mdp, CodaFid *fid, accmode_t accmode, struct ucred *cred,
353 DECL_NO_OUT(coda_access); /* sets Isize & Osize */
354 ALLOC_NO_OUT(coda_access); /* sets inp & outp */
357 * Send the open to venus.
359 INIT_IN(&inp->ih, CODA_ACCESS, cred, p);
363 * NOTE: FreeBSD and Venus internals use the "data" in the low 3
364 * bits. Hence, the conversion.
366 * XXX: We cast accmode_t variable into an int.
368 inp->flags = (int)accmode>>6;
370 error = coda_call(mdp, Isize, &Osize, (char *)inp);
372 CODA_FREE(inp, coda_access_size);
377 venus_readlink(void *mdp, CodaFid *fid, struct ucred *cred, struct proc *p,
378 /*out*/ char **str, int *len)
380 DECL(coda_readlink); /* sets Isize & Osize */
381 coda_readlink_size += CODA_MAXPATHLEN;
382 ALLOC(coda_readlink); /* sets inp & outp */
385 * Send the open to venus.
387 INIT_IN(&inp->ih, CODA_READLINK, cred, p);
390 Osize += CODA_MAXPATHLEN;
391 error = coda_call(mdp, Isize, &Osize, (char *)inp);
393 CODA_ALLOC(*str, char *, outp->count);
395 bcopy((char *)outp + (long)outp->data, *str, *len);
398 CODA_FREE(inp, coda_readlink_size);
403 venus_fsync(void *mdp, CodaFid *fid, struct proc *p)
405 DECL_NO_OUT(coda_fsync); /* sets Isize & Osize */
406 ALLOC_NO_OUT(coda_fsync); /* sets inp & outp */
409 * Send the open to venus.
411 * XXX: should be cached mount cred.
413 INIT_IN(&inp->ih, CODA_FSYNC, NOCRED, p);
416 error = coda_call(mdp, Isize, &Osize, (char *)inp);
418 CODA_FREE(inp, coda_fsync_size);
423 venus_lookup(void *mdp, CodaFid *fid, const char *nm, int len,
424 struct ucred *cred, struct proc *p, /*out*/ CodaFid *VFid, int *vtype)
426 DECL(coda_lookup); /* sets Isize & Osize */
427 coda_lookup_size += len + 1;
428 ALLOC(coda_lookup); /* sets inp & outp */
431 * Send the open to venus.
433 INIT_IN(&inp->ih, CODA_LOOKUP, cred, p);
437 * NOTE: Between version 1 and version 2 we have added an extra flag
438 * field to this structure. But because the string was at the end
439 * and because of the wierd way we represent strings by having the
440 * slot point to where the string characters are in the "heap", we
441 * can just slip the flag parameter in after the string slot pointer
442 * and veni that don't know better won't see this new flag field ...
443 * Otherwise we'd need two different venus_lookup functions.
446 inp->flags = CLU_CASE_SENSITIVE; /* doesn't really matter for BSD */
447 STRCPY(name, nm, len); /* increments Isize */
449 error = coda_call(mdp, Isize, &Osize, (char *)inp);
452 *vtype = outp->vtype;
455 CODA_FREE(inp, coda_lookup_size);
460 venus_create(void *mdp, CodaFid *fid, const char *nm, int len, int exclusive,
461 int mode, struct vattr *va, struct ucred *cred, struct proc *p,
462 /*out*/ CodaFid *VFid, struct vattr *attr)
464 DECL(coda_create); /* sets Isize & Osize */
465 coda_create_size += len + 1;
466 ALLOC(coda_create); /* sets inp & outp */
469 * Send the open to venus.
471 INIT_IN(&inp->ih, CODA_CREATE, cred, p);
473 inp->excl = exclusive ? C_O_EXCL : 0;
475 CNV_V2VV_ATTR(&inp->attr, va);
478 STRCPY(name, nm, len); /* increments Isize */
480 error = coda_call(mdp, Isize, &Osize, (char *)inp);
483 CNV_VV2V_ATTR(attr, &outp->attr);
486 CODA_FREE(inp, coda_create_size);
491 venus_remove(void *mdp, CodaFid *fid, const char *nm, int len,
492 struct ucred *cred, struct proc *p)
494 DECL_NO_OUT(coda_remove); /* sets Isize & Osize */
495 coda_remove_size += len + 1;
496 ALLOC_NO_OUT(coda_remove); /* sets inp & outp */
499 * Send the open to venus.
501 INIT_IN(&inp->ih, CODA_REMOVE, cred, p);
505 STRCPY(name, nm, len); /* increments Isize */
507 error = coda_call(mdp, Isize, &Osize, (char *)inp);
509 CODA_FREE(inp, coda_remove_size);
514 venus_link(void *mdp, CodaFid *fid, CodaFid *tfid, const char *nm, int len,
515 struct ucred *cred, struct proc *p)
517 DECL_NO_OUT(coda_link); /* sets Isize & Osize */
518 coda_link_size += len + 1;
519 ALLOC_NO_OUT(coda_link); /* sets inp & outp */
522 * Send the open to venus.
524 INIT_IN(&inp->ih, CODA_LINK, cred, p);
525 inp->sourceFid = *fid;
526 inp->destFid = *tfid;
529 STRCPY(tname, nm, len); /* increments Isize */
531 error = coda_call(mdp, Isize, &Osize, (char *)inp);
533 CODA_FREE(inp, coda_link_size);
538 venus_rename(void *mdp, CodaFid *fid, CodaFid *tfid, const char *nm, int len,
539 const char *tnm, int tlen, struct ucred *cred, struct proc *p)
541 DECL_NO_OUT(coda_rename); /* sets Isize & Osize */
542 coda_rename_size += len + 1 + tlen + 1;
543 ALLOC_NO_OUT(coda_rename); /* sets inp & outp */
546 * Send the open to venus.
548 INIT_IN(&inp->ih, CODA_RENAME, cred, p);
549 inp->sourceFid = *fid;
550 inp->destFid = *tfid;
552 inp->srcname = Isize;
553 STRCPY(srcname, nm, len); /* increments Isize */
555 inp->destname = Isize;
556 STRCPY(destname, tnm, tlen); /* increments Isize */
558 error = coda_call(mdp, Isize, &Osize, (char *)inp);
560 CODA_FREE(inp, coda_rename_size);
565 venus_mkdir(void *mdp, CodaFid *fid, const char *nm, int len,
566 struct vattr *va, struct ucred *cred, struct proc *p,
567 /*out*/ CodaFid *VFid, struct vattr *ova)
569 DECL(coda_mkdir); /* sets Isize & Osize */
570 coda_mkdir_size += len + 1;
571 ALLOC(coda_mkdir); /* sets inp & outp */
574 * Send the open to venus.
576 INIT_IN(&inp->ih, CODA_MKDIR, cred, p);
578 CNV_V2VV_ATTR(&inp->attr, va);
581 STRCPY(name, nm, len); /* increments Isize */
583 error = coda_call(mdp, Isize, &Osize, (char *)inp);
586 CNV_VV2V_ATTR(ova, &outp->attr);
589 CODA_FREE(inp, coda_mkdir_size);
594 venus_rmdir(void *mdp, CodaFid *fid, const char *nm, int len,
595 struct ucred *cred, struct proc *p)
597 DECL_NO_OUT(coda_rmdir); /* sets Isize & Osize */
598 coda_rmdir_size += len + 1;
599 ALLOC_NO_OUT(coda_rmdir); /* sets inp & outp */
602 * Send the open to venus.
604 INIT_IN(&inp->ih, CODA_RMDIR, cred, p);
608 STRCPY(name, nm, len); /* increments Isize */
610 error = coda_call(mdp, Isize, &Osize, (char *)inp);
612 CODA_FREE(inp, coda_rmdir_size);
617 venus_symlink(void *mdp, CodaFid *fid, const char *lnm, int llen,
618 const char *nm, int len, struct vattr *va, struct ucred *cred,
621 DECL_NO_OUT(coda_symlink); /* sets Isize & Osize */
622 coda_symlink_size += llen + 1 + len + 1;
623 ALLOC_NO_OUT(coda_symlink); /* sets inp & outp */
626 * Send the open to venus.
628 INIT_IN(&inp->ih, CODA_SYMLINK, cred, p);
630 CNV_V2VV_ATTR(&inp->attr, va);
632 inp->srcname = Isize;
633 STRCPY(srcname, lnm, llen); /* increments Isize */
636 STRCPY(tname, nm, len); /* increments Isize */
638 error = coda_call(mdp, Isize, &Osize, (char *)inp);
640 CODA_FREE(inp, coda_symlink_size);
648 venus_readdir(void *mdp, CodaFid *fid, int count, int offset,
649 struct ucred *cred, struct proc *p, /*out*/ char *buffer, int *len)
651 DECL(coda_readdir); /* sets Isize & Osize */
652 coda_readdir_size = VC_MAXMSGSIZE;
653 ALLOC(coda_readdir); /* sets inp & outp */
656 * Send the open to venus.
658 INIT_IN(&inp->ih, CODA_READDIR, cred, p);
661 inp->offset = offset;
663 Osize = VC_MAXMSGSIZE;
664 error = coda_call(mdp, Isize, &Osize, (char *)inp);
666 bcopy((char *)outp + (long)outp->data, buffer, outp->size);
670 CODA_FREE(inp, coda_readdir_size);
675 venus_fhtovp(void *mdp, CodaFid *fid, struct ucred *cred, struct proc *p,
676 /*out*/ CodaFid *VFid, int *vtype)
678 DECL(coda_vget); /* sets Isize & Osize */
679 ALLOC(coda_vget); /* sets inp & outp */
682 * Send the open to Venus.
684 INIT_IN(&inp->ih, CODA_VGET, cred, p);
687 error = coda_call(mdp, Isize, &Osize, (char *)inp);
690 *vtype = outp->vtype;
693 CODA_FREE(inp, coda_vget_size);