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