1 /* CacheFiles path walking and related routines
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/file.h>
16 #include <linux/fsnotify.h>
17 #include <linux/quotaops.h>
18 #include <linux/xattr.h>
19 #include <linux/mount.h>
20 #include <linux/namei.h>
21 #include <linux/security.h>
22 #include <linux/slab.h>
25 #define CACHEFILES_KEYBUF_SIZE 512
28 * dump debugging info about an object
31 void __cachefiles_printk_object(struct cachefiles_object
*object
,
35 struct fscache_cookie
*cookie
;
36 unsigned keylen
, loop
;
38 printk(KERN_ERR
"%sobject: OBJ%x\n",
39 prefix
, object
->fscache
.debug_id
);
40 printk(KERN_ERR
"%sobjstate=%s fl=%lx 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
);
51 spin_lock(&object
->fscache
.lock
);
52 cookie
= object
->fscache
.cookie
;
54 printk(KERN_ERR
"%scookie=%p [pr=%p nd=%p fl=%lx]\n",
56 object
->fscache
.cookie
,
57 object
->fscache
.cookie
->parent
,
58 object
->fscache
.cookie
->netfs_data
,
59 object
->fscache
.cookie
->flags
);
61 keylen
= cookie
->def
->get_key(cookie
->netfs_data
, keybuf
,
62 CACHEFILES_KEYBUF_SIZE
);
66 printk(KERN_ERR
"%scookie=NULL\n", prefix
);
69 spin_unlock(&object
->fscache
.lock
);
72 printk(KERN_ERR
"%skey=[%u] '", prefix
, keylen
);
73 for (loop
= 0; loop
< keylen
; loop
++)
74 printk("%02x", keybuf
[loop
]);
80 * dump debugging info about a pair of objects
82 static noinline
void cachefiles_printk_object(struct cachefiles_object
*object
,
83 struct cachefiles_object
*xobject
)
87 keybuf
= kmalloc(CACHEFILES_KEYBUF_SIZE
, GFP_NOIO
);
89 __cachefiles_printk_object(object
, "", keybuf
);
91 __cachefiles_printk_object(xobject
, "x", keybuf
);
96 * record the fact that an object is now active
98 static int cachefiles_mark_object_active(struct cachefiles_cache
*cache
,
99 struct cachefiles_object
*object
)
101 struct cachefiles_object
*xobject
;
102 struct rb_node
**_p
, *_parent
= NULL
;
103 struct dentry
*dentry
;
105 _enter(",%p", object
);
108 write_lock(&cache
->active_lock
);
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
);
116 dentry
= object
->dentry
;
117 _p
= &cache
->active_nodes
.rb_node
;
120 xobject
= rb_entry(_parent
,
121 struct cachefiles_object
, active_node
);
123 ASSERT(xobject
!= object
);
125 if (xobject
->dentry
> dentry
)
126 _p
= &(*_p
)->rb_left
;
127 else if (xobject
->dentry
< dentry
)
128 _p
= &(*_p
)->rb_right
;
130 goto wait_for_old_object
;
133 rb_link_node(&object
->active_node
, _parent
, _p
);
134 rb_insert_color(&object
->active_node
, &cache
->active_nodes
);
136 write_unlock(&cache
->active_lock
);
140 /* an old object from a previous incarnation is hogging the slot - we
141 * need to wait for it to be destroyed */
143 if (xobject
->fscache
.state
< FSCACHE_OBJECT_DYING
) {
144 printk(KERN_ERR
"\n");
145 printk(KERN_ERR
"CacheFiles: Error:"
146 " Unexpected object collision\n");
147 cachefiles_printk_object(object
, xobject
);
150 atomic_inc(&xobject
->usage
);
151 write_unlock(&cache
->active_lock
);
153 if (test_bit(CACHEFILES_OBJECT_ACTIVE
, &xobject
->flags
)) {
154 wait_queue_head_t
*wq
;
156 signed long timeout
= 60 * HZ
;
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
);
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
172 wq
= bit_waitqueue(&xobject
->flags
, CACHEFILES_OBJECT_ACTIVE
);
176 prepare_to_wait(wq
, &wait
, TASK_UNINTERRUPTIBLE
);
177 if (!test_bit(CACHEFILES_OBJECT_ACTIVE
, &xobject
->flags
))
179 requeue
= slow_work_sleep_till_thread_needed(
180 &object
->fscache
.work
, &timeout
);
181 } while (timeout
> 0 && !requeue
);
182 finish_wait(wq
, &wait
);
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
);
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
);
201 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE
, &xobject
->flags
));
203 cache
->cache
.ops
->put_object(&xobject
->fscache
);
207 clear_bit(CACHEFILES_OBJECT_ACTIVE
, &object
->flags
);
208 cache
->cache
.ops
->put_object(&xobject
->fscache
);
209 _leave(" = -ETIMEDOUT");
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
218 * - unlocks the directory mutex
220 static int cachefiles_bury_object(struct cachefiles_cache
*cache
,
224 struct dentry
*grave
, *trap
;
225 char nbuffer
[8 + 8 + 1];
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
);
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
);
237 mutex_unlock(&dir
->d_inode
->i_mutex
);
240 cachefiles_io_error(cache
, "Unlink failed");
242 _leave(" = %d", ret
);
246 /* directories have to be moved to the graveyard */
247 _debug("move stale object to graveyard");
248 mutex_unlock(&dir
->d_inode
->i_mutex
);
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
));
256 /* do the multiway lock magic */
257 trap
= lock_rename(cache
->graveyard
, dir
);
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
263 unlock_rename(cache
->graveyard
, dir
);
264 _leave(" = 0 [culled?]");
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");
275 unlock_rename(cache
->graveyard
, dir
);
276 cachefiles_io_error(cache
, "May not make directory loop");
280 if (d_mountpoint(rep
)) {
281 unlock_rename(cache
->graveyard
, dir
);
282 cachefiles_io_error(cache
, "Mountpoint in cache");
286 grave
= lookup_one_len(nbuffer
, cache
->graveyard
, strlen(nbuffer
));
288 unlock_rename(cache
->graveyard
, dir
);
290 if (PTR_ERR(grave
) == -ENOMEM
) {
291 _leave(" = -ENOMEM");
295 cachefiles_io_error(cache
, "Lookup error %ld",
300 if (grave
->d_inode
) {
301 unlock_rename(cache
->graveyard
, dir
);
308 if (d_mountpoint(grave
)) {
309 unlock_rename(cache
->graveyard
, dir
);
311 cachefiles_io_error(cache
, "Mountpoint in graveyard");
315 /* target should not be an ancestor of source */
317 unlock_rename(cache
->graveyard
, dir
);
319 cachefiles_io_error(cache
, "May not make directory loop");
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
);
328 unlock_rename(cache
->graveyard
, dir
);
335 * delete an object representation from the cache
337 int cachefiles_delete_object(struct cachefiles_cache
*cache
,
338 struct cachefiles_object
*object
)
343 _enter(",{%p}", object
->dentry
);
345 ASSERT(object
->dentry
);
346 ASSERT(object
->dentry
->d_inode
);
347 ASSERT(object
->dentry
->d_parent
);
349 dir
= dget_parent(object
->dentry
);
351 mutex_lock_nested(&dir
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
353 /* we need to check that our parent is _still_ our parent - it may have
355 if (dir
== object
->dentry
->d_parent
) {
356 ret
= cachefiles_bury_object(cache
, dir
, object
->dentry
);
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
);
365 _leave(" = %d", ret
);
370 * walk from the parent object to the child object through the backing
371 * filesystem, creating directories as we go
373 int cachefiles_walk_to_object(struct cachefiles_object
*parent
,
374 struct cachefiles_object
*object
,
376 struct cachefiles_xattr
*auxdata
)
378 struct cachefiles_cache
*cache
;
379 struct dentry
*dir
, *next
= NULL
;
384 _enter("{%p},,%s,", parent
->dentry
, key
);
386 cache
= container_of(parent
->fscache
.cache
,
387 struct cachefiles_cache
, cache
);
389 ASSERT(parent
->dentry
);
390 ASSERT(parent
->dentry
->d_inode
);
392 if (!(S_ISDIR(parent
->dentry
->d_inode
->i_mode
))) {
393 // TODO: convert file to dir
394 _leave("looking up in none directory");
398 dir
= dget(parent
->dentry
);
401 /* attempt to transit the first directory component */
405 /* key ends in a double NUL */
406 key
= key
+ nlen
+ 1;
411 /* search the current directory for the element name */
412 _debug("lookup '%s'", name
);
414 mutex_lock_nested(&dir
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
417 next
= lookup_one_len(name
, dir
, nlen
);
418 cachefiles_hist(cachefiles_lookup_histogram
, start
);
422 _debug("next -> %p %s", next
, next
->d_inode
? "positive" : "negative");
425 object
->new = !next
->d_inode
;
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 */
431 fscache_object_lookup_negative(&object
->fscache
);
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);
442 ret
= vfs_mkdir(dir
->d_inode
, next
, 0);
443 cachefiles_hist(cachefiles_mkdir_histogram
, start
);
447 ASSERT(next
->d_inode
);
449 _debug("mkdir -> %p{%p{ino=%lu}}",
450 next
, next
->d_inode
, next
->d_inode
->i_ino
);
452 } else if (!S_ISDIR(next
->d_inode
->i_mode
)) {
453 kerror("inode %lu is not a directory",
454 next
->d_inode
->i_ino
);
460 /* non-index objects start out life as files */
461 if (!next
->d_inode
) {
462 ret
= cachefiles_has_space(cache
, 1, 0);
467 ret
= vfs_create(dir
->d_inode
, next
, S_IFREG
, NULL
);
468 cachefiles_hist(cachefiles_create_histogram
, start
);
472 ASSERT(next
->d_inode
);
474 _debug("create -> %p{%p{ino=%lu}}",
475 next
, next
->d_inode
, next
->d_inode
->i_ino
);
477 } else if (!S_ISDIR(next
->d_inode
->i_mode
) &&
478 !S_ISREG(next
->d_inode
->i_mode
)
480 kerror("inode %lu is not a file or directory",
481 next
->d_inode
->i_ino
);
487 /* process the next component */
490 mutex_unlock(&dir
->d_inode
->i_mutex
);
497 /* we've found the object we were looking for */
498 object
->dentry
= next
;
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 */
503 _debug("validate '%*.*s'",
504 next
->d_name
.len
, next
->d_name
.len
, next
->d_name
.name
);
506 ret
= cachefiles_check_object_xattr(object
, auxdata
);
507 if (ret
== -ESTALE
) {
508 /* delete the object (the deleter drops the directory
510 object
->dentry
= NULL
;
512 ret
= cachefiles_bury_object(cache
, dir
, next
);
519 _debug("redo lookup");
524 /* note that we're now using this object */
525 ret
= cachefiles_mark_object_active(cache
, object
);
527 mutex_unlock(&dir
->d_inode
->i_mutex
);
531 if (ret
== -ETIMEDOUT
)
532 goto mark_active_timed_out
;
534 _debug("=== OBTAINED_OBJECT ===");
537 /* attach data to a newly constructed terminal object */
538 ret
= cachefiles_set_object_xattr(object
, auxdata
);
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
546 touch_atime(cache
->mnt
, next
);
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
;
555 aops
= object
->dentry
->d_inode
->i_mapping
->a_ops
;
559 object
->backer
= object
->dentry
;
561 BUG(); // TODO: open file in data-class subdir
566 fscache_obtained_object(&object
->fscache
);
568 _leave(" = 0 [%lu]", object
->dentry
->d_inode
->i_ino
);
572 _debug("create error %d", ret
);
574 cachefiles_io_error(cache
, "Create/mkdir failed");
577 mark_active_timed_out
:
578 _debug("mark active timed out");
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
);
589 dput(object
->dentry
);
590 object
->dentry
= NULL
;
594 _debug("delete error %d", ret
);
598 _debug("lookup error %ld", PTR_ERR(next
));
601 cachefiles_io_error(cache
, "Lookup failed");
604 mutex_unlock(&dir
->d_inode
->i_mutex
);
609 _leave(" = error %d", -ret
);
616 struct dentry
*cachefiles_get_directory(struct cachefiles_cache
*cache
,
620 struct dentry
*subdir
;
624 _enter(",,%s", dirname
);
626 /* search the current directory for the element name */
627 mutex_lock(&dir
->d_inode
->i_mutex
);
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
)
638 _debug("subdir -> %p %s",
639 subdir
, subdir
->d_inode
? "positive" : "negative");
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);
647 _debug("attempt mkdir");
649 ret
= vfs_mkdir(dir
->d_inode
, subdir
, 0700);
653 ASSERT(subdir
->d_inode
);
655 _debug("mkdir -> %p{%p{ino=%lu}}",
658 subdir
->d_inode
->i_ino
);
661 mutex_unlock(&dir
->d_inode
->i_mutex
);
663 /* we need to make sure the subdir is a directory */
664 ASSERT(subdir
->d_inode
);
666 if (!S_ISDIR(subdir
->d_inode
->i_mode
)) {
667 kerror("%s is not a directory", dirname
);
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
)
684 _leave(" = [%lu]", subdir
->d_inode
->i_ino
);
689 _leave(" = %d [check]", ret
);
693 mutex_unlock(&dir
->d_inode
->i_mutex
);
695 kerror("mkdir %s failed with error %d", dirname
, ret
);
699 mutex_unlock(&dir
->d_inode
->i_mutex
);
700 ret
= PTR_ERR(subdir
);
701 kerror("Lookup %s failed with error %d", dirname
, ret
);
705 mutex_unlock(&dir
->d_inode
->i_mutex
);
706 _leave(" = -ENOMEM");
707 return ERR_PTR(-ENOMEM
);
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
716 static struct dentry
*cachefiles_check_active(struct cachefiles_cache
*cache
,
720 struct cachefiles_object
*object
;
722 struct dentry
*victim
;
726 //_enter(",%*.*s/,%s",
727 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
729 /* look up the victim */
730 mutex_lock_nested(&dir
->d_inode
->i_mutex
, 1);
733 victim
= lookup_one_len(filename
, dir
, strlen(filename
));
734 cachefiles_hist(cachefiles_lookup_histogram
, start
);
738 //_debug("victim -> %p %s",
739 // victim, victim->d_inode ? "positive" : "negative");
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
744 if (!victim
->d_inode
) {
745 mutex_unlock(&dir
->d_inode
->i_mutex
);
747 _leave(" = -ENOENT [absent]");
748 return ERR_PTR(-ENOENT
);
751 /* check to see if we're using this object */
752 read_lock(&cache
->active_lock
);
754 _n
= cache
->active_nodes
.rb_node
;
757 object
= rb_entry(_n
, struct cachefiles_object
, active_node
);
759 if (object
->dentry
> victim
)
761 else if (object
->dentry
< victim
)
767 read_unlock(&cache
->active_lock
);
769 //_leave(" = %p", victim);
773 read_unlock(&cache
->active_lock
);
774 mutex_unlock(&dir
->d_inode
->i_mutex
);
776 //_leave(" = -EBUSY [in use]");
777 return ERR_PTR(-EBUSY
);
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
);
789 cachefiles_io_error(cache
, "Lookup failed");
790 } else if (ret
!= -ENOMEM
) {
791 kerror("Internal error: %d", ret
);
795 _leave(" = %d", ret
);
800 * cull an object if it's not in use
801 * - called only by cache manager daemon
803 int cachefiles_cull(struct cachefiles_cache
*cache
, struct dentry
*dir
,
806 struct dentry
*victim
;
810 dir
->d_name
.len
, dir
->d_name
.len
, dir
->d_name
.name
, filename
);
812 victim
= cachefiles_check_active(cache
, dir
, filename
);
814 return PTR_ERR(victim
);
816 _debug("victim -> %p %s",
817 victim
, victim
->d_inode
? "positive" : "negative");
819 /* okay... the victim is not being used so we can cull it
820 * - start by marking it as stale
822 _debug("victim is cullable");
824 ret
= cachefiles_remove_object_xattr(cache
, victim
);
828 /* actually remove the victim (drops the dir mutex) */
831 ret
= cachefiles_bury_object(cache
, dir
, victim
);
840 mutex_unlock(&dir
->d_inode
->i_mutex
);
843 if (ret
== -ENOENT
) {
844 /* file or dir now absent - probably retired by netfs */
845 _leave(" = -ESTALE [absent]");
849 if (ret
!= -ENOMEM
) {
850 kerror("Internal error: %d", ret
);
854 _leave(" = %d", ret
);
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
863 int cachefiles_check_in_use(struct cachefiles_cache
*cache
, struct dentry
*dir
,
866 struct dentry
*victim
;
868 //_enter(",%*.*s/,%s",
869 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
871 victim
= cachefiles_check_active(cache
, dir
, filename
);
873 return PTR_ERR(victim
);
875 mutex_unlock(&dir
->d_inode
->i_mutex
);