xref: /openbmc/linux/fs/cachefiles/namei.c (revision dfc66bef)
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=%x\n",
43 	       prefix, object->fscache.parent ? object->fscache.parent->debug_id : 0);
44 
45 	spin_lock(&object->fscache.lock);
46 	cookie = object->fscache.cookie;
47 	if (cookie) {
48 		pr_err("%scookie=%x [pr=%x nd=%p fl=%lx]\n",
49 		       prefix,
50 		       cookie->debug_id,
51 		       cookie->parent ? cookie->parent->debug_id : 0,
52 		       cookie->netfs_data,
53 		       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] %pd",
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(",%x", object->fscache.debug_id);
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 	/* non-directories can just be unlinked */
302 	if (!d_is_dir(rep)) {
303 		_debug("unlink stale object");
304 
305 		path.mnt = cache->mnt;
306 		path.dentry = dir;
307 		ret = security_path_unlink(&path, rep);
308 		if (ret < 0) {
309 			cachefiles_io_error(cache, "Unlink security error");
310 		} else {
311 			trace_cachefiles_unlink(object, rep, why);
312 			ret = vfs_unlink(&init_user_ns, d_inode(dir), rep,
313 					 NULL);
314 
315 			if (preemptive)
316 				cachefiles_mark_object_buried(cache, rep, why);
317 		}
318 
319 		inode_unlock(d_inode(dir));
320 
321 		if (ret == -EIO)
322 			cachefiles_io_error(cache, "Unlink failed");
323 
324 		_leave(" = %d", ret);
325 		return ret;
326 	}
327 
328 	/* directories have to be moved to the graveyard */
329 	_debug("move stale object to graveyard");
330 	inode_unlock(d_inode(dir));
331 
332 try_again:
333 	/* first step is to make up a grave dentry in the graveyard */
334 	sprintf(nbuffer, "%08x%08x",
335 		(uint32_t) ktime_get_real_seconds(),
336 		(uint32_t) atomic_inc_return(&cache->gravecounter));
337 
338 	/* do the multiway lock magic */
339 	trap = lock_rename(cache->graveyard, dir);
340 
341 	/* do some checks before getting the grave dentry */
342 	if (rep->d_parent != dir || IS_DEADDIR(d_inode(rep))) {
343 		/* the entry was probably culled when we dropped the parent dir
344 		 * lock */
345 		unlock_rename(cache->graveyard, dir);
346 		_leave(" = 0 [culled?]");
347 		return 0;
348 	}
349 
350 	if (!d_can_lookup(cache->graveyard)) {
351 		unlock_rename(cache->graveyard, dir);
352 		cachefiles_io_error(cache, "Graveyard no longer a directory");
353 		return -EIO;
354 	}
355 
356 	if (trap == rep) {
357 		unlock_rename(cache->graveyard, dir);
358 		cachefiles_io_error(cache, "May not make directory loop");
359 		return -EIO;
360 	}
361 
362 	if (d_mountpoint(rep)) {
363 		unlock_rename(cache->graveyard, dir);
364 		cachefiles_io_error(cache, "Mountpoint in cache");
365 		return -EIO;
366 	}
367 
368 	grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
369 	if (IS_ERR(grave)) {
370 		unlock_rename(cache->graveyard, dir);
371 
372 		if (PTR_ERR(grave) == -ENOMEM) {
373 			_leave(" = -ENOMEM");
374 			return -ENOMEM;
375 		}
376 
377 		cachefiles_io_error(cache, "Lookup error %ld",
378 				    PTR_ERR(grave));
379 		return -EIO;
380 	}
381 
382 	if (d_is_positive(grave)) {
383 		unlock_rename(cache->graveyard, dir);
384 		dput(grave);
385 		grave = NULL;
386 		cond_resched();
387 		goto try_again;
388 	}
389 
390 	if (d_mountpoint(grave)) {
391 		unlock_rename(cache->graveyard, dir);
392 		dput(grave);
393 		cachefiles_io_error(cache, "Mountpoint in graveyard");
394 		return -EIO;
395 	}
396 
397 	/* target should not be an ancestor of source */
398 	if (trap == grave) {
399 		unlock_rename(cache->graveyard, dir);
400 		dput(grave);
401 		cachefiles_io_error(cache, "May not make directory loop");
402 		return -EIO;
403 	}
404 
405 	/* attempt the rename */
406 	path.mnt = cache->mnt;
407 	path.dentry = dir;
408 	path_to_graveyard.mnt = cache->mnt;
409 	path_to_graveyard.dentry = cache->graveyard;
410 	ret = security_path_rename(&path, rep, &path_to_graveyard, grave, 0);
411 	if (ret < 0) {
412 		cachefiles_io_error(cache, "Rename security error %d", ret);
413 	} else {
414 		struct renamedata rd = {
415 			.old_mnt_userns	= &init_user_ns,
416 			.old_dir	= d_inode(dir),
417 			.old_dentry	= rep,
418 			.new_mnt_userns	= &init_user_ns,
419 			.new_dir	= d_inode(cache->graveyard),
420 			.new_dentry	= grave,
421 		};
422 		trace_cachefiles_rename(object, rep, grave, why);
423 		ret = vfs_rename(&rd);
424 		if (ret != 0 && ret != -ENOMEM)
425 			cachefiles_io_error(cache,
426 					    "Rename failed with error %d", ret);
427 
428 		if (preemptive)
429 			cachefiles_mark_object_buried(cache, rep, why);
430 	}
431 
432 	unlock_rename(cache->graveyard, dir);
433 	dput(grave);
434 	_leave(" = 0");
435 	return 0;
436 }
437 
438 /*
439  * delete an object representation from the cache
440  */
441 int cachefiles_delete_object(struct cachefiles_cache *cache,
442 			     struct cachefiles_object *object)
443 {
444 	struct dentry *dir;
445 	int ret;
446 
447 	_enter(",OBJ%x{%pd}", object->fscache.debug_id, object->dentry);
448 
449 	ASSERT(object->dentry);
450 	ASSERT(d_backing_inode(object->dentry));
451 	ASSERT(object->dentry->d_parent);
452 
453 	dir = dget_parent(object->dentry);
454 
455 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
456 
457 	if (test_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->fscache.flags)) {
458 		/* object allocation for the same key preemptively deleted this
459 		 * object's file so that it could create its own file */
460 		_debug("object preemptively buried");
461 		inode_unlock(d_inode(dir));
462 		ret = 0;
463 	} else {
464 		/* we need to check that our parent is _still_ our parent - it
465 		 * may have been renamed */
466 		if (dir == object->dentry->d_parent) {
467 			ret = cachefiles_bury_object(cache, object, dir,
468 						     object->dentry, false,
469 						     FSCACHE_OBJECT_WAS_RETIRED);
470 		} else {
471 			/* it got moved, presumably by cachefilesd culling it,
472 			 * so it's no longer in the key path and we can ignore
473 			 * it */
474 			inode_unlock(d_inode(dir));
475 			ret = 0;
476 		}
477 	}
478 
479 	dput(dir);
480 	_leave(" = %d", ret);
481 	return ret;
482 }
483 
484 /*
485  * walk from the parent object to the child object through the backing
486  * filesystem, creating directories as we go
487  */
488 int cachefiles_walk_to_object(struct cachefiles_object *parent,
489 			      struct cachefiles_object *object,
490 			      const char *key,
491 			      struct cachefiles_xattr *auxdata)
492 {
493 	struct cachefiles_cache *cache;
494 	struct dentry *dir, *next = NULL;
495 	struct inode *inode;
496 	struct path path;
497 	const char *name;
498 	int ret, nlen;
499 
500 	_enter("OBJ%x{%pd},OBJ%x,%s,",
501 	       parent->fscache.debug_id, parent->dentry,
502 	       object->fscache.debug_id, key);
503 
504 	cache = container_of(parent->fscache.cache,
505 			     struct cachefiles_cache, cache);
506 	path.mnt = cache->mnt;
507 
508 	ASSERT(parent->dentry);
509 	ASSERT(d_backing_inode(parent->dentry));
510 
511 	if (!(d_is_dir(parent->dentry))) {
512 		// TODO: convert file to dir
513 		_leave("looking up in none directory");
514 		return -ENOBUFS;
515 	}
516 
517 	dir = dget(parent->dentry);
518 
519 advance:
520 	/* attempt to transit the first directory component */
521 	name = key;
522 	nlen = strlen(key);
523 
524 	/* key ends in a double NUL */
525 	key = key + nlen + 1;
526 	if (!*key)
527 		key = NULL;
528 
529 lookup_again:
530 	/* search the current directory for the element name */
531 	_debug("lookup '%s'", name);
532 
533 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
534 
535 	next = lookup_one_len(name, dir, nlen);
536 	if (IS_ERR(next)) {
537 		trace_cachefiles_lookup(object, next, NULL);
538 		goto lookup_error;
539 	}
540 
541 	inode = d_backing_inode(next);
542 	trace_cachefiles_lookup(object, next, inode);
543 	_debug("next -> %pd %s", next, inode ? "positive" : "negative");
544 
545 	if (!key)
546 		object->new = !inode;
547 
548 	/* if this element of the path doesn't exist, then the lookup phase
549 	 * failed, and we can release any readers in the certain knowledge that
550 	 * there's nothing for them to actually read */
551 	if (d_is_negative(next))
552 		fscache_object_lookup_negative(&object->fscache);
553 
554 	/* we need to create the object if it's negative */
555 	if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
556 		/* index objects and intervening tree levels must be subdirs */
557 		if (d_is_negative(next)) {
558 			ret = cachefiles_has_space(cache, 1, 0);
559 			if (ret < 0)
560 				goto no_space_error;
561 
562 			path.dentry = dir;
563 			ret = security_path_mkdir(&path, next, 0);
564 			if (ret < 0)
565 				goto create_error;
566 			ret = vfs_mkdir(&init_user_ns, d_inode(dir), next, 0);
567 			if (!key)
568 				trace_cachefiles_mkdir(object, next, ret);
569 			if (ret < 0)
570 				goto create_error;
571 
572 			if (unlikely(d_unhashed(next))) {
573 				dput(next);
574 				inode_unlock(d_inode(dir));
575 				goto lookup_again;
576 			}
577 			ASSERT(d_backing_inode(next));
578 
579 			_debug("mkdir -> %pd{ino=%lu}",
580 			       next, d_backing_inode(next)->i_ino);
581 
582 		} else if (!d_can_lookup(next)) {
583 			pr_err("inode %lu is not a directory\n",
584 			       d_backing_inode(next)->i_ino);
585 			ret = -ENOBUFS;
586 			goto error;
587 		}
588 
589 	} else {
590 		/* non-index objects start out life as files */
591 		if (d_is_negative(next)) {
592 			ret = cachefiles_has_space(cache, 1, 0);
593 			if (ret < 0)
594 				goto no_space_error;
595 
596 			path.dentry = dir;
597 			ret = security_path_mknod(&path, next, S_IFREG, 0);
598 			if (ret < 0)
599 				goto create_error;
600 			ret = vfs_create(&init_user_ns, d_inode(dir), next,
601 					 S_IFREG, true);
602 			trace_cachefiles_create(object, next, ret);
603 			if (ret < 0)
604 				goto create_error;
605 
606 			ASSERT(d_backing_inode(next));
607 
608 			_debug("create -> %pd{ino=%lu}",
609 			       next, d_backing_inode(next)->i_ino);
610 
611 		} else if (!d_can_lookup(next) &&
612 			   !d_is_reg(next)
613 			   ) {
614 			pr_err("inode %lu is not a file or directory\n",
615 			       d_backing_inode(next)->i_ino);
616 			ret = -ENOBUFS;
617 			goto error;
618 		}
619 	}
620 
621 	/* process the next component */
622 	if (key) {
623 		_debug("advance");
624 		inode_unlock(d_inode(dir));
625 		dput(dir);
626 		dir = next;
627 		next = NULL;
628 		goto advance;
629 	}
630 
631 	/* we've found the object we were looking for */
632 	object->dentry = next;
633 
634 	/* if we've found that the terminal object exists, then we need to
635 	 * check its attributes and delete it if it's out of date */
636 	if (!object->new) {
637 		_debug("validate '%pd'", next);
638 
639 		ret = cachefiles_check_object_xattr(object, auxdata);
640 		if (ret == -ESTALE) {
641 			/* delete the object (the deleter drops the directory
642 			 * mutex) */
643 			object->dentry = NULL;
644 
645 			ret = cachefiles_bury_object(cache, object, dir, next,
646 						     true,
647 						     FSCACHE_OBJECT_IS_STALE);
648 			dput(next);
649 			next = NULL;
650 
651 			if (ret < 0)
652 				goto delete_error;
653 
654 			_debug("redo lookup");
655 			fscache_object_retrying_stale(&object->fscache);
656 			goto lookup_again;
657 		}
658 	}
659 
660 	/* note that we're now using this object */
661 	ret = cachefiles_mark_object_active(cache, object);
662 
663 	inode_unlock(d_inode(dir));
664 	dput(dir);
665 	dir = NULL;
666 
667 	if (ret == -ETIMEDOUT)
668 		goto mark_active_timed_out;
669 
670 	_debug("=== OBTAINED_OBJECT ===");
671 
672 	if (object->new) {
673 		/* attach data to a newly constructed terminal object */
674 		ret = cachefiles_set_object_xattr(object, auxdata);
675 		if (ret < 0)
676 			goto check_error;
677 	} else {
678 		/* always update the atime on an object we've just looked up
679 		 * (this is used to keep track of culling, and atimes are only
680 		 * updated by read, write and readdir but not lookup or
681 		 * open) */
682 		path.dentry = next;
683 		touch_atime(&path);
684 	}
685 
686 	/* open a file interface onto a data file */
687 	if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
688 		if (d_is_reg(object->dentry)) {
689 			const struct address_space_operations *aops;
690 
691 			ret = -EPERM;
692 			aops = d_backing_inode(object->dentry)->i_mapping->a_ops;
693 			if (!aops->bmap)
694 				goto check_error;
695 			if (object->dentry->d_sb->s_blocksize > PAGE_SIZE)
696 				goto check_error;
697 
698 			object->backer = object->dentry;
699 		} else {
700 			BUG(); // TODO: open file in data-class subdir
701 		}
702 	}
703 
704 	object->new = 0;
705 	fscache_obtained_object(&object->fscache);
706 
707 	_leave(" = 0 [%lu]", d_backing_inode(object->dentry)->i_ino);
708 	return 0;
709 
710 no_space_error:
711 	fscache_object_mark_killed(&object->fscache, FSCACHE_OBJECT_NO_SPACE);
712 create_error:
713 	_debug("create error %d", ret);
714 	if (ret == -EIO)
715 		cachefiles_io_error(cache, "Create/mkdir failed");
716 	goto error;
717 
718 mark_active_timed_out:
719 	_debug("mark active timed out");
720 	goto release_dentry;
721 
722 check_error:
723 	_debug("check error %d", ret);
724 	cachefiles_mark_object_inactive(
725 		cache, object, d_backing_inode(object->dentry)->i_blocks);
726 release_dentry:
727 	dput(object->dentry);
728 	object->dentry = NULL;
729 	goto error_out;
730 
731 delete_error:
732 	_debug("delete error %d", ret);
733 	goto error_out2;
734 
735 lookup_error:
736 	_debug("lookup error %ld", PTR_ERR(next));
737 	ret = PTR_ERR(next);
738 	if (ret == -EIO)
739 		cachefiles_io_error(cache, "Lookup failed");
740 	next = NULL;
741 error:
742 	inode_unlock(d_inode(dir));
743 	dput(next);
744 error_out2:
745 	dput(dir);
746 error_out:
747 	_leave(" = error %d", -ret);
748 	return ret;
749 }
750 
751 /*
752  * get a subdirectory
753  */
754 struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
755 					struct dentry *dir,
756 					const char *dirname)
757 {
758 	struct dentry *subdir;
759 	struct path path;
760 	int ret;
761 
762 	_enter(",,%s", dirname);
763 
764 	/* search the current directory for the element name */
765 	inode_lock(d_inode(dir));
766 
767 retry:
768 	subdir = lookup_one_len(dirname, dir, strlen(dirname));
769 	if (IS_ERR(subdir)) {
770 		if (PTR_ERR(subdir) == -ENOMEM)
771 			goto nomem_d_alloc;
772 		goto lookup_error;
773 	}
774 
775 	_debug("subdir -> %pd %s",
776 	       subdir, d_backing_inode(subdir) ? "positive" : "negative");
777 
778 	/* we need to create the subdir if it doesn't exist yet */
779 	if (d_is_negative(subdir)) {
780 		ret = cachefiles_has_space(cache, 1, 0);
781 		if (ret < 0)
782 			goto mkdir_error;
783 
784 		_debug("attempt mkdir");
785 
786 		path.mnt = cache->mnt;
787 		path.dentry = dir;
788 		ret = security_path_mkdir(&path, subdir, 0700);
789 		if (ret < 0)
790 			goto mkdir_error;
791 		ret = vfs_mkdir(&init_user_ns, d_inode(dir), subdir, 0700);
792 		if (ret < 0)
793 			goto mkdir_error;
794 
795 		if (unlikely(d_unhashed(subdir))) {
796 			dput(subdir);
797 			goto retry;
798 		}
799 		ASSERT(d_backing_inode(subdir));
800 
801 		_debug("mkdir -> %pd{ino=%lu}",
802 		       subdir, d_backing_inode(subdir)->i_ino);
803 	}
804 
805 	inode_unlock(d_inode(dir));
806 
807 	/* we need to make sure the subdir is a directory */
808 	ASSERT(d_backing_inode(subdir));
809 
810 	if (!d_can_lookup(subdir)) {
811 		pr_err("%s is not a directory\n", dirname);
812 		ret = -EIO;
813 		goto check_error;
814 	}
815 
816 	ret = -EPERM;
817 	if (!(d_backing_inode(subdir)->i_opflags & IOP_XATTR) ||
818 	    !d_backing_inode(subdir)->i_op->lookup ||
819 	    !d_backing_inode(subdir)->i_op->mkdir ||
820 	    !d_backing_inode(subdir)->i_op->create ||
821 	    !d_backing_inode(subdir)->i_op->rename ||
822 	    !d_backing_inode(subdir)->i_op->rmdir ||
823 	    !d_backing_inode(subdir)->i_op->unlink)
824 		goto check_error;
825 
826 	_leave(" = [%lu]", d_backing_inode(subdir)->i_ino);
827 	return subdir;
828 
829 check_error:
830 	dput(subdir);
831 	_leave(" = %d [check]", ret);
832 	return ERR_PTR(ret);
833 
834 mkdir_error:
835 	inode_unlock(d_inode(dir));
836 	dput(subdir);
837 	pr_err("mkdir %s failed with error %d\n", dirname, ret);
838 	return ERR_PTR(ret);
839 
840 lookup_error:
841 	inode_unlock(d_inode(dir));
842 	ret = PTR_ERR(subdir);
843 	pr_err("Lookup %s failed with error %d\n", dirname, ret);
844 	return ERR_PTR(ret);
845 
846 nomem_d_alloc:
847 	inode_unlock(d_inode(dir));
848 	_leave(" = -ENOMEM");
849 	return ERR_PTR(-ENOMEM);
850 }
851 
852 /*
853  * find out if an object is in use or not
854  * - if finds object and it's not in use:
855  *   - returns a pointer to the object and a reference on it
856  *   - returns with the directory locked
857  */
858 static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
859 					      struct dentry *dir,
860 					      char *filename)
861 {
862 	struct cachefiles_object *object;
863 	struct rb_node *_n;
864 	struct dentry *victim;
865 	int ret;
866 
867 	//_enter(",%pd/,%s",
868 	//       dir, filename);
869 
870 	/* look up the victim */
871 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
872 
873 	victim = lookup_one_len(filename, dir, strlen(filename));
874 	if (IS_ERR(victim))
875 		goto lookup_error;
876 
877 	//_debug("victim -> %pd %s",
878 	//       victim, d_backing_inode(victim) ? "positive" : "negative");
879 
880 	/* if the object is no longer there then we probably retired the object
881 	 * at the netfs's request whilst the cull was in progress
882 	 */
883 	if (d_is_negative(victim)) {
884 		inode_unlock(d_inode(dir));
885 		dput(victim);
886 		_leave(" = -ENOENT [absent]");
887 		return ERR_PTR(-ENOENT);
888 	}
889 
890 	/* check to see if we're using this object */
891 	read_lock(&cache->active_lock);
892 
893 	_n = cache->active_nodes.rb_node;
894 
895 	while (_n) {
896 		object = rb_entry(_n, struct cachefiles_object, active_node);
897 
898 		if (object->dentry > victim)
899 			_n = _n->rb_left;
900 		else if (object->dentry < victim)
901 			_n = _n->rb_right;
902 		else
903 			goto object_in_use;
904 	}
905 
906 	read_unlock(&cache->active_lock);
907 
908 	//_leave(" = %pd", victim);
909 	return victim;
910 
911 object_in_use:
912 	read_unlock(&cache->active_lock);
913 	inode_unlock(d_inode(dir));
914 	dput(victim);
915 	//_leave(" = -EBUSY [in use]");
916 	return ERR_PTR(-EBUSY);
917 
918 lookup_error:
919 	inode_unlock(d_inode(dir));
920 	ret = PTR_ERR(victim);
921 	if (ret == -ENOENT) {
922 		/* file or dir now absent - probably retired by netfs */
923 		_leave(" = -ESTALE [absent]");
924 		return ERR_PTR(-ESTALE);
925 	}
926 
927 	if (ret == -EIO) {
928 		cachefiles_io_error(cache, "Lookup failed");
929 	} else if (ret != -ENOMEM) {
930 		pr_err("Internal error: %d\n", ret);
931 		ret = -EIO;
932 	}
933 
934 	_leave(" = %d", ret);
935 	return ERR_PTR(ret);
936 }
937 
938 /*
939  * cull an object if it's not in use
940  * - called only by cache manager daemon
941  */
942 int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
943 		    char *filename)
944 {
945 	struct dentry *victim;
946 	int ret;
947 
948 	_enter(",%pd/,%s", dir, filename);
949 
950 	victim = cachefiles_check_active(cache, dir, filename);
951 	if (IS_ERR(victim))
952 		return PTR_ERR(victim);
953 
954 	_debug("victim -> %pd %s",
955 	       victim, d_backing_inode(victim) ? "positive" : "negative");
956 
957 	/* okay... the victim is not being used so we can cull it
958 	 * - start by marking it as stale
959 	 */
960 	_debug("victim is cullable");
961 
962 	ret = cachefiles_remove_object_xattr(cache, victim);
963 	if (ret < 0)
964 		goto error_unlock;
965 
966 	/*  actually remove the victim (drops the dir mutex) */
967 	_debug("bury");
968 
969 	ret = cachefiles_bury_object(cache, NULL, dir, victim, false,
970 				     FSCACHE_OBJECT_WAS_CULLED);
971 	if (ret < 0)
972 		goto error;
973 
974 	dput(victim);
975 	_leave(" = 0");
976 	return 0;
977 
978 error_unlock:
979 	inode_unlock(d_inode(dir));
980 error:
981 	dput(victim);
982 	if (ret == -ENOENT) {
983 		/* file or dir now absent - probably retired by netfs */
984 		_leave(" = -ESTALE [absent]");
985 		return -ESTALE;
986 	}
987 
988 	if (ret != -ENOMEM) {
989 		pr_err("Internal error: %d\n", ret);
990 		ret = -EIO;
991 	}
992 
993 	_leave(" = %d", ret);
994 	return ret;
995 }
996 
997 /*
998  * find out if an object is in use or not
999  * - called only by cache manager daemon
1000  * - returns -EBUSY or 0 to indicate whether an object is in use or not
1001  */
1002 int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
1003 			    char *filename)
1004 {
1005 	struct dentry *victim;
1006 
1007 	//_enter(",%pd/,%s",
1008 	//       dir, filename);
1009 
1010 	victim = cachefiles_check_active(cache, dir, filename);
1011 	if (IS_ERR(victim))
1012 		return PTR_ERR(victim);
1013 
1014 	inode_unlock(d_inode(dir));
1015 	dput(victim);
1016 	//_leave(" = 0");
1017 	return 0;
1018 }
1019