powerpc: Disable CONFIG_SYSFS_DEPRECATED
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / cachefiles / namei.c
blobf4a7840bf42cbb4c4be9cf74a7488e61aab4c94d
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>
15 #include <linux/fs.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>
23 #include "internal.h"
25 #define CACHEFILES_KEYBUF_SIZE 512
28 * dump debugging info about an object
30 static noinline
31 void __cachefiles_printk_object(struct cachefiles_object *object,
32 const char *prefix,
33 u8 *keybuf)
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 swfl=%lx ev=%lx[%lx]\n",
41 prefix, fscache_object_states[object->fscache.state],
42 object->fscache.flags, object->fscache.work.flags,
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;
53 if (cookie) {
54 printk(KERN_ERR "%scookie=%p [pr=%p nd=%p fl=%lx]\n",
55 prefix,
56 object->fscache.cookie,
57 object->fscache.cookie->parent,
58 object->fscache.cookie->netfs_data,
59 object->fscache.cookie->flags);
60 if (keybuf)
61 keylen = cookie->def->get_key(cookie->netfs_data, keybuf,
62 CACHEFILES_KEYBUF_SIZE);
63 else
64 keylen = 0;
65 } else {
66 printk(KERN_ERR "%scookie=NULL\n", prefix);
67 keylen = 0;
69 spin_unlock(&object->fscache.lock);
71 if (keylen) {
72 printk(KERN_ERR "%skey=[%u] '", prefix, keylen);
73 for (loop = 0; loop < keylen; loop++)
74 printk("%02x", keybuf[loop]);
75 printk("'\n");
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)
85 u8 *keybuf;
87 keybuf = kmalloc(CACHEFILES_KEYBUF_SIZE, GFP_NOIO);
88 if (object)
89 __cachefiles_printk_object(object, "", keybuf);
90 if (xobject)
91 __cachefiles_printk_object(xobject, "x", keybuf);
92 kfree(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;
105 struct rb_node *p;
107 _enter(",'%*.*s'",
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;
113 while (p) {
114 object = rb_entry(p, struct cachefiles_object, active_node);
115 if (object->dentry > dentry)
116 p = p->rb_left;
117 else if (object->dentry < dentry)
118 p = p->rb_right;
119 else
120 goto found_dentry;
123 write_unlock(&cache->active_lock);
124 _leave(" [no owner]");
125 return;
127 /* found the dentry for */
128 found_dentry:
129 kdebug("preemptive burial: OBJ%x [%s] %p",
130 object->fscache.debug_id,
131 fscache_object_states[object->fscache.state],
132 dentry);
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);
160 try_again:
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);
166 BUG();
169 dentry = object->dentry;
170 _p = &cache->active_nodes.rb_node;
171 while (*_p) {
172 _parent = *_p;
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;
182 else
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);
190 _leave(" = 0");
191 return 0;
193 /* an old object from a previous incarnation is hogging the slot - we
194 * need to wait for it to be destroyed */
195 wait_for_old_object:
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);
201 BUG();
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;
210 wait_queue_t wait;
211 bool requeue;
213 /* if the object we're waiting for is queued for processing,
214 * then just put ourselves on the queue behind it */
215 if (slow_work_is_queued(&xobject->fscache.work)) {
216 _debug("queue OBJ%x behind OBJ%x immediately",
217 object->fscache.debug_id,
218 xobject->fscache.debug_id);
219 goto requeue;
222 /* otherwise we sleep until either the object we're waiting for
223 * is done, or the slow-work facility wants the thread back to
224 * do other work */
225 wq = bit_waitqueue(&xobject->flags, CACHEFILES_OBJECT_ACTIVE);
226 init_wait(&wait);
227 requeue = false;
228 do {
229 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
230 if (!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags))
231 break;
232 requeue = slow_work_sleep_till_thread_needed(
233 &object->fscache.work, &timeout);
234 } while (timeout > 0 && !requeue);
235 finish_wait(wq, &wait);
237 if (requeue &&
238 test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
239 _debug("queue OBJ%x behind OBJ%x after wait",
240 object->fscache.debug_id,
241 xobject->fscache.debug_id);
242 goto requeue;
245 if (timeout <= 0) {
246 printk(KERN_ERR "\n");
247 printk(KERN_ERR "CacheFiles: Error: Overlong"
248 " wait for old active object to go away\n");
249 cachefiles_printk_object(object, xobject);
250 goto requeue;
254 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags));
256 cache->cache.ops->put_object(&xobject->fscache);
257 goto try_again;
259 requeue:
260 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
261 cache->cache.ops->put_object(&xobject->fscache);
262 _leave(" = -ETIMEDOUT");
263 return -ETIMEDOUT;
267 * delete an object representation from the cache
268 * - file backed objects are unlinked
269 * - directory backed objects are stuffed into the graveyard for userspace to
270 * delete
271 * - unlocks the directory mutex
273 static int cachefiles_bury_object(struct cachefiles_cache *cache,
274 struct dentry *dir,
275 struct dentry *rep,
276 bool preemptive)
278 struct dentry *grave, *trap;
279 char nbuffer[8 + 8 + 1];
280 int ret;
282 _enter(",'%*.*s','%*.*s'",
283 dir->d_name.len, dir->d_name.len, dir->d_name.name,
284 rep->d_name.len, rep->d_name.len, rep->d_name.name);
286 _debug("remove %p from %p", rep, dir);
288 /* non-directories can just be unlinked */
289 if (!S_ISDIR(rep->d_inode->i_mode)) {
290 _debug("unlink stale object");
291 ret = vfs_unlink(dir->d_inode, rep);
293 if (preemptive)
294 cachefiles_mark_object_buried(cache, rep);
296 mutex_unlock(&dir->d_inode->i_mutex);
298 if (ret == -EIO)
299 cachefiles_io_error(cache, "Unlink failed");
301 _leave(" = %d", ret);
302 return ret;
305 /* directories have to be moved to the graveyard */
306 _debug("move stale object to graveyard");
307 mutex_unlock(&dir->d_inode->i_mutex);
309 try_again:
310 /* first step is to make up a grave dentry in the graveyard */
311 sprintf(nbuffer, "%08x%08x",
312 (uint32_t) get_seconds(),
313 (uint32_t) atomic_inc_return(&cache->gravecounter));
315 /* do the multiway lock magic */
316 trap = lock_rename(cache->graveyard, dir);
318 /* do some checks before getting the grave dentry */
319 if (rep->d_parent != dir) {
320 /* the entry was probably culled when we dropped the parent dir
321 * lock */
322 unlock_rename(cache->graveyard, dir);
323 _leave(" = 0 [culled?]");
324 return 0;
327 if (!S_ISDIR(cache->graveyard->d_inode->i_mode)) {
328 unlock_rename(cache->graveyard, dir);
329 cachefiles_io_error(cache, "Graveyard no longer a directory");
330 return -EIO;
333 if (trap == rep) {
334 unlock_rename(cache->graveyard, dir);
335 cachefiles_io_error(cache, "May not make directory loop");
336 return -EIO;
339 if (d_mountpoint(rep)) {
340 unlock_rename(cache->graveyard, dir);
341 cachefiles_io_error(cache, "Mountpoint in cache");
342 return -EIO;
345 grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
346 if (IS_ERR(grave)) {
347 unlock_rename(cache->graveyard, dir);
349 if (PTR_ERR(grave) == -ENOMEM) {
350 _leave(" = -ENOMEM");
351 return -ENOMEM;
354 cachefiles_io_error(cache, "Lookup error %ld",
355 PTR_ERR(grave));
356 return -EIO;
359 if (grave->d_inode) {
360 unlock_rename(cache->graveyard, dir);
361 dput(grave);
362 grave = NULL;
363 cond_resched();
364 goto try_again;
367 if (d_mountpoint(grave)) {
368 unlock_rename(cache->graveyard, dir);
369 dput(grave);
370 cachefiles_io_error(cache, "Mountpoint in graveyard");
371 return -EIO;
374 /* target should not be an ancestor of source */
375 if (trap == grave) {
376 unlock_rename(cache->graveyard, dir);
377 dput(grave);
378 cachefiles_io_error(cache, "May not make directory loop");
379 return -EIO;
382 /* attempt the rename */
383 ret = vfs_rename(dir->d_inode, rep, cache->graveyard->d_inode, grave);
384 if (ret != 0 && ret != -ENOMEM)
385 cachefiles_io_error(cache, "Rename failed with error %d", ret);
387 if (preemptive)
388 cachefiles_mark_object_buried(cache, rep);
390 unlock_rename(cache->graveyard, dir);
391 dput(grave);
392 _leave(" = 0");
393 return 0;
397 * delete an object representation from the cache
399 int cachefiles_delete_object(struct cachefiles_cache *cache,
400 struct cachefiles_object *object)
402 struct dentry *dir;
403 int ret;
405 _enter(",OBJ%x{%p}", object->fscache.debug_id, object->dentry);
407 ASSERT(object->dentry);
408 ASSERT(object->dentry->d_inode);
409 ASSERT(object->dentry->d_parent);
411 dir = dget_parent(object->dentry);
413 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
415 if (test_bit(CACHEFILES_OBJECT_BURIED, &object->flags)) {
416 /* object allocation for the same key preemptively deleted this
417 * object's file so that it could create its own file */
418 _debug("object preemptively buried");
419 mutex_unlock(&dir->d_inode->i_mutex);
420 ret = 0;
421 } else {
422 /* we need to check that our parent is _still_ our parent - it
423 * may have been renamed */
424 if (dir == object->dentry->d_parent) {
425 ret = cachefiles_bury_object(cache, dir,
426 object->dentry, false);
427 } else {
428 /* it got moved, presumably by cachefilesd culling it,
429 * so it's no longer in the key path and we can ignore
430 * it */
431 mutex_unlock(&dir->d_inode->i_mutex);
432 ret = 0;
436 dput(dir);
437 _leave(" = %d", ret);
438 return ret;
442 * walk from the parent object to the child object through the backing
443 * filesystem, creating directories as we go
445 int cachefiles_walk_to_object(struct cachefiles_object *parent,
446 struct cachefiles_object *object,
447 const char *key,
448 struct cachefiles_xattr *auxdata)
450 struct cachefiles_cache *cache;
451 struct dentry *dir, *next = NULL;
452 unsigned long start;
453 const char *name;
454 int ret, nlen;
456 _enter("OBJ%x{%p},OBJ%x,%s,",
457 parent->fscache.debug_id, parent->dentry,
458 object->fscache.debug_id, key);
460 cache = container_of(parent->fscache.cache,
461 struct cachefiles_cache, cache);
463 ASSERT(parent->dentry);
464 ASSERT(parent->dentry->d_inode);
466 if (!(S_ISDIR(parent->dentry->d_inode->i_mode))) {
467 // TODO: convert file to dir
468 _leave("looking up in none directory");
469 return -ENOBUFS;
472 dir = dget(parent->dentry);
474 advance:
475 /* attempt to transit the first directory component */
476 name = key;
477 nlen = strlen(key);
479 /* key ends in a double NUL */
480 key = key + nlen + 1;
481 if (!*key)
482 key = NULL;
484 lookup_again:
485 /* search the current directory for the element name */
486 _debug("lookup '%s'", name);
488 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
490 start = jiffies;
491 next = lookup_one_len(name, dir, nlen);
492 cachefiles_hist(cachefiles_lookup_histogram, start);
493 if (IS_ERR(next))
494 goto lookup_error;
496 _debug("next -> %p %s", next, next->d_inode ? "positive" : "negative");
498 if (!key)
499 object->new = !next->d_inode;
501 /* if this element of the path doesn't exist, then the lookup phase
502 * failed, and we can release any readers in the certain knowledge that
503 * there's nothing for them to actually read */
504 if (!next->d_inode)
505 fscache_object_lookup_negative(&object->fscache);
507 /* we need to create the object if it's negative */
508 if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
509 /* index objects and intervening tree levels must be subdirs */
510 if (!next->d_inode) {
511 ret = cachefiles_has_space(cache, 1, 0);
512 if (ret < 0)
513 goto create_error;
515 start = jiffies;
516 ret = vfs_mkdir(dir->d_inode, next, 0);
517 cachefiles_hist(cachefiles_mkdir_histogram, start);
518 if (ret < 0)
519 goto create_error;
521 ASSERT(next->d_inode);
523 _debug("mkdir -> %p{%p{ino=%lu}}",
524 next, next->d_inode, next->d_inode->i_ino);
526 } else if (!S_ISDIR(next->d_inode->i_mode)) {
527 kerror("inode %lu is not a directory",
528 next->d_inode->i_ino);
529 ret = -ENOBUFS;
530 goto error;
533 } else {
534 /* non-index objects start out life as files */
535 if (!next->d_inode) {
536 ret = cachefiles_has_space(cache, 1, 0);
537 if (ret < 0)
538 goto create_error;
540 start = jiffies;
541 ret = vfs_create(dir->d_inode, next, S_IFREG, NULL);
542 cachefiles_hist(cachefiles_create_histogram, start);
543 if (ret < 0)
544 goto create_error;
546 ASSERT(next->d_inode);
548 _debug("create -> %p{%p{ino=%lu}}",
549 next, next->d_inode, next->d_inode->i_ino);
551 } else if (!S_ISDIR(next->d_inode->i_mode) &&
552 !S_ISREG(next->d_inode->i_mode)
554 kerror("inode %lu is not a file or directory",
555 next->d_inode->i_ino);
556 ret = -ENOBUFS;
557 goto error;
561 /* process the next component */
562 if (key) {
563 _debug("advance");
564 mutex_unlock(&dir->d_inode->i_mutex);
565 dput(dir);
566 dir = next;
567 next = NULL;
568 goto advance;
571 /* we've found the object we were looking for */
572 object->dentry = next;
574 /* if we've found that the terminal object exists, then we need to
575 * check its attributes and delete it if it's out of date */
576 if (!object->new) {
577 _debug("validate '%*.*s'",
578 next->d_name.len, next->d_name.len, next->d_name.name);
580 ret = cachefiles_check_object_xattr(object, auxdata);
581 if (ret == -ESTALE) {
582 /* delete the object (the deleter drops the directory
583 * mutex) */
584 object->dentry = NULL;
586 ret = cachefiles_bury_object(cache, dir, next, true);
587 dput(next);
588 next = NULL;
590 if (ret < 0)
591 goto delete_error;
593 _debug("redo lookup");
594 goto lookup_again;
598 /* note that we're now using this object */
599 ret = cachefiles_mark_object_active(cache, object);
601 mutex_unlock(&dir->d_inode->i_mutex);
602 dput(dir);
603 dir = NULL;
605 if (ret == -ETIMEDOUT)
606 goto mark_active_timed_out;
608 _debug("=== OBTAINED_OBJECT ===");
610 if (object->new) {
611 /* attach data to a newly constructed terminal object */
612 ret = cachefiles_set_object_xattr(object, auxdata);
613 if (ret < 0)
614 goto check_error;
615 } else {
616 /* always update the atime on an object we've just looked up
617 * (this is used to keep track of culling, and atimes are only
618 * updated by read, write and readdir but not lookup or
619 * open) */
620 touch_atime(cache->mnt, next);
623 /* open a file interface onto a data file */
624 if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
625 if (S_ISREG(object->dentry->d_inode->i_mode)) {
626 const struct address_space_operations *aops;
628 ret = -EPERM;
629 aops = object->dentry->d_inode->i_mapping->a_ops;
630 if (!aops->bmap)
631 goto check_error;
633 object->backer = object->dentry;
634 } else {
635 BUG(); // TODO: open file in data-class subdir
639 object->new = 0;
640 fscache_obtained_object(&object->fscache);
642 _leave(" = 0 [%lu]", object->dentry->d_inode->i_ino);
643 return 0;
645 create_error:
646 _debug("create error %d", ret);
647 if (ret == -EIO)
648 cachefiles_io_error(cache, "Create/mkdir failed");
649 goto error;
651 mark_active_timed_out:
652 _debug("mark active timed out");
653 goto release_dentry;
655 check_error:
656 _debug("check error %d", ret);
657 write_lock(&cache->active_lock);
658 rb_erase(&object->active_node, &cache->active_nodes);
659 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
660 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
661 write_unlock(&cache->active_lock);
662 release_dentry:
663 dput(object->dentry);
664 object->dentry = NULL;
665 goto error_out;
667 delete_error:
668 _debug("delete error %d", ret);
669 goto error_out2;
671 lookup_error:
672 _debug("lookup error %ld", PTR_ERR(next));
673 ret = PTR_ERR(next);
674 if (ret == -EIO)
675 cachefiles_io_error(cache, "Lookup failed");
676 next = NULL;
677 error:
678 mutex_unlock(&dir->d_inode->i_mutex);
679 dput(next);
680 error_out2:
681 dput(dir);
682 error_out:
683 _leave(" = error %d", -ret);
684 return ret;
688 * get a subdirectory
690 struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
691 struct dentry *dir,
692 const char *dirname)
694 struct dentry *subdir;
695 unsigned long start;
696 int ret;
698 _enter(",,%s", dirname);
700 /* search the current directory for the element name */
701 mutex_lock(&dir->d_inode->i_mutex);
703 start = jiffies;
704 subdir = lookup_one_len(dirname, dir, strlen(dirname));
705 cachefiles_hist(cachefiles_lookup_histogram, start);
706 if (IS_ERR(subdir)) {
707 if (PTR_ERR(subdir) == -ENOMEM)
708 goto nomem_d_alloc;
709 goto lookup_error;
712 _debug("subdir -> %p %s",
713 subdir, subdir->d_inode ? "positive" : "negative");
715 /* we need to create the subdir if it doesn't exist yet */
716 if (!subdir->d_inode) {
717 ret = cachefiles_has_space(cache, 1, 0);
718 if (ret < 0)
719 goto mkdir_error;
721 _debug("attempt mkdir");
723 ret = vfs_mkdir(dir->d_inode, subdir, 0700);
724 if (ret < 0)
725 goto mkdir_error;
727 ASSERT(subdir->d_inode);
729 _debug("mkdir -> %p{%p{ino=%lu}}",
730 subdir,
731 subdir->d_inode,
732 subdir->d_inode->i_ino);
735 mutex_unlock(&dir->d_inode->i_mutex);
737 /* we need to make sure the subdir is a directory */
738 ASSERT(subdir->d_inode);
740 if (!S_ISDIR(subdir->d_inode->i_mode)) {
741 kerror("%s is not a directory", dirname);
742 ret = -EIO;
743 goto check_error;
746 ret = -EPERM;
747 if (!subdir->d_inode->i_op ||
748 !subdir->d_inode->i_op->setxattr ||
749 !subdir->d_inode->i_op->getxattr ||
750 !subdir->d_inode->i_op->lookup ||
751 !subdir->d_inode->i_op->mkdir ||
752 !subdir->d_inode->i_op->create ||
753 !subdir->d_inode->i_op->rename ||
754 !subdir->d_inode->i_op->rmdir ||
755 !subdir->d_inode->i_op->unlink)
756 goto check_error;
758 _leave(" = [%lu]", subdir->d_inode->i_ino);
759 return subdir;
761 check_error:
762 dput(subdir);
763 _leave(" = %d [check]", ret);
764 return ERR_PTR(ret);
766 mkdir_error:
767 mutex_unlock(&dir->d_inode->i_mutex);
768 dput(subdir);
769 kerror("mkdir %s failed with error %d", dirname, ret);
770 return ERR_PTR(ret);
772 lookup_error:
773 mutex_unlock(&dir->d_inode->i_mutex);
774 ret = PTR_ERR(subdir);
775 kerror("Lookup %s failed with error %d", dirname, ret);
776 return ERR_PTR(ret);
778 nomem_d_alloc:
779 mutex_unlock(&dir->d_inode->i_mutex);
780 _leave(" = -ENOMEM");
781 return ERR_PTR(-ENOMEM);
785 * find out if an object is in use or not
786 * - if finds object and it's not in use:
787 * - returns a pointer to the object and a reference on it
788 * - returns with the directory locked
790 static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
791 struct dentry *dir,
792 char *filename)
794 struct cachefiles_object *object;
795 struct rb_node *_n;
796 struct dentry *victim;
797 unsigned long start;
798 int ret;
800 //_enter(",%*.*s/,%s",
801 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
803 /* look up the victim */
804 mutex_lock_nested(&dir->d_inode->i_mutex, 1);
806 start = jiffies;
807 victim = lookup_one_len(filename, dir, strlen(filename));
808 cachefiles_hist(cachefiles_lookup_histogram, start);
809 if (IS_ERR(victim))
810 goto lookup_error;
812 //_debug("victim -> %p %s",
813 // victim, victim->d_inode ? "positive" : "negative");
815 /* if the object is no longer there then we probably retired the object
816 * at the netfs's request whilst the cull was in progress
818 if (!victim->d_inode) {
819 mutex_unlock(&dir->d_inode->i_mutex);
820 dput(victim);
821 _leave(" = -ENOENT [absent]");
822 return ERR_PTR(-ENOENT);
825 /* check to see if we're using this object */
826 read_lock(&cache->active_lock);
828 _n = cache->active_nodes.rb_node;
830 while (_n) {
831 object = rb_entry(_n, struct cachefiles_object, active_node);
833 if (object->dentry > victim)
834 _n = _n->rb_left;
835 else if (object->dentry < victim)
836 _n = _n->rb_right;
837 else
838 goto object_in_use;
841 read_unlock(&cache->active_lock);
843 //_leave(" = %p", victim);
844 return victim;
846 object_in_use:
847 read_unlock(&cache->active_lock);
848 mutex_unlock(&dir->d_inode->i_mutex);
849 dput(victim);
850 //_leave(" = -EBUSY [in use]");
851 return ERR_PTR(-EBUSY);
853 lookup_error:
854 mutex_unlock(&dir->d_inode->i_mutex);
855 ret = PTR_ERR(victim);
856 if (ret == -ENOENT) {
857 /* file or dir now absent - probably retired by netfs */
858 _leave(" = -ESTALE [absent]");
859 return ERR_PTR(-ESTALE);
862 if (ret == -EIO) {
863 cachefiles_io_error(cache, "Lookup failed");
864 } else if (ret != -ENOMEM) {
865 kerror("Internal error: %d", ret);
866 ret = -EIO;
869 _leave(" = %d", ret);
870 return ERR_PTR(ret);
874 * cull an object if it's not in use
875 * - called only by cache manager daemon
877 int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
878 char *filename)
880 struct dentry *victim;
881 int ret;
883 _enter(",%*.*s/,%s",
884 dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
886 victim = cachefiles_check_active(cache, dir, filename);
887 if (IS_ERR(victim))
888 return PTR_ERR(victim);
890 _debug("victim -> %p %s",
891 victim, victim->d_inode ? "positive" : "negative");
893 /* okay... the victim is not being used so we can cull it
894 * - start by marking it as stale
896 _debug("victim is cullable");
898 ret = cachefiles_remove_object_xattr(cache, victim);
899 if (ret < 0)
900 goto error_unlock;
902 /* actually remove the victim (drops the dir mutex) */
903 _debug("bury");
905 ret = cachefiles_bury_object(cache, dir, victim, false);
906 if (ret < 0)
907 goto error;
909 dput(victim);
910 _leave(" = 0");
911 return 0;
913 error_unlock:
914 mutex_unlock(&dir->d_inode->i_mutex);
915 error:
916 dput(victim);
917 if (ret == -ENOENT) {
918 /* file or dir now absent - probably retired by netfs */
919 _leave(" = -ESTALE [absent]");
920 return -ESTALE;
923 if (ret != -ENOMEM) {
924 kerror("Internal error: %d", ret);
925 ret = -EIO;
928 _leave(" = %d", ret);
929 return ret;
933 * find out if an object is in use or not
934 * - called only by cache manager daemon
935 * - returns -EBUSY or 0 to indicate whether an object is in use or not
937 int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
938 char *filename)
940 struct dentry *victim;
942 //_enter(",%*.*s/,%s",
943 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
945 victim = cachefiles_check_active(cache, dir, filename);
946 if (IS_ERR(victim))
947 return PTR_ERR(victim);
949 mutex_unlock(&dir->d_inode->i_mutex);
950 dput(victim);
951 //_leave(" = 0");
952 return 0;