⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cache.c

📁 Linux Kernel 2.6.9 for OMAP1710
💻 C
📖 第 1 页 / 共 2 页
字号:
	if (rp->offset == 0)		rq->readers++;	spin_unlock(&queue_lock);	if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {		err = -EAGAIN;		spin_lock(&queue_lock);		list_move(&rp->q.list, &rq->q.list);		spin_unlock(&queue_lock);	} else {		if (rp->offset + count > rq->len)			count = rq->len - rp->offset;		err = -EFAULT;		if (copy_to_user(buf, rq->buf + rp->offset, count))			goto out;		rp->offset += count;		if (rp->offset >= rq->len) {			rp->offset = 0;			spin_lock(&queue_lock);			list_move(&rp->q.list, &rq->q.list);			spin_unlock(&queue_lock);		}		err = 0;	} out:	if (rp->offset == 0) {		/* need to release rq */		spin_lock(&queue_lock);		rq->readers--;		if (rq->readers == 0 &&		    !test_bit(CACHE_PENDING, &rq->item->flags)) {			list_del(&rq->q.list);			spin_unlock(&queue_lock);			cd->cache_put(rq->item, cd);			kfree(rq->buf);			kfree(rq);		} else			spin_unlock(&queue_lock);	}	if (err == -EAGAIN)		goto again;	up(&queue_io_sem);	return err ? err :  count;}static char write_buf[8192]; /* protected by queue_io_sem */static ssize_tcache_write(struct file *filp, const char __user *buf, size_t count,	    loff_t *ppos){	int err;	struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;	if (count == 0)		return 0;	if (count >= sizeof(write_buf))		return -EINVAL;	down(&queue_io_sem);	if (copy_from_user(write_buf, buf, count)) {		up(&queue_io_sem);		return -EFAULT;	}	write_buf[count] = '\0';	if (cd->cache_parse)		err = cd->cache_parse(cd, write_buf, count);	else		err = -EINVAL;	up(&queue_io_sem);	return err ? err : count;}static DECLARE_WAIT_QUEUE_HEAD(queue_wait);static unsigned intcache_poll(struct file *filp, poll_table *wait){	unsigned int mask;	struct cache_reader *rp = filp->private_data;	struct cache_queue *cq;	struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;	poll_wait(filp, &queue_wait, wait);	/* alway allow write */	mask = POLL_OUT | POLLWRNORM;	if (!rp)		return mask;	spin_lock(&queue_lock);	for (cq= &rp->q; &cq->list != &cd->queue;	     cq = list_entry(cq->list.next, struct cache_queue, list))		if (!cq->reader) {			mask |= POLLIN | POLLRDNORM;			break;		}	spin_unlock(&queue_lock);	return mask;}static intcache_ioctl(struct inode *ino, struct file *filp,	    unsigned int cmd, unsigned long arg){	int len = 0;	struct cache_reader *rp = filp->private_data;	struct cache_queue *cq;	struct cache_detail *cd = PDE(ino)->data;	if (cmd != FIONREAD || !rp)		return -EINVAL;	spin_lock(&queue_lock);	/* only find the length remaining in current request,	 * or the length of the next request	 */	for (cq= &rp->q; &cq->list != &cd->queue;	     cq = list_entry(cq->list.next, struct cache_queue, list))		if (!cq->reader) {			struct cache_request *cr =				container_of(cq, struct cache_request, q);			len = cr->len - rp->offset;			break;		}	spin_unlock(&queue_lock);	return put_user(len, (int __user *)arg);}static intcache_open(struct inode *inode, struct file *filp){	struct cache_reader *rp = NULL;	nonseekable_open(inode, filp);	if (filp->f_mode & FMODE_READ) {		struct cache_detail *cd = PDE(inode)->data;		rp = kmalloc(sizeof(*rp), GFP_KERNEL);		if (!rp)			return -ENOMEM;		rp->offset = 0;		rp->q.reader = 1;		atomic_inc(&cd->readers);		spin_lock(&queue_lock);		list_add(&rp->q.list, &cd->queue);		spin_unlock(&queue_lock);	}	filp->private_data = rp;	return 0;}static intcache_release(struct inode *inode, struct file *filp){	struct cache_reader *rp = filp->private_data;	struct cache_detail *cd = PDE(inode)->data;	if (rp) {		spin_lock(&queue_lock);		if (rp->offset) {			struct cache_queue *cq;			for (cq= &rp->q; &cq->list != &cd->queue;			     cq = list_entry(cq->list.next, struct cache_queue, list))				if (!cq->reader) {					container_of(cq, struct cache_request, q)						->readers--;					break;				}			rp->offset = 0;		}		list_del(&rp->q.list);		spin_unlock(&queue_lock);		filp->private_data = NULL;		kfree(rp);		cd->last_close = get_seconds();		atomic_dec(&cd->readers);	}	return 0;}static struct file_operations cache_file_operations = {	.owner		= THIS_MODULE,	.llseek		= no_llseek,	.read		= cache_read,	.write		= cache_write,	.poll		= cache_poll,	.ioctl		= cache_ioctl, /* for FIONREAD */	.open		= cache_open,	.release	= cache_release,};static void queue_loose(struct cache_detail *detail, struct cache_head *ch){	struct cache_queue *cq;	spin_lock(&queue_lock);	list_for_each_entry(cq, &detail->queue, list)		if (!cq->reader) {			struct cache_request *cr = container_of(cq, struct cache_request, q);			if (cr->item != ch)				continue;			if (cr->readers != 0)				break;			list_del(&cr->q.list);			spin_unlock(&queue_lock);			detail->cache_put(cr->item, detail);			kfree(cr->buf);			kfree(cr);			return;		}	spin_unlock(&queue_lock);}/* * Support routines for text-based upcalls. * Fields are separated by spaces. * Fields are either mangled to quote space tab newline slosh with slosh * or a hexified with a leading \x * Record is terminated with newline. * */void qword_add(char **bpp, int *lp, char *str){	char *bp = *bpp;	int len = *lp;	char c;	if (len < 0) return;	while ((c=*str++) && len)		switch(c) {		case ' ':		case '\t':		case '\n':		case '\\':			if (len >= 4) {				*bp++ = '\\';				*bp++ = '0' + ((c & 0300)>>6);				*bp++ = '0' + ((c & 0070)>>3);				*bp++ = '0' + ((c & 0007)>>0);			}			len -= 4;			break;		default:			*bp++ = c;			len--;		}	if (c || len <1) len = -1;	else {		*bp++ = ' ';		len--;	}	*bpp = bp;	*lp = len;}void qword_addhex(char **bpp, int *lp, char *buf, int blen){	char *bp = *bpp;	int len = *lp;	if (len < 0) return;	if (len > 2) {		*bp++ = '\\';		*bp++ = 'x';		len -= 2;		while (blen && len >= 2) {			unsigned char c = *buf++;			*bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);			*bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);			len -= 2;			blen--;		}	}	if (blen || len<1) len = -1;	else {		*bp++ = ' ';		len--;	}	*bpp = bp;	*lp = len;}void warn_no_listener(struct cache_detail *detail){	if (detail->last_warn != detail->last_close) {		detail->last_warn = detail->last_close;		if (detail->warn_no_listener)			detail->warn_no_listener(detail);	}}/* * register an upcall request to user-space. * Each request is at most one page long. */static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h){	char *buf;	struct cache_request *crq;	char *bp;	int len;	if (detail->cache_request == NULL)		return -EINVAL;	if (atomic_read(&detail->readers) == 0 &&	    detail->last_close < get_seconds() - 30) {			warn_no_listener(detail);			return -EINVAL;	}	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);	if (!buf)		return -EAGAIN;	crq = kmalloc(sizeof (*crq), GFP_KERNEL);	if (!crq) {		kfree(buf);		return -EAGAIN;	}	bp = buf; len = PAGE_SIZE;	detail->cache_request(detail, h, &bp, &len);	if (len < 0) {		kfree(buf);		kfree(crq);		return -EAGAIN;	}	crq->q.reader = 0;	crq->item = cache_get(h);	crq->buf = buf;	crq->len = PAGE_SIZE - len;	crq->readers = 0;	spin_lock(&queue_lock);	list_add_tail(&crq->q.list, &detail->queue);	spin_unlock(&queue_lock);	wake_up(&queue_wait);	return 0;}/* * parse a message from user-space and pass it * to an appropriate cache * Messages are, like requests, separated into fields by * spaces and dequotes as \xHEXSTRING or embedded \nnn octal * * Message is  *   reply cachename expiry key ... content.... * * key and content are both parsed by cache  */#define isodigit(c) (isdigit(c) && c <= '7')int qword_get(char **bpp, char *dest, int bufsize){	/* return bytes copied, or -1 on error */	char *bp = *bpp;	int len = 0;	while (*bp == ' ') bp++;	if (bp[0] == '\\' && bp[1] == 'x') {		/* HEX STRING */		bp += 2;		while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {			int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;			bp++;			byte <<= 4;			byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;			*dest++ = byte;			bp++;			len++;		}	} else {		/* text with \nnn octal quoting */		while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {			if (*bp == '\\' &&			    isodigit(bp[1]) && (bp[1] <= '3') &&			    isodigit(bp[2]) &&			    isodigit(bp[3])) {				int byte = (*++bp -'0');				bp++;				byte = (byte << 3) | (*bp++ - '0');				byte = (byte << 3) | (*bp++ - '0');				*dest++ = byte;				len++;			} else {				*dest++ = *bp++;				len++;			}		}	}	if (*bp != ' ' && *bp != '\n' && *bp != '\0')		return -1;	while (*bp == ' ') bp++;	*bpp = bp;	*dest = '\0';	return len;}/* * support /proc/sunrpc/cache/$CACHENAME/content * as a seqfile. * We call ->cache_show passing NULL for the item to * get a header, then pass each real item in the cache */struct handle {	struct cache_detail *cd;};static void *c_start(struct seq_file *m, loff_t *pos){	loff_t n = *pos;	unsigned hash, entry;	struct cache_head *ch;	struct cache_detail *cd = ((struct handle*)m->private)->cd;		read_lock(&cd->hash_lock);	if (!n--)		return SEQ_START_TOKEN;	hash = n >> 32;	entry = n & ((1LL<<32) - 1);	for (ch=cd->hash_table[hash]; ch; ch=ch->next)		if (!entry--)			return ch;	n &= ~((1LL<<32) - 1);	do {		hash++;		n += 1LL<<32;	} while(hash < cd->hash_size && 		cd->hash_table[hash]==NULL);	if (hash >= cd->hash_size)		return NULL;	*pos = n+1;	return cd->hash_table[hash];}static void *c_next(struct seq_file *m, void *p, loff_t *pos){	struct cache_head *ch = p;	int hash = (*pos >> 32);	struct cache_detail *cd = ((struct handle*)m->private)->cd;	if (p == SEQ_START_TOKEN)		hash = 0;	else if (ch->next == NULL) {		hash++;		*pos += 1LL<<32;	} else {		++*pos;		return ch->next;	}	*pos &= ~((1LL<<32) - 1);	while (hash < cd->hash_size &&	       cd->hash_table[hash] == NULL) {		hash++;		*pos += 1LL<<32;	}	if (hash >= cd->hash_size)		return NULL;	++*pos;	return cd->hash_table[hash];}static void c_stop(struct seq_file *m, void *p){	struct cache_detail *cd = ((struct handle*)m->private)->cd;	read_unlock(&cd->hash_lock);}static int c_show(struct seq_file *m, void *p){	struct cache_head *cp = p;	struct cache_detail *cd = ((struct handle*)m->private)->cd;	if (p == SEQ_START_TOKEN)		return cd->cache_show(m, cd, NULL);	ifdebug(CACHE)		seq_printf(m, "# expiry=%ld refcnt=%d\n",			   cp->expiry_time, atomic_read(&cp->refcnt));	cache_get(cp);	if (cache_check(cd, cp, NULL))		/* cache_check does a cache_put on failure */		seq_printf(m, "# ");	else		cache_put(cp, cd);	return cd->cache_show(m, cd, cp);}struct seq_operations cache_content_op = {	.start	= c_start,	.next	= c_next,	.stop	= c_stop,	.show	= c_show,};static int content_open(struct inode *inode, struct file *file){	int res;	struct handle *han;	struct cache_detail *cd = PDE(inode)->data;	han = kmalloc(sizeof(*han), GFP_KERNEL);	if (han == NULL)		return -ENOMEM;	han->cd = cd;	res = seq_open(file, &cache_content_op);	if (res)		kfree(han);	else		((struct seq_file *)file->private_data)->private = han;	return res;}static int content_release(struct inode *inode, struct file *file){	struct seq_file *m = (struct seq_file *)file->private_data;	struct handle *han = m->private;	kfree(han);	m->private = NULL;	return seq_release(inode, file);}static struct file_operations content_file_operations = {	.open		= content_open,	.read		= seq_read,	.llseek		= seq_lseek,	.release	= content_release,};static ssize_t read_flush(struct file *file, char __user *buf,			    size_t count, loff_t *ppos){	struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;	char tbuf[20];	unsigned long p = *ppos;	int len;	sprintf(tbuf, "%lu\n", cd->flush_time);	len = strlen(tbuf);	if (p >= len)		return 0;	len -= p;	if (len > count) len = count;	if (copy_to_user(buf, (void*)(tbuf+p), len))		len = -EFAULT;	else		*ppos += len;	return len;}static ssize_t write_flush(struct file * file, const char __user * buf,			     size_t count, loff_t *ppos){	struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;	char tbuf[20];	char *ep;	long flushtime;	if (*ppos || count > sizeof(tbuf)-1)		return -EINVAL;	if (copy_from_user(tbuf, buf, count))		return -EFAULT;	tbuf[count] = 0;	flushtime = simple_strtoul(tbuf, &ep, 0);	if (*ep && *ep != '\n')		return -EINVAL;	cd->flush_time = flushtime;	cd->nextcheck = get_seconds();	cache_flush();	*ppos += count;	return count;}static struct file_operations cache_flush_operations = {	.open		= nonseekable_open,	.read		= read_flush,	.write		= write_flush,};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -