blob: 0daa1e3fe0df837e3cbbade70184287aa129c294 [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>
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +020023#include <linux/xattr.h>
David Howells9ae326a2009-04-03 16:42:41 +010024#include "internal.h"
25
David Howellsd0e27b72009-11-19 18:12:02 +000026#define CACHEFILES_KEYBUF_SIZE 512
27
28/*
29 * dump debugging info about an object
30 */
31static noinline
32void __cachefiles_printk_object(struct cachefiles_object *object,
David Howells402cb8d2018-04-04 13:41:28 +010033 const char *prefix)
David Howellsd0e27b72009-11-19 18:12:02 +000034{
35 struct fscache_cookie *cookie;
David Howells402cb8d2018-04-04 13:41:28 +010036 const u8 *k;
37 unsigned loop;
David Howellsd0e27b72009-11-19 18:12:02 +000038
Fabian Frederick4e1eb882014-06-06 14:37:32 -070039 pr_err("%sobject: OBJ%x\n", prefix, object->fscache.debug_id);
40 pr_err("%sobjstate=%s fl=%lx wbusy=%x ev=%lx[%lx]\n",
David Howellscaaef692013-05-10 19:50:26 +010041 prefix, object->fscache.state->name,
Tejun Heo8b8edef2010-07-20 22:09:01 +020042 object->fscache.flags, work_busy(&object->fscache.work),
David Howellsc2d35bf2012-12-05 13:34:47 +000043 object->fscache.events, object->fscache.event_mask);
Fabian Frederick4e1eb882014-06-06 14:37:32 -070044 pr_err("%sops=%u inp=%u exc=%u\n",
David Howellsd0e27b72009-11-19 18:12:02 +000045 prefix, object->fscache.n_ops, object->fscache.n_in_progress,
46 object->fscache.n_exclusive);
Fabian Frederick4e1eb882014-06-06 14:37:32 -070047 pr_err("%sparent=%p\n",
David Howellsd0e27b72009-11-19 18:12:02 +000048 prefix, object->fscache.parent);
49
50 spin_lock(&object->fscache.lock);
51 cookie = object->fscache.cookie;
52 if (cookie) {
Fabian Frederick4e1eb882014-06-06 14:37:32 -070053 pr_err("%scookie=%p [pr=%p nd=%p fl=%lx]\n",
David Howellsd0e27b72009-11-19 18:12:02 +000054 prefix,
55 object->fscache.cookie,
56 object->fscache.cookie->parent,
57 object->fscache.cookie->netfs_data,
58 object->fscache.cookie->flags);
David Howells402cb8d2018-04-04 13:41:28 +010059 pr_err("%skey=[%u] '", prefix, cookie->key_len);
60 k = (cookie->key_len <= sizeof(cookie->inline_key)) ?
61 cookie->inline_key : cookie->key;
62 for (loop = 0; loop < cookie->key_len; loop++)
63 pr_cont("%02x", k[loop]);
64 pr_cont("'\n");
David Howellsd0e27b72009-11-19 18:12:02 +000065 } else {
Fabian Frederick4e1eb882014-06-06 14:37:32 -070066 pr_err("%scookie=NULL\n", prefix);
David Howellsd0e27b72009-11-19 18:12:02 +000067 }
68 spin_unlock(&object->fscache.lock);
David Howellsd0e27b72009-11-19 18:12:02 +000069}
70
71/*
72 * dump debugging info about a pair of objects
73 */
74static noinline void cachefiles_printk_object(struct cachefiles_object *object,
75 struct cachefiles_object *xobject)
76{
David Howellsd0e27b72009-11-19 18:12:02 +000077 if (object)
David Howells402cb8d2018-04-04 13:41:28 +010078 __cachefiles_printk_object(object, "");
David Howellsd0e27b72009-11-19 18:12:02 +000079 if (xobject)
David Howells402cb8d2018-04-04 13:41:28 +010080 __cachefiles_printk_object(xobject, "x");
David Howellsd0e27b72009-11-19 18:12:02 +000081}
82
David Howells9ae326a2009-04-03 16:42:41 +010083/*
David Howellsc61ea312010-05-11 16:51:39 +010084 * mark the owner of a dentry, if there is one, to indicate that that dentry
85 * has been preemptively deleted
86 * - the caller must hold the i_mutex on the dentry's parent as required to
87 * call vfs_unlink(), vfs_rmdir() or vfs_rename()
88 */
89static void cachefiles_mark_object_buried(struct cachefiles_cache *cache,
David Howells182d9192015-02-19 23:47:31 +000090 struct dentry *dentry,
91 enum fscache_why_object_killed why)
David Howellsc61ea312010-05-11 16:51:39 +010092{
93 struct cachefiles_object *object;
94 struct rb_node *p;
95
Al Viroa4555892014-10-21 20:11:25 -040096 _enter(",'%pd'", dentry);
David Howellsc61ea312010-05-11 16:51:39 +010097
98 write_lock(&cache->active_lock);
99
100 p = cache->active_nodes.rb_node;
101 while (p) {
102 object = rb_entry(p, struct cachefiles_object, active_node);
103 if (object->dentry > dentry)
104 p = p->rb_left;
105 else if (object->dentry < dentry)
106 p = p->rb_right;
107 else
108 goto found_dentry;
109 }
110
111 write_unlock(&cache->active_lock);
David Howellsa18feb52018-04-04 13:41:27 +0100112 trace_cachefiles_mark_buried(NULL, dentry, why);
David Howellsc61ea312010-05-11 16:51:39 +0100113 _leave(" [no owner]");
114 return;
115
116 /* found the dentry for */
117found_dentry:
118 kdebug("preemptive burial: OBJ%x [%s] %p",
119 object->fscache.debug_id,
David Howellscaaef692013-05-10 19:50:26 +0100120 object->fscache.state->name,
David Howellsc61ea312010-05-11 16:51:39 +0100121 dentry);
122
David Howellsa18feb52018-04-04 13:41:27 +0100123 trace_cachefiles_mark_buried(object, dentry, why);
124
David Howells493f7bc2013-05-10 19:50:26 +0100125 if (fscache_object_is_live(&object->fscache)) {
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700126 pr_err("\n");
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700127 pr_err("Error: Can't preemptively bury live object\n");
David Howellsc61ea312010-05-11 16:51:39 +0100128 cachefiles_printk_object(object, NULL);
David Howells182d9192015-02-19 23:47:31 +0000129 } else {
130 if (why != FSCACHE_OBJECT_IS_STALE)
131 fscache_object_mark_killed(&object->fscache, why);
David Howellsc61ea312010-05-11 16:51:39 +0100132 }
133
134 write_unlock(&cache->active_lock);
135 _leave(" [owner marked]");
136}
137
138/*
David Howells9ae326a2009-04-03 16:42:41 +0100139 * record the fact that an object is now active
140 */
David Howellsfee096d2009-11-19 18:12:05 +0000141static int cachefiles_mark_object_active(struct cachefiles_cache *cache,
142 struct cachefiles_object *object)
David Howells9ae326a2009-04-03 16:42:41 +0100143{
144 struct cachefiles_object *xobject;
145 struct rb_node **_p, *_parent = NULL;
146 struct dentry *dentry;
147
148 _enter(",%p", object);
149
150try_again:
151 write_lock(&cache->active_lock);
152
David Howellsa18feb52018-04-04 13:41:27 +0100153 dentry = object->dentry;
154 trace_cachefiles_mark_active(object, dentry);
155
David Howellsd0e27b72009-11-19 18:12:02 +0000156 if (test_and_set_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700157 pr_err("Error: Object already active\n");
David Howellsd0e27b72009-11-19 18:12:02 +0000158 cachefiles_printk_object(object, NULL);
David Howells9ae326a2009-04-03 16:42:41 +0100159 BUG();
David Howellsd0e27b72009-11-19 18:12:02 +0000160 }
David Howells9ae326a2009-04-03 16:42:41 +0100161
David Howells9ae326a2009-04-03 16:42:41 +0100162 _p = &cache->active_nodes.rb_node;
163 while (*_p) {
164 _parent = *_p;
165 xobject = rb_entry(_parent,
166 struct cachefiles_object, active_node);
167
168 ASSERT(xobject != object);
169
170 if (xobject->dentry > dentry)
171 _p = &(*_p)->rb_left;
172 else if (xobject->dentry < dentry)
173 _p = &(*_p)->rb_right;
174 else
175 goto wait_for_old_object;
176 }
177
178 rb_link_node(&object->active_node, _parent, _p);
179 rb_insert_color(&object->active_node, &cache->active_nodes);
180
181 write_unlock(&cache->active_lock);
David Howellsfee096d2009-11-19 18:12:05 +0000182 _leave(" = 0");
183 return 0;
David Howells9ae326a2009-04-03 16:42:41 +0100184
185 /* an old object from a previous incarnation is hogging the slot - we
186 * need to wait for it to be destroyed */
187wait_for_old_object:
David Howellsa18feb52018-04-04 13:41:27 +0100188 trace_cachefiles_wait_active(object, dentry, xobject);
189
David Howellsa30efe22014-09-30 14:50:30 +0100190 if (fscache_object_is_live(&xobject->fscache)) {
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700191 pr_err("\n");
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700192 pr_err("Error: Unexpected object collision\n");
David Howellsd0e27b72009-11-19 18:12:02 +0000193 cachefiles_printk_object(object, xobject);
David Howells9ae326a2009-04-03 16:42:41 +0100194 BUG();
195 }
196 atomic_inc(&xobject->usage);
197 write_unlock(&cache->active_lock);
198
David Howellsfee096d2009-11-19 18:12:05 +0000199 if (test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
200 wait_queue_head_t *wq;
201
202 signed long timeout = 60 * HZ;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200203 wait_queue_entry_t wait;
David Howellsfee096d2009-11-19 18:12:05 +0000204 bool requeue;
205
206 /* if the object we're waiting for is queued for processing,
207 * then just put ourselves on the queue behind it */
Tejun Heo8b8edef2010-07-20 22:09:01 +0200208 if (work_pending(&xobject->fscache.work)) {
David Howellsfee096d2009-11-19 18:12:05 +0000209 _debug("queue OBJ%x behind OBJ%x immediately",
210 object->fscache.debug_id,
211 xobject->fscache.debug_id);
212 goto requeue;
213 }
214
215 /* otherwise we sleep until either the object we're waiting for
Tejun Heo8b8edef2010-07-20 22:09:01 +0200216 * is done, or the fscache_object is congested */
David Howellsfee096d2009-11-19 18:12:05 +0000217 wq = bit_waitqueue(&xobject->flags, CACHEFILES_OBJECT_ACTIVE);
218 init_wait(&wait);
219 requeue = false;
220 do {
221 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
222 if (!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags))
223 break;
Tejun Heo8b8edef2010-07-20 22:09:01 +0200224
225 requeue = fscache_object_sleep_till_congested(&timeout);
David Howellsfee096d2009-11-19 18:12:05 +0000226 } while (timeout > 0 && !requeue);
227 finish_wait(wq, &wait);
228
229 if (requeue &&
230 test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
231 _debug("queue OBJ%x behind OBJ%x after wait",
232 object->fscache.debug_id,
233 xobject->fscache.debug_id);
234 goto requeue;
235 }
236
237 if (timeout <= 0) {
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700238 pr_err("\n");
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700239 pr_err("Error: Overlong wait for old active object to go away\n");
David Howellsfee096d2009-11-19 18:12:05 +0000240 cachefiles_printk_object(object, xobject);
241 goto requeue;
242 }
243 }
244
245 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags));
David Howells9ae326a2009-04-03 16:42:41 +0100246
David Howellsa18feb52018-04-04 13:41:27 +0100247 cache->cache.ops->put_object(&xobject->fscache, cachefiles_obj_put_wait_retry);
David Howells9ae326a2009-04-03 16:42:41 +0100248 goto try_again;
David Howellsfee096d2009-11-19 18:12:05 +0000249
250requeue:
251 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
David Howellsa18feb52018-04-04 13:41:27 +0100252 cache->cache.ops->put_object(&xobject->fscache, cachefiles_obj_put_wait_timeo);
David Howellsfee096d2009-11-19 18:12:05 +0000253 _leave(" = -ETIMEDOUT");
254 return -ETIMEDOUT;
David Howells9ae326a2009-04-03 16:42:41 +0100255}
256
257/*
David Howellsa5b3a802016-02-01 16:43:04 +0000258 * Mark an object as being inactive.
259 */
260void cachefiles_mark_object_inactive(struct cachefiles_cache *cache,
David Howellsa8181012016-08-09 17:41:16 +0100261 struct cachefiles_object *object,
262 blkcnt_t i_blocks)
David Howellsa5b3a802016-02-01 16:43:04 +0000263{
David Howellsa18feb52018-04-04 13:41:27 +0100264 struct dentry *dentry = object->dentry;
265 struct inode *inode = d_backing_inode(dentry);
266
267 trace_cachefiles_mark_inactive(object, dentry, inode);
268
David Howellsa5b3a802016-02-01 16:43:04 +0000269 write_lock(&cache->active_lock);
270 rb_erase(&object->active_node, &cache->active_nodes);
271 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
272 write_unlock(&cache->active_lock);
273
274 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
275
276 /* This object can now be culled, so we need to let the daemon know
277 * that there is something it can remove if it needs to.
278 */
David Howellsdb20a892016-08-03 17:57:39 +0100279 atomic_long_add(i_blocks, &cache->b_released);
David Howellsa5b3a802016-02-01 16:43:04 +0000280 if (atomic_inc_return(&cache->f_released))
281 cachefiles_state_changed(cache);
282}
283
284/*
David Howells9ae326a2009-04-03 16:42:41 +0100285 * delete an object representation from the cache
286 * - file backed objects are unlinked
287 * - directory backed objects are stuffed into the graveyard for userspace to
288 * delete
289 * - unlocks the directory mutex
290 */
291static int cachefiles_bury_object(struct cachefiles_cache *cache,
David Howellsa18feb52018-04-04 13:41:27 +0100292 struct cachefiles_object *object,
David Howells9ae326a2009-04-03 16:42:41 +0100293 struct dentry *dir,
David Howellsc61ea312010-05-11 16:51:39 +0100294 struct dentry *rep,
David Howells182d9192015-02-19 23:47:31 +0000295 bool preemptive,
296 enum fscache_why_object_killed why)
David Howells9ae326a2009-04-03 16:42:41 +0100297{
298 struct dentry *grave, *trap;
David Howells82140442010-12-24 14:48:35 +0000299 struct path path, path_to_graveyard;
David Howells9ae326a2009-04-03 16:42:41 +0100300 char nbuffer[8 + 8 + 1];
301 int ret;
302
Al Viroa4555892014-10-21 20:11:25 -0400303 _enter(",'%pd','%pd'", dir, rep);
David Howells9ae326a2009-04-03 16:42:41 +0100304
David Howellsc61ea312010-05-11 16:51:39 +0100305 _debug("remove %p from %p", rep, dir);
306
David Howells9ae326a2009-04-03 16:42:41 +0100307 /* non-directories can just be unlinked */
David Howellse36cb0b2015-01-29 12:02:35 +0000308 if (!d_is_dir(rep)) {
David Howells9ae326a2009-04-03 16:42:41 +0100309 _debug("unlink stale object");
David Howells9ae326a2009-04-03 16:42:41 +0100310
David Howells82140442010-12-24 14:48:35 +0000311 path.mnt = cache->mnt;
312 path.dentry = dir;
313 ret = security_path_unlink(&path, rep);
314 if (ret < 0) {
315 cachefiles_io_error(cache, "Unlink security error");
316 } else {
David Howellsa18feb52018-04-04 13:41:27 +0100317 trace_cachefiles_unlink(object, rep, why);
David Howells5153bc82015-03-06 14:08:58 +0000318 ret = vfs_unlink(d_inode(dir), rep, NULL);
David Howells82140442010-12-24 14:48:35 +0000319
320 if (preemptive)
David Howells182d9192015-02-19 23:47:31 +0000321 cachefiles_mark_object_buried(cache, rep, why);
David Howells82140442010-12-24 14:48:35 +0000322 }
David Howellsc61ea312010-05-11 16:51:39 +0100323
Al Viro59551022016-01-22 15:40:57 -0500324 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100325
326 if (ret == -EIO)
327 cachefiles_io_error(cache, "Unlink failed");
328
329 _leave(" = %d", ret);
330 return ret;
331 }
332
333 /* directories have to be moved to the graveyard */
334 _debug("move stale object to graveyard");
Al Viro59551022016-01-22 15:40:57 -0500335 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100336
337try_again:
338 /* first step is to make up a grave dentry in the graveyard */
339 sprintf(nbuffer, "%08x%08x",
340 (uint32_t) get_seconds(),
341 (uint32_t) atomic_inc_return(&cache->gravecounter));
342
343 /* do the multiway lock magic */
344 trap = lock_rename(cache->graveyard, dir);
345
346 /* do some checks before getting the grave dentry */
347 if (rep->d_parent != dir) {
348 /* the entry was probably culled when we dropped the parent dir
349 * lock */
350 unlock_rename(cache->graveyard, dir);
351 _leave(" = 0 [culled?]");
352 return 0;
353 }
354
David Howellsce40fa72015-01-29 12:02:36 +0000355 if (!d_can_lookup(cache->graveyard)) {
David Howells9ae326a2009-04-03 16:42:41 +0100356 unlock_rename(cache->graveyard, dir);
357 cachefiles_io_error(cache, "Graveyard no longer a directory");
358 return -EIO;
359 }
360
361 if (trap == rep) {
362 unlock_rename(cache->graveyard, dir);
363 cachefiles_io_error(cache, "May not make directory loop");
364 return -EIO;
365 }
366
367 if (d_mountpoint(rep)) {
368 unlock_rename(cache->graveyard, dir);
369 cachefiles_io_error(cache, "Mountpoint in cache");
370 return -EIO;
371 }
372
373 grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
374 if (IS_ERR(grave)) {
375 unlock_rename(cache->graveyard, dir);
376
377 if (PTR_ERR(grave) == -ENOMEM) {
378 _leave(" = -ENOMEM");
379 return -ENOMEM;
380 }
381
382 cachefiles_io_error(cache, "Lookup error %ld",
383 PTR_ERR(grave));
384 return -EIO;
385 }
386
David Howells466b77b2015-03-17 22:26:21 +0000387 if (d_is_positive(grave)) {
David Howells9ae326a2009-04-03 16:42:41 +0100388 unlock_rename(cache->graveyard, dir);
389 dput(grave);
390 grave = NULL;
391 cond_resched();
392 goto try_again;
393 }
394
395 if (d_mountpoint(grave)) {
396 unlock_rename(cache->graveyard, dir);
397 dput(grave);
398 cachefiles_io_error(cache, "Mountpoint in graveyard");
399 return -EIO;
400 }
401
402 /* target should not be an ancestor of source */
403 if (trap == grave) {
404 unlock_rename(cache->graveyard, dir);
405 dput(grave);
406 cachefiles_io_error(cache, "May not make directory loop");
407 return -EIO;
408 }
409
410 /* attempt the rename */
David Howells82140442010-12-24 14:48:35 +0000411 path.mnt = cache->mnt;
412 path.dentry = dir;
413 path_to_graveyard.mnt = cache->mnt;
414 path_to_graveyard.dentry = cache->graveyard;
Miklos Szeredi0b3974e2014-04-01 17:08:43 +0200415 ret = security_path_rename(&path, rep, &path_to_graveyard, grave, 0);
David Howells82140442010-12-24 14:48:35 +0000416 if (ret < 0) {
417 cachefiles_io_error(cache, "Rename security error %d", ret);
418 } else {
David Howellsa18feb52018-04-04 13:41:27 +0100419 trace_cachefiles_rename(object, rep, grave, why);
David Howells5153bc82015-03-06 14:08:58 +0000420 ret = vfs_rename(d_inode(dir), rep,
421 d_inode(cache->graveyard), grave, NULL, 0);
David Howells82140442010-12-24 14:48:35 +0000422 if (ret != 0 && ret != -ENOMEM)
423 cachefiles_io_error(cache,
424 "Rename failed with error %d", ret);
David Howells9ae326a2009-04-03 16:42:41 +0100425
David Howells82140442010-12-24 14:48:35 +0000426 if (preemptive)
David Howells182d9192015-02-19 23:47:31 +0000427 cachefiles_mark_object_buried(cache, rep, why);
David Howells82140442010-12-24 14:48:35 +0000428 }
David Howellsc61ea312010-05-11 16:51:39 +0100429
David Howells9ae326a2009-04-03 16:42:41 +0100430 unlock_rename(cache->graveyard, dir);
431 dput(grave);
432 _leave(" = 0");
433 return 0;
434}
435
436/*
437 * delete an object representation from the cache
438 */
439int cachefiles_delete_object(struct cachefiles_cache *cache,
440 struct cachefiles_object *object)
441{
442 struct dentry *dir;
443 int ret;
444
David Howellsc61ea312010-05-11 16:51:39 +0100445 _enter(",OBJ%x{%p}", object->fscache.debug_id, object->dentry);
David Howells9ae326a2009-04-03 16:42:41 +0100446
447 ASSERT(object->dentry);
David Howells466b77b2015-03-17 22:26:21 +0000448 ASSERT(d_backing_inode(object->dentry));
David Howells9ae326a2009-04-03 16:42:41 +0100449 ASSERT(object->dentry->d_parent);
450
451 dir = dget_parent(object->dentry);
452
Al Viro59551022016-01-22 15:40:57 -0500453 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
David Howells8f9941a2010-02-19 18:14:21 +0000454
David Howells182d9192015-02-19 23:47:31 +0000455 if (test_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->fscache.flags)) {
David Howellsc61ea312010-05-11 16:51:39 +0100456 /* object allocation for the same key preemptively deleted this
457 * object's file so that it could create its own file */
458 _debug("object preemptively buried");
Al Viro59551022016-01-22 15:40:57 -0500459 inode_unlock(d_inode(dir));
David Howells8f9941a2010-02-19 18:14:21 +0000460 ret = 0;
David Howellsc61ea312010-05-11 16:51:39 +0100461 } else {
462 /* we need to check that our parent is _still_ our parent - it
463 * may have been renamed */
464 if (dir == object->dentry->d_parent) {
David Howellsa18feb52018-04-04 13:41:27 +0100465 ret = cachefiles_bury_object(cache, object, dir,
David Howells182d9192015-02-19 23:47:31 +0000466 object->dentry, false,
467 FSCACHE_OBJECT_WAS_RETIRED);
David Howellsc61ea312010-05-11 16:51:39 +0100468 } else {
469 /* it got moved, presumably by cachefilesd culling it,
470 * so it's no longer in the key path and we can ignore
471 * it */
Al Viro59551022016-01-22 15:40:57 -0500472 inode_unlock(d_inode(dir));
David Howellsc61ea312010-05-11 16:51:39 +0100473 ret = 0;
474 }
David Howells8f9941a2010-02-19 18:14:21 +0000475 }
David Howells9ae326a2009-04-03 16:42:41 +0100476
477 dput(dir);
478 _leave(" = %d", ret);
479 return ret;
480}
481
482/*
483 * walk from the parent object to the child object through the backing
484 * filesystem, creating directories as we go
485 */
486int cachefiles_walk_to_object(struct cachefiles_object *parent,
487 struct cachefiles_object *object,
488 const char *key,
489 struct cachefiles_xattr *auxdata)
490{
491 struct cachefiles_cache *cache;
492 struct dentry *dir, *next = NULL;
David Howellsa18feb52018-04-04 13:41:27 +0100493 struct inode *inode;
David Howells82140442010-12-24 14:48:35 +0000494 struct path path;
David Howells9ae326a2009-04-03 16:42:41 +0100495 unsigned long start;
496 const char *name;
497 int ret, nlen;
498
David Howellsc61ea312010-05-11 16:51:39 +0100499 _enter("OBJ%x{%p},OBJ%x,%s,",
500 parent->fscache.debug_id, parent->dentry,
501 object->fscache.debug_id, key);
David Howells9ae326a2009-04-03 16:42:41 +0100502
503 cache = container_of(parent->fscache.cache,
504 struct cachefiles_cache, cache);
David Howells82140442010-12-24 14:48:35 +0000505 path.mnt = cache->mnt;
David Howells9ae326a2009-04-03 16:42:41 +0100506
507 ASSERT(parent->dentry);
David Howells466b77b2015-03-17 22:26:21 +0000508 ASSERT(d_backing_inode(parent->dentry));
David Howells9ae326a2009-04-03 16:42:41 +0100509
David Howellse36cb0b2015-01-29 12:02:35 +0000510 if (!(d_is_dir(parent->dentry))) {
David Howells9ae326a2009-04-03 16:42:41 +0100511 // TODO: convert file to dir
512 _leave("looking up in none directory");
513 return -ENOBUFS;
514 }
515
516 dir = dget(parent->dentry);
517
518advance:
519 /* attempt to transit the first directory component */
520 name = key;
521 nlen = strlen(key);
522
523 /* key ends in a double NUL */
524 key = key + nlen + 1;
525 if (!*key)
526 key = NULL;
527
528lookup_again:
529 /* search the current directory for the element name */
530 _debug("lookup '%s'", name);
531
Al Viro59551022016-01-22 15:40:57 -0500532 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
David Howells9ae326a2009-04-03 16:42:41 +0100533
534 start = jiffies;
535 next = lookup_one_len(name, dir, nlen);
536 cachefiles_hist(cachefiles_lookup_histogram, start);
David Howellsa18feb52018-04-04 13:41:27 +0100537 if (IS_ERR(next)) {
538 trace_cachefiles_lookup(object, next, NULL);
David Howells9ae326a2009-04-03 16:42:41 +0100539 goto lookup_error;
David Howellsa18feb52018-04-04 13:41:27 +0100540 }
David Howells9ae326a2009-04-03 16:42:41 +0100541
David Howellsa18feb52018-04-04 13:41:27 +0100542 inode = d_backing_inode(next);
543 trace_cachefiles_lookup(object, next, inode);
544 _debug("next -> %p %s", next, inode ? "positive" : "negative");
David Howells9ae326a2009-04-03 16:42:41 +0100545
546 if (!key)
David Howellsa18feb52018-04-04 13:41:27 +0100547 object->new = !inode;
David Howells9ae326a2009-04-03 16:42:41 +0100548
549 /* if this element of the path doesn't exist, then the lookup phase
550 * failed, and we can release any readers in the certain knowledge that
551 * there's nothing for them to actually read */
David Howells466b77b2015-03-17 22:26:21 +0000552 if (d_is_negative(next))
David Howells9ae326a2009-04-03 16:42:41 +0100553 fscache_object_lookup_negative(&object->fscache);
554
555 /* we need to create the object if it's negative */
556 if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
557 /* index objects and intervening tree levels must be subdirs */
David Howells466b77b2015-03-17 22:26:21 +0000558 if (d_is_negative(next)) {
David Howells9ae326a2009-04-03 16:42:41 +0100559 ret = cachefiles_has_space(cache, 1, 0);
560 if (ret < 0)
David Howells182d9192015-02-19 23:47:31 +0000561 goto no_space_error;
David Howells9ae326a2009-04-03 16:42:41 +0100562
David Howells82140442010-12-24 14:48:35 +0000563 path.dentry = dir;
564 ret = security_path_mkdir(&path, next, 0);
565 if (ret < 0)
566 goto create_error;
David Howells9ae326a2009-04-03 16:42:41 +0100567 start = jiffies;
David Howells5153bc82015-03-06 14:08:58 +0000568 ret = vfs_mkdir(d_inode(dir), next, 0);
David Howells9ae326a2009-04-03 16:42:41 +0100569 cachefiles_hist(cachefiles_mkdir_histogram, start);
David Howellsa18feb52018-04-04 13:41:27 +0100570 if (!key)
571 trace_cachefiles_mkdir(object, next, ret);
David Howells9ae326a2009-04-03 16:42:41 +0100572 if (ret < 0)
573 goto create_error;
574
David Howells466b77b2015-03-17 22:26:21 +0000575 ASSERT(d_backing_inode(next));
David Howells9ae326a2009-04-03 16:42:41 +0100576
577 _debug("mkdir -> %p{%p{ino=%lu}}",
David Howells466b77b2015-03-17 22:26:21 +0000578 next, d_backing_inode(next), d_backing_inode(next)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100579
David Howellsce40fa72015-01-29 12:02:36 +0000580 } else if (!d_can_lookup(next)) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700581 pr_err("inode %lu is not a directory\n",
David Howells466b77b2015-03-17 22:26:21 +0000582 d_backing_inode(next)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100583 ret = -ENOBUFS;
584 goto error;
585 }
586
587 } else {
588 /* non-index objects start out life as files */
David Howells466b77b2015-03-17 22:26:21 +0000589 if (d_is_negative(next)) {
David Howells9ae326a2009-04-03 16:42:41 +0100590 ret = cachefiles_has_space(cache, 1, 0);
591 if (ret < 0)
David Howells182d9192015-02-19 23:47:31 +0000592 goto no_space_error;
David Howells9ae326a2009-04-03 16:42:41 +0100593
David Howells82140442010-12-24 14:48:35 +0000594 path.dentry = dir;
595 ret = security_path_mknod(&path, next, S_IFREG, 0);
596 if (ret < 0)
597 goto create_error;
David Howells9ae326a2009-04-03 16:42:41 +0100598 start = jiffies;
David Howells5153bc82015-03-06 14:08:58 +0000599 ret = vfs_create(d_inode(dir), next, S_IFREG, true);
David Howells9ae326a2009-04-03 16:42:41 +0100600 cachefiles_hist(cachefiles_create_histogram, start);
David Howellsa18feb52018-04-04 13:41:27 +0100601 trace_cachefiles_create(object, next, ret);
David Howells9ae326a2009-04-03 16:42:41 +0100602 if (ret < 0)
603 goto create_error;
604
David Howells466b77b2015-03-17 22:26:21 +0000605 ASSERT(d_backing_inode(next));
David Howells9ae326a2009-04-03 16:42:41 +0100606
607 _debug("create -> %p{%p{ino=%lu}}",
David Howells466b77b2015-03-17 22:26:21 +0000608 next, d_backing_inode(next), d_backing_inode(next)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100609
David Howellsce40fa72015-01-29 12:02:36 +0000610 } else if (!d_can_lookup(next) &&
David Howellse36cb0b2015-01-29 12:02:35 +0000611 !d_is_reg(next)
David Howells9ae326a2009-04-03 16:42:41 +0100612 ) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700613 pr_err("inode %lu is not a file or directory\n",
David Howells466b77b2015-03-17 22:26:21 +0000614 d_backing_inode(next)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100615 ret = -ENOBUFS;
616 goto error;
617 }
618 }
619
620 /* process the next component */
621 if (key) {
622 _debug("advance");
Al Viro59551022016-01-22 15:40:57 -0500623 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100624 dput(dir);
625 dir = next;
626 next = NULL;
627 goto advance;
628 }
629
630 /* we've found the object we were looking for */
631 object->dentry = next;
632
633 /* if we've found that the terminal object exists, then we need to
634 * check its attributes and delete it if it's out of date */
635 if (!object->new) {
Al Viroa4555892014-10-21 20:11:25 -0400636 _debug("validate '%pd'", next);
David Howells9ae326a2009-04-03 16:42:41 +0100637
638 ret = cachefiles_check_object_xattr(object, auxdata);
639 if (ret == -ESTALE) {
640 /* delete the object (the deleter drops the directory
641 * mutex) */
642 object->dentry = NULL;
643
David Howellsa18feb52018-04-04 13:41:27 +0100644 ret = cachefiles_bury_object(cache, object, dir, next,
645 true,
David Howells182d9192015-02-19 23:47:31 +0000646 FSCACHE_OBJECT_IS_STALE);
David Howells9ae326a2009-04-03 16:42:41 +0100647 dput(next);
648 next = NULL;
649
650 if (ret < 0)
651 goto delete_error;
652
653 _debug("redo lookup");
David Howells182d9192015-02-19 23:47:31 +0000654 fscache_object_retrying_stale(&object->fscache);
David Howells9ae326a2009-04-03 16:42:41 +0100655 goto lookup_again;
656 }
657 }
658
659 /* note that we're now using this object */
David Howellsfee096d2009-11-19 18:12:05 +0000660 ret = cachefiles_mark_object_active(cache, object);
David Howells9ae326a2009-04-03 16:42:41 +0100661
Al Viro59551022016-01-22 15:40:57 -0500662 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100663 dput(dir);
664 dir = NULL;
665
David Howellsfee096d2009-11-19 18:12:05 +0000666 if (ret == -ETIMEDOUT)
667 goto mark_active_timed_out;
668
David Howells9ae326a2009-04-03 16:42:41 +0100669 _debug("=== OBTAINED_OBJECT ===");
670
671 if (object->new) {
672 /* attach data to a newly constructed terminal object */
673 ret = cachefiles_set_object_xattr(object, auxdata);
674 if (ret < 0)
675 goto check_error;
676 } else {
677 /* always update the atime on an object we've just looked up
678 * (this is used to keep track of culling, and atimes are only
679 * updated by read, write and readdir but not lookup or
680 * open) */
Al Viro68ac1232012-03-15 08:21:57 -0400681 path.dentry = next;
682 touch_atime(&path);
David Howells9ae326a2009-04-03 16:42:41 +0100683 }
684
685 /* open a file interface onto a data file */
686 if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
David Howellse36cb0b2015-01-29 12:02:35 +0000687 if (d_is_reg(object->dentry)) {
David Howells9ae326a2009-04-03 16:42:41 +0100688 const struct address_space_operations *aops;
689
690 ret = -EPERM;
David Howells466b77b2015-03-17 22:26:21 +0000691 aops = d_backing_inode(object->dentry)->i_mapping->a_ops;
David Howells9ae326a2009-04-03 16:42:41 +0100692 if (!aops->bmap)
693 goto check_error;
NeilBrown95201a42015-11-04 15:20:34 +0000694 if (object->dentry->d_sb->s_blocksize > PAGE_SIZE)
695 goto check_error;
David Howells9ae326a2009-04-03 16:42:41 +0100696
697 object->backer = object->dentry;
698 } else {
699 BUG(); // TODO: open file in data-class subdir
700 }
701 }
702
703 object->new = 0;
704 fscache_obtained_object(&object->fscache);
705
David Howells466b77b2015-03-17 22:26:21 +0000706 _leave(" = 0 [%lu]", d_backing_inode(object->dentry)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100707 return 0;
708
David Howells182d9192015-02-19 23:47:31 +0000709no_space_error:
710 fscache_object_mark_killed(&object->fscache, FSCACHE_OBJECT_NO_SPACE);
David Howells9ae326a2009-04-03 16:42:41 +0100711create_error:
712 _debug("create error %d", ret);
713 if (ret == -EIO)
714 cachefiles_io_error(cache, "Create/mkdir failed");
715 goto error;
716
David Howellsfee096d2009-11-19 18:12:05 +0000717mark_active_timed_out:
718 _debug("mark active timed out");
719 goto release_dentry;
720
David Howells9ae326a2009-04-03 16:42:41 +0100721check_error:
722 _debug("check error %d", ret);
David Howellsa8181012016-08-09 17:41:16 +0100723 cachefiles_mark_object_inactive(
724 cache, object, d_backing_inode(object->dentry)->i_blocks);
David Howellsfee096d2009-11-19 18:12:05 +0000725release_dentry:
David Howells9ae326a2009-04-03 16:42:41 +0100726 dput(object->dentry);
727 object->dentry = NULL;
728 goto error_out;
729
730delete_error:
731 _debug("delete error %d", ret);
732 goto error_out2;
733
734lookup_error:
735 _debug("lookup error %ld", PTR_ERR(next));
736 ret = PTR_ERR(next);
737 if (ret == -EIO)
738 cachefiles_io_error(cache, "Lookup failed");
739 next = NULL;
740error:
Al Viro59551022016-01-22 15:40:57 -0500741 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100742 dput(next);
743error_out2:
744 dput(dir);
745error_out:
David Howells9ae326a2009-04-03 16:42:41 +0100746 _leave(" = error %d", -ret);
747 return ret;
748}
749
750/*
751 * get a subdirectory
752 */
753struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
754 struct dentry *dir,
755 const char *dirname)
756{
757 struct dentry *subdir;
758 unsigned long start;
David Howells82140442010-12-24 14:48:35 +0000759 struct path path;
David Howells9ae326a2009-04-03 16:42:41 +0100760 int ret;
761
762 _enter(",,%s", dirname);
763
764 /* search the current directory for the element name */
Al Viro59551022016-01-22 15:40:57 -0500765 inode_lock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100766
767 start = jiffies;
768 subdir = lookup_one_len(dirname, dir, strlen(dirname));
769 cachefiles_hist(cachefiles_lookup_histogram, start);
770 if (IS_ERR(subdir)) {
771 if (PTR_ERR(subdir) == -ENOMEM)
772 goto nomem_d_alloc;
773 goto lookup_error;
774 }
775
776 _debug("subdir -> %p %s",
David Howells466b77b2015-03-17 22:26:21 +0000777 subdir, d_backing_inode(subdir) ? "positive" : "negative");
David Howells9ae326a2009-04-03 16:42:41 +0100778
779 /* we need to create the subdir if it doesn't exist yet */
David Howells466b77b2015-03-17 22:26:21 +0000780 if (d_is_negative(subdir)) {
David Howells9ae326a2009-04-03 16:42:41 +0100781 ret = cachefiles_has_space(cache, 1, 0);
782 if (ret < 0)
783 goto mkdir_error;
784
785 _debug("attempt mkdir");
786
David Howells82140442010-12-24 14:48:35 +0000787 path.mnt = cache->mnt;
788 path.dentry = dir;
789 ret = security_path_mkdir(&path, subdir, 0700);
790 if (ret < 0)
791 goto mkdir_error;
David Howells5153bc82015-03-06 14:08:58 +0000792 ret = vfs_mkdir(d_inode(dir), subdir, 0700);
David Howells9ae326a2009-04-03 16:42:41 +0100793 if (ret < 0)
794 goto mkdir_error;
795
David Howells466b77b2015-03-17 22:26:21 +0000796 ASSERT(d_backing_inode(subdir));
David Howells9ae326a2009-04-03 16:42:41 +0100797
798 _debug("mkdir -> %p{%p{ino=%lu}}",
799 subdir,
David Howells466b77b2015-03-17 22:26:21 +0000800 d_backing_inode(subdir),
801 d_backing_inode(subdir)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100802 }
803
Al Viro59551022016-01-22 15:40:57 -0500804 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100805
806 /* we need to make sure the subdir is a directory */
David Howells466b77b2015-03-17 22:26:21 +0000807 ASSERT(d_backing_inode(subdir));
David Howells9ae326a2009-04-03 16:42:41 +0100808
David Howellsce40fa72015-01-29 12:02:36 +0000809 if (!d_can_lookup(subdir)) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700810 pr_err("%s is not a directory\n", dirname);
David Howells9ae326a2009-04-03 16:42:41 +0100811 ret = -EIO;
812 goto check_error;
813 }
814
815 ret = -EPERM;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200816 if (!(d_backing_inode(subdir)->i_opflags & IOP_XATTR) ||
David Howells466b77b2015-03-17 22:26:21 +0000817 !d_backing_inode(subdir)->i_op->lookup ||
818 !d_backing_inode(subdir)->i_op->mkdir ||
819 !d_backing_inode(subdir)->i_op->create ||
Miklos Szeredi2773bf02016-09-27 11:03:58 +0200820 !d_backing_inode(subdir)->i_op->rename ||
David Howells466b77b2015-03-17 22:26:21 +0000821 !d_backing_inode(subdir)->i_op->rmdir ||
822 !d_backing_inode(subdir)->i_op->unlink)
David Howells9ae326a2009-04-03 16:42:41 +0100823 goto check_error;
824
David Howells466b77b2015-03-17 22:26:21 +0000825 _leave(" = [%lu]", d_backing_inode(subdir)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100826 return subdir;
827
828check_error:
829 dput(subdir);
830 _leave(" = %d [check]", ret);
831 return ERR_PTR(ret);
832
833mkdir_error:
Al Viro59551022016-01-22 15:40:57 -0500834 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100835 dput(subdir);
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700836 pr_err("mkdir %s failed with error %d\n", dirname, ret);
David Howells9ae326a2009-04-03 16:42:41 +0100837 return ERR_PTR(ret);
838
839lookup_error:
Al Viro59551022016-01-22 15:40:57 -0500840 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100841 ret = PTR_ERR(subdir);
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700842 pr_err("Lookup %s failed with error %d\n", dirname, ret);
David Howells9ae326a2009-04-03 16:42:41 +0100843 return ERR_PTR(ret);
844
845nomem_d_alloc:
Al Viro59551022016-01-22 15:40:57 -0500846 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100847 _leave(" = -ENOMEM");
848 return ERR_PTR(-ENOMEM);
849}
850
851/*
852 * find out if an object is in use or not
853 * - if finds object and it's not in use:
854 * - returns a pointer to the object and a reference on it
855 * - returns with the directory locked
856 */
857static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
858 struct dentry *dir,
859 char *filename)
860{
861 struct cachefiles_object *object;
862 struct rb_node *_n;
863 struct dentry *victim;
864 unsigned long start;
865 int ret;
866
Al Viroa4555892014-10-21 20:11:25 -0400867 //_enter(",%pd/,%s",
868 // dir, filename);
David Howells9ae326a2009-04-03 16:42:41 +0100869
870 /* look up the victim */
Al Viro59551022016-01-22 15:40:57 -0500871 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
David Howells9ae326a2009-04-03 16:42:41 +0100872
873 start = jiffies;
874 victim = lookup_one_len(filename, dir, strlen(filename));
875 cachefiles_hist(cachefiles_lookup_histogram, start);
876 if (IS_ERR(victim))
877 goto lookup_error;
878
879 //_debug("victim -> %p %s",
David Howells466b77b2015-03-17 22:26:21 +0000880 // victim, d_backing_inode(victim) ? "positive" : "negative");
David Howells9ae326a2009-04-03 16:42:41 +0100881
882 /* if the object is no longer there then we probably retired the object
883 * at the netfs's request whilst the cull was in progress
884 */
David Howells466b77b2015-03-17 22:26:21 +0000885 if (d_is_negative(victim)) {
Al Viro59551022016-01-22 15:40:57 -0500886 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100887 dput(victim);
888 _leave(" = -ENOENT [absent]");
889 return ERR_PTR(-ENOENT);
890 }
891
892 /* check to see if we're using this object */
893 read_lock(&cache->active_lock);
894
895 _n = cache->active_nodes.rb_node;
896
897 while (_n) {
898 object = rb_entry(_n, struct cachefiles_object, active_node);
899
900 if (object->dentry > victim)
901 _n = _n->rb_left;
902 else if (object->dentry < victim)
903 _n = _n->rb_right;
904 else
905 goto object_in_use;
906 }
907
908 read_unlock(&cache->active_lock);
909
910 //_leave(" = %p", victim);
911 return victim;
912
913object_in_use:
914 read_unlock(&cache->active_lock);
Al Viro59551022016-01-22 15:40:57 -0500915 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100916 dput(victim);
917 //_leave(" = -EBUSY [in use]");
918 return ERR_PTR(-EBUSY);
919
920lookup_error:
Al Viro59551022016-01-22 15:40:57 -0500921 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100922 ret = PTR_ERR(victim);
923 if (ret == -ENOENT) {
924 /* file or dir now absent - probably retired by netfs */
925 _leave(" = -ESTALE [absent]");
926 return ERR_PTR(-ESTALE);
927 }
928
929 if (ret == -EIO) {
930 cachefiles_io_error(cache, "Lookup failed");
931 } else if (ret != -ENOMEM) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700932 pr_err("Internal error: %d\n", ret);
David Howells9ae326a2009-04-03 16:42:41 +0100933 ret = -EIO;
934 }
935
936 _leave(" = %d", ret);
937 return ERR_PTR(ret);
938}
939
940/*
941 * cull an object if it's not in use
942 * - called only by cache manager daemon
943 */
944int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
945 char *filename)
946{
947 struct dentry *victim;
948 int ret;
949
Al Viroa4555892014-10-21 20:11:25 -0400950 _enter(",%pd/,%s", dir, filename);
David Howells9ae326a2009-04-03 16:42:41 +0100951
952 victim = cachefiles_check_active(cache, dir, filename);
953 if (IS_ERR(victim))
954 return PTR_ERR(victim);
955
956 _debug("victim -> %p %s",
David Howells466b77b2015-03-17 22:26:21 +0000957 victim, d_backing_inode(victim) ? "positive" : "negative");
David Howells9ae326a2009-04-03 16:42:41 +0100958
959 /* okay... the victim is not being used so we can cull it
960 * - start by marking it as stale
961 */
962 _debug("victim is cullable");
963
964 ret = cachefiles_remove_object_xattr(cache, victim);
965 if (ret < 0)
966 goto error_unlock;
967
968 /* actually remove the victim (drops the dir mutex) */
969 _debug("bury");
970
David Howellsa18feb52018-04-04 13:41:27 +0100971 ret = cachefiles_bury_object(cache, NULL, dir, victim, false,
David Howells182d9192015-02-19 23:47:31 +0000972 FSCACHE_OBJECT_WAS_CULLED);
David Howells9ae326a2009-04-03 16:42:41 +0100973 if (ret < 0)
974 goto error;
975
976 dput(victim);
977 _leave(" = 0");
978 return 0;
979
980error_unlock:
Al Viro59551022016-01-22 15:40:57 -0500981 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100982error:
983 dput(victim);
984 if (ret == -ENOENT) {
985 /* file or dir now absent - probably retired by netfs */
986 _leave(" = -ESTALE [absent]");
987 return -ESTALE;
988 }
989
990 if (ret != -ENOMEM) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700991 pr_err("Internal error: %d\n", ret);
David Howells9ae326a2009-04-03 16:42:41 +0100992 ret = -EIO;
993 }
994
995 _leave(" = %d", ret);
996 return ret;
997}
998
999/*
1000 * find out if an object is in use or not
1001 * - called only by cache manager daemon
1002 * - returns -EBUSY or 0 to indicate whether an object is in use or not
1003 */
1004int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
1005 char *filename)
1006{
1007 struct dentry *victim;
1008
Al Viroa4555892014-10-21 20:11:25 -04001009 //_enter(",%pd/,%s",
1010 // dir, filename);
David Howells9ae326a2009-04-03 16:42:41 +01001011
1012 victim = cachefiles_check_active(cache, dir, filename);
1013 if (IS_ERR(victim))
1014 return PTR_ERR(victim);
1015
Al Viro59551022016-01-22 15:40:57 -05001016 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +01001017 dput(victim);
1018 //_leave(" = 0");
1019 return 0;
1020}