Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /* CacheFiles path walking and related routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Written by David Howells (dhowells@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/fsnotify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #define CACHEFILES_KEYBUF_SIZE 512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  * dump debugging info about an object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) static noinline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) void __cachefiles_printk_object(struct cachefiles_object *object,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 				const char *prefix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 	struct fscache_cookie *cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 	const u8 *k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	unsigned loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	pr_err("%sobject: OBJ%x\n", prefix, object->fscache.debug_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	pr_err("%sobjstate=%s fl=%lx wbusy=%x ev=%lx[%lx]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	       prefix, object->fscache.state->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	       object->fscache.flags, work_busy(&object->fscache.work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 	       object->fscache.events, object->fscache.event_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	pr_err("%sops=%u inp=%u exc=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	       prefix, object->fscache.n_ops, object->fscache.n_in_progress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	       object->fscache.n_exclusive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	pr_err("%sparent=%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	       prefix, object->fscache.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	spin_lock(&object->fscache.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	cookie = object->fscache.cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	if (cookie) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 		pr_err("%scookie=%p [pr=%p nd=%p fl=%lx]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 		       prefix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 		       object->fscache.cookie,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 		       object->fscache.cookie->parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 		       object->fscache.cookie->netfs_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 		       object->fscache.cookie->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 		pr_err("%skey=[%u] '", prefix, cookie->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 		k = (cookie->key_len <= sizeof(cookie->inline_key)) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 			cookie->inline_key : cookie->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 		for (loop = 0; loop < cookie->key_len; loop++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 			pr_cont("%02x", k[loop]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 		pr_cont("'\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 		pr_err("%scookie=NULL\n", prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	spin_unlock(&object->fscache.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  * dump debugging info about a pair of objects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) static noinline void cachefiles_printk_object(struct cachefiles_object *object,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 					      struct cachefiles_object *xobject)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	if (object)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 		__cachefiles_printk_object(object, "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	if (xobject)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		__cachefiles_printk_object(xobject, "x");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  * mark the owner of a dentry, if there is one, to indicate that that dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  * has been preemptively deleted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  * - the caller must hold the i_mutex on the dentry's parent as required to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  *   call vfs_unlink(), vfs_rmdir() or vfs_rename()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) static void cachefiles_mark_object_buried(struct cachefiles_cache *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 					  struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 					  enum fscache_why_object_killed why)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	struct cachefiles_object *object;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	struct rb_node *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	_enter(",'%pd'", dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	write_lock(&cache->active_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	p = cache->active_nodes.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	while (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		object = rb_entry(p, struct cachefiles_object, active_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		if (object->dentry > dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 			p = p->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		else if (object->dentry < dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 			p = p->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 			goto found_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	write_unlock(&cache->active_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	trace_cachefiles_mark_buried(NULL, dentry, why);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	_leave(" [no owner]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	/* found the dentry for  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) found_dentry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	kdebug("preemptive burial: OBJ%x [%s] %p",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	       object->fscache.debug_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	       object->fscache.state->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	       dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	trace_cachefiles_mark_buried(object, dentry, why);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	if (fscache_object_is_live(&object->fscache)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		pr_err("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 		pr_err("Error: Can't preemptively bury live object\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		cachefiles_printk_object(object, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 		if (why != FSCACHE_OBJECT_IS_STALE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 			fscache_object_mark_killed(&object->fscache, why);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	write_unlock(&cache->active_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	_leave(" [owner marked]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134)  * record the fact that an object is now active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) static int cachefiles_mark_object_active(struct cachefiles_cache *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 					 struct cachefiles_object *object)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	struct cachefiles_object *xobject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	struct rb_node **_p, *_parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	_enter(",%p", object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) try_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	write_lock(&cache->active_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	dentry = object->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	trace_cachefiles_mark_active(object, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	if (test_and_set_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 		pr_err("Error: Object already active\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		cachefiles_printk_object(object, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	_p = &cache->active_nodes.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	while (*_p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		_parent = *_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		xobject = rb_entry(_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 				   struct cachefiles_object, active_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		ASSERT(xobject != object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		if (xobject->dentry > dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 			_p = &(*_p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		else if (xobject->dentry < dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 			_p = &(*_p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 			goto wait_for_old_object;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	rb_link_node(&object->active_node, _parent, _p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	rb_insert_color(&object->active_node, &cache->active_nodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	write_unlock(&cache->active_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	_leave(" = 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	/* an old object from a previous incarnation is hogging the slot - we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	 * need to wait for it to be destroyed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) wait_for_old_object:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	trace_cachefiles_wait_active(object, dentry, xobject);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	if (fscache_object_is_live(&xobject->fscache)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		pr_err("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		pr_err("Error: Unexpected object collision\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		cachefiles_printk_object(object, xobject);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	atomic_inc(&xobject->usage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	write_unlock(&cache->active_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	if (test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		wait_queue_head_t *wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		signed long timeout = 60 * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		wait_queue_entry_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		bool requeue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		/* if the object we're waiting for is queued for processing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		 * then just put ourselves on the queue behind it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		if (work_pending(&xobject->fscache.work)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 			_debug("queue OBJ%x behind OBJ%x immediately",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 			       object->fscache.debug_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 			       xobject->fscache.debug_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 			goto requeue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		/* otherwise we sleep until either the object we're waiting for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		 * is done, or the fscache_object is congested */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		wq = bit_waitqueue(&xobject->flags, CACHEFILES_OBJECT_ACTIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		init_wait(&wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		requeue = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 			prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 			if (!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 			requeue = fscache_object_sleep_till_congested(&timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		} while (timeout > 0 && !requeue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		finish_wait(wq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		if (requeue &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		    test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 			_debug("queue OBJ%x behind OBJ%x after wait",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 			       object->fscache.debug_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 			       xobject->fscache.debug_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 			goto requeue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		if (timeout <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 			pr_err("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 			pr_err("Error: Overlong wait for old active object to go away\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 			cachefiles_printk_object(object, xobject);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 			goto requeue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	cache->cache.ops->put_object(&xobject->fscache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		(enum fscache_obj_ref_trace)cachefiles_obj_put_wait_retry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	goto try_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) requeue:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	cache->cache.ops->put_object(&xobject->fscache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		(enum fscache_obj_ref_trace)cachefiles_obj_put_wait_timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	_leave(" = -ETIMEDOUT");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254)  * Mark an object as being inactive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) void cachefiles_mark_object_inactive(struct cachefiles_cache *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 				     struct cachefiles_object *object,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 				     blkcnt_t i_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	struct dentry *dentry = object->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	struct inode *inode = d_backing_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	trace_cachefiles_mark_inactive(object, dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	write_lock(&cache->active_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	rb_erase(&object->active_node, &cache->active_nodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	write_unlock(&cache->active_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	/* This object can now be culled, so we need to let the daemon know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	 * that there is something it can remove if it needs to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	atomic_long_add(i_blocks, &cache->b_released);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	if (atomic_inc_return(&cache->f_released))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		cachefiles_state_changed(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281)  * delete an object representation from the cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282)  * - file backed objects are unlinked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283)  * - directory backed objects are stuffed into the graveyard for userspace to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284)  *   delete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285)  * - unlocks the directory mutex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) static int cachefiles_bury_object(struct cachefiles_cache *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 				  struct cachefiles_object *object,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 				  struct dentry *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 				  struct dentry *rep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 				  bool preemptive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 				  enum fscache_why_object_killed why)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	struct dentry *grave, *trap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	struct path path, path_to_graveyard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	char nbuffer[8 + 8 + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	_enter(",'%pd','%pd'", dir, rep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	_debug("remove %p from %p", rep, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	/* non-directories can just be unlinked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	if (!d_is_dir(rep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		_debug("unlink stale object");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		path.mnt = cache->mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		path.dentry = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		ret = security_path_unlink(&path, rep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 			cachefiles_io_error(cache, "Unlink security error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 			trace_cachefiles_unlink(object, rep, why);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 			ret = vfs_unlink(d_inode(dir), rep, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 			if (preemptive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 				cachefiles_mark_object_buried(cache, rep, why);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 		if (ret == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 			cachefiles_io_error(cache, "Unlink failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		_leave(" = %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	/* directories have to be moved to the graveyard */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	_debug("move stale object to graveyard");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) try_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	/* first step is to make up a grave dentry in the graveyard */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	sprintf(nbuffer, "%08x%08x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		(uint32_t) ktime_get_real_seconds(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		(uint32_t) atomic_inc_return(&cache->gravecounter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	/* do the multiway lock magic */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	trap = lock_rename(cache->graveyard, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	/* do some checks before getting the grave dentry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	if (rep->d_parent != dir || IS_DEADDIR(d_inode(rep))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		/* the entry was probably culled when we dropped the parent dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		 * lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		unlock_rename(cache->graveyard, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		_leave(" = 0 [culled?]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	if (!d_can_lookup(cache->graveyard)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		unlock_rename(cache->graveyard, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		cachefiles_io_error(cache, "Graveyard no longer a directory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	if (trap == rep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		unlock_rename(cache->graveyard, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		cachefiles_io_error(cache, "May not make directory loop");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	if (d_mountpoint(rep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		unlock_rename(cache->graveyard, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		cachefiles_io_error(cache, "Mountpoint in cache");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	if (IS_ERR(grave)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		unlock_rename(cache->graveyard, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		if (PTR_ERR(grave) == -ENOMEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 			_leave(" = -ENOMEM");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		cachefiles_io_error(cache, "Lookup error %ld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 				    PTR_ERR(grave));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	if (d_is_positive(grave)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		unlock_rename(cache->graveyard, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		dput(grave);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		grave = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		goto try_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	if (d_mountpoint(grave)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		unlock_rename(cache->graveyard, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		dput(grave);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		cachefiles_io_error(cache, "Mountpoint in graveyard");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	/* target should not be an ancestor of source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	if (trap == grave) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		unlock_rename(cache->graveyard, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		dput(grave);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		cachefiles_io_error(cache, "May not make directory loop");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	/* attempt the rename */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	path.mnt = cache->mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	path.dentry = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	path_to_graveyard.mnt = cache->mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	path_to_graveyard.dentry = cache->graveyard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	ret = security_path_rename(&path, rep, &path_to_graveyard, grave, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		cachefiles_io_error(cache, "Rename security error %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		trace_cachefiles_rename(object, rep, grave, why);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		ret = vfs_rename(d_inode(dir), rep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 				 d_inode(cache->graveyard), grave, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		if (ret != 0 && ret != -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 			cachefiles_io_error(cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 					    "Rename failed with error %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		if (preemptive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 			cachefiles_mark_object_buried(cache, rep, why);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	unlock_rename(cache->graveyard, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	dput(grave);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	_leave(" = 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433)  * delete an object representation from the cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) int cachefiles_delete_object(struct cachefiles_cache *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 			     struct cachefiles_object *object)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	struct dentry *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	_enter(",OBJ%x{%p}", object->fscache.debug_id, object->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	ASSERT(object->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	ASSERT(d_backing_inode(object->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	ASSERT(object->dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	dir = dget_parent(object->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	if (test_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->fscache.flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		/* object allocation for the same key preemptively deleted this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		 * object's file so that it could create its own file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		_debug("object preemptively buried");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		/* we need to check that our parent is _still_ our parent - it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		 * may have been renamed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		if (dir == object->dentry->d_parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 			ret = cachefiles_bury_object(cache, object, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 						     object->dentry, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 						     FSCACHE_OBJECT_WAS_RETIRED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 			/* it got moved, presumably by cachefilesd culling it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 			 * so it's no longer in the key path and we can ignore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 			 * it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 			inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	dput(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	_leave(" = %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479)  * walk from the parent object to the child object through the backing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480)  * filesystem, creating directories as we go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) int cachefiles_walk_to_object(struct cachefiles_object *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 			      struct cachefiles_object *object,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 			      const char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 			      struct cachefiles_xattr *auxdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	struct cachefiles_cache *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	struct dentry *dir, *next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	struct path path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	unsigned long start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	int ret, nlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	_enter("OBJ%x{%p},OBJ%x,%s,",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	       parent->fscache.debug_id, parent->dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	       object->fscache.debug_id, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	cache = container_of(parent->fscache.cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			     struct cachefiles_cache, cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	path.mnt = cache->mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	ASSERT(parent->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	ASSERT(d_backing_inode(parent->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	if (!(d_is_dir(parent->dentry))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		// TODO: convert file to dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		_leave("looking up in none directory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	dir = dget(parent->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) advance:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	/* attempt to transit the first directory component */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	name = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	nlen = strlen(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	/* key ends in a double NUL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	key = key + nlen + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	if (!*key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) lookup_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	/* search the current directory for the element name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	_debug("lookup '%s'", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	start = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	next = lookup_one_len(name, dir, nlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	cachefiles_hist(cachefiles_lookup_histogram, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	if (IS_ERR(next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		trace_cachefiles_lookup(object, next, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		goto lookup_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	inode = d_backing_inode(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	trace_cachefiles_lookup(object, next, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	_debug("next -> %p %s", next, inode ? "positive" : "negative");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	if (!key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		object->new = !inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	/* if this element of the path doesn't exist, then the lookup phase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	 * failed, and we can release any readers in the certain knowledge that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	 * there's nothing for them to actually read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	if (d_is_negative(next))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		fscache_object_lookup_negative(&object->fscache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	/* we need to create the object if it's negative */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		/* index objects and intervening tree levels must be subdirs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		if (d_is_negative(next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			ret = cachefiles_has_space(cache, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 				goto no_space_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 			path.dentry = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 			ret = security_path_mkdir(&path, next, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 				goto create_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 			start = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 			ret = vfs_mkdir(d_inode(dir), next, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 			cachefiles_hist(cachefiles_mkdir_histogram, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 			if (!key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 				trace_cachefiles_mkdir(object, next, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 				goto create_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 			if (unlikely(d_unhashed(next))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 				dput(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 				inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 				goto lookup_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 			ASSERT(d_backing_inode(next));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 			_debug("mkdir -> %p{%p{ino=%lu}}",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 			       next, d_backing_inode(next), d_backing_inode(next)->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		} else if (!d_can_lookup(next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 			pr_err("inode %lu is not a directory\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 			       d_backing_inode(next)->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 			ret = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		/* non-index objects start out life as files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		if (d_is_negative(next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 			ret = cachefiles_has_space(cache, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 				goto no_space_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 			path.dentry = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 			ret = security_path_mknod(&path, next, S_IFREG, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 				goto create_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 			start = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 			ret = vfs_create(d_inode(dir), next, S_IFREG, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 			cachefiles_hist(cachefiles_create_histogram, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 			trace_cachefiles_create(object, next, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 				goto create_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 			ASSERT(d_backing_inode(next));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 			_debug("create -> %p{%p{ino=%lu}}",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 			       next, d_backing_inode(next), d_backing_inode(next)->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		} else if (!d_can_lookup(next) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 			   !d_is_reg(next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			   ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 			pr_err("inode %lu is not a file or directory\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 			       d_backing_inode(next)->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 			ret = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	/* process the next component */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	if (key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		_debug("advance");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		dput(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		dir = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		goto advance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	/* we've found the object we were looking for */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	object->dentry = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	/* if we've found that the terminal object exists, then we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	 * check its attributes and delete it if it's out of date */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	if (!object->new) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		_debug("validate '%pd'", next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		ret = cachefiles_check_object_xattr(object, auxdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		if (ret == -ESTALE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 			/* delete the object (the deleter drops the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 			 * mutex) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 			object->dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 			ret = cachefiles_bury_object(cache, object, dir, next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 						     true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 						     FSCACHE_OBJECT_IS_STALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			dput(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 			next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 				goto delete_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 			_debug("redo lookup");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 			fscache_object_retrying_stale(&object->fscache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 			goto lookup_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	/* note that we're now using this object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	ret = cachefiles_mark_object_active(cache, object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	dput(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	if (ret == -ETIMEDOUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		goto mark_active_timed_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	_debug("=== OBTAINED_OBJECT ===");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	if (object->new) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		/* attach data to a newly constructed terminal object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		ret = cachefiles_set_object_xattr(object, auxdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 			goto check_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		/* always update the atime on an object we've just looked up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		 * (this is used to keep track of culling, and atimes are only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		 * updated by read, write and readdir but not lookup or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		 * open) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		path.dentry = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		touch_atime(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	/* open a file interface onto a data file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		if (d_is_reg(object->dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 			const struct address_space_operations *aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 			ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 			aops = d_backing_inode(object->dentry)->i_mapping->a_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 			if (!aops->bmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 				goto check_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 			if (object->dentry->d_sb->s_blocksize > PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 				goto check_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 			object->backer = object->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 			BUG(); // TODO: open file in data-class subdir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	object->new = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	fscache_obtained_object(&object->fscache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	_leave(" = 0 [%lu]", d_backing_inode(object->dentry)->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) no_space_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	fscache_object_mark_killed(&object->fscache, FSCACHE_OBJECT_NO_SPACE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) create_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	_debug("create error %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	if (ret == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		cachefiles_io_error(cache, "Create/mkdir failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) mark_active_timed_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	_debug("mark active timed out");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	goto release_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) check_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	_debug("check error %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	cachefiles_mark_object_inactive(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		cache, object, d_backing_inode(object->dentry)->i_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) release_dentry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	dput(object->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	object->dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) delete_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	_debug("delete error %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	goto error_out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) lookup_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	_debug("lookup error %ld", PTR_ERR(next));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	ret = PTR_ERR(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	if (ret == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		cachefiles_io_error(cache, "Lookup failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	dput(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) error_out2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	dput(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) error_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	_leave(" = error %d", -ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752)  * get a subdirectory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 					struct dentry *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 					const char *dirname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	struct dentry *subdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	unsigned long start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	struct path path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	_enter(",,%s", dirname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	/* search the current directory for the element name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	inode_lock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	start = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	subdir = lookup_one_len(dirname, dir, strlen(dirname));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	cachefiles_hist(cachefiles_lookup_histogram, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	if (IS_ERR(subdir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		if (PTR_ERR(subdir) == -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 			goto nomem_d_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		goto lookup_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	_debug("subdir -> %p %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	       subdir, d_backing_inode(subdir) ? "positive" : "negative");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	/* we need to create the subdir if it doesn't exist yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	if (d_is_negative(subdir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		ret = cachefiles_has_space(cache, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 			goto mkdir_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		_debug("attempt mkdir");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		path.mnt = cache->mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		path.dentry = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		ret = security_path_mkdir(&path, subdir, 0700);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 			goto mkdir_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		ret = vfs_mkdir(d_inode(dir), subdir, 0700);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 			goto mkdir_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		if (unlikely(d_unhashed(subdir))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 			dput(subdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		ASSERT(d_backing_inode(subdir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		_debug("mkdir -> %p{%p{ino=%lu}}",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		       subdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		       d_backing_inode(subdir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		       d_backing_inode(subdir)->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	/* we need to make sure the subdir is a directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	ASSERT(d_backing_inode(subdir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	if (!d_can_lookup(subdir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		pr_err("%s is not a directory\n", dirname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		goto check_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	if (!(d_backing_inode(subdir)->i_opflags & IOP_XATTR) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	    !d_backing_inode(subdir)->i_op->lookup ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	    !d_backing_inode(subdir)->i_op->mkdir ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	    !d_backing_inode(subdir)->i_op->create ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	    !d_backing_inode(subdir)->i_op->rename ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	    !d_backing_inode(subdir)->i_op->rmdir ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	    !d_backing_inode(subdir)->i_op->unlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		goto check_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	_leave(" = [%lu]", d_backing_inode(subdir)->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	return subdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) check_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	dput(subdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	_leave(" = %d [check]", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) mkdir_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	dput(subdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	pr_err("mkdir %s failed with error %d\n", dirname, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) lookup_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	ret = PTR_ERR(subdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	pr_err("Lookup %s failed with error %d\n", dirname, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) nomem_d_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	_leave(" = -ENOMEM");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858)  * find out if an object is in use or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859)  * - if finds object and it's not in use:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860)  *   - returns a pointer to the object and a reference on it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861)  *   - returns with the directory locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 					      struct dentry *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 					      char *filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	struct cachefiles_object *object;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	struct rb_node *_n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	struct dentry *victim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	unsigned long start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	//_enter(",%pd/,%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	//       dir, filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	/* look up the victim */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	start = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	victim = lookup_one_len(filename, dir, strlen(filename));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	cachefiles_hist(cachefiles_lookup_histogram, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	if (IS_ERR(victim))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		goto lookup_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	//_debug("victim -> %p %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	//       victim, d_backing_inode(victim) ? "positive" : "negative");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	/* if the object is no longer there then we probably retired the object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	 * at the netfs's request whilst the cull was in progress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	if (d_is_negative(victim)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		dput(victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		_leave(" = -ENOENT [absent]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	/* check to see if we're using this object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	read_lock(&cache->active_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	_n = cache->active_nodes.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	while (_n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		object = rb_entry(_n, struct cachefiles_object, active_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		if (object->dentry > victim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 			_n = _n->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		else if (object->dentry < victim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 			_n = _n->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 			goto object_in_use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	read_unlock(&cache->active_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	//_leave(" = %p", victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	return victim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) object_in_use:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	read_unlock(&cache->active_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	dput(victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	//_leave(" = -EBUSY [in use]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	return ERR_PTR(-EBUSY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) lookup_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	ret = PTR_ERR(victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	if (ret == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		/* file or dir now absent - probably retired by netfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		_leave(" = -ESTALE [absent]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		return ERR_PTR(-ESTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	if (ret == -EIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		cachefiles_io_error(cache, "Lookup failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	} else if (ret != -ENOMEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		pr_err("Internal error: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	_leave(" = %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947)  * cull an object if it's not in use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948)  * - called only by cache manager daemon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		    char *filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	struct dentry *victim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	_enter(",%pd/,%s", dir, filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	victim = cachefiles_check_active(cache, dir, filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	if (IS_ERR(victim))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		return PTR_ERR(victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	_debug("victim -> %p %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	       victim, d_backing_inode(victim) ? "positive" : "negative");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	/* okay... the victim is not being used so we can cull it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	 * - start by marking it as stale
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	_debug("victim is cullable");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	ret = cachefiles_remove_object_xattr(cache, victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		goto error_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	/*  actually remove the victim (drops the dir mutex) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	_debug("bury");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	ret = cachefiles_bury_object(cache, NULL, dir, victim, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 				     FSCACHE_OBJECT_WAS_CULLED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	dput(victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	_leave(" = 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) error_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	dput(victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	if (ret == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		/* file or dir now absent - probably retired by netfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		_leave(" = -ESTALE [absent]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		return -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	if (ret != -ENOMEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		pr_err("Internal error: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	_leave(" = %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)  * find out if an object is in use or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)  * - called only by cache manager daemon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)  * - returns -EBUSY or 0 to indicate whether an object is in use or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 			    char *filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	struct dentry *victim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	//_enter(",%pd/,%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	//       dir, filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	victim = cachefiles_check_active(cache, dir, filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	if (IS_ERR(victim))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		return PTR_ERR(victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	dput(victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	//_leave(" = 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) }