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,
22 if (!(inode->i_flags & S_KERNEL_FILE)) {
23 inode->i_flags |= S_KERNEL_FILE;
24 trace_cachefiles_mark_active(object, inode);
27 trace_cachefiles_mark_failed(object, inode);
33 static bool cachefiles_mark_inode_in_use(struct cachefiles_object *object,
39 can_use = __cachefiles_mark_inode_in_use(object, inode);
45 * Unmark a backing inode. The caller must hold the inode lock.
47 static void __cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
50 inode->i_flags &= ~S_KERNEL_FILE;
51 trace_cachefiles_mark_inactive(object, inode);
54 static void cachefiles_do_unmark_inode_in_use(struct cachefiles_object *object,
58 __cachefiles_unmark_inode_in_use(object, inode);
63 * Unmark a backing inode and tell cachefilesd that there's something that can
66 void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
69 struct cachefiles_cache *cache = object->volume->cache;
70 struct inode *inode = file_inode(file);
72 cachefiles_do_unmark_inode_in_use(object, inode);
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);
84 struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
89 struct dentry *subdir;
93 _enter(",,%s", dirname);
95 /* search the current directory for the element name */
96 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
99 ret = cachefiles_inject_read_error();
101 subdir = lookup_one_len(dirname, dir, strlen(dirname));
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),
108 cachefiles_trace_lookup_error);
109 if (PTR_ERR(subdir) == -ENOMEM)
114 _debug("subdir -> %pd %s",
115 subdir, d_backing_inode(subdir) ? "positive" : "negative");
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);
124 _debug("attempt mkdir");
126 path.mnt = cache->mnt;
128 ret = security_path_mkdir(&path, subdir, 0700);
131 ret = cachefiles_inject_write_error();
133 ret = vfs_mkdir(&nop_mnt_idmap, d_inode(dir), subdir, 0700);
135 trace_cachefiles_vfs_error(NULL, d_inode(dir), ret,
136 cachefiles_trace_mkdir_error);
139 trace_cachefiles_mkdir(dir, subdir);
141 if (unlikely(d_unhashed(subdir))) {
142 cachefiles_put_directory(subdir);
145 ASSERT(d_backing_inode(subdir));
147 _debug("mkdir -> %pd{ino=%lu}",
148 subdir, d_backing_inode(subdir)->i_ino);
153 /* Tell rmdir() it's not allowed to delete the subdir */
154 inode_lock(d_inode(subdir));
155 inode_unlock(d_inode(dir));
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);
163 inode_unlock(d_inode(subdir));
165 /* we need to make sure the subdir is a directory */
166 ASSERT(d_backing_inode(subdir));
168 if (!d_can_lookup(subdir)) {
169 pr_err("%s is not a directory\n", dirname);
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)
183 _leave(" = [%lu]", d_backing_inode(subdir)->i_ino);
187 cachefiles_put_directory(subdir);
188 _leave(" = %d [check]", ret);
192 inode_unlock(d_inode(subdir));
194 return ERR_PTR(-EBUSY);
197 inode_unlock(d_inode(dir));
199 pr_err("mkdir %s failed with error %d\n", dirname, ret);
203 inode_unlock(d_inode(dir));
204 ret = PTR_ERR(subdir);
205 pr_err("Lookup %s failed with error %d\n", dirname, ret);
209 inode_unlock(d_inode(dir));
210 _leave(" = -ENOMEM");
211 return ERR_PTR(-ENOMEM);
215 * Put a subdirectory.
217 void cachefiles_put_directory(struct dentry *dir)
220 cachefiles_do_unmark_inode_in_use(NULL, d_inode(dir));
226 * Remove a regular file from the cache.
228 static 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)
239 trace_cachefiles_unlink(object, d_inode(dentry)->i_ino, why);
240 ret = security_path_unlink(&path, dentry);
242 cachefiles_io_error(cache, "Unlink security error");
246 ret = cachefiles_inject_remove_error();
248 ret = vfs_unlink(&nop_mnt_idmap, d_backing_inode(dir), dentry, NULL);
250 cachefiles_io_error(cache, "Unlink failed");
253 trace_cachefiles_vfs_error(object, d_backing_inode(dir), ret,
254 cachefiles_trace_unlink_error);
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
264 int cachefiles_bury_object(struct cachefiles_cache *cache,
265 struct cachefiles_object *object,
268 enum fscache_why_object_killed why)
270 struct dentry *grave, *trap;
271 struct path path, path_to_graveyard;
272 char nbuffer[8 + 8 + 1];
275 _enter(",'%pd','%pd'", dir, rep);
277 if (rep->d_parent != dir) {
278 inode_unlock(d_inode(dir));
279 _leave(" = -ESTALE");
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
288 ret = cachefiles_unlink(cache, object, dir, rep, why);
291 inode_unlock(d_inode(dir));
292 _leave(" = %d", ret);
296 /* directories have to be moved to the graveyard */
297 _debug("move stale object to graveyard");
298 inode_unlock(d_inode(dir));
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));
306 /* do the multiway lock magic */
307 trap = lock_rename(cache->graveyard, dir);
309 /* do some checks before getting the grave dentry */
310 if (rep->d_parent != dir || IS_DEADDIR(d_inode(rep))) {
311 /* the entry was probably culled when we dropped the parent dir
313 unlock_rename(cache->graveyard, dir);
314 _leave(" = 0 [culled?]");
318 if (!d_can_lookup(cache->graveyard)) {
319 unlock_rename(cache->graveyard, dir);
320 cachefiles_io_error(cache, "Graveyard no longer a directory");
325 unlock_rename(cache->graveyard, dir);
326 cachefiles_io_error(cache, "May not make directory loop");
330 if (d_mountpoint(rep)) {
331 unlock_rename(cache->graveyard, dir);
332 cachefiles_io_error(cache, "Mountpoint in cache");
336 grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
338 unlock_rename(cache->graveyard, dir);
339 trace_cachefiles_vfs_error(object, d_inode(cache->graveyard),
341 cachefiles_trace_lookup_error);
343 if (PTR_ERR(grave) == -ENOMEM) {
344 _leave(" = -ENOMEM");
348 cachefiles_io_error(cache, "Lookup error %ld", PTR_ERR(grave));
352 if (d_is_positive(grave)) {
353 unlock_rename(cache->graveyard, dir);
360 if (d_mountpoint(grave)) {
361 unlock_rename(cache->graveyard, dir);
363 cachefiles_io_error(cache, "Mountpoint in graveyard");
367 /* target should not be an ancestor of source */
369 unlock_rename(cache->graveyard, dir);
371 cachefiles_io_error(cache, "May not make directory loop");
375 /* attempt the rename */
376 path.mnt = cache->mnt;
378 path_to_graveyard.mnt = cache->mnt;
379 path_to_graveyard.dentry = cache->graveyard;
380 ret = security_path_rename(&path, rep, &path_to_graveyard, grave, 0);
382 cachefiles_io_error(cache, "Rename security error %d", ret);
384 struct renamedata rd = {
385 .old_mnt_idmap = &nop_mnt_idmap,
386 .old_dir = d_inode(dir),
388 .new_mnt_idmap = &nop_mnt_idmap,
389 .new_dir = d_inode(cache->graveyard),
392 trace_cachefiles_rename(object, d_inode(rep)->i_ino, why);
393 ret = cachefiles_inject_read_error();
395 ret = vfs_rename(&rd);
397 trace_cachefiles_vfs_error(object, d_inode(dir), ret,
398 cachefiles_trace_rename_error);
399 if (ret != 0 && ret != -ENOMEM)
400 cachefiles_io_error(cache,
401 "Rename failed with error %d", ret);
404 __cachefiles_unmark_inode_in_use(object, d_inode(rep));
405 unlock_rename(cache->graveyard, dir);
412 * Delete a cache file.
414 int cachefiles_delete_object(struct cachefiles_object *object,
415 enum fscache_why_object_killed why)
417 struct cachefiles_volume *volume = object->volume;
418 struct dentry *dentry = object->file->f_path.dentry;
419 struct dentry *fan = volume->fanout[(u8)object->cookie->key_hash];
422 _enter(",OBJ%x{%pD}", object->debug_id, object->file);
424 /* Stop the dentry being negated if it's only pinned by a file struct. */
427 inode_lock_nested(d_backing_inode(fan), I_MUTEX_PARENT);
428 ret = cachefiles_unlink(volume->cache, object, fan, dentry, why);
429 inode_unlock(d_backing_inode(fan));
435 * Create a temporary file and leave it unattached and un-xattr'd until the
436 * time comes to discard the object from memory.
438 struct file *cachefiles_create_tmpfile(struct cachefiles_object *object)
440 struct cachefiles_volume *volume = object->volume;
441 struct cachefiles_cache *cache = volume->cache;
442 const struct cred *saved_cred;
443 struct dentry *fan = volume->fanout[(u8)object->cookie->key_hash];
445 const struct path parentpath = { .mnt = cache->mnt, .dentry = fan };
450 cachefiles_begin_secure(cache, &saved_cred);
452 ret = cachefiles_inject_write_error();
454 file = kernel_tmpfile_open(&nop_mnt_idmap, &parentpath,
456 O_RDWR | O_LARGEFILE | O_DIRECT,
458 ret = PTR_ERR_OR_ZERO(file);
461 trace_cachefiles_vfs_error(object, d_inode(fan), ret,
462 cachefiles_trace_tmpfile_error);
464 cachefiles_io_error_obj(object, "Failed to create tmpfile");
468 trace_cachefiles_tmpfile(object, file_inode(file));
470 /* This is a newly created file with no other possible user */
471 if (!cachefiles_mark_inode_in_use(object, file_inode(file)))
474 ret = cachefiles_ondemand_init_object(object);
478 ni_size = object->cookie->object_size;
479 ni_size = round_up(ni_size, CACHEFILES_DIO_BLOCK_SIZE);
482 trace_cachefiles_trunc(object, file_inode(file), 0, ni_size,
483 cachefiles_trunc_expand_tmpfile);
484 ret = cachefiles_inject_write_error();
486 ret = vfs_truncate(&file->f_path, ni_size);
488 trace_cachefiles_vfs_error(
489 object, file_inode(file), ret,
490 cachefiles_trace_trunc_error);
496 if (unlikely(!file->f_op->read_iter) ||
497 unlikely(!file->f_op->write_iter)) {
499 pr_notice("Cache does not support read_iter and write_iter\n");
503 cachefiles_end_secure(cache, saved_cred);
507 cachefiles_do_unmark_inode_in_use(object, file_inode(file));
517 static bool cachefiles_create_file(struct cachefiles_object *object)
522 ret = cachefiles_has_space(object->volume->cache, 1, 0,
523 cachefiles_has_space_for_create);
527 file = cachefiles_create_tmpfile(object);
531 set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &object->cookie->flags);
532 set_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags);
533 _debug("create -> %pD{ino=%lu}", file, file_inode(file)->i_ino);
539 * Open an existing file, checking its attributes and replacing it if it is
542 static bool cachefiles_open_file(struct cachefiles_object *object,
543 struct dentry *dentry)
545 struct cachefiles_cache *cache = object->volume->cache;
550 _enter("%pd", dentry);
552 if (!cachefiles_mark_inode_in_use(object, d_inode(dentry))) {
553 pr_notice("cachefiles: Inode already in use: %pd (B=%lx)\n",
554 dentry, d_inode(dentry)->i_ino);
558 /* We need to open a file interface onto a data file now as we can't do
559 * it on demand because writeback called from do_exit() sees
560 * current->fs == NULL - which breaks d_path() called from ext4 open.
562 path.mnt = cache->mnt;
563 path.dentry = dentry;
564 file = kernel_file_open(&path, O_RDWR | O_LARGEFILE | O_DIRECT,
565 d_backing_inode(dentry), cache->cache_cred);
567 trace_cachefiles_vfs_error(object, d_backing_inode(dentry),
569 cachefiles_trace_open_error);
573 if (unlikely(!file->f_op->read_iter) ||
574 unlikely(!file->f_op->write_iter)) {
575 pr_notice("Cache does not support read_iter and write_iter\n");
578 _debug("file -> %pd positive", dentry);
580 ret = cachefiles_ondemand_init_object(object);
584 ret = cachefiles_check_auxdata(object, file);
588 clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &object->cookie->flags);
592 /* Always update the atime on an object we've just looked up (this is
593 * used to keep track of culling, and atimes are only updated by read,
594 * write and readdir but not lookup or open).
596 touch_atime(&file->f_path);
601 fscache_cookie_lookup_negative(object->cookie);
602 cachefiles_unmark_inode_in_use(object, file);
606 return cachefiles_create_file(object);
612 cachefiles_do_unmark_inode_in_use(object, d_inode(dentry));
618 * walk from the parent object to the child object through the backing
619 * filesystem, creating directories as we go
621 bool cachefiles_look_up_object(struct cachefiles_object *object)
623 struct cachefiles_volume *volume = object->volume;
624 struct dentry *dentry, *fan = volume->fanout[(u8)object->cookie->key_hash];
627 _enter("OBJ%x,%s,", object->debug_id, object->d_name);
629 /* Look up path "cache/vol/fanout/file". */
630 ret = cachefiles_inject_read_error();
632 dentry = lookup_positive_unlocked(object->d_name, fan,
635 dentry = ERR_PTR(ret);
636 trace_cachefiles_lookup(object, fan, dentry);
637 if (IS_ERR(dentry)) {
638 if (dentry == ERR_PTR(-ENOENT))
640 if (dentry == ERR_PTR(-EIO))
641 cachefiles_io_error_obj(object, "Lookup failed");
645 if (!d_is_reg(dentry)) {
646 pr_err("%pd is not a file\n", dentry);
647 inode_lock_nested(d_inode(fan), I_MUTEX_PARENT);
648 ret = cachefiles_bury_object(volume->cache, object, fan, dentry,
649 FSCACHE_OBJECT_IS_WEIRD);
656 if (!cachefiles_open_file(object, dentry))
659 _leave(" = t [%lu]", file_inode(object->file)->i_ino);
663 fscache_cookie_lookup_negative(object->cookie);
664 return cachefiles_create_file(object);
668 * Attempt to link a temporary file into its rightful place in the cache.
670 bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache,
671 struct cachefiles_object *object)
673 struct cachefiles_volume *volume = object->volume;
674 struct dentry *dentry, *fan = volume->fanout[(u8)object->cookie->key_hash];
675 bool success = false;
678 _enter(",%pD", object->file);
680 inode_lock_nested(d_inode(fan), I_MUTEX_PARENT);
681 ret = cachefiles_inject_read_error();
683 dentry = lookup_one_len(object->d_name, fan, object->d_name_len);
685 dentry = ERR_PTR(ret);
686 if (IS_ERR(dentry)) {
687 trace_cachefiles_vfs_error(object, d_inode(fan), PTR_ERR(dentry),
688 cachefiles_trace_lookup_error);
689 _debug("lookup fail %ld", PTR_ERR(dentry));
693 if (!d_is_negative(dentry)) {
694 if (d_backing_inode(dentry) == file_inode(object->file)) {
699 ret = cachefiles_unlink(volume->cache, object, fan, dentry,
700 FSCACHE_OBJECT_IS_STALE);
705 ret = cachefiles_inject_read_error();
707 dentry = lookup_one_len(object->d_name, fan, object->d_name_len);
709 dentry = ERR_PTR(ret);
710 if (IS_ERR(dentry)) {
711 trace_cachefiles_vfs_error(object, d_inode(fan), PTR_ERR(dentry),
712 cachefiles_trace_lookup_error);
713 _debug("lookup fail %ld", PTR_ERR(dentry));
718 ret = cachefiles_inject_read_error();
720 ret = vfs_link(object->file->f_path.dentry, &nop_mnt_idmap,
721 d_inode(fan), dentry, NULL);
723 trace_cachefiles_vfs_error(object, d_inode(fan), ret,
724 cachefiles_trace_link_error);
725 _debug("link fail %d", ret);
727 trace_cachefiles_link(object, file_inode(object->file));
728 spin_lock(&object->lock);
729 /* TODO: Do we want to switch the file pointer to the new dentry? */
730 clear_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags);
731 spin_unlock(&object->lock);
738 inode_unlock(d_inode(fan));
739 _leave(" = %u", success);
744 * Look up an inode to be checked or culled. Return -EBUSY if the inode is
747 static struct dentry *cachefiles_lookup_for_cull(struct cachefiles_cache *cache,
751 struct dentry *victim;
754 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
756 victim = lookup_one_len(filename, dir, strlen(filename));
759 if (d_is_negative(victim))
761 if (d_inode(victim)->i_flags & S_KERNEL_FILE)
768 inode_unlock(d_inode(dir));
773 inode_unlock(d_inode(dir));
774 ret = PTR_ERR(victim);
776 return ERR_PTR(-ESTALE); /* Probably got retired by the netfs */
779 cachefiles_io_error(cache, "Lookup failed");
780 } else if (ret != -ENOMEM) {
781 pr_err("Internal error: %d\n", ret);
789 * Cull an object if it's not in use
790 * - called only by cache manager daemon
792 int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
795 struct dentry *victim;
799 _enter(",%pd/,%s", dir, filename);
801 victim = cachefiles_lookup_for_cull(cache, dir, filename);
803 return PTR_ERR(victim);
805 /* check to see if someone is using this object */
806 inode = d_inode(victim);
808 if (inode->i_flags & S_KERNEL_FILE) {
811 /* Stop the cache from picking it back up */
812 inode->i_flags |= S_KERNEL_FILE;
819 ret = cachefiles_bury_object(cache, NULL, dir, victim,
820 FSCACHE_OBJECT_WAS_CULLED);
824 fscache_count_culled();
830 inode_unlock(d_inode(dir));
834 return -ESTALE; /* Probably got retired by the netfs */
836 if (ret != -ENOMEM) {
837 pr_err("Internal error: %d\n", ret);
841 _leave(" = %d", ret);
846 * Find out if an object is in use or not
847 * - called only by cache manager daemon
848 * - returns -EBUSY or 0 to indicate whether an object is in use or not
850 int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
853 struct dentry *victim;
856 victim = cachefiles_lookup_for_cull(cache, dir, filename);
858 return PTR_ERR(victim);
860 inode_unlock(d_inode(dir));