blob: f4a7840bf42cbb4c4be9cf74a7488e61aab4c94d [file] [log] [blame]
David Howells9ae326a2009-04-03 16:42:41 +01001/* 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Howells9ae326a2009-04-03 16:42:41 +010023#include "internal.h"
24
David Howellsd0e27b72009-11-19 18:12:02 +000025#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
David Howells9ae326a2009-04-03 16:42:41 +010095/*
David Howellsc61ea312010-05-11 16:51:39 +010096 * 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()
100 */
101static void cachefiles_mark_object_buried(struct cachefiles_cache *cache,
102 struct dentry *dentry)
103{
104 struct cachefiles_object *object;
105 struct rb_node *p;
106
107 _enter(",'%*.*s'",
108 dentry->d_name.len, dentry->d_name.len, dentry->d_name.name);
109
110 write_lock(&cache->active_lock);
111
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;
121 }
122
123 write_unlock(&cache->active_lock);
124 _leave(" [no owner]");
125 return;
126
127 /* found the dentry for */
128found_dentry:
129 kdebug("preemptive burial: OBJ%x [%s] %p",
130 object->fscache.debug_id,
131 fscache_object_states[object->fscache.state],
132 dentry);
133
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");
142 }
143
144 write_unlock(&cache->active_lock);
145 _leave(" [owner marked]");
146}
147
148/*
David Howells9ae326a2009-04-03 16:42:41 +0100149 * record the fact that an object is now active
150 */
David Howellsfee096d2009-11-19 18:12:05 +0000151static int cachefiles_mark_object_active(struct cachefiles_cache *cache,
152 struct cachefiles_object *object)
David Howells9ae326a2009-04-03 16:42:41 +0100153{
154 struct cachefiles_object *xobject;
155 struct rb_node **_p, *_parent = NULL;
156 struct dentry *dentry;
157
158 _enter(",%p", object);
159
160try_again:
161 write_lock(&cache->active_lock);
162
David Howellsd0e27b72009-11-19 18:12:02 +0000163 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);
David Howells9ae326a2009-04-03 16:42:41 +0100166 BUG();
David Howellsd0e27b72009-11-19 18:12:02 +0000167 }
David Howells9ae326a2009-04-03 16:42:41 +0100168
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);
175
176 ASSERT(xobject != object);
177
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;
184 }
185
186 rb_link_node(&object->active_node, _parent, _p);
187 rb_insert_color(&object->active_node, &cache->active_nodes);
188
189 write_unlock(&cache->active_lock);
David Howellsfee096d2009-11-19 18:12:05 +0000190 _leave(" = 0");
191 return 0;
David Howells9ae326a2009-04-03 16:42:41 +0100192
193 /* an old object from a previous incarnation is hogging the slot - we
194 * need to wait for it to be destroyed */
195wait_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");
David Howellsd0e27b72009-11-19 18:12:02 +0000200 cachefiles_printk_object(object, xobject);
David Howells9ae326a2009-04-03 16:42:41 +0100201 BUG();
202 }
203 atomic_inc(&xobject->usage);
204 write_unlock(&cache->active_lock);
205
David Howellsfee096d2009-11-19 18:12:05 +0000206 if (test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
207 wait_queue_head_t *wq;
208
209 signed long timeout = 60 * HZ;
210 wait_queue_t wait;
211 bool requeue;
212
213 /* if the object we're waiting for is queued for processing,
214 * then just put ourselves on the queue behind it */
215 if (slow_work_is_queued(&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;
220 }
221
222 /* otherwise we sleep until either the object we're waiting for
223 * is done, or the slow-work facility wants the thread back to
224 * do other work */
225 wq = bit_waitqueue(&xobject->flags, CACHEFILES_OBJECT_ACTIVE);
226 init_wait(&wait);
227 requeue = false;
228 do {
229 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
230 if (!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags))
231 break;
232 requeue = slow_work_sleep_till_thread_needed(
233 &object->fscache.work, &timeout);
234 } while (timeout > 0 && !requeue);
235 finish_wait(wq, &wait);
236
237 if (requeue &&
238 test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
239 _debug("queue OBJ%x behind OBJ%x after wait",
240 object->fscache.debug_id,
241 xobject->fscache.debug_id);
242 goto requeue;
243 }
244
245 if (timeout <= 0) {
246 printk(KERN_ERR "\n");
247 printk(KERN_ERR "CacheFiles: Error: Overlong"
248 " wait for old active object to go away\n");
249 cachefiles_printk_object(object, xobject);
250 goto requeue;
251 }
252 }
253
254 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags));
David Howells9ae326a2009-04-03 16:42:41 +0100255
256 cache->cache.ops->put_object(&xobject->fscache);
257 goto try_again;
David Howellsfee096d2009-11-19 18:12:05 +0000258
259requeue:
260 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
261 cache->cache.ops->put_object(&xobject->fscache);
262 _leave(" = -ETIMEDOUT");
263 return -ETIMEDOUT;
David Howells9ae326a2009-04-03 16:42:41 +0100264}
265
266/*
267 * delete an object representation from the cache
268 * - file backed objects are unlinked
269 * - directory backed objects are stuffed into the graveyard for userspace to
270 * delete
271 * - unlocks the directory mutex
272 */
273static int cachefiles_bury_object(struct cachefiles_cache *cache,
274 struct dentry *dir,
David Howellsc61ea312010-05-11 16:51:39 +0100275 struct dentry *rep,
276 bool preemptive)
David Howells9ae326a2009-04-03 16:42:41 +0100277{
278 struct dentry *grave, *trap;
279 char nbuffer[8 + 8 + 1];
280 int ret;
281
282 _enter(",'%*.*s','%*.*s'",
283 dir->d_name.len, dir->d_name.len, dir->d_name.name,
284 rep->d_name.len, rep->d_name.len, rep->d_name.name);
285
David Howellsc61ea312010-05-11 16:51:39 +0100286 _debug("remove %p from %p", rep, dir);
287
David Howells9ae326a2009-04-03 16:42:41 +0100288 /* non-directories can just be unlinked */
289 if (!S_ISDIR(rep->d_inode->i_mode)) {
290 _debug("unlink stale object");
291 ret = vfs_unlink(dir->d_inode, rep);
292
David Howellsc61ea312010-05-11 16:51:39 +0100293 if (preemptive)
294 cachefiles_mark_object_buried(cache, rep);
295
David Howells9ae326a2009-04-03 16:42:41 +0100296 mutex_unlock(&dir->d_inode->i_mutex);
297
298 if (ret == -EIO)
299 cachefiles_io_error(cache, "Unlink failed");
300
301 _leave(" = %d", ret);
302 return ret;
303 }
304
305 /* directories have to be moved to the graveyard */
306 _debug("move stale object to graveyard");
307 mutex_unlock(&dir->d_inode->i_mutex);
308
309try_again:
310 /* first step is to make up a grave dentry in the graveyard */
311 sprintf(nbuffer, "%08x%08x",
312 (uint32_t) get_seconds(),
313 (uint32_t) atomic_inc_return(&cache->gravecounter));
314
315 /* do the multiway lock magic */
316 trap = lock_rename(cache->graveyard, dir);
317
318 /* do some checks before getting the grave dentry */
319 if (rep->d_parent != dir) {
320 /* the entry was probably culled when we dropped the parent dir
321 * lock */
322 unlock_rename(cache->graveyard, dir);
323 _leave(" = 0 [culled?]");
324 return 0;
325 }
326
327 if (!S_ISDIR(cache->graveyard->d_inode->i_mode)) {
328 unlock_rename(cache->graveyard, dir);
329 cachefiles_io_error(cache, "Graveyard no longer a directory");
330 return -EIO;
331 }
332
333 if (trap == rep) {
334 unlock_rename(cache->graveyard, dir);
335 cachefiles_io_error(cache, "May not make directory loop");
336 return -EIO;
337 }
338
339 if (d_mountpoint(rep)) {
340 unlock_rename(cache->graveyard, dir);
341 cachefiles_io_error(cache, "Mountpoint in cache");
342 return -EIO;
343 }
344
345 grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
346 if (IS_ERR(grave)) {
347 unlock_rename(cache->graveyard, dir);
348
349 if (PTR_ERR(grave) == -ENOMEM) {
350 _leave(" = -ENOMEM");
351 return -ENOMEM;
352 }
353
354 cachefiles_io_error(cache, "Lookup error %ld",
355 PTR_ERR(grave));
356 return -EIO;
357 }
358
359 if (grave->d_inode) {
360 unlock_rename(cache->graveyard, dir);
361 dput(grave);
362 grave = NULL;
363 cond_resched();
364 goto try_again;
365 }
366
367 if (d_mountpoint(grave)) {
368 unlock_rename(cache->graveyard, dir);
369 dput(grave);
370 cachefiles_io_error(cache, "Mountpoint in graveyard");
371 return -EIO;
372 }
373
374 /* target should not be an ancestor of source */
375 if (trap == grave) {
376 unlock_rename(cache->graveyard, dir);
377 dput(grave);
378 cachefiles_io_error(cache, "May not make directory loop");
379 return -EIO;
380 }
381
382 /* attempt the rename */
383 ret = vfs_rename(dir->d_inode, rep, cache->graveyard->d_inode, grave);
384 if (ret != 0 && ret != -ENOMEM)
385 cachefiles_io_error(cache, "Rename failed with error %d", ret);
386
David Howellsc61ea312010-05-11 16:51:39 +0100387 if (preemptive)
388 cachefiles_mark_object_buried(cache, rep);
389
David Howells9ae326a2009-04-03 16:42:41 +0100390 unlock_rename(cache->graveyard, dir);
391 dput(grave);
392 _leave(" = 0");
393 return 0;
394}
395
396/*
397 * delete an object representation from the cache
398 */
399int cachefiles_delete_object(struct cachefiles_cache *cache,
400 struct cachefiles_object *object)
401{
402 struct dentry *dir;
403 int ret;
404
David Howellsc61ea312010-05-11 16:51:39 +0100405 _enter(",OBJ%x{%p}", object->fscache.debug_id, object->dentry);
David Howells9ae326a2009-04-03 16:42:41 +0100406
407 ASSERT(object->dentry);
408 ASSERT(object->dentry->d_inode);
409 ASSERT(object->dentry->d_parent);
410
411 dir = dget_parent(object->dentry);
412
David Howells6511de32009-11-19 18:11:58 +0000413 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
David Howells8f9941a2010-02-19 18:14:21 +0000414
David Howellsc61ea312010-05-11 16:51:39 +0100415 if (test_bit(CACHEFILES_OBJECT_BURIED, &object->flags)) {
416 /* object allocation for the same key preemptively deleted this
417 * object's file so that it could create its own file */
418 _debug("object preemptively buried");
David Howells8f9941a2010-02-19 18:14:21 +0000419 mutex_unlock(&dir->d_inode->i_mutex);
420 ret = 0;
David Howellsc61ea312010-05-11 16:51:39 +0100421 } else {
422 /* we need to check that our parent is _still_ our parent - it
423 * may have been renamed */
424 if (dir == object->dentry->d_parent) {
425 ret = cachefiles_bury_object(cache, dir,
426 object->dentry, false);
427 } else {
428 /* it got moved, presumably by cachefilesd culling it,
429 * so it's no longer in the key path and we can ignore
430 * it */
431 mutex_unlock(&dir->d_inode->i_mutex);
432 ret = 0;
433 }
David Howells8f9941a2010-02-19 18:14:21 +0000434 }
David Howells9ae326a2009-04-03 16:42:41 +0100435
436 dput(dir);
437 _leave(" = %d", ret);
438 return ret;
439}
440
441/*
442 * walk from the parent object to the child object through the backing
443 * filesystem, creating directories as we go
444 */
445int cachefiles_walk_to_object(struct cachefiles_object *parent,
446 struct cachefiles_object *object,
447 const char *key,
448 struct cachefiles_xattr *auxdata)
449{
450 struct cachefiles_cache *cache;
451 struct dentry *dir, *next = NULL;
452 unsigned long start;
453 const char *name;
454 int ret, nlen;
455
David Howellsc61ea312010-05-11 16:51:39 +0100456 _enter("OBJ%x{%p},OBJ%x,%s,",
457 parent->fscache.debug_id, parent->dentry,
458 object->fscache.debug_id, key);
David Howells9ae326a2009-04-03 16:42:41 +0100459
460 cache = container_of(parent->fscache.cache,
461 struct cachefiles_cache, cache);
462
463 ASSERT(parent->dentry);
464 ASSERT(parent->dentry->d_inode);
465
466 if (!(S_ISDIR(parent->dentry->d_inode->i_mode))) {
467 // TODO: convert file to dir
468 _leave("looking up in none directory");
469 return -ENOBUFS;
470 }
471
472 dir = dget(parent->dentry);
473
474advance:
475 /* attempt to transit the first directory component */
476 name = key;
477 nlen = strlen(key);
478
479 /* key ends in a double NUL */
480 key = key + nlen + 1;
481 if (!*key)
482 key = NULL;
483
484lookup_again:
485 /* search the current directory for the element name */
486 _debug("lookup '%s'", name);
487
David Howells6511de32009-11-19 18:11:58 +0000488 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
David Howells9ae326a2009-04-03 16:42:41 +0100489
490 start = jiffies;
491 next = lookup_one_len(name, dir, nlen);
492 cachefiles_hist(cachefiles_lookup_histogram, start);
493 if (IS_ERR(next))
494 goto lookup_error;
495
496 _debug("next -> %p %s", next, next->d_inode ? "positive" : "negative");
497
498 if (!key)
499 object->new = !next->d_inode;
500
501 /* if this element of the path doesn't exist, then the lookup phase
502 * failed, and we can release any readers in the certain knowledge that
503 * there's nothing for them to actually read */
504 if (!next->d_inode)
505 fscache_object_lookup_negative(&object->fscache);
506
507 /* we need to create the object if it's negative */
508 if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
509 /* index objects and intervening tree levels must be subdirs */
510 if (!next->d_inode) {
511 ret = cachefiles_has_space(cache, 1, 0);
512 if (ret < 0)
513 goto create_error;
514
515 start = jiffies;
516 ret = vfs_mkdir(dir->d_inode, next, 0);
517 cachefiles_hist(cachefiles_mkdir_histogram, start);
518 if (ret < 0)
519 goto create_error;
520
521 ASSERT(next->d_inode);
522
523 _debug("mkdir -> %p{%p{ino=%lu}}",
524 next, next->d_inode, next->d_inode->i_ino);
525
526 } else if (!S_ISDIR(next->d_inode->i_mode)) {
527 kerror("inode %lu is not a directory",
528 next->d_inode->i_ino);
529 ret = -ENOBUFS;
530 goto error;
531 }
532
533 } else {
534 /* non-index objects start out life as files */
535 if (!next->d_inode) {
536 ret = cachefiles_has_space(cache, 1, 0);
537 if (ret < 0)
538 goto create_error;
539
540 start = jiffies;
541 ret = vfs_create(dir->d_inode, next, S_IFREG, NULL);
542 cachefiles_hist(cachefiles_create_histogram, start);
543 if (ret < 0)
544 goto create_error;
545
546 ASSERT(next->d_inode);
547
548 _debug("create -> %p{%p{ino=%lu}}",
549 next, next->d_inode, next->d_inode->i_ino);
550
551 } else if (!S_ISDIR(next->d_inode->i_mode) &&
552 !S_ISREG(next->d_inode->i_mode)
553 ) {
554 kerror("inode %lu is not a file or directory",
555 next->d_inode->i_ino);
556 ret = -ENOBUFS;
557 goto error;
558 }
559 }
560
561 /* process the next component */
562 if (key) {
563 _debug("advance");
564 mutex_unlock(&dir->d_inode->i_mutex);
565 dput(dir);
566 dir = next;
567 next = NULL;
568 goto advance;
569 }
570
571 /* we've found the object we were looking for */
572 object->dentry = next;
573
574 /* if we've found that the terminal object exists, then we need to
575 * check its attributes and delete it if it's out of date */
576 if (!object->new) {
577 _debug("validate '%*.*s'",
578 next->d_name.len, next->d_name.len, next->d_name.name);
579
580 ret = cachefiles_check_object_xattr(object, auxdata);
581 if (ret == -ESTALE) {
582 /* delete the object (the deleter drops the directory
583 * mutex) */
584 object->dentry = NULL;
585
David Howellsc61ea312010-05-11 16:51:39 +0100586 ret = cachefiles_bury_object(cache, dir, next, true);
David Howells9ae326a2009-04-03 16:42:41 +0100587 dput(next);
588 next = NULL;
589
590 if (ret < 0)
591 goto delete_error;
592
593 _debug("redo lookup");
594 goto lookup_again;
595 }
596 }
597
598 /* note that we're now using this object */
David Howellsfee096d2009-11-19 18:12:05 +0000599 ret = cachefiles_mark_object_active(cache, object);
David Howells9ae326a2009-04-03 16:42:41 +0100600
601 mutex_unlock(&dir->d_inode->i_mutex);
602 dput(dir);
603 dir = NULL;
604
David Howellsfee096d2009-11-19 18:12:05 +0000605 if (ret == -ETIMEDOUT)
606 goto mark_active_timed_out;
607
David Howells9ae326a2009-04-03 16:42:41 +0100608 _debug("=== OBTAINED_OBJECT ===");
609
610 if (object->new) {
611 /* attach data to a newly constructed terminal object */
612 ret = cachefiles_set_object_xattr(object, auxdata);
613 if (ret < 0)
614 goto check_error;
615 } else {
616 /* always update the atime on an object we've just looked up
617 * (this is used to keep track of culling, and atimes are only
618 * updated by read, write and readdir but not lookup or
619 * open) */
620 touch_atime(cache->mnt, next);
621 }
622
623 /* open a file interface onto a data file */
624 if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
625 if (S_ISREG(object->dentry->d_inode->i_mode)) {
626 const struct address_space_operations *aops;
627
628 ret = -EPERM;
629 aops = object->dentry->d_inode->i_mapping->a_ops;
630 if (!aops->bmap)
631 goto check_error;
632
633 object->backer = object->dentry;
634 } else {
635 BUG(); // TODO: open file in data-class subdir
636 }
637 }
638
639 object->new = 0;
640 fscache_obtained_object(&object->fscache);
641
642 _leave(" = 0 [%lu]", object->dentry->d_inode->i_ino);
643 return 0;
644
645create_error:
646 _debug("create error %d", ret);
647 if (ret == -EIO)
648 cachefiles_io_error(cache, "Create/mkdir failed");
649 goto error;
650
David Howellsfee096d2009-11-19 18:12:05 +0000651mark_active_timed_out:
652 _debug("mark active timed out");
653 goto release_dentry;
654
David Howells9ae326a2009-04-03 16:42:41 +0100655check_error:
656 _debug("check error %d", ret);
657 write_lock(&cache->active_lock);
658 rb_erase(&object->active_node, &cache->active_nodes);
659 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
660 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
661 write_unlock(&cache->active_lock);
David Howellsfee096d2009-11-19 18:12:05 +0000662release_dentry:
David Howells9ae326a2009-04-03 16:42:41 +0100663 dput(object->dentry);
664 object->dentry = NULL;
665 goto error_out;
666
667delete_error:
668 _debug("delete error %d", ret);
669 goto error_out2;
670
671lookup_error:
672 _debug("lookup error %ld", PTR_ERR(next));
673 ret = PTR_ERR(next);
674 if (ret == -EIO)
675 cachefiles_io_error(cache, "Lookup failed");
676 next = NULL;
677error:
678 mutex_unlock(&dir->d_inode->i_mutex);
679 dput(next);
680error_out2:
681 dput(dir);
682error_out:
David Howells9ae326a2009-04-03 16:42:41 +0100683 _leave(" = error %d", -ret);
684 return ret;
685}
686
687/*
688 * get a subdirectory
689 */
690struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
691 struct dentry *dir,
692 const char *dirname)
693{
694 struct dentry *subdir;
695 unsigned long start;
696 int ret;
697
698 _enter(",,%s", dirname);
699
700 /* search the current directory for the element name */
701 mutex_lock(&dir->d_inode->i_mutex);
702
703 start = jiffies;
704 subdir = lookup_one_len(dirname, dir, strlen(dirname));
705 cachefiles_hist(cachefiles_lookup_histogram, start);
706 if (IS_ERR(subdir)) {
707 if (PTR_ERR(subdir) == -ENOMEM)
708 goto nomem_d_alloc;
709 goto lookup_error;
710 }
711
712 _debug("subdir -> %p %s",
713 subdir, subdir->d_inode ? "positive" : "negative");
714
715 /* we need to create the subdir if it doesn't exist yet */
716 if (!subdir->d_inode) {
717 ret = cachefiles_has_space(cache, 1, 0);
718 if (ret < 0)
719 goto mkdir_error;
720
721 _debug("attempt mkdir");
722
723 ret = vfs_mkdir(dir->d_inode, subdir, 0700);
724 if (ret < 0)
725 goto mkdir_error;
726
727 ASSERT(subdir->d_inode);
728
729 _debug("mkdir -> %p{%p{ino=%lu}}",
730 subdir,
731 subdir->d_inode,
732 subdir->d_inode->i_ino);
733 }
734
735 mutex_unlock(&dir->d_inode->i_mutex);
736
737 /* we need to make sure the subdir is a directory */
738 ASSERT(subdir->d_inode);
739
740 if (!S_ISDIR(subdir->d_inode->i_mode)) {
741 kerror("%s is not a directory", dirname);
742 ret = -EIO;
743 goto check_error;
744 }
745
746 ret = -EPERM;
747 if (!subdir->d_inode->i_op ||
748 !subdir->d_inode->i_op->setxattr ||
749 !subdir->d_inode->i_op->getxattr ||
750 !subdir->d_inode->i_op->lookup ||
751 !subdir->d_inode->i_op->mkdir ||
752 !subdir->d_inode->i_op->create ||
753 !subdir->d_inode->i_op->rename ||
754 !subdir->d_inode->i_op->rmdir ||
755 !subdir->d_inode->i_op->unlink)
756 goto check_error;
757
758 _leave(" = [%lu]", subdir->d_inode->i_ino);
759 return subdir;
760
761check_error:
762 dput(subdir);
763 _leave(" = %d [check]", ret);
764 return ERR_PTR(ret);
765
766mkdir_error:
767 mutex_unlock(&dir->d_inode->i_mutex);
768 dput(subdir);
769 kerror("mkdir %s failed with error %d", dirname, ret);
770 return ERR_PTR(ret);
771
772lookup_error:
773 mutex_unlock(&dir->d_inode->i_mutex);
774 ret = PTR_ERR(subdir);
775 kerror("Lookup %s failed with error %d", dirname, ret);
776 return ERR_PTR(ret);
777
778nomem_d_alloc:
779 mutex_unlock(&dir->d_inode->i_mutex);
780 _leave(" = -ENOMEM");
781 return ERR_PTR(-ENOMEM);
782}
783
784/*
785 * find out if an object is in use or not
786 * - if finds object and it's not in use:
787 * - returns a pointer to the object and a reference on it
788 * - returns with the directory locked
789 */
790static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
791 struct dentry *dir,
792 char *filename)
793{
794 struct cachefiles_object *object;
795 struct rb_node *_n;
796 struct dentry *victim;
797 unsigned long start;
798 int ret;
799
800 //_enter(",%*.*s/,%s",
801 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
802
803 /* look up the victim */
804 mutex_lock_nested(&dir->d_inode->i_mutex, 1);
805
806 start = jiffies;
807 victim = lookup_one_len(filename, dir, strlen(filename));
808 cachefiles_hist(cachefiles_lookup_histogram, start);
809 if (IS_ERR(victim))
810 goto lookup_error;
811
812 //_debug("victim -> %p %s",
813 // victim, victim->d_inode ? "positive" : "negative");
814
815 /* if the object is no longer there then we probably retired the object
816 * at the netfs's request whilst the cull was in progress
817 */
818 if (!victim->d_inode) {
819 mutex_unlock(&dir->d_inode->i_mutex);
820 dput(victim);
821 _leave(" = -ENOENT [absent]");
822 return ERR_PTR(-ENOENT);
823 }
824
825 /* check to see if we're using this object */
826 read_lock(&cache->active_lock);
827
828 _n = cache->active_nodes.rb_node;
829
830 while (_n) {
831 object = rb_entry(_n, struct cachefiles_object, active_node);
832
833 if (object->dentry > victim)
834 _n = _n->rb_left;
835 else if (object->dentry < victim)
836 _n = _n->rb_right;
837 else
838 goto object_in_use;
839 }
840
841 read_unlock(&cache->active_lock);
842
843 //_leave(" = %p", victim);
844 return victim;
845
846object_in_use:
847 read_unlock(&cache->active_lock);
848 mutex_unlock(&dir->d_inode->i_mutex);
849 dput(victim);
850 //_leave(" = -EBUSY [in use]");
851 return ERR_PTR(-EBUSY);
852
853lookup_error:
854 mutex_unlock(&dir->d_inode->i_mutex);
855 ret = PTR_ERR(victim);
856 if (ret == -ENOENT) {
857 /* file or dir now absent - probably retired by netfs */
858 _leave(" = -ESTALE [absent]");
859 return ERR_PTR(-ESTALE);
860 }
861
862 if (ret == -EIO) {
863 cachefiles_io_error(cache, "Lookup failed");
864 } else if (ret != -ENOMEM) {
865 kerror("Internal error: %d", ret);
866 ret = -EIO;
867 }
868
869 _leave(" = %d", ret);
870 return ERR_PTR(ret);
871}
872
873/*
874 * cull an object if it's not in use
875 * - called only by cache manager daemon
876 */
877int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
878 char *filename)
879{
880 struct dentry *victim;
881 int ret;
882
883 _enter(",%*.*s/,%s",
884 dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
885
886 victim = cachefiles_check_active(cache, dir, filename);
887 if (IS_ERR(victim))
888 return PTR_ERR(victim);
889
890 _debug("victim -> %p %s",
891 victim, victim->d_inode ? "positive" : "negative");
892
893 /* okay... the victim is not being used so we can cull it
894 * - start by marking it as stale
895 */
896 _debug("victim is cullable");
897
898 ret = cachefiles_remove_object_xattr(cache, victim);
899 if (ret < 0)
900 goto error_unlock;
901
902 /* actually remove the victim (drops the dir mutex) */
903 _debug("bury");
904
David Howellsc61ea312010-05-11 16:51:39 +0100905 ret = cachefiles_bury_object(cache, dir, victim, false);
David Howells9ae326a2009-04-03 16:42:41 +0100906 if (ret < 0)
907 goto error;
908
909 dput(victim);
910 _leave(" = 0");
911 return 0;
912
913error_unlock:
914 mutex_unlock(&dir->d_inode->i_mutex);
915error:
916 dput(victim);
917 if (ret == -ENOENT) {
918 /* file or dir now absent - probably retired by netfs */
919 _leave(" = -ESTALE [absent]");
920 return -ESTALE;
921 }
922
923 if (ret != -ENOMEM) {
924 kerror("Internal error: %d", ret);
925 ret = -EIO;
926 }
927
928 _leave(" = %d", ret);
929 return ret;
930}
931
932/*
933 * find out if an object is in use or not
934 * - called only by cache manager daemon
935 * - returns -EBUSY or 0 to indicate whether an object is in use or not
936 */
937int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
938 char *filename)
939{
940 struct dentry *victim;
941
942 //_enter(",%*.*s/,%s",
943 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
944
945 victim = cachefiles_check_active(cache, dir, filename);
946 if (IS_ERR(victim))
947 return PTR_ERR(victim);
948
949 mutex_unlock(&dir->d_inode->i_mutex);
950 dput(victim);
951 //_leave(" = 0");
952 return 0;
953}