1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* CacheFiles path walking and related routines
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
9 #include <linux/namei.h>
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.
17 static bool __cachefiles_mark_inode_in_use(struct cachefiles_object *object,
18 struct dentry *dentry)
20 struct inode *inode = d_backing_inode(dentry);
23 if (!(inode->i_flags & S_KERNEL_FILE)) {
24 inode->i_flags |= S_KERNEL_FILE;
25 trace_cachefiles_mark_active(object, inode);
28 trace_cachefiles_mark_failed(object, inode);
29 pr_notice("cachefiles: Inode already in use: %pd (B=%lx)\n",
30 dentry, inode->i_ino);
36 static bool cachefiles_mark_inode_in_use(struct cachefiles_object *object,
37 struct dentry *dentry)
39 struct inode *inode = d_backing_inode(dentry);
43 can_use = __cachefiles_mark_inode_in_use(object, dentry);
49 * Unmark a backing inode. The caller must hold the inode lock.
51 static void __cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
52 struct dentry *dentry)
54 struct inode *inode = d_backing_inode(dentry);
56 inode->i_flags &= ~S_KERNEL_FILE;
57 trace_cachefiles_mark_inactive(object, inode);
60 static void cachefiles_do_unmark_inode_in_use(struct cachefiles_object *object,
61 struct dentry *dentry)
63 struct inode *inode = d_backing_inode(dentry);
66 __cachefiles_unmark_inode_in_use(object, dentry);
71 * Unmark a backing inode and tell cachefilesd that there's something that can
74 void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
77 struct cachefiles_cache *cache = object->volume->cache;
78 struct inode *inode = file_inode(file);
81 cachefiles_do_unmark_inode_in_use(object, file->f_path.dentry);
83 if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) {
84 atomic_long_add(inode->i_blocks, &cache->b_released);
85 if (atomic_inc_return(&cache->f_released))
86 cachefiles_state_changed(cache);
94 struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
99 struct dentry *subdir;
103 _enter(",,%s", dirname);
105 /* search the current directory for the element name */
106 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
109 ret = cachefiles_inject_read_error();
111 subdir = lookup_one_len(dirname, dir, strlen(dirname));
113 subdir = ERR_PTR(ret);
114 trace_cachefiles_lookup(NULL, dir, subdir);
115 if (IS_ERR(subdir)) {
116 trace_cachefiles_vfs_error(NULL, d_backing_inode(dir),
118 cachefiles_trace_lookup_error);
119 if (PTR_ERR(subdir) == -ENOMEM)
124 _debug("subdir -> %pd %s",
125 subdir, d_backing_inode(subdir) ? "positive" : "negative");
127 /* we need to create the subdir if it doesn't exist yet */
128 if (d_is_negative(subdir)) {
129 ret = cachefiles_has_space(cache, 1, 0,
130 cachefiles_has_space_for_create);
134 _debug("attempt mkdir");
136 path.mnt = cache->mnt;
138 ret = security_path_mkdir(&path, subdir, 0700);
141 ret = cachefiles_inject_write_error();
143 ret = vfs_mkdir(&init_user_ns, d_inode(dir), subdir, 0700);
145 trace_cachefiles_vfs_error(NULL, d_inode(dir), ret,
146 cachefiles_trace_mkdir_error);
149 trace_cachefiles_mkdir(dir, subdir);
151 if (unlikely(d_unhashed(subdir))) {
152 cachefiles_put_directory(subdir);
155 ASSERT(d_backing_inode(subdir));
157 _debug("mkdir -> %pd{ino=%lu}",
158 subdir, d_backing_inode(subdir)->i_ino);
163 /* Tell rmdir() it's not allowed to delete the subdir */
164 inode_lock(d_inode(subdir));
165 inode_unlock(d_inode(dir));
167 if (!__cachefiles_mark_inode_in_use(NULL, subdir))
170 inode_unlock(d_inode(subdir));
172 /* we need to make sure the subdir is a directory */
173 ASSERT(d_backing_inode(subdir));
175 if (!d_can_lookup(subdir)) {
176 pr_err("%s is not a directory\n", dirname);
182 if (!(d_backing_inode(subdir)->i_opflags & IOP_XATTR) ||
183 !d_backing_inode(subdir)->i_op->lookup ||
184 !d_backing_inode(subdir)->i_op->mkdir ||
185 !d_backing_inode(subdir)->i_op->rename ||
186 !d_backing_inode(subdir)->i_op->rmdir ||
187 !d_backing_inode(subdir)->i_op->unlink)
190 _leave(" = [%lu]", d_backing_inode(subdir)->i_ino);
194 cachefiles_put_directory(subdir);
195 _leave(" = %d [check]", ret);
199 inode_unlock(d_inode(subdir));
201 return ERR_PTR(-EBUSY);
204 inode_unlock(d_inode(dir));
206 pr_err("mkdir %s failed with error %d\n", dirname, ret);
210 inode_unlock(d_inode(dir));
211 ret = PTR_ERR(subdir);
212 pr_err("Lookup %s failed with error %d\n", dirname, ret);
216 inode_unlock(d_inode(dir));
217 _leave(" = -ENOMEM");
218 return ERR_PTR(-ENOMEM);
222 * Put a subdirectory.
224 void cachefiles_put_directory(struct dentry *dir)
227 inode_lock(dir->d_inode);
228 __cachefiles_unmark_inode_in_use(NULL, dir);
229 inode_unlock(dir->d_inode);
235 * Remove a regular file from the cache.
237 static int cachefiles_unlink(struct cachefiles_cache *cache,
238 struct cachefiles_object *object,
239 struct dentry *dir, struct dentry *dentry,
240 enum fscache_why_object_killed why)
248 trace_cachefiles_unlink(object, d_inode(dentry)->i_ino, why);
249 ret = security_path_unlink(&path, dentry);
251 cachefiles_io_error(cache, "Unlink security error");
255 ret = cachefiles_inject_remove_error();
257 ret = vfs_unlink(&init_user_ns, d_backing_inode(dir), dentry, NULL);
259 cachefiles_io_error(cache, "Unlink failed");
262 trace_cachefiles_vfs_error(object, d_backing_inode(dir), ret,
263 cachefiles_trace_unlink_error);
268 * Delete an object representation from the cache
269 * - File backed objects are unlinked
270 * - Directory backed objects are stuffed into the graveyard for userspace to
273 int cachefiles_bury_object(struct cachefiles_cache *cache,
274 struct cachefiles_object *object,
277 enum fscache_why_object_killed why)
279 struct dentry *grave, *trap;
280 struct path path, path_to_graveyard;
281 char nbuffer[8 + 8 + 1];
284 _enter(",'%pd','%pd'", dir, rep);
286 if (rep->d_parent != dir) {
287 inode_unlock(d_inode(dir));
288 _leave(" = -ESTALE");
292 /* non-directories can just be unlinked */
293 if (!d_is_dir(rep)) {
294 dget(rep); /* Stop the dentry being negated if it's only pinned
297 ret = cachefiles_unlink(cache, object, dir, rep, why);
300 inode_unlock(d_inode(dir));
301 _leave(" = %d", ret);
305 /* directories have to be moved to the graveyard */
306 _debug("move stale object to graveyard");
307 inode_unlock(d_inode(dir));
310 /* first step is to make up a grave dentry in the graveyard */
311 sprintf(nbuffer, "%08x%08x",
312 (uint32_t) ktime_get_real_seconds(),
313 (uint32_t) atomic_inc_return(&cache->gravecounter));
315 /* do the multiway lock magic */
316 trap = lock_rename(cache->graveyard, dir);
318 /* do some checks before getting the grave dentry */
319 if (rep->d_parent != dir || IS_DEADDIR(d_inode(rep))) {
320 /* the entry was probably culled when we dropped the parent dir
322 unlock_rename(cache->graveyard, dir);
323 _leave(" = 0 [culled?]");
327 if (!d_can_lookup(cache->graveyard)) {
328 unlock_rename(cache->graveyard, dir);
329 cachefiles_io_error(cache, "Graveyard no longer a directory");
334 unlock_rename(cache->graveyard, dir);
335 cachefiles_io_error(cache, "May not make directory loop");
339 if (d_mountpoint(rep)) {
340 unlock_rename(cache->graveyard, dir);
341 cachefiles_io_error(cache, "Mountpoint in cache");
345 grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
347 unlock_rename(cache->graveyard, dir);
348 trace_cachefiles_vfs_error(object, d_inode(cache->graveyard),
350 cachefiles_trace_lookup_error);
352 if (PTR_ERR(grave) == -ENOMEM) {
353 _leave(" = -ENOMEM");
357 cachefiles_io_error(cache, "Lookup error %ld", PTR_ERR(grave));
361 if (d_is_positive(grave)) {
362 unlock_rename(cache->graveyard, dir);
369 if (d_mountpoint(grave)) {
370 unlock_rename(cache->graveyard, dir);
372 cachefiles_io_error(cache, "Mountpoint in graveyard");
376 /* target should not be an ancestor of source */
378 unlock_rename(cache->graveyard, dir);
380 cachefiles_io_error(cache, "May not make directory loop");
384 /* attempt the rename */
385 path.mnt = cache->mnt;
387 path_to_graveyard.mnt = cache->mnt;
388 path_to_graveyard.dentry = cache->graveyard;
389 ret = security_path_rename(&path, rep, &path_to_graveyard, grave, 0);
391 cachefiles_io_error(cache, "Rename security error %d", ret);
393 struct renamedata rd = {
394 .old_mnt_userns = &init_user_ns,
395 .old_dir = d_inode(dir),
397 .new_mnt_userns = &init_user_ns,
398 .new_dir = d_inode(cache->graveyard),
401 trace_cachefiles_rename(object, d_inode(rep)->i_ino, why);
402 ret = cachefiles_inject_read_error();
404 ret = vfs_rename(&rd);
406 trace_cachefiles_vfs_error(object, d_inode(dir), ret,
407 cachefiles_trace_rename_error);
408 if (ret != 0 && ret != -ENOMEM)
409 cachefiles_io_error(cache,
410 "Rename failed with error %d", ret);
413 __cachefiles_unmark_inode_in_use(object, rep);
414 unlock_rename(cache->graveyard, dir);
421 * Delete a cache file.
423 int cachefiles_delete_object(struct cachefiles_object *object,
424 enum fscache_why_object_killed why)
426 struct cachefiles_volume *volume = object->volume;
427 struct dentry *dentry = object->file->f_path.dentry;
428 struct dentry *fan = volume->fanout[(u8)object->cookie->key_hash];
431 _enter(",OBJ%x{%pD}", object->debug_id, object->file);
433 /* Stop the dentry being negated if it's only pinned by a file struct. */
436 inode_lock_nested(d_backing_inode(fan), I_MUTEX_PARENT);
437 ret = cachefiles_unlink(volume->cache, object, fan, dentry, why);
438 inode_unlock(d_backing_inode(fan));
444 * Create a temporary file and leave it unattached and un-xattr'd until the
445 * time comes to discard the object from memory.
447 struct file *cachefiles_create_tmpfile(struct cachefiles_object *object)
449 struct cachefiles_volume *volume = object->volume;
450 struct cachefiles_cache *cache = volume->cache;
451 const struct cred *saved_cred;
452 struct dentry *fan = volume->fanout[(u8)object->cookie->key_hash];
459 cachefiles_begin_secure(cache, &saved_cred);
461 path.mnt = cache->mnt;
462 ret = cachefiles_inject_write_error();
464 path.dentry = vfs_tmpfile(&init_user_ns, fan, S_IFREG, O_RDWR);
466 path.dentry = ERR_PTR(ret);
467 if (IS_ERR(path.dentry)) {
468 trace_cachefiles_vfs_error(object, d_inode(fan), PTR_ERR(path.dentry),
469 cachefiles_trace_tmpfile_error);
470 if (PTR_ERR(path.dentry) == -EIO)
471 cachefiles_io_error_obj(object, "Failed to create tmpfile");
472 file = ERR_CAST(path.dentry);
476 trace_cachefiles_tmpfile(object, d_backing_inode(path.dentry));
478 if (!cachefiles_mark_inode_in_use(object, path.dentry)) {
479 file = ERR_PTR(-EBUSY);
483 ret = cachefiles_ondemand_init_object(object);
489 ni_size = object->cookie->object_size;
490 ni_size = round_up(ni_size, CACHEFILES_DIO_BLOCK_SIZE);
493 trace_cachefiles_trunc(object, d_backing_inode(path.dentry), 0, ni_size,
494 cachefiles_trunc_expand_tmpfile);
495 ret = cachefiles_inject_write_error();
497 ret = vfs_truncate(&path, ni_size);
499 trace_cachefiles_vfs_error(
500 object, d_backing_inode(path.dentry), ret,
501 cachefiles_trace_trunc_error);
507 file = open_with_fake_path(&path, O_RDWR | O_LARGEFILE | O_DIRECT,
508 d_backing_inode(path.dentry), cache->cache_cred);
510 trace_cachefiles_vfs_error(object, d_backing_inode(path.dentry),
512 cachefiles_trace_open_error);
515 if (unlikely(!file->f_op->read_iter) ||
516 unlikely(!file->f_op->write_iter)) {
518 pr_notice("Cache does not support read_iter and write_iter\n");
519 file = ERR_PTR(-EINVAL);
526 cachefiles_do_unmark_inode_in_use(object, path.dentry);
530 cachefiles_end_secure(cache, saved_cred);
537 static bool cachefiles_create_file(struct cachefiles_object *object)
542 ret = cachefiles_has_space(object->volume->cache, 1, 0,
543 cachefiles_has_space_for_create);
547 file = cachefiles_create_tmpfile(object);
551 set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &object->cookie->flags);
552 set_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags);
553 _debug("create -> %pD{ino=%lu}", file, file_inode(file)->i_ino);
559 * Open an existing file, checking its attributes and replacing it if it is
562 static bool cachefiles_open_file(struct cachefiles_object *object,
563 struct dentry *dentry)
565 struct cachefiles_cache *cache = object->volume->cache;
570 _enter("%pd", dentry);
572 if (!cachefiles_mark_inode_in_use(object, dentry))
575 /* We need to open a file interface onto a data file now as we can't do
576 * it on demand because writeback called from do_exit() sees
577 * current->fs == NULL - which breaks d_path() called from ext4 open.
579 path.mnt = cache->mnt;
580 path.dentry = dentry;
581 file = open_with_fake_path(&path, O_RDWR | O_LARGEFILE | O_DIRECT,
582 d_backing_inode(dentry), cache->cache_cred);
584 trace_cachefiles_vfs_error(object, d_backing_inode(dentry),
586 cachefiles_trace_open_error);
590 if (unlikely(!file->f_op->read_iter) ||
591 unlikely(!file->f_op->write_iter)) {
592 pr_notice("Cache does not support read_iter and write_iter\n");
595 _debug("file -> %pd positive", dentry);
597 ret = cachefiles_ondemand_init_object(object);
601 ret = cachefiles_check_auxdata(object, file);
607 /* Always update the atime on an object we've just looked up (this is
608 * used to keep track of culling, and atimes are only updated by read,
609 * write and readdir but not lookup or open).
611 touch_atime(&file->f_path);
616 fscache_cookie_lookup_negative(object->cookie);
617 cachefiles_unmark_inode_in_use(object, file);
621 return cachefiles_create_file(object);
627 cachefiles_do_unmark_inode_in_use(object, dentry);
633 * walk from the parent object to the child object through the backing
634 * filesystem, creating directories as we go
636 bool cachefiles_look_up_object(struct cachefiles_object *object)
638 struct cachefiles_volume *volume = object->volume;
639 struct dentry *dentry, *fan = volume->fanout[(u8)object->cookie->key_hash];
642 _enter("OBJ%x,%s,", object->debug_id, object->d_name);
644 /* Look up path "cache/vol/fanout/file". */
645 ret = cachefiles_inject_read_error();
647 dentry = lookup_positive_unlocked(object->d_name, fan,
650 dentry = ERR_PTR(ret);
651 trace_cachefiles_lookup(object, fan, dentry);
652 if (IS_ERR(dentry)) {
653 if (dentry == ERR_PTR(-ENOENT))
655 if (dentry == ERR_PTR(-EIO))
656 cachefiles_io_error_obj(object, "Lookup failed");
660 if (!d_is_reg(dentry)) {
661 pr_err("%pd is not a file\n", dentry);
662 inode_lock_nested(d_inode(fan), I_MUTEX_PARENT);
663 ret = cachefiles_bury_object(volume->cache, object, fan, dentry,
664 FSCACHE_OBJECT_IS_WEIRD);
671 if (!cachefiles_open_file(object, dentry))
674 _leave(" = t [%lu]", file_inode(object->file)->i_ino);
678 fscache_cookie_lookup_negative(object->cookie);
679 return cachefiles_create_file(object);
683 * Attempt to link a temporary file into its rightful place in the cache.
685 bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache,
686 struct cachefiles_object *object)
688 struct cachefiles_volume *volume = object->volume;
689 struct dentry *dentry, *fan = volume->fanout[(u8)object->cookie->key_hash];
690 bool success = false;
693 _enter(",%pD", object->file);
695 inode_lock_nested(d_inode(fan), I_MUTEX_PARENT);
696 ret = cachefiles_inject_read_error();
698 dentry = lookup_one_len(object->d_name, fan, object->d_name_len);
700 dentry = ERR_PTR(ret);
701 if (IS_ERR(dentry)) {
702 trace_cachefiles_vfs_error(object, d_inode(fan), PTR_ERR(dentry),
703 cachefiles_trace_lookup_error);
704 _debug("lookup fail %ld", PTR_ERR(dentry));
708 if (!d_is_negative(dentry)) {
709 if (d_backing_inode(dentry) == file_inode(object->file)) {
714 ret = cachefiles_unlink(volume->cache, object, fan, dentry,
715 FSCACHE_OBJECT_IS_STALE);
720 ret = cachefiles_inject_read_error();
722 dentry = lookup_one_len(object->d_name, fan, object->d_name_len);
724 dentry = ERR_PTR(ret);
725 if (IS_ERR(dentry)) {
726 trace_cachefiles_vfs_error(object, d_inode(fan), PTR_ERR(dentry),
727 cachefiles_trace_lookup_error);
728 _debug("lookup fail %ld", PTR_ERR(dentry));
733 ret = cachefiles_inject_read_error();
735 ret = vfs_link(object->file->f_path.dentry, &init_user_ns,
736 d_inode(fan), dentry, NULL);
738 trace_cachefiles_vfs_error(object, d_inode(fan), ret,
739 cachefiles_trace_link_error);
740 _debug("link fail %d", ret);
742 trace_cachefiles_link(object, file_inode(object->file));
743 spin_lock(&object->lock);
744 /* TODO: Do we want to switch the file pointer to the new dentry? */
745 clear_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags);
746 spin_unlock(&object->lock);
753 inode_unlock(d_inode(fan));
754 _leave(" = %u", success);
759 * Look up an inode to be checked or culled. Return -EBUSY if the inode is
762 static struct dentry *cachefiles_lookup_for_cull(struct cachefiles_cache *cache,
766 struct dentry *victim;
769 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
771 victim = lookup_one_len(filename, dir, strlen(filename));
774 if (d_is_negative(victim))
776 if (d_inode(victim)->i_flags & S_KERNEL_FILE)
783 inode_unlock(d_inode(dir));
788 inode_unlock(d_inode(dir));
789 ret = PTR_ERR(victim);
791 return ERR_PTR(-ESTALE); /* Probably got retired by the netfs */
794 cachefiles_io_error(cache, "Lookup failed");
795 } else if (ret != -ENOMEM) {
796 pr_err("Internal error: %d\n", ret);
804 * Cull an object if it's not in use
805 * - called only by cache manager daemon
807 int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
810 struct dentry *victim;
814 _enter(",%pd/,%s", dir, filename);
816 victim = cachefiles_lookup_for_cull(cache, dir, filename);
818 return PTR_ERR(victim);
820 /* check to see if someone is using this object */
821 inode = d_inode(victim);
823 if (inode->i_flags & S_KERNEL_FILE) {
826 /* Stop the cache from picking it back up */
827 inode->i_flags |= S_KERNEL_FILE;
834 ret = cachefiles_bury_object(cache, NULL, dir, victim,
835 FSCACHE_OBJECT_WAS_CULLED);
839 fscache_count_culled();
845 inode_unlock(d_inode(dir));
849 return -ESTALE; /* Probably got retired by the netfs */
851 if (ret != -ENOMEM) {
852 pr_err("Internal error: %d\n", ret);
856 _leave(" = %d", ret);
861 * Find out if an object is in use or not
862 * - called only by cache manager daemon
863 * - returns -EBUSY or 0 to indicate whether an object is in use or not
865 int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
868 struct dentry *victim;
871 victim = cachefiles_lookup_for_cull(cache, dir, filename);
873 return PTR_ERR(victim);
875 inode_unlock(d_inode(dir));