David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 23 | #include "internal.h" |
| 24 | |
David Howells | d0e27b7 | 2009-11-19 18:12:02 +0000 | [diff] [blame] | 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 | |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 38 | pr_err("%sobject: OBJ%x\n", prefix, object->fscache.debug_id); |
| 39 | pr_err("%sobjstate=%s fl=%lx wbusy=%x ev=%lx[%lx]\n", |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 40 | prefix, object->fscache.state->name, |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 41 | object->fscache.flags, work_busy(&object->fscache.work), |
David Howells | c2d35bf | 2012-12-05 13:34:47 +0000 | [diff] [blame] | 42 | object->fscache.events, object->fscache.event_mask); |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 43 | pr_err("%sops=%u inp=%u exc=%u\n", |
David Howells | d0e27b7 | 2009-11-19 18:12:02 +0000 | [diff] [blame] | 44 | prefix, object->fscache.n_ops, object->fscache.n_in_progress, |
| 45 | object->fscache.n_exclusive); |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 46 | pr_err("%sparent=%p\n", |
David Howells | d0e27b7 | 2009-11-19 18:12:02 +0000 | [diff] [blame] | 47 | prefix, object->fscache.parent); |
| 48 | |
| 49 | spin_lock(&object->fscache.lock); |
| 50 | cookie = object->fscache.cookie; |
| 51 | if (cookie) { |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 52 | pr_err("%scookie=%p [pr=%p nd=%p fl=%lx]\n", |
David Howells | d0e27b7 | 2009-11-19 18:12:02 +0000 | [diff] [blame] | 53 | prefix, |
| 54 | object->fscache.cookie, |
| 55 | object->fscache.cookie->parent, |
| 56 | object->fscache.cookie->netfs_data, |
| 57 | object->fscache.cookie->flags); |
David Howells | 509bf24 | 2013-09-20 14:18:00 +0100 | [diff] [blame] | 58 | if (keybuf && cookie->def) |
David Howells | d0e27b7 | 2009-11-19 18:12:02 +0000 | [diff] [blame] | 59 | keylen = cookie->def->get_key(cookie->netfs_data, keybuf, |
| 60 | CACHEFILES_KEYBUF_SIZE); |
| 61 | else |
| 62 | keylen = 0; |
| 63 | } else { |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 64 | pr_err("%scookie=NULL\n", prefix); |
David Howells | d0e27b7 | 2009-11-19 18:12:02 +0000 | [diff] [blame] | 65 | keylen = 0; |
| 66 | } |
| 67 | spin_unlock(&object->fscache.lock); |
| 68 | |
| 69 | if (keylen) { |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 70 | pr_err("%skey=[%u] '", prefix, keylen); |
David Howells | d0e27b7 | 2009-11-19 18:12:02 +0000 | [diff] [blame] | 71 | for (loop = 0; loop < keylen; loop++) |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 72 | pr_cont("%02x", keybuf[loop]); |
| 73 | pr_cont("'\n"); |
David Howells | d0e27b7 | 2009-11-19 18:12:02 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * dump debugging info about a pair of objects |
| 79 | */ |
| 80 | static noinline void cachefiles_printk_object(struct cachefiles_object *object, |
| 81 | struct cachefiles_object *xobject) |
| 82 | { |
| 83 | u8 *keybuf; |
| 84 | |
| 85 | keybuf = kmalloc(CACHEFILES_KEYBUF_SIZE, GFP_NOIO); |
| 86 | if (object) |
| 87 | __cachefiles_printk_object(object, "", keybuf); |
| 88 | if (xobject) |
| 89 | __cachefiles_printk_object(xobject, "x", keybuf); |
| 90 | kfree(keybuf); |
| 91 | } |
| 92 | |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 93 | /* |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 94 | * mark the owner of a dentry, if there is one, to indicate that that dentry |
| 95 | * has been preemptively deleted |
| 96 | * - the caller must hold the i_mutex on the dentry's parent as required to |
| 97 | * call vfs_unlink(), vfs_rmdir() or vfs_rename() |
| 98 | */ |
| 99 | static void cachefiles_mark_object_buried(struct cachefiles_cache *cache, |
| 100 | struct dentry *dentry) |
| 101 | { |
| 102 | struct cachefiles_object *object; |
| 103 | struct rb_node *p; |
| 104 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 105 | _enter(",'%pd'", dentry); |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 106 | |
| 107 | write_lock(&cache->active_lock); |
| 108 | |
| 109 | p = cache->active_nodes.rb_node; |
| 110 | while (p) { |
| 111 | object = rb_entry(p, struct cachefiles_object, active_node); |
| 112 | if (object->dentry > dentry) |
| 113 | p = p->rb_left; |
| 114 | else if (object->dentry < dentry) |
| 115 | p = p->rb_right; |
| 116 | else |
| 117 | goto found_dentry; |
| 118 | } |
| 119 | |
| 120 | write_unlock(&cache->active_lock); |
| 121 | _leave(" [no owner]"); |
| 122 | return; |
| 123 | |
| 124 | /* found the dentry for */ |
| 125 | found_dentry: |
| 126 | kdebug("preemptive burial: OBJ%x [%s] %p", |
| 127 | object->fscache.debug_id, |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 128 | object->fscache.state->name, |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 129 | dentry); |
| 130 | |
David Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 131 | if (fscache_object_is_live(&object->fscache)) { |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 132 | pr_err("\n"); |
Fabian Frederick | 0227d6ab | 2014-06-06 14:37:33 -0700 | [diff] [blame] | 133 | pr_err("Error: Can't preemptively bury live object\n"); |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 134 | cachefiles_printk_object(object, NULL); |
| 135 | } else if (test_and_set_bit(CACHEFILES_OBJECT_BURIED, &object->flags)) { |
Fabian Frederick | 0227d6ab | 2014-06-06 14:37:33 -0700 | [diff] [blame] | 136 | pr_err("Error: Object already preemptively buried\n"); |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | write_unlock(&cache->active_lock); |
| 140 | _leave(" [owner marked]"); |
| 141 | } |
| 142 | |
| 143 | /* |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 144 | * record the fact that an object is now active |
| 145 | */ |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 146 | static int cachefiles_mark_object_active(struct cachefiles_cache *cache, |
| 147 | struct cachefiles_object *object) |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 148 | { |
| 149 | struct cachefiles_object *xobject; |
| 150 | struct rb_node **_p, *_parent = NULL; |
| 151 | struct dentry *dentry; |
| 152 | |
| 153 | _enter(",%p", object); |
| 154 | |
| 155 | try_again: |
| 156 | write_lock(&cache->active_lock); |
| 157 | |
David Howells | d0e27b7 | 2009-11-19 18:12:02 +0000 | [diff] [blame] | 158 | if (test_and_set_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) { |
Fabian Frederick | 0227d6ab | 2014-06-06 14:37:33 -0700 | [diff] [blame] | 159 | pr_err("Error: Object already active\n"); |
David Howells | d0e27b7 | 2009-11-19 18:12:02 +0000 | [diff] [blame] | 160 | cachefiles_printk_object(object, NULL); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 161 | BUG(); |
David Howells | d0e27b7 | 2009-11-19 18:12:02 +0000 | [diff] [blame] | 162 | } |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 163 | |
| 164 | dentry = object->dentry; |
| 165 | _p = &cache->active_nodes.rb_node; |
| 166 | while (*_p) { |
| 167 | _parent = *_p; |
| 168 | xobject = rb_entry(_parent, |
| 169 | struct cachefiles_object, active_node); |
| 170 | |
| 171 | ASSERT(xobject != object); |
| 172 | |
| 173 | if (xobject->dentry > dentry) |
| 174 | _p = &(*_p)->rb_left; |
| 175 | else if (xobject->dentry < dentry) |
| 176 | _p = &(*_p)->rb_right; |
| 177 | else |
| 178 | goto wait_for_old_object; |
| 179 | } |
| 180 | |
| 181 | rb_link_node(&object->active_node, _parent, _p); |
| 182 | rb_insert_color(&object->active_node, &cache->active_nodes); |
| 183 | |
| 184 | write_unlock(&cache->active_lock); |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 185 | _leave(" = 0"); |
| 186 | return 0; |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 187 | |
| 188 | /* an old object from a previous incarnation is hogging the slot - we |
| 189 | * need to wait for it to be destroyed */ |
| 190 | wait_for_old_object: |
David Howells | a30efe2 | 2014-09-30 14:50:30 +0100 | [diff] [blame] | 191 | if (fscache_object_is_live(&xobject->fscache)) { |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 192 | pr_err("\n"); |
Fabian Frederick | 0227d6ab | 2014-06-06 14:37:33 -0700 | [diff] [blame] | 193 | pr_err("Error: Unexpected object collision\n"); |
David Howells | d0e27b7 | 2009-11-19 18:12:02 +0000 | [diff] [blame] | 194 | cachefiles_printk_object(object, xobject); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 195 | BUG(); |
| 196 | } |
| 197 | atomic_inc(&xobject->usage); |
| 198 | write_unlock(&cache->active_lock); |
| 199 | |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 200 | if (test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) { |
| 201 | wait_queue_head_t *wq; |
| 202 | |
| 203 | signed long timeout = 60 * HZ; |
| 204 | wait_queue_t wait; |
| 205 | bool requeue; |
| 206 | |
| 207 | /* if the object we're waiting for is queued for processing, |
| 208 | * then just put ourselves on the queue behind it */ |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 209 | if (work_pending(&xobject->fscache.work)) { |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 210 | _debug("queue OBJ%x behind OBJ%x immediately", |
| 211 | object->fscache.debug_id, |
| 212 | xobject->fscache.debug_id); |
| 213 | goto requeue; |
| 214 | } |
| 215 | |
| 216 | /* otherwise we sleep until either the object we're waiting for |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 217 | * is done, or the fscache_object is congested */ |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 218 | wq = bit_waitqueue(&xobject->flags, CACHEFILES_OBJECT_ACTIVE); |
| 219 | init_wait(&wait); |
| 220 | requeue = false; |
| 221 | do { |
| 222 | prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE); |
| 223 | if (!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) |
| 224 | break; |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 225 | |
| 226 | requeue = fscache_object_sleep_till_congested(&timeout); |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 227 | } while (timeout > 0 && !requeue); |
| 228 | finish_wait(wq, &wait); |
| 229 | |
| 230 | if (requeue && |
| 231 | test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) { |
| 232 | _debug("queue OBJ%x behind OBJ%x after wait", |
| 233 | object->fscache.debug_id, |
| 234 | xobject->fscache.debug_id); |
| 235 | goto requeue; |
| 236 | } |
| 237 | |
| 238 | if (timeout <= 0) { |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 239 | pr_err("\n"); |
Fabian Frederick | 0227d6ab | 2014-06-06 14:37:33 -0700 | [diff] [blame] | 240 | pr_err("Error: Overlong wait for old active object to go away\n"); |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 241 | cachefiles_printk_object(object, xobject); |
| 242 | goto requeue; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 247 | |
| 248 | cache->cache.ops->put_object(&xobject->fscache); |
| 249 | goto try_again; |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 250 | |
| 251 | requeue: |
| 252 | clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags); |
| 253 | cache->cache.ops->put_object(&xobject->fscache); |
| 254 | _leave(" = -ETIMEDOUT"); |
| 255 | return -ETIMEDOUT; |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* |
| 259 | * delete an object representation from the cache |
| 260 | * - file backed objects are unlinked |
| 261 | * - directory backed objects are stuffed into the graveyard for userspace to |
| 262 | * delete |
| 263 | * - unlocks the directory mutex |
| 264 | */ |
| 265 | static int cachefiles_bury_object(struct cachefiles_cache *cache, |
| 266 | struct dentry *dir, |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 267 | struct dentry *rep, |
| 268 | bool preemptive) |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 269 | { |
| 270 | struct dentry *grave, *trap; |
David Howells | 8214044 | 2010-12-24 14:48:35 +0000 | [diff] [blame] | 271 | struct path path, path_to_graveyard; |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 272 | char nbuffer[8 + 8 + 1]; |
| 273 | int ret; |
| 274 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 275 | _enter(",'%pd','%pd'", dir, rep); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 276 | |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 277 | _debug("remove %p from %p", rep, dir); |
| 278 | |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 279 | /* non-directories can just be unlinked */ |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 280 | if (!d_is_dir(rep)) { |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 281 | _debug("unlink stale object"); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 282 | |
David Howells | 8214044 | 2010-12-24 14:48:35 +0000 | [diff] [blame] | 283 | path.mnt = cache->mnt; |
| 284 | path.dentry = dir; |
| 285 | ret = security_path_unlink(&path, rep); |
| 286 | if (ret < 0) { |
| 287 | cachefiles_io_error(cache, "Unlink security error"); |
| 288 | } else { |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 289 | ret = vfs_unlink(d_inode(dir), rep, NULL); |
David Howells | 8214044 | 2010-12-24 14:48:35 +0000 | [diff] [blame] | 290 | |
| 291 | if (preemptive) |
| 292 | cachefiles_mark_object_buried(cache, rep); |
| 293 | } |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 294 | |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 295 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 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"); |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 306 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 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 | |
David Howells | ce40fa7 | 2015-01-29 12:02:36 +0000 | [diff] [blame] | 326 | if (!d_can_lookup(cache->graveyard)) { |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 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 | |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 358 | if (d_is_positive(grave)) { |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 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 */ |
David Howells | 8214044 | 2010-12-24 14:48:35 +0000 | [diff] [blame] | 382 | path.mnt = cache->mnt; |
| 383 | path.dentry = dir; |
| 384 | path_to_graveyard.mnt = cache->mnt; |
| 385 | path_to_graveyard.dentry = cache->graveyard; |
Miklos Szeredi | 0b3974e | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 386 | ret = security_path_rename(&path, rep, &path_to_graveyard, grave, 0); |
David Howells | 8214044 | 2010-12-24 14:48:35 +0000 | [diff] [blame] | 387 | if (ret < 0) { |
| 388 | cachefiles_io_error(cache, "Rename security error %d", ret); |
| 389 | } else { |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 390 | ret = vfs_rename(d_inode(dir), rep, |
| 391 | d_inode(cache->graveyard), grave, NULL, 0); |
David Howells | 8214044 | 2010-12-24 14:48:35 +0000 | [diff] [blame] | 392 | if (ret != 0 && ret != -ENOMEM) |
| 393 | cachefiles_io_error(cache, |
| 394 | "Rename failed with error %d", ret); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 395 | |
David Howells | 8214044 | 2010-12-24 14:48:35 +0000 | [diff] [blame] | 396 | if (preemptive) |
| 397 | cachefiles_mark_object_buried(cache, rep); |
| 398 | } |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 399 | |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 400 | unlock_rename(cache->graveyard, dir); |
| 401 | dput(grave); |
| 402 | _leave(" = 0"); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * delete an object representation from the cache |
| 408 | */ |
| 409 | int cachefiles_delete_object(struct cachefiles_cache *cache, |
| 410 | struct cachefiles_object *object) |
| 411 | { |
| 412 | struct dentry *dir; |
| 413 | int ret; |
| 414 | |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 415 | _enter(",OBJ%x{%p}", object->fscache.debug_id, object->dentry); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 416 | |
| 417 | ASSERT(object->dentry); |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 418 | ASSERT(d_backing_inode(object->dentry)); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 419 | ASSERT(object->dentry->d_parent); |
| 420 | |
| 421 | dir = dget_parent(object->dentry); |
| 422 | |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 423 | mutex_lock_nested(&d_inode(dir)->i_mutex, I_MUTEX_PARENT); |
David Howells | 8f9941a | 2010-02-19 18:14:21 +0000 | [diff] [blame] | 424 | |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 425 | if (test_bit(CACHEFILES_OBJECT_BURIED, &object->flags)) { |
| 426 | /* object allocation for the same key preemptively deleted this |
| 427 | * object's file so that it could create its own file */ |
| 428 | _debug("object preemptively buried"); |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 429 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 8f9941a | 2010-02-19 18:14:21 +0000 | [diff] [blame] | 430 | ret = 0; |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 431 | } else { |
| 432 | /* we need to check that our parent is _still_ our parent - it |
| 433 | * may have been renamed */ |
| 434 | if (dir == object->dentry->d_parent) { |
| 435 | ret = cachefiles_bury_object(cache, dir, |
| 436 | object->dentry, false); |
| 437 | } else { |
| 438 | /* it got moved, presumably by cachefilesd culling it, |
| 439 | * so it's no longer in the key path and we can ignore |
| 440 | * it */ |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 441 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 442 | ret = 0; |
| 443 | } |
David Howells | 8f9941a | 2010-02-19 18:14:21 +0000 | [diff] [blame] | 444 | } |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 445 | |
| 446 | dput(dir); |
| 447 | _leave(" = %d", ret); |
| 448 | return ret; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * walk from the parent object to the child object through the backing |
| 453 | * filesystem, creating directories as we go |
| 454 | */ |
| 455 | int cachefiles_walk_to_object(struct cachefiles_object *parent, |
| 456 | struct cachefiles_object *object, |
| 457 | const char *key, |
| 458 | struct cachefiles_xattr *auxdata) |
| 459 | { |
| 460 | struct cachefiles_cache *cache; |
| 461 | struct dentry *dir, *next = NULL; |
David Howells | 8214044 | 2010-12-24 14:48:35 +0000 | [diff] [blame] | 462 | struct path path; |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 463 | unsigned long start; |
| 464 | const char *name; |
| 465 | int ret, nlen; |
| 466 | |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 467 | _enter("OBJ%x{%p},OBJ%x,%s,", |
| 468 | parent->fscache.debug_id, parent->dentry, |
| 469 | object->fscache.debug_id, key); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 470 | |
| 471 | cache = container_of(parent->fscache.cache, |
| 472 | struct cachefiles_cache, cache); |
David Howells | 8214044 | 2010-12-24 14:48:35 +0000 | [diff] [blame] | 473 | path.mnt = cache->mnt; |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 474 | |
| 475 | ASSERT(parent->dentry); |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 476 | ASSERT(d_backing_inode(parent->dentry)); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 477 | |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 478 | if (!(d_is_dir(parent->dentry))) { |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 479 | // TODO: convert file to dir |
| 480 | _leave("looking up in none directory"); |
| 481 | return -ENOBUFS; |
| 482 | } |
| 483 | |
| 484 | dir = dget(parent->dentry); |
| 485 | |
| 486 | advance: |
| 487 | /* attempt to transit the first directory component */ |
| 488 | name = key; |
| 489 | nlen = strlen(key); |
| 490 | |
| 491 | /* key ends in a double NUL */ |
| 492 | key = key + nlen + 1; |
| 493 | if (!*key) |
| 494 | key = NULL; |
| 495 | |
| 496 | lookup_again: |
| 497 | /* search the current directory for the element name */ |
| 498 | _debug("lookup '%s'", name); |
| 499 | |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 500 | mutex_lock_nested(&d_inode(dir)->i_mutex, I_MUTEX_PARENT); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 501 | |
| 502 | start = jiffies; |
| 503 | next = lookup_one_len(name, dir, nlen); |
| 504 | cachefiles_hist(cachefiles_lookup_histogram, start); |
| 505 | if (IS_ERR(next)) |
| 506 | goto lookup_error; |
| 507 | |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 508 | _debug("next -> %p %s", next, d_backing_inode(next) ? "positive" : "negative"); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 509 | |
| 510 | if (!key) |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 511 | object->new = !d_backing_inode(next); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 512 | |
| 513 | /* if this element of the path doesn't exist, then the lookup phase |
| 514 | * failed, and we can release any readers in the certain knowledge that |
| 515 | * there's nothing for them to actually read */ |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 516 | if (d_is_negative(next)) |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 517 | fscache_object_lookup_negative(&object->fscache); |
| 518 | |
| 519 | /* we need to create the object if it's negative */ |
| 520 | if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) { |
| 521 | /* index objects and intervening tree levels must be subdirs */ |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 522 | if (d_is_negative(next)) { |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 523 | ret = cachefiles_has_space(cache, 1, 0); |
| 524 | if (ret < 0) |
| 525 | goto create_error; |
| 526 | |
David Howells | 8214044 | 2010-12-24 14:48:35 +0000 | [diff] [blame] | 527 | path.dentry = dir; |
| 528 | ret = security_path_mkdir(&path, next, 0); |
| 529 | if (ret < 0) |
| 530 | goto create_error; |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 531 | start = jiffies; |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 532 | ret = vfs_mkdir(d_inode(dir), next, 0); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 533 | cachefiles_hist(cachefiles_mkdir_histogram, start); |
| 534 | if (ret < 0) |
| 535 | goto create_error; |
| 536 | |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 537 | ASSERT(d_backing_inode(next)); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 538 | |
| 539 | _debug("mkdir -> %p{%p{ino=%lu}}", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 540 | next, d_backing_inode(next), d_backing_inode(next)->i_ino); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 541 | |
David Howells | ce40fa7 | 2015-01-29 12:02:36 +0000 | [diff] [blame] | 542 | } else if (!d_can_lookup(next)) { |
Fabian Frederick | 6ff66ac | 2014-09-25 16:05:27 -0700 | [diff] [blame] | 543 | pr_err("inode %lu is not a directory\n", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 544 | d_backing_inode(next)->i_ino); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 545 | ret = -ENOBUFS; |
| 546 | goto error; |
| 547 | } |
| 548 | |
| 549 | } else { |
| 550 | /* non-index objects start out life as files */ |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 551 | if (d_is_negative(next)) { |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 552 | ret = cachefiles_has_space(cache, 1, 0); |
| 553 | if (ret < 0) |
| 554 | goto create_error; |
| 555 | |
David Howells | 8214044 | 2010-12-24 14:48:35 +0000 | [diff] [blame] | 556 | path.dentry = dir; |
| 557 | ret = security_path_mknod(&path, next, S_IFREG, 0); |
| 558 | if (ret < 0) |
| 559 | goto create_error; |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 560 | start = jiffies; |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 561 | ret = vfs_create(d_inode(dir), next, S_IFREG, true); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 562 | cachefiles_hist(cachefiles_create_histogram, start); |
| 563 | if (ret < 0) |
| 564 | goto create_error; |
| 565 | |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 566 | ASSERT(d_backing_inode(next)); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 567 | |
| 568 | _debug("create -> %p{%p{ino=%lu}}", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 569 | next, d_backing_inode(next), d_backing_inode(next)->i_ino); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 570 | |
David Howells | ce40fa7 | 2015-01-29 12:02:36 +0000 | [diff] [blame] | 571 | } else if (!d_can_lookup(next) && |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 572 | !d_is_reg(next) |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 573 | ) { |
Fabian Frederick | 6ff66ac | 2014-09-25 16:05:27 -0700 | [diff] [blame] | 574 | pr_err("inode %lu is not a file or directory\n", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 575 | d_backing_inode(next)->i_ino); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 576 | ret = -ENOBUFS; |
| 577 | goto error; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | /* process the next component */ |
| 582 | if (key) { |
| 583 | _debug("advance"); |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 584 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 585 | dput(dir); |
| 586 | dir = next; |
| 587 | next = NULL; |
| 588 | goto advance; |
| 589 | } |
| 590 | |
| 591 | /* we've found the object we were looking for */ |
| 592 | object->dentry = next; |
| 593 | |
| 594 | /* if we've found that the terminal object exists, then we need to |
| 595 | * check its attributes and delete it if it's out of date */ |
| 596 | if (!object->new) { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 597 | _debug("validate '%pd'", next); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 598 | |
| 599 | ret = cachefiles_check_object_xattr(object, auxdata); |
| 600 | if (ret == -ESTALE) { |
| 601 | /* delete the object (the deleter drops the directory |
| 602 | * mutex) */ |
| 603 | object->dentry = NULL; |
| 604 | |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 605 | ret = cachefiles_bury_object(cache, dir, next, true); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 606 | dput(next); |
| 607 | next = NULL; |
| 608 | |
| 609 | if (ret < 0) |
| 610 | goto delete_error; |
| 611 | |
| 612 | _debug("redo lookup"); |
| 613 | goto lookup_again; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /* note that we're now using this object */ |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 618 | ret = cachefiles_mark_object_active(cache, object); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 619 | |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 620 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 621 | dput(dir); |
| 622 | dir = NULL; |
| 623 | |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 624 | if (ret == -ETIMEDOUT) |
| 625 | goto mark_active_timed_out; |
| 626 | |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 627 | _debug("=== OBTAINED_OBJECT ==="); |
| 628 | |
| 629 | if (object->new) { |
| 630 | /* attach data to a newly constructed terminal object */ |
| 631 | ret = cachefiles_set_object_xattr(object, auxdata); |
| 632 | if (ret < 0) |
| 633 | goto check_error; |
| 634 | } else { |
| 635 | /* always update the atime on an object we've just looked up |
| 636 | * (this is used to keep track of culling, and atimes are only |
| 637 | * updated by read, write and readdir but not lookup or |
| 638 | * open) */ |
Al Viro | 68ac123 | 2012-03-15 08:21:57 -0400 | [diff] [blame] | 639 | path.dentry = next; |
| 640 | touch_atime(&path); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* open a file interface onto a data file */ |
| 644 | if (object->type != FSCACHE_COOKIE_TYPE_INDEX) { |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 645 | if (d_is_reg(object->dentry)) { |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 646 | const struct address_space_operations *aops; |
| 647 | |
| 648 | ret = -EPERM; |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 649 | aops = d_backing_inode(object->dentry)->i_mapping->a_ops; |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 650 | if (!aops->bmap) |
| 651 | goto check_error; |
| 652 | |
| 653 | object->backer = object->dentry; |
| 654 | } else { |
| 655 | BUG(); // TODO: open file in data-class subdir |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | object->new = 0; |
| 660 | fscache_obtained_object(&object->fscache); |
| 661 | |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 662 | _leave(" = 0 [%lu]", d_backing_inode(object->dentry)->i_ino); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 663 | return 0; |
| 664 | |
| 665 | create_error: |
| 666 | _debug("create error %d", ret); |
| 667 | if (ret == -EIO) |
| 668 | cachefiles_io_error(cache, "Create/mkdir failed"); |
| 669 | goto error; |
| 670 | |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 671 | mark_active_timed_out: |
| 672 | _debug("mark active timed out"); |
| 673 | goto release_dentry; |
| 674 | |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 675 | check_error: |
| 676 | _debug("check error %d", ret); |
| 677 | write_lock(&cache->active_lock); |
| 678 | rb_erase(&object->active_node, &cache->active_nodes); |
| 679 | clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags); |
| 680 | wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE); |
| 681 | write_unlock(&cache->active_lock); |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 682 | release_dentry: |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 683 | dput(object->dentry); |
| 684 | object->dentry = NULL; |
| 685 | goto error_out; |
| 686 | |
| 687 | delete_error: |
| 688 | _debug("delete error %d", ret); |
| 689 | goto error_out2; |
| 690 | |
| 691 | lookup_error: |
| 692 | _debug("lookup error %ld", PTR_ERR(next)); |
| 693 | ret = PTR_ERR(next); |
| 694 | if (ret == -EIO) |
| 695 | cachefiles_io_error(cache, "Lookup failed"); |
| 696 | next = NULL; |
| 697 | error: |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 698 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 699 | dput(next); |
| 700 | error_out2: |
| 701 | dput(dir); |
| 702 | error_out: |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 703 | _leave(" = error %d", -ret); |
| 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * get a subdirectory |
| 709 | */ |
| 710 | struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache, |
| 711 | struct dentry *dir, |
| 712 | const char *dirname) |
| 713 | { |
| 714 | struct dentry *subdir; |
| 715 | unsigned long start; |
David Howells | 8214044 | 2010-12-24 14:48:35 +0000 | [diff] [blame] | 716 | struct path path; |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 717 | int ret; |
| 718 | |
| 719 | _enter(",,%s", dirname); |
| 720 | |
| 721 | /* search the current directory for the element name */ |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 722 | mutex_lock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 723 | |
| 724 | start = jiffies; |
| 725 | subdir = lookup_one_len(dirname, dir, strlen(dirname)); |
| 726 | cachefiles_hist(cachefiles_lookup_histogram, start); |
| 727 | if (IS_ERR(subdir)) { |
| 728 | if (PTR_ERR(subdir) == -ENOMEM) |
| 729 | goto nomem_d_alloc; |
| 730 | goto lookup_error; |
| 731 | } |
| 732 | |
| 733 | _debug("subdir -> %p %s", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 734 | subdir, d_backing_inode(subdir) ? "positive" : "negative"); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 735 | |
| 736 | /* we need to create the subdir if it doesn't exist yet */ |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 737 | if (d_is_negative(subdir)) { |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 738 | ret = cachefiles_has_space(cache, 1, 0); |
| 739 | if (ret < 0) |
| 740 | goto mkdir_error; |
| 741 | |
| 742 | _debug("attempt mkdir"); |
| 743 | |
David Howells | 8214044 | 2010-12-24 14:48:35 +0000 | [diff] [blame] | 744 | path.mnt = cache->mnt; |
| 745 | path.dentry = dir; |
| 746 | ret = security_path_mkdir(&path, subdir, 0700); |
| 747 | if (ret < 0) |
| 748 | goto mkdir_error; |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 749 | ret = vfs_mkdir(d_inode(dir), subdir, 0700); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 750 | if (ret < 0) |
| 751 | goto mkdir_error; |
| 752 | |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 753 | ASSERT(d_backing_inode(subdir)); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 754 | |
| 755 | _debug("mkdir -> %p{%p{ino=%lu}}", |
| 756 | subdir, |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 757 | d_backing_inode(subdir), |
| 758 | d_backing_inode(subdir)->i_ino); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 759 | } |
| 760 | |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 761 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 762 | |
| 763 | /* we need to make sure the subdir is a directory */ |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 764 | ASSERT(d_backing_inode(subdir)); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 765 | |
David Howells | ce40fa7 | 2015-01-29 12:02:36 +0000 | [diff] [blame] | 766 | if (!d_can_lookup(subdir)) { |
Fabian Frederick | 6ff66ac | 2014-09-25 16:05:27 -0700 | [diff] [blame] | 767 | pr_err("%s is not a directory\n", dirname); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 768 | ret = -EIO; |
| 769 | goto check_error; |
| 770 | } |
| 771 | |
| 772 | ret = -EPERM; |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 773 | if (!d_backing_inode(subdir)->i_op->setxattr || |
| 774 | !d_backing_inode(subdir)->i_op->getxattr || |
| 775 | !d_backing_inode(subdir)->i_op->lookup || |
| 776 | !d_backing_inode(subdir)->i_op->mkdir || |
| 777 | !d_backing_inode(subdir)->i_op->create || |
| 778 | (!d_backing_inode(subdir)->i_op->rename && |
| 779 | !d_backing_inode(subdir)->i_op->rename2) || |
| 780 | !d_backing_inode(subdir)->i_op->rmdir || |
| 781 | !d_backing_inode(subdir)->i_op->unlink) |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 782 | goto check_error; |
| 783 | |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 784 | _leave(" = [%lu]", d_backing_inode(subdir)->i_ino); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 785 | return subdir; |
| 786 | |
| 787 | check_error: |
| 788 | dput(subdir); |
| 789 | _leave(" = %d [check]", ret); |
| 790 | return ERR_PTR(ret); |
| 791 | |
| 792 | mkdir_error: |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 793 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 794 | dput(subdir); |
Fabian Frederick | 6ff66ac | 2014-09-25 16:05:27 -0700 | [diff] [blame] | 795 | pr_err("mkdir %s failed with error %d\n", dirname, ret); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 796 | return ERR_PTR(ret); |
| 797 | |
| 798 | lookup_error: |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 799 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 800 | ret = PTR_ERR(subdir); |
Fabian Frederick | 6ff66ac | 2014-09-25 16:05:27 -0700 | [diff] [blame] | 801 | pr_err("Lookup %s failed with error %d\n", dirname, ret); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 802 | return ERR_PTR(ret); |
| 803 | |
| 804 | nomem_d_alloc: |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 805 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 806 | _leave(" = -ENOMEM"); |
| 807 | return ERR_PTR(-ENOMEM); |
| 808 | } |
| 809 | |
| 810 | /* |
| 811 | * find out if an object is in use or not |
| 812 | * - if finds object and it's not in use: |
| 813 | * - returns a pointer to the object and a reference on it |
| 814 | * - returns with the directory locked |
| 815 | */ |
| 816 | static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache, |
| 817 | struct dentry *dir, |
| 818 | char *filename) |
| 819 | { |
| 820 | struct cachefiles_object *object; |
| 821 | struct rb_node *_n; |
| 822 | struct dentry *victim; |
| 823 | unsigned long start; |
| 824 | int ret; |
| 825 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 826 | //_enter(",%pd/,%s", |
| 827 | // dir, filename); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 828 | |
| 829 | /* look up the victim */ |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 830 | mutex_lock_nested(&d_inode(dir)->i_mutex, I_MUTEX_PARENT); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 831 | |
| 832 | start = jiffies; |
| 833 | victim = lookup_one_len(filename, dir, strlen(filename)); |
| 834 | cachefiles_hist(cachefiles_lookup_histogram, start); |
| 835 | if (IS_ERR(victim)) |
| 836 | goto lookup_error; |
| 837 | |
| 838 | //_debug("victim -> %p %s", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 839 | // victim, d_backing_inode(victim) ? "positive" : "negative"); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 840 | |
| 841 | /* if the object is no longer there then we probably retired the object |
| 842 | * at the netfs's request whilst the cull was in progress |
| 843 | */ |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 844 | if (d_is_negative(victim)) { |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 845 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 846 | dput(victim); |
| 847 | _leave(" = -ENOENT [absent]"); |
| 848 | return ERR_PTR(-ENOENT); |
| 849 | } |
| 850 | |
| 851 | /* check to see if we're using this object */ |
| 852 | read_lock(&cache->active_lock); |
| 853 | |
| 854 | _n = cache->active_nodes.rb_node; |
| 855 | |
| 856 | while (_n) { |
| 857 | object = rb_entry(_n, struct cachefiles_object, active_node); |
| 858 | |
| 859 | if (object->dentry > victim) |
| 860 | _n = _n->rb_left; |
| 861 | else if (object->dentry < victim) |
| 862 | _n = _n->rb_right; |
| 863 | else |
| 864 | goto object_in_use; |
| 865 | } |
| 866 | |
| 867 | read_unlock(&cache->active_lock); |
| 868 | |
| 869 | //_leave(" = %p", victim); |
| 870 | return victim; |
| 871 | |
| 872 | object_in_use: |
| 873 | read_unlock(&cache->active_lock); |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 874 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 875 | dput(victim); |
| 876 | //_leave(" = -EBUSY [in use]"); |
| 877 | return ERR_PTR(-EBUSY); |
| 878 | |
| 879 | lookup_error: |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 880 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 881 | ret = PTR_ERR(victim); |
| 882 | if (ret == -ENOENT) { |
| 883 | /* file or dir now absent - probably retired by netfs */ |
| 884 | _leave(" = -ESTALE [absent]"); |
| 885 | return ERR_PTR(-ESTALE); |
| 886 | } |
| 887 | |
| 888 | if (ret == -EIO) { |
| 889 | cachefiles_io_error(cache, "Lookup failed"); |
| 890 | } else if (ret != -ENOMEM) { |
Fabian Frederick | 6ff66ac | 2014-09-25 16:05:27 -0700 | [diff] [blame] | 891 | pr_err("Internal error: %d\n", ret); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 892 | ret = -EIO; |
| 893 | } |
| 894 | |
| 895 | _leave(" = %d", ret); |
| 896 | return ERR_PTR(ret); |
| 897 | } |
| 898 | |
| 899 | /* |
| 900 | * cull an object if it's not in use |
| 901 | * - called only by cache manager daemon |
| 902 | */ |
| 903 | int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir, |
| 904 | char *filename) |
| 905 | { |
| 906 | struct dentry *victim; |
| 907 | int ret; |
| 908 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 909 | _enter(",%pd/,%s", dir, filename); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 910 | |
| 911 | victim = cachefiles_check_active(cache, dir, filename); |
| 912 | if (IS_ERR(victim)) |
| 913 | return PTR_ERR(victim); |
| 914 | |
| 915 | _debug("victim -> %p %s", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 916 | victim, d_backing_inode(victim) ? "positive" : "negative"); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 917 | |
| 918 | /* okay... the victim is not being used so we can cull it |
| 919 | * - start by marking it as stale |
| 920 | */ |
| 921 | _debug("victim is cullable"); |
| 922 | |
| 923 | ret = cachefiles_remove_object_xattr(cache, victim); |
| 924 | if (ret < 0) |
| 925 | goto error_unlock; |
| 926 | |
| 927 | /* actually remove the victim (drops the dir mutex) */ |
| 928 | _debug("bury"); |
| 929 | |
David Howells | c61ea31 | 2010-05-11 16:51:39 +0100 | [diff] [blame] | 930 | ret = cachefiles_bury_object(cache, dir, victim, false); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 931 | if (ret < 0) |
| 932 | goto error; |
| 933 | |
| 934 | dput(victim); |
| 935 | _leave(" = 0"); |
| 936 | return 0; |
| 937 | |
| 938 | error_unlock: |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 939 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 940 | error: |
| 941 | dput(victim); |
| 942 | if (ret == -ENOENT) { |
| 943 | /* file or dir now absent - probably retired by netfs */ |
| 944 | _leave(" = -ESTALE [absent]"); |
| 945 | return -ESTALE; |
| 946 | } |
| 947 | |
| 948 | if (ret != -ENOMEM) { |
Fabian Frederick | 6ff66ac | 2014-09-25 16:05:27 -0700 | [diff] [blame] | 949 | pr_err("Internal error: %d\n", ret); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 950 | ret = -EIO; |
| 951 | } |
| 952 | |
| 953 | _leave(" = %d", ret); |
| 954 | return ret; |
| 955 | } |
| 956 | |
| 957 | /* |
| 958 | * find out if an object is in use or not |
| 959 | * - called only by cache manager daemon |
| 960 | * - returns -EBUSY or 0 to indicate whether an object is in use or not |
| 961 | */ |
| 962 | int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir, |
| 963 | char *filename) |
| 964 | { |
| 965 | struct dentry *victim; |
| 966 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 967 | //_enter(",%pd/,%s", |
| 968 | // dir, filename); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 969 | |
| 970 | victim = cachefiles_check_active(cache, dir, filename); |
| 971 | if (IS_ERR(victim)) |
| 972 | return PTR_ERR(victim); |
| 973 | |
David Howells | 5153bc8 | 2015-03-06 14:08:58 +0000 | [diff] [blame] | 974 | mutex_unlock(&d_inode(dir)->i_mutex); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 975 | dput(victim); |
| 976 | //_leave(" = 0"); |
| 977 | return 0; |
| 978 | } |