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