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