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
);
352 /* we need to check that our parent is _still_ our parent - it may have
354 if (dir
== object
->dentry
->d_parent
) {
355 ret
= cachefiles_bury_object(cache
, dir
, object
->dentry
);
357 /* it got moved, presumably by cachefilesd culling it, so it's
358 * no longer in the key path and we can ignore it */
359 mutex_unlock(&dir
->d_inode
->i_mutex
);
364 _leave(" = %d", ret
);
369 * walk from the parent object to the child object through the backing
370 * filesystem, creating directories as we go
372 int cachefiles_walk_to_object(struct cachefiles_object
*parent
,
373 struct cachefiles_object
*object
,
375 struct cachefiles_xattr
*auxdata
)
377 struct cachefiles_cache
*cache
;
378 struct dentry
*dir
, *next
= NULL
;
383 _enter("{%p},,%s,", parent
->dentry
, key
);
385 cache
= container_of(parent
->fscache
.cache
,
386 struct cachefiles_cache
, cache
);
388 ASSERT(parent
->dentry
);
389 ASSERT(parent
->dentry
->d_inode
);
391 if (!(S_ISDIR(parent
->dentry
->d_inode
->i_mode
))) {
392 // TODO: convert file to dir
393 _leave("looking up in none directory");
397 dir
= dget(parent
->dentry
);
400 /* attempt to transit the first directory component */
404 /* key ends in a double NUL */
405 key
= key
+ nlen
+ 1;
410 /* search the current directory for the element name */
411 _debug("lookup '%s'", name
);
413 mutex_lock_nested(&dir
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
416 next
= lookup_one_len(name
, dir
, nlen
);
417 cachefiles_hist(cachefiles_lookup_histogram
, start
);
421 _debug("next -> %p %s", next
, next
->d_inode
? "positive" : "negative");
424 object
->new = !next
->d_inode
;
426 /* if this element of the path doesn't exist, then the lookup phase
427 * failed, and we can release any readers in the certain knowledge that
428 * there's nothing for them to actually read */
430 fscache_object_lookup_negative(&object
->fscache
);
432 /* we need to create the object if it's negative */
433 if (key
|| object
->type
== FSCACHE_COOKIE_TYPE_INDEX
) {
434 /* index objects and intervening tree levels must be subdirs */
435 if (!next
->d_inode
) {
436 ret
= cachefiles_has_space(cache
, 1, 0);
441 ret
= vfs_mkdir(dir
->d_inode
, next
, 0);
442 cachefiles_hist(cachefiles_mkdir_histogram
, start
);
446 ASSERT(next
->d_inode
);
448 _debug("mkdir -> %p{%p{ino=%lu}}",
449 next
, next
->d_inode
, next
->d_inode
->i_ino
);
451 } else if (!S_ISDIR(next
->d_inode
->i_mode
)) {
452 kerror("inode %lu is not a directory",
453 next
->d_inode
->i_ino
);
459 /* non-index objects start out life as files */
460 if (!next
->d_inode
) {
461 ret
= cachefiles_has_space(cache
, 1, 0);
466 ret
= vfs_create(dir
->d_inode
, next
, S_IFREG
, NULL
);
467 cachefiles_hist(cachefiles_create_histogram
, start
);
471 ASSERT(next
->d_inode
);
473 _debug("create -> %p{%p{ino=%lu}}",
474 next
, next
->d_inode
, next
->d_inode
->i_ino
);
476 } else if (!S_ISDIR(next
->d_inode
->i_mode
) &&
477 !S_ISREG(next
->d_inode
->i_mode
)
479 kerror("inode %lu is not a file or directory",
480 next
->d_inode
->i_ino
);
486 /* process the next component */
489 mutex_unlock(&dir
->d_inode
->i_mutex
);
496 /* we've found the object we were looking for */
497 object
->dentry
= next
;
499 /* if we've found that the terminal object exists, then we need to
500 * check its attributes and delete it if it's out of date */
502 _debug("validate '%*.*s'",
503 next
->d_name
.len
, next
->d_name
.len
, next
->d_name
.name
);
505 ret
= cachefiles_check_object_xattr(object
, auxdata
);
506 if (ret
== -ESTALE
) {
507 /* delete the object (the deleter drops the directory
509 object
->dentry
= NULL
;
511 ret
= cachefiles_bury_object(cache
, dir
, next
);
518 _debug("redo lookup");
523 /* note that we're now using this object */
524 ret
= cachefiles_mark_object_active(cache
, object
);
526 mutex_unlock(&dir
->d_inode
->i_mutex
);
530 if (ret
== -ETIMEDOUT
)
531 goto mark_active_timed_out
;
533 _debug("=== OBTAINED_OBJECT ===");
536 /* attach data to a newly constructed terminal object */
537 ret
= cachefiles_set_object_xattr(object
, auxdata
);
541 /* always update the atime on an object we've just looked up
542 * (this is used to keep track of culling, and atimes are only
543 * updated by read, write and readdir but not lookup or
545 touch_atime(cache
->mnt
, next
);
548 /* open a file interface onto a data file */
549 if (object
->type
!= FSCACHE_COOKIE_TYPE_INDEX
) {
550 if (S_ISREG(object
->dentry
->d_inode
->i_mode
)) {
551 const struct address_space_operations
*aops
;
554 aops
= object
->dentry
->d_inode
->i_mapping
->a_ops
;
558 object
->backer
= object
->dentry
;
560 BUG(); // TODO: open file in data-class subdir
565 fscache_obtained_object(&object
->fscache
);
567 _leave(" = 0 [%lu]", object
->dentry
->d_inode
->i_ino
);
571 _debug("create error %d", ret
);
573 cachefiles_io_error(cache
, "Create/mkdir failed");
576 mark_active_timed_out
:
577 _debug("mark active timed out");
581 _debug("check error %d", ret
);
582 write_lock(&cache
->active_lock
);
583 rb_erase(&object
->active_node
, &cache
->active_nodes
);
584 clear_bit(CACHEFILES_OBJECT_ACTIVE
, &object
->flags
);
585 wake_up_bit(&object
->flags
, CACHEFILES_OBJECT_ACTIVE
);
586 write_unlock(&cache
->active_lock
);
588 dput(object
->dentry
);
589 object
->dentry
= NULL
;
593 _debug("delete error %d", ret
);
597 _debug("lookup error %ld", PTR_ERR(next
));
600 cachefiles_io_error(cache
, "Lookup failed");
603 mutex_unlock(&dir
->d_inode
->i_mutex
);
608 _leave(" = error %d", -ret
);
615 struct dentry
*cachefiles_get_directory(struct cachefiles_cache
*cache
,
619 struct dentry
*subdir
;
623 _enter(",,%s", dirname
);
625 /* search the current directory for the element name */
626 mutex_lock(&dir
->d_inode
->i_mutex
);
629 subdir
= lookup_one_len(dirname
, dir
, strlen(dirname
));
630 cachefiles_hist(cachefiles_lookup_histogram
, start
);
631 if (IS_ERR(subdir
)) {
632 if (PTR_ERR(subdir
) == -ENOMEM
)
637 _debug("subdir -> %p %s",
638 subdir
, subdir
->d_inode
? "positive" : "negative");
640 /* we need to create the subdir if it doesn't exist yet */
641 if (!subdir
->d_inode
) {
642 ret
= cachefiles_has_space(cache
, 1, 0);
646 _debug("attempt mkdir");
648 ret
= vfs_mkdir(dir
->d_inode
, subdir
, 0700);
652 ASSERT(subdir
->d_inode
);
654 _debug("mkdir -> %p{%p{ino=%lu}}",
657 subdir
->d_inode
->i_ino
);
660 mutex_unlock(&dir
->d_inode
->i_mutex
);
662 /* we need to make sure the subdir is a directory */
663 ASSERT(subdir
->d_inode
);
665 if (!S_ISDIR(subdir
->d_inode
->i_mode
)) {
666 kerror("%s is not a directory", dirname
);
672 if (!subdir
->d_inode
->i_op
||
673 !subdir
->d_inode
->i_op
->setxattr
||
674 !subdir
->d_inode
->i_op
->getxattr
||
675 !subdir
->d_inode
->i_op
->lookup
||
676 !subdir
->d_inode
->i_op
->mkdir
||
677 !subdir
->d_inode
->i_op
->create
||
678 !subdir
->d_inode
->i_op
->rename
||
679 !subdir
->d_inode
->i_op
->rmdir
||
680 !subdir
->d_inode
->i_op
->unlink
)
683 _leave(" = [%lu]", subdir
->d_inode
->i_ino
);
688 _leave(" = %d [check]", ret
);
692 mutex_unlock(&dir
->d_inode
->i_mutex
);
694 kerror("mkdir %s failed with error %d", dirname
, ret
);
698 mutex_unlock(&dir
->d_inode
->i_mutex
);
699 ret
= PTR_ERR(subdir
);
700 kerror("Lookup %s failed with error %d", dirname
, ret
);
704 mutex_unlock(&dir
->d_inode
->i_mutex
);
705 _leave(" = -ENOMEM");
706 return ERR_PTR(-ENOMEM
);
710 * find out if an object is in use or not
711 * - if finds object and it's not in use:
712 * - returns a pointer to the object and a reference on it
713 * - returns with the directory locked
715 static struct dentry
*cachefiles_check_active(struct cachefiles_cache
*cache
,
719 struct cachefiles_object
*object
;
721 struct dentry
*victim
;
725 //_enter(",%*.*s/,%s",
726 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
728 /* look up the victim */
729 mutex_lock_nested(&dir
->d_inode
->i_mutex
, 1);
732 victim
= lookup_one_len(filename
, dir
, strlen(filename
));
733 cachefiles_hist(cachefiles_lookup_histogram
, start
);
737 //_debug("victim -> %p %s",
738 // victim, victim->d_inode ? "positive" : "negative");
740 /* if the object is no longer there then we probably retired the object
741 * at the netfs's request whilst the cull was in progress
743 if (!victim
->d_inode
) {
744 mutex_unlock(&dir
->d_inode
->i_mutex
);
746 _leave(" = -ENOENT [absent]");
747 return ERR_PTR(-ENOENT
);
750 /* check to see if we're using this object */
751 read_lock(&cache
->active_lock
);
753 _n
= cache
->active_nodes
.rb_node
;
756 object
= rb_entry(_n
, struct cachefiles_object
, active_node
);
758 if (object
->dentry
> victim
)
760 else if (object
->dentry
< victim
)
766 read_unlock(&cache
->active_lock
);
768 //_leave(" = %p", victim);
772 read_unlock(&cache
->active_lock
);
773 mutex_unlock(&dir
->d_inode
->i_mutex
);
775 //_leave(" = -EBUSY [in use]");
776 return ERR_PTR(-EBUSY
);
779 mutex_unlock(&dir
->d_inode
->i_mutex
);
780 ret
= PTR_ERR(victim
);
781 if (ret
== -ENOENT
) {
782 /* file or dir now absent - probably retired by netfs */
783 _leave(" = -ESTALE [absent]");
784 return ERR_PTR(-ESTALE
);
788 cachefiles_io_error(cache
, "Lookup failed");
789 } else if (ret
!= -ENOMEM
) {
790 kerror("Internal error: %d", ret
);
794 _leave(" = %d", ret
);
799 * cull an object if it's not in use
800 * - called only by cache manager daemon
802 int cachefiles_cull(struct cachefiles_cache
*cache
, struct dentry
*dir
,
805 struct dentry
*victim
;
809 dir
->d_name
.len
, dir
->d_name
.len
, dir
->d_name
.name
, filename
);
811 victim
= cachefiles_check_active(cache
, dir
, filename
);
813 return PTR_ERR(victim
);
815 _debug("victim -> %p %s",
816 victim
, victim
->d_inode
? "positive" : "negative");
818 /* okay... the victim is not being used so we can cull it
819 * - start by marking it as stale
821 _debug("victim is cullable");
823 ret
= cachefiles_remove_object_xattr(cache
, victim
);
827 /* actually remove the victim (drops the dir mutex) */
830 ret
= cachefiles_bury_object(cache
, dir
, victim
);
839 mutex_unlock(&dir
->d_inode
->i_mutex
);
842 if (ret
== -ENOENT
) {
843 /* file or dir now absent - probably retired by netfs */
844 _leave(" = -ESTALE [absent]");
848 if (ret
!= -ENOMEM
) {
849 kerror("Internal error: %d", ret
);
853 _leave(" = %d", ret
);
858 * find out if an object is in use or not
859 * - called only by cache manager daemon
860 * - returns -EBUSY or 0 to indicate whether an object is in use or not
862 int cachefiles_check_in_use(struct cachefiles_cache
*cache
, struct dentry
*dir
,
865 struct dentry
*victim
;
867 //_enter(",%*.*s/,%s",
868 // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
870 victim
= cachefiles_check_active(cache
, dir
, filename
);
872 return PTR_ERR(victim
);
874 mutex_unlock(&dir
->d_inode
->i_mutex
);