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

📄 relay.c

📁 linux 2.6.19 kernel source code before patching
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Public API and common code for kernel->userspace relay file support. * * See Documentation/filesystems/relayfs.txt for an overview of relayfs. * * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com) * * Moved to kernel/relay.c by Paul Mundt, 2006. * November 2006 - CPU hotplug support by Mathieu Desnoyers * 	(mathieu.desnoyers@polymtl.ca) * * This file is released under the GPL. */#include <linux/errno.h>#include <linux/stddef.h>#include <linux/slab.h>#include <linux/module.h>#include <linux/string.h>#include <linux/relay.h>#include <linux/vmalloc.h>#include <linux/mm.h>#include <linux/cpu.h>/* list of open channels, for cpu hotplug */static DEFINE_MUTEX(relay_channels_mutex);static LIST_HEAD(relay_channels);/* * close() vm_op implementation for relay file mapping. */static void relay_file_mmap_close(struct vm_area_struct *vma){	struct rchan_buf *buf = vma->vm_private_data;	buf->chan->cb->buf_unmapped(buf, vma->vm_file);}/* * nopage() vm_op implementation for relay file mapping. */static struct page *relay_buf_nopage(struct vm_area_struct *vma,				     unsigned long address,				     int *type){	struct page *page;	struct rchan_buf *buf = vma->vm_private_data;	unsigned long offset = address - vma->vm_start;	if (address > vma->vm_end)		return NOPAGE_SIGBUS; /* Disallow mremap */	if (!buf)		return NOPAGE_OOM;	page = vmalloc_to_page(buf->start + offset);	if (!page)		return NOPAGE_OOM;	get_page(page);	if (type)		*type = VM_FAULT_MINOR;	return page;}/* * vm_ops for relay file mappings. */static struct vm_operations_struct relay_file_mmap_ops = {	.nopage = relay_buf_nopage,	.close = relay_file_mmap_close,};/** *	relay_mmap_buf: - mmap channel buffer to process address space *	@buf: relay channel buffer *	@vma: vm_area_struct describing memory to be mapped * *	Returns 0 if ok, negative on error * *	Caller should already have grabbed mmap_sem. */int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma){	unsigned long length = vma->vm_end - vma->vm_start;	struct file *filp = vma->vm_file;	if (!buf)		return -EBADF;	if (length != (unsigned long)buf->chan->alloc_size)		return -EINVAL;	vma->vm_ops = &relay_file_mmap_ops;	vma->vm_private_data = buf;	buf->chan->cb->buf_mapped(buf, filp);	return 0;}/** *	relay_alloc_buf - allocate a channel buffer *	@buf: the buffer struct *	@size: total size of the buffer * *	Returns a pointer to the resulting buffer, %NULL if unsuccessful. The *	passed in size will get page aligned, if it isn't already. */static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size){	void *mem;	unsigned int i, j, n_pages;	*size = PAGE_ALIGN(*size);	n_pages = *size >> PAGE_SHIFT;	buf->page_array = kcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);	if (!buf->page_array)		return NULL;	for (i = 0; i < n_pages; i++) {		buf->page_array[i] = alloc_page(GFP_KERNEL);		if (unlikely(!buf->page_array[i]))			goto depopulate;	}	mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);	if (!mem)		goto depopulate;	memset(mem, 0, *size);	buf->page_count = n_pages;	return mem;depopulate:	for (j = 0; j < i; j++)		__free_page(buf->page_array[j]);	kfree(buf->page_array);	return NULL;}/** *	relay_create_buf - allocate and initialize a channel buffer *	@chan: the relay channel * *	Returns channel buffer if successful, %NULL otherwise. */struct rchan_buf *relay_create_buf(struct rchan *chan){	struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);	if (!buf)		return NULL;	buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL);	if (!buf->padding)		goto free_buf;	buf->start = relay_alloc_buf(buf, &chan->alloc_size);	if (!buf->start)		goto free_buf;	buf->chan = chan;	kref_get(&buf->chan->kref);	return buf;free_buf:	kfree(buf->padding);	kfree(buf);	return NULL;}/** *	relay_destroy_channel - free the channel struct *	@kref: target kernel reference that contains the relay channel * *	Should only be called from kref_put(). */void relay_destroy_channel(struct kref *kref){	struct rchan *chan = container_of(kref, struct rchan, kref);	kfree(chan);}/** *	relay_destroy_buf - destroy an rchan_buf struct and associated buffer *	@buf: the buffer struct */void relay_destroy_buf(struct rchan_buf *buf){	struct rchan *chan = buf->chan;	unsigned int i;	if (likely(buf->start)) {		vunmap(buf->start);		for (i = 0; i < buf->page_count; i++)			__free_page(buf->page_array[i]);		kfree(buf->page_array);	}	chan->buf[buf->cpu] = NULL;	kfree(buf->padding);	kfree(buf);	kref_put(&chan->kref, relay_destroy_channel);}/** *	relay_remove_buf - remove a channel buffer *	@kref: target kernel reference that contains the relay buffer * *	Removes the file from the fileystem, which also frees the *	rchan_buf_struct and the channel buffer.  Should only be called from *	kref_put(). */void relay_remove_buf(struct kref *kref){	struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);	buf->chan->cb->remove_buf_file(buf->dentry);	relay_destroy_buf(buf);}/** *	relay_buf_empty - boolean, is the channel buffer empty? *	@buf: channel buffer * *	Returns 1 if the buffer is empty, 0 otherwise. */int relay_buf_empty(struct rchan_buf *buf){	return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;}EXPORT_SYMBOL_GPL(relay_buf_empty);/** *	relay_buf_full - boolean, is the channel buffer full? *	@buf: channel buffer * *	Returns 1 if the buffer is full, 0 otherwise. */int relay_buf_full(struct rchan_buf *buf){	size_t ready = buf->subbufs_produced - buf->subbufs_consumed;	return (ready >= buf->chan->n_subbufs) ? 1 : 0;}EXPORT_SYMBOL_GPL(relay_buf_full);/* * High-level relay kernel API and associated functions. *//* * rchan_callback implementations defining default channel behavior.  Used * in place of corresponding NULL values in client callback struct. *//* * subbuf_start() default callback.  Does nothing. */static int subbuf_start_default_callback (struct rchan_buf *buf,					  void *subbuf,					  void *prev_subbuf,					  size_t prev_padding){	if (relay_buf_full(buf))		return 0;	return 1;}/* * buf_mapped() default callback.  Does nothing. */static void buf_mapped_default_callback(struct rchan_buf *buf,					struct file *filp){}/* * buf_unmapped() default callback.  Does nothing. */static void buf_unmapped_default_callback(struct rchan_buf *buf,					  struct file *filp){}/* * create_buf_file_create() default callback.  Does nothing. */static struct dentry *create_buf_file_default_callback(const char *filename,						       struct dentry *parent,						       int mode,						       struct rchan_buf *buf,						       int *is_global){	return NULL;}/* * remove_buf_file() default callback.  Does nothing. */static int remove_buf_file_default_callback(struct dentry *dentry){	return -EINVAL;}/* relay channel default callbacks */static struct rchan_callbacks default_channel_callbacks = {	.subbuf_start = subbuf_start_default_callback,	.buf_mapped = buf_mapped_default_callback,	.buf_unmapped = buf_unmapped_default_callback,	.create_buf_file = create_buf_file_default_callback,	.remove_buf_file = remove_buf_file_default_callback,};/** *	wakeup_readers - wake up readers waiting on a channel *	@data: contains the channel buffer * *	This is the timer function used to defer reader waking. */static void wakeup_readers(unsigned long data){	struct rchan_buf *buf = (struct rchan_buf *)data;	wake_up_interruptible(&buf->read_wait);}/** *	__relay_reset - reset a channel buffer *	@buf: the channel buffer *	@init: 1 if this is a first-time initialization * *	See relay_reset() for description of effect. */static void __relay_reset(struct rchan_buf *buf, unsigned int init){	size_t i;	if (init) {		init_waitqueue_head(&buf->read_wait);		kref_init(&buf->kref);		setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);	} else		del_timer_sync(&buf->timer);	buf->subbufs_produced = 0;	buf->subbufs_consumed = 0;	buf->bytes_consumed = 0;	buf->finalized = 0;	buf->data = buf->start;	buf->offset = 0;	for (i = 0; i < buf->chan->n_subbufs; i++)		buf->padding[i] = 0;	buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);}/** *	relay_reset - reset the channel *	@chan: the channel * *	This has the effect of erasing all data from all channel buffers *	and restarting the channel in its initial state.  The buffers *	are not freed, so any mappings are still in effect. * *	NOTE. Care should be taken that the channel isn't actually *	being used by anything when this call is made. */void relay_reset(struct rchan *chan){	unsigned int i;	if (!chan)		return; 	if (chan->is_global && chan->buf[0]) {		__relay_reset(chan->buf[0], 0);		return;	}	mutex_lock(&relay_channels_mutex);	for_each_online_cpu(i)		if (chan->buf[i])			__relay_reset(chan->buf[i], 0);	mutex_unlock(&relay_channels_mutex);}EXPORT_SYMBOL_GPL(relay_reset);/* *	relay_open_buf - create a new relay channel buffer * *	used by relay_open() and CPU hotplug. */static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu){ 	struct rchan_buf *buf = NULL;	struct dentry *dentry; 	char *tmpname; 	if (chan->is_global)		return chan->buf[0];	tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL); 	if (!tmpname) 		goto end; 	snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);	buf = relay_create_buf(chan);	if (!buf) 		goto free_name; 	buf->cpu = cpu; 	__relay_reset(buf, 1);	/* Create file in fs */ 	dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR, 					   buf, &chan->is_global); 	if (!dentry) 		goto free_buf;	buf->dentry = dentry; 	if(chan->is_global) { 		chan->buf[0] = buf; 		buf->cpu = 0;  	} 	goto free_name;free_buf: 	relay_destroy_buf(buf);free_name: 	kfree(tmpname);end:	return buf;}/** *	relay_close_buf - close a channel buffer *	@buf: channel buffer * *	Marks the buffer finalized and restores the default callbacks. *	The channel buffer and channel buffer data structure are then freed *	automatically when the last reference is given up. */static void relay_close_buf(struct rchan_buf *buf){	buf->finalized = 1;	del_timer_sync(&buf->timer);	kref_put(&buf->kref, relay_remove_buf);}static void setup_callbacks(struct rchan *chan,				   struct rchan_callbacks *cb){	if (!cb) {		chan->cb = &default_channel_callbacks;		return;	}	if (!cb->subbuf_start)		cb->subbuf_start = subbuf_start_default_callback;	if (!cb->buf_mapped)		cb->buf_mapped = buf_mapped_default_callback;	if (!cb->buf_unmapped)		cb->buf_unmapped = buf_unmapped_default_callback;	if (!cb->create_buf_file)		cb->create_buf_file = create_buf_file_default_callback;	if (!cb->remove_buf_file)		cb->remove_buf_file = remove_buf_file_default_callback;	chan->cb = cb;}/** * 	relay_hotcpu_callback - CPU hotplug callback * 	@nb: notifier block * 	@action: hotplug action to take * 	@hcpu: CPU number * * 	Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD) */static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,				unsigned long action,				void *hcpu){	unsigned int hotcpu = (unsigned long)hcpu;	struct rchan *chan;	switch(action) {	case CPU_UP_PREPARE:	case CPU_UP_PREPARE_FROZEN:		mutex_lock(&relay_channels_mutex);		list_for_each_entry(chan, &relay_channels, list) {			if (chan->buf[hotcpu])				continue;			chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);			if(!chan->buf[hotcpu]) {				printk(KERN_ERR					"relay_hotcpu_callback: cpu %d buffer "					"creation failed\n", hotcpu);				mutex_unlock(&relay_channels_mutex);				return NOTIFY_BAD;			}		}		mutex_unlock(&relay_channels_mutex);		break;	case CPU_DEAD:	case CPU_DEAD_FROZEN:		/* No need to flush the cpu : will be flushed upon		 * final relay_flush() call. */		break;	}	return NOTIFY_OK;}/** *	relay_open - create a new relay channel *	@base_filename: base name of files to create *	@parent: dentry of parent directory, %NULL for root directory *	@subbuf_size: size of sub-buffers *	@n_subbufs: number of sub-buffers *	@cb: client callback functions *	@private_data: user-defined data * *	Returns channel pointer if successful, %NULL otherwise. * *	Creates a channel buffer for each cpu using the sizes and *	attributes specified.  The created channel buffer files *	will be named base_filename0...base_filenameN-1.  File *	permissions will be %S_IRUSR. */struct rchan *relay_open(const char *base_filename,			 struct dentry *parent,			 size_t subbuf_size,			 size_t n_subbufs,			 struct rchan_callbacks *cb,			 void *private_data){	unsigned int i;	struct rchan *chan;	if (!base_filename)		return NULL;	if (!(subbuf_size && n_subbufs))		return NULL;	chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);	if (!chan)		return NULL;	chan->version = RELAYFS_CHANNEL_VERSION;	chan->n_subbufs = n_subbufs;	chan->subbuf_size = subbuf_size;	chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);	chan->parent = parent;	chan->private_data = private_data;

⌨️ 快捷键说明

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