Linux Audio

Check our new training course

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