Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2011 Novell Inc. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/namei.h> |
| 13 | #include <linux/file.h> |
| 14 | #include <linux/xattr.h> |
| 15 | #include <linux/rbtree.h> |
| 16 | #include <linux/security.h> |
| 17 | #include <linux/cred.h> |
| 18 | #include "overlayfs.h" |
| 19 | |
| 20 | struct ovl_cache_entry { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 21 | unsigned int len; |
| 22 | unsigned int type; |
| 23 | u64 ino; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 24 | struct list_head l_node; |
| 25 | struct rb_node node; |
Miklos Szeredi | c209653 | 2014-10-27 13:48:48 +0100 | [diff] [blame^] | 26 | bool is_whiteout; |
| 27 | bool is_cursor; |
Al Viro | 68bf861 | 2014-10-23 22:58:56 -0400 | [diff] [blame] | 28 | char name[]; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | struct ovl_dir_cache { |
| 32 | long refcount; |
| 33 | u64 version; |
| 34 | struct list_head entries; |
| 35 | }; |
| 36 | |
| 37 | struct ovl_readdir_data { |
| 38 | struct dir_context ctx; |
| 39 | bool is_merge; |
Al Viro | 49be4fb | 2014-10-23 23:00:53 -0400 | [diff] [blame] | 40 | struct rb_root root; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 41 | struct list_head *list; |
Al Viro | db6ec21 | 2014-10-23 23:03:03 -0400 | [diff] [blame] | 42 | struct list_head middle; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 43 | int count; |
| 44 | int err; |
| 45 | }; |
| 46 | |
| 47 | struct ovl_dir_file { |
| 48 | bool is_real; |
| 49 | bool is_upper; |
| 50 | struct ovl_dir_cache *cache; |
| 51 | struct ovl_cache_entry cursor; |
| 52 | struct file *realfile; |
| 53 | struct file *upperfile; |
| 54 | }; |
| 55 | |
| 56 | static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n) |
| 57 | { |
| 58 | return container_of(n, struct ovl_cache_entry, node); |
| 59 | } |
| 60 | |
| 61 | static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root, |
| 62 | const char *name, int len) |
| 63 | { |
| 64 | struct rb_node *node = root->rb_node; |
| 65 | int cmp; |
| 66 | |
| 67 | while (node) { |
| 68 | struct ovl_cache_entry *p = ovl_cache_entry_from_node(node); |
| 69 | |
| 70 | cmp = strncmp(name, p->name, len); |
| 71 | if (cmp > 0) |
| 72 | node = p->node.rb_right; |
| 73 | else if (cmp < 0 || len < p->len) |
| 74 | node = p->node.rb_left; |
| 75 | else |
| 76 | return p; |
| 77 | } |
| 78 | |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len, |
| 83 | u64 ino, unsigned int d_type) |
| 84 | { |
| 85 | struct ovl_cache_entry *p; |
Al Viro | 68bf861 | 2014-10-23 22:58:56 -0400 | [diff] [blame] | 86 | size_t size = offsetof(struct ovl_cache_entry, name[len + 1]); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 87 | |
Al Viro | 68bf861 | 2014-10-23 22:58:56 -0400 | [diff] [blame] | 88 | p = kmalloc(size, GFP_KERNEL); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 89 | if (p) { |
Al Viro | 68bf861 | 2014-10-23 22:58:56 -0400 | [diff] [blame] | 90 | memcpy(p->name, name, len); |
| 91 | p->name[len] = '\0'; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 92 | p->len = len; |
| 93 | p->type = d_type; |
| 94 | p->ino = ino; |
| 95 | p->is_whiteout = false; |
| 96 | } |
| 97 | |
| 98 | return p; |
| 99 | } |
| 100 | |
| 101 | static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd, |
| 102 | const char *name, int len, u64 ino, |
| 103 | unsigned int d_type) |
| 104 | { |
Al Viro | 49be4fb | 2014-10-23 23:00:53 -0400 | [diff] [blame] | 105 | struct rb_node **newp = &rdd->root.rb_node; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 106 | struct rb_node *parent = NULL; |
| 107 | struct ovl_cache_entry *p; |
| 108 | |
| 109 | while (*newp) { |
| 110 | int cmp; |
| 111 | struct ovl_cache_entry *tmp; |
| 112 | |
| 113 | parent = *newp; |
| 114 | tmp = ovl_cache_entry_from_node(*newp); |
| 115 | cmp = strncmp(name, tmp->name, len); |
| 116 | if (cmp > 0) |
| 117 | newp = &tmp->node.rb_right; |
| 118 | else if (cmp < 0 || len < tmp->len) |
| 119 | newp = &tmp->node.rb_left; |
| 120 | else |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | p = ovl_cache_entry_new(name, len, ino, d_type); |
| 125 | if (p == NULL) |
| 126 | return -ENOMEM; |
| 127 | |
| 128 | list_add_tail(&p->l_node, rdd->list); |
| 129 | rb_link_node(&p->node, parent, newp); |
Al Viro | 49be4fb | 2014-10-23 23:00:53 -0400 | [diff] [blame] | 130 | rb_insert_color(&p->node, &rdd->root); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static int ovl_fill_lower(struct ovl_readdir_data *rdd, |
| 136 | const char *name, int namelen, |
| 137 | loff_t offset, u64 ino, unsigned int d_type) |
| 138 | { |
| 139 | struct ovl_cache_entry *p; |
| 140 | |
Al Viro | 49be4fb | 2014-10-23 23:00:53 -0400 | [diff] [blame] | 141 | p = ovl_cache_entry_find(&rdd->root, name, namelen); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 142 | if (p) { |
Al Viro | db6ec21 | 2014-10-23 23:03:03 -0400 | [diff] [blame] | 143 | list_move_tail(&p->l_node, &rdd->middle); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 144 | } else { |
| 145 | p = ovl_cache_entry_new(name, namelen, ino, d_type); |
| 146 | if (p == NULL) |
| 147 | rdd->err = -ENOMEM; |
| 148 | else |
Al Viro | db6ec21 | 2014-10-23 23:03:03 -0400 | [diff] [blame] | 149 | list_add_tail(&p->l_node, &rdd->middle); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | return rdd->err; |
| 153 | } |
| 154 | |
| 155 | void ovl_cache_free(struct list_head *list) |
| 156 | { |
| 157 | struct ovl_cache_entry *p; |
| 158 | struct ovl_cache_entry *n; |
| 159 | |
| 160 | list_for_each_entry_safe(p, n, list, l_node) |
| 161 | kfree(p); |
| 162 | |
| 163 | INIT_LIST_HEAD(list); |
| 164 | } |
| 165 | |
| 166 | static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry) |
| 167 | { |
| 168 | struct ovl_dir_cache *cache = od->cache; |
| 169 | |
| 170 | list_del(&od->cursor.l_node); |
| 171 | WARN_ON(cache->refcount <= 0); |
| 172 | cache->refcount--; |
| 173 | if (!cache->refcount) { |
| 174 | if (ovl_dir_cache(dentry) == cache) |
| 175 | ovl_set_dir_cache(dentry, NULL); |
| 176 | |
| 177 | ovl_cache_free(&cache->entries); |
| 178 | kfree(cache); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | static int ovl_fill_merge(void *buf, const char *name, int namelen, |
| 183 | loff_t offset, u64 ino, unsigned int d_type) |
| 184 | { |
| 185 | struct ovl_readdir_data *rdd = buf; |
| 186 | |
| 187 | rdd->count++; |
| 188 | if (!rdd->is_merge) |
| 189 | return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type); |
| 190 | else |
| 191 | return ovl_fill_lower(rdd, name, namelen, offset, ino, d_type); |
| 192 | } |
| 193 | |
| 194 | static inline int ovl_dir_read(struct path *realpath, |
| 195 | struct ovl_readdir_data *rdd) |
| 196 | { |
| 197 | struct file *realfile; |
| 198 | int err; |
| 199 | |
| 200 | realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY); |
| 201 | if (IS_ERR(realfile)) |
| 202 | return PTR_ERR(realfile); |
| 203 | |
| 204 | rdd->ctx.pos = 0; |
| 205 | do { |
| 206 | rdd->count = 0; |
| 207 | rdd->err = 0; |
| 208 | err = iterate_dir(realfile, &rdd->ctx); |
| 209 | if (err >= 0) |
| 210 | err = rdd->err; |
| 211 | } while (!err && rdd->count); |
| 212 | fput(realfile); |
| 213 | |
| 214 | return err; |
| 215 | } |
| 216 | |
| 217 | static void ovl_dir_reset(struct file *file) |
| 218 | { |
| 219 | struct ovl_dir_file *od = file->private_data; |
| 220 | struct ovl_dir_cache *cache = od->cache; |
| 221 | struct dentry *dentry = file->f_path.dentry; |
| 222 | enum ovl_path_type type = ovl_path_type(dentry); |
| 223 | |
| 224 | if (cache && ovl_dentry_version_get(dentry) != cache->version) { |
| 225 | ovl_cache_put(od, dentry); |
| 226 | od->cache = NULL; |
| 227 | } |
| 228 | WARN_ON(!od->is_real && type != OVL_PATH_MERGE); |
| 229 | if (od->is_real && type == OVL_PATH_MERGE) |
| 230 | od->is_real = false; |
| 231 | } |
| 232 | |
| 233 | static int ovl_dir_mark_whiteouts(struct dentry *dir, |
| 234 | struct ovl_readdir_data *rdd) |
| 235 | { |
| 236 | struct ovl_cache_entry *p; |
| 237 | struct dentry *dentry; |
| 238 | const struct cred *old_cred; |
| 239 | struct cred *override_cred; |
| 240 | |
| 241 | override_cred = prepare_creds(); |
| 242 | if (!override_cred) { |
| 243 | ovl_cache_free(rdd->list); |
| 244 | return -ENOMEM; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * CAP_DAC_OVERRIDE for lookup |
| 249 | */ |
| 250 | cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE); |
| 251 | old_cred = override_creds(override_cred); |
| 252 | |
| 253 | mutex_lock(&dir->d_inode->i_mutex); |
| 254 | list_for_each_entry(p, rdd->list, l_node) { |
Miklos Szeredi | c209653 | 2014-10-27 13:48:48 +0100 | [diff] [blame^] | 255 | if (p->is_cursor) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 256 | continue; |
| 257 | |
| 258 | if (p->type != DT_CHR) |
| 259 | continue; |
| 260 | |
| 261 | dentry = lookup_one_len(p->name, dir, p->len); |
| 262 | if (IS_ERR(dentry)) |
| 263 | continue; |
| 264 | |
| 265 | p->is_whiteout = ovl_is_whiteout(dentry); |
| 266 | dput(dentry); |
| 267 | } |
| 268 | mutex_unlock(&dir->d_inode->i_mutex); |
| 269 | |
| 270 | revert_creds(old_cred); |
| 271 | put_cred(override_cred); |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static inline int ovl_dir_read_merged(struct path *upperpath, |
| 277 | struct path *lowerpath, |
| 278 | struct list_head *list) |
| 279 | { |
| 280 | int err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 281 | struct ovl_readdir_data rdd = { |
| 282 | .ctx.actor = ovl_fill_merge, |
| 283 | .list = list, |
Al Viro | 49be4fb | 2014-10-23 23:00:53 -0400 | [diff] [blame] | 284 | .root = RB_ROOT, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 285 | .is_merge = false, |
| 286 | }; |
| 287 | |
| 288 | if (upperpath->dentry) { |
| 289 | err = ovl_dir_read(upperpath, &rdd); |
| 290 | if (err) |
| 291 | goto out; |
| 292 | |
| 293 | if (lowerpath->dentry) { |
| 294 | err = ovl_dir_mark_whiteouts(upperpath->dentry, &rdd); |
| 295 | if (err) |
| 296 | goto out; |
| 297 | } |
| 298 | } |
| 299 | if (lowerpath->dentry) { |
| 300 | /* |
| 301 | * Insert lowerpath entries before upperpath ones, this allows |
| 302 | * offsets to be reasonably constant |
| 303 | */ |
Al Viro | db6ec21 | 2014-10-23 23:03:03 -0400 | [diff] [blame] | 304 | list_add(&rdd.middle, rdd.list); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 305 | rdd.is_merge = true; |
| 306 | err = ovl_dir_read(lowerpath, &rdd); |
Al Viro | db6ec21 | 2014-10-23 23:03:03 -0400 | [diff] [blame] | 307 | list_del(&rdd.middle); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 308 | } |
| 309 | out: |
| 310 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos) |
| 314 | { |
| 315 | struct ovl_cache_entry *p; |
| 316 | loff_t off = 0; |
| 317 | |
| 318 | list_for_each_entry(p, &od->cache->entries, l_node) { |
Miklos Szeredi | c209653 | 2014-10-27 13:48:48 +0100 | [diff] [blame^] | 319 | if (p->is_cursor) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 320 | continue; |
| 321 | if (off >= pos) |
| 322 | break; |
| 323 | off++; |
| 324 | } |
| 325 | list_move_tail(&od->cursor.l_node, &p->l_node); |
| 326 | } |
| 327 | |
| 328 | static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry) |
| 329 | { |
| 330 | int res; |
| 331 | struct path lowerpath; |
| 332 | struct path upperpath; |
| 333 | struct ovl_dir_cache *cache; |
| 334 | |
| 335 | cache = ovl_dir_cache(dentry); |
| 336 | if (cache && ovl_dentry_version_get(dentry) == cache->version) { |
| 337 | cache->refcount++; |
| 338 | return cache; |
| 339 | } |
| 340 | ovl_set_dir_cache(dentry, NULL); |
| 341 | |
| 342 | cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL); |
| 343 | if (!cache) |
| 344 | return ERR_PTR(-ENOMEM); |
| 345 | |
| 346 | cache->refcount = 1; |
| 347 | INIT_LIST_HEAD(&cache->entries); |
| 348 | |
| 349 | ovl_path_lower(dentry, &lowerpath); |
| 350 | ovl_path_upper(dentry, &upperpath); |
| 351 | |
| 352 | res = ovl_dir_read_merged(&upperpath, &lowerpath, &cache->entries); |
| 353 | if (res) { |
| 354 | ovl_cache_free(&cache->entries); |
| 355 | kfree(cache); |
| 356 | return ERR_PTR(res); |
| 357 | } |
| 358 | |
| 359 | cache->version = ovl_dentry_version_get(dentry); |
| 360 | ovl_set_dir_cache(dentry, cache); |
| 361 | |
| 362 | return cache; |
| 363 | } |
| 364 | |
| 365 | static int ovl_iterate(struct file *file, struct dir_context *ctx) |
| 366 | { |
| 367 | struct ovl_dir_file *od = file->private_data; |
| 368 | struct dentry *dentry = file->f_path.dentry; |
| 369 | |
| 370 | if (!ctx->pos) |
| 371 | ovl_dir_reset(file); |
| 372 | |
| 373 | if (od->is_real) |
| 374 | return iterate_dir(od->realfile, ctx); |
| 375 | |
| 376 | if (!od->cache) { |
| 377 | struct ovl_dir_cache *cache; |
| 378 | |
| 379 | cache = ovl_cache_get(dentry); |
| 380 | if (IS_ERR(cache)) |
| 381 | return PTR_ERR(cache); |
| 382 | |
| 383 | od->cache = cache; |
| 384 | ovl_seek_cursor(od, ctx->pos); |
| 385 | } |
| 386 | |
| 387 | while (od->cursor.l_node.next != &od->cache->entries) { |
| 388 | struct ovl_cache_entry *p; |
| 389 | |
| 390 | p = list_entry(od->cursor.l_node.next, struct ovl_cache_entry, l_node); |
| 391 | /* Skip cursors */ |
Miklos Szeredi | c209653 | 2014-10-27 13:48:48 +0100 | [diff] [blame^] | 392 | if (!p->is_cursor) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 393 | if (!p->is_whiteout) { |
| 394 | if (!dir_emit(ctx, p->name, p->len, p->ino, p->type)) |
| 395 | break; |
| 396 | } |
| 397 | ctx->pos++; |
| 398 | } |
| 399 | list_move(&od->cursor.l_node, &p->l_node); |
| 400 | } |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin) |
| 405 | { |
| 406 | loff_t res; |
| 407 | struct ovl_dir_file *od = file->private_data; |
| 408 | |
| 409 | mutex_lock(&file_inode(file)->i_mutex); |
| 410 | if (!file->f_pos) |
| 411 | ovl_dir_reset(file); |
| 412 | |
| 413 | if (od->is_real) { |
| 414 | res = vfs_llseek(od->realfile, offset, origin); |
| 415 | file->f_pos = od->realfile->f_pos; |
| 416 | } else { |
| 417 | res = -EINVAL; |
| 418 | |
| 419 | switch (origin) { |
| 420 | case SEEK_CUR: |
| 421 | offset += file->f_pos; |
| 422 | break; |
| 423 | case SEEK_SET: |
| 424 | break; |
| 425 | default: |
| 426 | goto out_unlock; |
| 427 | } |
| 428 | if (offset < 0) |
| 429 | goto out_unlock; |
| 430 | |
| 431 | if (offset != file->f_pos) { |
| 432 | file->f_pos = offset; |
| 433 | if (od->cache) |
| 434 | ovl_seek_cursor(od, offset); |
| 435 | } |
| 436 | res = offset; |
| 437 | } |
| 438 | out_unlock: |
| 439 | mutex_unlock(&file_inode(file)->i_mutex); |
| 440 | |
| 441 | return res; |
| 442 | } |
| 443 | |
| 444 | static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end, |
| 445 | int datasync) |
| 446 | { |
| 447 | struct ovl_dir_file *od = file->private_data; |
| 448 | struct dentry *dentry = file->f_path.dentry; |
| 449 | struct file *realfile = od->realfile; |
| 450 | |
| 451 | /* |
| 452 | * Need to check if we started out being a lower dir, but got copied up |
| 453 | */ |
| 454 | if (!od->is_upper && ovl_path_type(dentry) == OVL_PATH_MERGE) { |
| 455 | struct inode *inode = file_inode(file); |
| 456 | |
Al Viro | d45f00a | 2014-10-28 18:27:28 -0400 | [diff] [blame] | 457 | realfile =lockless_dereference(od->upperfile); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 458 | if (!realfile) { |
| 459 | struct path upperpath; |
| 460 | |
| 461 | ovl_path_upper(dentry, &upperpath); |
| 462 | realfile = ovl_path_open(&upperpath, O_RDONLY); |
Al Viro | d45f00a | 2014-10-28 18:27:28 -0400 | [diff] [blame] | 463 | smp_mb__before_spinlock(); |
Al Viro | 3d268c9 | 2014-10-23 22:56:05 -0400 | [diff] [blame] | 464 | mutex_lock(&inode->i_mutex); |
| 465 | if (!od->upperfile) { |
| 466 | if (IS_ERR(realfile)) { |
| 467 | mutex_unlock(&inode->i_mutex); |
| 468 | return PTR_ERR(realfile); |
| 469 | } |
| 470 | od->upperfile = realfile; |
| 471 | } else { |
| 472 | /* somebody has beaten us to it */ |
| 473 | if (!IS_ERR(realfile)) |
| 474 | fput(realfile); |
| 475 | realfile = od->upperfile; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 476 | } |
Al Viro | 3d268c9 | 2014-10-23 22:56:05 -0400 | [diff] [blame] | 477 | mutex_unlock(&inode->i_mutex); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 478 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | return vfs_fsync_range(realfile, start, end, datasync); |
| 482 | } |
| 483 | |
| 484 | static int ovl_dir_release(struct inode *inode, struct file *file) |
| 485 | { |
| 486 | struct ovl_dir_file *od = file->private_data; |
| 487 | |
| 488 | if (od->cache) { |
| 489 | mutex_lock(&inode->i_mutex); |
| 490 | ovl_cache_put(od, file->f_path.dentry); |
| 491 | mutex_unlock(&inode->i_mutex); |
| 492 | } |
| 493 | fput(od->realfile); |
| 494 | if (od->upperfile) |
| 495 | fput(od->upperfile); |
| 496 | kfree(od); |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | static int ovl_dir_open(struct inode *inode, struct file *file) |
| 502 | { |
| 503 | struct path realpath; |
| 504 | struct file *realfile; |
| 505 | struct ovl_dir_file *od; |
| 506 | enum ovl_path_type type; |
| 507 | |
| 508 | od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL); |
| 509 | if (!od) |
| 510 | return -ENOMEM; |
| 511 | |
| 512 | type = ovl_path_real(file->f_path.dentry, &realpath); |
| 513 | realfile = ovl_path_open(&realpath, file->f_flags); |
| 514 | if (IS_ERR(realfile)) { |
| 515 | kfree(od); |
| 516 | return PTR_ERR(realfile); |
| 517 | } |
| 518 | INIT_LIST_HEAD(&od->cursor.l_node); |
| 519 | od->realfile = realfile; |
| 520 | od->is_real = (type != OVL_PATH_MERGE); |
| 521 | od->is_upper = (type != OVL_PATH_LOWER); |
Miklos Szeredi | c209653 | 2014-10-27 13:48:48 +0100 | [diff] [blame^] | 522 | od->cursor.is_cursor = true; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 523 | file->private_data = od; |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | const struct file_operations ovl_dir_operations = { |
| 529 | .read = generic_read_dir, |
| 530 | .open = ovl_dir_open, |
| 531 | .iterate = ovl_iterate, |
| 532 | .llseek = ovl_dir_llseek, |
| 533 | .fsync = ovl_dir_fsync, |
| 534 | .release = ovl_dir_release, |
| 535 | }; |
| 536 | |
| 537 | int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list) |
| 538 | { |
| 539 | int err; |
| 540 | struct path lowerpath; |
| 541 | struct path upperpath; |
| 542 | struct ovl_cache_entry *p; |
| 543 | |
| 544 | ovl_path_upper(dentry, &upperpath); |
| 545 | ovl_path_lower(dentry, &lowerpath); |
| 546 | |
| 547 | err = ovl_dir_read_merged(&upperpath, &lowerpath, list); |
| 548 | if (err) |
| 549 | return err; |
| 550 | |
| 551 | err = 0; |
| 552 | |
| 553 | list_for_each_entry(p, list, l_node) { |
| 554 | if (p->is_whiteout) |
| 555 | continue; |
| 556 | |
| 557 | if (p->name[0] == '.') { |
| 558 | if (p->len == 1) |
| 559 | continue; |
| 560 | if (p->len == 2 && p->name[1] == '.') |
| 561 | continue; |
| 562 | } |
| 563 | err = -ENOTEMPTY; |
| 564 | break; |
| 565 | } |
| 566 | |
| 567 | return err; |
| 568 | } |
| 569 | |
| 570 | void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list) |
| 571 | { |
| 572 | struct ovl_cache_entry *p; |
| 573 | |
| 574 | mutex_lock_nested(&upper->d_inode->i_mutex, I_MUTEX_PARENT); |
| 575 | list_for_each_entry(p, list, l_node) { |
| 576 | struct dentry *dentry; |
| 577 | |
| 578 | if (!p->is_whiteout) |
| 579 | continue; |
| 580 | |
| 581 | dentry = lookup_one_len(p->name, upper, p->len); |
| 582 | if (IS_ERR(dentry)) { |
| 583 | pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n", |
| 584 | upper->d_name.name, p->len, p->name, |
| 585 | (int) PTR_ERR(dentry)); |
| 586 | continue; |
| 587 | } |
| 588 | ovl_cleanup(upper->d_inode, dentry); |
| 589 | dput(dentry); |
| 590 | } |
| 591 | mutex_unlock(&upper->d_inode->i_mutex); |
| 592 | } |