1217 lines · 30.6 KB · C
RawDownload
/* *	fs/libfs.c *	Library for filesystems writers. */ #include <linux/blkdev.h>#include <linux/export.h>#include <linux/pagemap.h>#include <linux/slab.h>#include <linux/cred.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 <linux/uaccess.h> #include "internal.h" int simple_getattr(const struct path *path, struct kstat *stat,		   u32 request_mask, unsigned int query_flags){	struct inode *inode = d_inode(path->dentry);	generic_fillattr(inode, stat);	stat->blocks = inode->i_mapping->nrpages << (PAGE_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_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){	file->private_data = d_alloc_cursor(file->f_path.dentry); 	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); /* parent is locked at least shared */static struct dentry *next_positive(struct dentry *parent,				    struct list_head *from,				    int count){	unsigned *seq = &parent->d_inode->i_dir_seq, n;	struct dentry *res;	struct list_head *p;	bool skipped;	int i; retry:	i = count;	skipped = false;	n = smp_load_acquire(seq) & ~1;	res = NULL;	rcu_read_lock();	for (p = from->next; p != &parent->d_subdirs; p = p->next) {		struct dentry *d = list_entry(p, struct dentry, d_child);		if (!simple_positive(d)) {			skipped = true;		} else if (!--i) {			res = d;			break;		}	}	rcu_read_unlock();	if (skipped) {		smp_rmb();		if (unlikely(*seq != n))			goto retry;	}	return res;} static void move_cursor(struct dentry *cursor, struct list_head *after){	struct dentry *parent = cursor->d_parent;	unsigned n, *seq = &parent->d_inode->i_dir_seq;	spin_lock(&parent->d_lock);	for (;;) {		n = *seq;		if (!(n & 1) && cmpxchg(seq, n, n + 1) == n)			break;		cpu_relax();	}	__list_del(cursor->d_child.prev, cursor->d_child.next);	if (after)		list_add(&cursor->d_child, after);	else		list_add_tail(&cursor->d_child, &parent->d_subdirs);	smp_store_release(seq, n + 2);	spin_unlock(&parent->d_lock);} loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence){	struct dentry *dentry = file->f_path.dentry;	switch (whence) {		case 1:			offset += file->f_pos;		case 0:			if (offset >= 0)				break;		default:			return -EINVAL;	}	if (offset != file->f_pos) {		file->f_pos = offset;		if (file->f_pos >= 2) {			struct dentry *cursor = file->private_data;			struct dentry *to;			loff_t n = file->f_pos - 2; 			inode_lock_shared(dentry->d_inode);			to = next_positive(dentry, &dentry->d_subdirs, n);			move_cursor(cursor, to ? &to->d_child : NULL);			inode_unlock_shared(dentry->d_inode);		}	}	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 = &cursor->d_child;	struct dentry *next;	bool moved = false; 	if (!dir_emit_dots(file, ctx))		return 0; 	if (ctx->pos == 2)		p = &dentry->d_subdirs;	while ((next = next_positive(dentry, p, 1)) != NULL) {		if (!dir_emit(ctx, next->d_name.name, next->d_name.len,			      d_inode(next)->i_ino, dt_type(d_inode(next))))			break;		moved = true;		p = &next->d_child;		ctx->pos++;	}	if (moved)		move_cursor(cursor, p);	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_shared	= 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_xattr(struct file_system_type *fs_type, char *name,	const struct super_operations *ops, const struct xattr_handler **xattr,	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_userns(fs_type, NULL, set_anon_super, MS_KERNMOUNT|MS_NOUSER,			&init_user_ns, 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_xattr = xattr;	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(root);	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_xattr); 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 = d_inode(old_dentry); 	inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);	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_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 = d_inode(dentry); 	inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);	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(d_inode(dentry));	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,		  unsigned int flags){	struct inode *inode = d_inode(old_dentry);	int they_are_dirs = d_is_dir(old_dentry); 	if (flags & ~RENAME_NOREPLACE)		return -EINVAL; 	if (!simple_empty(new_dentry))		return -ENOTEMPTY; 	if (d_really_is_positive(new_dentry)) {		simple_unlink(new_dir, new_dentry);		if (they_are_dirs) {			drop_nlink(d_inode(new_dentry));			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(old_dir); 	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 = d_inode(dentry);	int error; 	error = setattr_prepare(dentry, 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_SHIFT; 	page = grab_cache_page_write_begin(mapping, index, flags);	if (!page)		return -ENOMEM; 	*pagep = page; 	if (!PageUptodate(page) && (len != PAGE_SIZE)) {		unsigned from = pos & (PAGE_SIZE - 1); 		zero_user_segments(page, 0, from, from + len, PAGE_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. * * Use *ONLY* with simple_readpage() */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 (!PageUptodate(page)) {		if (copied < len) {			unsigned from = pos & (PAGE_SIZE - 1); 			zero_user(page, from + copied, len - copied);		}		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);	put_page(page); 	return copied;

Lines 1–500 of 1217.