Merge branch 'sched-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6/cjktty.git] / fs / cachefiles / namei.c
blob42c7fafc8bfeff5d33e8ccf36a039323f851d12f
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 wbusy=%x ev=%lx[%lx]\n",
41 prefix, fscache_object_states[object->fscache.state],
42 object->fscache.flags, work_busy(&object->fscache.work),
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 (work_pending(&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 fscache_object is congested */
224 wq = bit_waitqueue(&xobject->flags, CACHEFILES_OBJECT_ACTIVE);
225 init_wait(&wait);
226 requeue = false;
227 do {
228 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
229 if (!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags))
230 break;
232 requeue = fscache_object_sleep_till_congested(&timeout);
233 } while (timeout > 0 && !requeue);
234 finish_wait(wq, &wait);
236 if (requeue &&
237 test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
238 _debug("queue OBJ%x behind OBJ%x after wait",
239 object->fscache.debug_id,
240 xobject->fscache.debug_id);
241 goto requeue;
244 if (timeout <= 0) {
245 printk(KERN_ERR "\n");
246 printk(KERN_ERR "CacheFiles: Error: Overlong"
247 " wait for old active object to go away\n");
248 cachefiles_printk_object(object, xobject);
249 goto requeue;
253 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags));
255 cache->cache.ops->put_object(&xobject->fscache);
256 goto try_again;
258 requeue:
259 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
260 cache->cache.ops->put_object(&xobject->fscache);
261 _leave(" = -ETIMEDOUT");
262 return -ETIMEDOUT;
266 * delete an object representation from the cache
267 * - file backed objects are unlinked
268 * - directory backed objects are stuffed into the graveyard for userspace to
269 * delete
270 * - unlocks the directory mutex
272 static int cachefiles_bury_object(struct cachefiles_cache *cache,
273 struct dentry *dir,
274 struct dentry *rep,
275 bool preemptive)
277 struct dentry *grave, *trap;
278 char nbuffer[8 + 8 + 1];
279 int ret;
281 _enter(",'%*.*s','%*.*s'",
282 dir->d_name.len, dir->d_name.len, dir->d_name.name,
283 rep->d_name.len, rep->d_name.len, rep->d_name.name);
285 _debug("remove %p from %p", rep, dir);
287 /* non-directories can just be unlinked */
288 if (!S_ISDIR(rep->d_inode->i_mode)) {
289 _debug("unlink stale object");
290 ret = vfs_unlink(dir->d_inode, rep);
292 if (preemptive)
293 cachefiles_mark_object_buried(cache, rep);
295 mutex_unlock(&dir->d_inode->i_mutex);
297 if (ret == -EIO)
298 cachefiles_io_error(cache, "Unlink failed");
300 _leave(" = %d", ret);
301 return ret;
304 /* directories have to be moved to the graveyard */
305 _debug("move stale object to graveyard");
306 mutex_unlock(&dir->d_inode->i_mutex);
308 try_again:
309 /* first step is to make up a grave dentry in the graveyard */
310 sprintf(nbuffer, "%08x%08x",
311 (uint32_t) get_seconds(),
312 (uint32_t) atomic_inc_return(&cache->gravecounter));
314 /* do the multiway lock magic */
315 trap = lock_rename(cache->graveyard, dir);
317 /* do some checks before getting the grave dentry */
318 if (rep->d_parent != dir) {
319 /* the entry was probably culled when we dropped the parent dir
320 * lock */
321 unlock_rename(cache->graveyard, dir);
322 _leave(" = 0 [culled?]");
323 return 0;
326 if (!S_ISDIR(cache->graveyard->d_inode->i_mode)) {
327 unlock_rename(cache->graveyard, dir);
328 cachefiles_io_error(cache, "Graveyard no longer a directory");
329 return -EIO;
332 if (trap == rep) {
333 unlock_rename(cache->graveyard, dir);
334 cachefiles_io_error(cache, "May not make directory loop");
335 return -EIO;
338 if (d_mountpoint(rep)) {
339 unlock_rename(cache->graveyard, dir);
340 cachefiles_io_error(cache, "Mountpoint in cache");
341 return -EIO;
344 grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
345 if (IS_ERR(grave)) {
346 unlock_rename(cache->graveyard, dir);
348 if (PTR_ERR(grave) == -ENOMEM) {
349 _leave(" = -ENOMEM");
350 return -ENOMEM;
353 cachefiles_io_error(cache, "Lookup error %ld",
354 PTR_ERR(grave));
355 return -EIO;
358 if (grave->d_inode) {
359 unlock_rename(cache->graveyard, dir);
360 dput(grave);
361 grave = NULL;
362 cond_resched();
363 goto try_again;
366 if (d_mountpoint(grave)) {
367 unlock_rename(cache->graveyard, dir);
368 dput(grave);
369 cachefiles_io_error(cache, "Mountpoint in graveyard");
370 return -EIO;
373 /* target should not be an ancestor of source */
374 if (trap == grave) {
375 unlock_rename(cache->graveyard, dir);
376 dput(grave);
377 cachefiles_io_error(cache, "May not make directory loop");
378 return -EIO;
381 /* attempt the rename */
382 ret = vfs_rename(dir->d_inode, rep, cache->graveyard->d_inode, grave);
383 if (ret != 0 && ret != -ENOMEM)
384 cachefiles_io_error(cache, "Rename failed with error %d", ret);
386 if (preemptive)
387 cachefiles_mark_object_buried(cache, rep);
389 unlock_rename(cache->graveyard, dir);
390 dput(grave);
391 _leave(" = 0");
392 return 0;
396 * delete an object representation from the cache
398 int cachefiles_delete_object(struct cachefiles_cache *cache,
399 struct cachefiles_object *object)
401 struct dentry *dir;
402 int ret;
404 _enter(",OBJ%x{%p}", object->fscache.debug_id, object->dentry);
406 ASSERT(object->dentry);
407 ASSERT(object->dentry->d_inode);
408 ASSERT(object->dentry->d_parent);
410 dir = dget_parent(object->dentry);
412 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
414 if (test_bit(CACHEFILES_OBJECT_BURIED, &object->flags)) {
415 /* object allocation for the same key preemptively deleted this
416 * object's file so that it could create its own file */
417 _debug("object preemptively buried");
418 mutex_unlock(&dir->d_inode->i_mutex);
419 ret = 0;
420 } else {
421 /* we need to check that our parent is _still_ our parent - it
422 * may have been renamed */
423 if (dir == object->dentry->d_parent) {
424 ret = cachefiles_bury_object(cache, dir,
425 object->dentry, false);
426 } else {
427 /* it got moved, presumably by cachefilesd culling it,
428 * so it's no longer in the key path and we can ignore
429 * it */
430 mutex_unlock(&dir->d_inode->i_mutex);
431 ret = 0;
435 dput(dir);
436 _leave(" = %d", ret);
437 return ret;
441 * walk from the parent object to the child object through the backing
442 * filesystem, creating directories as we go
444 int cachefiles_walk_to_object(struct cachefiles_object *parent,
445 struct cachefiles_object *object,
446 const char *key,
447 struct cachefiles_xattr *auxdata)
449 struct cachefiles_cache *cache;
450 struct dentry *dir, *next = NULL;
451 unsigned long start;
452 const char *name;
453 int ret, nlen;
455 _enter("OBJ%x{%p},OBJ%x,%s,",
456 parent->fscache.debug_id, parent->dentry,
457 object->fscache.debug_id, key);
459 cache = container_of(parent->fscache.cache,
460 struct cachefiles_cache, cache);
462 ASSERT(parent->dentry);
463 ASSERT(parent->dentry->d_inode);
465 if (!(S_ISDIR(parent->dentry->d_inode->i_mode))) {
466 // TODO: convert file to dir
467 _leave("looking up in none directory");
468 return -ENOBUFS;
471 dir = dget(parent->dentry);
473 advance:
474 /* attempt to transit the first directory component */
475 name = key;
476 nlen = strlen(key);
478 /* key ends in a double NUL */
479 key = key + nlen + 1;
480 if (!*key)
481 key = NULL;
483 lookup_again:
484 /* search the current directory for the element name */
485 _debug("lookup '%s'", name);
487 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
489 start = jiffies;
490 next = lookup_one_len(name, dir, nlen);
491 cachefiles_hist(cachefiles_lookup_histogram, start);
492 if (IS_ERR(next))
493 goto lookup_error;
495 _debug("next -> %p %s", next, next->d_inode ? "positive" : "negative");
497 if (!key)
498 object->new = !next->d_inode;
500 /* if this element of the path doesn't exist, then the lookup phase
501 * failed, and we can release any readers in the certain knowledge that
502 * there's nothing for them to actually read */
503 if (!next->d_inode)
504 fscache_object_lookup_negative(&object->fscache);
506 /* we need to create the object if it's negative */
507 if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
508 /* index objects and intervening tree levels must be subdirs */
509 if (!next->d_inode) {
510 ret = cachefiles_has_space(cache, 1, 0);
511 if (ret < 0)
512 goto create_error;
514 start = jiffies;
515 ret = vfs_mkdir(dir->d_inode, next, 0);
516 cachefiles_hist(cachefiles_mkdir_histogram, start);
517 if (ret < 0)
518 goto create_error;
520 ASSERT(next->d_inode);
522 _debug("mkdir -> %p{%p{ino=%lu}}",
523 next, next->d_inode, next->d_inode->i_ino);
525 } else if (!S_ISDIR(next->d_inode->i_mode)) {
526 kerror("inode %lu is not a directory",
527 next->d_inode->i_ino);
528 ret = -ENOBUFS;
529 goto error;
532 } else {
533 /* non-index objects start out life as files */
534 if (!next->d_inode) {
535 ret = cachefiles_has_space(cache, 1, 0);
536 if (ret < 0)
537 goto create_error;
539 start = jiffies;
540 ret = vfs_create(dir->d_inode, next, S_IFREG, NULL);
541 cachefiles_hist(cachefiles_create_histogram, start);
542 if (ret < 0)
543 goto create_error;
545 ASSERT(next->d_inode);
547 _debug("create -> %p{%p{ino=%lu}}",
548 next, next->d_inode, next->d_inode->i_ino);
550 } else if (!S_ISDIR(next->d_inode->i_mode) &&
551 !S_ISREG(next->d_inode->i_mode)
553 kerror("inode %lu is not a file or directory",
554 next->d_inode->i_ino);
555 ret = -ENOBUFS;
556 goto error;
560 /* process the next component */
561 if (key) {
562 _debug("advance");
563 mutex_unlock(&dir->d_inode->i_mutex);
564 dput(dir);
565 dir = next;
566 next = NULL;
567 goto advance;
570 /* we've found the object we were looking for */
571 object->dentry = next;
573 /* if we've found that the terminal object exists, then we need to
574 * check its attributes and delete it if it's out of date */
575 if (!object->new) {
576 _debug("validate '%*.*s'",
577 next->d_name.len, next->d_name.len, next->d_name.name);
579 ret = cachefiles_check_object_xattr(object, auxdata);
580 if (ret == -ESTALE) {
581 /* delete the object (the deleter drops the directory
582 * mutex) */
583 object->dentry = NULL;
585 ret = cachefiles_bury_object(cache, dir, next, true);
586 dput(next);
587 next = NULL;
589 if (ret < 0)
590 goto delete_error;
592 _debug("redo lookup");
593 goto lookup_again;
597 /* note that we're now using this object */
598 ret = cachefiles_mark_object_active(cache, object);
600 mutex_unlock(&dir->d_inode->i_mutex);
601 dput(dir);
602 dir = NULL;
604 if (ret == -ETIMEDOUT)
605 goto mark_active_timed_out;
607 _debug("=== OBTAINED_OBJECT ===");
609 if (object->new) {
610 /* attach data to a newly constructed terminal object */
611 ret = cachefiles_set_object_xattr(object, auxdata);
612 if (ret < 0)
613 goto check_error;
614 } else {
615 /* always update the atime on an object we've just looked up
616 * (this is used to keep track of culling, and atimes are only
617 * updated by read, write and readdir but not lookup or
618 * open) */
619 touch_atime(cache->mnt, next);
622 /* open a file interface onto a data file */
623 if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
624 if (S_ISREG(object->dentry->d_inode->i_mode)) {
625 const struct address_space_operations *aops;
627 ret = -EPERM;
628 aops = object->dentry->d_inode->i_mapping->a_ops;
629 if (!aops->bmap)
630 goto check_error;
632 object->backer = object->dentry;
633 } else {
634 BUG(); // TODO: open file in data-class subdir
638 object->new = 0;
639 fscache_obtained_object(&object->fscache);
641 _leave(" = 0 [%lu]", object->dentry->d_inode->i_ino);
642 return 0;
644 create_error:
645 _debug("create error %d", ret);
646 if (ret == -EIO)
647 cachefiles_io_error(cache, "Create/mkdir failed");
648 goto error;
650 mark_active_timed_out:
651 _debug("mark active timed out");
652 goto release_dentry;
654 check_error:
655 _debug("check error %d", ret);
656 write_lock(&cache->active_lock);
657 rb_erase(&object->active_node, &cache->active_nodes);
658 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
659 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
660 write_unlock(&cache->active_lock);
661 release_dentry:
662 dput(object->dentry);
663 object->dentry = NULL;
664 goto error_out;
666 delete_error:
667 _debug("delete error %d", ret);
668 goto error_out2;
670 lookup_error:
671 _debug("lookup error %ld", PTR_ERR(next));
672 ret = PTR_ERR(next);
673 if (ret == -EIO)
674 cachefiles_io_error(cache, "Lookup failed");
675 next = NULL;
676 error:
677 mutex_unlock(&dir->d_inode->i_mutex);
678 dput(next);
679 error_out2:
680 dput(dir);
681 error_out:
682 _leave(" = error %d", -ret);
683 return ret;
687 * get a subdirectory
689 struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
690 struct dentry *dir,
691 const char *dirname)
693 struct dentry *subdir;
694 unsigned long start;
695 int ret;
697 _enter(",,%s", dirname);
699 /* search the current directory for the element name */
700 mutex_lock(&dir->d_inode->i_mutex);
702 start = jiffies;
703 subdir = lookup_one_len(dirname, dir, strlen(dirname));
704 cachefiles_hist(cachefiles_lookup_histogram, start);
705 if (IS_ERR(subdir)) {
706 if (PTR_ERR(subdir) == -ENOMEM)
707 goto nomem_d_alloc;
708 goto lookup_error;
711 _debug("subdir -> %p %s",
712 subdir, subdir->d_inode ? "positive" : "negative");
714 /* we need to create the subdir if it doesn't exist yet */
715 if (!subdir->d_inode) {
716 ret = cachefiles_has_space(cache, 1, 0);
717 if (ret < 0)
718 goto mkdir_error;
720 _debug("attempt mkdir");
722 ret = vfs_mkdir(dir->d_inode, subdir, 0700);
723 if (ret < 0)
724 goto mkdir_error;
726 ASSERT(subdir->d_inode);
728 _debug("mkdir -> %p{%p{ino=%lu}}",
729 subdir,
730 subdir->d_inode,
731 subdir->d_inode->i_ino);
734 mutex_unlock(&dir->d_inode->i_mutex);
736 /* we need to make sure the subdir is a directory */
737 ASSERT(subdir->d_inode);
739 if (!S_ISDIR(subdir->d_inode->i_mode)) {
740 kerror("%s is not a directory", dirname);
741 ret = -EIO;
742 goto check_error;
745 ret = -EPERM;
746 if (!subdir->d_inode->i_op ||
747 !subdir->d_inode->i_op->setxattr ||
748 !subdir->d_inode->i_op->getxattr ||
749 !subdir->d_inode->i_op->lookup ||
750 !subdir->d_inode->i_op->mkdir ||
751 !subdir->d_inode->i_op->create ||
752 !subdir->d_inode->i_op->rename ||
753 !subdir->d_inode->i_op->rmdir ||
754 !subdir->d_inode->i_op->unlink)
755 goto check_error;
757 _leave(" = [%lu]", subdir->d_inode->i_ino);
758 return subdir;
760 check_error:
761 dput(subdir);
762 _leave(" = %d [check]", ret);
763 return ERR_PTR(ret);
765 mkdir_error:
766 mutex_unlock(&dir->d_inode->i_mutex);
767 dput(subdir);
768 kerror("mkdir %s failed with error %d", dirname, ret);
769 return ERR_PTR(ret);
771 lookup_error:
772 mutex_unlock(&dir->d_inode->i_mutex);
773 ret = PTR_ERR(subdir);
774 kerror("Lookup %s failed with error %d", dirname, ret);
775 return ERR_PTR(ret);
777 nomem_d_alloc:
778 mutex_unlock(&dir->d_inode->i_mutex);
779 _leave(" = -ENOMEM");
780 return ERR_PTR(-ENOMEM);
784 * find out if an object is in use or not
785 * - if finds object and it's not in use:
786 * - returns a pointer to the object and a reference on it
787 * - returns with the directory locked
789 static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
790 struct dentry *dir,
791 char *filename)
793 struct cachefiles_object *object;
794 struct rb_node *_n;
795 struct dentry *victim;
796 unsigned long start;
797 int ret;
799 //_enter(",%*.*s/,%s",
800 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
802 /* look up the victim */
803 mutex_lock_nested(&dir->d_inode->i_mutex, 1);
805 start = jiffies;
806 victim = lookup_one_len(filename, dir, strlen(filename));
807 cachefiles_hist(cachefiles_lookup_histogram, start);
808 if (IS_ERR(victim))
809 goto lookup_error;
811 //_debug("victim -> %p %s",
812 // victim, victim->d_inode ? "positive" : "negative");
814 /* if the object is no longer there then we probably retired the object
815 * at the netfs's request whilst the cull was in progress
817 if (!victim->d_inode) {
818 mutex_unlock(&dir->d_inode->i_mutex);
819 dput(victim);
820 _leave(" = -ENOENT [absent]");
821 return ERR_PTR(-ENOENT);
824 /* check to see if we're using this object */
825 read_lock(&cache->active_lock);
827 _n = cache->active_nodes.rb_node;
829 while (_n) {
830 object = rb_entry(_n, struct cachefiles_object, active_node);
832 if (object->dentry > victim)
833 _n = _n->rb_left;
834 else if (object->dentry < victim)
835 _n = _n->rb_right;
836 else
837 goto object_in_use;
840 read_unlock(&cache->active_lock);
842 //_leave(" = %p", victim);
843 return victim;
845 object_in_use:
846 read_unlock(&cache->active_lock);
847 mutex_unlock(&dir->d_inode->i_mutex);
848 dput(victim);
849 //_leave(" = -EBUSY [in use]");
850 return ERR_PTR(-EBUSY);
852 lookup_error:
853 mutex_unlock(&dir->d_inode->i_mutex);
854 ret = PTR_ERR(victim);
855 if (ret == -ENOENT) {
856 /* file or dir now absent - probably retired by netfs */
857 _leave(" = -ESTALE [absent]");
858 return ERR_PTR(-ESTALE);
861 if (ret == -EIO) {
862 cachefiles_io_error(cache, "Lookup failed");
863 } else if (ret != -ENOMEM) {
864 kerror("Internal error: %d", ret);
865 ret = -EIO;
868 _leave(" = %d", ret);
869 return ERR_PTR(ret);
873 * cull an object if it's not in use
874 * - called only by cache manager daemon
876 int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
877 char *filename)
879 struct dentry *victim;
880 int ret;
882 _enter(",%*.*s/,%s",
883 dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
885 victim = cachefiles_check_active(cache, dir, filename);
886 if (IS_ERR(victim))
887 return PTR_ERR(victim);
889 _debug("victim -> %p %s",
890 victim, victim->d_inode ? "positive" : "negative");
892 /* okay... the victim is not being used so we can cull it
893 * - start by marking it as stale
895 _debug("victim is cullable");
897 ret = cachefiles_remove_object_xattr(cache, victim);
898 if (ret < 0)
899 goto error_unlock;
901 /* actually remove the victim (drops the dir mutex) */
902 _debug("bury");
904 ret = cachefiles_bury_object(cache, dir, victim, false);
905 if (ret < 0)
906 goto error;
908 dput(victim);
909 _leave(" = 0");
910 return 0;
912 error_unlock:
913 mutex_unlock(&dir->d_inode->i_mutex);
914 error:
915 dput(victim);
916 if (ret == -ENOENT) {
917 /* file or dir now absent - probably retired by netfs */
918 _leave(" = -ESTALE [absent]");
919 return -ESTALE;
922 if (ret != -ENOMEM) {
923 kerror("Internal error: %d", ret);
924 ret = -EIO;
927 _leave(" = %d", ret);
928 return ret;
932 * find out if an object is in use or not
933 * - called only by cache manager daemon
934 * - returns -EBUSY or 0 to indicate whether an object is in use or not
936 int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
937 char *filename)
939 struct dentry *victim;
941 //_enter(",%*.*s/,%s",
942 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
944 victim = cachefiles_check_active(cache, dir, filename);
945 if (IS_ERR(victim))
946 return PTR_ERR(victim);
948 mutex_unlock(&dir->d_inode->i_mutex);
949 dput(victim);
950 //_leave(" = 0");
951 return 0;