void buf_put (Buf_s **bp) { Buf_s *b = *bp; *bp = NULL; aver(b->blknum == ((Node_s *)b->d)->blknum); aver(0 < b->inuse); ++Cache.stat.puts; --b->inuse; if (!b->inuse) { u64 crc = crc64(b->d, BLOCK_SIZE); if (b->dirty) { if (crc == b->crc) { printf("Didn't change %lld\n", b->blknum); } //XXX: this can be true. aver(crc != b->crc); b->crc = crc; dev_flush(b); b->dirty = FALSE; } else { if (crc != b->crc) { PRd(b->blknum); } aver(crc == b->crc); } } }
static void new_view( /* change view parameters */ register VIEW *v ) { static VIEW viewhist[VIEWHISTLEN]; static unsigned nhist; VIEW *dv; int i, res[2]; int16 *slist; char *err; /* restore previous view? */ if (v == NULL) { if (nhist > 1) /* get one before last setting */ nhist--; else /* else go to end of list */ while (nhist < VIEWHISTLEN && viewhist[nhist].type) nhist++; v = viewhist + ((nhist-1)%VIEWHISTLEN); } else again: if ((err = setview(v)) != NULL) { error(COMMAND, err); return; } if (!dev_view(v)) /* notify display driver */ goto again; if (v->type == VT_PAR) { error(COMMAND, "cannot handle parallel views"); return; } beam_init(odev.firstuse); /* compute new beam set */ for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++) { if ((slist = beam_view(dv, res[0], res[1])) == NULL) { if (!nhist) { error(COMMAND, "invalid starting view"); return; } *v = *(viewhist + ((nhist-1)%VIEWHISTLEN)); goto again; /* XXX overloading dev_section()? */ } DCHECK(*slist < 0, WARNING, "no visible sections in new_view"); for ( ; *slist >= 0; slist++) dev_section(hdgfn[*slist], hdpfn[*slist]); } dev_section(NULL,NULL); /* end section list */ dev_flush(); /* update display */ /* update server */ imm_mode = beam_sync(odev.firstuse) > 0; /* record new view */ if (v < viewhist || v >= viewhist+VIEWHISTLEN) { *(viewhist + (nhist%VIEWHISTLEN)) = *v; nhist++; } }
static int disp_wait(void) /* wait for more input */ { fd_set readset, errset; int flgs; int n; /* see if we can avoid select call */ if (hdlist[0] == NULL) return(RDY_SRV); /* initialize first */ flgs = 0; /* flag what's ready already */ if (imm_mode || fbufcnt(stdin) > 0) flgs |= RDY_SRV; if (sstdin != NULL && fbufcnt(sstdin) > 0) flgs |= RDY_SIN; if (odev.inpready) flgs |= RDY_DEV; if (flgs) /* got something? */ return(flgs); if (dev_flush()) /* else flush output & check keyboard+mouse */ return(RDY_DEV); /* if nothing, we need to call select */ FD_ZERO(&readset); FD_ZERO(&errset); FD_SET(0, &readset); FD_SET(0, &errset); FD_SET(odev.ifd, &readset); FD_SET(odev.ifd, &errset); n = odev.ifd+1; if (sstdin != NULL) { FD_SET(fileno(sstdin), &readset); FD_SET(fileno(sstdin), &errset); if (fileno(sstdin) >= n) n = fileno(sstdin) + 1; } n = select(n, &readset, NULL, &errset, NULL); if (n < 0) { if (errno == EINTR) return(0); error(SYSTEM, "select call failure in disp_wait"); } if (FD_ISSET(0, &readset) || FD_ISSET(0, &errset)) flgs |= RDY_SRV; if (FD_ISSET(odev.ifd, &readset) || FD_ISSET(odev.ifd, &errset)) flgs |= RDY_DEV; if (sstdin != NULL && (FD_ISSET(fileno(sstdin), &readset) || FD_ISSET(fileno(sstdin), &errset))) flgs |= RDY_SIN; return(flgs); }
static void getmove( /* get view change */ XButtonPressedEvent *ebut ) { int movdir = MOVDIR(ebut->button); int movorb = MOVORB(ebut->state); int oldnodesiz = qtMinNodesiz; Window rootw, childw; int rootx, rooty, wx, wy; unsigned int statemask; qtMinNodesiz = 24; /* accelerate update rate */ XNoOp(ourdisplay); while (!XCheckMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent))) { if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, &rootx, &rooty, &wx, &wy, &statemask)) break; /* on another screen */ if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) { sleep(1); continue; } glClear(GL_COLOR_BUFFER_BIT); qtUpdate(); draw_grids(); glFlush(); } if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */ movdir = MOVDIR(levptr(XButtonReleasedEvent)->button); wx = levptr(XButtonReleasedEvent)->x; wy = levptr(XButtonReleasedEvent)->y; moveview(wx, odev.vres-1-wy, movdir, movorb); } dev_flush(); qtMinNodesiz = oldnodesiz; /* restore quadtree resolution */ }
int dev_open_flags(struct device *dev, int flags, int direct, int quiet) { struct stat buf; const char *name; int need_excl = 0, need_rw = 0; if ((flags & O_ACCMODE) == O_RDWR) need_rw = 1; if ((flags & O_EXCL)) need_excl = 1; if (dev->fd >= 0) { if (((dev->flags & DEV_OPENED_RW) || !need_rw) && ((dev->flags & DEV_OPENED_EXCL) || !need_excl)) { dev->open_count++; return 1; } if (dev->open_count && !need_excl) { /* FIXME Ensure we never get here */ log_error(INTERNAL_ERROR "%s already opened read-only", dev_name(dev)); dev->open_count++; } dev_close_immediate(dev); } if (critical_section()) /* FIXME Make this log_error */ log_verbose("dev_open(%s) called while suspended", dev_name(dev)); if (dev->flags & DEV_REGULAR) name = dev_name(dev); else if (!(name = dev_name_confirmed(dev, quiet))) return_0; if (!(dev->flags & DEV_REGULAR)) { if (stat(name, &buf) < 0) { log_sys_error("%s: stat failed", name); return 0; } if (buf.st_rdev != dev->dev) { log_error("%s: device changed", name); return 0; } } #ifdef O_DIRECT_SUPPORT if (direct) { if (!(dev->flags & DEV_O_DIRECT_TESTED)) dev->flags |= DEV_O_DIRECT; if ((dev->flags & DEV_O_DIRECT)) flags |= O_DIRECT; } #endif #ifdef O_NOATIME /* Don't update atime on device inodes */ if (!(dev->flags & DEV_REGULAR)) flags |= O_NOATIME; #endif if ((dev->fd = open(name, flags, 0777)) < 0) { #ifdef O_DIRECT_SUPPORT if (direct && !(dev->flags & DEV_O_DIRECT_TESTED)) { flags &= ~O_DIRECT; if ((dev->fd = open(name, flags, 0777)) >= 0) { dev->flags &= ~DEV_O_DIRECT; log_debug("%s: Not using O_DIRECT", name); goto opened; } } #endif if (quiet) log_sys_debug("open", name); else log_sys_error("open", name); return 0; } #ifdef O_DIRECT_SUPPORT opened: if (direct) dev->flags |= DEV_O_DIRECT_TESTED; #endif dev->open_count++; dev->flags &= ~DEV_ACCESSED_W; if (need_rw) dev->flags |= DEV_OPENED_RW; else dev->flags &= ~DEV_OPENED_RW; if (need_excl) dev->flags |= DEV_OPENED_EXCL; else dev->flags &= ~DEV_OPENED_EXCL; if (!(dev->flags & DEV_REGULAR) && ((fstat(dev->fd, &buf) < 0) || (buf.st_rdev != dev->dev))) { log_error("%s: fstat failed: Has device name changed?", name); dev_close_immediate(dev); return 0; } #ifndef O_DIRECT_SUPPORT if (!(dev->flags & DEV_REGULAR)) dev_flush(dev); #endif if ((flags & O_CREAT) && !(flags & O_TRUNC)) dev->end = lseek(dev->fd, (off_t) 0, SEEK_END); dm_list_add(&_open_devices, &dev->open_list); log_debug("Opened %s %s%s%s", dev_name(dev), dev->flags & DEV_OPENED_RW ? "RW" : "RO", dev->flags & DEV_OPENED_EXCL ? " O_EXCL" : "", dev->flags & DEV_O_DIRECT ? " O_DIRECT" : ""); return 1; }
/* FIXME Also wipe associated metadata area headers? */ int label_remove(struct device *dev) { #pragma pack(8) char buf[LABEL_SIZE]; //__attribute((aligned(8))); #pragma pack() char readbuf[LABEL_SCAN_SIZE]; //__attribute((aligned(8))); int r = 1; uint64_t sector; int wipe; struct labeller_i *li; struct label_header *lh; memset(buf, 0, LABEL_SIZE); log_very_verbose("Scanning for labels to wipe from %s", dev_name(dev)); if (!dev_open(dev)) { stack; return 0; } /* * We flush the device just in case someone is stupid * enough to be trying to import an open pv into lvm. */ dev_flush(dev); if (!dev_read(dev, UINT64_C(0), LABEL_SCAN_SIZE, readbuf)) { log_debug("%s: Failed to read label area", dev_name(dev)); goto out; } /* Scan first few sectors for anything looking like a label */ for (sector = 0; sector < LABEL_SCAN_SECTORS; sector += LABEL_SIZE >> SECTOR_SHIFT) { lh = (struct label_header *) (readbuf + (sector << SECTOR_SHIFT)); wipe = 0; if (!strncmp((char *)lh->id, LABEL_ID, sizeof(lh->id))) { if (xlate64(lh->sector_xl) == sector) wipe = 1; } else { //list_iterate_items(li, &_labellers) { list_iterate_items(li, struct labeller_i, &_labellers) { if (li->l->ops->can_handle(li->l, (char *) lh, sector)) { wipe = 1; break; } } } if (wipe) { log_info("%s: Wiping label at sector %" PRIu64, dev_name(dev), sector); if (!dev_write(dev, sector << SECTOR_SHIFT, LABEL_SIZE, buf)) { log_error("Failed to remove label from %s at " "sector %" PRIu64, dev_name(dev), sector); r = 0; } } } out: if (!dev_close(dev)) stack; return r; }
extern int serv_result(void) /* get next server result and process it */ { static char *buf = NULL; static int bufsiz = 0; MSGHEAD msg; /* read message header */ if (fread((char *)&msg, sizeof(MSGHEAD), 1, stdin) != 1) goto readerr; if (msg.nbytes > 0) { /* get the message body */ if (msg.nbytes > bufsiz) { if (buf == NULL) buf = (char *)malloc(bufsiz=msg.nbytes); else buf = (char *)realloc((void *)buf, bufsiz=msg.nbytes); if (buf == NULL) error(SYSTEM, "out of memory in serv_result"); } if (fread(buf, 1, msg.nbytes, stdin) != msg.nbytes) goto readerr; } switch (msg.type) { /* process results */ case DS_BUNDLE: if (msg.nbytes < sizeof(PACKHEAD) || msg.nbytes != packsiz(((PACKHEAD *)buf)->nr)) error(INTERNAL, "bad display packet from server"); disp_bundle((PACKHEAD *)buf); break; case DS_ADDHOLO: if (msg.nbytes < sizeof(HDGRID)+2) error(INTERNAL, "bad holodeck record from server"); add_holo((HDGRID *)buf, buf+sizeof(HDGRID), buf+sizeof(HDGRID)+strlen(buf+sizeof(HDGRID))+1); break; case DS_OUTSECT: do_outside = 1; goto noargs; case DS_EYESEP: if (msg.nbytes <= 1 || (eyesepdist = atof(buf)) <= FTINY) error(INTERNAL, "bad eye separation from server"); break; case DS_STARTIMM: case DS_ENDIMM: if (!(imm_mode = (msg.type==DS_STARTIMM))) dev_flush(); #ifdef DEBUG { time_t tnow = time(NULL); if (msg.type==DS_STARTIMM) tadd += tnow - tmodesw; else timm += tnow - tmodesw; tmodesw = tnow; } #endif goto noargs; case DS_ACKNOW: case DS_SHUTDOWN: goto noargs; default: error(INTERNAL, "unrecognized result from server process"); } return(msg.type); /* return message type */ noargs: if (msg.nbytes) { sprintf(errmsg, "unexpected body with server message %d", msg.type); error(INTERNAL, errmsg); } return(msg.type); readerr: if (feof(stdin)) error(SYSTEM, "server process died"); error(SYSTEM, "error reading from server process"); return -1; }
static int p_ioctl(int index, struct inode* inode, struct file* filp, unsigned int cmd, unsigned long arg) { context *ct = filp->private_data; pipe_file *pp = &(pipes[index]); tell_inf t_inf; size_inf s_inf; #ifdef KRSA krsa_arg rsa; unsigned char *input = NULL, output[MAX_RSA_MODULUS_LEN]; unsigned int inputlen = 0, outputlen = 0; #endif int err = 0, ret = 0, i; if(!capable(CAP_SYS_ADMIN)) return -EPERM; if(_IOC_TYPE(cmd) != P_IOCMAGIC) return -ENOTTY; if(_IOC_DIR(cmd) & _IOC_READ) { err = !access_ok(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd)); } else if(_IOC_DIR(cmd) & _IOC_WRITE) { err = !access_ok(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd)); } if(err) return -EFAULT; Down(index); switch(cmd) { case P_IOCCLEAR: dev_flush(&(pp->device)); for(i=0; i<=CONTEXT; i++) { ((pp->ct)[i]).hotp = NULL; } break; case P_IOCRESET: memset(pp->ct, 0, sizeof(context)*(CONTEXT+1)); #ifdef __PIPE_SELECT__ pp->r_poll = 1; pp->r_pos = 0; #endif for(i=0; i<=CONTEXT; i++) { ((pp->ct)[i]).r_able = 1; ((pp->ct)[i]).w_able = 1; } RELEASE(index) = 0; WRITERS(index) = 0; dev_flush(&(pp->device)); (pp->device).ctp = pp->ct; (pp->device).pred = 2*BUFF_SIZE; #ifdef KERNEL_PIPE pipeType[index]=PIPE_TYPE_USER_THREAD; #endif break; case P_IOCSIZE: if(copy_from_user((char*)&s_inf, (char*)arg, sizeof(size_inf))) { ret = (-EFAULT); goto my_error; } #ifndef CONFIG_PROC_PIPE_64_BITS_SUPPORT #ifndef CONFIG_PROC_PIPE_KERNEL_SUPPORT_64_BITS if(s_inf.mdas > 0xFFFFFFFFF || s_inf.idxs > 0xFFFFFFFF) { ret = (-EFAULT); goto my_error; } #endif (pp->mda)->size = s_inf.mdas; (pp->idx)->size = s_inf.idxs; #else // !CONFIG_PROC_PIPE_64_BITS_SUPPORT #if 0 (pp->mda)->size = s_inf.mdas; (pp->idx)->size = s_inf.idxs; #else (pp->mda)->size = 0; (pp->idx)->size = 0; #endif pp->mda_size = s_inf.mdas; pp->idx_size = s_inf.idxs; #endif // !CONFIG_PROC_PIPE_64_BITS_SUPPORT for(i=0; i<CONTEXT; i++) { ((pp->ct)[i]).epos = s_inf.mdas; } ((pp->ct)[CONTEXT]).bpos = s_inf.dats; ((pp->ct)[CONTEXT]).epos = s_inf.dats + s_inf.idxs; ((pp->ct)[CONTEXT]).seek = s_inf.dats; break; case P_IOCREVS: ((pp->ct)[CONTEXT]).reverse = *((int*)arg); break; case P_IOCTELL: t_inf.seek = filp->f_pos; t_inf.size = (BPOS(ct))?(BUFF_SIZE):(SIZE(ct)); if(copy_to_user((char*)arg, (char*)&t_inf, sizeof(tell_inf))) { ret = (-EFAULT); goto my_error; } break; case P_IOCRELS: RELEASE(index) = 1; wake_up_interruptible(&(RQ(index))); wake_up_interruptible(&(WQ(index))); break; case P_IOCPRED: (pp->device).pred = *((size_t*)arg); break; #ifdef KRSA case P_IOCKRSA: if(copy_from_user((char*)&rsa, (char*)arg, sizeof(krsa_arg))) { ret = (-EFAULT); goto my_error; } if(!access_ok(VERIFY_READ, (void *)(rsa.len), sizeof(unsigned int))) { ret = (-EFAULT); goto my_error; } if(copy_from_user((char*)&inputlen, (char*)(rsa.len), sizeof(unsigned int))) { ret = (-EFAULT); goto my_error; } if(!(input = kmalloc(inputlen, GFP_KERNEL))) { ret = (-EFAULT); goto my_error; } if(!access_ok(VERIFY_READ, (void *)(rsa.buf), inputlen)) { ret = (-EFAULT); goto my_error; } if(copy_from_user((char*)input, (char*)(rsa.buf), inputlen)) { ret = (-EFAULT); goto my_error; } if(RSAPrivateBlock(output, &outputlen, input, inputlen, &(rsa.key))) { ret = (-EFAULT); goto my_error; } if(!access_ok(VERIFY_WRITE, (void *)(rsa.buf), outputlen)) { ret = (-EFAULT); goto my_error; } if(copy_to_user((char*)(rsa.buf), (char*)output, outputlen)) { ret = (-EFAULT); goto my_error; } if(!access_ok(VERIFY_WRITE, (void *)(rsa.len), sizeof(unsigned int))) { ret = (-EFAULT); goto my_error; } if(copy_to_user((char*)(rsa.len), (char*)&outputlen, sizeof(unsigned int))) { ret = (-EFAULT); goto my_error; } break; #endif #ifdef KERNEL_PIPE case P_IOCPIPETYPE: { int val; get_user(val,(int *)arg); if(val<0) { ret= -EFAULT; break; } #ifdef NO_KERNEL_PIPE_THREAD if(val<=PIPE_TYPE_KERNEL_DIRECT) #else if(val<=PIPE_TYPE_KERNEL_THREAD) #endif pipeType[index]=val; else ret= -EFAULT; } break; case P_IOCPIPESTART: { // int len; kpipe_setup userPath; kpipe_setup *pipeSetup; pipe_thread *pipeThread; DECLARE_WAIT_QUEUE_HEAD(WQ); copy_from_user(&userPath,(kpipe_setup *)arg,sizeof(kpipe_setup)); pipeSetup=pipeManage.threads[index].pipeSetup; pipeThread=&(pipeManage.threads[index]); //select a default pipe type. if(pipeType[index]==PIPE_TYPE_USER_THREAD) pipeType[index]=defaultPipeType; if(pipeType[index]!=PIPE_TYPE_KERNEL_THREAD && pipeType[index]!=PIPE_TYPE_KERNEL_DIRECT) pipeType[index]=PIPE_TYPE_KERNEL_DIRECT; //pipe_type_kernel_direct doesn't support local file. #ifdef NO_KERNEL_PIPE_THREAD if(pipeType[index]==PIPE_TYPE_KERNEL_DIRECT) { if(userPath.type>PIPE_KP) { ret = -EFAULT; break; } } #endif if(*(pipeSetup->host)) //if it has used already? a.dma or a.index. pipeSetup++; if(*(pipeSetup->host)) { HDEBUG("no pipeSetup\n"); ret = -EFAULT; break; } pipeThread->index = index; //a or b. pipeSetup->index = index; // copy_from_user(&pipeSetup->nums,&userPath.nums,1); pipeSetup->nums = userPath.nums; // copy_from_user(&pipeSetup->size,&((kpipe_setup *)arg)->size,sizeof(int)); pipeSetup->size = userPath.size; // copy_from_user(&httpThread->type,&((kpipe_setup *)arg)->type,1); pipeThread->type = userPath.type; //close or keep alive. memcpy(pipeSetup->host,userPath.host,16); memcpy(pipeSetup->procfile,userPath.procfile,16); if(!strcmp(pipeSetup->procfile+strlen("/proc/a."),"mda")) { pipeSetup->pathIndex = MEDIA_PATH_NUM; //dma or index. } else { pipeSetup->pathIndex = INDEX_PATH_NUM; } // pipeSetup->host=HTTP_MALLOC(strlen(userPath->host)+1); pipeSetup->path=HTTP_MALLOC(userPath.pathSize); // pipeSetup->sessionid=HTTP_MALLOC(((kpipe_setup *)arg)->sessionidSize); if(!pipeSetup->path) { // no memroy. } copy_from_user(pipeSetup->path,userPath.path,userPath.pathSize); if(userPath.sessionid){ pipeSetup->sessionid=HTTP_MALLOC(userPath.sessionidSize); copy_from_user(pipeSetup->sessionid,userPath.sessionid,userPath.sessionidSize); } // copy_from_user(&(pipeSetup->port),&userPath.port),sizeof(short)); pipeSetup->port = userPath.port; // copy_from_user(&(pipeSetup->protection),&(((kpipe_setup *)arg)->protection),sizeof(int)); pipeSetup->protection = userPath.protection; HDEBUG("ioctl:%s %d:%s\n",pipeSetup->host,pipeSetup->port,pipeSetup->path); //pipeManage.starts[index][pipeSetup->index].start=1; if(pipeType[index]==PIPE_TYPE_KERNEL_THREAD) { down_interruptible(&(pipeManage.semaphore)); atomic_set(&(pipeManage.starts[index][pipeSetup->pathIndex]),1); //start kernel_thread. //to wakeup manage thread. while(pipeManage.change) { interruptible_sleep_on_timeout(&WQ,5); } pipeManage.change=1; up(&(pipeManage.semaphore)); wake_up_interruptible(&(pipeManage.wq)); } } break; case P_IOCPIPESTOP: { pipe_thread *thread=&(pipeManage.threads[index]); int i; DECLARE_WAIT_QUEUE_HEAD(WQ); RELEASE(index) = 1; if(pipeType[index]==PIPE_TYPE_KERNEL_THREAD) { wake_up_interruptible(&(RQ(index))); wake_up_interruptible(&(WQ(index))); down_interruptible(&(pipeManage.semaphore)); atomic_set(&(pipeManage.stops[index]),1); //to wakeup manage thread. while(pipeManage.change) { interruptible_sleep_on_timeout(&WQ,5); } pipeManage.change = 1; up(&(pipeManage.semaphore)); wake_up_interruptible(&(pipeManage.wq)); } else { for(i=0;i<END_PATH_NUM;i++) { if(thread->pipeSetup[i].path) { HTTP_FREE(thread->pipeSetup[i].path); if(thread->pipeSetup[i].sessionid) HTTP_FREE(thread->pipeSetup[i].sessionid); memset(&(thread->pipeSetup[i]),0,sizeof(kpipe_setup)); } } } } break; #endif default: ret = (-ENOTTY); } my_error: #ifdef KRSA if(input) kfree(input); #endif Up(index); return ret; }