xref: /openbmc/linux/fs/cachefiles/namei.c (revision 1f08c925)
11bd9c4e4SDavid Howells // SPDX-License-Identifier: GPL-2.0-or-later
21bd9c4e4SDavid Howells /* CacheFiles path walking and related routines
31bd9c4e4SDavid Howells  *
41bd9c4e4SDavid Howells  * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
51bd9c4e4SDavid Howells  * Written by David Howells (dhowells@redhat.com)
61bd9c4e4SDavid Howells  */
71bd9c4e4SDavid Howells 
81bd9c4e4SDavid Howells #include <linux/fs.h>
932759f7dSDavid Howells #include <linux/namei.h>
101bd9c4e4SDavid Howells #include "internal.h"
111bd9c4e4SDavid Howells 
121bd9c4e4SDavid Howells /*
131bd9c4e4SDavid Howells  * Mark the backing file as being a cache file if it's not already in use.  The
141bd9c4e4SDavid Howells  * mark tells the culling request command that it's not allowed to cull the
151bd9c4e4SDavid Howells  * file or directory.  The caller must hold the inode lock.
161bd9c4e4SDavid Howells  */
171bd9c4e4SDavid Howells static bool __cachefiles_mark_inode_in_use(struct cachefiles_object *object,
181bd9c4e4SDavid Howells 					   struct dentry *dentry)
191bd9c4e4SDavid Howells {
201bd9c4e4SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
211bd9c4e4SDavid Howells 	bool can_use = false;
221bd9c4e4SDavid Howells 
231bd9c4e4SDavid Howells 	if (!(inode->i_flags & S_KERNEL_FILE)) {
241bd9c4e4SDavid Howells 		inode->i_flags |= S_KERNEL_FILE;
251bd9c4e4SDavid Howells 		trace_cachefiles_mark_active(object, inode);
261bd9c4e4SDavid Howells 		can_use = true;
271bd9c4e4SDavid Howells 	} else {
281bd9c4e4SDavid Howells 		pr_notice("cachefiles: Inode already in use: %pd\n", dentry);
291bd9c4e4SDavid Howells 	}
301bd9c4e4SDavid Howells 
311bd9c4e4SDavid Howells 	return can_use;
321bd9c4e4SDavid Howells }
331bd9c4e4SDavid Howells 
34169379eaSDavid Howells static bool cachefiles_mark_inode_in_use(struct cachefiles_object *object,
35169379eaSDavid Howells 					 struct dentry *dentry)
36169379eaSDavid Howells {
37169379eaSDavid Howells 	struct inode *inode = d_backing_inode(dentry);
38169379eaSDavid Howells 	bool can_use;
39169379eaSDavid Howells 
40169379eaSDavid Howells 	inode_lock(inode);
41169379eaSDavid Howells 	can_use = __cachefiles_mark_inode_in_use(object, dentry);
42169379eaSDavid Howells 	inode_unlock(inode);
43169379eaSDavid Howells 	return can_use;
44169379eaSDavid Howells }
45169379eaSDavid Howells 
461bd9c4e4SDavid Howells /*
471bd9c4e4SDavid Howells  * Unmark a backing inode.  The caller must hold the inode lock.
481bd9c4e4SDavid Howells  */
491bd9c4e4SDavid Howells static void __cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
501bd9c4e4SDavid Howells 					     struct dentry *dentry)
511bd9c4e4SDavid Howells {
521bd9c4e4SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
531bd9c4e4SDavid Howells 
541bd9c4e4SDavid Howells 	inode->i_flags &= ~S_KERNEL_FILE;
551bd9c4e4SDavid Howells 	trace_cachefiles_mark_inactive(object, inode);
561bd9c4e4SDavid Howells }
5732759f7dSDavid Howells 
5832759f7dSDavid Howells /*
59169379eaSDavid Howells  * Unmark a backing inode and tell cachefilesd that there's something that can
60169379eaSDavid Howells  * be culled.
61169379eaSDavid Howells  */
62169379eaSDavid Howells void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
63169379eaSDavid Howells 				    struct file *file)
64169379eaSDavid Howells {
65169379eaSDavid Howells 	struct cachefiles_cache *cache = object->volume->cache;
66169379eaSDavid Howells 	struct inode *inode = file_inode(file);
67169379eaSDavid Howells 
68169379eaSDavid Howells 	if (inode) {
69169379eaSDavid Howells 		inode_lock(inode);
70169379eaSDavid Howells 		__cachefiles_unmark_inode_in_use(object, file->f_path.dentry);
71169379eaSDavid Howells 		inode_unlock(inode);
72169379eaSDavid Howells 
73169379eaSDavid Howells 		if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) {
74169379eaSDavid Howells 			atomic_long_add(inode->i_blocks, &cache->b_released);
75169379eaSDavid Howells 			if (atomic_inc_return(&cache->f_released))
76169379eaSDavid Howells 				cachefiles_state_changed(cache);
77169379eaSDavid Howells 		}
78169379eaSDavid Howells 	}
79169379eaSDavid Howells }
80169379eaSDavid Howells 
81169379eaSDavid Howells /*
8232759f7dSDavid Howells  * get a subdirectory
8332759f7dSDavid Howells  */
8432759f7dSDavid Howells struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
8532759f7dSDavid Howells 					struct dentry *dir,
8632759f7dSDavid Howells 					const char *dirname,
8732759f7dSDavid Howells 					bool *_is_new)
8832759f7dSDavid Howells {
8932759f7dSDavid Howells 	struct dentry *subdir;
9032759f7dSDavid Howells 	struct path path;
9132759f7dSDavid Howells 	int ret;
9232759f7dSDavid Howells 
9332759f7dSDavid Howells 	_enter(",,%s", dirname);
9432759f7dSDavid Howells 
9532759f7dSDavid Howells 	/* search the current directory for the element name */
9632759f7dSDavid Howells 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
9732759f7dSDavid Howells 
9832759f7dSDavid Howells retry:
9932759f7dSDavid Howells 	ret = cachefiles_inject_read_error();
10032759f7dSDavid Howells 	if (ret == 0)
10132759f7dSDavid Howells 		subdir = lookup_one_len(dirname, dir, strlen(dirname));
10232759f7dSDavid Howells 	else
10332759f7dSDavid Howells 		subdir = ERR_PTR(ret);
10432759f7dSDavid Howells 	if (IS_ERR(subdir)) {
10532759f7dSDavid Howells 		trace_cachefiles_vfs_error(NULL, d_backing_inode(dir),
10632759f7dSDavid Howells 					   PTR_ERR(subdir),
10732759f7dSDavid Howells 					   cachefiles_trace_lookup_error);
10832759f7dSDavid Howells 		if (PTR_ERR(subdir) == -ENOMEM)
10932759f7dSDavid Howells 			goto nomem_d_alloc;
11032759f7dSDavid Howells 		goto lookup_error;
11132759f7dSDavid Howells 	}
11232759f7dSDavid Howells 
11332759f7dSDavid Howells 	_debug("subdir -> %pd %s",
11432759f7dSDavid Howells 	       subdir, d_backing_inode(subdir) ? "positive" : "negative");
11532759f7dSDavid Howells 
11632759f7dSDavid Howells 	/* we need to create the subdir if it doesn't exist yet */
11732759f7dSDavid Howells 	if (d_is_negative(subdir)) {
11832759f7dSDavid Howells 		ret = cachefiles_has_space(cache, 1, 0);
11932759f7dSDavid Howells 		if (ret < 0)
12032759f7dSDavid Howells 			goto mkdir_error;
12132759f7dSDavid Howells 
12232759f7dSDavid Howells 		_debug("attempt mkdir");
12332759f7dSDavid Howells 
12432759f7dSDavid Howells 		path.mnt = cache->mnt;
12532759f7dSDavid Howells 		path.dentry = dir;
12632759f7dSDavid Howells 		ret = security_path_mkdir(&path, subdir, 0700);
12732759f7dSDavid Howells 		if (ret < 0)
12832759f7dSDavid Howells 			goto mkdir_error;
12932759f7dSDavid Howells 		ret = cachefiles_inject_write_error();
13032759f7dSDavid Howells 		if (ret == 0)
13132759f7dSDavid Howells 			ret = vfs_mkdir(&init_user_ns, d_inode(dir), subdir, 0700);
13232759f7dSDavid Howells 		if (ret < 0) {
13332759f7dSDavid Howells 			trace_cachefiles_vfs_error(NULL, d_inode(dir), ret,
13432759f7dSDavid Howells 						   cachefiles_trace_mkdir_error);
13532759f7dSDavid Howells 			goto mkdir_error;
13632759f7dSDavid Howells 		}
13732759f7dSDavid Howells 
13832759f7dSDavid Howells 		if (unlikely(d_unhashed(subdir))) {
13932759f7dSDavid Howells 			cachefiles_put_directory(subdir);
14032759f7dSDavid Howells 			goto retry;
14132759f7dSDavid Howells 		}
14232759f7dSDavid Howells 		ASSERT(d_backing_inode(subdir));
14332759f7dSDavid Howells 
14432759f7dSDavid Howells 		_debug("mkdir -> %pd{ino=%lu}",
14532759f7dSDavid Howells 		       subdir, d_backing_inode(subdir)->i_ino);
14632759f7dSDavid Howells 		if (_is_new)
14732759f7dSDavid Howells 			*_is_new = true;
14832759f7dSDavid Howells 	}
14932759f7dSDavid Howells 
15032759f7dSDavid Howells 	/* Tell rmdir() it's not allowed to delete the subdir */
15132759f7dSDavid Howells 	inode_lock(d_inode(subdir));
15232759f7dSDavid Howells 	inode_unlock(d_inode(dir));
15332759f7dSDavid Howells 
15432759f7dSDavid Howells 	if (!__cachefiles_mark_inode_in_use(NULL, subdir))
15532759f7dSDavid Howells 		goto mark_error;
15632759f7dSDavid Howells 
15732759f7dSDavid Howells 	inode_unlock(d_inode(subdir));
15832759f7dSDavid Howells 
15932759f7dSDavid Howells 	/* we need to make sure the subdir is a directory */
16032759f7dSDavid Howells 	ASSERT(d_backing_inode(subdir));
16132759f7dSDavid Howells 
16232759f7dSDavid Howells 	if (!d_can_lookup(subdir)) {
16332759f7dSDavid Howells 		pr_err("%s is not a directory\n", dirname);
16432759f7dSDavid Howells 		ret = -EIO;
16532759f7dSDavid Howells 		goto check_error;
16632759f7dSDavid Howells 	}
16732759f7dSDavid Howells 
16832759f7dSDavid Howells 	ret = -EPERM;
16932759f7dSDavid Howells 	if (!(d_backing_inode(subdir)->i_opflags & IOP_XATTR) ||
17032759f7dSDavid Howells 	    !d_backing_inode(subdir)->i_op->lookup ||
17132759f7dSDavid Howells 	    !d_backing_inode(subdir)->i_op->mkdir ||
17232759f7dSDavid Howells 	    !d_backing_inode(subdir)->i_op->rename ||
17332759f7dSDavid Howells 	    !d_backing_inode(subdir)->i_op->rmdir ||
17432759f7dSDavid Howells 	    !d_backing_inode(subdir)->i_op->unlink)
17532759f7dSDavid Howells 		goto check_error;
17632759f7dSDavid Howells 
17732759f7dSDavid Howells 	_leave(" = [%lu]", d_backing_inode(subdir)->i_ino);
17832759f7dSDavid Howells 	return subdir;
17932759f7dSDavid Howells 
18032759f7dSDavid Howells check_error:
18132759f7dSDavid Howells 	cachefiles_put_directory(subdir);
18232759f7dSDavid Howells 	_leave(" = %d [check]", ret);
18332759f7dSDavid Howells 	return ERR_PTR(ret);
18432759f7dSDavid Howells 
18532759f7dSDavid Howells mark_error:
18632759f7dSDavid Howells 	inode_unlock(d_inode(subdir));
18732759f7dSDavid Howells 	dput(subdir);
18832759f7dSDavid Howells 	return ERR_PTR(-EBUSY);
18932759f7dSDavid Howells 
19032759f7dSDavid Howells mkdir_error:
19132759f7dSDavid Howells 	inode_unlock(d_inode(dir));
19232759f7dSDavid Howells 	dput(subdir);
19332759f7dSDavid Howells 	pr_err("mkdir %s failed with error %d\n", dirname, ret);
19432759f7dSDavid Howells 	return ERR_PTR(ret);
19532759f7dSDavid Howells 
19632759f7dSDavid Howells lookup_error:
19732759f7dSDavid Howells 	inode_unlock(d_inode(dir));
19832759f7dSDavid Howells 	ret = PTR_ERR(subdir);
19932759f7dSDavid Howells 	pr_err("Lookup %s failed with error %d\n", dirname, ret);
20032759f7dSDavid Howells 	return ERR_PTR(ret);
20132759f7dSDavid Howells 
20232759f7dSDavid Howells nomem_d_alloc:
20332759f7dSDavid Howells 	inode_unlock(d_inode(dir));
20432759f7dSDavid Howells 	_leave(" = -ENOMEM");
20532759f7dSDavid Howells 	return ERR_PTR(-ENOMEM);
20632759f7dSDavid Howells }
20732759f7dSDavid Howells 
20832759f7dSDavid Howells /*
20932759f7dSDavid Howells  * Put a subdirectory.
21032759f7dSDavid Howells  */
21132759f7dSDavid Howells void cachefiles_put_directory(struct dentry *dir)
21232759f7dSDavid Howells {
21332759f7dSDavid Howells 	if (dir) {
21432759f7dSDavid Howells 		inode_lock(dir->d_inode);
21532759f7dSDavid Howells 		__cachefiles_unmark_inode_in_use(NULL, dir);
21632759f7dSDavid Howells 		inode_unlock(dir->d_inode);
21732759f7dSDavid Howells 		dput(dir);
21832759f7dSDavid Howells 	}
21932759f7dSDavid Howells }
22007a90e97SDavid Howells 
22107a90e97SDavid Howells /*
22207a90e97SDavid Howells  * Remove a regular file from the cache.
22307a90e97SDavid Howells  */
22407a90e97SDavid Howells static int cachefiles_unlink(struct cachefiles_cache *cache,
22507a90e97SDavid Howells 			     struct cachefiles_object *object,
22607a90e97SDavid Howells 			     struct dentry *dir, struct dentry *dentry,
22707a90e97SDavid Howells 			     enum fscache_why_object_killed why)
22807a90e97SDavid Howells {
22907a90e97SDavid Howells 	struct path path = {
23007a90e97SDavid Howells 		.mnt	= cache->mnt,
23107a90e97SDavid Howells 		.dentry	= dir,
23207a90e97SDavid Howells 	};
23307a90e97SDavid Howells 	int ret;
23407a90e97SDavid Howells 
23507a90e97SDavid Howells 	trace_cachefiles_unlink(object, dentry, why);
23607a90e97SDavid Howells 	ret = security_path_unlink(&path, dentry);
23707a90e97SDavid Howells 	if (ret < 0) {
23807a90e97SDavid Howells 		cachefiles_io_error(cache, "Unlink security error");
23907a90e97SDavid Howells 		return ret;
24007a90e97SDavid Howells 	}
24107a90e97SDavid Howells 
24207a90e97SDavid Howells 	ret = cachefiles_inject_remove_error();
24307a90e97SDavid Howells 	if (ret == 0) {
24407a90e97SDavid Howells 		ret = vfs_unlink(&init_user_ns, d_backing_inode(dir), dentry, NULL);
24507a90e97SDavid Howells 		if (ret == -EIO)
24607a90e97SDavid Howells 			cachefiles_io_error(cache, "Unlink failed");
24707a90e97SDavid Howells 	}
24807a90e97SDavid Howells 	if (ret != 0)
24907a90e97SDavid Howells 		trace_cachefiles_vfs_error(object, d_backing_inode(dir), ret,
25007a90e97SDavid Howells 					   cachefiles_trace_unlink_error);
25107a90e97SDavid Howells 	return ret;
25207a90e97SDavid Howells }
25307a90e97SDavid Howells 
25407a90e97SDavid Howells /*
25507a90e97SDavid Howells  * Delete an object representation from the cache
25607a90e97SDavid Howells  * - File backed objects are unlinked
25707a90e97SDavid Howells  * - Directory backed objects are stuffed into the graveyard for userspace to
25807a90e97SDavid Howells  *   delete
25907a90e97SDavid Howells  */
26007a90e97SDavid Howells int cachefiles_bury_object(struct cachefiles_cache *cache,
26107a90e97SDavid Howells 			   struct cachefiles_object *object,
26207a90e97SDavid Howells 			   struct dentry *dir,
26307a90e97SDavid Howells 			   struct dentry *rep,
26407a90e97SDavid Howells 			   enum fscache_why_object_killed why)
26507a90e97SDavid Howells {
26607a90e97SDavid Howells 	struct dentry *grave, *trap;
26707a90e97SDavid Howells 	struct path path, path_to_graveyard;
26807a90e97SDavid Howells 	char nbuffer[8 + 8 + 1];
26907a90e97SDavid Howells 	int ret;
27007a90e97SDavid Howells 
27107a90e97SDavid Howells 	_enter(",'%pd','%pd'", dir, rep);
27207a90e97SDavid Howells 
27307a90e97SDavid Howells 	if (rep->d_parent != dir) {
27407a90e97SDavid Howells 		inode_unlock(d_inode(dir));
27507a90e97SDavid Howells 		_leave(" = -ESTALE");
27607a90e97SDavid Howells 		return -ESTALE;
27707a90e97SDavid Howells 	}
27807a90e97SDavid Howells 
27907a90e97SDavid Howells 	/* non-directories can just be unlinked */
28007a90e97SDavid Howells 	if (!d_is_dir(rep)) {
28107a90e97SDavid Howells 		dget(rep); /* Stop the dentry being negated if it's only pinned
28207a90e97SDavid Howells 			    * by a file struct.
28307a90e97SDavid Howells 			    */
28407a90e97SDavid Howells 		ret = cachefiles_unlink(cache, object, dir, rep, why);
28507a90e97SDavid Howells 		dput(rep);
28607a90e97SDavid Howells 
28707a90e97SDavid Howells 		inode_unlock(d_inode(dir));
28807a90e97SDavid Howells 		_leave(" = %d", ret);
28907a90e97SDavid Howells 		return ret;
29007a90e97SDavid Howells 	}
29107a90e97SDavid Howells 
29207a90e97SDavid Howells 	/* directories have to be moved to the graveyard */
29307a90e97SDavid Howells 	_debug("move stale object to graveyard");
29407a90e97SDavid Howells 	inode_unlock(d_inode(dir));
29507a90e97SDavid Howells 
29607a90e97SDavid Howells try_again:
29707a90e97SDavid Howells 	/* first step is to make up a grave dentry in the graveyard */
29807a90e97SDavid Howells 	sprintf(nbuffer, "%08x%08x",
29907a90e97SDavid Howells 		(uint32_t) ktime_get_real_seconds(),
30007a90e97SDavid Howells 		(uint32_t) atomic_inc_return(&cache->gravecounter));
30107a90e97SDavid Howells 
30207a90e97SDavid Howells 	/* do the multiway lock magic */
30307a90e97SDavid Howells 	trap = lock_rename(cache->graveyard, dir);
30407a90e97SDavid Howells 
30507a90e97SDavid Howells 	/* do some checks before getting the grave dentry */
30607a90e97SDavid Howells 	if (rep->d_parent != dir || IS_DEADDIR(d_inode(rep))) {
30707a90e97SDavid Howells 		/* the entry was probably culled when we dropped the parent dir
30807a90e97SDavid Howells 		 * lock */
30907a90e97SDavid Howells 		unlock_rename(cache->graveyard, dir);
31007a90e97SDavid Howells 		_leave(" = 0 [culled?]");
31107a90e97SDavid Howells 		return 0;
31207a90e97SDavid Howells 	}
31307a90e97SDavid Howells 
31407a90e97SDavid Howells 	if (!d_can_lookup(cache->graveyard)) {
31507a90e97SDavid Howells 		unlock_rename(cache->graveyard, dir);
31607a90e97SDavid Howells 		cachefiles_io_error(cache, "Graveyard no longer a directory");
31707a90e97SDavid Howells 		return -EIO;
31807a90e97SDavid Howells 	}
31907a90e97SDavid Howells 
32007a90e97SDavid Howells 	if (trap == rep) {
32107a90e97SDavid Howells 		unlock_rename(cache->graveyard, dir);
32207a90e97SDavid Howells 		cachefiles_io_error(cache, "May not make directory loop");
32307a90e97SDavid Howells 		return -EIO;
32407a90e97SDavid Howells 	}
32507a90e97SDavid Howells 
32607a90e97SDavid Howells 	if (d_mountpoint(rep)) {
32707a90e97SDavid Howells 		unlock_rename(cache->graveyard, dir);
32807a90e97SDavid Howells 		cachefiles_io_error(cache, "Mountpoint in cache");
32907a90e97SDavid Howells 		return -EIO;
33007a90e97SDavid Howells 	}
33107a90e97SDavid Howells 
33207a90e97SDavid Howells 	grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
33307a90e97SDavid Howells 	if (IS_ERR(grave)) {
33407a90e97SDavid Howells 		unlock_rename(cache->graveyard, dir);
33507a90e97SDavid Howells 		trace_cachefiles_vfs_error(object, d_inode(cache->graveyard),
33607a90e97SDavid Howells 					   PTR_ERR(grave),
33707a90e97SDavid Howells 					   cachefiles_trace_lookup_error);
33807a90e97SDavid Howells 
33907a90e97SDavid Howells 		if (PTR_ERR(grave) == -ENOMEM) {
34007a90e97SDavid Howells 			_leave(" = -ENOMEM");
34107a90e97SDavid Howells 			return -ENOMEM;
34207a90e97SDavid Howells 		}
34307a90e97SDavid Howells 
34407a90e97SDavid Howells 		cachefiles_io_error(cache, "Lookup error %ld", PTR_ERR(grave));
34507a90e97SDavid Howells 		return -EIO;
34607a90e97SDavid Howells 	}
34707a90e97SDavid Howells 
34807a90e97SDavid Howells 	if (d_is_positive(grave)) {
34907a90e97SDavid Howells 		unlock_rename(cache->graveyard, dir);
35007a90e97SDavid Howells 		dput(grave);
35107a90e97SDavid Howells 		grave = NULL;
35207a90e97SDavid Howells 		cond_resched();
35307a90e97SDavid Howells 		goto try_again;
35407a90e97SDavid Howells 	}
35507a90e97SDavid Howells 
35607a90e97SDavid Howells 	if (d_mountpoint(grave)) {
35707a90e97SDavid Howells 		unlock_rename(cache->graveyard, dir);
35807a90e97SDavid Howells 		dput(grave);
35907a90e97SDavid Howells 		cachefiles_io_error(cache, "Mountpoint in graveyard");
36007a90e97SDavid Howells 		return -EIO;
36107a90e97SDavid Howells 	}
36207a90e97SDavid Howells 
36307a90e97SDavid Howells 	/* target should not be an ancestor of source */
36407a90e97SDavid Howells 	if (trap == grave) {
36507a90e97SDavid Howells 		unlock_rename(cache->graveyard, dir);
36607a90e97SDavid Howells 		dput(grave);
36707a90e97SDavid Howells 		cachefiles_io_error(cache, "May not make directory loop");
36807a90e97SDavid Howells 		return -EIO;
36907a90e97SDavid Howells 	}
37007a90e97SDavid Howells 
37107a90e97SDavid Howells 	/* attempt the rename */
37207a90e97SDavid Howells 	path.mnt = cache->mnt;
37307a90e97SDavid Howells 	path.dentry = dir;
37407a90e97SDavid Howells 	path_to_graveyard.mnt = cache->mnt;
37507a90e97SDavid Howells 	path_to_graveyard.dentry = cache->graveyard;
37607a90e97SDavid Howells 	ret = security_path_rename(&path, rep, &path_to_graveyard, grave, 0);
37707a90e97SDavid Howells 	if (ret < 0) {
37807a90e97SDavid Howells 		cachefiles_io_error(cache, "Rename security error %d", ret);
37907a90e97SDavid Howells 	} else {
38007a90e97SDavid Howells 		struct renamedata rd = {
38107a90e97SDavid Howells 			.old_mnt_userns	= &init_user_ns,
38207a90e97SDavid Howells 			.old_dir	= d_inode(dir),
38307a90e97SDavid Howells 			.old_dentry	= rep,
38407a90e97SDavid Howells 			.new_mnt_userns	= &init_user_ns,
38507a90e97SDavid Howells 			.new_dir	= d_inode(cache->graveyard),
38607a90e97SDavid Howells 			.new_dentry	= grave,
38707a90e97SDavid Howells 		};
38807a90e97SDavid Howells 		trace_cachefiles_rename(object, rep, grave, why);
38907a90e97SDavid Howells 		ret = cachefiles_inject_read_error();
39007a90e97SDavid Howells 		if (ret == 0)
39107a90e97SDavid Howells 			ret = vfs_rename(&rd);
39207a90e97SDavid Howells 		if (ret != 0)
39307a90e97SDavid Howells 			trace_cachefiles_vfs_error(object, d_inode(dir), ret,
39407a90e97SDavid Howells 						   cachefiles_trace_rename_error);
39507a90e97SDavid Howells 		if (ret != 0 && ret != -ENOMEM)
39607a90e97SDavid Howells 			cachefiles_io_error(cache,
39707a90e97SDavid Howells 					    "Rename failed with error %d", ret);
39807a90e97SDavid Howells 	}
39907a90e97SDavid Howells 
40007a90e97SDavid Howells 	__cachefiles_unmark_inode_in_use(object, rep);
40107a90e97SDavid Howells 	unlock_rename(cache->graveyard, dir);
40207a90e97SDavid Howells 	dput(grave);
40307a90e97SDavid Howells 	_leave(" = 0");
40407a90e97SDavid Howells 	return 0;
40507a90e97SDavid Howells }
40607a90e97SDavid Howells 
40707a90e97SDavid Howells /*
408*1f08c925SDavid Howells  * Delete a cache file.
409*1f08c925SDavid Howells  */
410*1f08c925SDavid Howells int cachefiles_delete_object(struct cachefiles_object *object,
411*1f08c925SDavid Howells 			     enum fscache_why_object_killed why)
412*1f08c925SDavid Howells {
413*1f08c925SDavid Howells 	struct cachefiles_volume *volume = object->volume;
414*1f08c925SDavid Howells 	struct dentry *dentry = object->file->f_path.dentry;
415*1f08c925SDavid Howells 	struct dentry *fan = volume->fanout[(u8)object->cookie->key_hash];
416*1f08c925SDavid Howells 	int ret;
417*1f08c925SDavid Howells 
418*1f08c925SDavid Howells 	_enter(",OBJ%x{%pD}", object->debug_id, object->file);
419*1f08c925SDavid Howells 
420*1f08c925SDavid Howells 	/* Stop the dentry being negated if it's only pinned by a file struct. */
421*1f08c925SDavid Howells 	dget(dentry);
422*1f08c925SDavid Howells 
423*1f08c925SDavid Howells 	inode_lock_nested(d_backing_inode(fan), I_MUTEX_PARENT);
424*1f08c925SDavid Howells 	ret = cachefiles_unlink(volume->cache, object, fan, dentry, why);
425*1f08c925SDavid Howells 	inode_unlock(d_backing_inode(fan));
426*1f08c925SDavid Howells 	dput(dentry);
427*1f08c925SDavid Howells 	return ret;
428*1f08c925SDavid Howells }
429*1f08c925SDavid Howells 
430*1f08c925SDavid Howells /*
431*1f08c925SDavid Howells  * Create a temporary file and leave it unattached and un-xattr'd until the
432*1f08c925SDavid Howells  * time comes to discard the object from memory.
433*1f08c925SDavid Howells  */
434*1f08c925SDavid Howells struct file *cachefiles_create_tmpfile(struct cachefiles_object *object)
435*1f08c925SDavid Howells {
436*1f08c925SDavid Howells 	struct cachefiles_volume *volume = object->volume;
437*1f08c925SDavid Howells 	struct cachefiles_cache *cache = volume->cache;
438*1f08c925SDavid Howells 	const struct cred *saved_cred;
439*1f08c925SDavid Howells 	struct dentry *fan = volume->fanout[(u8)object->cookie->key_hash];
440*1f08c925SDavid Howells 	struct file *file;
441*1f08c925SDavid Howells 	struct path path;
442*1f08c925SDavid Howells 	uint64_t ni_size = object->cookie->object_size;
443*1f08c925SDavid Howells 	long ret;
444*1f08c925SDavid Howells 
445*1f08c925SDavid Howells 	ni_size = round_up(ni_size, CACHEFILES_DIO_BLOCK_SIZE);
446*1f08c925SDavid Howells 
447*1f08c925SDavid Howells 	cachefiles_begin_secure(cache, &saved_cred);
448*1f08c925SDavid Howells 
449*1f08c925SDavid Howells 	path.mnt = cache->mnt;
450*1f08c925SDavid Howells 	ret = cachefiles_inject_write_error();
451*1f08c925SDavid Howells 	if (ret == 0)
452*1f08c925SDavid Howells 		path.dentry = vfs_tmpfile(&init_user_ns, fan, S_IFREG, O_RDWR);
453*1f08c925SDavid Howells 	else
454*1f08c925SDavid Howells 		path.dentry = ERR_PTR(ret);
455*1f08c925SDavid Howells 	if (IS_ERR(path.dentry)) {
456*1f08c925SDavid Howells 		trace_cachefiles_vfs_error(object, d_inode(fan), PTR_ERR(path.dentry),
457*1f08c925SDavid Howells 					   cachefiles_trace_tmpfile_error);
458*1f08c925SDavid Howells 		if (PTR_ERR(path.dentry) == -EIO)
459*1f08c925SDavid Howells 			cachefiles_io_error_obj(object, "Failed to create tmpfile");
460*1f08c925SDavid Howells 		file = ERR_CAST(path.dentry);
461*1f08c925SDavid Howells 		goto out;
462*1f08c925SDavid Howells 	}
463*1f08c925SDavid Howells 
464*1f08c925SDavid Howells 	trace_cachefiles_tmpfile(object, d_backing_inode(path.dentry));
465*1f08c925SDavid Howells 
466*1f08c925SDavid Howells 	if (!cachefiles_mark_inode_in_use(object, path.dentry)) {
467*1f08c925SDavid Howells 		file = ERR_PTR(-EBUSY);
468*1f08c925SDavid Howells 		goto out_dput;
469*1f08c925SDavid Howells 	}
470*1f08c925SDavid Howells 
471*1f08c925SDavid Howells 	if (ni_size > 0) {
472*1f08c925SDavid Howells 		trace_cachefiles_trunc(object, d_backing_inode(path.dentry), 0, ni_size,
473*1f08c925SDavid Howells 				       cachefiles_trunc_expand_tmpfile);
474*1f08c925SDavid Howells 		ret = cachefiles_inject_write_error();
475*1f08c925SDavid Howells 		if (ret == 0)
476*1f08c925SDavid Howells 			ret = vfs_truncate(&path, ni_size);
477*1f08c925SDavid Howells 		if (ret < 0) {
478*1f08c925SDavid Howells 			trace_cachefiles_vfs_error(
479*1f08c925SDavid Howells 				object, d_backing_inode(path.dentry), ret,
480*1f08c925SDavid Howells 				cachefiles_trace_trunc_error);
481*1f08c925SDavid Howells 			file = ERR_PTR(ret);
482*1f08c925SDavid Howells 			goto out_dput;
483*1f08c925SDavid Howells 		}
484*1f08c925SDavid Howells 	}
485*1f08c925SDavid Howells 
486*1f08c925SDavid Howells 	file = open_with_fake_path(&path, O_RDWR | O_LARGEFILE | O_DIRECT,
487*1f08c925SDavid Howells 				   d_backing_inode(path.dentry), cache->cache_cred);
488*1f08c925SDavid Howells 	if (IS_ERR(file)) {
489*1f08c925SDavid Howells 		trace_cachefiles_vfs_error(object, d_backing_inode(path.dentry),
490*1f08c925SDavid Howells 					   PTR_ERR(file),
491*1f08c925SDavid Howells 					   cachefiles_trace_open_error);
492*1f08c925SDavid Howells 		goto out_dput;
493*1f08c925SDavid Howells 	}
494*1f08c925SDavid Howells 	if (unlikely(!file->f_op->read_iter) ||
495*1f08c925SDavid Howells 	    unlikely(!file->f_op->write_iter)) {
496*1f08c925SDavid Howells 		fput(file);
497*1f08c925SDavid Howells 		pr_notice("Cache does not support read_iter and write_iter\n");
498*1f08c925SDavid Howells 		file = ERR_PTR(-EINVAL);
499*1f08c925SDavid Howells 	}
500*1f08c925SDavid Howells 
501*1f08c925SDavid Howells out_dput:
502*1f08c925SDavid Howells 	dput(path.dentry);
503*1f08c925SDavid Howells out:
504*1f08c925SDavid Howells 	cachefiles_end_secure(cache, saved_cred);
505*1f08c925SDavid Howells 	return file;
506*1f08c925SDavid Howells }
507*1f08c925SDavid Howells 
508*1f08c925SDavid Howells /*
509*1f08c925SDavid Howells  * Create a new file.
510*1f08c925SDavid Howells  */
511*1f08c925SDavid Howells static bool cachefiles_create_file(struct cachefiles_object *object)
512*1f08c925SDavid Howells {
513*1f08c925SDavid Howells 	struct file *file;
514*1f08c925SDavid Howells 	int ret;
515*1f08c925SDavid Howells 
516*1f08c925SDavid Howells 	ret = cachefiles_has_space(object->volume->cache, 1, 0);
517*1f08c925SDavid Howells 	if (ret < 0)
518*1f08c925SDavid Howells 		return false;
519*1f08c925SDavid Howells 
520*1f08c925SDavid Howells 	file = cachefiles_create_tmpfile(object);
521*1f08c925SDavid Howells 	if (IS_ERR(file))
522*1f08c925SDavid Howells 		return false;
523*1f08c925SDavid Howells 
524*1f08c925SDavid Howells 	set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &object->cookie->flags);
525*1f08c925SDavid Howells 	set_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags);
526*1f08c925SDavid Howells 	_debug("create -> %pD{ino=%lu}", file, file_inode(file)->i_ino);
527*1f08c925SDavid Howells 	object->file = file;
528*1f08c925SDavid Howells 	return true;
529*1f08c925SDavid Howells }
530*1f08c925SDavid Howells 
531*1f08c925SDavid Howells /*
532*1f08c925SDavid Howells  * Open an existing file, checking its attributes and replacing it if it is
533*1f08c925SDavid Howells  * stale.
534*1f08c925SDavid Howells  */
535*1f08c925SDavid Howells static bool cachefiles_open_file(struct cachefiles_object *object,
536*1f08c925SDavid Howells 				 struct dentry *dentry)
537*1f08c925SDavid Howells {
538*1f08c925SDavid Howells 	struct cachefiles_cache *cache = object->volume->cache;
539*1f08c925SDavid Howells 	struct file *file;
540*1f08c925SDavid Howells 	struct path path;
541*1f08c925SDavid Howells 	int ret;
542*1f08c925SDavid Howells 
543*1f08c925SDavid Howells 	_enter("%pd", dentry);
544*1f08c925SDavid Howells 
545*1f08c925SDavid Howells 	if (!cachefiles_mark_inode_in_use(object, dentry))
546*1f08c925SDavid Howells 		return false;
547*1f08c925SDavid Howells 
548*1f08c925SDavid Howells 	/* We need to open a file interface onto a data file now as we can't do
549*1f08c925SDavid Howells 	 * it on demand because writeback called from do_exit() sees
550*1f08c925SDavid Howells 	 * current->fs == NULL - which breaks d_path() called from ext4 open.
551*1f08c925SDavid Howells 	 */
552*1f08c925SDavid Howells 	path.mnt = cache->mnt;
553*1f08c925SDavid Howells 	path.dentry = dentry;
554*1f08c925SDavid Howells 	file = open_with_fake_path(&path, O_RDWR | O_LARGEFILE | O_DIRECT,
555*1f08c925SDavid Howells 				   d_backing_inode(dentry), cache->cache_cred);
556*1f08c925SDavid Howells 	if (IS_ERR(file)) {
557*1f08c925SDavid Howells 		trace_cachefiles_vfs_error(object, d_backing_inode(dentry),
558*1f08c925SDavid Howells 					   PTR_ERR(file),
559*1f08c925SDavid Howells 					   cachefiles_trace_open_error);
560*1f08c925SDavid Howells 		goto error;
561*1f08c925SDavid Howells 	}
562*1f08c925SDavid Howells 
563*1f08c925SDavid Howells 	if (unlikely(!file->f_op->read_iter) ||
564*1f08c925SDavid Howells 	    unlikely(!file->f_op->write_iter)) {
565*1f08c925SDavid Howells 		pr_notice("Cache does not support read_iter and write_iter\n");
566*1f08c925SDavid Howells 		goto error_fput;
567*1f08c925SDavid Howells 	}
568*1f08c925SDavid Howells 	_debug("file -> %pd positive", dentry);
569*1f08c925SDavid Howells 
570*1f08c925SDavid Howells 	ret = cachefiles_check_auxdata(object, file);
571*1f08c925SDavid Howells 	if (ret < 0)
572*1f08c925SDavid Howells 		goto check_failed;
573*1f08c925SDavid Howells 
574*1f08c925SDavid Howells 	object->file = file;
575*1f08c925SDavid Howells 
576*1f08c925SDavid Howells 	/* Always update the atime on an object we've just looked up (this is
577*1f08c925SDavid Howells 	 * used to keep track of culling, and atimes are only updated by read,
578*1f08c925SDavid Howells 	 * write and readdir but not lookup or open).
579*1f08c925SDavid Howells 	 */
580*1f08c925SDavid Howells 	touch_atime(&file->f_path);
581*1f08c925SDavid Howells 	dput(dentry);
582*1f08c925SDavid Howells 	return true;
583*1f08c925SDavid Howells 
584*1f08c925SDavid Howells check_failed:
585*1f08c925SDavid Howells 	fscache_cookie_lookup_negative(object->cookie);
586*1f08c925SDavid Howells 	cachefiles_unmark_inode_in_use(object, file);
587*1f08c925SDavid Howells 	if (ret == -ESTALE) {
588*1f08c925SDavid Howells 		fput(file);
589*1f08c925SDavid Howells 		dput(dentry);
590*1f08c925SDavid Howells 		return cachefiles_create_file(object);
591*1f08c925SDavid Howells 	}
592*1f08c925SDavid Howells error_fput:
593*1f08c925SDavid Howells 	fput(file);
594*1f08c925SDavid Howells error:
595*1f08c925SDavid Howells 	dput(dentry);
596*1f08c925SDavid Howells 	return false;
597*1f08c925SDavid Howells }
598*1f08c925SDavid Howells 
599*1f08c925SDavid Howells /*
600*1f08c925SDavid Howells  * walk from the parent object to the child object through the backing
601*1f08c925SDavid Howells  * filesystem, creating directories as we go
602*1f08c925SDavid Howells  */
603*1f08c925SDavid Howells bool cachefiles_look_up_object(struct cachefiles_object *object)
604*1f08c925SDavid Howells {
605*1f08c925SDavid Howells 	struct cachefiles_volume *volume = object->volume;
606*1f08c925SDavid Howells 	struct dentry *dentry, *fan = volume->fanout[(u8)object->cookie->key_hash];
607*1f08c925SDavid Howells 	int ret;
608*1f08c925SDavid Howells 
609*1f08c925SDavid Howells 	_enter("OBJ%x,%s,", object->debug_id, object->d_name);
610*1f08c925SDavid Howells 
611*1f08c925SDavid Howells 	/* Look up path "cache/vol/fanout/file". */
612*1f08c925SDavid Howells 	ret = cachefiles_inject_read_error();
613*1f08c925SDavid Howells 	if (ret == 0)
614*1f08c925SDavid Howells 		dentry = lookup_positive_unlocked(object->d_name, fan,
615*1f08c925SDavid Howells 						  object->d_name_len);
616*1f08c925SDavid Howells 	else
617*1f08c925SDavid Howells 		dentry = ERR_PTR(ret);
618*1f08c925SDavid Howells 	trace_cachefiles_lookup(object, dentry);
619*1f08c925SDavid Howells 	if (IS_ERR(dentry)) {
620*1f08c925SDavid Howells 		if (dentry == ERR_PTR(-ENOENT))
621*1f08c925SDavid Howells 			goto new_file;
622*1f08c925SDavid Howells 		if (dentry == ERR_PTR(-EIO))
623*1f08c925SDavid Howells 			cachefiles_io_error_obj(object, "Lookup failed");
624*1f08c925SDavid Howells 		return false;
625*1f08c925SDavid Howells 	}
626*1f08c925SDavid Howells 
627*1f08c925SDavid Howells 	if (!d_is_reg(dentry)) {
628*1f08c925SDavid Howells 		pr_err("%pd is not a file\n", dentry);
629*1f08c925SDavid Howells 		inode_lock_nested(d_inode(fan), I_MUTEX_PARENT);
630*1f08c925SDavid Howells 		ret = cachefiles_bury_object(volume->cache, object, fan, dentry,
631*1f08c925SDavid Howells 					     FSCACHE_OBJECT_IS_WEIRD);
632*1f08c925SDavid Howells 		dput(dentry);
633*1f08c925SDavid Howells 		if (ret < 0)
634*1f08c925SDavid Howells 			return false;
635*1f08c925SDavid Howells 		goto new_file;
636*1f08c925SDavid Howells 	}
637*1f08c925SDavid Howells 
638*1f08c925SDavid Howells 	if (!cachefiles_open_file(object, dentry))
639*1f08c925SDavid Howells 		return false;
640*1f08c925SDavid Howells 
641*1f08c925SDavid Howells 	_leave(" = t [%lu]", file_inode(object->file)->i_ino);
642*1f08c925SDavid Howells 	return true;
643*1f08c925SDavid Howells 
644*1f08c925SDavid Howells new_file:
645*1f08c925SDavid Howells 	fscache_cookie_lookup_negative(object->cookie);
646*1f08c925SDavid Howells 	return cachefiles_create_file(object);
647*1f08c925SDavid Howells }
648*1f08c925SDavid Howells 
649*1f08c925SDavid Howells /*
650*1f08c925SDavid Howells  * Attempt to link a temporary file into its rightful place in the cache.
651*1f08c925SDavid Howells  */
652*1f08c925SDavid Howells bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache,
653*1f08c925SDavid Howells 			       struct cachefiles_object *object)
654*1f08c925SDavid Howells {
655*1f08c925SDavid Howells 	struct cachefiles_volume *volume = object->volume;
656*1f08c925SDavid Howells 	struct dentry *dentry, *fan = volume->fanout[(u8)object->cookie->key_hash];
657*1f08c925SDavid Howells 	bool success = false;
658*1f08c925SDavid Howells 	int ret;
659*1f08c925SDavid Howells 
660*1f08c925SDavid Howells 	_enter(",%pD", object->file);
661*1f08c925SDavid Howells 
662*1f08c925SDavid Howells 	inode_lock_nested(d_inode(fan), I_MUTEX_PARENT);
663*1f08c925SDavid Howells 	ret = cachefiles_inject_read_error();
664*1f08c925SDavid Howells 	if (ret == 0)
665*1f08c925SDavid Howells 		dentry = lookup_one_len(object->d_name, fan, object->d_name_len);
666*1f08c925SDavid Howells 	else
667*1f08c925SDavid Howells 		dentry = ERR_PTR(ret);
668*1f08c925SDavid Howells 	if (IS_ERR(dentry)) {
669*1f08c925SDavid Howells 		trace_cachefiles_vfs_error(object, d_inode(fan), PTR_ERR(dentry),
670*1f08c925SDavid Howells 					   cachefiles_trace_lookup_error);
671*1f08c925SDavid Howells 		_debug("lookup fail %ld", PTR_ERR(dentry));
672*1f08c925SDavid Howells 		goto out_unlock;
673*1f08c925SDavid Howells 	}
674*1f08c925SDavid Howells 
675*1f08c925SDavid Howells 	if (!d_is_negative(dentry)) {
676*1f08c925SDavid Howells 		if (d_backing_inode(dentry) == file_inode(object->file)) {
677*1f08c925SDavid Howells 			success = true;
678*1f08c925SDavid Howells 			goto out_dput;
679*1f08c925SDavid Howells 		}
680*1f08c925SDavid Howells 
681*1f08c925SDavid Howells 		ret = cachefiles_unlink(volume->cache, object, fan, dentry,
682*1f08c925SDavid Howells 					FSCACHE_OBJECT_IS_STALE);
683*1f08c925SDavid Howells 		if (ret < 0)
684*1f08c925SDavid Howells 			goto out_dput;
685*1f08c925SDavid Howells 
686*1f08c925SDavid Howells 		dput(dentry);
687*1f08c925SDavid Howells 		ret = cachefiles_inject_read_error();
688*1f08c925SDavid Howells 		if (ret == 0)
689*1f08c925SDavid Howells 			dentry = lookup_one_len(object->d_name, fan, object->d_name_len);
690*1f08c925SDavid Howells 		else
691*1f08c925SDavid Howells 			dentry = ERR_PTR(ret);
692*1f08c925SDavid Howells 		if (IS_ERR(dentry)) {
693*1f08c925SDavid Howells 			trace_cachefiles_vfs_error(object, d_inode(fan), PTR_ERR(dentry),
694*1f08c925SDavid Howells 						   cachefiles_trace_lookup_error);
695*1f08c925SDavid Howells 			_debug("lookup fail %ld", PTR_ERR(dentry));
696*1f08c925SDavid Howells 			goto out_unlock;
697*1f08c925SDavid Howells 		}
698*1f08c925SDavid Howells 	}
699*1f08c925SDavid Howells 
700*1f08c925SDavid Howells 	ret = cachefiles_inject_read_error();
701*1f08c925SDavid Howells 	if (ret == 0)
702*1f08c925SDavid Howells 		ret = vfs_link(object->file->f_path.dentry, &init_user_ns,
703*1f08c925SDavid Howells 			       d_inode(fan), dentry, NULL);
704*1f08c925SDavid Howells 	if (ret < 0) {
705*1f08c925SDavid Howells 		trace_cachefiles_vfs_error(object, d_inode(fan), ret,
706*1f08c925SDavid Howells 					   cachefiles_trace_link_error);
707*1f08c925SDavid Howells 		_debug("link fail %d", ret);
708*1f08c925SDavid Howells 	} else {
709*1f08c925SDavid Howells 		trace_cachefiles_link(object, file_inode(object->file));
710*1f08c925SDavid Howells 		spin_lock(&object->lock);
711*1f08c925SDavid Howells 		/* TODO: Do we want to switch the file pointer to the new dentry? */
712*1f08c925SDavid Howells 		clear_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags);
713*1f08c925SDavid Howells 		spin_unlock(&object->lock);
714*1f08c925SDavid Howells 		success = true;
715*1f08c925SDavid Howells 	}
716*1f08c925SDavid Howells 
717*1f08c925SDavid Howells out_dput:
718*1f08c925SDavid Howells 	dput(dentry);
719*1f08c925SDavid Howells out_unlock:
720*1f08c925SDavid Howells 	inode_unlock(d_inode(fan));
721*1f08c925SDavid Howells 	_leave(" = %u", success);
722*1f08c925SDavid Howells 	return success;
723*1f08c925SDavid Howells }
724*1f08c925SDavid Howells 
725*1f08c925SDavid Howells /*
72607a90e97SDavid Howells  * Look up an inode to be checked or culled.  Return -EBUSY if the inode is
72707a90e97SDavid Howells  * marked in use.
72807a90e97SDavid Howells  */
72907a90e97SDavid Howells static struct dentry *cachefiles_lookup_for_cull(struct cachefiles_cache *cache,
73007a90e97SDavid Howells 						 struct dentry *dir,
73107a90e97SDavid Howells 						 char *filename)
73207a90e97SDavid Howells {
73307a90e97SDavid Howells 	struct dentry *victim;
73407a90e97SDavid Howells 	int ret = -ENOENT;
73507a90e97SDavid Howells 
73607a90e97SDavid Howells 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
73707a90e97SDavid Howells 
73807a90e97SDavid Howells 	victim = lookup_one_len(filename, dir, strlen(filename));
73907a90e97SDavid Howells 	if (IS_ERR(victim))
74007a90e97SDavid Howells 		goto lookup_error;
74107a90e97SDavid Howells 	if (d_is_negative(victim))
74207a90e97SDavid Howells 		goto lookup_put;
74307a90e97SDavid Howells 	if (d_inode(victim)->i_flags & S_KERNEL_FILE)
74407a90e97SDavid Howells 		goto lookup_busy;
74507a90e97SDavid Howells 	return victim;
74607a90e97SDavid Howells 
74707a90e97SDavid Howells lookup_busy:
74807a90e97SDavid Howells 	ret = -EBUSY;
74907a90e97SDavid Howells lookup_put:
75007a90e97SDavid Howells 	inode_unlock(d_inode(dir));
75107a90e97SDavid Howells 	dput(victim);
75207a90e97SDavid Howells 	return ERR_PTR(ret);
75307a90e97SDavid Howells 
75407a90e97SDavid Howells lookup_error:
75507a90e97SDavid Howells 	inode_unlock(d_inode(dir));
75607a90e97SDavid Howells 	ret = PTR_ERR(victim);
75707a90e97SDavid Howells 	if (ret == -ENOENT)
75807a90e97SDavid Howells 		return ERR_PTR(-ESTALE); /* Probably got retired by the netfs */
75907a90e97SDavid Howells 
76007a90e97SDavid Howells 	if (ret == -EIO) {
76107a90e97SDavid Howells 		cachefiles_io_error(cache, "Lookup failed");
76207a90e97SDavid Howells 	} else if (ret != -ENOMEM) {
76307a90e97SDavid Howells 		pr_err("Internal error: %d\n", ret);
76407a90e97SDavid Howells 		ret = -EIO;
76507a90e97SDavid Howells 	}
76607a90e97SDavid Howells 
76707a90e97SDavid Howells 	return ERR_PTR(ret);
76807a90e97SDavid Howells }
76907a90e97SDavid Howells 
77007a90e97SDavid Howells /*
77107a90e97SDavid Howells  * Cull an object if it's not in use
77207a90e97SDavid Howells  * - called only by cache manager daemon
77307a90e97SDavid Howells  */
77407a90e97SDavid Howells int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
77507a90e97SDavid Howells 		    char *filename)
77607a90e97SDavid Howells {
77707a90e97SDavid Howells 	struct dentry *victim;
77807a90e97SDavid Howells 	struct inode *inode;
77907a90e97SDavid Howells 	int ret;
78007a90e97SDavid Howells 
78107a90e97SDavid Howells 	_enter(",%pd/,%s", dir, filename);
78207a90e97SDavid Howells 
78307a90e97SDavid Howells 	victim = cachefiles_lookup_for_cull(cache, dir, filename);
78407a90e97SDavid Howells 	if (IS_ERR(victim))
78507a90e97SDavid Howells 		return PTR_ERR(victim);
78607a90e97SDavid Howells 
78707a90e97SDavid Howells 	/* check to see if someone is using this object */
78807a90e97SDavid Howells 	inode = d_inode(victim);
78907a90e97SDavid Howells 	inode_lock(inode);
79007a90e97SDavid Howells 	if (inode->i_flags & S_KERNEL_FILE) {
79107a90e97SDavid Howells 		ret = -EBUSY;
79207a90e97SDavid Howells 	} else {
79307a90e97SDavid Howells 		/* Stop the cache from picking it back up */
79407a90e97SDavid Howells 		inode->i_flags |= S_KERNEL_FILE;
79507a90e97SDavid Howells 		ret = 0;
79607a90e97SDavid Howells 	}
79707a90e97SDavid Howells 	inode_unlock(inode);
79807a90e97SDavid Howells 	if (ret < 0)
79907a90e97SDavid Howells 		goto error_unlock;
80007a90e97SDavid Howells 
80107a90e97SDavid Howells 	ret = cachefiles_bury_object(cache, NULL, dir, victim,
80207a90e97SDavid Howells 				     FSCACHE_OBJECT_WAS_CULLED);
80307a90e97SDavid Howells 	if (ret < 0)
80407a90e97SDavid Howells 		goto error;
80507a90e97SDavid Howells 
80607a90e97SDavid Howells 	dput(victim);
80707a90e97SDavid Howells 	_leave(" = 0");
80807a90e97SDavid Howells 	return 0;
80907a90e97SDavid Howells 
81007a90e97SDavid Howells error_unlock:
81107a90e97SDavid Howells 	inode_unlock(d_inode(dir));
81207a90e97SDavid Howells error:
81307a90e97SDavid Howells 	dput(victim);
81407a90e97SDavid Howells 	if (ret == -ENOENT)
81507a90e97SDavid Howells 		return -ESTALE; /* Probably got retired by the netfs */
81607a90e97SDavid Howells 
81707a90e97SDavid Howells 	if (ret != -ENOMEM) {
81807a90e97SDavid Howells 		pr_err("Internal error: %d\n", ret);
81907a90e97SDavid Howells 		ret = -EIO;
82007a90e97SDavid Howells 	}
82107a90e97SDavid Howells 
82207a90e97SDavid Howells 	_leave(" = %d", ret);
82307a90e97SDavid Howells 	return ret;
82407a90e97SDavid Howells }
82507a90e97SDavid Howells 
82607a90e97SDavid Howells /*
82707a90e97SDavid Howells  * Find out if an object is in use or not
82807a90e97SDavid Howells  * - called only by cache manager daemon
82907a90e97SDavid Howells  * - returns -EBUSY or 0 to indicate whether an object is in use or not
83007a90e97SDavid Howells  */
83107a90e97SDavid Howells int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
83207a90e97SDavid Howells 			    char *filename)
83307a90e97SDavid Howells {
83407a90e97SDavid Howells 	struct dentry *victim;
83507a90e97SDavid Howells 	int ret = 0;
83607a90e97SDavid Howells 
83707a90e97SDavid Howells 	victim = cachefiles_lookup_for_cull(cache, dir, filename);
83807a90e97SDavid Howells 	if (IS_ERR(victim))
83907a90e97SDavid Howells 		return PTR_ERR(victim);
84007a90e97SDavid Howells 
84107a90e97SDavid Howells 	inode_unlock(d_inode(dir));
84207a90e97SDavid Howells 	dput(victim);
84307a90e97SDavid Howells 	return ret;
84407a90e97SDavid Howells }
845