2 * Mostly platform independent upcall operations to Venus:
7 * Copyright (C) 1996 Peter J. Braam <braam@maths.ox.ac.uk>,
8 * Michael Callahan <callahan@maths.ox.ac.uk>
10 * Redone for Linux 2.1
11 * Copyright (C) 1997 Carnegie Mellon University
13 * Carnegie Mellon University encourages users of this code to contribute
14 * improvements to the Coda project. Contact Peter Braam <coda@cs.cmu.edu>.
17 #include <asm/system.h>
18 #include <linux/signal.h>
19 #include <linux/sched.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
23 #include <linux/time.h>
25 #include <linux/file.h>
26 #include <linux/stat.h>
27 #include <linux/errno.h>
28 #include <linux/string.h>
29 #include <linux/slab.h>
30 #include <linux/smp_lock.h>
31 #include <asm/uaccess.h>
32 #include <linux/vmalloc.h>
33 #include <linux/vfs.h>
35 #include <linux/coda.h>
36 #include <linux/coda_linux.h>
37 #include <linux/coda_psdev.h>
38 #include <linux/coda_fs_i.h>
39 #include <linux/coda_cache.h>
43 static int coda_upcall(struct venus_comm *vc, int inSize, int *outSize,
44 union inputArgs *buffer);
46 static void *alloc_upcall(int opcode, int size)
50 CODA_ALLOC(inp, union inputArgs *, size);
52 return ERR_PTR(-ENOMEM);
54 inp->ih.opcode = opcode;
55 inp->ih.pid = current->pid;
56 inp->ih.pgid = task_pgrp_nr(current);
57 inp->ih.uid = current_fsuid();
64 inp = (union inputArgs *)alloc_upcall(op, insize); \
65 if (IS_ERR(inp)) { return PTR_ERR(inp); }\
66 outp = (union outputArgs *)(inp); \
70 #define INSIZE(tag) sizeof(struct coda_ ## tag ## _in)
71 #define OUTSIZE(tag) sizeof(struct coda_ ## tag ## _out)
72 #define SIZE(tag) max_t(unsigned int, INSIZE(tag), OUTSIZE(tag))
76 int venus_rootfid(struct super_block *sb, struct CodaFid *fidp)
79 union outputArgs *outp;
80 int insize, outsize, error;
85 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
87 *fidp = outp->coda_root.VFid;
89 CODA_FREE(inp, insize);
93 int venus_getattr(struct super_block *sb, struct CodaFid *fid,
94 struct coda_vattr *attr)
97 union outputArgs *outp;
98 int insize, outsize, error;
100 insize = SIZE(getattr);
102 inp->coda_getattr.VFid = *fid;
104 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
106 *attr = outp->coda_getattr.attr;
108 CODA_FREE(inp, insize);
112 int venus_setattr(struct super_block *sb, struct CodaFid *fid,
113 struct coda_vattr *vattr)
115 union inputArgs *inp;
116 union outputArgs *outp;
117 int insize, outsize, error;
119 insize = SIZE(setattr);
122 inp->coda_setattr.VFid = *fid;
123 inp->coda_setattr.attr = *vattr;
125 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
127 CODA_FREE(inp, insize);
131 int venus_lookup(struct super_block *sb, struct CodaFid *fid,
132 const char *name, int length, int * type,
133 struct CodaFid *resfid)
135 union inputArgs *inp;
136 union outputArgs *outp;
137 int insize, outsize, error;
140 offset = INSIZE(lookup);
141 insize = max_t(unsigned int, offset + length +1, OUTSIZE(lookup));
144 inp->coda_lookup.VFid = *fid;
145 inp->coda_lookup.name = offset;
146 inp->coda_lookup.flags = CLU_CASE_SENSITIVE;
147 /* send Venus a null terminated string */
148 memcpy((char *)(inp) + offset, name, length);
149 *((char *)inp + offset + length) = '\0';
151 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
153 *resfid = outp->coda_lookup.VFid;
154 *type = outp->coda_lookup.vtype;
157 CODA_FREE(inp, insize);
161 int venus_close(struct super_block *sb, struct CodaFid *fid, int flags,
164 union inputArgs *inp;
165 union outputArgs *outp;
166 int insize, outsize, error;
168 insize = SIZE(release);
172 inp->coda_close.VFid = *fid;
173 inp->coda_close.flags = flags;
175 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
177 CODA_FREE(inp, insize);
181 int venus_open(struct super_block *sb, struct CodaFid *fid,
182 int flags, struct file **fh)
184 union inputArgs *inp;
185 union outputArgs *outp;
186 int insize, outsize, error;
188 insize = SIZE(open_by_fd);
189 UPARG(CODA_OPEN_BY_FD);
191 inp->coda_open_by_fd.VFid = *fid;
192 inp->coda_open_by_fd.flags = flags;
194 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
196 *fh = outp->coda_open_by_fd.fh;
198 CODA_FREE(inp, insize);
202 int venus_mkdir(struct super_block *sb, struct CodaFid *dirfid,
203 const char *name, int length,
204 struct CodaFid *newfid, struct coda_vattr *attrs)
206 union inputArgs *inp;
207 union outputArgs *outp;
208 int insize, outsize, error;
211 offset = INSIZE(mkdir);
212 insize = max_t(unsigned int, offset + length + 1, OUTSIZE(mkdir));
215 inp->coda_mkdir.VFid = *dirfid;
216 inp->coda_mkdir.attr = *attrs;
217 inp->coda_mkdir.name = offset;
218 /* Venus must get null terminated string */
219 memcpy((char *)(inp) + offset, name, length);
220 *((char *)inp + offset + length) = '\0';
222 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
224 *attrs = outp->coda_mkdir.attr;
225 *newfid = outp->coda_mkdir.VFid;
228 CODA_FREE(inp, insize);
233 int venus_rename(struct super_block *sb, struct CodaFid *old_fid,
234 struct CodaFid *new_fid, size_t old_length,
235 size_t new_length, const char *old_name,
236 const char *new_name)
238 union inputArgs *inp;
239 union outputArgs *outp;
240 int insize, outsize, error;
243 offset = INSIZE(rename);
244 insize = max_t(unsigned int, offset + new_length + old_length + 8,
248 inp->coda_rename.sourceFid = *old_fid;
249 inp->coda_rename.destFid = *new_fid;
250 inp->coda_rename.srcname = offset;
252 /* Venus must receive an null terminated string */
253 s = ( old_length & ~0x3) +4; /* round up to word boundary */
254 memcpy((char *)(inp) + offset, old_name, old_length);
255 *((char *)inp + offset + old_length) = '\0';
257 /* another null terminated string for Venus */
259 inp->coda_rename.destname = offset;
260 s = ( new_length & ~0x3) +4; /* round up to word boundary */
261 memcpy((char *)(inp) + offset, new_name, new_length);
262 *((char *)inp + offset + new_length) = '\0';
264 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
266 CODA_FREE(inp, insize);
270 int venus_create(struct super_block *sb, struct CodaFid *dirfid,
271 const char *name, int length, int excl, int mode,
272 struct CodaFid *newfid, struct coda_vattr *attrs)
274 union inputArgs *inp;
275 union outputArgs *outp;
276 int insize, outsize, error;
279 offset = INSIZE(create);
280 insize = max_t(unsigned int, offset + length + 1, OUTSIZE(create));
283 inp->coda_create.VFid = *dirfid;
284 inp->coda_create.attr.va_mode = mode;
285 inp->coda_create.excl = excl;
286 inp->coda_create.mode = mode;
287 inp->coda_create.name = offset;
289 /* Venus must get null terminated string */
290 memcpy((char *)(inp) + offset, name, length);
291 *((char *)inp + offset + length) = '\0';
293 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
295 *attrs = outp->coda_create.attr;
296 *newfid = outp->coda_create.VFid;
299 CODA_FREE(inp, insize);
303 int venus_rmdir(struct super_block *sb, struct CodaFid *dirfid,
304 const char *name, int length)
306 union inputArgs *inp;
307 union outputArgs *outp;
308 int insize, outsize, error;
311 offset = INSIZE(rmdir);
312 insize = max_t(unsigned int, offset + length + 1, OUTSIZE(rmdir));
315 inp->coda_rmdir.VFid = *dirfid;
316 inp->coda_rmdir.name = offset;
317 memcpy((char *)(inp) + offset, name, length);
318 *((char *)inp + offset + length) = '\0';
320 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
322 CODA_FREE(inp, insize);
326 int venus_remove(struct super_block *sb, struct CodaFid *dirfid,
327 const char *name, int length)
329 union inputArgs *inp;
330 union outputArgs *outp;
331 int error=0, insize, outsize, offset;
333 offset = INSIZE(remove);
334 insize = max_t(unsigned int, offset + length + 1, OUTSIZE(remove));
337 inp->coda_remove.VFid = *dirfid;
338 inp->coda_remove.name = offset;
339 memcpy((char *)(inp) + offset, name, length);
340 *((char *)inp + offset + length) = '\0';
342 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
344 CODA_FREE(inp, insize);
348 int venus_readlink(struct super_block *sb, struct CodaFid *fid,
349 char *buffer, int *length)
351 union inputArgs *inp;
352 union outputArgs *outp;
353 int insize, outsize, error;
357 insize = max_t(unsigned int,
358 INSIZE(readlink), OUTSIZE(readlink)+ *length + 1);
359 UPARG(CODA_READLINK);
361 inp->coda_readlink.VFid = *fid;
363 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
365 retlen = outp->coda_readlink.count;
366 if ( retlen > *length )
369 result = (char *)outp + (long)outp->coda_readlink.data;
370 memcpy(buffer, result, retlen);
371 *(buffer + retlen) = '\0';
374 CODA_FREE(inp, insize);
380 int venus_link(struct super_block *sb, struct CodaFid *fid,
381 struct CodaFid *dirfid, const char *name, int len )
383 union inputArgs *inp;
384 union outputArgs *outp;
385 int insize, outsize, error;
388 offset = INSIZE(link);
389 insize = max_t(unsigned int, offset + len + 1, OUTSIZE(link));
392 inp->coda_link.sourceFid = *fid;
393 inp->coda_link.destFid = *dirfid;
394 inp->coda_link.tname = offset;
396 /* make sure strings are null terminated */
397 memcpy((char *)(inp) + offset, name, len);
398 *((char *)inp + offset + len) = '\0';
400 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
402 CODA_FREE(inp, insize);
406 int venus_symlink(struct super_block *sb, struct CodaFid *fid,
407 const char *name, int len,
408 const char *symname, int symlen)
410 union inputArgs *inp;
411 union outputArgs *outp;
412 int insize, outsize, error;
415 offset = INSIZE(symlink);
416 insize = max_t(unsigned int, offset + len + symlen + 8, OUTSIZE(symlink));
419 /* inp->coda_symlink.attr = *tva; XXXXXX */
420 inp->coda_symlink.VFid = *fid;
422 /* Round up to word boundary and null terminate */
423 inp->coda_symlink.srcname = offset;
424 s = ( symlen & ~0x3 ) + 4;
425 memcpy((char *)(inp) + offset, symname, symlen);
426 *((char *)inp + offset + symlen) = '\0';
428 /* Round up to word boundary and null terminate */
430 inp->coda_symlink.tname = offset;
431 s = (len & ~0x3) + 4;
432 memcpy((char *)(inp) + offset, name, len);
433 *((char *)inp + offset + len) = '\0';
435 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
437 CODA_FREE(inp, insize);
441 int venus_fsync(struct super_block *sb, struct CodaFid *fid)
443 union inputArgs *inp;
444 union outputArgs *outp;
445 int insize, outsize, error;
450 inp->coda_fsync.VFid = *fid;
451 error = coda_upcall(coda_vcp(sb), sizeof(union inputArgs),
454 CODA_FREE(inp, insize);
458 int venus_access(struct super_block *sb, struct CodaFid *fid, int mask)
460 union inputArgs *inp;
461 union outputArgs *outp;
462 int insize, outsize, error;
464 insize = SIZE(access);
467 inp->coda_access.VFid = *fid;
468 inp->coda_access.flags = mask;
470 error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
472 CODA_FREE(inp, insize);
477 int venus_pioctl(struct super_block *sb, struct CodaFid *fid,
478 unsigned int cmd, struct PioctlData *data)
480 union inputArgs *inp;
481 union outputArgs *outp;
482 int insize, outsize, error;
485 insize = VC_MAXMSGSIZE;
488 /* build packet for Venus */
489 if (data->vi.in_size > VC_MAXDATASIZE) {
494 if (data->vi.out_size > VC_MAXDATASIZE) {
499 inp->coda_ioctl.VFid = *fid;
501 /* the cmd field was mutated by increasing its size field to
502 * reflect the path and follow args. We need to subtract that
503 * out before sending the command to Venus. */
504 inp->coda_ioctl.cmd = (cmd & ~(PIOCPARM_MASK << 16));
505 iocsize = ((cmd >> 16) & PIOCPARM_MASK) - sizeof(char *) - sizeof(int);
506 inp->coda_ioctl.cmd |= (iocsize & PIOCPARM_MASK) << 16;
508 /* in->coda_ioctl.rwflag = flag; */
509 inp->coda_ioctl.len = data->vi.in_size;
510 inp->coda_ioctl.data = (char *)(INSIZE(ioctl));
512 /* get the data out of user space */
513 if ( copy_from_user((char*)inp + (long)inp->coda_ioctl.data,
514 data->vi.in, data->vi.in_size) ) {
519 error = coda_upcall(coda_vcp(sb), SIZE(ioctl) + data->vi.in_size,
523 printk("coda_pioctl: Venus returns: %d for %s\n",
524 error, coda_f2s(fid));
528 if (outsize < (long)outp->coda_ioctl.data + outp->coda_ioctl.len) {
533 /* Copy out the OUT buffer. */
534 if (outp->coda_ioctl.len > data->vi.out_size) {
539 /* Copy out the OUT buffer. */
540 if (copy_to_user(data->vi.out,
541 (char *)outp + (long)outp->coda_ioctl.data,
542 outp->coda_ioctl.len)) {
548 CODA_FREE(inp, insize);
552 int venus_statfs(struct dentry *dentry, struct kstatfs *sfs)
554 union inputArgs *inp;
555 union outputArgs *outp;
556 int insize, outsize, error;
558 insize = max_t(unsigned int, INSIZE(statfs), OUTSIZE(statfs));
561 error = coda_upcall(coda_vcp(dentry->d_sb), insize, &outsize, inp);
563 sfs->f_blocks = outp->coda_statfs.stat.f_blocks;
564 sfs->f_bfree = outp->coda_statfs.stat.f_bfree;
565 sfs->f_bavail = outp->coda_statfs.stat.f_bavail;
566 sfs->f_files = outp->coda_statfs.stat.f_files;
567 sfs->f_ffree = outp->coda_statfs.stat.f_ffree;
570 CODA_FREE(inp, insize);
575 * coda_upcall and coda_downcall routines.
577 static void coda_block_signals(sigset_t *old)
579 spin_lock_irq(¤t->sighand->siglock);
580 *old = current->blocked;
582 sigfillset(¤t->blocked);
583 sigdelset(¤t->blocked, SIGKILL);
584 sigdelset(¤t->blocked, SIGSTOP);
585 sigdelset(¤t->blocked, SIGINT);
588 spin_unlock_irq(¤t->sighand->siglock);
591 static void coda_unblock_signals(sigset_t *old)
593 spin_lock_irq(¤t->sighand->siglock);
594 current->blocked = *old;
596 spin_unlock_irq(¤t->sighand->siglock);
599 /* Don't allow signals to interrupt the following upcalls before venus
601 * - CODA_CLOSE or CODA_RELEASE upcall (to avoid reference count problems)
602 * - CODA_STORE (to avoid data loss)
604 #define CODA_INTERRUPTIBLE(r) (!coda_hard && \
605 (((r)->uc_opcode != CODA_CLOSE && \
606 (r)->uc_opcode != CODA_STORE && \
607 (r)->uc_opcode != CODA_RELEASE) || \
608 (r)->uc_flags & CODA_REQ_READ))
610 static inline void coda_waitfor_upcall(struct upc_req *req)
612 DECLARE_WAITQUEUE(wait, current);
613 unsigned long timeout = jiffies + coda_timeout * HZ;
617 coda_block_signals(&old);
620 add_wait_queue(&req->uc_sleep, &wait);
622 if (CODA_INTERRUPTIBLE(req))
623 set_current_state(TASK_INTERRUPTIBLE);
625 set_current_state(TASK_UNINTERRUPTIBLE);
628 if (req->uc_flags & (CODA_REQ_WRITE | CODA_REQ_ABORT))
631 if (blocked && time_after(jiffies, timeout) &&
632 CODA_INTERRUPTIBLE(req))
634 coda_unblock_signals(&old);
638 if (signal_pending(current)) {
639 list_del(&req->uc_chain);
644 schedule_timeout(HZ);
649 coda_unblock_signals(&old);
651 remove_wait_queue(&req->uc_sleep, &wait);
652 set_current_state(TASK_RUNNING);
657 * coda_upcall will return an error in the case of
658 * failed communication with Venus _or_ will peek at Venus
659 * reply and return Venus' error.
661 * As venus has 2 types of errors, normal errors (positive) and internal
662 * errors (negative), normal errors are negated, while internal errors
663 * are all mapped to -EINTR, while showing a nice warning message. (jh)
665 static int coda_upcall(struct venus_comm *vcp,
666 int inSize, int *outSize,
667 union inputArgs *buffer)
669 union outputArgs *out;
670 union inputArgs *sig_inputArgs;
671 struct upc_req *req = NULL, *sig_req;
676 if (!vcp->vc_inuse) {
677 printk(KERN_NOTICE "coda: Venus dead, not sending upcall\n");
682 /* Format the request message. */
683 req = kmalloc(sizeof(struct upc_req), GFP_KERNEL);
689 req->uc_data = (void *)buffer;
691 req->uc_inSize = inSize;
692 req->uc_outSize = *outSize ? *outSize : inSize;
693 req->uc_opcode = ((union inputArgs *)buffer)->ih.opcode;
694 req->uc_unique = ++vcp->vc_seq;
695 init_waitqueue_head(&req->uc_sleep);
697 /* Fill in the common input args. */
698 ((union inputArgs *)buffer)->ih.unique = req->uc_unique;
700 /* Append msg to pending queue and poke Venus. */
701 list_add_tail(&req->uc_chain, &vcp->vc_pending);
703 wake_up_interruptible(&vcp->vc_waitq);
704 /* We can be interrupted while we wait for Venus to process
705 * our request. If the interrupt occurs before Venus has read
706 * the request, we dequeue and return. If it occurs after the
707 * read but before the reply, we dequeue, send a signal
708 * message, and return. If it occurs after the reply we ignore
709 * it. In no case do we want to restart the syscall. If it
710 * was interrupted by a venus shutdown (psdev_close), return
713 /* Go to sleep. Wake up on signals only after the timeout. */
714 coda_waitfor_upcall(req);
716 /* Op went through, interrupt or not... */
717 if (req->uc_flags & CODA_REQ_WRITE) {
718 out = (union outputArgs *)req->uc_data;
719 /* here we map positive Venus errors to kernel errors */
720 error = -out->oh.result;
721 *outSize = req->uc_outSize;
726 if ((req->uc_flags & CODA_REQ_ABORT) || !signal_pending(current)) {
727 printk(KERN_WARNING "coda: Unexpected interruption.\n");
731 /* Interrupted before venus read it. */
732 if (!(req->uc_flags & CODA_REQ_READ))
735 /* Venus saw the upcall, make sure we can send interrupt signal */
736 if (!vcp->vc_inuse) {
737 printk(KERN_INFO "coda: Venus dead, not sending signal.\n");
742 sig_req = kmalloc(sizeof(struct upc_req), GFP_KERNEL);
743 if (!sig_req) goto exit;
745 CODA_ALLOC((sig_req->uc_data), char *, sizeof(struct coda_in_hdr));
746 if (!sig_req->uc_data) {
752 sig_inputArgs = (union inputArgs *)sig_req->uc_data;
753 sig_inputArgs->ih.opcode = CODA_SIGNAL;
754 sig_inputArgs->ih.unique = req->uc_unique;
756 sig_req->uc_flags = CODA_REQ_ASYNC;
757 sig_req->uc_opcode = sig_inputArgs->ih.opcode;
758 sig_req->uc_unique = sig_inputArgs->ih.unique;
759 sig_req->uc_inSize = sizeof(struct coda_in_hdr);
760 sig_req->uc_outSize = sizeof(struct coda_in_hdr);
762 /* insert at head of queue! */
763 list_add(&(sig_req->uc_chain), &vcp->vc_pending);
764 wake_up_interruptible(&vcp->vc_waitq);
773 The statements below are part of the Coda opportunistic
774 programming -- taken from the Mach/BSD kernel code for Coda.
775 You don't get correct semantics by stating what needs to be
776 done without guaranteeing the invariants needed for it to happen.
777 When will be have time to find out what exactly is going on? (pjb)
782 * There are 7 cases where cache invalidations occur. The semantics
783 * of each is listed here:
785 * CODA_FLUSH -- flush all entries from the name cache and the cnode cache.
786 * CODA_PURGEUSER -- flush all entries from the name cache for a specific user
787 * This call is a result of token expiration.
789 * The next arise as the result of callbacks on a file or directory.
790 * CODA_ZAPFILE -- flush the cached attributes for a file.
792 * CODA_ZAPDIR -- flush the attributes for the dir and
793 * force a new lookup for all the children
797 * The next is a result of Venus detecting an inconsistent file.
798 * CODA_PURGEFID -- flush the attribute for the file
799 * purge it and its children from the dcache
801 * The last allows Venus to replace local fids with global ones
802 * during reintegration.
804 * CODA_REPLACE -- replace one CodaFid with another throughout the name cache */
806 int coda_downcall(struct venus_comm *vcp, int opcode, union outputArgs *out)
808 struct inode *inode = NULL;
809 struct CodaFid *fid, *newfid;
810 struct super_block *sb;
812 /* Handle invalidation requests. */
815 if (!sb || !sb->s_root)
820 coda_cache_clear_all(sb);
821 shrink_dcache_sb(sb);
822 if (sb->s_root->d_inode)
823 coda_flag_inode(sb->s_root->d_inode, C_FLUSH);
827 coda_cache_clear_all(sb);
831 fid = &out->coda_zapdir.CodaFid;
832 inode = coda_fid_to_inode(fid, sb);
834 coda_flag_inode_children(inode, C_PURGE);
835 coda_flag_inode(inode, C_VATTR);
840 fid = &out->coda_zapfile.CodaFid;
841 inode = coda_fid_to_inode(fid, sb);
843 coda_flag_inode(inode, C_VATTR);
847 fid = &out->coda_purgefid.CodaFid;
848 inode = coda_fid_to_inode(fid, sb);
850 coda_flag_inode_children(inode, C_PURGE);
852 /* catch the dentries later if some are still busy */
853 coda_flag_inode(inode, C_PURGE);
854 d_prune_aliases(inode);
860 fid = &out->coda_replace.OldFid;
861 newfid = &out->coda_replace.NewFid;
862 inode = coda_fid_to_inode(fid, sb);
864 coda_replace_fid(inode, fid, newfid);