Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi...
[sfrench/cifs-2.6.git] / fs / cachefiles / namei.c
1 /* CacheFiles path walking and related routines
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
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.
10  */
11
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"
23
24 #define CACHEFILES_KEYBUF_SIZE 512
25
26 /*
27  * dump debugging info about an object
28  */
29 static noinline
30 void __cachefiles_printk_object(struct cachefiles_object *object,
31                                 const char *prefix,
32                                 u8 *keybuf)
33 {
34         struct fscache_cookie *cookie;
35         unsigned keylen, loop;
36
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);
49
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;
67         }
68         spin_unlock(&object->fscache.lock);
69
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");
75         }
76 }
77
78 /*
79  * dump debugging info about a pair of objects
80  */
81 static noinline void cachefiles_printk_object(struct cachefiles_object *object,
82                                               struct cachefiles_object *xobject)
83 {
84         u8 *keybuf;
85
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);
92 }
93
94 /*
95  * record the fact that an object is now active
96  */
97 static int cachefiles_mark_object_active(struct cachefiles_cache *cache,
98                                          struct cachefiles_object *object)
99 {
100         struct cachefiles_object *xobject;
101         struct rb_node **_p, *_parent = NULL;
102         struct dentry *dentry;
103
104         _enter(",%p", object);
105
106 try_again:
107         write_lock(&cache->active_lock);
108
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();
113         }
114
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);
121
122                 ASSERT(xobject != object);
123
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;
130         }
131
132         rb_link_node(&object->active_node, _parent, _p);
133         rb_insert_color(&object->active_node, &cache->active_nodes);
134
135         write_unlock(&cache->active_lock);
136         _leave(" = 0");
137         return 0;
138
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();
148         }
149         atomic_inc(&xobject->usage);
150         write_unlock(&cache->active_lock);
151
152         if (test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
153                 wait_queue_head_t *wq;
154
155                 signed long timeout = 60 * HZ;
156                 wait_queue_t wait;
157                 bool requeue;
158
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;
166                 }
167
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);
182
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;
189                 }
190
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;
197                 }
198         }
199
200         ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags));
201
202         cache->cache.ops->put_object(&xobject->fscache);
203         goto try_again;
204
205 requeue:
206         clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
207         cache->cache.ops->put_object(&xobject->fscache);
208         _leave(" = -ETIMEDOUT");
209         return -ETIMEDOUT;
210 }
211
212 /*
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
218  */
219 static int cachefiles_bury_object(struct cachefiles_cache *cache,
220                                   struct dentry *dir,
221                                   struct dentry *rep)
222 {
223         struct dentry *grave, *trap;
224         char nbuffer[8 + 8 + 1];
225         int ret;
226
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);
230
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);
235
236                 mutex_unlock(&dir->d_inode->i_mutex);
237
238                 if (ret == -EIO)
239                         cachefiles_io_error(cache, "Unlink failed");
240
241                 _leave(" = %d", ret);
242                 return ret;
243         }
244
245         /* directories have to be moved to the graveyard */
246         _debug("move stale object to graveyard");
247         mutex_unlock(&dir->d_inode->i_mutex);
248
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));
254
255         /* do the multiway lock magic */
256         trap = lock_rename(cache->graveyard, dir);
257
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;
265         }
266
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;
271         }
272
273         if (trap == rep) {
274                 unlock_rename(cache->graveyard, dir);
275                 cachefiles_io_error(cache, "May not make directory loop");
276                 return -EIO;
277         }
278
279         if (d_mountpoint(rep)) {
280                 unlock_rename(cache->graveyard, dir);
281                 cachefiles_io_error(cache, "Mountpoint in cache");
282                 return -EIO;
283         }
284
285         grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
286         if (IS_ERR(grave)) {
287                 unlock_rename(cache->graveyard, dir);
288
289                 if (PTR_ERR(grave) == -ENOMEM) {
290                         _leave(" = -ENOMEM");
291                         return -ENOMEM;
292                 }
293
294                 cachefiles_io_error(cache, "Lookup error %ld",
295                                     PTR_ERR(grave));
296                 return -EIO;
297         }
298
299         if (grave->d_inode) {
300                 unlock_rename(cache->graveyard, dir);
301                 dput(grave);
302                 grave = NULL;
303                 cond_resched();
304                 goto try_again;
305         }
306
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;
312         }
313
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;
320         }
321
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);
326
327         unlock_rename(cache->graveyard, dir);
328         dput(grave);
329         _leave(" = 0");
330         return 0;
331 }
332
333 /*
334  * delete an object representation from the cache
335  */
336 int cachefiles_delete_object(struct cachefiles_cache *cache,
337                              struct cachefiles_object *object)
338 {
339         struct dentry *dir;
340         int ret;
341
342         _enter(",{%p}", object->dentry);
343
344         ASSERT(object->dentry);
345         ASSERT(object->dentry->d_inode);
346         ASSERT(object->dentry->d_parent);
347
348         dir = dget_parent(object->dentry);
349
350         mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
351         ret = cachefiles_bury_object(cache, dir, object->dentry);
352
353         dput(dir);
354         _leave(" = %d", ret);
355         return ret;
356 }
357
358 /*
359  * walk from the parent object to the child object through the backing
360  * filesystem, creating directories as we go
361  */
362 int cachefiles_walk_to_object(struct cachefiles_object *parent,
363                               struct cachefiles_object *object,
364                               const char *key,
365                               struct cachefiles_xattr *auxdata)
366 {
367         struct cachefiles_cache *cache;
368         struct dentry *dir, *next = NULL;
369         unsigned long start;
370         const char *name;
371         int ret, nlen;
372
373         _enter("{%p},,%s,", parent->dentry, key);
374
375         cache = container_of(parent->fscache.cache,
376                              struct cachefiles_cache, cache);
377
378         ASSERT(parent->dentry);
379         ASSERT(parent->dentry->d_inode);
380
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;
385         }
386
387         dir = dget(parent->dentry);
388
389 advance:
390         /* attempt to transit the first directory component */
391         name = key;
392         nlen = strlen(key);
393
394         /* key ends in a double NUL */
395         key = key + nlen + 1;
396         if (!*key)
397                 key = NULL;
398
399 lookup_again:
400         /* search the current directory for the element name */
401         _debug("lookup '%s'", name);
402
403         mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
404
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;
410
411         _debug("next -> %p %s", next, next->d_inode ? "positive" : "negative");
412
413         if (!key)
414                 object->new = !next->d_inode;
415
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);
421
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;
429
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;
435
436                         ASSERT(next->d_inode);
437
438                         _debug("mkdir -> %p{%p{ino=%lu}}",
439                                next, next->d_inode, next->d_inode->i_ino);
440
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;
446                 }
447
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;
454
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;
460
461                         ASSERT(next->d_inode);
462
463                         _debug("create -> %p{%p{ino=%lu}}",
464                                next, next->d_inode, next->d_inode->i_ino);
465
466                 } else if (!S_ISDIR(next->d_inode->i_mode) &&
467                            !S_ISREG(next->d_inode->i_mode)
468                            ) {
469                         kerror("inode %lu is not a file or directory",
470                                next->d_inode->i_ino);
471                         ret = -ENOBUFS;
472                         goto error;
473                 }
474         }
475
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;
484         }
485
486         /* we've found the object we were looking for */
487         object->dentry = next;
488
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);
494
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;
500
501                         ret = cachefiles_bury_object(cache, dir, next);
502                         dput(next);
503                         next = NULL;
504
505                         if (ret < 0)
506                                 goto delete_error;
507
508                         _debug("redo lookup");
509                         goto lookup_again;
510                 }
511         }
512
513         /* note that we're now using this object */
514         ret = cachefiles_mark_object_active(cache, object);
515
516         mutex_unlock(&dir->d_inode->i_mutex);
517         dput(dir);
518         dir = NULL;
519
520         if (ret == -ETIMEDOUT)
521                 goto mark_active_timed_out;
522
523         _debug("=== OBTAINED_OBJECT ===");
524
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);
536         }
537
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;
542
543                         ret = -EPERM;
544                         aops = object->dentry->d_inode->i_mapping->a_ops;
545                         if (!aops->bmap)
546                                 goto check_error;
547
548                         object->backer = object->dentry;
549                 } else {
550                         BUG(); // TODO: open file in data-class subdir
551                 }
552         }
553
554         object->new = 0;
555         fscache_obtained_object(&object->fscache);
556
557         _leave(" = 0 [%lu]", object->dentry->d_inode->i_ino);
558         return 0;
559
560 create_error:
561         _debug("create error %d", ret);
562         if (ret == -EIO)
563                 cachefiles_io_error(cache, "Create/mkdir failed");
564         goto error;
565
566 mark_active_timed_out:
567         _debug("mark active timed out");
568         goto release_dentry;
569
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;
581
582 delete_error:
583         _debug("delete error %d", ret);
584         goto error_out2;
585
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;
600 }
601
602 /*
603  * get a subdirectory
604  */
605 struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
606                                         struct dentry *dir,
607                                         const char *dirname)
608 {
609         struct dentry *subdir;
610         unsigned long start;
611         int ret;
612
613         _enter(",,%s", dirname);
614
615         /* search the current directory for the element name */
616         mutex_lock(&dir->d_inode->i_mutex);
617
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;
625         }
626
627         _debug("subdir -> %p %s",
628                subdir, subdir->d_inode ? "positive" : "negative");
629
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;
635
636                 _debug("attempt mkdir");
637
638                 ret = vfs_mkdir(dir->d_inode, subdir, 0700);
639                 if (ret < 0)
640                         goto mkdir_error;
641
642                 ASSERT(subdir->d_inode);
643
644                 _debug("mkdir -> %p{%p{ino=%lu}}",
645                        subdir,
646                        subdir->d_inode,
647                        subdir->d_inode->i_ino);
648         }
649
650         mutex_unlock(&dir->d_inode->i_mutex);
651
652         /* we need to make sure the subdir is a directory */
653         ASSERT(subdir->d_inode);
654
655         if (!S_ISDIR(subdir->d_inode->i_mode)) {
656                 kerror("%s is not a directory", dirname);
657                 ret = -EIO;
658                 goto check_error;
659         }
660
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;
672
673         _leave(" = [%lu]", subdir->d_inode->i_ino);
674         return subdir;
675
676 check_error:
677         dput(subdir);
678         _leave(" = %d [check]", ret);
679         return ERR_PTR(ret);
680
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);
686
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);
692
693 nomem_d_alloc:
694         mutex_unlock(&dir->d_inode->i_mutex);
695         _leave(" = -ENOMEM");
696         return ERR_PTR(-ENOMEM);
697 }
698
699 /*
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
704  */
705 static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
706                                               struct dentry *dir,
707                                               char *filename)
708 {
709         struct cachefiles_object *object;
710         struct rb_node *_n;
711         struct dentry *victim;
712         unsigned long start;
713         int ret;
714
715         //_enter(",%*.*s/,%s",
716         //       dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
717
718         /* look up the victim */
719         mutex_lock_nested(&dir->d_inode->i_mutex, 1);
720
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;
726
727         //_debug("victim -> %p %s",
728         //       victim, victim->d_inode ? "positive" : "negative");
729
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
732          */
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);
738         }
739
740         /* check to see if we're using this object */
741         read_lock(&cache->active_lock);
742
743         _n = cache->active_nodes.rb_node;
744
745         while (_n) {
746                 object = rb_entry(_n, struct cachefiles_object, active_node);
747
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;
754         }
755
756         read_unlock(&cache->active_lock);
757
758         //_leave(" = %p", victim);
759         return victim;
760
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);
767
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);
775         }
776
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;
782         }
783
784         _leave(" = %d", ret);
785         return ERR_PTR(ret);
786 }
787
788 /*
789  * cull an object if it's not in use
790  * - called only by cache manager daemon
791  */
792 int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
793                     char *filename)
794 {
795         struct dentry *victim;
796         int ret;
797
798         _enter(",%*.*s/,%s",
799                dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
800
801         victim = cachefiles_check_active(cache, dir, filename);
802         if (IS_ERR(victim))
803                 return PTR_ERR(victim);
804
805         _debug("victim -> %p %s",
806                victim, victim->d_inode ? "positive" : "negative");
807
808         /* okay... the victim is not being used so we can cull it
809          * - start by marking it as stale
810          */
811         _debug("victim is cullable");
812
813         ret = cachefiles_remove_object_xattr(cache, victim);
814         if (ret < 0)
815                 goto error_unlock;
816
817         /*  actually remove the victim (drops the dir mutex) */
818         _debug("bury");
819
820         ret = cachefiles_bury_object(cache, dir, victim);
821         if (ret < 0)
822                 goto error;
823
824         dput(victim);
825         _leave(" = 0");
826         return 0;
827
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;
836         }
837
838         if (ret != -ENOMEM) {
839                 kerror("Internal error: %d", ret);
840                 ret = -EIO;
841         }
842
843         _leave(" = %d", ret);
844         return ret;
845 }
846
847 /*
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
851  */
852 int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
853                             char *filename)
854 {
855         struct dentry *victim;
856
857         //_enter(",%*.*s/,%s",
858         //       dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
859
860         victim = cachefiles_check_active(cache, dir, filename);
861         if (IS_ERR(victim))
862                 return PTR_ERR(victim);
863
864         mutex_unlock(&dir->d_inode->i_mutex);
865         dput(victim);
866         //_leave(" = 0");
867         return 0;
868 }