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

📄 relay.c

📁 linux 2.6.19 kernel source code before patching
💻 C
📖 第 1 页 / 共 2 页
字号:
	strlcpy(chan->base_filename, base_filename, NAME_MAX);	setup_callbacks(chan, cb);	kref_init(&chan->kref);	mutex_lock(&relay_channels_mutex);	for_each_online_cpu(i) {		chan->buf[i] = relay_open_buf(chan, i);		if (!chan->buf[i])			goto free_bufs;	}	list_add(&chan->list, &relay_channels);	mutex_unlock(&relay_channels_mutex);	return chan;free_bufs:	for_each_online_cpu(i) {		if (!chan->buf[i])			break;		relay_close_buf(chan->buf[i]);	}	kref_put(&chan->kref, relay_destroy_channel);	mutex_unlock(&relay_channels_mutex);	return NULL;}EXPORT_SYMBOL_GPL(relay_open);/** *	relay_switch_subbuf - switch to a new sub-buffer *	@buf: channel buffer *	@length: size of current event * *	Returns either the length passed in or 0 if full. * *	Performs sub-buffer-switch tasks such as invoking callbacks, *	updating padding counts, waking up readers, etc. */size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length){	void *old, *new;	size_t old_subbuf, new_subbuf;	if (unlikely(length > buf->chan->subbuf_size))		goto toobig;	if (buf->offset != buf->chan->subbuf_size + 1) {		buf->prev_padding = buf->chan->subbuf_size - buf->offset;		old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;		buf->padding[old_subbuf] = buf->prev_padding;		buf->subbufs_produced++;		buf->dentry->d_inode->i_size += buf->chan->subbuf_size -			buf->padding[old_subbuf];		smp_mb();		if (waitqueue_active(&buf->read_wait))			/*			 * Calling wake_up_interruptible() from here			 * will deadlock if we happen to be logging			 * from the scheduler (trying to re-grab			 * rq->lock), so defer it.			 */			__mod_timer(&buf->timer, jiffies + 1);	}	old = buf->data;	new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;	new = buf->start + new_subbuf * buf->chan->subbuf_size;	buf->offset = 0;	if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {		buf->offset = buf->chan->subbuf_size + 1;		return 0;	}	buf->data = new;	buf->padding[new_subbuf] = 0;	if (unlikely(length + buf->offset > buf->chan->subbuf_size))		goto toobig;	return length;toobig:	buf->chan->last_toobig = length;	return 0;}EXPORT_SYMBOL_GPL(relay_switch_subbuf);/** *	relay_subbufs_consumed - update the buffer's sub-buffers-consumed count *	@chan: the channel *	@cpu: the cpu associated with the channel buffer to update *	@subbufs_consumed: number of sub-buffers to add to current buf's count * *	Adds to the channel buffer's consumed sub-buffer count. *	subbufs_consumed should be the number of sub-buffers newly consumed, *	not the total consumed. * *	NOTE. Kernel clients don't need to call this function if the channel *	mode is 'overwrite'. */void relay_subbufs_consumed(struct rchan *chan,			    unsigned int cpu,			    size_t subbufs_consumed){	struct rchan_buf *buf;	if (!chan)		return;	if (cpu >= NR_CPUS || !chan->buf[cpu])		return;	buf = chan->buf[cpu];	buf->subbufs_consumed += subbufs_consumed;	if (buf->subbufs_consumed > buf->subbufs_produced)		buf->subbufs_consumed = buf->subbufs_produced;}EXPORT_SYMBOL_GPL(relay_subbufs_consumed);/** *	relay_close - close the channel *	@chan: the channel * *	Closes all channel buffers and frees the channel. */void relay_close(struct rchan *chan){	unsigned int i;	if (!chan)		return;	mutex_lock(&relay_channels_mutex);	if (chan->is_global && chan->buf[0])		relay_close_buf(chan->buf[0]);	else		for_each_possible_cpu(i)			if (chan->buf[i])				relay_close_buf(chan->buf[i]);	if (chan->last_toobig)		printk(KERN_WARNING "relay: one or more items not logged "		       "[item size (%Zd) > sub-buffer size (%Zd)]\n",		       chan->last_toobig, chan->subbuf_size);	list_del(&chan->list);	kref_put(&chan->kref, relay_destroy_channel);	mutex_unlock(&relay_channels_mutex);}EXPORT_SYMBOL_GPL(relay_close);/** *	relay_flush - close the channel *	@chan: the channel * *	Flushes all channel buffers, i.e. forces buffer switch. */void relay_flush(struct rchan *chan){	unsigned int i;	if (!chan)		return;	if (chan->is_global && chan->buf[0]) {		relay_switch_subbuf(chan->buf[0], 0);		return;	}	mutex_lock(&relay_channels_mutex);	for_each_possible_cpu(i)		if (chan->buf[i])			relay_switch_subbuf(chan->buf[i], 0);	mutex_unlock(&relay_channels_mutex);}EXPORT_SYMBOL_GPL(relay_flush);/** *	relay_file_open - open file op for relay files *	@inode: the inode *	@filp: the file * *	Increments the channel buffer refcount. */static int relay_file_open(struct inode *inode, struct file *filp){	struct rchan_buf *buf = inode->i_private;	kref_get(&buf->kref);	filp->private_data = buf;	return 0;}/** *	relay_file_mmap - mmap file op for relay files *	@filp: the file *	@vma: the vma describing what to map * *	Calls upon relay_mmap_buf() to map the file into user space. */static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma){	struct rchan_buf *buf = filp->private_data;	return relay_mmap_buf(buf, vma);}/** *	relay_file_poll - poll file op for relay files *	@filp: the file *	@wait: poll table * *	Poll implemention. */static unsigned int relay_file_poll(struct file *filp, poll_table *wait){	unsigned int mask = 0;	struct rchan_buf *buf = filp->private_data;	if (buf->finalized)		return POLLERR;	if (filp->f_mode & FMODE_READ) {		poll_wait(filp, &buf->read_wait, wait);		if (!relay_buf_empty(buf))			mask |= POLLIN | POLLRDNORM;	}	return mask;}/** *	relay_file_release - release file op for relay files *	@inode: the inode *	@filp: the file * *	Decrements the channel refcount, as the filesystem is *	no longer using it. */static int relay_file_release(struct inode *inode, struct file *filp){	struct rchan_buf *buf = filp->private_data;	kref_put(&buf->kref, relay_remove_buf);	return 0;}/* *	relay_file_read_consume - update the consumed count for the buffer */static void relay_file_read_consume(struct rchan_buf *buf,				    size_t read_pos,				    size_t bytes_consumed){	size_t subbuf_size = buf->chan->subbuf_size;	size_t n_subbufs = buf->chan->n_subbufs;	size_t read_subbuf;	if (buf->bytes_consumed + bytes_consumed > subbuf_size) {		relay_subbufs_consumed(buf->chan, buf->cpu, 1);		buf->bytes_consumed = 0;	}	buf->bytes_consumed += bytes_consumed;	if (!read_pos)		read_subbuf = buf->subbufs_consumed % n_subbufs;	else		read_subbuf = read_pos / buf->chan->subbuf_size;	if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {		if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&		    (buf->offset == subbuf_size))			return;		relay_subbufs_consumed(buf->chan, buf->cpu, 1);		buf->bytes_consumed = 0;	}}/* *	relay_file_read_avail - boolean, are there unconsumed bytes available? */static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos){	size_t subbuf_size = buf->chan->subbuf_size;	size_t n_subbufs = buf->chan->n_subbufs;	size_t produced = buf->subbufs_produced;	size_t consumed = buf->subbufs_consumed;	relay_file_read_consume(buf, read_pos, 0);	if (unlikely(buf->offset > subbuf_size)) {		if (produced == consumed)			return 0;		return 1;	}	if (unlikely(produced - consumed >= n_subbufs)) {		consumed = produced - n_subbufs + 1;		buf->subbufs_consumed = consumed;		buf->bytes_consumed = 0;	}		produced = (produced % n_subbufs) * subbuf_size + buf->offset;	consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;	if (consumed > produced)		produced += n_subbufs * subbuf_size;		if (consumed == produced)		return 0;	return 1;}/** *	relay_file_read_subbuf_avail - return bytes available in sub-buffer *	@read_pos: file read position *	@buf: relay channel buffer */static size_t relay_file_read_subbuf_avail(size_t read_pos,					   struct rchan_buf *buf){	size_t padding, avail = 0;	size_t read_subbuf, read_offset, write_subbuf, write_offset;	size_t subbuf_size = buf->chan->subbuf_size;	write_subbuf = (buf->data - buf->start) / subbuf_size;	write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;	read_subbuf = read_pos / subbuf_size;	read_offset = read_pos % subbuf_size;	padding = buf->padding[read_subbuf];	if (read_subbuf == write_subbuf) {		if (read_offset + padding < write_offset)			avail = write_offset - (read_offset + padding);	} else		avail = (subbuf_size - padding) - read_offset;	return avail;}/** *	relay_file_read_start_pos - find the first available byte to read *	@read_pos: file read position *	@buf: relay channel buffer * *	If the @read_pos is in the middle of padding, return the *	position of the first actually available byte, otherwise *	return the original value. */static size_t relay_file_read_start_pos(size_t read_pos,					struct rchan_buf *buf){	size_t read_subbuf, padding, padding_start, padding_end;	size_t subbuf_size = buf->chan->subbuf_size;	size_t n_subbufs = buf->chan->n_subbufs;	size_t consumed = buf->subbufs_consumed % n_subbufs;	if (!read_pos)		read_pos = consumed * subbuf_size + buf->bytes_consumed;	read_subbuf = read_pos / subbuf_size;	padding = buf->padding[read_subbuf];	padding_start = (read_subbuf + 1) * subbuf_size - padding;	padding_end = (read_subbuf + 1) * subbuf_size;	if (read_pos >= padding_start && read_pos < padding_end) {		read_subbuf = (read_subbuf + 1) % n_subbufs;		read_pos = read_subbuf * subbuf_size;	}	return read_pos;}/** *	relay_file_read_end_pos - return the new read position *	@read_pos: file read position *	@buf: relay channel buffer *	@count: number of bytes to be read */static size_t relay_file_read_end_pos(struct rchan_buf *buf,				      size_t read_pos,				      size_t count){	size_t read_subbuf, padding, end_pos;	size_t subbuf_size = buf->chan->subbuf_size;	size_t n_subbufs = buf->chan->n_subbufs;	read_subbuf = read_pos / subbuf_size;	padding = buf->padding[read_subbuf];	if (read_pos % subbuf_size + count + padding == subbuf_size)		end_pos = (read_subbuf + 1) * subbuf_size;	else		end_pos = read_pos + count;	if (end_pos >= subbuf_size * n_subbufs)		end_pos = 0;	return end_pos;}/* *	subbuf_read_actor - read up to one subbuf's worth of data */static int subbuf_read_actor(size_t read_start,			     struct rchan_buf *buf,			     size_t avail,			     read_descriptor_t *desc,			     read_actor_t actor){	void *from;	int ret = 0;	from = buf->start + read_start;	ret = avail;	if (copy_to_user(desc->arg.buf, from, avail)) {		desc->error = -EFAULT;		ret = 0;	}	desc->arg.data += ret;	desc->written += ret;	desc->count -= ret;	return ret;}/* *	subbuf_send_actor - send up to one subbuf's worth of data */static int subbuf_send_actor(size_t read_start,			     struct rchan_buf *buf,			     size_t avail,			     read_descriptor_t *desc,			     read_actor_t actor){	unsigned long pidx, poff;	unsigned int subbuf_pages;	int ret = 0;	subbuf_pages = buf->chan->alloc_size >> PAGE_SHIFT;	pidx = (read_start / PAGE_SIZE) % subbuf_pages;	poff = read_start & ~PAGE_MASK;	while (avail) {		struct page *p = buf->page_array[pidx];		unsigned int len;		len = PAGE_SIZE - poff;		if (len > avail)			len = avail;		len = actor(desc, p, poff, len);		if (desc->error)			break;		avail -= len;		ret += len;		poff = 0;		pidx = (pidx + 1) % subbuf_pages;	}	return ret;}typedef int (*subbuf_actor_t) (size_t read_start,			       struct rchan_buf *buf,			       size_t avail,			       read_descriptor_t *desc,			       read_actor_t actor);/* *	relay_file_read_subbufs - read count bytes, bridging subbuf boundaries */static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos,					subbuf_actor_t subbuf_actor,					read_actor_t actor,					read_descriptor_t *desc){	struct rchan_buf *buf = filp->private_data;	size_t read_start, avail;	int ret;	if (!desc->count)		return 0;	mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);	do {		if (!relay_file_read_avail(buf, *ppos))			break;		read_start = relay_file_read_start_pos(*ppos, buf);		avail = relay_file_read_subbuf_avail(read_start, buf);		if (!avail)			break;		avail = min(desc->count, avail);		ret = subbuf_actor(read_start, buf, avail, desc, actor);		if (desc->error < 0)			break;		if (ret) {			relay_file_read_consume(buf, read_start, ret);			*ppos = relay_file_read_end_pos(buf, read_start, ret);		}	} while (desc->count && ret);	mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);	return desc->written;}static ssize_t relay_file_read(struct file *filp,			       char __user *buffer,			       size_t count,			       loff_t *ppos){	read_descriptor_t desc;	desc.written = 0;	desc.count = count;	desc.arg.buf = buffer;	desc.error = 0;	return relay_file_read_subbufs(filp, ppos, subbuf_read_actor,				       NULL, &desc);}static ssize_t relay_file_sendfile(struct file *filp,				   loff_t *ppos,				   size_t count,				   read_actor_t actor,				   void *target){	read_descriptor_t desc;	desc.written = 0;	desc.count = count;	desc.arg.data = target;	desc.error = 0;	return relay_file_read_subbufs(filp, ppos, subbuf_send_actor,				       actor, &desc);}const struct file_operations relay_file_operations = {	.open		= relay_file_open,	.poll		= relay_file_poll,	.mmap		= relay_file_mmap,	.read		= relay_file_read,	.llseek		= no_llseek,	.release	= relay_file_release,	.sendfile       = relay_file_sendfile,};EXPORT_SYMBOL_GPL(relay_file_operations);static __init int relay_init(void){	hotcpu_notifier(relay_hotcpu_callback, 0);	return 0;}module_init(relay_init);

⌨️ 快捷键说明

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