blob: ecc8ecbbfa5ac5fa66b1389cf3209b2ec2cf80a1 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells9ae326a2009-04-03 16:42:41 +01002/* CacheFiles path walking and related routines
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells9ae326a2009-04-03 16:42:41 +01006 */
7
8#include <linux/module.h>
9#include <linux/sched.h>
10#include <linux/file.h>
11#include <linux/fs.h>
12#include <linux/fsnotify.h>
13#include <linux/quotaops.h>
14#include <linux/xattr.h>
15#include <linux/mount.h>
16#include <linux/namei.h>
17#include <linux/security.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
David Howells9ae326a2009-04-03 16:42:41 +010019#include "internal.h"
20
David Howellsd0e27b72009-11-19 18:12:02 +000021#define CACHEFILES_KEYBUF_SIZE 512
22
23/*
24 * dump debugging info about an object
25 */
26static noinline
27void __cachefiles_printk_object(struct cachefiles_object *object,
David Howells402cb8d2018-04-04 13:41:28 +010028 const char *prefix)
David Howellsd0e27b72009-11-19 18:12:02 +000029{
30 struct fscache_cookie *cookie;
David Howells402cb8d2018-04-04 13:41:28 +010031 const u8 *k;
32 unsigned loop;
David Howellsd0e27b72009-11-19 18:12:02 +000033
Fabian Frederick4e1eb882014-06-06 14:37:32 -070034 pr_err("%sobject: OBJ%x\n", prefix, object->fscache.debug_id);
35 pr_err("%sobjstate=%s fl=%lx wbusy=%x ev=%lx[%lx]\n",
David Howellscaaef692013-05-10 19:50:26 +010036 prefix, object->fscache.state->name,
Tejun Heo8b8edef2010-07-20 22:09:01 +020037 object->fscache.flags, work_busy(&object->fscache.work),
David Howellsc2d35bf2012-12-05 13:34:47 +000038 object->fscache.events, object->fscache.event_mask);
Fabian Frederick4e1eb882014-06-06 14:37:32 -070039 pr_err("%sops=%u inp=%u exc=%u\n",
David Howellsd0e27b72009-11-19 18:12:02 +000040 prefix, object->fscache.n_ops, object->fscache.n_in_progress,
41 object->fscache.n_exclusive);
Fabian Frederick4e1eb882014-06-06 14:37:32 -070042 pr_err("%sparent=%p\n",
David Howellsd0e27b72009-11-19 18:12:02 +000043 prefix, object->fscache.parent);
44
45 spin_lock(&object->fscache.lock);
46 cookie = object->fscache.cookie;
47 if (cookie) {
Fabian Frederick4e1eb882014-06-06 14:37:32 -070048 pr_err("%scookie=%p [pr=%p nd=%p fl=%lx]\n",
David Howellsd0e27b72009-11-19 18:12:02 +000049 prefix,
50 object->fscache.cookie,
51 object->fscache.cookie->parent,
52 object->fscache.cookie->netfs_data,
53 object->fscache.cookie->flags);
David Howells402cb8d2018-04-04 13:41:28 +010054 pr_err("%skey=[%u] '", prefix, cookie->key_len);
55 k = (cookie->key_len <= sizeof(cookie->inline_key)) ?
56 cookie->inline_key : cookie->key;
57 for (loop = 0; loop < cookie->key_len; loop++)
58 pr_cont("%02x", k[loop]);
59 pr_cont("'\n");
David Howellsd0e27b72009-11-19 18:12:02 +000060 } else {
Fabian Frederick4e1eb882014-06-06 14:37:32 -070061 pr_err("%scookie=NULL\n", prefix);
David Howellsd0e27b72009-11-19 18:12:02 +000062 }
63 spin_unlock(&object->fscache.lock);
David Howellsd0e27b72009-11-19 18:12:02 +000064}
65
66/*
67 * dump debugging info about a pair of objects
68 */
69static noinline void cachefiles_printk_object(struct cachefiles_object *object,
70 struct cachefiles_object *xobject)
71{
David Howellsd0e27b72009-11-19 18:12:02 +000072 if (object)
David Howells402cb8d2018-04-04 13:41:28 +010073 __cachefiles_printk_object(object, "");
David Howellsd0e27b72009-11-19 18:12:02 +000074 if (xobject)
David Howells402cb8d2018-04-04 13:41:28 +010075 __cachefiles_printk_object(xobject, "x");
David Howellsd0e27b72009-11-19 18:12:02 +000076}
77
David Howells9ae326a2009-04-03 16:42:41 +010078/*
David Howellsc61ea312010-05-11 16:51:39 +010079 * mark the owner of a dentry, if there is one, to indicate that that dentry
80 * has been preemptively deleted
81 * - the caller must hold the i_mutex on the dentry's parent as required to
82 * call vfs_unlink(), vfs_rmdir() or vfs_rename()
83 */
84static void cachefiles_mark_object_buried(struct cachefiles_cache *cache,
David Howells182d9192015-02-19 23:47:31 +000085 struct dentry *dentry,
86 enum fscache_why_object_killed why)
David Howellsc61ea312010-05-11 16:51:39 +010087{
88 struct cachefiles_object *object;
89 struct rb_node *p;
90
Al Viroa4555892014-10-21 20:11:25 -040091 _enter(",'%pd'", dentry);
David Howellsc61ea312010-05-11 16:51:39 +010092
93 write_lock(&cache->active_lock);
94
95 p = cache->active_nodes.rb_node;
96 while (p) {
97 object = rb_entry(p, struct cachefiles_object, active_node);
98 if (object->dentry > dentry)
99 p = p->rb_left;
100 else if (object->dentry < dentry)
101 p = p->rb_right;
102 else
103 goto found_dentry;
104 }
105
106 write_unlock(&cache->active_lock);
David Howellsa18feb52018-04-04 13:41:27 +0100107 trace_cachefiles_mark_buried(NULL, dentry, why);
David Howellsc61ea312010-05-11 16:51:39 +0100108 _leave(" [no owner]");
109 return;
110
111 /* found the dentry for */
112found_dentry:
113 kdebug("preemptive burial: OBJ%x [%s] %p",
114 object->fscache.debug_id,
David Howellscaaef692013-05-10 19:50:26 +0100115 object->fscache.state->name,
David Howellsc61ea312010-05-11 16:51:39 +0100116 dentry);
117
David Howellsa18feb52018-04-04 13:41:27 +0100118 trace_cachefiles_mark_buried(object, dentry, why);
119
David Howells493f7bc2013-05-10 19:50:26 +0100120 if (fscache_object_is_live(&object->fscache)) {
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700121 pr_err("\n");
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700122 pr_err("Error: Can't preemptively bury live object\n");
David Howellsc61ea312010-05-11 16:51:39 +0100123 cachefiles_printk_object(object, NULL);
David Howells182d9192015-02-19 23:47:31 +0000124 } else {
125 if (why != FSCACHE_OBJECT_IS_STALE)
126 fscache_object_mark_killed(&object->fscache, why);
David Howellsc61ea312010-05-11 16:51:39 +0100127 }
128
129 write_unlock(&cache->active_lock);
130 _leave(" [owner marked]");
131}
132
133/*
David Howells9ae326a2009-04-03 16:42:41 +0100134 * record the fact that an object is now active
135 */
David Howellsfee096d2009-11-19 18:12:05 +0000136static int cachefiles_mark_object_active(struct cachefiles_cache *cache,
137 struct cachefiles_object *object)
David Howells9ae326a2009-04-03 16:42:41 +0100138{
139 struct cachefiles_object *xobject;
140 struct rb_node **_p, *_parent = NULL;
141 struct dentry *dentry;
142
143 _enter(",%p", object);
144
145try_again:
146 write_lock(&cache->active_lock);
147
David Howellsa18feb52018-04-04 13:41:27 +0100148 dentry = object->dentry;
149 trace_cachefiles_mark_active(object, dentry);
150
David Howellsd0e27b72009-11-19 18:12:02 +0000151 if (test_and_set_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700152 pr_err("Error: Object already active\n");
David Howellsd0e27b72009-11-19 18:12:02 +0000153 cachefiles_printk_object(object, NULL);
David Howells9ae326a2009-04-03 16:42:41 +0100154 BUG();
David Howellsd0e27b72009-11-19 18:12:02 +0000155 }
David Howells9ae326a2009-04-03 16:42:41 +0100156
David Howells9ae326a2009-04-03 16:42:41 +0100157 _p = &cache->active_nodes.rb_node;
158 while (*_p) {
159 _parent = *_p;
160 xobject = rb_entry(_parent,
161 struct cachefiles_object, active_node);
162
163 ASSERT(xobject != object);
164
165 if (xobject->dentry > dentry)
166 _p = &(*_p)->rb_left;
167 else if (xobject->dentry < dentry)
168 _p = &(*_p)->rb_right;
169 else
170 goto wait_for_old_object;
171 }
172
173 rb_link_node(&object->active_node, _parent, _p);
174 rb_insert_color(&object->active_node, &cache->active_nodes);
175
176 write_unlock(&cache->active_lock);
David Howellsfee096d2009-11-19 18:12:05 +0000177 _leave(" = 0");
178 return 0;
David Howells9ae326a2009-04-03 16:42:41 +0100179
180 /* an old object from a previous incarnation is hogging the slot - we
181 * need to wait for it to be destroyed */
182wait_for_old_object:
David Howellsa18feb52018-04-04 13:41:27 +0100183 trace_cachefiles_wait_active(object, dentry, xobject);
Kiran Kumar Modukuri5ce83d42018-06-21 13:25:53 -0700184 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
David Howellsa18feb52018-04-04 13:41:27 +0100185
David Howellsa30efe22014-09-30 14:50:30 +0100186 if (fscache_object_is_live(&xobject->fscache)) {
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700187 pr_err("\n");
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700188 pr_err("Error: Unexpected object collision\n");
David Howellsd0e27b72009-11-19 18:12:02 +0000189 cachefiles_printk_object(object, xobject);
David Howells9ae326a2009-04-03 16:42:41 +0100190 }
191 atomic_inc(&xobject->usage);
192 write_unlock(&cache->active_lock);
193
David Howellsfee096d2009-11-19 18:12:05 +0000194 if (test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
195 wait_queue_head_t *wq;
196
197 signed long timeout = 60 * HZ;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200198 wait_queue_entry_t wait;
David Howellsfee096d2009-11-19 18:12:05 +0000199 bool requeue;
200
201 /* if the object we're waiting for is queued for processing,
202 * then just put ourselves on the queue behind it */
Tejun Heo8b8edef2010-07-20 22:09:01 +0200203 if (work_pending(&xobject->fscache.work)) {
David Howellsfee096d2009-11-19 18:12:05 +0000204 _debug("queue OBJ%x behind OBJ%x immediately",
205 object->fscache.debug_id,
206 xobject->fscache.debug_id);
207 goto requeue;
208 }
209
210 /* otherwise we sleep until either the object we're waiting for
Tejun Heo8b8edef2010-07-20 22:09:01 +0200211 * is done, or the fscache_object is congested */
David Howellsfee096d2009-11-19 18:12:05 +0000212 wq = bit_waitqueue(&xobject->flags, CACHEFILES_OBJECT_ACTIVE);
213 init_wait(&wait);
214 requeue = false;
215 do {
216 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
217 if (!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags))
218 break;
Tejun Heo8b8edef2010-07-20 22:09:01 +0200219
220 requeue = fscache_object_sleep_till_congested(&timeout);
David Howellsfee096d2009-11-19 18:12:05 +0000221 } while (timeout > 0 && !requeue);
222 finish_wait(wq, &wait);
223
224 if (requeue &&
225 test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
226 _debug("queue OBJ%x behind OBJ%x after wait",
227 object->fscache.debug_id,
228 xobject->fscache.debug_id);
229 goto requeue;
230 }
231
232 if (timeout <= 0) {
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700233 pr_err("\n");
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700234 pr_err("Error: Overlong wait for old active object to go away\n");
David Howellsfee096d2009-11-19 18:12:05 +0000235 cachefiles_printk_object(object, xobject);
236 goto requeue;
237 }
238 }
239
240 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags));
David Howells9ae326a2009-04-03 16:42:41 +0100241
Nathan Chancellorb7e768b2018-09-24 10:33:44 -0700242 cache->cache.ops->put_object(&xobject->fscache,
243 (enum fscache_obj_ref_trace)cachefiles_obj_put_wait_retry);
David Howells9ae326a2009-04-03 16:42:41 +0100244 goto try_again;
David Howellsfee096d2009-11-19 18:12:05 +0000245
246requeue:
Nathan Chancellorb7e768b2018-09-24 10:33:44 -0700247 cache->cache.ops->put_object(&xobject->fscache,
248 (enum fscache_obj_ref_trace)cachefiles_obj_put_wait_timeo);
David Howellsfee096d2009-11-19 18:12:05 +0000249 _leave(" = -ETIMEDOUT");
250 return -ETIMEDOUT;
David Howells9ae326a2009-04-03 16:42:41 +0100251}
252
253/*
David Howellsa5b3a802016-02-01 16:43:04 +0000254 * Mark an object as being inactive.
255 */
256void cachefiles_mark_object_inactive(struct cachefiles_cache *cache,
David Howellsa8181012016-08-09 17:41:16 +0100257 struct cachefiles_object *object,
258 blkcnt_t i_blocks)
David Howellsa5b3a802016-02-01 16:43:04 +0000259{
David Howellsa18feb52018-04-04 13:41:27 +0100260 struct dentry *dentry = object->dentry;
261 struct inode *inode = d_backing_inode(dentry);
262
263 trace_cachefiles_mark_inactive(object, dentry, inode);
264
David Howellsa5b3a802016-02-01 16:43:04 +0000265 write_lock(&cache->active_lock);
266 rb_erase(&object->active_node, &cache->active_nodes);
267 clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
268 write_unlock(&cache->active_lock);
269
270 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
271
272 /* This object can now be culled, so we need to let the daemon know
273 * that there is something it can remove if it needs to.
274 */
David Howellsdb20a892016-08-03 17:57:39 +0100275 atomic_long_add(i_blocks, &cache->b_released);
David Howellsa5b3a802016-02-01 16:43:04 +0000276 if (atomic_inc_return(&cache->f_released))
277 cachefiles_state_changed(cache);
278}
279
280/*
David Howells9ae326a2009-04-03 16:42:41 +0100281 * delete an object representation from the cache
282 * - file backed objects are unlinked
283 * - directory backed objects are stuffed into the graveyard for userspace to
284 * delete
285 * - unlocks the directory mutex
286 */
287static int cachefiles_bury_object(struct cachefiles_cache *cache,
David Howellsa18feb52018-04-04 13:41:27 +0100288 struct cachefiles_object *object,
David Howells9ae326a2009-04-03 16:42:41 +0100289 struct dentry *dir,
David Howellsc61ea312010-05-11 16:51:39 +0100290 struct dentry *rep,
David Howells182d9192015-02-19 23:47:31 +0000291 bool preemptive,
292 enum fscache_why_object_killed why)
David Howells9ae326a2009-04-03 16:42:41 +0100293{
294 struct dentry *grave, *trap;
David Howells82140442010-12-24 14:48:35 +0000295 struct path path, path_to_graveyard;
David Howells9ae326a2009-04-03 16:42:41 +0100296 char nbuffer[8 + 8 + 1];
297 int ret;
298
Al Viroa4555892014-10-21 20:11:25 -0400299 _enter(",'%pd','%pd'", dir, rep);
David Howells9ae326a2009-04-03 16:42:41 +0100300
David Howellsc61ea312010-05-11 16:51:39 +0100301 _debug("remove %p from %p", rep, dir);
302
David Howells9ae326a2009-04-03 16:42:41 +0100303 /* non-directories can just be unlinked */
David Howellse36cb0b2015-01-29 12:02:35 +0000304 if (!d_is_dir(rep)) {
David Howells9ae326a2009-04-03 16:42:41 +0100305 _debug("unlink stale object");
David Howells9ae326a2009-04-03 16:42:41 +0100306
David Howells82140442010-12-24 14:48:35 +0000307 path.mnt = cache->mnt;
308 path.dentry = dir;
309 ret = security_path_unlink(&path, rep);
310 if (ret < 0) {
311 cachefiles_io_error(cache, "Unlink security error");
312 } else {
David Howellsa18feb52018-04-04 13:41:27 +0100313 trace_cachefiles_unlink(object, rep, why);
David Howells5153bc82015-03-06 14:08:58 +0000314 ret = vfs_unlink(d_inode(dir), rep, NULL);
David Howells82140442010-12-24 14:48:35 +0000315
316 if (preemptive)
David Howells182d9192015-02-19 23:47:31 +0000317 cachefiles_mark_object_buried(cache, rep, why);
David Howells82140442010-12-24 14:48:35 +0000318 }
David Howellsc61ea312010-05-11 16:51:39 +0100319
Al Viro59551022016-01-22 15:40:57 -0500320 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100321
322 if (ret == -EIO)
323 cachefiles_io_error(cache, "Unlink failed");
324
325 _leave(" = %d", ret);
326 return ret;
327 }
328
329 /* directories have to be moved to the graveyard */
330 _debug("move stale object to graveyard");
Al Viro59551022016-01-22 15:40:57 -0500331 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100332
333try_again:
334 /* first step is to make up a grave dentry in the graveyard */
335 sprintf(nbuffer, "%08x%08x",
Arnd Bergmann34e06fe2018-07-13 16:27:44 +0200336 (uint32_t) ktime_get_real_seconds(),
David Howells9ae326a2009-04-03 16:42:41 +0100337 (uint32_t) atomic_inc_return(&cache->gravecounter));
338
339 /* do the multiway lock magic */
340 trap = lock_rename(cache->graveyard, dir);
341
342 /* do some checks before getting the grave dentry */
Al Viro169b8032018-10-17 15:23:26 +0100343 if (rep->d_parent != dir || IS_DEADDIR(d_inode(rep))) {
David Howells9ae326a2009-04-03 16:42:41 +0100344 /* the entry was probably culled when we dropped the parent dir
345 * lock */
346 unlock_rename(cache->graveyard, dir);
347 _leave(" = 0 [culled?]");
348 return 0;
349 }
350
David Howellsce40fa72015-01-29 12:02:36 +0000351 if (!d_can_lookup(cache->graveyard)) {
David Howells9ae326a2009-04-03 16:42:41 +0100352 unlock_rename(cache->graveyard, dir);
353 cachefiles_io_error(cache, "Graveyard no longer a directory");
354 return -EIO;
355 }
356
357 if (trap == rep) {
358 unlock_rename(cache->graveyard, dir);
359 cachefiles_io_error(cache, "May not make directory loop");
360 return -EIO;
361 }
362
363 if (d_mountpoint(rep)) {
364 unlock_rename(cache->graveyard, dir);
365 cachefiles_io_error(cache, "Mountpoint in cache");
366 return -EIO;
367 }
368
369 grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
370 if (IS_ERR(grave)) {
371 unlock_rename(cache->graveyard, dir);
372
373 if (PTR_ERR(grave) == -ENOMEM) {
374 _leave(" = -ENOMEM");
375 return -ENOMEM;
376 }
377
378 cachefiles_io_error(cache, "Lookup error %ld",
379 PTR_ERR(grave));
380 return -EIO;
381 }
382
David Howells466b77b2015-03-17 22:26:21 +0000383 if (d_is_positive(grave)) {
David Howells9ae326a2009-04-03 16:42:41 +0100384 unlock_rename(cache->graveyard, dir);
385 dput(grave);
386 grave = NULL;
387 cond_resched();
388 goto try_again;
389 }
390
391 if (d_mountpoint(grave)) {
392 unlock_rename(cache->graveyard, dir);
393 dput(grave);
394 cachefiles_io_error(cache, "Mountpoint in graveyard");
395 return -EIO;
396 }
397
398 /* target should not be an ancestor of source */
399 if (trap == grave) {
400 unlock_rename(cache->graveyard, dir);
401 dput(grave);
402 cachefiles_io_error(cache, "May not make directory loop");
403 return -EIO;
404 }
405
406 /* attempt the rename */
David Howells82140442010-12-24 14:48:35 +0000407 path.mnt = cache->mnt;
408 path.dentry = dir;
409 path_to_graveyard.mnt = cache->mnt;
410 path_to_graveyard.dentry = cache->graveyard;
Miklos Szeredi0b3974e2014-04-01 17:08:43 +0200411 ret = security_path_rename(&path, rep, &path_to_graveyard, grave, 0);
David Howells82140442010-12-24 14:48:35 +0000412 if (ret < 0) {
413 cachefiles_io_error(cache, "Rename security error %d", ret);
414 } else {
David Howellsa18feb52018-04-04 13:41:27 +0100415 trace_cachefiles_rename(object, rep, grave, why);
David Howells5153bc82015-03-06 14:08:58 +0000416 ret = vfs_rename(d_inode(dir), rep,
417 d_inode(cache->graveyard), grave, NULL, 0);
David Howells82140442010-12-24 14:48:35 +0000418 if (ret != 0 && ret != -ENOMEM)
419 cachefiles_io_error(cache,
420 "Rename failed with error %d", ret);
David Howells9ae326a2009-04-03 16:42:41 +0100421
David Howells82140442010-12-24 14:48:35 +0000422 if (preemptive)
David Howells182d9192015-02-19 23:47:31 +0000423 cachefiles_mark_object_buried(cache, rep, why);
David Howells82140442010-12-24 14:48:35 +0000424 }
David Howellsc61ea312010-05-11 16:51:39 +0100425
David Howells9ae326a2009-04-03 16:42:41 +0100426 unlock_rename(cache->graveyard, dir);
427 dput(grave);
428 _leave(" = 0");
429 return 0;
430}
431
432/*
433 * delete an object representation from the cache
434 */
435int cachefiles_delete_object(struct cachefiles_cache *cache,
436 struct cachefiles_object *object)
437{
438 struct dentry *dir;
439 int ret;
440
David Howellsc61ea312010-05-11 16:51:39 +0100441 _enter(",OBJ%x{%p}", object->fscache.debug_id, object->dentry);
David Howells9ae326a2009-04-03 16:42:41 +0100442
443 ASSERT(object->dentry);
David Howells466b77b2015-03-17 22:26:21 +0000444 ASSERT(d_backing_inode(object->dentry));
David Howells9ae326a2009-04-03 16:42:41 +0100445 ASSERT(object->dentry->d_parent);
446
447 dir = dget_parent(object->dentry);
448
Al Viro59551022016-01-22 15:40:57 -0500449 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
David Howells8f9941a2010-02-19 18:14:21 +0000450
David Howells182d9192015-02-19 23:47:31 +0000451 if (test_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->fscache.flags)) {
David Howellsc61ea312010-05-11 16:51:39 +0100452 /* object allocation for the same key preemptively deleted this
453 * object's file so that it could create its own file */
454 _debug("object preemptively buried");
Al Viro59551022016-01-22 15:40:57 -0500455 inode_unlock(d_inode(dir));
David Howells8f9941a2010-02-19 18:14:21 +0000456 ret = 0;
David Howellsc61ea312010-05-11 16:51:39 +0100457 } else {
458 /* we need to check that our parent is _still_ our parent - it
459 * may have been renamed */
460 if (dir == object->dentry->d_parent) {
David Howellsa18feb52018-04-04 13:41:27 +0100461 ret = cachefiles_bury_object(cache, object, dir,
David Howells182d9192015-02-19 23:47:31 +0000462 object->dentry, false,
463 FSCACHE_OBJECT_WAS_RETIRED);
David Howellsc61ea312010-05-11 16:51:39 +0100464 } else {
465 /* it got moved, presumably by cachefilesd culling it,
466 * so it's no longer in the key path and we can ignore
467 * it */
Al Viro59551022016-01-22 15:40:57 -0500468 inode_unlock(d_inode(dir));
David Howellsc61ea312010-05-11 16:51:39 +0100469 ret = 0;
470 }
David Howells8f9941a2010-02-19 18:14:21 +0000471 }
David Howells9ae326a2009-04-03 16:42:41 +0100472
473 dput(dir);
474 _leave(" = %d", ret);
475 return ret;
476}
477
478/*
479 * walk from the parent object to the child object through the backing
480 * filesystem, creating directories as we go
481 */
482int cachefiles_walk_to_object(struct cachefiles_object *parent,
483 struct cachefiles_object *object,
484 const char *key,
485 struct cachefiles_xattr *auxdata)
486{
487 struct cachefiles_cache *cache;
488 struct dentry *dir, *next = NULL;
David Howellsa18feb52018-04-04 13:41:27 +0100489 struct inode *inode;
David Howells82140442010-12-24 14:48:35 +0000490 struct path path;
David Howells9ae326a2009-04-03 16:42:41 +0100491 unsigned long start;
492 const char *name;
493 int ret, nlen;
494
David Howellsc61ea312010-05-11 16:51:39 +0100495 _enter("OBJ%x{%p},OBJ%x,%s,",
496 parent->fscache.debug_id, parent->dentry,
497 object->fscache.debug_id, key);
David Howells9ae326a2009-04-03 16:42:41 +0100498
499 cache = container_of(parent->fscache.cache,
500 struct cachefiles_cache, cache);
David Howells82140442010-12-24 14:48:35 +0000501 path.mnt = cache->mnt;
David Howells9ae326a2009-04-03 16:42:41 +0100502
503 ASSERT(parent->dentry);
David Howells466b77b2015-03-17 22:26:21 +0000504 ASSERT(d_backing_inode(parent->dentry));
David Howells9ae326a2009-04-03 16:42:41 +0100505
David Howellse36cb0b2015-01-29 12:02:35 +0000506 if (!(d_is_dir(parent->dentry))) {
David Howells9ae326a2009-04-03 16:42:41 +0100507 // TODO: convert file to dir
508 _leave("looking up in none directory");
509 return -ENOBUFS;
510 }
511
512 dir = dget(parent->dentry);
513
514advance:
515 /* attempt to transit the first directory component */
516 name = key;
517 nlen = strlen(key);
518
519 /* key ends in a double NUL */
520 key = key + nlen + 1;
521 if (!*key)
522 key = NULL;
523
524lookup_again:
525 /* search the current directory for the element name */
526 _debug("lookup '%s'", name);
527
Al Viro59551022016-01-22 15:40:57 -0500528 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
David Howells9ae326a2009-04-03 16:42:41 +0100529
530 start = jiffies;
531 next = lookup_one_len(name, dir, nlen);
532 cachefiles_hist(cachefiles_lookup_histogram, start);
David Howellsa18feb52018-04-04 13:41:27 +0100533 if (IS_ERR(next)) {
534 trace_cachefiles_lookup(object, next, NULL);
David Howells9ae326a2009-04-03 16:42:41 +0100535 goto lookup_error;
David Howellsa18feb52018-04-04 13:41:27 +0100536 }
David Howells9ae326a2009-04-03 16:42:41 +0100537
David Howellsa18feb52018-04-04 13:41:27 +0100538 inode = d_backing_inode(next);
539 trace_cachefiles_lookup(object, next, inode);
540 _debug("next -> %p %s", next, inode ? "positive" : "negative");
David Howells9ae326a2009-04-03 16:42:41 +0100541
542 if (!key)
David Howellsa18feb52018-04-04 13:41:27 +0100543 object->new = !inode;
David Howells9ae326a2009-04-03 16:42:41 +0100544
545 /* if this element of the path doesn't exist, then the lookup phase
546 * failed, and we can release any readers in the certain knowledge that
547 * there's nothing for them to actually read */
David Howells466b77b2015-03-17 22:26:21 +0000548 if (d_is_negative(next))
David Howells9ae326a2009-04-03 16:42:41 +0100549 fscache_object_lookup_negative(&object->fscache);
550
551 /* we need to create the object if it's negative */
552 if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
553 /* index objects and intervening tree levels must be subdirs */
David Howells466b77b2015-03-17 22:26:21 +0000554 if (d_is_negative(next)) {
David Howells9ae326a2009-04-03 16:42:41 +0100555 ret = cachefiles_has_space(cache, 1, 0);
556 if (ret < 0)
David Howells182d9192015-02-19 23:47:31 +0000557 goto no_space_error;
David Howells9ae326a2009-04-03 16:42:41 +0100558
David Howells82140442010-12-24 14:48:35 +0000559 path.dentry = dir;
560 ret = security_path_mkdir(&path, next, 0);
561 if (ret < 0)
562 goto create_error;
David Howells9ae326a2009-04-03 16:42:41 +0100563 start = jiffies;
David Howells5153bc82015-03-06 14:08:58 +0000564 ret = vfs_mkdir(d_inode(dir), next, 0);
David Howells9ae326a2009-04-03 16:42:41 +0100565 cachefiles_hist(cachefiles_mkdir_histogram, start);
David Howellsa18feb52018-04-04 13:41:27 +0100566 if (!key)
567 trace_cachefiles_mkdir(object, next, ret);
David Howells9ae326a2009-04-03 16:42:41 +0100568 if (ret < 0)
569 goto create_error;
570
Al Viro9c3e9022018-05-10 22:59:45 -0400571 if (unlikely(d_unhashed(next))) {
572 dput(next);
573 inode_unlock(d_inode(dir));
574 goto lookup_again;
575 }
David Howells466b77b2015-03-17 22:26:21 +0000576 ASSERT(d_backing_inode(next));
David Howells9ae326a2009-04-03 16:42:41 +0100577
578 _debug("mkdir -> %p{%p{ino=%lu}}",
David Howells466b77b2015-03-17 22:26:21 +0000579 next, d_backing_inode(next), d_backing_inode(next)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100580
David Howellsce40fa72015-01-29 12:02:36 +0000581 } else if (!d_can_lookup(next)) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700582 pr_err("inode %lu is not a directory\n",
David Howells466b77b2015-03-17 22:26:21 +0000583 d_backing_inode(next)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100584 ret = -ENOBUFS;
585 goto error;
586 }
587
588 } else {
589 /* non-index objects start out life as files */
David Howells466b77b2015-03-17 22:26:21 +0000590 if (d_is_negative(next)) {
David Howells9ae326a2009-04-03 16:42:41 +0100591 ret = cachefiles_has_space(cache, 1, 0);
592 if (ret < 0)
David Howells182d9192015-02-19 23:47:31 +0000593 goto no_space_error;
David Howells9ae326a2009-04-03 16:42:41 +0100594
David Howells82140442010-12-24 14:48:35 +0000595 path.dentry = dir;
596 ret = security_path_mknod(&path, next, S_IFREG, 0);
597 if (ret < 0)
598 goto create_error;
David Howells9ae326a2009-04-03 16:42:41 +0100599 start = jiffies;
David Howells5153bc82015-03-06 14:08:58 +0000600 ret = vfs_create(d_inode(dir), next, S_IFREG, true);
David Howells9ae326a2009-04-03 16:42:41 +0100601 cachefiles_hist(cachefiles_create_histogram, start);
David Howellsa18feb52018-04-04 13:41:27 +0100602 trace_cachefiles_create(object, next, ret);
David Howells9ae326a2009-04-03 16:42:41 +0100603 if (ret < 0)
604 goto create_error;
605
David Howells466b77b2015-03-17 22:26:21 +0000606 ASSERT(d_backing_inode(next));
David Howells9ae326a2009-04-03 16:42:41 +0100607
608 _debug("create -> %p{%p{ino=%lu}}",
David Howells466b77b2015-03-17 22:26:21 +0000609 next, d_backing_inode(next), d_backing_inode(next)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100610
David Howellsce40fa72015-01-29 12:02:36 +0000611 } else if (!d_can_lookup(next) &&
David Howellse36cb0b2015-01-29 12:02:35 +0000612 !d_is_reg(next)
David Howells9ae326a2009-04-03 16:42:41 +0100613 ) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700614 pr_err("inode %lu is not a file or directory\n",
David Howells466b77b2015-03-17 22:26:21 +0000615 d_backing_inode(next)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100616 ret = -ENOBUFS;
617 goto error;
618 }
619 }
620
621 /* process the next component */
622 if (key) {
623 _debug("advance");
Al Viro59551022016-01-22 15:40:57 -0500624 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100625 dput(dir);
626 dir = next;
627 next = NULL;
628 goto advance;
629 }
630
631 /* we've found the object we were looking for */
632 object->dentry = next;
633
634 /* if we've found that the terminal object exists, then we need to
635 * check its attributes and delete it if it's out of date */
636 if (!object->new) {
Al Viroa4555892014-10-21 20:11:25 -0400637 _debug("validate '%pd'", next);
David Howells9ae326a2009-04-03 16:42:41 +0100638
639 ret = cachefiles_check_object_xattr(object, auxdata);
640 if (ret == -ESTALE) {
641 /* delete the object (the deleter drops the directory
642 * mutex) */
643 object->dentry = NULL;
644
David Howellsa18feb52018-04-04 13:41:27 +0100645 ret = cachefiles_bury_object(cache, object, dir, next,
646 true,
David Howells182d9192015-02-19 23:47:31 +0000647 FSCACHE_OBJECT_IS_STALE);
David Howells9ae326a2009-04-03 16:42:41 +0100648 dput(next);
649 next = NULL;
650
651 if (ret < 0)
652 goto delete_error;
653
654 _debug("redo lookup");
David Howells182d9192015-02-19 23:47:31 +0000655 fscache_object_retrying_stale(&object->fscache);
David Howells9ae326a2009-04-03 16:42:41 +0100656 goto lookup_again;
657 }
658 }
659
660 /* note that we're now using this object */
David Howellsfee096d2009-11-19 18:12:05 +0000661 ret = cachefiles_mark_object_active(cache, object);
David Howells9ae326a2009-04-03 16:42:41 +0100662
Al Viro59551022016-01-22 15:40:57 -0500663 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100664 dput(dir);
665 dir = NULL;
666
David Howellsfee096d2009-11-19 18:12:05 +0000667 if (ret == -ETIMEDOUT)
668 goto mark_active_timed_out;
669
David Howells9ae326a2009-04-03 16:42:41 +0100670 _debug("=== OBTAINED_OBJECT ===");
671
672 if (object->new) {
673 /* attach data to a newly constructed terminal object */
674 ret = cachefiles_set_object_xattr(object, auxdata);
675 if (ret < 0)
676 goto check_error;
677 } else {
678 /* always update the atime on an object we've just looked up
679 * (this is used to keep track of culling, and atimes are only
680 * updated by read, write and readdir but not lookup or
681 * open) */
Al Viro68ac1232012-03-15 08:21:57 -0400682 path.dentry = next;
683 touch_atime(&path);
David Howells9ae326a2009-04-03 16:42:41 +0100684 }
685
686 /* open a file interface onto a data file */
687 if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
David Howellse36cb0b2015-01-29 12:02:35 +0000688 if (d_is_reg(object->dentry)) {
David Howells9ae326a2009-04-03 16:42:41 +0100689 const struct address_space_operations *aops;
690
691 ret = -EPERM;
David Howells466b77b2015-03-17 22:26:21 +0000692 aops = d_backing_inode(object->dentry)->i_mapping->a_ops;
David Howells9ae326a2009-04-03 16:42:41 +0100693 if (!aops->bmap)
694 goto check_error;
NeilBrown95201a42015-11-04 15:20:34 +0000695 if (object->dentry->d_sb->s_blocksize > PAGE_SIZE)
696 goto check_error;
David Howells9ae326a2009-04-03 16:42:41 +0100697
698 object->backer = object->dentry;
699 } else {
700 BUG(); // TODO: open file in data-class subdir
701 }
702 }
703
704 object->new = 0;
705 fscache_obtained_object(&object->fscache);
706
David Howells466b77b2015-03-17 22:26:21 +0000707 _leave(" = 0 [%lu]", d_backing_inode(object->dentry)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100708 return 0;
709
David Howells182d9192015-02-19 23:47:31 +0000710no_space_error:
711 fscache_object_mark_killed(&object->fscache, FSCACHE_OBJECT_NO_SPACE);
David Howells9ae326a2009-04-03 16:42:41 +0100712create_error:
713 _debug("create error %d", ret);
714 if (ret == -EIO)
715 cachefiles_io_error(cache, "Create/mkdir failed");
716 goto error;
717
David Howellsfee096d2009-11-19 18:12:05 +0000718mark_active_timed_out:
719 _debug("mark active timed out");
720 goto release_dentry;
721
David Howells9ae326a2009-04-03 16:42:41 +0100722check_error:
723 _debug("check error %d", ret);
David Howellsa8181012016-08-09 17:41:16 +0100724 cachefiles_mark_object_inactive(
725 cache, object, d_backing_inode(object->dentry)->i_blocks);
David Howellsfee096d2009-11-19 18:12:05 +0000726release_dentry:
David Howells9ae326a2009-04-03 16:42:41 +0100727 dput(object->dentry);
728 object->dentry = NULL;
729 goto error_out;
730
731delete_error:
732 _debug("delete error %d", ret);
733 goto error_out2;
734
735lookup_error:
736 _debug("lookup error %ld", PTR_ERR(next));
737 ret = PTR_ERR(next);
738 if (ret == -EIO)
739 cachefiles_io_error(cache, "Lookup failed");
740 next = NULL;
741error:
Al Viro59551022016-01-22 15:40:57 -0500742 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100743 dput(next);
744error_out2:
745 dput(dir);
746error_out:
David Howells9ae326a2009-04-03 16:42:41 +0100747 _leave(" = error %d", -ret);
748 return ret;
749}
750
751/*
752 * get a subdirectory
753 */
754struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
755 struct dentry *dir,
756 const char *dirname)
757{
758 struct dentry *subdir;
759 unsigned long start;
David Howells82140442010-12-24 14:48:35 +0000760 struct path path;
David Howells9ae326a2009-04-03 16:42:41 +0100761 int ret;
762
763 _enter(",,%s", dirname);
764
765 /* search the current directory for the element name */
Al Viro59551022016-01-22 15:40:57 -0500766 inode_lock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100767
Al Viro9c3e9022018-05-10 22:59:45 -0400768retry:
David Howells9ae326a2009-04-03 16:42:41 +0100769 start = jiffies;
770 subdir = lookup_one_len(dirname, dir, strlen(dirname));
771 cachefiles_hist(cachefiles_lookup_histogram, start);
772 if (IS_ERR(subdir)) {
773 if (PTR_ERR(subdir) == -ENOMEM)
774 goto nomem_d_alloc;
775 goto lookup_error;
776 }
777
778 _debug("subdir -> %p %s",
David Howells466b77b2015-03-17 22:26:21 +0000779 subdir, d_backing_inode(subdir) ? "positive" : "negative");
David Howells9ae326a2009-04-03 16:42:41 +0100780
781 /* we need to create the subdir if it doesn't exist yet */
David Howells466b77b2015-03-17 22:26:21 +0000782 if (d_is_negative(subdir)) {
David Howells9ae326a2009-04-03 16:42:41 +0100783 ret = cachefiles_has_space(cache, 1, 0);
784 if (ret < 0)
785 goto mkdir_error;
786
787 _debug("attempt mkdir");
788
David Howells82140442010-12-24 14:48:35 +0000789 path.mnt = cache->mnt;
790 path.dentry = dir;
791 ret = security_path_mkdir(&path, subdir, 0700);
792 if (ret < 0)
793 goto mkdir_error;
David Howells5153bc82015-03-06 14:08:58 +0000794 ret = vfs_mkdir(d_inode(dir), subdir, 0700);
David Howells9ae326a2009-04-03 16:42:41 +0100795 if (ret < 0)
796 goto mkdir_error;
797
Al Viro9c3e9022018-05-10 22:59:45 -0400798 if (unlikely(d_unhashed(subdir))) {
799 dput(subdir);
800 goto retry;
801 }
David Howells466b77b2015-03-17 22:26:21 +0000802 ASSERT(d_backing_inode(subdir));
David Howells9ae326a2009-04-03 16:42:41 +0100803
804 _debug("mkdir -> %p{%p{ino=%lu}}",
805 subdir,
David Howells466b77b2015-03-17 22:26:21 +0000806 d_backing_inode(subdir),
807 d_backing_inode(subdir)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100808 }
809
Al Viro59551022016-01-22 15:40:57 -0500810 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100811
812 /* we need to make sure the subdir is a directory */
David Howells466b77b2015-03-17 22:26:21 +0000813 ASSERT(d_backing_inode(subdir));
David Howells9ae326a2009-04-03 16:42:41 +0100814
David Howellsce40fa72015-01-29 12:02:36 +0000815 if (!d_can_lookup(subdir)) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700816 pr_err("%s is not a directory\n", dirname);
David Howells9ae326a2009-04-03 16:42:41 +0100817 ret = -EIO;
818 goto check_error;
819 }
820
821 ret = -EPERM;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200822 if (!(d_backing_inode(subdir)->i_opflags & IOP_XATTR) ||
David Howells466b77b2015-03-17 22:26:21 +0000823 !d_backing_inode(subdir)->i_op->lookup ||
824 !d_backing_inode(subdir)->i_op->mkdir ||
825 !d_backing_inode(subdir)->i_op->create ||
Miklos Szeredi2773bf02016-09-27 11:03:58 +0200826 !d_backing_inode(subdir)->i_op->rename ||
David Howells466b77b2015-03-17 22:26:21 +0000827 !d_backing_inode(subdir)->i_op->rmdir ||
828 !d_backing_inode(subdir)->i_op->unlink)
David Howells9ae326a2009-04-03 16:42:41 +0100829 goto check_error;
830
David Howells466b77b2015-03-17 22:26:21 +0000831 _leave(" = [%lu]", d_backing_inode(subdir)->i_ino);
David Howells9ae326a2009-04-03 16:42:41 +0100832 return subdir;
833
834check_error:
835 dput(subdir);
836 _leave(" = %d [check]", ret);
837 return ERR_PTR(ret);
838
839mkdir_error:
Al Viro59551022016-01-22 15:40:57 -0500840 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100841 dput(subdir);
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700842 pr_err("mkdir %s failed with error %d\n", dirname, ret);
David Howells9ae326a2009-04-03 16:42:41 +0100843 return ERR_PTR(ret);
844
845lookup_error:
Al Viro59551022016-01-22 15:40:57 -0500846 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100847 ret = PTR_ERR(subdir);
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700848 pr_err("Lookup %s failed with error %d\n", dirname, ret);
David Howells9ae326a2009-04-03 16:42:41 +0100849 return ERR_PTR(ret);
850
851nomem_d_alloc:
Al Viro59551022016-01-22 15:40:57 -0500852 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100853 _leave(" = -ENOMEM");
854 return ERR_PTR(-ENOMEM);
855}
856
857/*
858 * find out if an object is in use or not
859 * - if finds object and it's not in use:
860 * - returns a pointer to the object and a reference on it
861 * - returns with the directory locked
862 */
863static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
864 struct dentry *dir,
865 char *filename)
866{
867 struct cachefiles_object *object;
868 struct rb_node *_n;
869 struct dentry *victim;
870 unsigned long start;
871 int ret;
872
Al Viroa4555892014-10-21 20:11:25 -0400873 //_enter(",%pd/,%s",
874 // dir, filename);
David Howells9ae326a2009-04-03 16:42:41 +0100875
876 /* look up the victim */
Al Viro59551022016-01-22 15:40:57 -0500877 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
David Howells9ae326a2009-04-03 16:42:41 +0100878
879 start = jiffies;
880 victim = lookup_one_len(filename, dir, strlen(filename));
881 cachefiles_hist(cachefiles_lookup_histogram, start);
882 if (IS_ERR(victim))
883 goto lookup_error;
884
885 //_debug("victim -> %p %s",
David Howells466b77b2015-03-17 22:26:21 +0000886 // victim, d_backing_inode(victim) ? "positive" : "negative");
David Howells9ae326a2009-04-03 16:42:41 +0100887
888 /* if the object is no longer there then we probably retired the object
889 * at the netfs's request whilst the cull was in progress
890 */
David Howells466b77b2015-03-17 22:26:21 +0000891 if (d_is_negative(victim)) {
Al Viro59551022016-01-22 15:40:57 -0500892 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100893 dput(victim);
894 _leave(" = -ENOENT [absent]");
895 return ERR_PTR(-ENOENT);
896 }
897
898 /* check to see if we're using this object */
899 read_lock(&cache->active_lock);
900
901 _n = cache->active_nodes.rb_node;
902
903 while (_n) {
904 object = rb_entry(_n, struct cachefiles_object, active_node);
905
906 if (object->dentry > victim)
907 _n = _n->rb_left;
908 else if (object->dentry < victim)
909 _n = _n->rb_right;
910 else
911 goto object_in_use;
912 }
913
914 read_unlock(&cache->active_lock);
915
916 //_leave(" = %p", victim);
917 return victim;
918
919object_in_use:
920 read_unlock(&cache->active_lock);
Al Viro59551022016-01-22 15:40:57 -0500921 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100922 dput(victim);
923 //_leave(" = -EBUSY [in use]");
924 return ERR_PTR(-EBUSY);
925
926lookup_error:
Al Viro59551022016-01-22 15:40:57 -0500927 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100928 ret = PTR_ERR(victim);
929 if (ret == -ENOENT) {
930 /* file or dir now absent - probably retired by netfs */
931 _leave(" = -ESTALE [absent]");
932 return ERR_PTR(-ESTALE);
933 }
934
935 if (ret == -EIO) {
936 cachefiles_io_error(cache, "Lookup failed");
937 } else if (ret != -ENOMEM) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700938 pr_err("Internal error: %d\n", ret);
David Howells9ae326a2009-04-03 16:42:41 +0100939 ret = -EIO;
940 }
941
942 _leave(" = %d", ret);
943 return ERR_PTR(ret);
944}
945
946/*
947 * cull an object if it's not in use
948 * - called only by cache manager daemon
949 */
950int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
951 char *filename)
952{
953 struct dentry *victim;
954 int ret;
955
Al Viroa4555892014-10-21 20:11:25 -0400956 _enter(",%pd/,%s", dir, filename);
David Howells9ae326a2009-04-03 16:42:41 +0100957
958 victim = cachefiles_check_active(cache, dir, filename);
959 if (IS_ERR(victim))
960 return PTR_ERR(victim);
961
962 _debug("victim -> %p %s",
David Howells466b77b2015-03-17 22:26:21 +0000963 victim, d_backing_inode(victim) ? "positive" : "negative");
David Howells9ae326a2009-04-03 16:42:41 +0100964
965 /* okay... the victim is not being used so we can cull it
966 * - start by marking it as stale
967 */
968 _debug("victim is cullable");
969
970 ret = cachefiles_remove_object_xattr(cache, victim);
971 if (ret < 0)
972 goto error_unlock;
973
974 /* actually remove the victim (drops the dir mutex) */
975 _debug("bury");
976
David Howellsa18feb52018-04-04 13:41:27 +0100977 ret = cachefiles_bury_object(cache, NULL, dir, victim, false,
David Howells182d9192015-02-19 23:47:31 +0000978 FSCACHE_OBJECT_WAS_CULLED);
David Howells9ae326a2009-04-03 16:42:41 +0100979 if (ret < 0)
980 goto error;
981
982 dput(victim);
983 _leave(" = 0");
984 return 0;
985
986error_unlock:
Al Viro59551022016-01-22 15:40:57 -0500987 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +0100988error:
989 dput(victim);
990 if (ret == -ENOENT) {
991 /* file or dir now absent - probably retired by netfs */
992 _leave(" = -ESTALE [absent]");
993 return -ESTALE;
994 }
995
996 if (ret != -ENOMEM) {
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700997 pr_err("Internal error: %d\n", ret);
David Howells9ae326a2009-04-03 16:42:41 +0100998 ret = -EIO;
999 }
1000
1001 _leave(" = %d", ret);
1002 return ret;
1003}
1004
1005/*
1006 * find out if an object is in use or not
1007 * - called only by cache manager daemon
1008 * - returns -EBUSY or 0 to indicate whether an object is in use or not
1009 */
1010int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
1011 char *filename)
1012{
1013 struct dentry *victim;
1014
Al Viroa4555892014-10-21 20:11:25 -04001015 //_enter(",%pd/,%s",
1016 // dir, filename);
David Howells9ae326a2009-04-03 16:42:41 +01001017
1018 victim = cachefiles_check_active(cache, dir, filename);
1019 if (IS_ERR(victim))
1020 return PTR_ERR(victim);
1021
Al Viro59551022016-01-22 15:40:57 -05001022 inode_unlock(d_inode(dir));
David Howells9ae326a2009-04-03 16:42:41 +01001023 dput(victim);
1024 //_leave(" = 0");
1025 return 0;
1026}