This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / cachefiles / cf-namei.c
1 /* cf-namei.c: CacheFiles path walking and related routines
2  *
3  * Copyright (C) 2006 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 License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, 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 "internal.h"
22
23 /*****************************************************************************/
24 /*
25  * record the fact that an object is now active
26  */
27 static void cachefiles_mark_object_active(struct cachefiles_cache *cache,
28                                           struct cachefiles_object *object)
29 {
30         struct cachefiles_object *xobject;
31         struct rb_node **_p, *_parent = NULL;
32         struct dentry *dentry;
33
34         write_lock(&cache->active_lock);
35
36         dentry = object->dentry;
37         _p = &cache->active_nodes.rb_node;
38         while (*_p) {
39                 _parent = *_p;
40                 xobject = rb_entry(_parent,
41                                    struct cachefiles_object, active_node);
42
43                 if (xobject->dentry > dentry)
44                         _p = &(*_p)->rb_left;
45                 else if (xobject->dentry < dentry)
46                         _p = &(*_p)->rb_right;
47                 else
48                         BUG(); /* uh oh... this dentry shouldn't be here */
49         }
50
51         rb_link_node(&object->active_node, _parent, _p);
52         rb_insert_color(&object->active_node, &cache->active_nodes);
53
54         write_unlock(&cache->active_lock);
55 }
56
57 /*****************************************************************************/
58 /*
59  * delete an object representation from the cache
60  * - file backed objects are unlinked
61  * - directory backed objects are stuffed into the graveyard for userspace to
62  *   delete
63  * - unlocks the directory mutex
64  */
65 static int cachefiles_bury_object(struct cachefiles_cache *cache,
66                                   struct dentry *dir,
67                                   struct dentry *rep)
68 {
69         struct dentry *grave, *alt, *trap;
70         struct qstr name;
71         const char *old_name;
72         char nbuffer[8 + 8 + 1];
73         int ret;
74
75         _enter(",'%*.*s','%*.*s'",
76                dir->d_name.len, dir->d_name.len, dir->d_name.name,
77                rep->d_name.len, rep->d_name.len, rep->d_name.name);
78
79         /* non-directories can just be unlinked */
80         if (!S_ISDIR(rep->d_inode->i_mode)) {
81                 _debug("unlink stale object");
82                 ret = dir->d_inode->i_op->unlink(dir->d_inode, rep);
83
84                 mutex_unlock(&dir->d_inode->i_mutex);
85
86                 if (ret == 0) {
87                         _debug("d_delete");
88                         d_delete(rep);
89                 } else if (ret == -EIO) {
90                         cachefiles_io_error(cache, "Unlink failed");
91                 }
92
93                 _leave(" = %d", ret);
94                 return ret;
95         }
96
97         /* directories have to be moved to the graveyard */
98         _debug("move stale object to graveyard");
99         mutex_unlock(&dir->d_inode->i_mutex);
100
101 try_again:
102         /* first step is to make up a grave dentry in the graveyard */
103         sprintf(nbuffer, "%08x%08x",
104                 (uint32_t) xtime.tv_sec,
105                 (uint32_t) atomic_inc_return(&cache->gravecounter));
106
107         name.name = nbuffer;
108         name.len = strlen(name.name);
109
110         /* hash the name */
111         name.hash = full_name_hash(name.name, name.len);
112
113         if (dir->d_op && dir->d_op->d_hash) {
114                 ret = dir->d_op->d_hash(dir, &name);
115                 if (ret < 0) {
116                         if (ret == -EIO)
117                                 cachefiles_io_error(cache, "Hash failed");
118
119                         _leave(" = %d", ret);
120                         return ret;
121                 }
122         }
123
124         /* do the multiway lock magic */
125         trap = lock_rename(cache->graveyard, dir);
126
127         /* do some checks before getting the grave dentry */
128         if (rep->d_parent != dir) {
129                 /* the entry was probably culled when we dropped the parent dir
130                  * lock */
131                 unlock_rename(cache->graveyard, dir);
132                 _leave(" = 0 [culled?]");
133                 return 0;
134         }
135
136         if (!S_ISDIR(cache->graveyard->d_inode->i_mode)) {
137                 unlock_rename(cache->graveyard, dir);
138                 cachefiles_io_error(cache, "Graveyard no longer a directory");
139                 return -EIO;
140         }
141
142         if (trap == rep) {
143                 unlock_rename(cache->graveyard, dir);
144                 cachefiles_io_error(cache, "May not make directory loop");
145                 return -EIO;
146         }
147
148         if (d_mountpoint(rep)) {
149                 unlock_rename(cache->graveyard, dir);
150                 cachefiles_io_error(cache, "Mountpoint in cache");
151                 return -EIO;
152         }
153
154         /* see if there's a dentry already there for this name */
155         grave = d_lookup(cache->graveyard, &name);
156         if (!grave) {
157                 _debug("not found");
158
159                 grave = d_alloc(cache->graveyard, &name);
160                 if (!grave) {
161                         unlock_rename(cache->graveyard, dir);
162                         _leave(" = -ENOMEM");
163                         return -ENOMEM;
164                 }
165
166                 alt = cache->graveyard->d_inode->i_op->lookup(
167                         cache->graveyard->d_inode, grave, NULL);
168                 if (IS_ERR(alt)) {
169                         unlock_rename(cache->graveyard, dir);
170                         dput(grave);
171
172                         if (PTR_ERR(alt) == -ENOMEM) {
173                                 _leave(" = -ENOMEM");
174                                 return -ENOMEM;
175                         }
176
177                         cachefiles_io_error(cache, "Lookup error %ld",
178                                             PTR_ERR(alt));
179                         return -EIO;
180                 }
181
182                 if (alt) {
183                         dput(grave);
184                         grave = alt;
185                 }
186         }
187
188         if (grave->d_inode) {
189                 unlock_rename(cache->graveyard, dir);
190                 dput(grave);
191                 grave = NULL;
192                 cond_resched();
193                 goto try_again;
194         }
195
196         if (d_mountpoint(grave)) {
197                 unlock_rename(cache->graveyard, dir);
198                 dput(grave);
199                 cachefiles_io_error(cache, "Mountpoint in graveyard");
200                 return -EIO;
201         }
202
203         /* target should not be an ancestor of source */
204         if (trap == grave) {
205                 unlock_rename(cache->graveyard, dir);
206                 dput(grave);
207                 cachefiles_io_error(cache, "May not make directory loop");
208                 return -EIO;
209         }
210
211         /* attempt the rename */
212         DQUOT_INIT(dir->d_inode);
213         DQUOT_INIT(cache->graveyard->d_inode);
214
215         old_name = fsnotify_oldname_init(rep->d_name.name);
216
217         ret = dir->d_inode->i_op->rename(dir->d_inode, rep,
218                                          cache->graveyard->d_inode, grave);
219
220         if (ret == 0) {
221                 d_move(rep, grave);
222                 fsnotify_move(dir->d_inode, cache->graveyard->d_inode,
223                               old_name, rep->d_name.name, 1,
224                               grave->d_inode, rep->d_inode);
225         } else if (ret != -ENOMEM) {
226                 cachefiles_io_error(cache, "Rename failed with error %d", ret);
227         }
228
229         fsnotify_oldname_free(old_name);
230
231         unlock_rename(cache->graveyard, dir);
232         dput(grave);
233         _leave(" = 0");
234         return 0;
235 }
236
237 /*****************************************************************************/
238 /*
239  * delete an object representation from the cache
240  */
241 int cachefiles_delete_object(struct cachefiles_cache *cache,
242                              struct cachefiles_object *object)
243 {
244         struct dentry *dir;
245         int ret;
246
247         _enter(",{%p}", object->dentry);
248
249         ASSERT(object->dentry);
250         ASSERT(object->dentry->d_inode);
251         ASSERT(object->dentry->d_parent);
252
253         dir = dget_parent(object->dentry);
254
255         mutex_lock(&dir->d_inode->i_mutex);
256         ret = cachefiles_bury_object(cache, dir, object->dentry);
257
258         dput(dir);
259         _leave(" = %d", ret);
260         return ret;
261 }
262
263 /*****************************************************************************/
264 /*
265  * walk from the parent object to the child object through the backing
266  * filesystem, creating directories as we go
267  */
268 int cachefiles_walk_to_object(struct cachefiles_object *parent,
269                               struct cachefiles_object *object,
270                               char *key,
271                               struct cachefiles_xattr *auxdata)
272 {
273         struct cachefiles_cache *cache;
274         struct dentry *dir, *next = NULL, *new;
275         struct qstr name;
276         uid_t fsuid;
277         gid_t fsgid;
278         int ret;
279
280         _enter("{%p}", parent->dentry);
281
282         cache = container_of(parent->fscache.cache,
283                              struct cachefiles_cache, cache);
284
285         ASSERT(parent->dentry);
286         ASSERT(parent->dentry->d_inode);
287
288         if (!(S_ISDIR(parent->dentry->d_inode->i_mode))) {
289                 // TODO: convert file to dir
290                 _leave("looking up in none directory");
291                 return -ENOBUFS;
292         }
293
294         fsuid = current->fsuid;
295         fsgid = current->fsgid;
296         current->fsuid = 0;
297         current->fsgid = 0;
298
299         dir = dget(parent->dentry);
300
301 advance:
302         /* attempt to transit the first directory component */
303         name.name = key;
304         key = strchr(key, '/');
305         if (key) {
306                 name.len = key - (char *) name.name;
307                 *key++ = 0;
308         } else {
309                 name.len = strlen(name.name);
310         }
311
312         /* hash the name */
313         name.hash = full_name_hash(name.name, name.len);
314
315         if (dir->d_op && dir->d_op->d_hash) {
316                 ret = dir->d_op->d_hash(dir, &name);
317                 if (ret < 0) {
318                         cachefiles_io_error(cache, "Hash failed");
319                         goto error_out2;
320                 }
321         }
322
323 lookup_again:
324         /* search the current directory for the element name */
325         _debug("lookup '%s' %x", name.name, name.hash);
326
327         mutex_lock(&dir->d_inode->i_mutex);
328
329         next = d_lookup(dir, &name);
330         if (!next) {
331                 _debug("not found");
332
333                 new = d_alloc(dir, &name);
334                 if (!new)
335                         goto nomem_d_alloc;
336
337                 ASSERT(dir->d_inode->i_op);
338                 ASSERT(dir->d_inode->i_op->lookup);
339
340                 next = dir->d_inode->i_op->lookup(dir->d_inode, new, NULL);
341                 if (IS_ERR(next))
342                         goto lookup_error;
343
344                 if (!next)
345                         next = new;
346                 else
347                         dput(new);
348
349                 if (next->d_inode) {
350                         ret = -EPERM;
351                         if (!next->d_inode->i_op ||
352                             !next->d_inode->i_op->setxattr ||
353                             !next->d_inode->i_op->getxattr ||
354                             !next->d_inode->i_op->removexattr)
355                                 goto error;
356
357                         if (key && (!next->d_inode->i_op->lookup ||
358                                     !next->d_inode->i_op->mkdir ||
359                                     !next->d_inode->i_op->create ||
360                                     !next->d_inode->i_op->rename ||
361                                     !next->d_inode->i_op->rmdir ||
362                                     !next->d_inode->i_op->unlink))
363                                 goto error;
364                 }
365         }
366
367         _debug("next -> %p %s", next, next->d_inode ? "positive" : "negative");
368
369         if (!key)
370                 object->new = !next->d_inode;
371
372         /* we need to create the object if it's negative */
373         if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
374                 /* index objects and intervening tree levels must be subdirs */
375                 if (!next->d_inode) {
376                         DQUOT_INIT(dir->d_inode);
377                         ret = dir->d_inode->i_op->mkdir(dir->d_inode, next, 0);
378                         if (ret < 0)
379                                 goto create_error;
380
381                         ASSERT(next->d_inode);
382
383                         fsnotify_mkdir(dir->d_inode, next);
384
385                         _debug("mkdir -> %p{%p{ino=%lu}}",
386                                next, next->d_inode, next->d_inode->i_ino);
387
388                 } else if (!S_ISDIR(next->d_inode->i_mode)) {
389                         kerror("inode %lu is not a directory",
390                                next->d_inode->i_ino);
391                         ret = -ENOBUFS;
392                         goto error;
393                 }
394
395         } else {
396                 /* non-index objects start out life as files */
397                 if (!next->d_inode) {
398                         DQUOT_INIT(dir->d_inode);
399                         ret = dir->d_inode->i_op->create(dir->d_inode, next,
400                                                          S_IFREG, NULL);
401                         if (ret < 0)
402                                 goto create_error;
403
404                         ASSERT(next->d_inode);
405
406                         fsnotify_create(dir->d_inode, next);
407
408                         _debug("create -> %p{%p{ino=%lu}}",
409                                next, next->d_inode, next->d_inode->i_ino);
410
411                 } else if (!S_ISDIR(next->d_inode->i_mode) &&
412                            !S_ISREG(next->d_inode->i_mode)
413                            ) {
414                         kerror("inode %lu is not a file or directory",
415                                next->d_inode->i_ino);
416                         ret = -ENOBUFS;
417                         goto error;
418                 }
419         }
420
421         /* process the next component */
422         if (key) {
423                 _debug("advance");
424                 mutex_unlock(&dir->d_inode->i_mutex);
425                 dput(dir);
426                 dir = next;
427                 next = NULL;
428                 goto advance;
429         }
430
431         /* we've found the object we were looking for */
432         object->dentry = next;
433
434         /* if we've found that the terminal object exists, then we need to
435          * check its attributes and delete it if it's out of date */
436         if (!object->new) {
437                 _debug("validate '%*.*s'",
438                        next->d_name.len, next->d_name.len, next->d_name.name);
439
440                 ret = cachefiles_check_object_xattr(object, auxdata);
441                 if (ret == -ESTALE) {
442                         /* delete the object (the deleter drops the directory
443                          * mutex) */
444                         object->dentry = NULL;
445
446                         ret = cachefiles_bury_object(cache, dir, next);
447                         dput(next);
448                         next = NULL;
449
450                         if (ret < 0)
451                                 goto delete_error;
452
453                         _debug("redo lookup");
454                         goto lookup_again;
455                 }
456         }
457
458         /* note that we're now using this object */
459         cachefiles_mark_object_active(cache, object);
460
461         mutex_unlock(&dir->d_inode->i_mutex);
462         dput(dir);
463         dir = NULL;
464
465         if (object->new) {
466                 /* attach data to a newly constructed terminal object */
467                 ret = cachefiles_set_object_xattr(object, auxdata);
468                 if (ret < 0)
469                         goto check_error;
470         } else {
471                 /* always update the atime on an object we've just looked up
472                  * (this is used to keep track of culling, and atimes are only
473                  * updated by read, write and readdir but not lookup or
474                  * open) */
475                 touch_atime(cache->mnt, next);
476         }
477
478         /* open a file interface onto a data file */
479         if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
480                 if (S_ISREG(object->dentry->d_inode->i_mode)) {
481                         const struct address_space_operations *aops;
482
483                         ret = -EPERM;
484                         aops = object->dentry->d_inode->i_mapping->a_ops;
485                         if (!aops->bmap ||
486                             !aops->prepare_write ||
487                             !aops->commit_write)
488                                 goto check_error;
489
490                         object->backer = object->dentry;
491                 } else {
492                         BUG(); // TODO: open file in data-class subdir
493                 }
494         }
495
496         current->fsuid = fsuid;
497         current->fsgid = fsgid;
498         object->new = 0;
499
500         _leave(" = 0 [%lu]", object->dentry->d_inode->i_ino);
501         return 0;
502
503 create_error:
504         if (ret == -EIO)
505                 cachefiles_io_error(cache, "create/mkdir failed");
506         goto error;
507
508 check_error:
509         write_lock(&cache->active_lock);
510         rb_erase(&object->active_node, &cache->active_nodes);
511         write_unlock(&cache->active_lock);
512
513         dput(object->dentry);
514         object->dentry = NULL;
515         goto error_out;
516
517 delete_error:
518         _debug("delete error %d", ret);
519         goto error_out2;
520
521 lookup_error:
522         _debug("lookup error %ld", PTR_ERR(next));
523         dput(new);
524         ret = PTR_ERR(next);
525         if (ret == -EIO)
526                 cachefiles_io_error(cache, "Lookup failed");
527         next = NULL;
528         goto error;
529
530 nomem_d_alloc:
531         ret = -ENOMEM;
532 error:
533         mutex_unlock(&dir->d_inode->i_mutex);
534         dput(next);
535 error_out2:
536         dput(dir);
537 error_out:
538         current->fsuid = fsuid;
539         current->fsgid = fsgid;
540
541         _leave(" = ret");
542         return ret;
543 }
544
545 /*****************************************************************************/
546 /*
547  * get a subdirectory
548  */
549 struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
550                                         struct dentry *dir,
551                                         const char *dirname)
552 {
553         struct dentry *subdir, *new;
554         struct qstr name;
555         uid_t fsuid;
556         gid_t fsgid;
557         int ret;
558
559         _enter("");
560
561         /* set up the name */
562         name.name = dirname;
563         name.len = strlen(dirname);
564         name.hash = full_name_hash(name.name, name.len);
565
566         if (dir->d_op && dir->d_op->d_hash) {
567                 ret = dir->d_op->d_hash(dir, &name);
568                 if (ret < 0) {
569                         if (ret == -EIO)
570                                 kerror("Hash failed");
571                         _leave(" = %d", ret);
572                         return ERR_PTR(ret);
573                 }
574         }
575
576         /* search the current directory for the element name */
577         _debug("lookup '%s' %x", name.name, name.hash);
578
579         fsuid = current->fsuid;
580         fsgid = current->fsgid;
581         current->fsuid = 0;
582         current->fsgid = 0;
583
584         mutex_lock(&dir->d_inode->i_mutex);
585
586         subdir = d_lookup(dir, &name);
587         if (!subdir) {
588                 _debug("not found");
589
590                 new = d_alloc(dir, &name);
591                 if (!new)
592                         goto nomem_d_alloc;
593
594                 subdir = dir->d_inode->i_op->lookup(dir->d_inode, new, NULL);
595                 if (IS_ERR(subdir))
596                         goto lookup_error;
597
598                 if (!subdir)
599                         subdir = new;
600                 else
601                         dput(new);
602         }
603
604         _debug("subdir -> %p %s",
605                subdir, subdir->d_inode ? "positive" : "negative");
606
607         /* we need to create the subdir if it doesn't exist yet */
608         if (!subdir->d_inode) {
609                 DQUOT_INIT(dir->d_inode);
610                 ret = dir->d_inode->i_op->mkdir(dir->d_inode, subdir, 0700);
611                 if (ret < 0)
612                         goto mkdir_error;
613
614                 ASSERT(subdir->d_inode);
615
616                 fsnotify_mkdir(dir->d_inode, subdir);
617
618                 _debug("mkdir -> %p{%p{ino=%lu}}",
619                        subdir,
620                        subdir->d_inode,
621                        subdir->d_inode->i_ino);
622         }
623
624         mutex_unlock(&dir->d_inode->i_mutex);
625
626         current->fsuid = fsuid;
627         current->fsgid = fsgid;
628
629         /* we need to make sure the subdir is a directory */
630         ASSERT(subdir->d_inode);
631
632         if (!S_ISDIR(subdir->d_inode->i_mode)) {
633                 kerror("%s is not a directory", dirname);
634                 ret = -EIO;
635                 goto check_error;
636         }
637
638         ret = -EPERM;
639         if (!subdir->d_inode->i_op ||
640             !subdir->d_inode->i_op->setxattr ||
641             !subdir->d_inode->i_op->getxattr ||
642             !subdir->d_inode->i_op->lookup ||
643             !subdir->d_inode->i_op->mkdir ||
644             !subdir->d_inode->i_op->create ||
645             !subdir->d_inode->i_op->rename ||
646             !subdir->d_inode->i_op->rmdir ||
647             !subdir->d_inode->i_op->unlink)
648                 goto check_error;
649
650         _leave(" = [%lu]", subdir->d_inode->i_ino);
651         return subdir;
652
653 check_error:
654         dput(subdir);
655         _leave(" = %d [check]", ret);
656         return ERR_PTR(ret);
657
658 mkdir_error:
659         mutex_unlock(&dir->d_inode->i_mutex);
660         kerror("mkdir %s failed with error %d", dirname, ret);
661         goto error_out;
662
663 lookup_error:
664         mutex_unlock(&dir->d_inode->i_mutex);
665         dput(new);
666         ret = PTR_ERR(subdir);
667         kerror("Lookup %s failed with error %d", dirname, ret);
668         goto error_out;
669
670 nomem_d_alloc:
671         mutex_unlock(&dir->d_inode->i_mutex);
672         ret = -ENOMEM;
673         goto error_out;
674
675 error_out:
676         current->fsuid = fsuid;
677         current->fsgid = fsgid;
678         _leave(" = %d", ret);
679         return ERR_PTR(ret);
680 }
681
682 /*****************************************************************************/
683 /*
684  * cull an object if it's not in use
685  * - called only by cache manager daemon
686  */
687 int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
688                     char *filename)
689 {
690         struct cachefiles_object *object;
691         struct rb_node *_n;
692         struct dentry *victim, *new;
693         struct qstr name;
694         int ret;
695
696         _enter(",%*.*s/,%s",
697                dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
698
699         /* set up the name */
700         name.name = filename;
701         name.len = strlen(filename);
702         name.hash = full_name_hash(name.name, name.len);
703
704         if (dir->d_op && dir->d_op->d_hash) {
705                 ret = dir->d_op->d_hash(dir, &name);
706                 if (ret < 0) {
707                         if (ret == -EIO)
708                                 cachefiles_io_error(cache, "Hash failed");
709                         _leave(" = %d", ret);
710                         return ret;
711                 }
712         }
713
714         /* look up the victim */
715         mutex_lock(&dir->d_inode->i_mutex);
716
717         victim = d_lookup(dir, &name);
718         if (!victim) {
719                 _debug("not found");
720
721                 new = d_alloc(dir, &name);
722                 if (!new)
723                         goto nomem_d_alloc;
724
725                 victim = dir->d_inode->i_op->lookup(dir->d_inode, new, NULL);
726                 if (IS_ERR(victim))
727                         goto lookup_error;
728
729                 if (!victim)
730                         victim = new;
731                 else
732                         dput(new);
733         }
734
735         _debug("victim -> %p %s",
736                victim, victim->d_inode ? "positive" : "negative");
737
738         /* if the object is no longer there then we probably retired the object
739          * at the netfs's request whilst the cull was in progress
740          */
741         if (!victim->d_inode) {
742                 mutex_unlock(&dir->d_inode->i_mutex);
743                 dput(victim);
744                 _leave(" = -ENOENT [absent]");
745                 return -ENOENT;
746         }
747
748         /* check to see if we're using this object */
749         read_lock(&cache->active_lock);
750
751         _n = cache->active_nodes.rb_node;
752
753         while (_n) {
754                 object = rb_entry(_n, struct cachefiles_object, active_node);
755
756                 if (object->dentry > victim)
757                         _n = _n->rb_left;
758                 else if (object->dentry < victim)
759                         _n = _n->rb_right;
760                 else
761                         goto object_in_use;
762         }
763
764         read_unlock(&cache->active_lock);
765
766         /* okay... the victim is not being used so we can cull it
767          * - start by marking it as stale
768          */
769         _debug("victim is cullable");
770
771         ret = cachefiles_remove_object_xattr(cache, victim);
772         if (ret < 0)
773                 goto error_unlock;
774
775         /*  actually remove the victim (drops the dir mutex) */
776         _debug("bury");
777
778         ret = cachefiles_bury_object(cache, dir, victim);
779         if (ret < 0)
780                 goto error;
781
782         dput(victim);
783         _leave(" = 0");
784         return 0;
785
786
787 object_in_use:
788         read_unlock(&cache->active_lock);
789         mutex_unlock(&dir->d_inode->i_mutex);
790         dput(victim);
791         _leave(" = -EBUSY [in use]");
792         return -EBUSY;
793
794 nomem_d_alloc:
795         mutex_unlock(&dir->d_inode->i_mutex);
796         _leave(" = -ENOMEM");
797         return -ENOMEM;
798
799 lookup_error:
800         mutex_unlock(&dir->d_inode->i_mutex);
801         dput(new);
802         ret = PTR_ERR(victim);
803         if (ret == -EIO)
804                 cachefiles_io_error(cache, "Lookup failed");
805         goto choose_error;
806
807 error_unlock:
808         mutex_unlock(&dir->d_inode->i_mutex);
809 error:
810         dput(victim);
811 choose_error:
812         if (ret == -ENOENT) {
813                 /* file or dir now absent - probably retired by netfs */
814                 _leave(" = -ESTALE [absent]");
815                 return -ESTALE;
816         }
817
818         if (ret != -ENOMEM) {
819                 kerror("Internal error: %d", ret);
820                 ret = -EIO;
821         }
822
823         _leave(" = %d", ret);
824         return ret;
825 }