1 /* CacheFiles path walking and related routines
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
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 printk(KERN_ERR "%sobject: OBJ%x\n",
39 prefix, object->fscache.debug_id);
40 printk(KERN_ERR "%sobjstate=%s fl=%lx wbusy=%x ev=%lx[%lx]\n",
41 prefix, fscache_object_states[object->fscache.state],
42 object->fscache.flags, work_busy(&object->fscache.work),
43 object->fscache.events,
44 object->fscache.event_mask & FSCACHE_OBJECT_EVENTS_MASK);
45 printk(KERN_ERR "%sops=%u inp=%u exc=%u\n",
46 prefix, object->fscache.n_ops, object->fscache.n_in_progress,
47 object->fscache.n_exclusive);
48 printk(KERN_ERR "%sparent=%p\n",
49 prefix, object->fscache.parent);
51 spin_lock(&object->fscache.lock);
52 cookie = object->fscache.cookie;
54 printk(KERN_ERR "%scookie=%p [pr=%p nd=%p fl=%lx]\n",
56 object->fscache.cookie,
57 object->fscache.cookie->parent,
58 object->fscache.cookie->netfs_data,
59 object->fscache.cookie->flags);
61 keylen = cookie->def->get_key(cookie->netfs_data, keybuf,
62 CACHEFILES_KEYBUF_SIZE);
66 printk(KERN_ERR "%scookie=NULL\n", prefix);
69 spin_unlock(&object->fscache.lock);
72 printk(KERN_ERR "%skey=[%u] '", prefix, keylen);
73 for (loop = 0; loop < keylen; loop++)
74 printk("%02x", keybuf[loop]);
80 * dump debugging info about a pair of objects
82 static noinline void cachefiles_printk_object(struct cachefiles_object *object,
83 struct cachefiles_object *xobject)
87 keybuf = kmalloc(CACHEFILES_KEYBUF_SIZE, GFP_NOIO);
89 __cachefiles_printk_object(object, "", keybuf);
91 __cachefiles_printk_object(xobject, "x", keybuf);
96 * mark the owner of a dentry, if there is one, to indicate that that dentry
97 * has been preemptively deleted
98 * - the caller must hold the i_mutex on the dentry's parent as required to
99 * call vfs_unlink(), vfs_rmdir() or vfs_rename()
101 static void cachefiles_mark_object_buried(struct cachefiles_cache *cache,
102 struct dentry *dentry)
104 struct cachefiles_object *object;
108 dentry->d_name.len, dentry->d_name.len, dentry->d_name.name);
110 write_lock(&cache->active_lock);
112 p = cache->active_nodes.rb_node;
114 object = rb_entry(p, struct cachefiles_object, active_node);
115 if (object->dentry > dentry)
117 else if (object->dentry < dentry)
123 write_unlock(&cache->active_lock);
124 _leave(" [no owner]");
127 /* found the dentry for */
129 kdebug("preemptive burial: OBJ%x [%s] %p",
130 object->fscache.debug_id,
131 fscache_object_states[object->fscache.state],
134 if (object->fscache.state < FSCACHE_OBJECT_DYING) {
135 printk(KERN_ERR "\n");
136 printk(KERN_ERR "CacheFiles: Error:"
137 " Can't preemptively bury live object\n");
138 cachefiles_printk_object(object, NULL);
139 } else if (test_and_set_bit(CACHEFILES_OBJECT_BURIED, &object->flags)) {
140 printk(KERN_ERR "CacheFiles: Error:"
141 " Object already preemptively buried\n");
144 write_unlock(&cache->active_lock);
145 _leave(" [owner marked]");
149 * record the fact that an object is now active
151 static int cachefiles_mark_object_active(struct cachefiles_cache *cache,
152 struct cachefiles_object *object)
154 struct cachefiles_object *xobject;
155 struct rb_node **_p, *_parent = NULL;
156 struct dentry *dentry;
158 _enter(",%p", object);
161 write_lock(&cache->active_lock);
163 if (test_and_set_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
164 printk(KERN_ERR "CacheFiles: Error: Object already active\n");
165 cachefiles_printk_object(object, NULL);
169 dentry = object->dentry;
170 _p = &cache->active_nodes.rb_node;
173 xobject = rb_entry(_parent,
174 struct cachefiles_object, active_node);
176 ASSERT(xobject != object);
178 if (xobject->dentry > dentry)
179 _p = &(*_p)->rb_left;
180 else if (xobject->dentry < dentry)
181 _p = &(*_p)->rb_right;
183 goto wait_for_old_object;
186 rb_link_node(&object->active_node, _parent, _p);
187 rb_insert_color(&object->active_node, &cache->active_nodes);
189 write_unlock(&cache->active_lock);
193 /* an old object from a previous incarnation is hogging the slot - we
194 * need to wait for it to be destroyed */
196 if (xobject->fscache.state < FSCACHE_OBJECT_DYING) {
197 printk(KERN_ERR "\n");
198 printk(KERN_ERR "CacheFiles: Error:"
199 " Unexpected object collision\n");
200 cachefiles_printk_object(object, xobject);
203 atomic_inc(&xobject->usage);
204 write_unlock(&cache->active_lock);
206 if (test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
207 wait_queue_head_t *wq;
209 signed long timeout = 60 * HZ;
213 /* if the object we're waiting for is queued for processing,
214 * then just put ourselves on the queue behind it */
215 if (work_pending(&xobject->fscache.work)) {
216 _debug("queue OBJ%x behind OBJ%x immediately",
217 object->fscache.debug_id,
218 xobject->fscache.debug_id);
222 /* otherwise we sleep until either the object we're waiting for
223 * is done, or the fscache_object is congested */
224 wq = bit_waitqueue(&xobject->flags, CACHEFILES_OBJECT_ACTIVE);
228 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
229 if (!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags))
232 requeue = fscache_object_sleep_till_congested(&timeout);
233 } while (timeout > 0 && !requeue);
234 finish_wait(wq, &wait);
237 test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
238 _debug("queue OBJ%x behind OBJ%x after wait",
239 object->fscache.debug_id,
240 xobject->fscache.debug_id);
245 printk(KERN_ERR "\n");
246 printk(KERN_ERR "CacheFiles: Error: Overlong"
247 " wait for old active object to go away\n");
248 cachefiles_printk_object(object, xobject);
253 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags));
255 cache->cache.ops->put_object(&xobject->fscache);
259 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
260 cache->cache.ops->put_object(&xobject->fscache);
261 _leave(" = -ETIMEDOUT");
266 * delete an object representation from the cache
267 * - file backed objects are unlinked
268 * - directory backed objects are stuffed into the graveyard for userspace to
270 * - unlocks the directory mutex
272 static int cachefiles_bury_object(struct cachefiles_cache *cache,
277 struct dentry *grave, *trap;
278 char nbuffer[8 + 8 + 1];
281 _enter(",'%*.*s','%*.*s'",
282 dir->d_name.len, dir->d_name.len, dir->d_name.name,
283 rep->d_name.len, rep->d_name.len, rep->d_name.name);
285 _debug("remove %p from %p", rep, dir);
287 /* non-directories can just be unlinked */
288 if (!S_ISDIR(rep->d_inode->i_mode)) {
289 _debug("unlink stale object");
290 ret = vfs_unlink(dir->d_inode, rep);
293 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 (!S_ISDIR(cache->graveyard->d_inode->i_mode)) {
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 ret = vfs_rename(dir->d_inode, rep, cache->graveyard->d_inode, grave);
383 if (ret != 0 && ret != -ENOMEM)
384 cachefiles_io_error(cache, "Rename failed with error %d", ret);
387 cachefiles_mark_object_buried(cache, rep);
389 unlock_rename(cache->graveyard, dir);
396 * delete an object representation from the cache
398 int cachefiles_delete_object(struct cachefiles_cache *cache,
399 struct cachefiles_object *object)
404 _enter(",OBJ%x{%p}", object->fscache.debug_id, object->dentry);
406 ASSERT(object->dentry);
407 ASSERT(object->dentry->d_inode);
408 ASSERT(object->dentry->d_parent);
410 dir = dget_parent(object->dentry);
412 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
414 if (test_bit(CACHEFILES_OBJECT_BURIED, &object->flags)) {
415 /* object allocation for the same key preemptively deleted this
416 * object's file so that it could create its own file */
417 _debug("object preemptively buried");
418 mutex_unlock(&dir->d_inode->i_mutex);
421 /* we need to check that our parent is _still_ our parent - it
422 * may have been renamed */
423 if (dir == object->dentry->d_parent) {
424 ret = cachefiles_bury_object(cache, dir,
425 object->dentry, false);
427 /* it got moved, presumably by cachefilesd culling it,
428 * so it's no longer in the key path and we can ignore
430 mutex_unlock(&dir->d_inode->i_mutex);
436 _leave(" = %d", ret);
441 * walk from the parent object to the child object through the backing
442 * filesystem, creating directories as we go
444 int cachefiles_walk_to_object(struct cachefiles_object *parent,
445 struct cachefiles_object *object,
447 struct cachefiles_xattr *auxdata)
449 struct cachefiles_cache *cache;
450 struct dentry *dir, *next = NULL;
455 _enter("OBJ%x{%p},OBJ%x,%s,",
456 parent->fscache.debug_id, parent->dentry,
457 object->fscache.debug_id, key);
459 cache = container_of(parent->fscache.cache,
460 struct cachefiles_cache, cache);
462 ASSERT(parent->dentry);
463 ASSERT(parent->dentry->d_inode);
465 if (!(S_ISDIR(parent->dentry->d_inode->i_mode))) {
466 // TODO: convert file to dir
467 _leave("looking up in none directory");
471 dir = dget(parent->dentry);
474 /* attempt to transit the first directory component */
478 /* key ends in a double NUL */
479 key = key + nlen + 1;
484 /* search the current directory for the element name */
485 _debug("lookup '%s'", name);
487 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
490 next = lookup_one_len(name, dir, nlen);
491 cachefiles_hist(cachefiles_lookup_histogram, start);
495 _debug("next -> %p %s", next, next->d_inode ? "positive" : "negative");
498 object->new = !next->d_inode;
500 /* if this element of the path doesn't exist, then the lookup phase
501 * failed, and we can release any readers in the certain knowledge that
502 * there's nothing for them to actually read */
504 fscache_object_lookup_negative(&object->fscache);
506 /* we need to create the object if it's negative */
507 if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
508 /* index objects and intervening tree levels must be subdirs */
509 if (!next->d_inode) {
510 ret = cachefiles_has_space(cache, 1, 0);
515 ret = vfs_mkdir(dir->d_inode, next, 0);
516 cachefiles_hist(cachefiles_mkdir_histogram, start);
520 ASSERT(next->d_inode);
522 _debug("mkdir -> %p{%p{ino=%lu}}",
523 next, next->d_inode, next->d_inode->i_ino);
525 } else if (!S_ISDIR(next->d_inode->i_mode)) {
526 kerror("inode %lu is not a directory",
527 next->d_inode->i_ino);
533 /* non-index objects start out life as files */
534 if (!next->d_inode) {
535 ret = cachefiles_has_space(cache, 1, 0);
540 ret = vfs_create(dir->d_inode, next, S_IFREG, NULL);
541 cachefiles_hist(cachefiles_create_histogram, start);
545 ASSERT(next->d_inode);
547 _debug("create -> %p{%p{ino=%lu}}",
548 next, next->d_inode, next->d_inode->i_ino);
550 } else if (!S_ISDIR(next->d_inode->i_mode) &&
551 !S_ISREG(next->d_inode->i_mode)
553 kerror("inode %lu is not a file or directory",
554 next->d_inode->i_ino);
560 /* process the next component */
563 mutex_unlock(&dir->d_inode->i_mutex);
570 /* we've found the object we were looking for */
571 object->dentry = next;
573 /* if we've found that the terminal object exists, then we need to
574 * check its attributes and delete it if it's out of date */
576 _debug("validate '%*.*s'",
577 next->d_name.len, next->d_name.len, next->d_name.name);
579 ret = cachefiles_check_object_xattr(object, auxdata);
580 if (ret == -ESTALE) {
581 /* delete the object (the deleter drops the directory
583 object->dentry = NULL;
585 ret = cachefiles_bury_object(cache, dir, next, true);
592 _debug("redo lookup");
597 /* note that we're now using this object */
598 ret = cachefiles_mark_object_active(cache, object);
600 mutex_unlock(&dir->d_inode->i_mutex);
604 if (ret == -ETIMEDOUT)
605 goto mark_active_timed_out;
607 _debug("=== OBTAINED_OBJECT ===");
610 /* attach data to a newly constructed terminal object */
611 ret = cachefiles_set_object_xattr(object, auxdata);
615 /* always update the atime on an object we've just looked up
616 * (this is used to keep track of culling, and atimes are only
617 * updated by read, write and readdir but not lookup or
619 touch_atime(cache->mnt, next);
622 /* open a file interface onto a data file */
623 if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
624 if (S_ISREG(object->dentry->d_inode->i_mode)) {
625 const struct address_space_operations *aops;
628 aops = object->dentry->d_inode->i_mapping->a_ops;
632 object->backer = object->dentry;
634 BUG(); // TODO: open file in data-class subdir
639 fscache_obtained_object(&object->fscache);
641 _leave(" = 0 [%lu]", object->dentry->d_inode->i_ino);
645 _debug("create error %d", ret);
647 cachefiles_io_error(cache, "Create/mkdir failed");
650 mark_active_timed_out:
651 _debug("mark active timed out");
655 _debug("check error %d", ret);
656 write_lock(&cache->active_lock);
657 rb_erase(&object->active_node, &cache->active_nodes);
658 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
659 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
660 write_unlock(&cache->active_lock);
662 dput(object->dentry);
663 object->dentry = NULL;
667 _debug("delete error %d", ret);
671 _debug("lookup error %ld", PTR_ERR(next));
674 cachefiles_io_error(cache, "Lookup failed");
677 mutex_unlock(&dir->d_inode->i_mutex);
682 _leave(" = error %d", -ret);
689 struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
693 struct dentry *subdir;
697 _enter(",,%s", dirname);
699 /* search the current directory for the element name */
700 mutex_lock(&dir->d_inode->i_mutex);
703 subdir = lookup_one_len(dirname, dir, strlen(dirname));
704 cachefiles_hist(cachefiles_lookup_histogram, start);
705 if (IS_ERR(subdir)) {
706 if (PTR_ERR(subdir) == -ENOMEM)
711 _debug("subdir -> %p %s",
712 subdir, subdir->d_inode ? "positive" : "negative");
714 /* we need to create the subdir if it doesn't exist yet */
715 if (!subdir->d_inode) {
716 ret = cachefiles_has_space(cache, 1, 0);
720 _debug("attempt mkdir");
722 ret = vfs_mkdir(dir->d_inode, subdir, 0700);
726 ASSERT(subdir->d_inode);
728 _debug("mkdir -> %p{%p{ino=%lu}}",
731 subdir->d_inode->i_ino);
734 mutex_unlock(&dir->d_inode->i_mutex);
736 /* we need to make sure the subdir is a directory */
737 ASSERT(subdir->d_inode);
739 if (!S_ISDIR(subdir->d_inode->i_mode)) {
740 kerror("%s is not a directory", dirname);
746 if (!subdir->d_inode->i_op ||
747 !subdir->d_inode->i_op->setxattr ||
748 !subdir->d_inode->i_op->getxattr ||
749 !subdir->d_inode->i_op->lookup ||
750 !subdir->d_inode->i_op->mkdir ||
751 !subdir->d_inode->i_op->create ||
752 !subdir->d_inode->i_op->rename ||
753 !subdir->d_inode->i_op->rmdir ||
754 !subdir->d_inode->i_op->unlink)
757 _leave(" = [%lu]", subdir->d_inode->i_ino);
762 _leave(" = %d [check]", ret);
766 mutex_unlock(&dir->d_inode->i_mutex);
768 kerror("mkdir %s failed with error %d", dirname, ret);
772 mutex_unlock(&dir->d_inode->i_mutex);
773 ret = PTR_ERR(subdir);
774 kerror("Lookup %s failed with error %d", dirname, ret);
778 mutex_unlock(&dir->d_inode->i_mutex);
779 _leave(" = -ENOMEM");
780 return ERR_PTR(-ENOMEM);
784 * find out if an object is in use or not
785 * - if finds object and it's not in use:
786 * - returns a pointer to the object and a reference on it
787 * - returns with the directory locked
789 static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
793 struct cachefiles_object *object;
795 struct dentry *victim;
799 //_enter(",%*.*s/,%s",
800 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
802 /* look up the victim */
803 mutex_lock_nested(&dir->d_inode->i_mutex, 1);
806 victim = lookup_one_len(filename, dir, strlen(filename));
807 cachefiles_hist(cachefiles_lookup_histogram, start);
811 //_debug("victim -> %p %s",
812 // victim, victim->d_inode ? "positive" : "negative");
814 /* if the object is no longer there then we probably retired the object
815 * at the netfs's request whilst the cull was in progress
817 if (!victim->d_inode) {
818 mutex_unlock(&dir->d_inode->i_mutex);
820 _leave(" = -ENOENT [absent]");
821 return ERR_PTR(-ENOENT);
824 /* check to see if we're using this object */
825 read_lock(&cache->active_lock);
827 _n = cache->active_nodes.rb_node;
830 object = rb_entry(_n, struct cachefiles_object, active_node);
832 if (object->dentry > victim)
834 else if (object->dentry < victim)
840 read_unlock(&cache->active_lock);
842 //_leave(" = %p", victim);
846 read_unlock(&cache->active_lock);
847 mutex_unlock(&dir->d_inode->i_mutex);
849 //_leave(" = -EBUSY [in use]");
850 return ERR_PTR(-EBUSY);
853 mutex_unlock(&dir->d_inode->i_mutex);
854 ret = PTR_ERR(victim);
855 if (ret == -ENOENT) {
856 /* file or dir now absent - probably retired by netfs */
857 _leave(" = -ESTALE [absent]");
858 return ERR_PTR(-ESTALE);
862 cachefiles_io_error(cache, "Lookup failed");
863 } else if (ret != -ENOMEM) {
864 kerror("Internal error: %d", ret);
868 _leave(" = %d", ret);
873 * cull an object if it's not in use
874 * - called only by cache manager daemon
876 int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
879 struct dentry *victim;
883 dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
885 victim = cachefiles_check_active(cache, dir, filename);
887 return PTR_ERR(victim);
889 _debug("victim -> %p %s",
890 victim, victim->d_inode ? "positive" : "negative");
892 /* okay... the victim is not being used so we can cull it
893 * - start by marking it as stale
895 _debug("victim is cullable");
897 ret = cachefiles_remove_object_xattr(cache, victim);
901 /* actually remove the victim (drops the dir mutex) */
904 ret = cachefiles_bury_object(cache, dir, victim, false);
913 mutex_unlock(&dir->d_inode->i_mutex);
916 if (ret == -ENOENT) {
917 /* file or dir now absent - probably retired by netfs */
918 _leave(" = -ESTALE [absent]");
922 if (ret != -ENOMEM) {
923 kerror("Internal error: %d", ret);
927 _leave(" = %d", ret);
932 * find out if an object is in use or not
933 * - called only by cache manager daemon
934 * - returns -EBUSY or 0 to indicate whether an object is in use or not
936 int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
939 struct dentry *victim;
941 //_enter(",%*.*s/,%s",
942 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
944 victim = cachefiles_check_active(cache, dir, filename);
946 return PTR_ERR(victim);
948 mutex_unlock(&dir->d_inode->i_mutex);