Revert "b43: Enforce DMA descriptor memory constraints"
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / cachefiles / namei.c
blob14ac4806e2913f8c37e76d95dc1d0274f4bbd027
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 "internal.h"
24 #define CACHEFILES_KEYBUF_SIZE 512
27 * dump debugging info about an object
29 static noinline
30 void __cachefiles_printk_object(struct cachefiles_object *object,
31 const char *prefix,
32 u8 *keybuf)
34 struct fscache_cookie *cookie;
35 unsigned keylen, loop;
37 printk(KERN_ERR "%sobject: OBJ%x\n",
38 prefix, object->fscache.debug_id);
39 printk(KERN_ERR "%sobjstate=%s fl=%lx swfl=%lx ev=%lx[%lx]\n",
40 prefix, fscache_object_states[object->fscache.state],
41 object->fscache.flags, object->fscache.work.flags,
42 object->fscache.events,
43 object->fscache.event_mask & FSCACHE_OBJECT_EVENTS_MASK);
44 printk(KERN_ERR "%sops=%u inp=%u exc=%u\n",
45 prefix, object->fscache.n_ops, object->fscache.n_in_progress,
46 object->fscache.n_exclusive);
47 printk(KERN_ERR "%sparent=%p\n",
48 prefix, object->fscache.parent);
50 spin_lock(&object->fscache.lock);
51 cookie = object->fscache.cookie;
52 if (cookie) {
53 printk(KERN_ERR "%scookie=%p [pr=%p nd=%p fl=%lx]\n",
54 prefix,
55 object->fscache.cookie,
56 object->fscache.cookie->parent,
57 object->fscache.cookie->netfs_data,
58 object->fscache.cookie->flags);
59 if (keybuf)
60 keylen = cookie->def->get_key(cookie->netfs_data, keybuf,
61 CACHEFILES_KEYBUF_SIZE);
62 else
63 keylen = 0;
64 } else {
65 printk(KERN_ERR "%scookie=NULL\n", prefix);
66 keylen = 0;
68 spin_unlock(&object->fscache.lock);
70 if (keylen) {
71 printk(KERN_ERR "%skey=[%u] '", prefix, keylen);
72 for (loop = 0; loop < keylen; loop++)
73 printk("%02x", keybuf[loop]);
74 printk("'\n");
79 * dump debugging info about a pair of objects
81 static noinline void cachefiles_printk_object(struct cachefiles_object *object,
82 struct cachefiles_object *xobject)
84 u8 *keybuf;
86 keybuf = kmalloc(CACHEFILES_KEYBUF_SIZE, GFP_NOIO);
87 if (object)
88 __cachefiles_printk_object(object, "", keybuf);
89 if (xobject)
90 __cachefiles_printk_object(xobject, "x", keybuf);
91 kfree(keybuf);
95 * record the fact that an object is now active
97 static int cachefiles_mark_object_active(struct cachefiles_cache *cache,
98 struct cachefiles_object *object)
100 struct cachefiles_object *xobject;
101 struct rb_node **_p, *_parent = NULL;
102 struct dentry *dentry;
104 _enter(",%p", object);
106 try_again:
107 write_lock(&cache->active_lock);
109 if (test_and_set_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
110 printk(KERN_ERR "CacheFiles: Error: Object already active\n");
111 cachefiles_printk_object(object, NULL);
112 BUG();
115 dentry = object->dentry;
116 _p = &cache->active_nodes.rb_node;
117 while (*_p) {
118 _parent = *_p;
119 xobject = rb_entry(_parent,
120 struct cachefiles_object, active_node);
122 ASSERT(xobject != object);
124 if (xobject->dentry > dentry)
125 _p = &(*_p)->rb_left;
126 else if (xobject->dentry < dentry)
127 _p = &(*_p)->rb_right;
128 else
129 goto wait_for_old_object;
132 rb_link_node(&object->active_node, _parent, _p);
133 rb_insert_color(&object->active_node, &cache->active_nodes);
135 write_unlock(&cache->active_lock);
136 _leave(" = 0");
137 return 0;
139 /* an old object from a previous incarnation is hogging the slot - we
140 * need to wait for it to be destroyed */
141 wait_for_old_object:
142 if (xobject->fscache.state < FSCACHE_OBJECT_DYING) {
143 printk(KERN_ERR "\n");
144 printk(KERN_ERR "CacheFiles: Error:"
145 " Unexpected object collision\n");
146 cachefiles_printk_object(object, xobject);
147 BUG();
149 atomic_inc(&xobject->usage);
150 write_unlock(&cache->active_lock);
152 if (test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
153 wait_queue_head_t *wq;
155 signed long timeout = 60 * HZ;
156 wait_queue_t wait;
157 bool requeue;
159 /* if the object we're waiting for is queued for processing,
160 * then just put ourselves on the queue behind it */
161 if (slow_work_is_queued(&xobject->fscache.work)) {
162 _debug("queue OBJ%x behind OBJ%x immediately",
163 object->fscache.debug_id,
164 xobject->fscache.debug_id);
165 goto requeue;
168 /* otherwise we sleep until either the object we're waiting for
169 * is done, or the slow-work facility wants the thread back to
170 * do other work */
171 wq = bit_waitqueue(&xobject->flags, CACHEFILES_OBJECT_ACTIVE);
172 init_wait(&wait);
173 requeue = false;
174 do {
175 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
176 if (!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags))
177 break;
178 requeue = slow_work_sleep_till_thread_needed(
179 &object->fscache.work, &timeout);
180 } while (timeout > 0 && !requeue);
181 finish_wait(wq, &wait);
183 if (requeue &&
184 test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
185 _debug("queue OBJ%x behind OBJ%x after wait",
186 object->fscache.debug_id,
187 xobject->fscache.debug_id);
188 goto requeue;
191 if (timeout <= 0) {
192 printk(KERN_ERR "\n");
193 printk(KERN_ERR "CacheFiles: Error: Overlong"
194 " wait for old active object to go away\n");
195 cachefiles_printk_object(object, xobject);
196 goto requeue;
200 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags));
202 cache->cache.ops->put_object(&xobject->fscache);
203 goto try_again;
205 requeue:
206 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
207 cache->cache.ops->put_object(&xobject->fscache);
208 _leave(" = -ETIMEDOUT");
209 return -ETIMEDOUT;
213 * delete an object representation from the cache
214 * - file backed objects are unlinked
215 * - directory backed objects are stuffed into the graveyard for userspace to
216 * delete
217 * - unlocks the directory mutex
219 static int cachefiles_bury_object(struct cachefiles_cache *cache,
220 struct dentry *dir,
221 struct dentry *rep)
223 struct dentry *grave, *trap;
224 char nbuffer[8 + 8 + 1];
225 int ret;
227 _enter(",'%*.*s','%*.*s'",
228 dir->d_name.len, dir->d_name.len, dir->d_name.name,
229 rep->d_name.len, rep->d_name.len, rep->d_name.name);
231 /* non-directories can just be unlinked */
232 if (!S_ISDIR(rep->d_inode->i_mode)) {
233 _debug("unlink stale object");
234 ret = vfs_unlink(dir->d_inode, rep);
236 mutex_unlock(&dir->d_inode->i_mutex);
238 if (ret == -EIO)
239 cachefiles_io_error(cache, "Unlink failed");
241 _leave(" = %d", ret);
242 return ret;
245 /* directories have to be moved to the graveyard */
246 _debug("move stale object to graveyard");
247 mutex_unlock(&dir->d_inode->i_mutex);
249 try_again:
250 /* first step is to make up a grave dentry in the graveyard */
251 sprintf(nbuffer, "%08x%08x",
252 (uint32_t) get_seconds(),
253 (uint32_t) atomic_inc_return(&cache->gravecounter));
255 /* do the multiway lock magic */
256 trap = lock_rename(cache->graveyard, dir);
258 /* do some checks before getting the grave dentry */
259 if (rep->d_parent != dir) {
260 /* the entry was probably culled when we dropped the parent dir
261 * lock */
262 unlock_rename(cache->graveyard, dir);
263 _leave(" = 0 [culled?]");
264 return 0;
267 if (!S_ISDIR(cache->graveyard->d_inode->i_mode)) {
268 unlock_rename(cache->graveyard, dir);
269 cachefiles_io_error(cache, "Graveyard no longer a directory");
270 return -EIO;
273 if (trap == rep) {
274 unlock_rename(cache->graveyard, dir);
275 cachefiles_io_error(cache, "May not make directory loop");
276 return -EIO;
279 if (d_mountpoint(rep)) {
280 unlock_rename(cache->graveyard, dir);
281 cachefiles_io_error(cache, "Mountpoint in cache");
282 return -EIO;
285 grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
286 if (IS_ERR(grave)) {
287 unlock_rename(cache->graveyard, dir);
289 if (PTR_ERR(grave) == -ENOMEM) {
290 _leave(" = -ENOMEM");
291 return -ENOMEM;
294 cachefiles_io_error(cache, "Lookup error %ld",
295 PTR_ERR(grave));
296 return -EIO;
299 if (grave->d_inode) {
300 unlock_rename(cache->graveyard, dir);
301 dput(grave);
302 grave = NULL;
303 cond_resched();
304 goto try_again;
307 if (d_mountpoint(grave)) {
308 unlock_rename(cache->graveyard, dir);
309 dput(grave);
310 cachefiles_io_error(cache, "Mountpoint in graveyard");
311 return -EIO;
314 /* target should not be an ancestor of source */
315 if (trap == grave) {
316 unlock_rename(cache->graveyard, dir);
317 dput(grave);
318 cachefiles_io_error(cache, "May not make directory loop");
319 return -EIO;
322 /* attempt the rename */
323 ret = vfs_rename(dir->d_inode, rep, cache->graveyard->d_inode, grave);
324 if (ret != 0 && ret != -ENOMEM)
325 cachefiles_io_error(cache, "Rename failed with error %d", ret);
327 unlock_rename(cache->graveyard, dir);
328 dput(grave);
329 _leave(" = 0");
330 return 0;
334 * delete an object representation from the cache
336 int cachefiles_delete_object(struct cachefiles_cache *cache,
337 struct cachefiles_object *object)
339 struct dentry *dir;
340 int ret;
342 _enter(",{%p}", object->dentry);
344 ASSERT(object->dentry);
345 ASSERT(object->dentry->d_inode);
346 ASSERT(object->dentry->d_parent);
348 dir = dget_parent(object->dentry);
350 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
351 ret = cachefiles_bury_object(cache, dir, object->dentry);
353 dput(dir);
354 _leave(" = %d", ret);
355 return ret;
359 * walk from the parent object to the child object through the backing
360 * filesystem, creating directories as we go
362 int cachefiles_walk_to_object(struct cachefiles_object *parent,
363 struct cachefiles_object *object,
364 const char *key,
365 struct cachefiles_xattr *auxdata)
367 struct cachefiles_cache *cache;
368 struct dentry *dir, *next = NULL;
369 unsigned long start;
370 const char *name;
371 int ret, nlen;
373 _enter("{%p},,%s,", parent->dentry, key);
375 cache = container_of(parent->fscache.cache,
376 struct cachefiles_cache, cache);
378 ASSERT(parent->dentry);
379 ASSERT(parent->dentry->d_inode);
381 if (!(S_ISDIR(parent->dentry->d_inode->i_mode))) {
382 // TODO: convert file to dir
383 _leave("looking up in none directory");
384 return -ENOBUFS;
387 dir = dget(parent->dentry);
389 advance:
390 /* attempt to transit the first directory component */
391 name = key;
392 nlen = strlen(key);
394 /* key ends in a double NUL */
395 key = key + nlen + 1;
396 if (!*key)
397 key = NULL;
399 lookup_again:
400 /* search the current directory for the element name */
401 _debug("lookup '%s'", name);
403 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
405 start = jiffies;
406 next = lookup_one_len(name, dir, nlen);
407 cachefiles_hist(cachefiles_lookup_histogram, start);
408 if (IS_ERR(next))
409 goto lookup_error;
411 _debug("next -> %p %s", next, next->d_inode ? "positive" : "negative");
413 if (!key)
414 object->new = !next->d_inode;
416 /* if this element of the path doesn't exist, then the lookup phase
417 * failed, and we can release any readers in the certain knowledge that
418 * there's nothing for them to actually read */
419 if (!next->d_inode)
420 fscache_object_lookup_negative(&object->fscache);
422 /* we need to create the object if it's negative */
423 if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
424 /* index objects and intervening tree levels must be subdirs */
425 if (!next->d_inode) {
426 ret = cachefiles_has_space(cache, 1, 0);
427 if (ret < 0)
428 goto create_error;
430 start = jiffies;
431 ret = vfs_mkdir(dir->d_inode, next, 0);
432 cachefiles_hist(cachefiles_mkdir_histogram, start);
433 if (ret < 0)
434 goto create_error;
436 ASSERT(next->d_inode);
438 _debug("mkdir -> %p{%p{ino=%lu}}",
439 next, next->d_inode, next->d_inode->i_ino);
441 } else if (!S_ISDIR(next->d_inode->i_mode)) {
442 kerror("inode %lu is not a directory",
443 next->d_inode->i_ino);
444 ret = -ENOBUFS;
445 goto error;
448 } else {
449 /* non-index objects start out life as files */
450 if (!next->d_inode) {
451 ret = cachefiles_has_space(cache, 1, 0);
452 if (ret < 0)
453 goto create_error;
455 start = jiffies;
456 ret = vfs_create(dir->d_inode, next, S_IFREG, NULL);
457 cachefiles_hist(cachefiles_create_histogram, start);
458 if (ret < 0)
459 goto create_error;
461 ASSERT(next->d_inode);
463 _debug("create -> %p{%p{ino=%lu}}",
464 next, next->d_inode, next->d_inode->i_ino);
466 } else if (!S_ISDIR(next->d_inode->i_mode) &&
467 !S_ISREG(next->d_inode->i_mode)
469 kerror("inode %lu is not a file or directory",
470 next->d_inode->i_ino);
471 ret = -ENOBUFS;
472 goto error;
476 /* process the next component */
477 if (key) {
478 _debug("advance");
479 mutex_unlock(&dir->d_inode->i_mutex);
480 dput(dir);
481 dir = next;
482 next = NULL;
483 goto advance;
486 /* we've found the object we were looking for */
487 object->dentry = next;
489 /* if we've found that the terminal object exists, then we need to
490 * check its attributes and delete it if it's out of date */
491 if (!object->new) {
492 _debug("validate '%*.*s'",
493 next->d_name.len, next->d_name.len, next->d_name.name);
495 ret = cachefiles_check_object_xattr(object, auxdata);
496 if (ret == -ESTALE) {
497 /* delete the object (the deleter drops the directory
498 * mutex) */
499 object->dentry = NULL;
501 ret = cachefiles_bury_object(cache, dir, next);
502 dput(next);
503 next = NULL;
505 if (ret < 0)
506 goto delete_error;
508 _debug("redo lookup");
509 goto lookup_again;
513 /* note that we're now using this object */
514 ret = cachefiles_mark_object_active(cache, object);
516 mutex_unlock(&dir->d_inode->i_mutex);
517 dput(dir);
518 dir = NULL;
520 if (ret == -ETIMEDOUT)
521 goto mark_active_timed_out;
523 _debug("=== OBTAINED_OBJECT ===");
525 if (object->new) {
526 /* attach data to a newly constructed terminal object */
527 ret = cachefiles_set_object_xattr(object, auxdata);
528 if (ret < 0)
529 goto check_error;
530 } else {
531 /* always update the atime on an object we've just looked up
532 * (this is used to keep track of culling, and atimes are only
533 * updated by read, write and readdir but not lookup or
534 * open) */
535 touch_atime(cache->mnt, next);
538 /* open a file interface onto a data file */
539 if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
540 if (S_ISREG(object->dentry->d_inode->i_mode)) {
541 const struct address_space_operations *aops;
543 ret = -EPERM;
544 aops = object->dentry->d_inode->i_mapping->a_ops;
545 if (!aops->bmap)
546 goto check_error;
548 object->backer = object->dentry;
549 } else {
550 BUG(); // TODO: open file in data-class subdir
554 object->new = 0;
555 fscache_obtained_object(&object->fscache);
557 _leave(" = 0 [%lu]", object->dentry->d_inode->i_ino);
558 return 0;
560 create_error:
561 _debug("create error %d", ret);
562 if (ret == -EIO)
563 cachefiles_io_error(cache, "Create/mkdir failed");
564 goto error;
566 mark_active_timed_out:
567 _debug("mark active timed out");
568 goto release_dentry;
570 check_error:
571 _debug("check error %d", ret);
572 write_lock(&cache->active_lock);
573 rb_erase(&object->active_node, &cache->active_nodes);
574 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
575 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
576 write_unlock(&cache->active_lock);
577 release_dentry:
578 dput(object->dentry);
579 object->dentry = NULL;
580 goto error_out;
582 delete_error:
583 _debug("delete error %d", ret);
584 goto error_out2;
586 lookup_error:
587 _debug("lookup error %ld", PTR_ERR(next));
588 ret = PTR_ERR(next);
589 if (ret == -EIO)
590 cachefiles_io_error(cache, "Lookup failed");
591 next = NULL;
592 error:
593 mutex_unlock(&dir->d_inode->i_mutex);
594 dput(next);
595 error_out2:
596 dput(dir);
597 error_out:
598 _leave(" = error %d", -ret);
599 return ret;
603 * get a subdirectory
605 struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
606 struct dentry *dir,
607 const char *dirname)
609 struct dentry *subdir;
610 unsigned long start;
611 int ret;
613 _enter(",,%s", dirname);
615 /* search the current directory for the element name */
616 mutex_lock(&dir->d_inode->i_mutex);
618 start = jiffies;
619 subdir = lookup_one_len(dirname, dir, strlen(dirname));
620 cachefiles_hist(cachefiles_lookup_histogram, start);
621 if (IS_ERR(subdir)) {
622 if (PTR_ERR(subdir) == -ENOMEM)
623 goto nomem_d_alloc;
624 goto lookup_error;
627 _debug("subdir -> %p %s",
628 subdir, subdir->d_inode ? "positive" : "negative");
630 /* we need to create the subdir if it doesn't exist yet */
631 if (!subdir->d_inode) {
632 ret = cachefiles_has_space(cache, 1, 0);
633 if (ret < 0)
634 goto mkdir_error;
636 _debug("attempt mkdir");
638 ret = vfs_mkdir(dir->d_inode, subdir, 0700);
639 if (ret < 0)
640 goto mkdir_error;
642 ASSERT(subdir->d_inode);
644 _debug("mkdir -> %p{%p{ino=%lu}}",
645 subdir,
646 subdir->d_inode,
647 subdir->d_inode->i_ino);
650 mutex_unlock(&dir->d_inode->i_mutex);
652 /* we need to make sure the subdir is a directory */
653 ASSERT(subdir->d_inode);
655 if (!S_ISDIR(subdir->d_inode->i_mode)) {
656 kerror("%s is not a directory", dirname);
657 ret = -EIO;
658 goto check_error;
661 ret = -EPERM;
662 if (!subdir->d_inode->i_op ||
663 !subdir->d_inode->i_op->setxattr ||
664 !subdir->d_inode->i_op->getxattr ||
665 !subdir->d_inode->i_op->lookup ||
666 !subdir->d_inode->i_op->mkdir ||
667 !subdir->d_inode->i_op->create ||
668 !subdir->d_inode->i_op->rename ||
669 !subdir->d_inode->i_op->rmdir ||
670 !subdir->d_inode->i_op->unlink)
671 goto check_error;
673 _leave(" = [%lu]", subdir->d_inode->i_ino);
674 return subdir;
676 check_error:
677 dput(subdir);
678 _leave(" = %d [check]", ret);
679 return ERR_PTR(ret);
681 mkdir_error:
682 mutex_unlock(&dir->d_inode->i_mutex);
683 dput(subdir);
684 kerror("mkdir %s failed with error %d", dirname, ret);
685 return ERR_PTR(ret);
687 lookup_error:
688 mutex_unlock(&dir->d_inode->i_mutex);
689 ret = PTR_ERR(subdir);
690 kerror("Lookup %s failed with error %d", dirname, ret);
691 return ERR_PTR(ret);
693 nomem_d_alloc:
694 mutex_unlock(&dir->d_inode->i_mutex);
695 _leave(" = -ENOMEM");
696 return ERR_PTR(-ENOMEM);
700 * find out if an object is in use or not
701 * - if finds object and it's not in use:
702 * - returns a pointer to the object and a reference on it
703 * - returns with the directory locked
705 static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
706 struct dentry *dir,
707 char *filename)
709 struct cachefiles_object *object;
710 struct rb_node *_n;
711 struct dentry *victim;
712 unsigned long start;
713 int ret;
715 //_enter(",%*.*s/,%s",
716 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
718 /* look up the victim */
719 mutex_lock_nested(&dir->d_inode->i_mutex, 1);
721 start = jiffies;
722 victim = lookup_one_len(filename, dir, strlen(filename));
723 cachefiles_hist(cachefiles_lookup_histogram, start);
724 if (IS_ERR(victim))
725 goto lookup_error;
727 //_debug("victim -> %p %s",
728 // victim, victim->d_inode ? "positive" : "negative");
730 /* if the object is no longer there then we probably retired the object
731 * at the netfs's request whilst the cull was in progress
733 if (!victim->d_inode) {
734 mutex_unlock(&dir->d_inode->i_mutex);
735 dput(victim);
736 _leave(" = -ENOENT [absent]");
737 return ERR_PTR(-ENOENT);
740 /* check to see if we're using this object */
741 read_lock(&cache->active_lock);
743 _n = cache->active_nodes.rb_node;
745 while (_n) {
746 object = rb_entry(_n, struct cachefiles_object, active_node);
748 if (object->dentry > victim)
749 _n = _n->rb_left;
750 else if (object->dentry < victim)
751 _n = _n->rb_right;
752 else
753 goto object_in_use;
756 read_unlock(&cache->active_lock);
758 //_leave(" = %p", victim);
759 return victim;
761 object_in_use:
762 read_unlock(&cache->active_lock);
763 mutex_unlock(&dir->d_inode->i_mutex);
764 dput(victim);
765 //_leave(" = -EBUSY [in use]");
766 return ERR_PTR(-EBUSY);
768 lookup_error:
769 mutex_unlock(&dir->d_inode->i_mutex);
770 ret = PTR_ERR(victim);
771 if (ret == -ENOENT) {
772 /* file or dir now absent - probably retired by netfs */
773 _leave(" = -ESTALE [absent]");
774 return ERR_PTR(-ESTALE);
777 if (ret == -EIO) {
778 cachefiles_io_error(cache, "Lookup failed");
779 } else if (ret != -ENOMEM) {
780 kerror("Internal error: %d", ret);
781 ret = -EIO;
784 _leave(" = %d", ret);
785 return ERR_PTR(ret);
789 * cull an object if it's not in use
790 * - called only by cache manager daemon
792 int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
793 char *filename)
795 struct dentry *victim;
796 int ret;
798 _enter(",%*.*s/,%s",
799 dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
801 victim = cachefiles_check_active(cache, dir, filename);
802 if (IS_ERR(victim))
803 return PTR_ERR(victim);
805 _debug("victim -> %p %s",
806 victim, victim->d_inode ? "positive" : "negative");
808 /* okay... the victim is not being used so we can cull it
809 * - start by marking it as stale
811 _debug("victim is cullable");
813 ret = cachefiles_remove_object_xattr(cache, victim);
814 if (ret < 0)
815 goto error_unlock;
817 /* actually remove the victim (drops the dir mutex) */
818 _debug("bury");
820 ret = cachefiles_bury_object(cache, dir, victim);
821 if (ret < 0)
822 goto error;
824 dput(victim);
825 _leave(" = 0");
826 return 0;
828 error_unlock:
829 mutex_unlock(&dir->d_inode->i_mutex);
830 error:
831 dput(victim);
832 if (ret == -ENOENT) {
833 /* file or dir now absent - probably retired by netfs */
834 _leave(" = -ESTALE [absent]");
835 return -ESTALE;
838 if (ret != -ENOMEM) {
839 kerror("Internal error: %d", ret);
840 ret = -EIO;
843 _leave(" = %d", ret);
844 return ret;
848 * find out if an object is in use or not
849 * - called only by cache manager daemon
850 * - returns -EBUSY or 0 to indicate whether an object is in use or not
852 int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
853 char *filename)
855 struct dentry *victim;
857 //_enter(",%*.*s/,%s",
858 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
860 victim = cachefiles_check_active(cache, dir, filename);
861 if (IS_ERR(victim))
862 return PTR_ERR(victim);
864 mutex_unlock(&dir->d_inode->i_mutex);
865 dput(victim);
866 //_leave(" = 0");
867 return 0;