include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / cachefiles / namei.c
CommitLineData
9ae326a6
DH
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>
5a0e3ad6 22#include <linux/slab.h>
9ae326a6
DH
23#include "internal.h"
24
d0e27b78
DH
25#define CACHEFILES_KEYBUF_SIZE 512
26
27/*
28 * dump debugging info about an object
29 */
30static noinline
31void __cachefiles_printk_object(struct cachefiles_object *object,
32 const char *prefix,
33 u8 *keybuf)
34{
35 struct fscache_cookie *cookie;
36 unsigned keylen, loop;
37
38 printk(KERN_ERR "%sobject: OBJ%x\n",
39 prefix, object->fscache.debug_id);
40 printk(KERN_ERR "%sobjstate=%s fl=%lx swfl=%lx ev=%lx[%lx]\n",
41 prefix, fscache_object_states[object->fscache.state],
42 object->fscache.flags, object->fscache.work.flags,
43 object->fscache.events,
44 object->fscache.event_mask & FSCACHE_OBJECT_EVENTS_MASK);
45 printk(KERN_ERR "%sops=%u inp=%u exc=%u\n",
46 prefix, object->fscache.n_ops, object->fscache.n_in_progress,
47 object->fscache.n_exclusive);
48 printk(KERN_ERR "%sparent=%p\n",
49 prefix, object->fscache.parent);
50
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;
68 }
69 spin_unlock(&object->fscache.lock);
70
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");
76 }
77}
78
79/*
80 * dump debugging info about a pair of objects
81 */
82static noinline void cachefiles_printk_object(struct cachefiles_object *object,
83 struct cachefiles_object *xobject)
84{
85 u8 *keybuf;
86
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);
93}
94
9ae326a6
DH
95/*
96 * record the fact that an object is now active
97 */
fee096de
DH
98static int cachefiles_mark_object_active(struct cachefiles_cache *cache,
99 struct cachefiles_object *object)
9ae326a6
DH
100{
101 struct cachefiles_object *xobject;
102 struct rb_node **_p, *_parent = NULL;
103 struct dentry *dentry;
104
105 _enter(",%p", object);
106
107try_again:
108 write_lock(&cache->active_lock);
109
d0e27b78
DH
110 if (test_and_set_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
111 printk(KERN_ERR "CacheFiles: Error: Object already active\n");
112 cachefiles_printk_object(object, NULL);
9ae326a6 113 BUG();
d0e27b78 114 }
9ae326a6
DH
115
116 dentry = object->dentry;
117 _p = &cache->active_nodes.rb_node;
118 while (*_p) {
119 _parent = *_p;
120 xobject = rb_entry(_parent,
121 struct cachefiles_object, active_node);
122
123 ASSERT(xobject != object);
124
125 if (xobject->dentry > dentry)
126 _p = &(*_p)->rb_left;
127 else if (xobject->dentry < dentry)
128 _p = &(*_p)->rb_right;
129 else
130 goto wait_for_old_object;
131 }
132
133 rb_link_node(&object->active_node, _parent, _p);
134 rb_insert_color(&object->active_node, &cache->active_nodes);
135
136 write_unlock(&cache->active_lock);
fee096de
DH
137 _leave(" = 0");
138 return 0;
9ae326a6
DH
139
140 /* an old object from a previous incarnation is hogging the slot - we
141 * need to wait for it to be destroyed */
142wait_for_old_object:
143 if (xobject->fscache.state < FSCACHE_OBJECT_DYING) {
144 printk(KERN_ERR "\n");
145 printk(KERN_ERR "CacheFiles: Error:"
146 " Unexpected object collision\n");
d0e27b78 147 cachefiles_printk_object(object, xobject);
9ae326a6
DH
148 BUG();
149 }
150 atomic_inc(&xobject->usage);
151 write_unlock(&cache->active_lock);
152
fee096de
DH
153 if (test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
154 wait_queue_head_t *wq;
155
156 signed long timeout = 60 * HZ;
157 wait_queue_t wait;
158 bool requeue;
159
160 /* if the object we're waiting for is queued for processing,
161 * then just put ourselves on the queue behind it */
162 if (slow_work_is_queued(&xobject->fscache.work)) {
163 _debug("queue OBJ%x behind OBJ%x immediately",
164 object->fscache.debug_id,
165 xobject->fscache.debug_id);
166 goto requeue;
167 }
168
169 /* otherwise we sleep until either the object we're waiting for
170 * is done, or the slow-work facility wants the thread back to
171 * do other work */
172 wq = bit_waitqueue(&xobject->flags, CACHEFILES_OBJECT_ACTIVE);
173 init_wait(&wait);
174 requeue = false;
175 do {
176 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
177 if (!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags))
178 break;
179 requeue = slow_work_sleep_till_thread_needed(
180 &object->fscache.work, &timeout);
181 } while (timeout > 0 && !requeue);
182 finish_wait(wq, &wait);
183
184 if (requeue &&
185 test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
186 _debug("queue OBJ%x behind OBJ%x after wait",
187 object->fscache.debug_id,
188 xobject->fscache.debug_id);
189 goto requeue;
190 }
191
192 if (timeout <= 0) {
193 printk(KERN_ERR "\n");
194 printk(KERN_ERR "CacheFiles: Error: Overlong"
195 " wait for old active object to go away\n");
196 cachefiles_printk_object(object, xobject);
197 goto requeue;
198 }
199 }
200
201 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags));
9ae326a6
DH
202
203 cache->cache.ops->put_object(&xobject->fscache);
204 goto try_again;
fee096de
DH
205
206requeue:
207 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
208 cache->cache.ops->put_object(&xobject->fscache);
209 _leave(" = -ETIMEDOUT");
210 return -ETIMEDOUT;
9ae326a6
DH
211}
212
213/*
214 * delete an object representation from the cache
215 * - file backed objects are unlinked
216 * - directory backed objects are stuffed into the graveyard for userspace to
217 * delete
218 * - unlocks the directory mutex
219 */
220static int cachefiles_bury_object(struct cachefiles_cache *cache,
221 struct dentry *dir,
222 struct dentry *rep)
223{
224 struct dentry *grave, *trap;
225 char nbuffer[8 + 8 + 1];
226 int ret;
227
228 _enter(",'%*.*s','%*.*s'",
229 dir->d_name.len, dir->d_name.len, dir->d_name.name,
230 rep->d_name.len, rep->d_name.len, rep->d_name.name);
231
232 /* non-directories can just be unlinked */
233 if (!S_ISDIR(rep->d_inode->i_mode)) {
234 _debug("unlink stale object");
235 ret = vfs_unlink(dir->d_inode, rep);
236
237 mutex_unlock(&dir->d_inode->i_mutex);
238
239 if (ret == -EIO)
240 cachefiles_io_error(cache, "Unlink failed");
241
242 _leave(" = %d", ret);
243 return ret;
244 }
245
246 /* directories have to be moved to the graveyard */
247 _debug("move stale object to graveyard");
248 mutex_unlock(&dir->d_inode->i_mutex);
249
250try_again:
251 /* first step is to make up a grave dentry in the graveyard */
252 sprintf(nbuffer, "%08x%08x",
253 (uint32_t) get_seconds(),
254 (uint32_t) atomic_inc_return(&cache->gravecounter));
255
256 /* do the multiway lock magic */
257 trap = lock_rename(cache->graveyard, dir);
258
259 /* do some checks before getting the grave dentry */
260 if (rep->d_parent != dir) {
261 /* the entry was probably culled when we dropped the parent dir
262 * lock */
263 unlock_rename(cache->graveyard, dir);
264 _leave(" = 0 [culled?]");
265 return 0;
266 }
267
268 if (!S_ISDIR(cache->graveyard->d_inode->i_mode)) {
269 unlock_rename(cache->graveyard, dir);
270 cachefiles_io_error(cache, "Graveyard no longer a directory");
271 return -EIO;
272 }
273
274 if (trap == rep) {
275 unlock_rename(cache->graveyard, dir);
276 cachefiles_io_error(cache, "May not make directory loop");
277 return -EIO;
278 }
279
280 if (d_mountpoint(rep)) {
281 unlock_rename(cache->graveyard, dir);
282 cachefiles_io_error(cache, "Mountpoint in cache");
283 return -EIO;
284 }
285
286 grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
287 if (IS_ERR(grave)) {
288 unlock_rename(cache->graveyard, dir);
289
290 if (PTR_ERR(grave) == -ENOMEM) {
291 _leave(" = -ENOMEM");
292 return -ENOMEM;
293 }
294
295 cachefiles_io_error(cache, "Lookup error %ld",
296 PTR_ERR(grave));
297 return -EIO;
298 }
299
300 if (grave->d_inode) {
301 unlock_rename(cache->graveyard, dir);
302 dput(grave);
303 grave = NULL;
304 cond_resched();
305 goto try_again;
306 }
307
308 if (d_mountpoint(grave)) {
309 unlock_rename(cache->graveyard, dir);
310 dput(grave);
311 cachefiles_io_error(cache, "Mountpoint in graveyard");
312 return -EIO;
313 }
314
315 /* target should not be an ancestor of source */
316 if (trap == grave) {
317 unlock_rename(cache->graveyard, dir);
318 dput(grave);
319 cachefiles_io_error(cache, "May not make directory loop");
320 return -EIO;
321 }
322
323 /* attempt the rename */
324 ret = vfs_rename(dir->d_inode, rep, cache->graveyard->d_inode, grave);
325 if (ret != 0 && ret != -ENOMEM)
326 cachefiles_io_error(cache, "Rename failed with error %d", ret);
327
328 unlock_rename(cache->graveyard, dir);
329 dput(grave);
330 _leave(" = 0");
331 return 0;
332}
333
334/*
335 * delete an object representation from the cache
336 */
337int cachefiles_delete_object(struct cachefiles_cache *cache,
338 struct cachefiles_object *object)
339{
340 struct dentry *dir;
341 int ret;
342
343 _enter(",{%p}", object->dentry);
344
345 ASSERT(object->dentry);
346 ASSERT(object->dentry->d_inode);
347 ASSERT(object->dentry->d_parent);
348
349 dir = dget_parent(object->dentry);
350
6511de33 351 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
8f9941ae
DH
352
353 /* we need to check that our parent is _still_ our parent - it may have
354 * been renamed */
355 if (dir == object->dentry->d_parent) {
356 ret = cachefiles_bury_object(cache, dir, object->dentry);
357 } else {
358 /* it got moved, presumably by cachefilesd culling it, so it's
359 * no longer in the key path and we can ignore it */
360 mutex_unlock(&dir->d_inode->i_mutex);
361 ret = 0;
362 }
9ae326a6
DH
363
364 dput(dir);
365 _leave(" = %d", ret);
366 return ret;
367}
368
369/*
370 * walk from the parent object to the child object through the backing
371 * filesystem, creating directories as we go
372 */
373int cachefiles_walk_to_object(struct cachefiles_object *parent,
374 struct cachefiles_object *object,
375 const char *key,
376 struct cachefiles_xattr *auxdata)
377{
378 struct cachefiles_cache *cache;
379 struct dentry *dir, *next = NULL;
380 unsigned long start;
381 const char *name;
382 int ret, nlen;
383
384 _enter("{%p},,%s,", parent->dentry, key);
385
386 cache = container_of(parent->fscache.cache,
387 struct cachefiles_cache, cache);
388
389 ASSERT(parent->dentry);
390 ASSERT(parent->dentry->d_inode);
391
392 if (!(S_ISDIR(parent->dentry->d_inode->i_mode))) {
393 // TODO: convert file to dir
394 _leave("looking up in none directory");
395 return -ENOBUFS;
396 }
397
398 dir = dget(parent->dentry);
399
400advance:
401 /* attempt to transit the first directory component */
402 name = key;
403 nlen = strlen(key);
404
405 /* key ends in a double NUL */
406 key = key + nlen + 1;
407 if (!*key)
408 key = NULL;
409
410lookup_again:
411 /* search the current directory for the element name */
412 _debug("lookup '%s'", name);
413
6511de33 414 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
9ae326a6
DH
415
416 start = jiffies;
417 next = lookup_one_len(name, dir, nlen);
418 cachefiles_hist(cachefiles_lookup_histogram, start);
419 if (IS_ERR(next))
420 goto lookup_error;
421
422 _debug("next -> %p %s", next, next->d_inode ? "positive" : "negative");
423
424 if (!key)
425 object->new = !next->d_inode;
426
427 /* if this element of the path doesn't exist, then the lookup phase
428 * failed, and we can release any readers in the certain knowledge that
429 * there's nothing for them to actually read */
430 if (!next->d_inode)
431 fscache_object_lookup_negative(&object->fscache);
432
433 /* we need to create the object if it's negative */
434 if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
435 /* index objects and intervening tree levels must be subdirs */
436 if (!next->d_inode) {
437 ret = cachefiles_has_space(cache, 1, 0);
438 if (ret < 0)
439 goto create_error;
440
441 start = jiffies;
442 ret = vfs_mkdir(dir->d_inode, next, 0);
443 cachefiles_hist(cachefiles_mkdir_histogram, start);
444 if (ret < 0)
445 goto create_error;
446
447 ASSERT(next->d_inode);
448
449 _debug("mkdir -> %p{%p{ino=%lu}}",
450 next, next->d_inode, next->d_inode->i_ino);
451
452 } else if (!S_ISDIR(next->d_inode->i_mode)) {
453 kerror("inode %lu is not a directory",
454 next->d_inode->i_ino);
455 ret = -ENOBUFS;
456 goto error;
457 }
458
459 } else {
460 /* non-index objects start out life as files */
461 if (!next->d_inode) {
462 ret = cachefiles_has_space(cache, 1, 0);
463 if (ret < 0)
464 goto create_error;
465
466 start = jiffies;
467 ret = vfs_create(dir->d_inode, next, S_IFREG, NULL);
468 cachefiles_hist(cachefiles_create_histogram, start);
469 if (ret < 0)
470 goto create_error;
471
472 ASSERT(next->d_inode);
473
474 _debug("create -> %p{%p{ino=%lu}}",
475 next, next->d_inode, next->d_inode->i_ino);
476
477 } else if (!S_ISDIR(next->d_inode->i_mode) &&
478 !S_ISREG(next->d_inode->i_mode)
479 ) {
480 kerror("inode %lu is not a file or directory",
481 next->d_inode->i_ino);
482 ret = -ENOBUFS;
483 goto error;
484 }
485 }
486
487 /* process the next component */
488 if (key) {
489 _debug("advance");
490 mutex_unlock(&dir->d_inode->i_mutex);
491 dput(dir);
492 dir = next;
493 next = NULL;
494 goto advance;
495 }
496
497 /* we've found the object we were looking for */
498 object->dentry = next;
499
500 /* if we've found that the terminal object exists, then we need to
501 * check its attributes and delete it if it's out of date */
502 if (!object->new) {
503 _debug("validate '%*.*s'",
504 next->d_name.len, next->d_name.len, next->d_name.name);
505
506 ret = cachefiles_check_object_xattr(object, auxdata);
507 if (ret == -ESTALE) {
508 /* delete the object (the deleter drops the directory
509 * mutex) */
510 object->dentry = NULL;
511
512 ret = cachefiles_bury_object(cache, dir, next);
513 dput(next);
514 next = NULL;
515
516 if (ret < 0)
517 goto delete_error;
518
519 _debug("redo lookup");
520 goto lookup_again;
521 }
522 }
523
524 /* note that we're now using this object */
fee096de 525 ret = cachefiles_mark_object_active(cache, object);
9ae326a6
DH
526
527 mutex_unlock(&dir->d_inode->i_mutex);
528 dput(dir);
529 dir = NULL;
530
fee096de
DH
531 if (ret == -ETIMEDOUT)
532 goto mark_active_timed_out;
533
9ae326a6
DH
534 _debug("=== OBTAINED_OBJECT ===");
535
536 if (object->new) {
537 /* attach data to a newly constructed terminal object */
538 ret = cachefiles_set_object_xattr(object, auxdata);
539 if (ret < 0)
540 goto check_error;
541 } else {
542 /* always update the atime on an object we've just looked up
543 * (this is used to keep track of culling, and atimes are only
544 * updated by read, write and readdir but not lookup or
545 * open) */
546 touch_atime(cache->mnt, next);
547 }
548
549 /* open a file interface onto a data file */
550 if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
551 if (S_ISREG(object->dentry->d_inode->i_mode)) {
552 const struct address_space_operations *aops;
553
554 ret = -EPERM;
555 aops = object->dentry->d_inode->i_mapping->a_ops;
556 if (!aops->bmap)
557 goto check_error;
558
559 object->backer = object->dentry;
560 } else {
561 BUG(); // TODO: open file in data-class subdir
562 }
563 }
564
565 object->new = 0;
566 fscache_obtained_object(&object->fscache);
567
568 _leave(" = 0 [%lu]", object->dentry->d_inode->i_ino);
569 return 0;
570
571create_error:
572 _debug("create error %d", ret);
573 if (ret == -EIO)
574 cachefiles_io_error(cache, "Create/mkdir failed");
575 goto error;
576
fee096de
DH
577mark_active_timed_out:
578 _debug("mark active timed out");
579 goto release_dentry;
580
9ae326a6
DH
581check_error:
582 _debug("check error %d", ret);
583 write_lock(&cache->active_lock);
584 rb_erase(&object->active_node, &cache->active_nodes);
585 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
586 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
587 write_unlock(&cache->active_lock);
fee096de 588release_dentry:
9ae326a6
DH
589 dput(object->dentry);
590 object->dentry = NULL;
591 goto error_out;
592
593delete_error:
594 _debug("delete error %d", ret);
595 goto error_out2;
596
597lookup_error:
598 _debug("lookup error %ld", PTR_ERR(next));
599 ret = PTR_ERR(next);
600 if (ret == -EIO)
601 cachefiles_io_error(cache, "Lookup failed");
602 next = NULL;
603error:
604 mutex_unlock(&dir->d_inode->i_mutex);
605 dput(next);
606error_out2:
607 dput(dir);
608error_out:
9ae326a6
DH
609 _leave(" = error %d", -ret);
610 return ret;
611}
612
613/*
614 * get a subdirectory
615 */
616struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
617 struct dentry *dir,
618 const char *dirname)
619{
620 struct dentry *subdir;
621 unsigned long start;
622 int ret;
623
624 _enter(",,%s", dirname);
625
626 /* search the current directory for the element name */
627 mutex_lock(&dir->d_inode->i_mutex);
628
629 start = jiffies;
630 subdir = lookup_one_len(dirname, dir, strlen(dirname));
631 cachefiles_hist(cachefiles_lookup_histogram, start);
632 if (IS_ERR(subdir)) {
633 if (PTR_ERR(subdir) == -ENOMEM)
634 goto nomem_d_alloc;
635 goto lookup_error;
636 }
637
638 _debug("subdir -> %p %s",
639 subdir, subdir->d_inode ? "positive" : "negative");
640
641 /* we need to create the subdir if it doesn't exist yet */
642 if (!subdir->d_inode) {
643 ret = cachefiles_has_space(cache, 1, 0);
644 if (ret < 0)
645 goto mkdir_error;
646
647 _debug("attempt mkdir");
648
649 ret = vfs_mkdir(dir->d_inode, subdir, 0700);
650 if (ret < 0)
651 goto mkdir_error;
652
653 ASSERT(subdir->d_inode);
654
655 _debug("mkdir -> %p{%p{ino=%lu}}",
656 subdir,
657 subdir->d_inode,
658 subdir->d_inode->i_ino);
659 }
660
661 mutex_unlock(&dir->d_inode->i_mutex);
662
663 /* we need to make sure the subdir is a directory */
664 ASSERT(subdir->d_inode);
665
666 if (!S_ISDIR(subdir->d_inode->i_mode)) {
667 kerror("%s is not a directory", dirname);
668 ret = -EIO;
669 goto check_error;
670 }
671
672 ret = -EPERM;
673 if (!subdir->d_inode->i_op ||
674 !subdir->d_inode->i_op->setxattr ||
675 !subdir->d_inode->i_op->getxattr ||
676 !subdir->d_inode->i_op->lookup ||
677 !subdir->d_inode->i_op->mkdir ||
678 !subdir->d_inode->i_op->create ||
679 !subdir->d_inode->i_op->rename ||
680 !subdir->d_inode->i_op->rmdir ||
681 !subdir->d_inode->i_op->unlink)
682 goto check_error;
683
684 _leave(" = [%lu]", subdir->d_inode->i_ino);
685 return subdir;
686
687check_error:
688 dput(subdir);
689 _leave(" = %d [check]", ret);
690 return ERR_PTR(ret);
691
692mkdir_error:
693 mutex_unlock(&dir->d_inode->i_mutex);
694 dput(subdir);
695 kerror("mkdir %s failed with error %d", dirname, ret);
696 return ERR_PTR(ret);
697
698lookup_error:
699 mutex_unlock(&dir->d_inode->i_mutex);
700 ret = PTR_ERR(subdir);
701 kerror("Lookup %s failed with error %d", dirname, ret);
702 return ERR_PTR(ret);
703
704nomem_d_alloc:
705 mutex_unlock(&dir->d_inode->i_mutex);
706 _leave(" = -ENOMEM");
707 return ERR_PTR(-ENOMEM);
708}
709
710/*
711 * find out if an object is in use or not
712 * - if finds object and it's not in use:
713 * - returns a pointer to the object and a reference on it
714 * - returns with the directory locked
715 */
716static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
717 struct dentry *dir,
718 char *filename)
719{
720 struct cachefiles_object *object;
721 struct rb_node *_n;
722 struct dentry *victim;
723 unsigned long start;
724 int ret;
725
726 //_enter(",%*.*s/,%s",
727 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
728
729 /* look up the victim */
730 mutex_lock_nested(&dir->d_inode->i_mutex, 1);
731
732 start = jiffies;
733 victim = lookup_one_len(filename, dir, strlen(filename));
734 cachefiles_hist(cachefiles_lookup_histogram, start);
735 if (IS_ERR(victim))
736 goto lookup_error;
737
738 //_debug("victim -> %p %s",
739 // victim, victim->d_inode ? "positive" : "negative");
740
741 /* if the object is no longer there then we probably retired the object
742 * at the netfs's request whilst the cull was in progress
743 */
744 if (!victim->d_inode) {
745 mutex_unlock(&dir->d_inode->i_mutex);
746 dput(victim);
747 _leave(" = -ENOENT [absent]");
748 return ERR_PTR(-ENOENT);
749 }
750
751 /* check to see if we're using this object */
752 read_lock(&cache->active_lock);
753
754 _n = cache->active_nodes.rb_node;
755
756 while (_n) {
757 object = rb_entry(_n, struct cachefiles_object, active_node);
758
759 if (object->dentry > victim)
760 _n = _n->rb_left;
761 else if (object->dentry < victim)
762 _n = _n->rb_right;
763 else
764 goto object_in_use;
765 }
766
767 read_unlock(&cache->active_lock);
768
769 //_leave(" = %p", victim);
770 return victim;
771
772object_in_use:
773 read_unlock(&cache->active_lock);
774 mutex_unlock(&dir->d_inode->i_mutex);
775 dput(victim);
776 //_leave(" = -EBUSY [in use]");
777 return ERR_PTR(-EBUSY);
778
779lookup_error:
780 mutex_unlock(&dir->d_inode->i_mutex);
781 ret = PTR_ERR(victim);
782 if (ret == -ENOENT) {
783 /* file or dir now absent - probably retired by netfs */
784 _leave(" = -ESTALE [absent]");
785 return ERR_PTR(-ESTALE);
786 }
787
788 if (ret == -EIO) {
789 cachefiles_io_error(cache, "Lookup failed");
790 } else if (ret != -ENOMEM) {
791 kerror("Internal error: %d", ret);
792 ret = -EIO;
793 }
794
795 _leave(" = %d", ret);
796 return ERR_PTR(ret);
797}
798
799/*
800 * cull an object if it's not in use
801 * - called only by cache manager daemon
802 */
803int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
804 char *filename)
805{
806 struct dentry *victim;
807 int ret;
808
809 _enter(",%*.*s/,%s",
810 dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
811
812 victim = cachefiles_check_active(cache, dir, filename);
813 if (IS_ERR(victim))
814 return PTR_ERR(victim);
815
816 _debug("victim -> %p %s",
817 victim, victim->d_inode ? "positive" : "negative");
818
819 /* okay... the victim is not being used so we can cull it
820 * - start by marking it as stale
821 */
822 _debug("victim is cullable");
823
824 ret = cachefiles_remove_object_xattr(cache, victim);
825 if (ret < 0)
826 goto error_unlock;
827
828 /* actually remove the victim (drops the dir mutex) */
829 _debug("bury");
830
831 ret = cachefiles_bury_object(cache, dir, victim);
832 if (ret < 0)
833 goto error;
834
835 dput(victim);
836 _leave(" = 0");
837 return 0;
838
839error_unlock:
840 mutex_unlock(&dir->d_inode->i_mutex);
841error:
842 dput(victim);
843 if (ret == -ENOENT) {
844 /* file or dir now absent - probably retired by netfs */
845 _leave(" = -ESTALE [absent]");
846 return -ESTALE;
847 }
848
849 if (ret != -ENOMEM) {
850 kerror("Internal error: %d", ret);
851 ret = -EIO;
852 }
853
854 _leave(" = %d", ret);
855 return ret;
856}
857
858/*
859 * find out if an object is in use or not
860 * - called only by cache manager daemon
861 * - returns -EBUSY or 0 to indicate whether an object is in use or not
862 */
863int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
864 char *filename)
865{
866 struct dentry *victim;
867
868 //_enter(",%*.*s/,%s",
869 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
870
871 victim = cachefiles_check_active(cache, dir, filename);
872 if (IS_ERR(victim))
873 return PTR_ERR(victim);
874
875 mutex_unlock(&dir->d_inode->i_mutex);
876 dput(victim);
877 //_leave(" = 0");
878 return 0;
879}