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)
31 * record the fact that an object is now active
33 static void cachefiles_mark_object_active(struct cachefiles_cache *cache,
34 struct cachefiles_object *object)
36 struct cachefiles_object *xobject;
37 struct rb_node **_p, *_parent = NULL;
38 struct dentry *dentry;
40 _enter(",%p", object);
43 write_lock(&cache->active_lock);
45 if (test_and_set_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags))
48 dentry = object->dentry;
49 _p = &cache->active_nodes.rb_node;
52 xobject = rb_entry(_parent,
53 struct cachefiles_object, active_node);
55 ASSERT(xobject != object);
57 if (xobject->dentry > dentry)
59 else if (xobject->dentry < dentry)
60 _p = &(*_p)->rb_right;
62 goto wait_for_old_object;
65 rb_link_node(&object->active_node, _parent, _p);
66 rb_insert_color(&object->active_node, &cache->active_nodes);
68 write_unlock(&cache->active_lock);
72 /* an old object from a previous incarnation is hogging the slot - we
73 * need to wait for it to be destroyed */
75 if (xobject->fscache.state < FSCACHE_OBJECT_DYING) {
76 printk(KERN_ERR "\n");
77 printk(KERN_ERR "CacheFiles: Error:"
78 " Unexpected object collision\n");
79 printk(KERN_ERR "xobject: OBJ%x\n",
80 xobject->fscache.debug_id);
81 printk(KERN_ERR "xobjstate=%s\n",
82 fscache_object_states[xobject->fscache.state]);
83 printk(KERN_ERR "xobjflags=%lx\n", xobject->fscache.flags);
84 printk(KERN_ERR "xobjevent=%lx [%lx]\n",
85 xobject->fscache.events, xobject->fscache.event_mask);
86 printk(KERN_ERR "xops=%u inp=%u exc=%u\n",
87 xobject->fscache.n_ops, xobject->fscache.n_in_progress,
88 xobject->fscache.n_exclusive);
89 printk(KERN_ERR "xcookie=%p [pr=%p nd=%p fl=%lx]\n",
90 xobject->fscache.cookie,
91 xobject->fscache.cookie->parent,
92 xobject->fscache.cookie->netfs_data,
93 xobject->fscache.cookie->flags);
94 printk(KERN_ERR "xparent=%p\n",
95 xobject->fscache.parent);
96 printk(KERN_ERR "object: OBJ%x\n",
97 object->fscache.debug_id);
98 printk(KERN_ERR "cookie=%p [pr=%p nd=%p fl=%lx]\n",
99 object->fscache.cookie,
100 object->fscache.cookie->parent,
101 object->fscache.cookie->netfs_data,
102 object->fscache.cookie->flags);
103 printk(KERN_ERR "parent=%p\n",
104 object->fscache.parent);
107 atomic_inc(&xobject->usage);
108 write_unlock(&cache->active_lock);
111 wait_on_bit(&xobject->flags, CACHEFILES_OBJECT_ACTIVE,
112 cachefiles_wait_bit, TASK_UNINTERRUPTIBLE);
113 _debug("<<< waited");
115 cache->cache.ops->put_object(&xobject->fscache);
120 * delete an object representation from the cache
121 * - file backed objects are unlinked
122 * - directory backed objects are stuffed into the graveyard for userspace to
124 * - unlocks the directory mutex
126 static int cachefiles_bury_object(struct cachefiles_cache *cache,
130 struct dentry *grave, *trap;
131 char nbuffer[8 + 8 + 1];
134 _enter(",'%*.*s','%*.*s'",
135 dir->d_name.len, dir->d_name.len, dir->d_name.name,
136 rep->d_name.len, rep->d_name.len, rep->d_name.name);
138 /* non-directories can just be unlinked */
139 if (!S_ISDIR(rep->d_inode->i_mode)) {
140 _debug("unlink stale object");
141 ret = vfs_unlink(dir->d_inode, rep);
143 mutex_unlock(&dir->d_inode->i_mutex);
146 cachefiles_io_error(cache, "Unlink failed");
148 _leave(" = %d", ret);
152 /* directories have to be moved to the graveyard */
153 _debug("move stale object to graveyard");
154 mutex_unlock(&dir->d_inode->i_mutex);
157 /* first step is to make up a grave dentry in the graveyard */
158 sprintf(nbuffer, "%08x%08x",
159 (uint32_t) get_seconds(),
160 (uint32_t) atomic_inc_return(&cache->gravecounter));
162 /* do the multiway lock magic */
163 trap = lock_rename(cache->graveyard, dir);
165 /* do some checks before getting the grave dentry */
166 if (rep->d_parent != dir) {
167 /* the entry was probably culled when we dropped the parent dir
169 unlock_rename(cache->graveyard, dir);
170 _leave(" = 0 [culled?]");
174 if (!S_ISDIR(cache->graveyard->d_inode->i_mode)) {
175 unlock_rename(cache->graveyard, dir);
176 cachefiles_io_error(cache, "Graveyard no longer a directory");
181 unlock_rename(cache->graveyard, dir);
182 cachefiles_io_error(cache, "May not make directory loop");
186 if (d_mountpoint(rep)) {
187 unlock_rename(cache->graveyard, dir);
188 cachefiles_io_error(cache, "Mountpoint in cache");
192 grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
194 unlock_rename(cache->graveyard, dir);
196 if (PTR_ERR(grave) == -ENOMEM) {
197 _leave(" = -ENOMEM");
201 cachefiles_io_error(cache, "Lookup error %ld",
206 if (grave->d_inode) {
207 unlock_rename(cache->graveyard, dir);
214 if (d_mountpoint(grave)) {
215 unlock_rename(cache->graveyard, dir);
217 cachefiles_io_error(cache, "Mountpoint in graveyard");
221 /* target should not be an ancestor of source */
223 unlock_rename(cache->graveyard, dir);
225 cachefiles_io_error(cache, "May not make directory loop");
229 /* attempt the rename */
230 ret = vfs_rename(dir->d_inode, rep, cache->graveyard->d_inode, grave);
231 if (ret != 0 && ret != -ENOMEM)
232 cachefiles_io_error(cache, "Rename failed with error %d", ret);
234 unlock_rename(cache->graveyard, dir);
241 * delete an object representation from the cache
243 int cachefiles_delete_object(struct cachefiles_cache *cache,
244 struct cachefiles_object *object)
249 _enter(",{%p}", object->dentry);
251 ASSERT(object->dentry);
252 ASSERT(object->dentry->d_inode);
253 ASSERT(object->dentry->d_parent);
255 dir = dget_parent(object->dentry);
257 mutex_lock(&dir->d_inode->i_mutex);
258 ret = cachefiles_bury_object(cache, dir, object->dentry);
261 _leave(" = %d", ret);
266 * walk from the parent object to the child object through the backing
267 * filesystem, creating directories as we go
269 int cachefiles_walk_to_object(struct cachefiles_object *parent,
270 struct cachefiles_object *object,
272 struct cachefiles_xattr *auxdata)
274 struct cachefiles_cache *cache;
275 struct dentry *dir, *next = NULL;
280 _enter("{%p},,%s,", parent->dentry, key);
282 cache = container_of(parent->fscache.cache,
283 struct cachefiles_cache, cache);
285 ASSERT(parent->dentry);
286 ASSERT(parent->dentry->d_inode);
288 if (!(S_ISDIR(parent->dentry->d_inode->i_mode))) {
289 // TODO: convert file to dir
290 _leave("looking up in none directory");
294 dir = dget(parent->dentry);
297 /* attempt to transit the first directory component */
301 /* key ends in a double NUL */
302 key = key + nlen + 1;
307 /* search the current directory for the element name */
308 _debug("lookup '%s'", name);
310 mutex_lock(&dir->d_inode->i_mutex);
313 next = lookup_one_len(name, dir, nlen);
314 cachefiles_hist(cachefiles_lookup_histogram, start);
318 _debug("next -> %p %s", next, next->d_inode ? "positive" : "negative");
321 object->new = !next->d_inode;
323 /* if this element of the path doesn't exist, then the lookup phase
324 * failed, and we can release any readers in the certain knowledge that
325 * there's nothing for them to actually read */
327 fscache_object_lookup_negative(&object->fscache);
329 /* we need to create the object if it's negative */
330 if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
331 /* index objects and intervening tree levels must be subdirs */
332 if (!next->d_inode) {
333 ret = cachefiles_has_space(cache, 1, 0);
338 ret = vfs_mkdir(dir->d_inode, next, 0);
339 cachefiles_hist(cachefiles_mkdir_histogram, start);
343 ASSERT(next->d_inode);
345 _debug("mkdir -> %p{%p{ino=%lu}}",
346 next, next->d_inode, next->d_inode->i_ino);
348 } else if (!S_ISDIR(next->d_inode->i_mode)) {
349 kerror("inode %lu is not a directory",
350 next->d_inode->i_ino);
356 /* non-index objects start out life as files */
357 if (!next->d_inode) {
358 ret = cachefiles_has_space(cache, 1, 0);
363 ret = vfs_create(dir->d_inode, next, S_IFREG, NULL);
364 cachefiles_hist(cachefiles_create_histogram, start);
368 ASSERT(next->d_inode);
370 _debug("create -> %p{%p{ino=%lu}}",
371 next, next->d_inode, next->d_inode->i_ino);
373 } else if (!S_ISDIR(next->d_inode->i_mode) &&
374 !S_ISREG(next->d_inode->i_mode)
376 kerror("inode %lu is not a file or directory",
377 next->d_inode->i_ino);
383 /* process the next component */
386 mutex_unlock(&dir->d_inode->i_mutex);
393 /* we've found the object we were looking for */
394 object->dentry = next;
396 /* if we've found that the terminal object exists, then we need to
397 * check its attributes and delete it if it's out of date */
399 _debug("validate '%*.*s'",
400 next->d_name.len, next->d_name.len, next->d_name.name);
402 ret = cachefiles_check_object_xattr(object, auxdata);
403 if (ret == -ESTALE) {
404 /* delete the object (the deleter drops the directory
406 object->dentry = NULL;
408 ret = cachefiles_bury_object(cache, dir, next);
415 _debug("redo lookup");
420 /* note that we're now using this object */
421 cachefiles_mark_object_active(cache, object);
423 mutex_unlock(&dir->d_inode->i_mutex);
427 _debug("=== OBTAINED_OBJECT ===");
430 /* attach data to a newly constructed terminal object */
431 ret = cachefiles_set_object_xattr(object, auxdata);
435 /* always update the atime on an object we've just looked up
436 * (this is used to keep track of culling, and atimes are only
437 * updated by read, write and readdir but not lookup or
439 touch_atime(cache->mnt, next);
442 /* open a file interface onto a data file */
443 if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
444 if (S_ISREG(object->dentry->d_inode->i_mode)) {
445 const struct address_space_operations *aops;
448 aops = object->dentry->d_inode->i_mapping->a_ops;
452 object->backer = object->dentry;
454 BUG(); // TODO: open file in data-class subdir
459 fscache_obtained_object(&object->fscache);
461 _leave(" = 0 [%lu]", object->dentry->d_inode->i_ino);
465 _debug("create error %d", ret);
467 cachefiles_io_error(cache, "Create/mkdir failed");
471 _debug("check error %d", ret);
472 write_lock(&cache->active_lock);
473 rb_erase(&object->active_node, &cache->active_nodes);
474 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
475 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
476 write_unlock(&cache->active_lock);
478 dput(object->dentry);
479 object->dentry = NULL;
483 _debug("delete error %d", ret);
487 _debug("lookup error %ld", PTR_ERR(next));
490 cachefiles_io_error(cache, "Lookup failed");
493 mutex_unlock(&dir->d_inode->i_mutex);
501 _leave(" = error %d", -ret);
508 struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
512 struct dentry *subdir;
516 _enter(",,%s", dirname);
518 /* search the current directory for the element name */
519 mutex_lock(&dir->d_inode->i_mutex);
522 subdir = lookup_one_len(dirname, dir, strlen(dirname));
523 cachefiles_hist(cachefiles_lookup_histogram, start);
524 if (IS_ERR(subdir)) {
525 if (PTR_ERR(subdir) == -ENOMEM)
530 _debug("subdir -> %p %s",
531 subdir, subdir->d_inode ? "positive" : "negative");
533 /* we need to create the subdir if it doesn't exist yet */
534 if (!subdir->d_inode) {
535 ret = cachefiles_has_space(cache, 1, 0);
539 _debug("attempt mkdir");
541 ret = vfs_mkdir(dir->d_inode, subdir, 0700);
545 ASSERT(subdir->d_inode);
547 _debug("mkdir -> %p{%p{ino=%lu}}",
550 subdir->d_inode->i_ino);
553 mutex_unlock(&dir->d_inode->i_mutex);
555 /* we need to make sure the subdir is a directory */
556 ASSERT(subdir->d_inode);
558 if (!S_ISDIR(subdir->d_inode->i_mode)) {
559 kerror("%s is not a directory", dirname);
565 if (!subdir->d_inode->i_op ||
566 !subdir->d_inode->i_op->setxattr ||
567 !subdir->d_inode->i_op->getxattr ||
568 !subdir->d_inode->i_op->lookup ||
569 !subdir->d_inode->i_op->mkdir ||
570 !subdir->d_inode->i_op->create ||
571 !subdir->d_inode->i_op->rename ||
572 !subdir->d_inode->i_op->rmdir ||
573 !subdir->d_inode->i_op->unlink)
576 _leave(" = [%lu]", subdir->d_inode->i_ino);
581 _leave(" = %d [check]", ret);
585 mutex_unlock(&dir->d_inode->i_mutex);
587 kerror("mkdir %s failed with error %d", dirname, ret);
591 mutex_unlock(&dir->d_inode->i_mutex);
592 ret = PTR_ERR(subdir);
593 kerror("Lookup %s failed with error %d", dirname, ret);
597 mutex_unlock(&dir->d_inode->i_mutex);
598 _leave(" = -ENOMEM");
599 return ERR_PTR(-ENOMEM);
603 * find out if an object is in use or not
604 * - if finds object and it's not in use:
605 * - returns a pointer to the object and a reference on it
606 * - returns with the directory locked
608 static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
612 struct cachefiles_object *object;
614 struct dentry *victim;
618 //_enter(",%*.*s/,%s",
619 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
621 /* look up the victim */
622 mutex_lock_nested(&dir->d_inode->i_mutex, 1);
625 victim = lookup_one_len(filename, dir, strlen(filename));
626 cachefiles_hist(cachefiles_lookup_histogram, start);
630 //_debug("victim -> %p %s",
631 // victim, victim->d_inode ? "positive" : "negative");
633 /* if the object is no longer there then we probably retired the object
634 * at the netfs's request whilst the cull was in progress
636 if (!victim->d_inode) {
637 mutex_unlock(&dir->d_inode->i_mutex);
639 _leave(" = -ENOENT [absent]");
640 return ERR_PTR(-ENOENT);
643 /* check to see if we're using this object */
644 read_lock(&cache->active_lock);
646 _n = cache->active_nodes.rb_node;
649 object = rb_entry(_n, struct cachefiles_object, active_node);
651 if (object->dentry > victim)
653 else if (object->dentry < victim)
659 read_unlock(&cache->active_lock);
661 //_leave(" = %p", victim);
665 read_unlock(&cache->active_lock);
666 mutex_unlock(&dir->d_inode->i_mutex);
668 //_leave(" = -EBUSY [in use]");
669 return ERR_PTR(-EBUSY);
672 mutex_unlock(&dir->d_inode->i_mutex);
673 ret = PTR_ERR(victim);
674 if (ret == -ENOENT) {
675 /* file or dir now absent - probably retired by netfs */
676 _leave(" = -ESTALE [absent]");
677 return ERR_PTR(-ESTALE);
681 cachefiles_io_error(cache, "Lookup failed");
682 } else if (ret != -ENOMEM) {
683 kerror("Internal error: %d", ret);
687 _leave(" = %d", ret);
692 * cull an object if it's not in use
693 * - called only by cache manager daemon
695 int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
698 struct dentry *victim;
702 dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
704 victim = cachefiles_check_active(cache, dir, filename);
706 return PTR_ERR(victim);
708 _debug("victim -> %p %s",
709 victim, victim->d_inode ? "positive" : "negative");
711 /* okay... the victim is not being used so we can cull it
712 * - start by marking it as stale
714 _debug("victim is cullable");
716 ret = cachefiles_remove_object_xattr(cache, victim);
720 /* actually remove the victim (drops the dir mutex) */
723 ret = cachefiles_bury_object(cache, dir, victim);
732 mutex_unlock(&dir->d_inode->i_mutex);
735 if (ret == -ENOENT) {
736 /* file or dir now absent - probably retired by netfs */
737 _leave(" = -ESTALE [absent]");
741 if (ret != -ENOMEM) {
742 kerror("Internal error: %d", ret);
746 _leave(" = %d", ret);
751 * find out if an object is in use or not
752 * - called only by cache manager daemon
753 * - returns -EBUSY or 0 to indicate whether an object is in use or not
755 int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
758 struct dentry *victim;
760 //_enter(",%*.*s/,%s",
761 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
763 victim = cachefiles_check_active(cache, dir, filename);
765 return PTR_ERR(victim);
767 mutex_unlock(&dir->d_inode->i_mutex);