2240 lines · 59.8 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* dir.c: AFS filesystem directory handling * * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com) */ #include <linux/kernel.h>#include <linux/fs.h>#include <linux/namei.h>#include <linux/pagemap.h>#include <linux/swap.h>#include <linux/ctype.h>#include <linux/sched.h>#include <linux/iversion.h>#include <linux/iov_iter.h>#include <linux/task_io_accounting_ops.h>#include "internal.h"#include "afs_fs.h"#include "xdr_fs.h" static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,				 unsigned int flags);static int afs_dir_open(struct inode *inode, struct file *file);static int afs_readdir(struct file *file, struct dir_context *ctx);static int afs_d_revalidate(struct inode *dir, const struct qstr *name,			    struct dentry *dentry, unsigned int flags);static int afs_d_delete(const struct dentry *dentry);static void afs_d_iput(struct dentry *dentry, struct inode *inode);static bool afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,				   u64 ino, u32 uniquifier);#define AFS_LOOKUP_ONE ((filldir_t)0x123UL)static bool afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,			       u64 ino, u32 uniquifier);#define AFS_LOOKUP ((filldir_t)0x137UL)static int afs_create(struct mnt_idmap *idmap, struct inode *dir,		      struct dentry *dentry, umode_t mode);static struct dentry *afs_mkdir(struct mnt_idmap *idmap, struct inode *dir,				struct dentry *dentry, umode_t mode);static int afs_rmdir(struct inode *dir, struct dentry *dentry);static int afs_unlink(struct inode *dir, struct dentry *dentry);static int afs_link(struct dentry *from, struct inode *dir,		    struct dentry *dentry);static int afs_symlink(struct mnt_idmap *idmap, struct inode *dir,		       struct dentry *dentry, const char *content);static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir,		      struct dentry *old_dentry, struct inode *new_dir,		      struct dentry *new_dentry, unsigned int flags);static int afs_dir_writepages(struct address_space *mapping,			      struct writeback_control *wbc); const struct file_operations afs_dir_file_operations = {	.open		= afs_dir_open,	.release	= afs_release,	.iterate_shared	= afs_readdir,	.lock		= afs_lock,	.llseek		= generic_file_llseek,}; const struct inode_operations afs_dir_inode_operations = {	.create		= afs_create,	.lookup		= afs_lookup,	.link		= afs_link,	.unlink		= afs_unlink,	.symlink	= afs_symlink,	.mkdir		= afs_mkdir,	.rmdir		= afs_rmdir,	.rename		= afs_rename,	.permission	= afs_permission,	.getattr	= afs_getattr,	.setattr	= afs_setattr,}; const struct address_space_operations afs_dir_aops = {	.writepages	= afs_dir_writepages,}; const struct dentry_operations afs_fs_dentry_operations = {	.d_revalidate	= afs_d_revalidate,	.d_delete	= afs_d_delete,	.d_release	= afs_d_release,	.d_automount	= afs_d_automount,	.d_iput		= afs_d_iput,}; struct afs_lookup_one_cookie {	struct dir_context	ctx;	struct qstr		name;	bool			found;	struct afs_fid		fid;}; struct afs_lookup_cookie {	struct dir_context	ctx;	struct qstr		name;	unsigned short		nr_fids;	struct afs_fid		fids[50];}; static void afs_dir_unuse_cookie(struct afs_vnode *dvnode, int ret){	if (ret == 0) {		struct afs_vnode_cache_aux aux;		loff_t i_size = i_size_read(&dvnode->netfs.inode); 		afs_set_cache_aux(dvnode, &aux);		fscache_unuse_cookie(afs_vnode_cache(dvnode), &aux, &i_size);	} else {		fscache_unuse_cookie(afs_vnode_cache(dvnode), NULL, NULL);	}} /* * Iterate through a kmapped directory segment, dumping a summary of * the contents. */static size_t afs_dir_dump_step(void *iter_base, size_t progress, size_t len,				void *priv, void *priv2){	do {		union afs_xdr_dir_block *block = iter_base; 		pr_warn("[%05zx] %32phN\n", progress, block);		iter_base += AFS_DIR_BLOCK_SIZE;		progress += AFS_DIR_BLOCK_SIZE;		len -= AFS_DIR_BLOCK_SIZE;	} while (len > 0); 	return len;} /* * Dump the contents of a directory. */static void afs_dir_dump(struct afs_vnode *dvnode){	struct iov_iter iter;	unsigned long long i_size = i_size_read(&dvnode->netfs.inode); 	pr_warn("DIR %llx:%llx is=%llx\n",		dvnode->fid.vid, dvnode->fid.vnode, i_size); 	iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0, i_size);	iterate_folioq(&iter, iov_iter_count(&iter), NULL, NULL,		       afs_dir_dump_step);} /* * check that a directory folio is valid */static bool afs_dir_check_block(struct afs_vnode *dvnode, size_t progress,				union afs_xdr_dir_block *block){	if (block->hdr.magic != AFS_DIR_MAGIC) {		pr_warn("%s(%llx): [%zx] bad magic %04x\n",		       __func__, dvnode->netfs.inode.i_ino,		       progress, ntohs(block->hdr.magic));		trace_afs_dir_check_failed(dvnode, progress);		trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);		return false;	} 	/* Make sure each block is NUL terminated so we can reasonably	 * use string functions on it.  The filenames in the folio	 * *should* be NUL-terminated anyway.	 */	((u8 *)block)[AFS_DIR_BLOCK_SIZE - 1] = 0;	afs_stat_v(dvnode, n_read_dir);	return true;} /* * Iterate through a kmapped directory segment, checking the content. */static size_t afs_dir_check_step(void *iter_base, size_t progress, size_t len,				 void *priv, void *priv2){	struct afs_vnode *dvnode = priv; 	if (WARN_ON_ONCE(progress % AFS_DIR_BLOCK_SIZE ||			 len % AFS_DIR_BLOCK_SIZE))		return len; 	do {		if (!afs_dir_check_block(dvnode, progress, iter_base))			break;		iter_base += AFS_DIR_BLOCK_SIZE;		len -= AFS_DIR_BLOCK_SIZE;	} while (len > 0); 	return len;} /* * Check all the blocks in a directory. */static int afs_dir_check(struct afs_vnode *dvnode){	struct iov_iter iter;	unsigned long long i_size = i_size_read(&dvnode->netfs.inode);	size_t checked = 0; 	if (unlikely(!i_size))		return 0; 	iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0, i_size);	checked = iterate_folioq(&iter, iov_iter_count(&iter), dvnode, NULL,				 afs_dir_check_step);	if (checked != i_size) {		afs_dir_dump(dvnode);		return -EIO;	}	return 0;} /* * open an AFS directory file */static int afs_dir_open(struct inode *inode, struct file *file){	_enter("{%llu}", inode->i_ino); 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32); 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))		return -ENOENT; 	return afs_open(inode, file);} /* * Read a file in a single download. */static ssize_t afs_do_read_single(struct afs_vnode *dvnode, struct file *file){	struct iov_iter iter;	ssize_t ret;	loff_t i_size; 	i_size = i_size_read(&dvnode->netfs.inode);	if (i_size < AFS_DIR_BLOCK_SIZE)		return afs_bad(dvnode, afs_file_error_dir_small);	if (i_size > AFS_DIR_BLOCK_SIZE * 1024) {		trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);		return -EFBIG;	} 	/* Expand the storage.  TODO: Shrink the storage too. */	if (dvnode->directory_size < i_size) {		size_t cur_size = dvnode->directory_size; 		ret = netfs_alloc_folioq_buffer(NULL,						&dvnode->directory, &cur_size, i_size,						mapping_gfp_mask(dvnode->netfs.inode.i_mapping));		dvnode->directory_size = cur_size;		if (ret < 0)			return ret;	} 	iov_iter_folio_queue(&iter, ITER_DEST, dvnode->directory, 0, 0, dvnode->directory_size); 	/* AFS requires us to perform the read of a directory synchronously as	 * a single unit to avoid issues with the directory contents being	 * changed between reads.	 */	ret = netfs_read_single(&dvnode->netfs.inode, file, &iter);	if (ret >= 0) {		i_size = i_size_read(&dvnode->netfs.inode);		if (i_size > ret) {			/* The content has grown, so we need to expand the			 * buffer.			 */			ret = -ESTALE;		} else {			int ret2 = afs_dir_check(dvnode); 			if (ret2 < 0)				ret = ret2;		}	} 	return ret;} static ssize_t afs_read_single(struct afs_vnode *dvnode, struct file *file){	ssize_t ret; 	fscache_use_cookie(afs_vnode_cache(dvnode), false);	ret = afs_do_read_single(dvnode, file);	fscache_unuse_cookie(afs_vnode_cache(dvnode), NULL, NULL);	return ret;} /* * Read the directory into a folio_queue buffer in one go, scrubbing the * previous contents.  We return -ESTALE if the caller needs to call us again. */ssize_t afs_read_dir(struct afs_vnode *dvnode, struct file *file)	__acquires(&dvnode->validate_lock){	ssize_t ret;	loff_t i_size; 	i_size = i_size_read(&dvnode->netfs.inode); 	ret = -ERESTARTSYS;	if (down_read_killable(&dvnode->validate_lock) < 0)		goto error; 	/* We only need to reread the data if it became invalid - or if we	 * haven't read it yet.	 */	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&	    test_bit(AFS_VNODE_DIR_READ, &dvnode->flags)) {		ret = i_size;		goto valid;	} 	up_read(&dvnode->validate_lock);	if (down_write_killable(&dvnode->validate_lock) < 0)		goto error; 	if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))		afs_invalidate_cache(dvnode, 0); 	if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) ||	    !test_bit(AFS_VNODE_DIR_READ, &dvnode->flags)) {		trace_afs_reload_dir(dvnode);		ret = afs_read_single(dvnode, file);		if (ret < 0)			goto error_unlock; 		// TODO: Trim excess pages 		set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);		set_bit(AFS_VNODE_DIR_READ, &dvnode->flags);	} else {		ret = i_size;	} 	downgrade_write(&dvnode->validate_lock);valid:	return ret; error_unlock:	up_write(&dvnode->validate_lock);error:	_leave(" = %zd", ret);	return ret;} /* * deal with one block in an AFS directory */static int afs_dir_iterate_block(struct afs_vnode *dvnode,				 struct dir_context *ctx,				 union afs_xdr_dir_block *block){	union afs_xdr_dirent *dire;	unsigned int blknum, base, hdr, pos, next, nr_slots;	size_t nlen;	int tmp; 	blknum	= ctx->pos / AFS_DIR_BLOCK_SIZE;	base	= blknum * AFS_DIR_SLOTS_PER_BLOCK;	hdr	= (blknum == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);	pos	= DIV_ROUND_UP(ctx->pos, AFS_DIR_DIRENT_SIZE) - base; 	_enter("%llx,%x", ctx->pos, blknum); 	/* walk through the block, an entry at a time */	for (unsigned int slot = hdr; slot < AFS_DIR_SLOTS_PER_BLOCK; slot = next) {		/* skip entries marked unused in the bitmap */		if (!(block->hdr.bitmap[slot / 8] &		      (1 << (slot % 8)))) {			_debug("ENT[%x]: Unused", base + slot);			next = slot + 1;			if (next >= pos)				ctx->pos = (base + next) * sizeof(union afs_xdr_dirent);			continue;		} 		/* got a valid entry */		dire = &block->dirents[slot];		nlen = strnlen(dire->u.name,			       (unsigned long)(block + 1) - (unsigned long)dire->u.name - 1);		if (nlen > AFSNAMEMAX - 1) {			_debug("ENT[%x]: Name too long (len %zx)",			       base + slot, nlen);			return afs_bad(dvnode, afs_file_error_dir_name_too_long);		} 		_debug("ENT[%x]: %s %zx \"%s\"",		       base + slot, (slot < pos ? "skip" : "fill"),		       nlen, dire->u.name); 		nr_slots = afs_dir_calc_slots(nlen);		next = slot + nr_slots;		if (next > AFS_DIR_SLOTS_PER_BLOCK) {			_debug("ENT[%x]: extends beyond end dir block (len %zx)",			       base + slot, nlen);			return afs_bad(dvnode, afs_file_error_dir_over_end);		} 		/* Check that the name-extension dirents are all allocated */		for (tmp = 1; tmp < nr_slots; tmp++) {			unsigned int xslot = slot + tmp; 			if (!(block->hdr.bitmap[xslot / 8] & (1 << (xslot % 8)))) {				_debug("ENT[%x]: Unmarked extension (%x/%x)",				       base + slot, tmp, nr_slots);				return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);			}		} 		/* skip if starts before the current position */		if (slot < pos) {			if (next > pos)				ctx->pos = (base + next) * sizeof(union afs_xdr_dirent);			continue;		} 		/* found the next entry */		if (ctx->actor == AFS_LOOKUP) {			if (!afs_lookup_filldir(ctx, dire->u.name, nlen,						ntohl(dire->u.vnode),						ntohl(dire->u.unique)))				return 0;		} else if (ctx->actor == AFS_LOOKUP_ONE) {			if (!afs_lookup_one_filldir(ctx, dire->u.name, nlen,						    ntohl(dire->u.vnode),						    ntohl(dire->u.unique)))				return 0;		} else if (!dir_emit(ctx, dire->u.name, nlen,				     ntohl(dire->u.vnode), DT_UNKNOWN)) {			_leave(" = 0 [full]");			return 0;		} 		ctx->pos = (base + next) * sizeof(union afs_xdr_dirent);	} 	_leave(" = 1 [more]");	return 1;} struct afs_dir_iteration_ctx {	struct dir_context	*dir_ctx;	int			error;}; /* * Iterate through a kmapped directory segment. */static size_t afs_dir_iterate_step(void *iter_base, size_t progress, size_t len,				   void *priv, void *priv2){	struct afs_dir_iteration_ctx *ctx = priv2;	struct afs_vnode *dvnode = priv;	int ret; 	if (WARN_ON_ONCE(progress % AFS_DIR_BLOCK_SIZE ||			 len % AFS_DIR_BLOCK_SIZE)) {		pr_err("Mis-iteration prog=%zx len=%zx\n",		       progress % AFS_DIR_BLOCK_SIZE,		       len % AFS_DIR_BLOCK_SIZE);		return len;	} 	do {		ret = afs_dir_iterate_block(dvnode, ctx->dir_ctx, iter_base);		if (ret != 1)			break; 		ctx->dir_ctx->pos = round_up(ctx->dir_ctx->pos, AFS_DIR_BLOCK_SIZE);		iter_base += AFS_DIR_BLOCK_SIZE;		len -= AFS_DIR_BLOCK_SIZE;	} while (len > 0); 	return len;} /* * Iterate through the directory folios. */static int afs_dir_iterate_contents(struct inode *dir, struct dir_context *dir_ctx){	struct afs_dir_iteration_ctx ctx = { .dir_ctx = dir_ctx };	struct afs_vnode *dvnode = AFS_FS_I(dir);	struct iov_iter iter;	unsigned long long i_size = i_size_read(dir); 	/* Round the file position up to the next entry boundary */	dir_ctx->pos = round_up(dir_ctx->pos, sizeof(union afs_xdr_dirent)); 	if (i_size <= 0 || dir_ctx->pos >= i_size)		return 0; 

Lines 1–500 of 2240.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t