1 /* CacheFiles path walking and related routines
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/file.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>
25 #define CACHEFILES_KEYBUF_SIZE 512
28 * dump debugging info about an object
31 void __cachefiles_printk_object(struct cachefiles_object *object,
35 struct fscache_cookie *cookie;
36 unsigned keylen, loop;
38 pr_err("%sobject: OBJ%x\n", prefix, object->fscache.debug_id);
39 pr_err("%sobjstate=%s fl=%lx wbusy=%x ev=%lx[%lx]\n",
40 prefix, object->fscache.state->name,
41 object->fscache.flags, work_busy(&object->fscache.work),
42 object->fscache.events, object->fscache.event_mask);
43 pr_err("%sops=%u inp=%u exc=%u\n",
44 prefix, object->fscache.n_ops, object->fscache.n_in_progress,
45 object->fscache.n_exclusive);
46 pr_err("%sparent=%p\n",
47 prefix, object->fscache.parent);
49 spin_lock(&object->fscache.lock);
50 cookie = object->fscache.cookie;
52 pr_err("%scookie=%p [pr=%p nd=%p fl=%lx]\n",
54 object->fscache.cookie,
55 object->fscache.cookie->parent,
56 object->fscache.cookie->netfs_data,
57 object->fscache.cookie->flags);
58 if (keybuf && cookie->def)
59 keylen = cookie->def->get_key(cookie->netfs_data, keybuf,
60 CACHEFILES_KEYBUF_SIZE);
64 pr_err("%scookie=NULL\n", prefix);
67 spin_unlock(&object->fscache.lock);
70 pr_err("%skey=[%u] '", prefix, keylen);
71 for (loop = 0; loop < keylen; loop++)
72 pr_cont("%02x", keybuf[loop]);
78 * dump debugging info about a pair of objects
80 static noinline void cachefiles_printk_object(struct cachefiles_object *object,
81 struct cachefiles_object *xobject)
85 keybuf = kmalloc(CACHEFILES_KEYBUF_SIZE, GFP_NOIO);
87 __cachefiles_printk_object(object, "", keybuf);
89 __cachefiles_printk_object(xobject, "x", keybuf);
94 * mark the owner of a dentry, if there is one, to indicate that that dentry
95 * has been preemptively deleted
96 * - the caller must hold the i_mutex on the dentry's parent as required to
97 * call vfs_unlink(), vfs_rmdir() or vfs_rename()
99 static void cachefiles_mark_object_buried(struct cachefiles_cache *cache,
100 struct dentry *dentry)
102 struct cachefiles_object *object;
105 _enter(",'%pd'", dentry);
107 write_lock(&cache->active_lock);
109 p = cache->active_nodes.rb_node;
111 object = rb_entry(p, struct cachefiles_object, active_node);
112 if (object->dentry > dentry)
114 else if (object->dentry < dentry)
120 write_unlock(&cache->active_lock);
121 _leave(" [no owner]");
124 /* found the dentry for */
126 kdebug("preemptive burial: OBJ%x [%s] %p",
127 object->fscache.debug_id,
128 object->fscache.state->name,
131 if (fscache_object_is_live(&object->fscache)) {
133 pr_err("Error: Can't preemptively bury live object\n");
134 cachefiles_printk_object(object, NULL);
135 } else if (test_and_set_bit(CACHEFILES_OBJECT_BURIED, &object->flags)) {
136 pr_err("Error: Object already preemptively buried\n");
139 write_unlock(&cache->active_lock);
140 _leave(" [owner marked]");
144 * record the fact that an object is now active
146 static int cachefiles_mark_object_active(struct cachefiles_cache *cache,
147 struct cachefiles_object *object)
149 struct cachefiles_object *xobject;
150 struct rb_node **_p, *_parent = NULL;
151 struct dentry *dentry;
153 _enter(",%p", object);
156 write_lock(&cache->active_lock);
158 if (test_and_set_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
159 pr_err("Error: Object already active\n");
160 cachefiles_printk_object(object, NULL);
164 dentry = object->dentry;
165 _p = &cache->active_nodes.rb_node;
168 xobject = rb_entry(_parent,
169 struct cachefiles_object, active_node);
171 ASSERT(xobject != object);
173 if (xobject->dentry > dentry)
174 _p = &(*_p)->rb_left;
175 else if (xobject->dentry < dentry)
176 _p = &(*_p)->rb_right;
178 goto wait_for_old_object;
181 rb_link_node(&object->active_node, _parent, _p);
182 rb_insert_color(&object->active_node, &cache->active_nodes);
184 write_unlock(&cache->active_lock);
188 /* an old object from a previous incarnation is hogging the slot - we
189 * need to wait for it to be destroyed */
191 if (fscache_object_is_live(&xobject->fscache)) {
193 pr_err("Error: Unexpected object collision\n");
194 cachefiles_printk_object(object, xobject);
197 atomic_inc(&xobject->usage);
198 write_unlock(&cache->active_lock);
200 if (test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
201 wait_queue_head_t *wq;
203 signed long timeout = 60 * HZ;
207 /* if the object we're waiting for is queued for processing,
208 * then just put ourselves on the queue behind it */
209 if (work_pending(&xobject->fscache.work)) {
210 _debug("queue OBJ%x behind OBJ%x immediately",
211 object->fscache.debug_id,
212 xobject->fscache.debug_id);
216 /* otherwise we sleep until either the object we're waiting for
217 * is done, or the fscache_object is congested */
218 wq = bit_waitqueue(&xobject->flags, CACHEFILES_OBJECT_ACTIVE);
222 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
223 if (!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags))
226 requeue = fscache_object_sleep_till_congested(&timeout);
227 } while (timeout > 0 && !requeue);
228 finish_wait(wq, &wait);
231 test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
232 _debug("queue OBJ%x behind OBJ%x after wait",
233 object->fscache.debug_id,
234 xobject->fscache.debug_id);
240 pr_err("Error: Overlong wait for old active object to go away\n");
241 cachefiles_printk_object(object, xobject);
246 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags));
248 cache->cache.ops->put_object(&xobject->fscache);
252 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
253 cache->cache.ops->put_object(&xobject->fscache);
254 _leave(" = -ETIMEDOUT");
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
263 * - unlocks the directory mutex
265 static int cachefiles_bury_object(struct cachefiles_cache *cache,
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 _debug("remove %p from %p", rep, dir);
279 /* non-directories can just be unlinked */
280 if (!d_is_dir(rep)) {
281 _debug("unlink stale object");
283 path.mnt = cache->mnt;
285 ret = security_path_unlink(&path, rep);
287 cachefiles_io_error(cache, "Unlink security error");
289 ret = vfs_unlink(dir->d_inode, rep, NULL);
292 cachefiles_mark_object_buried(cache, rep);
295 mutex_unlock(&dir->d_inode->i_mutex);
298 cachefiles_io_error(cache, "Unlink failed");
300 _leave(" = %d", ret);
304 /* directories have to be moved to the graveyard */
305 _debug("move stale object to graveyard");
306 mutex_unlock(&dir->d_inode->i_mutex);
309 /* first step is to make up a grave dentry in the graveyard */
310 sprintf(nbuffer, "%08x%08x",
311 (uint32_t) get_seconds(),
312 (uint32_t) atomic_inc_return(&cache->gravecounter));
314 /* do the multiway lock magic */
315 trap = lock_rename(cache->graveyard, dir);
317 /* do some checks before getting the grave dentry */
318 if (rep->d_parent != dir) {
319 /* the entry was probably culled when we dropped the parent dir
321 unlock_rename(cache->graveyard, dir);
322 _leave(" = 0 [culled?]");
326 if (!d_can_lookup(cache->graveyard)) {
327 unlock_rename(cache->graveyard, dir);
328 cachefiles_io_error(cache, "Graveyard no longer a directory");
333 unlock_rename(cache->graveyard, dir);
334 cachefiles_io_error(cache, "May not make directory loop");
338 if (d_mountpoint(rep)) {
339 unlock_rename(cache->graveyard, dir);
340 cachefiles_io_error(cache, "Mountpoint in cache");
344 grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
346 unlock_rename(cache->graveyard, dir);
348 if (PTR_ERR(grave) == -ENOMEM) {
349 _leave(" = -ENOMEM");
353 cachefiles_io_error(cache, "Lookup error %ld",
358 if (grave->d_inode) {
359 unlock_rename(cache->graveyard, dir);
366 if (d_mountpoint(grave)) {
367 unlock_rename(cache->graveyard, dir);
369 cachefiles_io_error(cache, "Mountpoint in graveyard");
373 /* target should not be an ancestor of source */
375 unlock_rename(cache->graveyard, dir);
377 cachefiles_io_error(cache, "May not make directory loop");
381 /* attempt the rename */
382 path.mnt = cache->mnt;
384 path_to_graveyard.mnt = cache->mnt;
385 path_to_graveyard.dentry = cache->graveyard;
386 ret = security_path_rename(&path, rep, &path_to_graveyard, grave, 0);
388 cachefiles_io_error(cache, "Rename security error %d", ret);
390 ret = vfs_rename(dir->d_inode, rep,
391 cache->graveyard->d_inode, grave, NULL, 0);
392 if (ret != 0 && ret != -ENOMEM)
393 cachefiles_io_error(cache,
394 "Rename failed with error %d", ret);
397 cachefiles_mark_object_buried(cache, rep);
400 unlock_rename(cache->graveyard, dir);
407 * delete an object representation from the cache
409 int cachefiles_delete_object(struct cachefiles_cache *cache,
410 struct cachefiles_object *object)
415 _enter(",OBJ%x{%p}", object->fscache.debug_id, object->dentry);
417 ASSERT(object->dentry);
418 ASSERT(object->dentry->d_inode);
419 ASSERT(object->dentry->d_parent);
421 dir = dget_parent(object->dentry);
423 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
425 if (test_bit(CACHEFILES_OBJECT_BURIED, &object->flags)) {
426 /* object allocation for the same key preemptively deleted this
427 * object's file so that it could create its own file */
428 _debug("object preemptively buried");
429 mutex_unlock(&dir->d_inode->i_mutex);
432 /* we need to check that our parent is _still_ our parent - it
433 * may have been renamed */
434 if (dir == object->dentry->d_parent) {
435 ret = cachefiles_bury_object(cache, dir,
436 object->dentry, false);
438 /* it got moved, presumably by cachefilesd culling it,
439 * so it's no longer in the key path and we can ignore
441 mutex_unlock(&dir->d_inode->i_mutex);
447 _leave(" = %d", ret);
452 * walk from the parent object to the child object through the backing
453 * filesystem, creating directories as we go
455 int cachefiles_walk_to_object(struct cachefiles_object *parent,
456 struct cachefiles_object *object,
458 struct cachefiles_xattr *auxdata)
460 struct cachefiles_cache *cache;
461 struct dentry *dir, *next = NULL;
467 _enter("OBJ%x{%p},OBJ%x,%s,",
468 parent->fscache.debug_id, parent->dentry,
469 object->fscache.debug_id, key);
471 cache = container_of(parent->fscache.cache,
472 struct cachefiles_cache, cache);
473 path.mnt = cache->mnt;
475 ASSERT(parent->dentry);
476 ASSERT(parent->dentry->d_inode);
478 if (!(d_is_dir(parent->dentry))) {
479 // TODO: convert file to dir
480 _leave("looking up in none directory");
484 dir = dget(parent->dentry);
487 /* attempt to transit the first directory component */
491 /* key ends in a double NUL */
492 key = key + nlen + 1;
497 /* search the current directory for the element name */
498 _debug("lookup '%s'", name);
500 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
503 next = lookup_one_len(name, dir, nlen);
504 cachefiles_hist(cachefiles_lookup_histogram, start);
508 _debug("next -> %p %s", next, next->d_inode ? "positive" : "negative");
511 object->new = !next->d_inode;
513 /* if this element of the path doesn't exist, then the lookup phase
514 * failed, and we can release any readers in the certain knowledge that
515 * there's nothing for them to actually read */
517 fscache_object_lookup_negative(&object->fscache);
519 /* we need to create the object if it's negative */
520 if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
521 /* index objects and intervening tree levels must be subdirs */
522 if (!next->d_inode) {
523 ret = cachefiles_has_space(cache, 1, 0);
528 ret = security_path_mkdir(&path, next, 0);
532 ret = vfs_mkdir(dir->d_inode, next, 0);
533 cachefiles_hist(cachefiles_mkdir_histogram, start);
537 ASSERT(next->d_inode);
539 _debug("mkdir -> %p{%p{ino=%lu}}",
540 next, next->d_inode, next->d_inode->i_ino);
542 } else if (!d_can_lookup(next)) {
543 pr_err("inode %lu is not a directory\n",
544 next->d_inode->i_ino);
550 /* non-index objects start out life as files */
551 if (!next->d_inode) {
552 ret = cachefiles_has_space(cache, 1, 0);
557 ret = security_path_mknod(&path, next, S_IFREG, 0);
561 ret = vfs_create(dir->d_inode, next, S_IFREG, true);
562 cachefiles_hist(cachefiles_create_histogram, start);
566 ASSERT(next->d_inode);
568 _debug("create -> %p{%p{ino=%lu}}",
569 next, next->d_inode, next->d_inode->i_ino);
571 } else if (!d_can_lookup(next) &&
574 pr_err("inode %lu is not a file or directory\n",
575 next->d_inode->i_ino);
581 /* process the next component */
584 mutex_unlock(&dir->d_inode->i_mutex);
591 /* we've found the object we were looking for */
592 object->dentry = next;
594 /* if we've found that the terminal object exists, then we need to
595 * check its attributes and delete it if it's out of date */
597 _debug("validate '%pd'", next);
599 ret = cachefiles_check_object_xattr(object, auxdata);
600 if (ret == -ESTALE) {
601 /* delete the object (the deleter drops the directory
603 object->dentry = NULL;
605 ret = cachefiles_bury_object(cache, dir, next, true);
612 _debug("redo lookup");
617 /* note that we're now using this object */
618 ret = cachefiles_mark_object_active(cache, object);
620 mutex_unlock(&dir->d_inode->i_mutex);
624 if (ret == -ETIMEDOUT)
625 goto mark_active_timed_out;
627 _debug("=== OBTAINED_OBJECT ===");
630 /* attach data to a newly constructed terminal object */
631 ret = cachefiles_set_object_xattr(object, auxdata);
635 /* always update the atime on an object we've just looked up
636 * (this is used to keep track of culling, and atimes are only
637 * updated by read, write and readdir but not lookup or
643 /* open a file interface onto a data file */
644 if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
645 if (d_is_reg(object->dentry)) {
646 const struct address_space_operations *aops;
649 aops = object->dentry->d_inode->i_mapping->a_ops;
653 object->backer = object->dentry;
655 BUG(); // TODO: open file in data-class subdir
660 fscache_obtained_object(&object->fscache);
662 _leave(" = 0 [%lu]", object->dentry->d_inode->i_ino);
666 _debug("create error %d", ret);
668 cachefiles_io_error(cache, "Create/mkdir failed");
671 mark_active_timed_out:
672 _debug("mark active timed out");
676 _debug("check error %d", ret);
677 write_lock(&cache->active_lock);
678 rb_erase(&object->active_node, &cache->active_nodes);
679 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
680 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
681 write_unlock(&cache->active_lock);
683 dput(object->dentry);
684 object->dentry = NULL;
688 _debug("delete error %d", ret);
692 _debug("lookup error %ld", PTR_ERR(next));
695 cachefiles_io_error(cache, "Lookup failed");
698 mutex_unlock(&dir->d_inode->i_mutex);
703 _leave(" = error %d", -ret);
710 struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
714 struct dentry *subdir;
719 _enter(",,%s", dirname);
721 /* search the current directory for the element name */
722 mutex_lock(&dir->d_inode->i_mutex);
725 subdir = lookup_one_len(dirname, dir, strlen(dirname));
726 cachefiles_hist(cachefiles_lookup_histogram, start);
727 if (IS_ERR(subdir)) {
728 if (PTR_ERR(subdir) == -ENOMEM)
733 _debug("subdir -> %p %s",
734 subdir, subdir->d_inode ? "positive" : "negative");
736 /* we need to create the subdir if it doesn't exist yet */
737 if (!subdir->d_inode) {
738 ret = cachefiles_has_space(cache, 1, 0);
742 _debug("attempt mkdir");
744 path.mnt = cache->mnt;
746 ret = security_path_mkdir(&path, subdir, 0700);
749 ret = vfs_mkdir(dir->d_inode, subdir, 0700);
753 ASSERT(subdir->d_inode);
755 _debug("mkdir -> %p{%p{ino=%lu}}",
758 subdir->d_inode->i_ino);
761 mutex_unlock(&dir->d_inode->i_mutex);
763 /* we need to make sure the subdir is a directory */
764 ASSERT(subdir->d_inode);
766 if (!d_can_lookup(subdir)) {
767 pr_err("%s is not a directory\n", dirname);
773 if (!subdir->d_inode->i_op->setxattr ||
774 !subdir->d_inode->i_op->getxattr ||
775 !subdir->d_inode->i_op->lookup ||
776 !subdir->d_inode->i_op->mkdir ||
777 !subdir->d_inode->i_op->create ||
778 (!subdir->d_inode->i_op->rename &&
779 !subdir->d_inode->i_op->rename2) ||
780 !subdir->d_inode->i_op->rmdir ||
781 !subdir->d_inode->i_op->unlink)
784 _leave(" = [%lu]", subdir->d_inode->i_ino);
789 _leave(" = %d [check]", ret);
793 mutex_unlock(&dir->d_inode->i_mutex);
795 pr_err("mkdir %s failed with error %d\n", dirname, ret);
799 mutex_unlock(&dir->d_inode->i_mutex);
800 ret = PTR_ERR(subdir);
801 pr_err("Lookup %s failed with error %d\n", dirname, ret);
805 mutex_unlock(&dir->d_inode->i_mutex);
806 _leave(" = -ENOMEM");
807 return ERR_PTR(-ENOMEM);
811 * find out if an object is in use or not
812 * - if finds object and it's not in use:
813 * - returns a pointer to the object and a reference on it
814 * - returns with the directory locked
816 static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
820 struct cachefiles_object *object;
822 struct dentry *victim;
829 /* look up the victim */
830 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
833 victim = lookup_one_len(filename, dir, strlen(filename));
834 cachefiles_hist(cachefiles_lookup_histogram, start);
838 //_debug("victim -> %p %s",
839 // victim, victim->d_inode ? "positive" : "negative");
841 /* if the object is no longer there then we probably retired the object
842 * at the netfs's request whilst the cull was in progress
844 if (!victim->d_inode) {
845 mutex_unlock(&dir->d_inode->i_mutex);
847 _leave(" = -ENOENT [absent]");
848 return ERR_PTR(-ENOENT);
851 /* check to see if we're using this object */
852 read_lock(&cache->active_lock);
854 _n = cache->active_nodes.rb_node;
857 object = rb_entry(_n, struct cachefiles_object, active_node);
859 if (object->dentry > victim)
861 else if (object->dentry < victim)
867 read_unlock(&cache->active_lock);
869 //_leave(" = %p", victim);
873 read_unlock(&cache->active_lock);
874 mutex_unlock(&dir->d_inode->i_mutex);
876 //_leave(" = -EBUSY [in use]");
877 return ERR_PTR(-EBUSY);
880 mutex_unlock(&dir->d_inode->i_mutex);
881 ret = PTR_ERR(victim);
882 if (ret == -ENOENT) {
883 /* file or dir now absent - probably retired by netfs */
884 _leave(" = -ESTALE [absent]");
885 return ERR_PTR(-ESTALE);
889 cachefiles_io_error(cache, "Lookup failed");
890 } else if (ret != -ENOMEM) {
891 pr_err("Internal error: %d\n", ret);
895 _leave(" = %d", ret);
900 * cull an object if it's not in use
901 * - called only by cache manager daemon
903 int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
906 struct dentry *victim;
909 _enter(",%pd/,%s", dir, filename);
911 victim = cachefiles_check_active(cache, dir, filename);
913 return PTR_ERR(victim);
915 _debug("victim -> %p %s",
916 victim, victim->d_inode ? "positive" : "negative");
918 /* okay... the victim is not being used so we can cull it
919 * - start by marking it as stale
921 _debug("victim is cullable");
923 ret = cachefiles_remove_object_xattr(cache, victim);
927 /* actually remove the victim (drops the dir mutex) */
930 ret = cachefiles_bury_object(cache, dir, victim, false);
939 mutex_unlock(&dir->d_inode->i_mutex);
942 if (ret == -ENOENT) {
943 /* file or dir now absent - probably retired by netfs */
944 _leave(" = -ESTALE [absent]");
948 if (ret != -ENOMEM) {
949 pr_err("Internal error: %d\n", ret);
953 _leave(" = %d", ret);
958 * find out if an object is in use or not
959 * - called only by cache manager daemon
960 * - returns -EBUSY or 0 to indicate whether an object is in use or not
962 int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
965 struct dentry *victim;
970 victim = cachefiles_check_active(cache, dir, filename);
972 return PTR_ERR(victim);
974 mutex_unlock(&dir->d_inode->i_mutex);