cache.o \
        daemon.o \
        main.o \
+       namei.o \
        security.o
 
 cachefiles-$(CONFIG_CACHEFILES_ERROR_INJECTION) += error_inject.o
 
--- /dev/null
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* CacheFiles path walking and related routines
+ *
+ * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ */
+
+#include <linux/fs.h>
+#include "internal.h"
+
+/*
+ * Mark the backing file as being a cache file if it's not already in use.  The
+ * mark tells the culling request command that it's not allowed to cull the
+ * file or directory.  The caller must hold the inode lock.
+ */
+static bool __cachefiles_mark_inode_in_use(struct cachefiles_object *object,
+                                          struct dentry *dentry)
+{
+       struct inode *inode = d_backing_inode(dentry);
+       bool can_use = false;
+
+       if (!(inode->i_flags & S_KERNEL_FILE)) {
+               inode->i_flags |= S_KERNEL_FILE;
+               trace_cachefiles_mark_active(object, inode);
+               can_use = true;
+       } else {
+               pr_notice("cachefiles: Inode already in use: %pd\n", dentry);
+       }
+
+       return can_use;
+}
+
+/*
+ * Unmark a backing inode.  The caller must hold the inode lock.
+ */
+static void __cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
+                                            struct dentry *dentry)
+{
+       struct inode *inode = d_backing_inode(dentry);
+
+       inode->i_flags &= ~S_KERNEL_FILE;
+       trace_cachefiles_mark_inactive(object, inode);
+}
 
        inode_lock(dentry->d_inode);
 
        error = -EBUSY;
-       if (is_local_mountpoint(dentry))
+       if (is_local_mountpoint(dentry) ||
+           (dentry->d_inode->i_flags & S_KERNEL_FILE))
                goto out;
 
        error = security_inode_rmdir(dir, dentry);
 
 #define S_ENCRYPTED    (1 << 14) /* Encrypted file (using fs/crypto/) */
 #define S_CASEFOLD     (1 << 15) /* Casefolded file */
 #define S_VERITY       (1 << 16) /* Verity file (using fs/verity/) */
+#define S_KERNEL_FILE  (1 << 17) /* File is in use by the kernel (eg. fs/cachefiles) */
 
 /*
  * Note that nosuid etc flags are inode-specific: setting some file-system
 
 #define E_(a, b)       { a, b }
 
 
+TRACE_EVENT(cachefiles_mark_active,
+           TP_PROTO(struct cachefiles_object *obj,
+                    struct inode *inode),
+
+           TP_ARGS(obj, inode),
+
+           /* Note that obj may be NULL */
+           TP_STRUCT__entry(
+                   __field(unsigned int,               obj             )
+                   __field(ino_t,                      inode           )
+                            ),
+
+           TP_fast_assign(
+                   __entry->obj        = obj ? obj->debug_id : 0;
+                   __entry->inode      = inode->i_ino;
+                          ),
+
+           TP_printk("o=%08x i=%lx",
+                     __entry->obj, __entry->inode)
+           );
+
+TRACE_EVENT(cachefiles_mark_inactive,
+           TP_PROTO(struct cachefiles_object *obj,
+                    struct inode *inode),
+
+           TP_ARGS(obj, inode),
+
+           /* Note that obj may be NULL */
+           TP_STRUCT__entry(
+                   __field(unsigned int,               obj             )
+                   __field(ino_t,                      inode           )
+                            ),
+
+           TP_fast_assign(
+                   __entry->obj        = obj ? obj->debug_id : 0;
+                   __entry->inode      = inode->i_ino;
+                          ),
+
+           TP_printk("o=%08x i=%lx",
+                     __entry->obj, __entry->inode)
+           );
+
 TRACE_EVENT(cachefiles_vfs_error,
            TP_PROTO(struct cachefiles_object *obj, struct inode *backer,
                     int error, enum cachefiles_error_trace where),