1 /* cf-namei.c: CacheFiles path walking and related routines
3 * Copyright (C) 2006 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 License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, 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>
23 /*****************************************************************************/
25 * record the fact that an object is now active
27 static void cachefiles_mark_object_active(struct cachefiles_cache *cache,
28 struct cachefiles_object *object)
30 struct cachefiles_object *xobject;
31 struct rb_node **_p, *_parent = NULL;
32 struct dentry *dentry;
34 write_lock(&cache->active_lock);
36 dentry = object->dentry;
37 _p = &cache->active_nodes.rb_node;
40 xobject = rb_entry(_parent,
41 struct cachefiles_object, active_node);
43 if (xobject->dentry > dentry)
45 else if (xobject->dentry < dentry)
46 _p = &(*_p)->rb_right;
48 BUG(); /* uh oh... this dentry shouldn't be here */
51 rb_link_node(&object->active_node, _parent, _p);
52 rb_insert_color(&object->active_node, &cache->active_nodes);
54 write_unlock(&cache->active_lock);
57 /*****************************************************************************/
59 * delete an object representation from the cache
60 * - file backed objects are unlinked
61 * - directory backed objects are stuffed into the graveyard for userspace to
63 * - unlocks the directory mutex
65 static int cachefiles_bury_object(struct cachefiles_cache *cache,
69 struct dentry *grave, *alt, *trap;
72 char nbuffer[8 + 8 + 1];
75 _enter(",'%*.*s','%*.*s'",
76 dir->d_name.len, dir->d_name.len, dir->d_name.name,
77 rep->d_name.len, rep->d_name.len, rep->d_name.name);
79 /* non-directories can just be unlinked */
80 if (!S_ISDIR(rep->d_inode->i_mode)) {
81 _debug("unlink stale object");
82 ret = dir->d_inode->i_op->unlink(dir->d_inode, rep);
84 mutex_unlock(&dir->d_inode->i_mutex);
89 } else if (ret == -EIO) {
90 cachefiles_io_error(cache, "Unlink failed");
97 /* directories have to be moved to the graveyard */
98 _debug("move stale object to graveyard");
99 mutex_unlock(&dir->d_inode->i_mutex);
102 /* first step is to make up a grave dentry in the graveyard */
103 sprintf(nbuffer, "%08x%08x",
104 (uint32_t) xtime.tv_sec,
105 (uint32_t) atomic_inc_return(&cache->gravecounter));
108 name.len = strlen(name.name);
111 name.hash = full_name_hash(name.name, name.len);
113 if (dir->d_op && dir->d_op->d_hash) {
114 ret = dir->d_op->d_hash(dir, &name);
117 cachefiles_io_error(cache, "Hash failed");
119 _leave(" = %d", ret);
124 /* do the multiway lock magic */
125 trap = lock_rename(cache->graveyard, dir);
127 /* do some checks before getting the grave dentry */
128 if (rep->d_parent != dir) {
129 /* the entry was probably culled when we dropped the parent dir
131 unlock_rename(cache->graveyard, dir);
132 _leave(" = 0 [culled?]");
136 if (!S_ISDIR(cache->graveyard->d_inode->i_mode)) {
137 unlock_rename(cache->graveyard, dir);
138 cachefiles_io_error(cache, "Graveyard no longer a directory");
143 unlock_rename(cache->graveyard, dir);
144 cachefiles_io_error(cache, "May not make directory loop");
148 if (d_mountpoint(rep)) {
149 unlock_rename(cache->graveyard, dir);
150 cachefiles_io_error(cache, "Mountpoint in cache");
154 /* see if there's a dentry already there for this name */
155 grave = d_lookup(cache->graveyard, &name);
159 grave = d_alloc(cache->graveyard, &name);
161 unlock_rename(cache->graveyard, dir);
162 _leave(" = -ENOMEM");
166 alt = cache->graveyard->d_inode->i_op->lookup(
167 cache->graveyard->d_inode, grave, NULL);
169 unlock_rename(cache->graveyard, dir);
172 if (PTR_ERR(alt) == -ENOMEM) {
173 _leave(" = -ENOMEM");
177 cachefiles_io_error(cache, "Lookup error %ld",
188 if (grave->d_inode) {
189 unlock_rename(cache->graveyard, dir);
196 if (d_mountpoint(grave)) {
197 unlock_rename(cache->graveyard, dir);
199 cachefiles_io_error(cache, "Mountpoint in graveyard");
203 /* target should not be an ancestor of source */
205 unlock_rename(cache->graveyard, dir);
207 cachefiles_io_error(cache, "May not make directory loop");
211 /* attempt the rename */
212 DQUOT_INIT(dir->d_inode);
213 DQUOT_INIT(cache->graveyard->d_inode);
215 old_name = fsnotify_oldname_init(rep->d_name.name);
217 ret = dir->d_inode->i_op->rename(dir->d_inode, rep,
218 cache->graveyard->d_inode, grave);
222 fsnotify_move(dir->d_inode, cache->graveyard->d_inode,
223 old_name, rep->d_name.name, 1,
224 grave->d_inode, rep->d_inode);
225 } else if (ret != -ENOMEM) {
226 cachefiles_io_error(cache, "Rename failed with error %d", ret);
229 fsnotify_oldname_free(old_name);
231 unlock_rename(cache->graveyard, dir);
237 /*****************************************************************************/
239 * delete an object representation from the cache
241 int cachefiles_delete_object(struct cachefiles_cache *cache,
242 struct cachefiles_object *object)
247 _enter(",{%p}", object->dentry);
249 ASSERT(object->dentry);
250 ASSERT(object->dentry->d_inode);
251 ASSERT(object->dentry->d_parent);
253 dir = dget_parent(object->dentry);
255 mutex_lock(&dir->d_inode->i_mutex);
256 ret = cachefiles_bury_object(cache, dir, object->dentry);
259 _leave(" = %d", ret);
263 /*****************************************************************************/
265 * walk from the parent object to the child object through the backing
266 * filesystem, creating directories as we go
268 int cachefiles_walk_to_object(struct cachefiles_object *parent,
269 struct cachefiles_object *object,
271 struct cachefiles_xattr *auxdata)
273 struct cachefiles_cache *cache;
274 struct dentry *dir, *next = NULL, *new;
280 _enter("{%p}", parent->dentry);
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 fsuid = current->fsuid;
295 fsgid = current->fsgid;
299 dir = dget(parent->dentry);
302 /* attempt to transit the first directory component */
304 key = strchr(key, '/');
306 name.len = key - (char *) name.name;
309 name.len = strlen(name.name);
313 name.hash = full_name_hash(name.name, name.len);
315 if (dir->d_op && dir->d_op->d_hash) {
316 ret = dir->d_op->d_hash(dir, &name);
318 cachefiles_io_error(cache, "Hash failed");
324 /* search the current directory for the element name */
325 _debug("lookup '%s' %x", name.name, name.hash);
327 mutex_lock(&dir->d_inode->i_mutex);
329 next = d_lookup(dir, &name);
333 new = d_alloc(dir, &name);
337 ASSERT(dir->d_inode->i_op);
338 ASSERT(dir->d_inode->i_op->lookup);
340 next = dir->d_inode->i_op->lookup(dir->d_inode, new, NULL);
351 if (!next->d_inode->i_op ||
352 !next->d_inode->i_op->setxattr ||
353 !next->d_inode->i_op->getxattr ||
354 !next->d_inode->i_op->removexattr)
357 if (key && (!next->d_inode->i_op->lookup ||
358 !next->d_inode->i_op->mkdir ||
359 !next->d_inode->i_op->create ||
360 !next->d_inode->i_op->rename ||
361 !next->d_inode->i_op->rmdir ||
362 !next->d_inode->i_op->unlink))
367 _debug("next -> %p %s", next, next->d_inode ? "positive" : "negative");
370 object->new = !next->d_inode;
372 /* we need to create the object if it's negative */
373 if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
374 /* index objects and intervening tree levels must be subdirs */
375 if (!next->d_inode) {
376 DQUOT_INIT(dir->d_inode);
377 ret = dir->d_inode->i_op->mkdir(dir->d_inode, next, 0);
381 ASSERT(next->d_inode);
383 fsnotify_mkdir(dir->d_inode, next);
385 _debug("mkdir -> %p{%p{ino=%lu}}",
386 next, next->d_inode, next->d_inode->i_ino);
388 } else if (!S_ISDIR(next->d_inode->i_mode)) {
389 kerror("inode %lu is not a directory",
390 next->d_inode->i_ino);
396 /* non-index objects start out life as files */
397 if (!next->d_inode) {
398 DQUOT_INIT(dir->d_inode);
399 ret = dir->d_inode->i_op->create(dir->d_inode, next,
404 ASSERT(next->d_inode);
406 fsnotify_create(dir->d_inode, next);
408 _debug("create -> %p{%p{ino=%lu}}",
409 next, next->d_inode, next->d_inode->i_ino);
411 } else if (!S_ISDIR(next->d_inode->i_mode) &&
412 !S_ISREG(next->d_inode->i_mode)
414 kerror("inode %lu is not a file or directory",
415 next->d_inode->i_ino);
421 /* process the next component */
424 mutex_unlock(&dir->d_inode->i_mutex);
431 /* we've found the object we were looking for */
432 object->dentry = next;
434 /* if we've found that the terminal object exists, then we need to
435 * check its attributes and delete it if it's out of date */
437 _debug("validate '%*.*s'",
438 next->d_name.len, next->d_name.len, next->d_name.name);
440 ret = cachefiles_check_object_xattr(object, auxdata);
441 if (ret == -ESTALE) {
442 /* delete the object (the deleter drops the directory
444 object->dentry = NULL;
446 ret = cachefiles_bury_object(cache, dir, next);
453 _debug("redo lookup");
458 /* note that we're now using this object */
459 cachefiles_mark_object_active(cache, object);
461 mutex_unlock(&dir->d_inode->i_mutex);
466 /* attach data to a newly constructed terminal object */
467 ret = cachefiles_set_object_xattr(object, auxdata);
471 /* always update the atime on an object we've just looked up
472 * (this is used to keep track of culling, and atimes are only
473 * updated by read, write and readdir but not lookup or
475 touch_atime(cache->mnt, next);
478 /* open a file interface onto a data file */
479 if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
480 if (S_ISREG(object->dentry->d_inode->i_mode)) {
481 const struct address_space_operations *aops;
484 aops = object->dentry->d_inode->i_mapping->a_ops;
486 !aops->prepare_write ||
490 object->backer = object->dentry;
492 BUG(); // TODO: open file in data-class subdir
496 current->fsuid = fsuid;
497 current->fsgid = fsgid;
500 _leave(" = 0 [%lu]", object->dentry->d_inode->i_ino);
505 cachefiles_io_error(cache, "create/mkdir failed");
509 write_lock(&cache->active_lock);
510 rb_erase(&object->active_node, &cache->active_nodes);
511 write_unlock(&cache->active_lock);
513 dput(object->dentry);
514 object->dentry = NULL;
518 _debug("delete error %d", ret);
522 _debug("lookup error %ld", PTR_ERR(next));
526 cachefiles_io_error(cache, "Lookup failed");
533 mutex_unlock(&dir->d_inode->i_mutex);
538 current->fsuid = fsuid;
539 current->fsgid = fsgid;
545 /*****************************************************************************/
549 struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
553 struct dentry *subdir, *new;
561 /* set up the name */
563 name.len = strlen(dirname);
564 name.hash = full_name_hash(name.name, name.len);
566 if (dir->d_op && dir->d_op->d_hash) {
567 ret = dir->d_op->d_hash(dir, &name);
570 kerror("Hash failed");
571 _leave(" = %d", ret);
576 /* search the current directory for the element name */
577 _debug("lookup '%s' %x", name.name, name.hash);
579 fsuid = current->fsuid;
580 fsgid = current->fsgid;
584 mutex_lock(&dir->d_inode->i_mutex);
586 subdir = d_lookup(dir, &name);
590 new = d_alloc(dir, &name);
594 subdir = dir->d_inode->i_op->lookup(dir->d_inode, new, NULL);
604 _debug("subdir -> %p %s",
605 subdir, subdir->d_inode ? "positive" : "negative");
607 /* we need to create the subdir if it doesn't exist yet */
608 if (!subdir->d_inode) {
609 DQUOT_INIT(dir->d_inode);
610 ret = dir->d_inode->i_op->mkdir(dir->d_inode, subdir, 0700);
614 ASSERT(subdir->d_inode);
616 fsnotify_mkdir(dir->d_inode, subdir);
618 _debug("mkdir -> %p{%p{ino=%lu}}",
621 subdir->d_inode->i_ino);
624 mutex_unlock(&dir->d_inode->i_mutex);
626 current->fsuid = fsuid;
627 current->fsgid = fsgid;
629 /* we need to make sure the subdir is a directory */
630 ASSERT(subdir->d_inode);
632 if (!S_ISDIR(subdir->d_inode->i_mode)) {
633 kerror("%s is not a directory", dirname);
639 if (!subdir->d_inode->i_op ||
640 !subdir->d_inode->i_op->setxattr ||
641 !subdir->d_inode->i_op->getxattr ||
642 !subdir->d_inode->i_op->lookup ||
643 !subdir->d_inode->i_op->mkdir ||
644 !subdir->d_inode->i_op->create ||
645 !subdir->d_inode->i_op->rename ||
646 !subdir->d_inode->i_op->rmdir ||
647 !subdir->d_inode->i_op->unlink)
650 _leave(" = [%lu]", subdir->d_inode->i_ino);
655 _leave(" = %d [check]", ret);
659 mutex_unlock(&dir->d_inode->i_mutex);
660 kerror("mkdir %s failed with error %d", dirname, ret);
664 mutex_unlock(&dir->d_inode->i_mutex);
666 ret = PTR_ERR(subdir);
667 kerror("Lookup %s failed with error %d", dirname, ret);
671 mutex_unlock(&dir->d_inode->i_mutex);
676 current->fsuid = fsuid;
677 current->fsgid = fsgid;
678 _leave(" = %d", ret);
682 /*****************************************************************************/
684 * cull an object if it's not in use
685 * - called only by cache manager daemon
687 int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
690 struct cachefiles_object *object;
692 struct dentry *victim, *new;
697 dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
699 /* set up the name */
700 name.name = filename;
701 name.len = strlen(filename);
702 name.hash = full_name_hash(name.name, name.len);
704 if (dir->d_op && dir->d_op->d_hash) {
705 ret = dir->d_op->d_hash(dir, &name);
708 cachefiles_io_error(cache, "Hash failed");
709 _leave(" = %d", ret);
714 /* look up the victim */
715 mutex_lock(&dir->d_inode->i_mutex);
717 victim = d_lookup(dir, &name);
721 new = d_alloc(dir, &name);
725 victim = dir->d_inode->i_op->lookup(dir->d_inode, new, NULL);
735 _debug("victim -> %p %s",
736 victim, victim->d_inode ? "positive" : "negative");
738 /* if the object is no longer there then we probably retired the object
739 * at the netfs's request whilst the cull was in progress
741 if (!victim->d_inode) {
742 mutex_unlock(&dir->d_inode->i_mutex);
744 _leave(" = -ENOENT [absent]");
748 /* check to see if we're using this object */
749 read_lock(&cache->active_lock);
751 _n = cache->active_nodes.rb_node;
754 object = rb_entry(_n, struct cachefiles_object, active_node);
756 if (object->dentry > victim)
758 else if (object->dentry < victim)
764 read_unlock(&cache->active_lock);
766 /* okay... the victim is not being used so we can cull it
767 * - start by marking it as stale
769 _debug("victim is cullable");
771 ret = cachefiles_remove_object_xattr(cache, victim);
775 /* actually remove the victim (drops the dir mutex) */
778 ret = cachefiles_bury_object(cache, dir, victim);
788 read_unlock(&cache->active_lock);
789 mutex_unlock(&dir->d_inode->i_mutex);
791 _leave(" = -EBUSY [in use]");
795 mutex_unlock(&dir->d_inode->i_mutex);
796 _leave(" = -ENOMEM");
800 mutex_unlock(&dir->d_inode->i_mutex);
802 ret = PTR_ERR(victim);
804 cachefiles_io_error(cache, "Lookup failed");
808 mutex_unlock(&dir->d_inode->i_mutex);
812 if (ret == -ENOENT) {
813 /* file or dir now absent - probably retired by netfs */
814 _leave(" = -ESTALE [absent]");
818 if (ret != -ENOMEM) {
819 kerror("Internal error: %d", ret);
823 _leave(" = %d", ret);