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>
24 static int cachefiles_wait_bit(void *flags)
30 #define CACHEFILES_KEYBUF_SIZE 512
33 * dump debugging info about an object
36 void __cachefiles_printk_object(struct cachefiles_object *object,
40 struct fscache_cookie *cookie;
41 unsigned keylen, loop;
43 printk(KERN_ERR "%sobject: OBJ%x\n",
44 prefix, object->fscache.debug_id);
45 printk(KERN_ERR "%sobjstate=%s fl=%lx swfl=%lx ev=%lx[%lx]\n",
46 prefix, fscache_object_states[object->fscache.state],
47 object->fscache.flags, object->fscache.work.flags,
48 object->fscache.events,
49 object->fscache.event_mask & FSCACHE_OBJECT_EVENTS_MASK);
50 printk(KERN_ERR "%sops=%u inp=%u exc=%u\n",
51 prefix, object->fscache.n_ops, object->fscache.n_in_progress,
52 object->fscache.n_exclusive);
53 printk(KERN_ERR "%sparent=%p\n",
54 prefix, object->fscache.parent);
56 spin_lock(&object->fscache.lock);
57 cookie = object->fscache.cookie;
59 printk(KERN_ERR "%scookie=%p [pr=%p nd=%p fl=%lx]\n",
61 object->fscache.cookie,
62 object->fscache.cookie->parent,
63 object->fscache.cookie->netfs_data,
64 object->fscache.cookie->flags);
66 keylen = cookie->def->get_key(cookie->netfs_data, keybuf,
67 CACHEFILES_KEYBUF_SIZE);
71 printk(KERN_ERR "%scookie=NULL\n", prefix);
74 spin_unlock(&object->fscache.lock);
77 printk(KERN_ERR "%skey=[%u] '", prefix, keylen);
78 for (loop = 0; loop < keylen; loop++)
79 printk("%02x", keybuf[loop]);
85 * dump debugging info about a pair of objects
87 static noinline void cachefiles_printk_object(struct cachefiles_object *object,
88 struct cachefiles_object *xobject)
92 keybuf = kmalloc(CACHEFILES_KEYBUF_SIZE, GFP_NOIO);
94 __cachefiles_printk_object(object, "", keybuf);
96 __cachefiles_printk_object(xobject, "x", keybuf);
101 * record the fact that an object is now active
103 static void cachefiles_mark_object_active(struct cachefiles_cache *cache,
104 struct cachefiles_object *object)
106 struct cachefiles_object *xobject;
107 struct rb_node **_p, *_parent = NULL;
108 struct dentry *dentry;
110 _enter(",%p", object);
113 write_lock(&cache->active_lock);
115 if (test_and_set_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
116 printk(KERN_ERR "CacheFiles: Error: Object already active\n");
117 cachefiles_printk_object(object, NULL);
121 dentry = object->dentry;
122 _p = &cache->active_nodes.rb_node;
125 xobject = rb_entry(_parent,
126 struct cachefiles_object, active_node);
128 ASSERT(xobject != object);
130 if (xobject->dentry > dentry)
131 _p = &(*_p)->rb_left;
132 else if (xobject->dentry < dentry)
133 _p = &(*_p)->rb_right;
135 goto wait_for_old_object;
138 rb_link_node(&object->active_node, _parent, _p);
139 rb_insert_color(&object->active_node, &cache->active_nodes);
141 write_unlock(&cache->active_lock);
145 /* an old object from a previous incarnation is hogging the slot - we
146 * need to wait for it to be destroyed */
148 if (xobject->fscache.state < FSCACHE_OBJECT_DYING) {
149 printk(KERN_ERR "\n");
150 printk(KERN_ERR "CacheFiles: Error:"
151 " Unexpected object collision\n");
152 cachefiles_printk_object(object, xobject);
155 atomic_inc(&xobject->usage);
156 write_unlock(&cache->active_lock);
159 wait_on_bit(&xobject->flags, CACHEFILES_OBJECT_ACTIVE,
160 cachefiles_wait_bit, TASK_UNINTERRUPTIBLE);
161 _debug("<<< waited");
163 cache->cache.ops->put_object(&xobject->fscache);
168 * delete an object representation from the cache
169 * - file backed objects are unlinked
170 * - directory backed objects are stuffed into the graveyard for userspace to
172 * - unlocks the directory mutex
174 static int cachefiles_bury_object(struct cachefiles_cache *cache,
178 struct dentry *grave, *trap;
179 char nbuffer[8 + 8 + 1];
182 _enter(",'%*.*s','%*.*s'",
183 dir->d_name.len, dir->d_name.len, dir->d_name.name,
184 rep->d_name.len, rep->d_name.len, rep->d_name.name);
186 /* non-directories can just be unlinked */
187 if (!S_ISDIR(rep->d_inode->i_mode)) {
188 _debug("unlink stale object");
189 ret = vfs_unlink(dir->d_inode, rep);
191 mutex_unlock(&dir->d_inode->i_mutex);
194 cachefiles_io_error(cache, "Unlink failed");
196 _leave(" = %d", ret);
200 /* directories have to be moved to the graveyard */
201 _debug("move stale object to graveyard");
202 mutex_unlock(&dir->d_inode->i_mutex);
205 /* first step is to make up a grave dentry in the graveyard */
206 sprintf(nbuffer, "%08x%08x",
207 (uint32_t) get_seconds(),
208 (uint32_t) atomic_inc_return(&cache->gravecounter));
210 /* do the multiway lock magic */
211 trap = lock_rename(cache->graveyard, dir);
213 /* do some checks before getting the grave dentry */
214 if (rep->d_parent != dir) {
215 /* the entry was probably culled when we dropped the parent dir
217 unlock_rename(cache->graveyard, dir);
218 _leave(" = 0 [culled?]");
222 if (!S_ISDIR(cache->graveyard->d_inode->i_mode)) {
223 unlock_rename(cache->graveyard, dir);
224 cachefiles_io_error(cache, "Graveyard no longer a directory");
229 unlock_rename(cache->graveyard, dir);
230 cachefiles_io_error(cache, "May not make directory loop");
234 if (d_mountpoint(rep)) {
235 unlock_rename(cache->graveyard, dir);
236 cachefiles_io_error(cache, "Mountpoint in cache");
240 grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
242 unlock_rename(cache->graveyard, dir);
244 if (PTR_ERR(grave) == -ENOMEM) {
245 _leave(" = -ENOMEM");
249 cachefiles_io_error(cache, "Lookup error %ld",
254 if (grave->d_inode) {
255 unlock_rename(cache->graveyard, dir);
262 if (d_mountpoint(grave)) {
263 unlock_rename(cache->graveyard, dir);
265 cachefiles_io_error(cache, "Mountpoint in graveyard");
269 /* target should not be an ancestor of source */
271 unlock_rename(cache->graveyard, dir);
273 cachefiles_io_error(cache, "May not make directory loop");
277 /* attempt the rename */
278 ret = vfs_rename(dir->d_inode, rep, cache->graveyard->d_inode, grave);
279 if (ret != 0 && ret != -ENOMEM)
280 cachefiles_io_error(cache, "Rename failed with error %d", ret);
282 unlock_rename(cache->graveyard, dir);
289 * delete an object representation from the cache
291 int cachefiles_delete_object(struct cachefiles_cache *cache,
292 struct cachefiles_object *object)
297 _enter(",{%p}", object->dentry);
299 ASSERT(object->dentry);
300 ASSERT(object->dentry->d_inode);
301 ASSERT(object->dentry->d_parent);
303 dir = dget_parent(object->dentry);
305 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
306 ret = cachefiles_bury_object(cache, dir, object->dentry);
309 _leave(" = %d", ret);
314 * walk from the parent object to the child object through the backing
315 * filesystem, creating directories as we go
317 int cachefiles_walk_to_object(struct cachefiles_object *parent,
318 struct cachefiles_object *object,
320 struct cachefiles_xattr *auxdata)
322 struct cachefiles_cache *cache;
323 struct dentry *dir, *next = NULL;
328 _enter("{%p},,%s,", parent->dentry, key);
330 cache = container_of(parent->fscache.cache,
331 struct cachefiles_cache, cache);
333 ASSERT(parent->dentry);
334 ASSERT(parent->dentry->d_inode);
336 if (!(S_ISDIR(parent->dentry->d_inode->i_mode))) {
337 // TODO: convert file to dir
338 _leave("looking up in none directory");
342 dir = dget(parent->dentry);
345 /* attempt to transit the first directory component */
349 /* key ends in a double NUL */
350 key = key + nlen + 1;
355 /* search the current directory for the element name */
356 _debug("lookup '%s'", name);
358 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
361 next = lookup_one_len(name, dir, nlen);
362 cachefiles_hist(cachefiles_lookup_histogram, start);
366 _debug("next -> %p %s", next, next->d_inode ? "positive" : "negative");
369 object->new = !next->d_inode;
371 /* if this element of the path doesn't exist, then the lookup phase
372 * failed, and we can release any readers in the certain knowledge that
373 * there's nothing for them to actually read */
375 fscache_object_lookup_negative(&object->fscache);
377 /* we need to create the object if it's negative */
378 if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
379 /* index objects and intervening tree levels must be subdirs */
380 if (!next->d_inode) {
381 ret = cachefiles_has_space(cache, 1, 0);
386 ret = vfs_mkdir(dir->d_inode, next, 0);
387 cachefiles_hist(cachefiles_mkdir_histogram, start);
391 ASSERT(next->d_inode);
393 _debug("mkdir -> %p{%p{ino=%lu}}",
394 next, next->d_inode, next->d_inode->i_ino);
396 } else if (!S_ISDIR(next->d_inode->i_mode)) {
397 kerror("inode %lu is not a directory",
398 next->d_inode->i_ino);
404 /* non-index objects start out life as files */
405 if (!next->d_inode) {
406 ret = cachefiles_has_space(cache, 1, 0);
411 ret = vfs_create(dir->d_inode, next, S_IFREG, NULL);
412 cachefiles_hist(cachefiles_create_histogram, start);
416 ASSERT(next->d_inode);
418 _debug("create -> %p{%p{ino=%lu}}",
419 next, next->d_inode, next->d_inode->i_ino);
421 } else if (!S_ISDIR(next->d_inode->i_mode) &&
422 !S_ISREG(next->d_inode->i_mode)
424 kerror("inode %lu is not a file or directory",
425 next->d_inode->i_ino);
431 /* process the next component */
434 mutex_unlock(&dir->d_inode->i_mutex);
441 /* we've found the object we were looking for */
442 object->dentry = next;
444 /* if we've found that the terminal object exists, then we need to
445 * check its attributes and delete it if it's out of date */
447 _debug("validate '%*.*s'",
448 next->d_name.len, next->d_name.len, next->d_name.name);
450 ret = cachefiles_check_object_xattr(object, auxdata);
451 if (ret == -ESTALE) {
452 /* delete the object (the deleter drops the directory
454 object->dentry = NULL;
456 ret = cachefiles_bury_object(cache, dir, next);
463 _debug("redo lookup");
468 /* note that we're now using this object */
469 cachefiles_mark_object_active(cache, object);
471 mutex_unlock(&dir->d_inode->i_mutex);
475 _debug("=== OBTAINED_OBJECT ===");
478 /* attach data to a newly constructed terminal object */
479 ret = cachefiles_set_object_xattr(object, auxdata);
483 /* always update the atime on an object we've just looked up
484 * (this is used to keep track of culling, and atimes are only
485 * updated by read, write and readdir but not lookup or
487 touch_atime(cache->mnt, next);
490 /* open a file interface onto a data file */
491 if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
492 if (S_ISREG(object->dentry->d_inode->i_mode)) {
493 const struct address_space_operations *aops;
496 aops = object->dentry->d_inode->i_mapping->a_ops;
500 object->backer = object->dentry;
502 BUG(); // TODO: open file in data-class subdir
507 fscache_obtained_object(&object->fscache);
509 _leave(" = 0 [%lu]", object->dentry->d_inode->i_ino);
513 _debug("create error %d", ret);
515 cachefiles_io_error(cache, "Create/mkdir failed");
519 _debug("check error %d", ret);
520 write_lock(&cache->active_lock);
521 rb_erase(&object->active_node, &cache->active_nodes);
522 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
523 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
524 write_unlock(&cache->active_lock);
526 dput(object->dentry);
527 object->dentry = NULL;
531 _debug("delete error %d", ret);
535 _debug("lookup error %ld", PTR_ERR(next));
538 cachefiles_io_error(cache, "Lookup failed");
541 mutex_unlock(&dir->d_inode->i_mutex);
549 _leave(" = error %d", -ret);
556 struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
560 struct dentry *subdir;
564 _enter(",,%s", dirname);
566 /* search the current directory for the element name */
567 mutex_lock(&dir->d_inode->i_mutex);
570 subdir = lookup_one_len(dirname, dir, strlen(dirname));
571 cachefiles_hist(cachefiles_lookup_histogram, start);
572 if (IS_ERR(subdir)) {
573 if (PTR_ERR(subdir) == -ENOMEM)
578 _debug("subdir -> %p %s",
579 subdir, subdir->d_inode ? "positive" : "negative");
581 /* we need to create the subdir if it doesn't exist yet */
582 if (!subdir->d_inode) {
583 ret = cachefiles_has_space(cache, 1, 0);
587 _debug("attempt mkdir");
589 ret = vfs_mkdir(dir->d_inode, subdir, 0700);
593 ASSERT(subdir->d_inode);
595 _debug("mkdir -> %p{%p{ino=%lu}}",
598 subdir->d_inode->i_ino);
601 mutex_unlock(&dir->d_inode->i_mutex);
603 /* we need to make sure the subdir is a directory */
604 ASSERT(subdir->d_inode);
606 if (!S_ISDIR(subdir->d_inode->i_mode)) {
607 kerror("%s is not a directory", dirname);
613 if (!subdir->d_inode->i_op ||
614 !subdir->d_inode->i_op->setxattr ||
615 !subdir->d_inode->i_op->getxattr ||
616 !subdir->d_inode->i_op->lookup ||
617 !subdir->d_inode->i_op->mkdir ||
618 !subdir->d_inode->i_op->create ||
619 !subdir->d_inode->i_op->rename ||
620 !subdir->d_inode->i_op->rmdir ||
621 !subdir->d_inode->i_op->unlink)
624 _leave(" = [%lu]", subdir->d_inode->i_ino);
629 _leave(" = %d [check]", ret);
633 mutex_unlock(&dir->d_inode->i_mutex);
635 kerror("mkdir %s failed with error %d", dirname, ret);
639 mutex_unlock(&dir->d_inode->i_mutex);
640 ret = PTR_ERR(subdir);
641 kerror("Lookup %s failed with error %d", dirname, ret);
645 mutex_unlock(&dir->d_inode->i_mutex);
646 _leave(" = -ENOMEM");
647 return ERR_PTR(-ENOMEM);
651 * find out if an object is in use or not
652 * - if finds object and it's not in use:
653 * - returns a pointer to the object and a reference on it
654 * - returns with the directory locked
656 static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
660 struct cachefiles_object *object;
662 struct dentry *victim;
666 //_enter(",%*.*s/,%s",
667 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
669 /* look up the victim */
670 mutex_lock_nested(&dir->d_inode->i_mutex, 1);
673 victim = lookup_one_len(filename, dir, strlen(filename));
674 cachefiles_hist(cachefiles_lookup_histogram, start);
678 //_debug("victim -> %p %s",
679 // victim, victim->d_inode ? "positive" : "negative");
681 /* if the object is no longer there then we probably retired the object
682 * at the netfs's request whilst the cull was in progress
684 if (!victim->d_inode) {
685 mutex_unlock(&dir->d_inode->i_mutex);
687 _leave(" = -ENOENT [absent]");
688 return ERR_PTR(-ENOENT);
691 /* check to see if we're using this object */
692 read_lock(&cache->active_lock);
694 _n = cache->active_nodes.rb_node;
697 object = rb_entry(_n, struct cachefiles_object, active_node);
699 if (object->dentry > victim)
701 else if (object->dentry < victim)
707 read_unlock(&cache->active_lock);
709 //_leave(" = %p", victim);
713 read_unlock(&cache->active_lock);
714 mutex_unlock(&dir->d_inode->i_mutex);
716 //_leave(" = -EBUSY [in use]");
717 return ERR_PTR(-EBUSY);
720 mutex_unlock(&dir->d_inode->i_mutex);
721 ret = PTR_ERR(victim);
722 if (ret == -ENOENT) {
723 /* file or dir now absent - probably retired by netfs */
724 _leave(" = -ESTALE [absent]");
725 return ERR_PTR(-ESTALE);
729 cachefiles_io_error(cache, "Lookup failed");
730 } else if (ret != -ENOMEM) {
731 kerror("Internal error: %d", ret);
735 _leave(" = %d", ret);
740 * cull an object if it's not in use
741 * - called only by cache manager daemon
743 int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
746 struct dentry *victim;
750 dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
752 victim = cachefiles_check_active(cache, dir, filename);
754 return PTR_ERR(victim);
756 _debug("victim -> %p %s",
757 victim, victim->d_inode ? "positive" : "negative");
759 /* okay... the victim is not being used so we can cull it
760 * - start by marking it as stale
762 _debug("victim is cullable");
764 ret = cachefiles_remove_object_xattr(cache, victim);
768 /* actually remove the victim (drops the dir mutex) */
771 ret = cachefiles_bury_object(cache, dir, victim);
780 mutex_unlock(&dir->d_inode->i_mutex);
783 if (ret == -ENOENT) {
784 /* file or dir now absent - probably retired by netfs */
785 _leave(" = -ESTALE [absent]");
789 if (ret != -ENOMEM) {
790 kerror("Internal error: %d", ret);
794 _leave(" = %d", ret);
799 * find out if an object is in use or not
800 * - called only by cache manager daemon
801 * - returns -EBUSY or 0 to indicate whether an object is in use or not
803 int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
806 struct dentry *victim;
808 //_enter(",%*.*s/,%s",
809 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
811 victim = cachefiles_check_active(cache, dir, filename);
813 return PTR_ERR(victim);
815 mutex_unlock(&dir->d_inode->i_mutex);