1049 lines · 26.7 KB · C
RawDownload
/* *	fs/libfs.c *	Library for filesystems writers. */ #include <linux/export.h>#include <linux/pagemap.h>#include <linux/slab.h>#include <linux/mount.h>#include <linux/vfs.h>#include <linux/quotaops.h>#include <linux/mutex.h>#include <linux/namei.h>#include <linux/exportfs.h>#include <linux/writeback.h>#include <linux/buffer_head.h> /* sync_mapping_buffers */ #include <asm/uaccess.h> #include "internal.h" static inline int simple_positive(struct dentry *dentry){	return dentry->d_inode && !d_unhashed(dentry);} int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,		   struct kstat *stat){	struct inode *inode = dentry->d_inode;	generic_fillattr(inode, stat);	stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);	return 0;}EXPORT_SYMBOL(simple_getattr); int simple_statfs(struct dentry *dentry, struct kstatfs *buf){	buf->f_type = dentry->d_sb->s_magic;	buf->f_bsize = PAGE_CACHE_SIZE;	buf->f_namelen = NAME_MAX;	return 0;}EXPORT_SYMBOL(simple_statfs); /* * Retaining negative dentries for an in-memory filesystem just wastes * memory and lookup time: arrange for them to be deleted immediately. */int always_delete_dentry(const struct dentry *dentry){	return 1;}EXPORT_SYMBOL(always_delete_dentry); const struct dentry_operations simple_dentry_operations = {	.d_delete = always_delete_dentry,};EXPORT_SYMBOL(simple_dentry_operations); /* * Lookup the data. This is trivial - if the dentry didn't already * exist, we know it is negative.  Set d_op to delete negative dentries. */struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags){	if (dentry->d_name.len > NAME_MAX)		return ERR_PTR(-ENAMETOOLONG);	if (!dentry->d_sb->s_d_op)		d_set_d_op(dentry, &simple_dentry_operations);	d_add(dentry, NULL);	return NULL;}EXPORT_SYMBOL(simple_lookup); int dcache_dir_open(struct inode *inode, struct file *file){	static struct qstr cursor_name = QSTR_INIT(".", 1); 	file->private_data = d_alloc(file->f_path.dentry, &cursor_name); 	return file->private_data ? 0 : -ENOMEM;}EXPORT_SYMBOL(dcache_dir_open); int dcache_dir_close(struct inode *inode, struct file *file){	dput(file->private_data);	return 0;}EXPORT_SYMBOL(dcache_dir_close); loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence){	struct dentry *dentry = file->f_path.dentry;	mutex_lock(&dentry->d_inode->i_mutex);	switch (whence) {		case 1:			offset += file->f_pos;		case 0:			if (offset >= 0)				break;		default:			mutex_unlock(&dentry->d_inode->i_mutex);			return -EINVAL;	}	if (offset != file->f_pos) {		file->f_pos = offset;		if (file->f_pos >= 2) {			struct list_head *p;			struct dentry *cursor = file->private_data;			loff_t n = file->f_pos - 2; 			spin_lock(&dentry->d_lock);			/* d_lock not required for cursor */			list_del(&cursor->d_u.d_child);			p = dentry->d_subdirs.next;			while (n && p != &dentry->d_subdirs) {				struct dentry *next;				next = list_entry(p, struct dentry, d_u.d_child);				spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);				if (simple_positive(next))					n--;				spin_unlock(&next->d_lock);				p = p->next;			}			list_add_tail(&cursor->d_u.d_child, p);			spin_unlock(&dentry->d_lock);		}	}	mutex_unlock(&dentry->d_inode->i_mutex);	return offset;}EXPORT_SYMBOL(dcache_dir_lseek); /* Relationship between i_mode and the DT_xxx types */static inline unsigned char dt_type(struct inode *inode){	return (inode->i_mode >> 12) & 15;} /* * Directory is locked and all positive dentries in it are safe, since * for ramfs-type trees they can't go away without unlink() or rmdir(), * both impossible due to the lock on directory. */ int dcache_readdir(struct file *file, struct dir_context *ctx){	struct dentry *dentry = file->f_path.dentry;	struct dentry *cursor = file->private_data;	struct list_head *p, *q = &cursor->d_u.d_child; 	if (!dir_emit_dots(file, ctx))		return 0;	spin_lock(&dentry->d_lock);	if (ctx->pos == 2)		list_move(q, &dentry->d_subdirs); 	for (p = q->next; p != &dentry->d_subdirs; p = p->next) {		struct dentry *next = list_entry(p, struct dentry, d_u.d_child);		spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);		if (!simple_positive(next)) {			spin_unlock(&next->d_lock);			continue;		} 		spin_unlock(&next->d_lock);		spin_unlock(&dentry->d_lock);		if (!dir_emit(ctx, next->d_name.name, next->d_name.len,			      next->d_inode->i_ino, dt_type(next->d_inode)))			return 0;		spin_lock(&dentry->d_lock);		spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);		/* next is still alive */		list_move(q, p);		spin_unlock(&next->d_lock);		p = q;		ctx->pos++;	}	spin_unlock(&dentry->d_lock);	return 0;}EXPORT_SYMBOL(dcache_readdir); ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos){	return -EISDIR;}EXPORT_SYMBOL(generic_read_dir); const struct file_operations simple_dir_operations = {	.open		= dcache_dir_open,	.release	= dcache_dir_close,	.llseek		= dcache_dir_lseek,	.read		= generic_read_dir,	.iterate	= dcache_readdir,	.fsync		= noop_fsync,};EXPORT_SYMBOL(simple_dir_operations); const struct inode_operations simple_dir_inode_operations = {	.lookup		= simple_lookup,};EXPORT_SYMBOL(simple_dir_inode_operations); static const struct super_operations simple_super_operations = {	.statfs		= simple_statfs,}; /* * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that * will never be mountable) */struct dentry *mount_pseudo(struct file_system_type *fs_type, char *name,	const struct super_operations *ops,	const struct dentry_operations *dops, unsigned long magic){	struct super_block *s;	struct dentry *dentry;	struct inode *root;	struct qstr d_name = QSTR_INIT(name, strlen(name)); 	s = sget(fs_type, NULL, set_anon_super, MS_NOUSER, NULL);	if (IS_ERR(s))		return ERR_CAST(s); 	s->s_maxbytes = MAX_LFS_FILESIZE;	s->s_blocksize = PAGE_SIZE;	s->s_blocksize_bits = PAGE_SHIFT;	s->s_magic = magic;	s->s_op = ops ? ops : &simple_super_operations;	s->s_time_gran = 1;	root = new_inode(s);	if (!root)		goto Enomem;	/*	 * since this is the first inode, make it number 1. New inodes created	 * after this must take care not to collide with it (by passing	 * max_reserved of 1 to iunique).	 */	root->i_ino = 1;	root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;	root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;	dentry = __d_alloc(s, &d_name);	if (!dentry) {		iput(root);		goto Enomem;	}	d_instantiate(dentry, root);	s->s_root = dentry;	s->s_d_op = dops;	s->s_flags |= MS_ACTIVE;	return dget(s->s_root); Enomem:	deactivate_locked_super(s);	return ERR_PTR(-ENOMEM);}EXPORT_SYMBOL(mount_pseudo); int simple_open(struct inode *inode, struct file *file){	if (inode->i_private)		file->private_data = inode->i_private;	return 0;}EXPORT_SYMBOL(simple_open); int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry){	struct inode *inode = old_dentry->d_inode; 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;	inc_nlink(inode);	ihold(inode);	dget(dentry);	d_instantiate(dentry, inode);	return 0;}EXPORT_SYMBOL(simple_link); int simple_empty(struct dentry *dentry){	struct dentry *child;	int ret = 0; 	spin_lock(&dentry->d_lock);	list_for_each_entry(child, &dentry->d_subdirs, d_u.d_child) {		spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);		if (simple_positive(child)) {			spin_unlock(&child->d_lock);			goto out;		}		spin_unlock(&child->d_lock);	}	ret = 1;out:	spin_unlock(&dentry->d_lock);	return ret;}EXPORT_SYMBOL(simple_empty); int simple_unlink(struct inode *dir, struct dentry *dentry){	struct inode *inode = dentry->d_inode; 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;	drop_nlink(inode);	dput(dentry);	return 0;}EXPORT_SYMBOL(simple_unlink); int simple_rmdir(struct inode *dir, struct dentry *dentry){	if (!simple_empty(dentry))		return -ENOTEMPTY; 	drop_nlink(dentry->d_inode);	simple_unlink(dir, dentry);	drop_nlink(dir);	return 0;}EXPORT_SYMBOL(simple_rmdir); int simple_rename(struct inode *old_dir, struct dentry *old_dentry,		struct inode *new_dir, struct dentry *new_dentry){	struct inode *inode = old_dentry->d_inode;	int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode); 	if (!simple_empty(new_dentry))		return -ENOTEMPTY; 	if (new_dentry->d_inode) {		simple_unlink(new_dir, new_dentry);		if (they_are_dirs) {			drop_nlink(new_dentry->d_inode);			drop_nlink(old_dir);		}	} else if (they_are_dirs) {		drop_nlink(old_dir);		inc_nlink(new_dir);	} 	old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =		new_dir->i_mtime = inode->i_ctime = CURRENT_TIME; 	return 0;}EXPORT_SYMBOL(simple_rename); /** * simple_setattr - setattr for simple filesystem * @dentry: dentry * @iattr: iattr structure * * Returns 0 on success, -error on failure. * * simple_setattr is a simple ->setattr implementation without a proper * implementation of size changes. * * It can either be used for in-memory filesystems or special files * on simple regular filesystems.  Anything that needs to change on-disk * or wire state on size changes needs its own setattr method. */int simple_setattr(struct dentry *dentry, struct iattr *iattr){	struct inode *inode = dentry->d_inode;	int error; 	error = inode_change_ok(inode, iattr);	if (error)		return error; 	if (iattr->ia_valid & ATTR_SIZE)		truncate_setsize(inode, iattr->ia_size);	setattr_copy(inode, iattr);	mark_inode_dirty(inode);	return 0;}EXPORT_SYMBOL(simple_setattr); int simple_readpage(struct file *file, struct page *page){	clear_highpage(page);	flush_dcache_page(page);	SetPageUptodate(page);	unlock_page(page);	return 0;}EXPORT_SYMBOL(simple_readpage); int simple_write_begin(struct file *file, struct address_space *mapping,			loff_t pos, unsigned len, unsigned flags,			struct page **pagep, void **fsdata){	struct page *page;	pgoff_t index; 	index = pos >> PAGE_CACHE_SHIFT; 	page = grab_cache_page_write_begin(mapping, index, flags);	if (!page)		return -ENOMEM; 	*pagep = page; 	if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {		unsigned from = pos & (PAGE_CACHE_SIZE - 1); 		zero_user_segments(page, 0, from, from + len, PAGE_CACHE_SIZE);	}	return 0;}EXPORT_SYMBOL(simple_write_begin); /** * simple_write_end - .write_end helper for non-block-device FSes * @available: See .write_end of address_space_operations * @file: 		" * @mapping: 		" * @pos: 		" * @len: 		" * @copied: 		" * @page: 		" * @fsdata: 		" * * simple_write_end does the minimum needed for updating a page after writing is * done. It has the same API signature as the .write_end of * address_space_operations vector. So it can just be set onto .write_end for * FSes that don't need any other processing. i_mutex is assumed to be held. * Block based filesystems should use generic_write_end(). * NOTE: Even though i_size might get updated by this function, mark_inode_dirty * is not called, so a filesystem that actually does store data in .write_inode * should extend on what's done here with a call to mark_inode_dirty() in the * case that i_size has changed. */int simple_write_end(struct file *file, struct address_space *mapping,			loff_t pos, unsigned len, unsigned copied,			struct page *page, void *fsdata){	struct inode *inode = page->mapping->host;	loff_t last_pos = pos + copied; 	/* zero the stale part of the page if we did a short copy */	if (copied < len) {		unsigned from = pos & (PAGE_CACHE_SIZE - 1); 		zero_user(page, from + copied, len - copied);	} 	if (!PageUptodate(page))		SetPageUptodate(page);	/*	 * No need to use i_size_read() here, the i_size	 * cannot change under us because we hold the i_mutex.	 */	if (last_pos > inode->i_size)		i_size_write(inode, last_pos); 	set_page_dirty(page);	unlock_page(page);	page_cache_release(page); 	return copied;}EXPORT_SYMBOL(simple_write_end); /* * the inodes created here are not hashed. If you use iunique to generate * unique inode values later for this filesystem, then you must take care * to pass it an appropriate max_reserved value to avoid collisions. */int simple_fill_super(struct super_block *s, unsigned long magic,		      struct tree_descr *files){	struct inode *inode;	struct dentry *root;	struct dentry *dentry;	int i; 	s->s_blocksize = PAGE_CACHE_SIZE;	s->s_blocksize_bits = PAGE_CACHE_SHIFT;	s->s_magic = magic;	s->s_op = &simple_super_operations;	s->s_time_gran = 1; 	inode = new_inode(s);	if (!inode)		return -ENOMEM;	/*	 * because the root inode is 1, the files array must not contain an	 * entry at index 1	 */	inode->i_ino = 1;	inode->i_mode = S_IFDIR | 0755;	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;	inode->i_op = &simple_dir_inode_operations;

Lines 1–500 of 1049.