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>
24 #define CACHEFILES_KEYBUF_SIZE 512
27 * dump debugging info about an object
30 void __cachefiles_printk_object(struct cachefiles_object
*object
,
34 struct fscache_cookie
*cookie
;
35 unsigned keylen
, loop
;
37 printk(KERN_ERR
"%sobject: OBJ%x\n",
38 prefix
, object
->fscache
.debug_id
);
39 printk(KERN_ERR
"%sobjstate=%s fl=%lx swfl=%lx ev=%lx[%lx]\n",
40 prefix
, fscache_object_states
[object
->fscache
.state
],
41 object
->fscache
.flags
, object
->fscache
.work
.flags
,
42 object
->fscache
.events
,
43 object
->fscache
.event_mask
& FSCACHE_OBJECT_EVENTS_MASK
);
44 printk(KERN_ERR
"%sops=%u inp=%u exc=%u\n",
45 prefix
, object
->fscache
.n_ops
, object
->fscache
.n_in_progress
,
46 object
->fscache
.n_exclusive
);
47 printk(KERN_ERR
"%sparent=%p\n",
48 prefix
, object
->fscache
.parent
);
50 spin_lock(&object
->fscache
.lock
);
51 cookie
= object
->fscache
.cookie
;
53 printk(KERN_ERR
"%scookie=%p [pr=%p nd=%p fl=%lx]\n",
55 object
->fscache
.cookie
,
56 object
->fscache
.cookie
->parent
,
57 object
->fscache
.cookie
->netfs_data
,
58 object
->fscache
.cookie
->flags
);
60 keylen
= cookie
->def
->get_key(cookie
->netfs_data
, keybuf
,
61 CACHEFILES_KEYBUF_SIZE
);
65 printk(KERN_ERR
"%scookie=NULL\n", prefix
);
68 spin_unlock(&object
->fscache
.lock
);
71 printk(KERN_ERR
"%skey=[%u] '", prefix
, keylen
);
72 for (loop
= 0; loop
< keylen
; loop
++)
73 printk("%02x", keybuf
[loop
]);
79 * dump debugging info about a pair of objects
81 static noinline
void cachefiles_printk_object(struct cachefiles_object
*object
,
82 struct cachefiles_object
*xobject
)
86 keybuf
= kmalloc(CACHEFILES_KEYBUF_SIZE
, GFP_NOIO
);
88 __cachefiles_printk_object(object
, "", keybuf
);
90 __cachefiles_printk_object(xobject
, "x", keybuf
);
95 * record the fact that an object is now active
97 static int cachefiles_mark_object_active(struct cachefiles_cache
*cache
,
98 struct cachefiles_object
*object
)
100 struct cachefiles_object
*xobject
;
101 struct rb_node
**_p
, *_parent
= NULL
;
102 struct dentry
*dentry
;
104 _enter(",%p", object
);
107 write_lock(&cache
->active_lock
);
109 if (test_and_set_bit(CACHEFILES_OBJECT_ACTIVE
, &object
->flags
)) {
110 printk(KERN_ERR
"CacheFiles: Error: Object already active\n");
111 cachefiles_printk_object(object
, NULL
);
115 dentry
= object
->dentry
;
116 _p
= &cache
->active_nodes
.rb_node
;
119 xobject
= rb_entry(_parent
,
120 struct cachefiles_object
, active_node
);
122 ASSERT(xobject
!= object
);
124 if (xobject
->dentry
> dentry
)
125 _p
= &(*_p
)->rb_left
;
126 else if (xobject
->dentry
< dentry
)
127 _p
= &(*_p
)->rb_right
;
129 goto wait_for_old_object
;
132 rb_link_node(&object
->active_node
, _parent
, _p
);
133 rb_insert_color(&object
->active_node
, &cache
->active_nodes
);
135 write_unlock(&cache
->active_lock
);
139 /* an old object from a previous incarnation is hogging the slot - we
140 * need to wait for it to be destroyed */
142 if (xobject
->fscache
.state
< FSCACHE_OBJECT_DYING
) {
143 printk(KERN_ERR
"\n");
144 printk(KERN_ERR
"CacheFiles: Error:"
145 " Unexpected object collision\n");
146 cachefiles_printk_object(object
, xobject
);
149 atomic_inc(&xobject
->usage
);
150 write_unlock(&cache
->active_lock
);
152 if (test_bit(CACHEFILES_OBJECT_ACTIVE
, &xobject
->flags
)) {
153 wait_queue_head_t
*wq
;
155 signed long timeout
= 60 * HZ
;
159 /* if the object we're waiting for is queued for processing,
160 * then just put ourselves on the queue behind it */
161 if (slow_work_is_queued(&xobject
->fscache
.work
)) {
162 _debug("queue OBJ%x behind OBJ%x immediately",
163 object
->fscache
.debug_id
,
164 xobject
->fscache
.debug_id
);
168 /* otherwise we sleep until either the object we're waiting for
169 * is done, or the slow-work facility wants the thread back to
171 wq
= bit_waitqueue(&xobject
->flags
, CACHEFILES_OBJECT_ACTIVE
);
175 prepare_to_wait(wq
, &wait
, TASK_UNINTERRUPTIBLE
);
176 if (!test_bit(CACHEFILES_OBJECT_ACTIVE
, &xobject
->flags
))
178 requeue
= slow_work_sleep_till_thread_needed(
179 &object
->fscache
.work
, &timeout
);
180 } while (timeout
> 0 && !requeue
);
181 finish_wait(wq
, &wait
);
184 test_bit(CACHEFILES_OBJECT_ACTIVE
, &xobject
->flags
)) {
185 _debug("queue OBJ%x behind OBJ%x after wait",
186 object
->fscache
.debug_id
,
187 xobject
->fscache
.debug_id
);
192 printk(KERN_ERR
"\n");
193 printk(KERN_ERR
"CacheFiles: Error: Overlong"
194 " wait for old active object to go away\n");
195 cachefiles_printk_object(object
, xobject
);
200 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE
, &xobject
->flags
));
202 cache
->cache
.ops
->put_object(&xobject
->fscache
);
206 clear_bit(CACHEFILES_OBJECT_ACTIVE
, &object
->flags
);
207 cache
->cache
.ops
->put_object(&xobject
->fscache
);
208 _leave(" = -ETIMEDOUT");
213 * delete an object representation from the cache
214 * - file backed objects are unlinked
215 * - directory backed objects are stuffed into the graveyard for userspace to
217 * - unlocks the directory mutex
219 static int cachefiles_bury_object(struct cachefiles_cache
*cache
,
223 struct dentry
*grave
, *trap
;
224 char nbuffer
[8 + 8 + 1];
227 _enter(",'%*.*s','%*.*s'",
228 dir
->d_name
.len
, dir
->d_name
.len
, dir
->d_name
.name
,
229 rep
->d_name
.len
, rep
->d_name
.len
, rep
->d_name
.name
);
231 /* non-directories can just be unlinked */
232 if (!S_ISDIR(rep
->d_inode
->i_mode
)) {
233 _debug("unlink stale object");
234 ret
= vfs_unlink(dir
->d_inode
, rep
);
236 mutex_unlock(&dir
->d_inode
->i_mutex
);
239 cachefiles_io_error(cache
, "Unlink failed");
241 _leave(" = %d", ret
);
245 /* directories have to be moved to the graveyard */
246 _debug("move stale object to graveyard");
247 mutex_unlock(&dir
->d_inode
->i_mutex
);
250 /* first step is to make up a grave dentry in the graveyard */
251 sprintf(nbuffer
, "%08x%08x",
252 (uint32_t) get_seconds(),
253 (uint32_t) atomic_inc_return(&cache
->gravecounter
));
255 /* do the multiway lock magic */
256 trap
= lock_rename(cache
->graveyard
, dir
);
258 /* do some checks before getting the grave dentry */
259 if (rep
->d_parent
!= dir
) {
260 /* the entry was probably culled when we dropped the parent dir
262 unlock_rename(cache
->graveyard
, dir
);
263 _leave(" = 0 [culled?]");
267 if (!S_ISDIR(cache
->graveyard
->d_inode
->i_mode
)) {
268 unlock_rename(cache
->graveyard
, dir
);
269 cachefiles_io_error(cache
, "Graveyard no longer a directory");
274 unlock_rename(cache
->graveyard
, dir
);
275 cachefiles_io_error(cache
, "May not make directory loop");
279 if (d_mountpoint(rep
)) {
280 unlock_rename(cache
->graveyard
, dir
);
281 cachefiles_io_error(cache
, "Mountpoint in cache");
285 grave
= lookup_one_len(nbuffer
, cache
->graveyard
, strlen(nbuffer
));
287 unlock_rename(cache
->graveyard
, dir
);
289 if (PTR_ERR(grave
) == -ENOMEM
) {
290 _leave(" = -ENOMEM");
294 cachefiles_io_error(cache
, "Lookup error %ld",
299 if (grave
->d_inode
) {
300 unlock_rename(cache
->graveyard
, dir
);
307 if (d_mountpoint(grave
)) {
308 unlock_rename(cache
->graveyard
, dir
);
310 cachefiles_io_error(cache
, "Mountpoint in graveyard");
314 /* target should not be an ancestor of source */
316 unlock_rename(cache
->graveyard
, dir
);
318 cachefiles_io_error(cache
, "May not make directory loop");
322 /* attempt the rename */
323 ret
= vfs_rename(dir
->d_inode
, rep
, cache
->graveyard
->d_inode
, grave
);
324 if (ret
!= 0 && ret
!= -ENOMEM
)
325 cachefiles_io_error(cache
, "Rename failed with error %d", ret
);
327 unlock_rename(cache
->graveyard
, dir
);
334 * delete an object representation from the cache
336 int cachefiles_delete_object(struct cachefiles_cache
*cache
,
337 struct cachefiles_object
*object
)
342 _enter(",{%p}", object
->dentry
);
344 ASSERT(object
->dentry
);
345 ASSERT(object
->dentry
->d_inode
);
346 ASSERT(object
->dentry
->d_parent
);
348 dir
= dget_parent(object
->dentry
);
350 mutex_lock_nested(&dir
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
351 ret
= cachefiles_bury_object(cache
, dir
, object
->dentry
);
354 _leave(" = %d", ret
);
359 * walk from the parent object to the child object through the backing
360 * filesystem, creating directories as we go
362 int cachefiles_walk_to_object(struct cachefiles_object
*parent
,
363 struct cachefiles_object
*object
,
365 struct cachefiles_xattr
*auxdata
)
367 struct cachefiles_cache
*cache
;
368 struct dentry
*dir
, *next
= NULL
;
373 _enter("{%p},,%s,", parent
->dentry
, key
);
375 cache
= container_of(parent
->fscache
.cache
,
376 struct cachefiles_cache
, cache
);
378 ASSERT(parent
->dentry
);
379 ASSERT(parent
->dentry
->d_inode
);
381 if (!(S_ISDIR(parent
->dentry
->d_inode
->i_mode
))) {
382 // TODO: convert file to dir
383 _leave("looking up in none directory");
387 dir
= dget(parent
->dentry
);
390 /* attempt to transit the first directory component */
394 /* key ends in a double NUL */
395 key
= key
+ nlen
+ 1;
400 /* search the current directory for the element name */
401 _debug("lookup '%s'", name
);
403 mutex_lock_nested(&dir
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
406 next
= lookup_one_len(name
, dir
, nlen
);
407 cachefiles_hist(cachefiles_lookup_histogram
, start
);
411 _debug("next -> %p %s", next
, next
->d_inode
? "positive" : "negative");
414 object
->new = !next
->d_inode
;
416 /* if this element of the path doesn't exist, then the lookup phase
417 * failed, and we can release any readers in the certain knowledge that
418 * there's nothing for them to actually read */
420 fscache_object_lookup_negative(&object
->fscache
);
422 /* we need to create the object if it's negative */
423 if (key
|| object
->type
== FSCACHE_COOKIE_TYPE_INDEX
) {
424 /* index objects and intervening tree levels must be subdirs */
425 if (!next
->d_inode
) {
426 ret
= cachefiles_has_space(cache
, 1, 0);
431 ret
= vfs_mkdir(dir
->d_inode
, next
, 0);
432 cachefiles_hist(cachefiles_mkdir_histogram
, start
);
436 ASSERT(next
->d_inode
);
438 _debug("mkdir -> %p{%p{ino=%lu}}",
439 next
, next
->d_inode
, next
->d_inode
->i_ino
);
441 } else if (!S_ISDIR(next
->d_inode
->i_mode
)) {
442 kerror("inode %lu is not a directory",
443 next
->d_inode
->i_ino
);
449 /* non-index objects start out life as files */
450 if (!next
->d_inode
) {
451 ret
= cachefiles_has_space(cache
, 1, 0);
456 ret
= vfs_create(dir
->d_inode
, next
, S_IFREG
, NULL
);
457 cachefiles_hist(cachefiles_create_histogram
, start
);
461 ASSERT(next
->d_inode
);
463 _debug("create -> %p{%p{ino=%lu}}",
464 next
, next
->d_inode
, next
->d_inode
->i_ino
);
466 } else if (!S_ISDIR(next
->d_inode
->i_mode
) &&
467 !S_ISREG(next
->d_inode
->i_mode
)
469 kerror("inode %lu is not a file or directory",
470 next
->d_inode
->i_ino
);
476 /* process the next component */
479 mutex_unlock(&dir
->d_inode
->i_mutex
);
486 /* we've found the object we were looking for */
487 object
->dentry
= next
;
489 /* if we've found that the terminal object exists, then we need to
490 * check its attributes and delete it if it's out of date */
492 _debug("validate '%*.*s'",
493 next
->d_name
.len
, next
->d_name
.len
, next
->d_name
.name
);
495 ret
= cachefiles_check_object_xattr(object
, auxdata
);
496 if (ret
== -ESTALE
) {
497 /* delete the object (the deleter drops the directory
499 object
->dentry
= NULL
;
501 ret
= cachefiles_bury_object(cache
, dir
, next
);
508 _debug("redo lookup");
513 /* note that we're now using this object */
514 ret
= cachefiles_mark_object_active(cache
, object
);
516 mutex_unlock(&dir
->d_inode
->i_mutex
);
520 if (ret
== -ETIMEDOUT
)
521 goto mark_active_timed_out
;
523 _debug("=== OBTAINED_OBJECT ===");
526 /* attach data to a newly constructed terminal object */
527 ret
= cachefiles_set_object_xattr(object
, auxdata
);
531 /* always update the atime on an object we've just looked up
532 * (this is used to keep track of culling, and atimes are only
533 * updated by read, write and readdir but not lookup or
535 touch_atime(cache
->mnt
, next
);
538 /* open a file interface onto a data file */
539 if (object
->type
!= FSCACHE_COOKIE_TYPE_INDEX
) {
540 if (S_ISREG(object
->dentry
->d_inode
->i_mode
)) {
541 const struct address_space_operations
*aops
;
544 aops
= object
->dentry
->d_inode
->i_mapping
->a_ops
;
548 object
->backer
= object
->dentry
;
550 BUG(); // TODO: open file in data-class subdir
555 fscache_obtained_object(&object
->fscache
);
557 _leave(" = 0 [%lu]", object
->dentry
->d_inode
->i_ino
);
561 _debug("create error %d", ret
);
563 cachefiles_io_error(cache
, "Create/mkdir failed");
566 mark_active_timed_out
:
567 _debug("mark active timed out");
571 _debug("check error %d", ret
);
572 write_lock(&cache
->active_lock
);
573 rb_erase(&object
->active_node
, &cache
->active_nodes
);
574 clear_bit(CACHEFILES_OBJECT_ACTIVE
, &object
->flags
);
575 wake_up_bit(&object
->flags
, CACHEFILES_OBJECT_ACTIVE
);
576 write_unlock(&cache
->active_lock
);
578 dput(object
->dentry
);
579 object
->dentry
= NULL
;
583 _debug("delete error %d", ret
);
587 _debug("lookup error %ld", PTR_ERR(next
));
590 cachefiles_io_error(cache
, "Lookup failed");
593 mutex_unlock(&dir
->d_inode
->i_mutex
);
598 _leave(" = error %d", -ret
);
605 struct dentry
*cachefiles_get_directory(struct cachefiles_cache
*cache
,
609 struct dentry
*subdir
;
613 _enter(",,%s", dirname
);
615 /* search the current directory for the element name */
616 mutex_lock(&dir
->d_inode
->i_mutex
);
619 subdir
= lookup_one_len(dirname
, dir
, strlen(dirname
));
620 cachefiles_hist(cachefiles_lookup_histogram
, start
);
621 if (IS_ERR(subdir
)) {
622 if (PTR_ERR(subdir
) == -ENOMEM
)
627 _debug("subdir -> %p %s",
628 subdir
, subdir
->d_inode
? "positive" : "negative");
630 /* we need to create the subdir if it doesn't exist yet */
631 if (!subdir
->d_inode
) {
632 ret
= cachefiles_has_space(cache
, 1, 0);
636 _debug("attempt mkdir");
638 ret
= vfs_mkdir(dir
->d_inode
, subdir
, 0700);
642 ASSERT(subdir
->d_inode
);
644 _debug("mkdir -> %p{%p{ino=%lu}}",
647 subdir
->d_inode
->i_ino
);
650 mutex_unlock(&dir
->d_inode
->i_mutex
);
652 /* we need to make sure the subdir is a directory */
653 ASSERT(subdir
->d_inode
);
655 if (!S_ISDIR(subdir
->d_inode
->i_mode
)) {
656 kerror("%s is not a directory", dirname
);
662 if (!subdir
->d_inode
->i_op
||
663 !subdir
->d_inode
->i_op
->setxattr
||
664 !subdir
->d_inode
->i_op
->getxattr
||
665 !subdir
->d_inode
->i_op
->lookup
||
666 !subdir
->d_inode
->i_op
->mkdir
||
667 !subdir
->d_inode
->i_op
->create
||
668 !subdir
->d_inode
->i_op
->rename
||
669 !subdir
->d_inode
->i_op
->rmdir
||
670 !subdir
->d_inode
->i_op
->unlink
)
673 _leave(" = [%lu]", subdir
->d_inode
->i_ino
);
678 _leave(" = %d [check]", ret
);
682 mutex_unlock(&dir
->d_inode
->i_mutex
);
684 kerror("mkdir %s failed with error %d", dirname
, ret
);
688 mutex_unlock(&dir
->d_inode
->i_mutex
);
689 ret
= PTR_ERR(subdir
);
690 kerror("Lookup %s failed with error %d", dirname
, ret
);
694 mutex_unlock(&dir
->d_inode
->i_mutex
);
695 _leave(" = -ENOMEM");
696 return ERR_PTR(-ENOMEM
);
700 * find out if an object is in use or not
701 * - if finds object and it's not in use:
702 * - returns a pointer to the object and a reference on it
703 * - returns with the directory locked
705 static struct dentry
*cachefiles_check_active(struct cachefiles_cache
*cache
,
709 struct cachefiles_object
*object
;
711 struct dentry
*victim
;
715 //_enter(",%*.*s/,%s",
716 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
718 /* look up the victim */
719 mutex_lock_nested(&dir
->d_inode
->i_mutex
, 1);
722 victim
= lookup_one_len(filename
, dir
, strlen(filename
));
723 cachefiles_hist(cachefiles_lookup_histogram
, start
);
727 //_debug("victim -> %p %s",
728 // victim, victim->d_inode ? "positive" : "negative");
730 /* if the object is no longer there then we probably retired the object
731 * at the netfs's request whilst the cull was in progress
733 if (!victim
->d_inode
) {
734 mutex_unlock(&dir
->d_inode
->i_mutex
);
736 _leave(" = -ENOENT [absent]");
737 return ERR_PTR(-ENOENT
);
740 /* check to see if we're using this object */
741 read_lock(&cache
->active_lock
);
743 _n
= cache
->active_nodes
.rb_node
;
746 object
= rb_entry(_n
, struct cachefiles_object
, active_node
);
748 if (object
->dentry
> victim
)
750 else if (object
->dentry
< victim
)
756 read_unlock(&cache
->active_lock
);
758 //_leave(" = %p", victim);
762 read_unlock(&cache
->active_lock
);
763 mutex_unlock(&dir
->d_inode
->i_mutex
);
765 //_leave(" = -EBUSY [in use]");
766 return ERR_PTR(-EBUSY
);
769 mutex_unlock(&dir
->d_inode
->i_mutex
);
770 ret
= PTR_ERR(victim
);
771 if (ret
== -ENOENT
) {
772 /* file or dir now absent - probably retired by netfs */
773 _leave(" = -ESTALE [absent]");
774 return ERR_PTR(-ESTALE
);
778 cachefiles_io_error(cache
, "Lookup failed");
779 } else if (ret
!= -ENOMEM
) {
780 kerror("Internal error: %d", ret
);
784 _leave(" = %d", ret
);
789 * cull an object if it's not in use
790 * - called only by cache manager daemon
792 int cachefiles_cull(struct cachefiles_cache
*cache
, struct dentry
*dir
,
795 struct dentry
*victim
;
799 dir
->d_name
.len
, dir
->d_name
.len
, dir
->d_name
.name
, filename
);
801 victim
= cachefiles_check_active(cache
, dir
, filename
);
803 return PTR_ERR(victim
);
805 _debug("victim -> %p %s",
806 victim
, victim
->d_inode
? "positive" : "negative");
808 /* okay... the victim is not being used so we can cull it
809 * - start by marking it as stale
811 _debug("victim is cullable");
813 ret
= cachefiles_remove_object_xattr(cache
, victim
);
817 /* actually remove the victim (drops the dir mutex) */
820 ret
= cachefiles_bury_object(cache
, dir
, victim
);
829 mutex_unlock(&dir
->d_inode
->i_mutex
);
832 if (ret
== -ENOENT
) {
833 /* file or dir now absent - probably retired by netfs */
834 _leave(" = -ESTALE [absent]");
838 if (ret
!= -ENOMEM
) {
839 kerror("Internal error: %d", ret
);
843 _leave(" = %d", ret
);
848 * find out if an object is in use or not
849 * - called only by cache manager daemon
850 * - returns -EBUSY or 0 to indicate whether an object is in use or not
852 int cachefiles_check_in_use(struct cachefiles_cache
*cache
, struct dentry
*dir
,
855 struct dentry
*victim
;
857 //_enter(",%*.*s/,%s",
858 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
860 victim
= cachefiles_check_active(cache
, dir
, filename
);
862 return PTR_ERR(victim
);
864 mutex_unlock(&dir
->d_inode
->i_mutex
);