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> |
Amir Goldstein | b5efccb | 2017-05-11 16:42:27 +0300 | [diff] [blame] | 18 | #include <linux/ratelimit.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 19 | #include "overlayfs.h" |
| 20 | |
| 21 | struct ovl_cache_entry { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 22 | unsigned int len; |
| 23 | unsigned int type; |
Amir Goldstein | b5efccb | 2017-05-11 16:42:27 +0300 | [diff] [blame] | 24 | u64 real_ino; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 25 | u64 ino; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 26 | struct list_head l_node; |
| 27 | struct rb_node node; |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 28 | struct ovl_cache_entry *next_maybe_whiteout; |
zhangyi (F) | 95e598e | 2017-10-31 22:57:00 +0200 | [diff] [blame] | 29 | bool is_upper; |
Miklos Szeredi | c209653 | 2014-10-27 13:48:48 +0100 | [diff] [blame] | 30 | bool is_whiteout; |
Al Viro | 68bf861 | 2014-10-23 22:58:56 -0400 | [diff] [blame] | 31 | char name[]; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | struct ovl_dir_cache { |
| 35 | long refcount; |
| 36 | u64 version; |
| 37 | struct list_head entries; |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 38 | struct rb_root root; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | struct ovl_readdir_data { |
| 42 | struct dir_context ctx; |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 43 | struct dentry *dentry; |
Miklos Szeredi | 56656e9 | 2016-03-21 17:31:46 +0100 | [diff] [blame] | 44 | bool is_lowest; |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 45 | struct rb_root *root; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 46 | struct list_head *list; |
Al Viro | db6ec21 | 2014-10-23 23:03:03 -0400 | [diff] [blame] | 47 | struct list_head middle; |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 48 | struct ovl_cache_entry *first_maybe_whiteout; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 49 | int count; |
| 50 | int err; |
Amir Goldstein | b5efccb | 2017-05-11 16:42:27 +0300 | [diff] [blame] | 51 | bool is_upper; |
Vivek Goyal | 45aebea | 2016-02-22 09:28:34 -0500 | [diff] [blame] | 52 | bool d_type_supported; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | struct ovl_dir_file { |
| 56 | bool is_real; |
| 57 | bool is_upper; |
| 58 | struct ovl_dir_cache *cache; |
hujianyang | 4330397 | 2014-12-11 10:30:18 +0800 | [diff] [blame] | 59 | struct list_head *cursor; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 60 | struct file *realfile; |
| 61 | struct file *upperfile; |
| 62 | }; |
| 63 | |
| 64 | static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n) |
| 65 | { |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 66 | return rb_entry(n, struct ovl_cache_entry, node); |
| 67 | } |
| 68 | |
| 69 | static bool ovl_cache_entry_find_link(const char *name, int len, |
| 70 | struct rb_node ***link, |
| 71 | struct rb_node **parent) |
| 72 | { |
| 73 | bool found = false; |
| 74 | struct rb_node **newp = *link; |
| 75 | |
| 76 | while (!found && *newp) { |
| 77 | int cmp; |
| 78 | struct ovl_cache_entry *tmp; |
| 79 | |
| 80 | *parent = *newp; |
| 81 | tmp = ovl_cache_entry_from_node(*newp); |
| 82 | cmp = strncmp(name, tmp->name, len); |
| 83 | if (cmp > 0) |
| 84 | newp = &tmp->node.rb_right; |
| 85 | else if (cmp < 0 || len < tmp->len) |
| 86 | newp = &tmp->node.rb_left; |
| 87 | else |
| 88 | found = true; |
| 89 | } |
| 90 | *link = newp; |
| 91 | |
| 92 | return found; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root, |
| 96 | const char *name, int len) |
| 97 | { |
| 98 | struct rb_node *node = root->rb_node; |
| 99 | int cmp; |
| 100 | |
| 101 | while (node) { |
| 102 | struct ovl_cache_entry *p = ovl_cache_entry_from_node(node); |
| 103 | |
| 104 | cmp = strncmp(name, p->name, len); |
| 105 | if (cmp > 0) |
| 106 | node = p->node.rb_right; |
| 107 | else if (cmp < 0 || len < p->len) |
| 108 | node = p->node.rb_left; |
| 109 | else |
| 110 | return p; |
| 111 | } |
| 112 | |
| 113 | return NULL; |
| 114 | } |
| 115 | |
Amir Goldstein | b5efccb | 2017-05-11 16:42:27 +0300 | [diff] [blame] | 116 | static bool ovl_calc_d_ino(struct ovl_readdir_data *rdd, |
| 117 | struct ovl_cache_entry *p) |
| 118 | { |
| 119 | /* Don't care if not doing ovl_iter() */ |
| 120 | if (!rdd->dentry) |
| 121 | return false; |
| 122 | |
| 123 | /* Always recalc d_ino for parent */ |
| 124 | if (strcmp(p->name, "..") == 0) |
| 125 | return true; |
| 126 | |
| 127 | /* If this is lower, then native d_ino will do */ |
| 128 | if (!rdd->is_upper) |
| 129 | return false; |
| 130 | |
| 131 | /* |
| 132 | * Recalc d_ino for '.' and for all entries if dir is impure (contains |
| 133 | * copied up entries) |
| 134 | */ |
| 135 | if ((p->name[0] == '.' && p->len == 1) || |
| 136 | ovl_test_flag(OVL_IMPURE, d_inode(rdd->dentry))) |
| 137 | return true; |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 142 | 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] | 143 | const char *name, int len, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 144 | u64 ino, unsigned int d_type) |
| 145 | { |
| 146 | struct ovl_cache_entry *p; |
Al Viro | 68bf861 | 2014-10-23 22:58:56 -0400 | [diff] [blame] | 147 | size_t size = offsetof(struct ovl_cache_entry, name[len + 1]); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 148 | |
Al Viro | 68bf861 | 2014-10-23 22:58:56 -0400 | [diff] [blame] | 149 | p = kmalloc(size, GFP_KERNEL); |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 150 | if (!p) |
| 151 | return NULL; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 152 | |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 153 | memcpy(p->name, name, len); |
| 154 | p->name[len] = '\0'; |
| 155 | p->len = len; |
| 156 | p->type = d_type; |
Amir Goldstein | b5efccb | 2017-05-11 16:42:27 +0300 | [diff] [blame] | 157 | p->real_ino = ino; |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 158 | p->ino = ino; |
Amir Goldstein | b5efccb | 2017-05-11 16:42:27 +0300 | [diff] [blame] | 159 | /* Defer setting d_ino for upper entry to ovl_iterate() */ |
| 160 | if (ovl_calc_d_ino(rdd, p)) |
| 161 | p->ino = 0; |
zhangyi (F) | 95e598e | 2017-10-31 22:57:00 +0200 | [diff] [blame] | 162 | p->is_upper = rdd->is_upper; |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 163 | p->is_whiteout = false; |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 164 | |
| 165 | if (d_type == DT_CHR) { |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 166 | p->next_maybe_whiteout = rdd->first_maybe_whiteout; |
| 167 | rdd->first_maybe_whiteout = p; |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 168 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 169 | return p; |
| 170 | } |
| 171 | |
| 172 | static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd, |
| 173 | const char *name, int len, u64 ino, |
| 174 | unsigned int d_type) |
| 175 | { |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 176 | struct rb_node **newp = &rdd->root->rb_node; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 177 | struct rb_node *parent = NULL; |
| 178 | struct ovl_cache_entry *p; |
| 179 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 180 | if (ovl_cache_entry_find_link(name, len, &newp, &parent)) |
| 181 | return 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 182 | |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 183 | p = ovl_cache_entry_new(rdd, name, len, ino, d_type); |
Miklos Szeredi | 31e8cce | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 184 | if (p == NULL) { |
| 185 | rdd->err = -ENOMEM; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 186 | return -ENOMEM; |
Miklos Szeredi | 31e8cce | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 187 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 188 | |
| 189 | list_add_tail(&p->l_node, rdd->list); |
| 190 | rb_link_node(&p->node, parent, newp); |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 191 | rb_insert_color(&p->node, rdd->root); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Miklos Szeredi | 56656e9 | 2016-03-21 17:31:46 +0100 | [diff] [blame] | 196 | static int ovl_fill_lowest(struct ovl_readdir_data *rdd, |
| 197 | const char *name, int namelen, |
| 198 | loff_t offset, u64 ino, unsigned int d_type) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 199 | { |
| 200 | struct ovl_cache_entry *p; |
| 201 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 202 | p = ovl_cache_entry_find(rdd->root, name, namelen); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 203 | if (p) { |
Al Viro | db6ec21 | 2014-10-23 23:03:03 -0400 | [diff] [blame] | 204 | list_move_tail(&p->l_node, &rdd->middle); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 205 | } else { |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 206 | p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 207 | if (p == NULL) |
| 208 | rdd->err = -ENOMEM; |
| 209 | else |
Al Viro | db6ec21 | 2014-10-23 23:03:03 -0400 | [diff] [blame] | 210 | list_add_tail(&p->l_node, &rdd->middle); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | return rdd->err; |
| 214 | } |
| 215 | |
| 216 | void ovl_cache_free(struct list_head *list) |
| 217 | { |
| 218 | struct ovl_cache_entry *p; |
| 219 | struct ovl_cache_entry *n; |
| 220 | |
| 221 | list_for_each_entry_safe(p, n, list, l_node) |
| 222 | kfree(p); |
| 223 | |
| 224 | INIT_LIST_HEAD(list); |
| 225 | } |
| 226 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 227 | void ovl_dir_cache_free(struct inode *inode) |
| 228 | { |
| 229 | struct ovl_dir_cache *cache = ovl_dir_cache(inode); |
| 230 | |
| 231 | if (cache) { |
| 232 | ovl_cache_free(&cache->entries); |
| 233 | kfree(cache); |
| 234 | } |
| 235 | } |
| 236 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 237 | static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry) |
| 238 | { |
| 239 | struct ovl_dir_cache *cache = od->cache; |
| 240 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 241 | WARN_ON(cache->refcount <= 0); |
| 242 | cache->refcount--; |
| 243 | if (!cache->refcount) { |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 244 | if (ovl_dir_cache(d_inode(dentry)) == cache) |
| 245 | ovl_set_dir_cache(d_inode(dentry), NULL); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 246 | |
| 247 | ovl_cache_free(&cache->entries); |
| 248 | kfree(cache); |
| 249 | } |
| 250 | } |
| 251 | |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 252 | static int ovl_fill_merge(struct dir_context *ctx, const char *name, |
| 253 | int namelen, loff_t offset, u64 ino, |
| 254 | unsigned int d_type) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 255 | { |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 256 | struct ovl_readdir_data *rdd = |
| 257 | container_of(ctx, struct ovl_readdir_data, ctx); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 258 | |
| 259 | rdd->count++; |
Miklos Szeredi | 56656e9 | 2016-03-21 17:31:46 +0100 | [diff] [blame] | 260 | if (!rdd->is_lowest) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 261 | return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type); |
| 262 | else |
Miklos Szeredi | 56656e9 | 2016-03-21 17:31:46 +0100 | [diff] [blame] | 263 | return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 266 | static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd) |
| 267 | { |
| 268 | int err; |
| 269 | struct ovl_cache_entry *p; |
| 270 | struct dentry *dentry; |
| 271 | const struct cred *old_cred; |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 272 | |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 273 | old_cred = ovl_override_creds(rdd->dentry->d_sb); |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 274 | |
Al Viro | 0023541 | 2016-05-26 00:05:12 -0400 | [diff] [blame] | 275 | err = down_write_killable(&dir->d_inode->i_rwsem); |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 276 | if (!err) { |
| 277 | while (rdd->first_maybe_whiteout) { |
| 278 | p = rdd->first_maybe_whiteout; |
| 279 | rdd->first_maybe_whiteout = p->next_maybe_whiteout; |
| 280 | dentry = lookup_one_len(p->name, dir, p->len); |
| 281 | if (!IS_ERR(dentry)) { |
| 282 | p->is_whiteout = ovl_is_whiteout(dentry); |
| 283 | dput(dentry); |
| 284 | } |
| 285 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 286 | inode_unlock(dir->d_inode); |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 287 | } |
| 288 | revert_creds(old_cred); |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 289 | |
| 290 | return err; |
| 291 | } |
| 292 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 293 | static inline int ovl_dir_read(struct path *realpath, |
| 294 | struct ovl_readdir_data *rdd) |
| 295 | { |
| 296 | struct file *realfile; |
| 297 | int err; |
| 298 | |
| 299 | realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY); |
| 300 | if (IS_ERR(realfile)) |
| 301 | return PTR_ERR(realfile); |
| 302 | |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 303 | rdd->first_maybe_whiteout = NULL; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 304 | rdd->ctx.pos = 0; |
| 305 | do { |
| 306 | rdd->count = 0; |
| 307 | rdd->err = 0; |
| 308 | err = iterate_dir(realfile, &rdd->ctx); |
| 309 | if (err >= 0) |
| 310 | err = rdd->err; |
| 311 | } while (!err && rdd->count); |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 312 | |
Miklos Szeredi | eea2fb4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 313 | if (!err && rdd->first_maybe_whiteout && rdd->dentry) |
Miklos Szeredi | cdb6727 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 314 | err = ovl_check_whiteouts(realpath->dentry, rdd); |
| 315 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 316 | fput(realfile); |
| 317 | |
| 318 | return err; |
| 319 | } |
| 320 | |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 321 | /* |
| 322 | * Can we iterate real dir directly? |
| 323 | * |
| 324 | * Non-merge dir may contain whiteouts from a time it was a merge upper, before |
| 325 | * lower dir was removed under it and possibly before it was rotated from upper |
| 326 | * to lower layer. |
| 327 | */ |
| 328 | static bool ovl_dir_is_real(struct dentry *dir) |
| 329 | { |
| 330 | return !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir)); |
| 331 | } |
| 332 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 333 | static void ovl_dir_reset(struct file *file) |
| 334 | { |
| 335 | struct ovl_dir_file *od = file->private_data; |
| 336 | struct ovl_dir_cache *cache = od->cache; |
| 337 | struct dentry *dentry = file->f_path.dentry; |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 338 | bool is_real; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 339 | |
| 340 | if (cache && ovl_dentry_version_get(dentry) != cache->version) { |
| 341 | ovl_cache_put(od, dentry); |
| 342 | od->cache = NULL; |
hujianyang | 4330397 | 2014-12-11 10:30:18 +0800 | [diff] [blame] | 343 | od->cursor = NULL; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 344 | } |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 345 | is_real = ovl_dir_is_real(dentry); |
| 346 | if (od->is_real != is_real) { |
| 347 | /* is_real can only become false when dir is copied up */ |
| 348 | if (WARN_ON(is_real)) |
| 349 | return; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 350 | od->is_real = false; |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 351 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 354 | static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list, |
| 355 | struct rb_root *root) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 356 | { |
| 357 | int err; |
Miklos Szeredi | 9d7459d | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 358 | struct path realpath; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 359 | struct ovl_readdir_data rdd = { |
| 360 | .ctx.actor = ovl_fill_merge, |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 361 | .dentry = dentry, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 362 | .list = list, |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 363 | .root = root, |
Miklos Szeredi | 56656e9 | 2016-03-21 17:31:46 +0100 | [diff] [blame] | 364 | .is_lowest = false, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 365 | }; |
Miklos Szeredi | 9d7459d | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 366 | int idx, next; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 367 | |
Miklos Szeredi | 9d7459d | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 368 | for (idx = 0; idx != -1; idx = next) { |
| 369 | next = ovl_path_next(idx, dentry, &realpath); |
Amir Goldstein | b5efccb | 2017-05-11 16:42:27 +0300 | [diff] [blame] | 370 | rdd.is_upper = ovl_dentry_upper(dentry) == realpath.dentry; |
Miklos Szeredi | c9f00fd | 2014-11-20 16:40:01 +0100 | [diff] [blame] | 371 | |
Miklos Szeredi | 9d7459d | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 372 | if (next != -1) { |
Miklos Szeredi | 9d7459d | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 373 | err = ovl_dir_read(&realpath, &rdd); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 374 | if (err) |
Miklos Szeredi | 9d7459d | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 375 | break; |
| 376 | } else { |
| 377 | /* |
| 378 | * Insert lowest layer entries before upper ones, this |
| 379 | * allows offsets to be reasonably constant |
| 380 | */ |
| 381 | list_add(&rdd.middle, rdd.list); |
Miklos Szeredi | 56656e9 | 2016-03-21 17:31:46 +0100 | [diff] [blame] | 382 | rdd.is_lowest = true; |
Miklos Szeredi | 9d7459d | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 383 | err = ovl_dir_read(&realpath, &rdd); |
| 384 | list_del(&rdd.middle); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 385 | } |
| 386 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 387 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos) |
| 391 | { |
hujianyang | 4330397 | 2014-12-11 10:30:18 +0800 | [diff] [blame] | 392 | struct list_head *p; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 393 | loff_t off = 0; |
| 394 | |
hujianyang | 4330397 | 2014-12-11 10:30:18 +0800 | [diff] [blame] | 395 | list_for_each(p, &od->cache->entries) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 396 | if (off >= pos) |
| 397 | break; |
| 398 | off++; |
| 399 | } |
hujianyang | 4330397 | 2014-12-11 10:30:18 +0800 | [diff] [blame] | 400 | /* Cursor is safe since the cache is stable */ |
| 401 | od->cursor = p; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry) |
| 405 | { |
| 406 | int res; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 407 | struct ovl_dir_cache *cache; |
| 408 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 409 | cache = ovl_dir_cache(d_inode(dentry)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 410 | if (cache && ovl_dentry_version_get(dentry) == cache->version) { |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 411 | WARN_ON(!cache->refcount); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 412 | cache->refcount++; |
| 413 | return cache; |
| 414 | } |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 415 | ovl_set_dir_cache(d_inode(dentry), NULL); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 416 | |
| 417 | cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL); |
| 418 | if (!cache) |
| 419 | return ERR_PTR(-ENOMEM); |
| 420 | |
| 421 | cache->refcount = 1; |
| 422 | INIT_LIST_HEAD(&cache->entries); |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 423 | cache->root = RB_ROOT; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 424 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 425 | res = ovl_dir_read_merged(dentry, &cache->entries, &cache->root); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 426 | if (res) { |
| 427 | ovl_cache_free(&cache->entries); |
| 428 | kfree(cache); |
| 429 | return ERR_PTR(res); |
| 430 | } |
| 431 | |
| 432 | cache->version = ovl_dentry_version_get(dentry); |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 433 | ovl_set_dir_cache(d_inode(dentry), cache); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 434 | |
| 435 | return cache; |
| 436 | } |
| 437 | |
Amir Goldstein | b5efccb | 2017-05-11 16:42:27 +0300 | [diff] [blame] | 438 | /* |
| 439 | * Set d_ino for upper entries. Non-upper entries should always report |
| 440 | * the uppermost real inode ino and should not call this function. |
| 441 | * |
| 442 | * When not all layer are on same fs, report real ino also for upper. |
| 443 | * |
| 444 | * When all layers are on the same fs, and upper has a reference to |
| 445 | * copy up origin, call vfs_getattr() on the overlay entry to make |
| 446 | * sure that d_ino will be consistent with st_ino from stat(2). |
| 447 | */ |
| 448 | static int ovl_cache_update_ino(struct path *path, struct ovl_cache_entry *p) |
| 449 | |
| 450 | { |
| 451 | struct dentry *dir = path->dentry; |
| 452 | struct dentry *this = NULL; |
| 453 | enum ovl_path_type type; |
| 454 | u64 ino = p->real_ino; |
| 455 | int err = 0; |
| 456 | |
| 457 | if (!ovl_same_sb(dir->d_sb)) |
| 458 | goto out; |
| 459 | |
| 460 | if (p->name[0] == '.') { |
| 461 | if (p->len == 1) { |
| 462 | this = dget(dir); |
| 463 | goto get; |
| 464 | } |
| 465 | if (p->len == 2 && p->name[1] == '.') { |
| 466 | /* we shall not be moved */ |
| 467 | this = dget(dir->d_parent); |
| 468 | goto get; |
| 469 | } |
| 470 | } |
| 471 | this = lookup_one_len(p->name, dir, p->len); |
| 472 | if (IS_ERR_OR_NULL(this) || !this->d_inode) { |
| 473 | if (IS_ERR(this)) { |
| 474 | err = PTR_ERR(this); |
| 475 | this = NULL; |
| 476 | goto fail; |
| 477 | } |
| 478 | goto out; |
| 479 | } |
| 480 | |
| 481 | get: |
| 482 | type = ovl_path_type(this); |
| 483 | if (OVL_TYPE_ORIGIN(type)) { |
| 484 | struct kstat stat; |
| 485 | struct path statpath = *path; |
| 486 | |
| 487 | statpath.dentry = this; |
| 488 | err = vfs_getattr(&statpath, &stat, STATX_INO, 0); |
| 489 | if (err) |
| 490 | goto fail; |
| 491 | |
| 492 | WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev); |
| 493 | ino = stat.ino; |
| 494 | } |
| 495 | |
| 496 | out: |
| 497 | p->ino = ino; |
| 498 | dput(this); |
| 499 | return err; |
| 500 | |
| 501 | fail: |
Amir Goldstein | da2e6b7 | 2017-11-22 20:27:34 +0200 | [diff] [blame] | 502 | pr_warn_ratelimited("overlayfs: failed to look up (%s) for ino (%i)\n", |
Amir Goldstein | b5efccb | 2017-05-11 16:42:27 +0300 | [diff] [blame] | 503 | p->name, err); |
| 504 | goto out; |
| 505 | } |
| 506 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 507 | static int ovl_fill_plain(struct dir_context *ctx, const char *name, |
| 508 | int namelen, loff_t offset, u64 ino, |
| 509 | unsigned int d_type) |
| 510 | { |
| 511 | struct ovl_cache_entry *p; |
| 512 | struct ovl_readdir_data *rdd = |
| 513 | container_of(ctx, struct ovl_readdir_data, ctx); |
| 514 | |
| 515 | rdd->count++; |
| 516 | p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type); |
| 517 | if (p == NULL) { |
| 518 | rdd->err = -ENOMEM; |
| 519 | return -ENOMEM; |
| 520 | } |
| 521 | list_add_tail(&p->l_node, rdd->list); |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | static int ovl_dir_read_impure(struct path *path, struct list_head *list, |
| 527 | struct rb_root *root) |
| 528 | { |
| 529 | int err; |
| 530 | struct path realpath; |
| 531 | struct ovl_cache_entry *p, *n; |
| 532 | struct ovl_readdir_data rdd = { |
| 533 | .ctx.actor = ovl_fill_plain, |
| 534 | .list = list, |
| 535 | .root = root, |
| 536 | }; |
| 537 | |
| 538 | INIT_LIST_HEAD(list); |
| 539 | *root = RB_ROOT; |
| 540 | ovl_path_upper(path->dentry, &realpath); |
| 541 | |
| 542 | err = ovl_dir_read(&realpath, &rdd); |
| 543 | if (err) |
| 544 | return err; |
| 545 | |
| 546 | list_for_each_entry_safe(p, n, list, l_node) { |
| 547 | if (strcmp(p->name, ".") != 0 && |
| 548 | strcmp(p->name, "..") != 0) { |
| 549 | err = ovl_cache_update_ino(path, p); |
| 550 | if (err) |
| 551 | return err; |
| 552 | } |
| 553 | if (p->ino == p->real_ino) { |
| 554 | list_del(&p->l_node); |
| 555 | kfree(p); |
| 556 | } else { |
| 557 | struct rb_node **newp = &root->rb_node; |
| 558 | struct rb_node *parent = NULL; |
| 559 | |
| 560 | if (WARN_ON(ovl_cache_entry_find_link(p->name, p->len, |
| 561 | &newp, &parent))) |
| 562 | return -EIO; |
| 563 | |
| 564 | rb_link_node(&p->node, parent, newp); |
| 565 | rb_insert_color(&p->node, root); |
| 566 | } |
| 567 | } |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static struct ovl_dir_cache *ovl_cache_get_impure(struct path *path) |
| 572 | { |
| 573 | int res; |
| 574 | struct dentry *dentry = path->dentry; |
| 575 | struct ovl_dir_cache *cache; |
| 576 | |
| 577 | cache = ovl_dir_cache(d_inode(dentry)); |
| 578 | if (cache && ovl_dentry_version_get(dentry) == cache->version) |
| 579 | return cache; |
| 580 | |
| 581 | /* Impure cache is not refcounted, free it here */ |
| 582 | ovl_dir_cache_free(d_inode(dentry)); |
| 583 | ovl_set_dir_cache(d_inode(dentry), NULL); |
| 584 | |
| 585 | cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL); |
| 586 | if (!cache) |
| 587 | return ERR_PTR(-ENOMEM); |
| 588 | |
| 589 | res = ovl_dir_read_impure(path, &cache->entries, &cache->root); |
| 590 | if (res) { |
| 591 | ovl_cache_free(&cache->entries); |
| 592 | kfree(cache); |
| 593 | return ERR_PTR(res); |
| 594 | } |
| 595 | if (list_empty(&cache->entries)) { |
Amir Goldstein | a5a927a | 2018-01-03 18:54:42 +0200 | [diff] [blame] | 596 | /* |
| 597 | * A good opportunity to get rid of an unneeded "impure" flag. |
| 598 | * Removing the "impure" xattr is best effort. |
| 599 | */ |
| 600 | if (!ovl_want_write(dentry)) { |
| 601 | ovl_do_removexattr(ovl_dentry_upper(dentry), |
| 602 | OVL_XATTR_IMPURE); |
| 603 | ovl_drop_write(dentry); |
| 604 | } |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 605 | ovl_clear_flag(OVL_IMPURE, d_inode(dentry)); |
| 606 | kfree(cache); |
| 607 | return NULL; |
| 608 | } |
| 609 | |
| 610 | cache->version = ovl_dentry_version_get(dentry); |
| 611 | ovl_set_dir_cache(d_inode(dentry), cache); |
| 612 | |
| 613 | return cache; |
| 614 | } |
| 615 | |
| 616 | struct ovl_readdir_translate { |
| 617 | struct dir_context *orig_ctx; |
| 618 | struct ovl_dir_cache *cache; |
| 619 | struct dir_context ctx; |
| 620 | u64 parent_ino; |
| 621 | }; |
| 622 | |
| 623 | static int ovl_fill_real(struct dir_context *ctx, const char *name, |
| 624 | int namelen, loff_t offset, u64 ino, |
| 625 | unsigned int d_type) |
| 626 | { |
| 627 | struct ovl_readdir_translate *rdt = |
| 628 | container_of(ctx, struct ovl_readdir_translate, ctx); |
| 629 | struct dir_context *orig_ctx = rdt->orig_ctx; |
| 630 | |
| 631 | if (rdt->parent_ino && strcmp(name, "..") == 0) |
| 632 | ino = rdt->parent_ino; |
| 633 | else if (rdt->cache) { |
| 634 | struct ovl_cache_entry *p; |
| 635 | |
| 636 | p = ovl_cache_entry_find(&rdt->cache->root, name, namelen); |
| 637 | if (p) |
| 638 | ino = p->ino; |
| 639 | } |
| 640 | |
| 641 | return orig_ctx->actor(orig_ctx, name, namelen, offset, ino, d_type); |
| 642 | } |
| 643 | |
| 644 | static int ovl_iterate_real(struct file *file, struct dir_context *ctx) |
| 645 | { |
| 646 | int err; |
| 647 | struct ovl_dir_file *od = file->private_data; |
| 648 | struct dentry *dir = file->f_path.dentry; |
| 649 | struct ovl_readdir_translate rdt = { |
| 650 | .ctx.actor = ovl_fill_real, |
| 651 | .orig_ctx = ctx, |
| 652 | }; |
| 653 | |
| 654 | if (OVL_TYPE_MERGE(ovl_path_type(dir->d_parent))) { |
| 655 | struct kstat stat; |
| 656 | struct path statpath = file->f_path; |
| 657 | |
| 658 | statpath.dentry = dir->d_parent; |
| 659 | err = vfs_getattr(&statpath, &stat, STATX_INO, 0); |
| 660 | if (err) |
| 661 | return err; |
| 662 | |
| 663 | WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev); |
| 664 | rdt.parent_ino = stat.ino; |
| 665 | } |
| 666 | |
| 667 | if (ovl_test_flag(OVL_IMPURE, d_inode(dir))) { |
| 668 | rdt.cache = ovl_cache_get_impure(&file->f_path); |
| 669 | if (IS_ERR(rdt.cache)) |
| 670 | return PTR_ERR(rdt.cache); |
| 671 | } |
| 672 | |
Amir Goldstein | b02a16e | 2017-11-29 07:35:21 +0200 | [diff] [blame] | 673 | err = iterate_dir(od->realfile, &rdt.ctx); |
| 674 | ctx->pos = rdt.ctx.pos; |
| 675 | |
| 676 | return err; |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 680 | static int ovl_iterate(struct file *file, struct dir_context *ctx) |
| 681 | { |
| 682 | struct ovl_dir_file *od = file->private_data; |
| 683 | struct dentry *dentry = file->f_path.dentry; |
hujianyang | 4330397 | 2014-12-11 10:30:18 +0800 | [diff] [blame] | 684 | struct ovl_cache_entry *p; |
Amir Goldstein | b5efccb | 2017-05-11 16:42:27 +0300 | [diff] [blame] | 685 | int err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 686 | |
| 687 | if (!ctx->pos) |
| 688 | ovl_dir_reset(file); |
| 689 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 690 | if (od->is_real) { |
| 691 | /* |
| 692 | * If parent is merge, then need to adjust d_ino for '..', if |
| 693 | * dir is impure then need to adjust d_ino for copied up |
| 694 | * entries. |
| 695 | */ |
| 696 | if (ovl_same_sb(dentry->d_sb) && |
| 697 | (ovl_test_flag(OVL_IMPURE, d_inode(dentry)) || |
| 698 | OVL_TYPE_MERGE(ovl_path_type(dentry->d_parent)))) { |
| 699 | return ovl_iterate_real(file, ctx); |
| 700 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 701 | return iterate_dir(od->realfile, ctx); |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 702 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 703 | |
| 704 | if (!od->cache) { |
| 705 | struct ovl_dir_cache *cache; |
| 706 | |
| 707 | cache = ovl_cache_get(dentry); |
| 708 | if (IS_ERR(cache)) |
| 709 | return PTR_ERR(cache); |
| 710 | |
| 711 | od->cache = cache; |
| 712 | ovl_seek_cursor(od, ctx->pos); |
| 713 | } |
| 714 | |
hujianyang | 4330397 | 2014-12-11 10:30:18 +0800 | [diff] [blame] | 715 | while (od->cursor != &od->cache->entries) { |
| 716 | p = list_entry(od->cursor, struct ovl_cache_entry, l_node); |
Amir Goldstein | b5efccb | 2017-05-11 16:42:27 +0300 | [diff] [blame] | 717 | if (!p->is_whiteout) { |
| 718 | if (!p->ino) { |
| 719 | err = ovl_cache_update_ino(&file->f_path, p); |
| 720 | if (err) |
| 721 | return err; |
| 722 | } |
hujianyang | 4330397 | 2014-12-11 10:30:18 +0800 | [diff] [blame] | 723 | if (!dir_emit(ctx, p->name, p->len, p->ino, p->type)) |
| 724 | break; |
Amir Goldstein | b5efccb | 2017-05-11 16:42:27 +0300 | [diff] [blame] | 725 | } |
hujianyang | 4330397 | 2014-12-11 10:30:18 +0800 | [diff] [blame] | 726 | od->cursor = p->l_node.next; |
| 727 | ctx->pos++; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 728 | } |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin) |
| 733 | { |
| 734 | loff_t res; |
| 735 | struct ovl_dir_file *od = file->private_data; |
| 736 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 737 | inode_lock(file_inode(file)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 738 | if (!file->f_pos) |
| 739 | ovl_dir_reset(file); |
| 740 | |
| 741 | if (od->is_real) { |
| 742 | res = vfs_llseek(od->realfile, offset, origin); |
| 743 | file->f_pos = od->realfile->f_pos; |
| 744 | } else { |
| 745 | res = -EINVAL; |
| 746 | |
| 747 | switch (origin) { |
| 748 | case SEEK_CUR: |
| 749 | offset += file->f_pos; |
| 750 | break; |
| 751 | case SEEK_SET: |
| 752 | break; |
| 753 | default: |
| 754 | goto out_unlock; |
| 755 | } |
| 756 | if (offset < 0) |
| 757 | goto out_unlock; |
| 758 | |
| 759 | if (offset != file->f_pos) { |
| 760 | file->f_pos = offset; |
| 761 | if (od->cache) |
| 762 | ovl_seek_cursor(od, offset); |
| 763 | } |
| 764 | res = offset; |
| 765 | } |
| 766 | out_unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 767 | inode_unlock(file_inode(file)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 768 | |
| 769 | return res; |
| 770 | } |
| 771 | |
| 772 | static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end, |
| 773 | int datasync) |
| 774 | { |
| 775 | struct ovl_dir_file *od = file->private_data; |
| 776 | struct dentry *dentry = file->f_path.dentry; |
| 777 | struct file *realfile = od->realfile; |
| 778 | |
Amir Goldstein | d796e77 | 2017-11-08 09:39:46 +0200 | [diff] [blame] | 779 | /* Nothing to sync for lower */ |
| 780 | if (!OVL_TYPE_UPPER(ovl_path_type(dentry))) |
| 781 | return 0; |
| 782 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 783 | /* |
| 784 | * Need to check if we started out being a lower dir, but got copied up |
| 785 | */ |
Amir Goldstein | d796e77 | 2017-11-08 09:39:46 +0200 | [diff] [blame] | 786 | if (!od->is_upper) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 787 | struct inode *inode = file_inode(file); |
| 788 | |
Will Deacon | 506458e | 2017-10-24 11:22:48 +0100 | [diff] [blame] | 789 | realfile = READ_ONCE(od->upperfile); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 790 | if (!realfile) { |
| 791 | struct path upperpath; |
| 792 | |
| 793 | ovl_path_upper(dentry, &upperpath); |
| 794 | realfile = ovl_path_open(&upperpath, O_RDONLY); |
Peter Zijlstra | ff7a5fb | 2017-06-07 17:43:46 +0200 | [diff] [blame] | 795 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 796 | inode_lock(inode); |
Al Viro | 3d268c9 | 2014-10-23 22:56:05 -0400 | [diff] [blame] | 797 | if (!od->upperfile) { |
| 798 | if (IS_ERR(realfile)) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 799 | inode_unlock(inode); |
Al Viro | 3d268c9 | 2014-10-23 22:56:05 -0400 | [diff] [blame] | 800 | return PTR_ERR(realfile); |
| 801 | } |
Peter Zijlstra | ff7a5fb | 2017-06-07 17:43:46 +0200 | [diff] [blame] | 802 | smp_store_release(&od->upperfile, realfile); |
Al Viro | 3d268c9 | 2014-10-23 22:56:05 -0400 | [diff] [blame] | 803 | } else { |
| 804 | /* somebody has beaten us to it */ |
| 805 | if (!IS_ERR(realfile)) |
| 806 | fput(realfile); |
| 807 | realfile = od->upperfile; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 808 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 809 | inode_unlock(inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 810 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | return vfs_fsync_range(realfile, start, end, datasync); |
| 814 | } |
| 815 | |
| 816 | static int ovl_dir_release(struct inode *inode, struct file *file) |
| 817 | { |
| 818 | struct ovl_dir_file *od = file->private_data; |
| 819 | |
| 820 | if (od->cache) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 821 | inode_lock(inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 822 | ovl_cache_put(od, file->f_path.dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 823 | inode_unlock(inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 824 | } |
| 825 | fput(od->realfile); |
| 826 | if (od->upperfile) |
| 827 | fput(od->upperfile); |
| 828 | kfree(od); |
| 829 | |
| 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | static int ovl_dir_open(struct inode *inode, struct file *file) |
| 834 | { |
| 835 | struct path realpath; |
| 836 | struct file *realfile; |
| 837 | struct ovl_dir_file *od; |
| 838 | enum ovl_path_type type; |
| 839 | |
| 840 | od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL); |
| 841 | if (!od) |
| 842 | return -ENOMEM; |
| 843 | |
| 844 | type = ovl_path_real(file->f_path.dentry, &realpath); |
| 845 | realfile = ovl_path_open(&realpath, file->f_flags); |
| 846 | if (IS_ERR(realfile)) { |
| 847 | kfree(od); |
| 848 | return PTR_ERR(realfile); |
| 849 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 850 | od->realfile = realfile; |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 851 | od->is_real = ovl_dir_is_real(file->f_path.dentry); |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 852 | od->is_upper = OVL_TYPE_UPPER(type); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 853 | file->private_data = od; |
| 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | const struct file_operations ovl_dir_operations = { |
| 859 | .read = generic_read_dir, |
| 860 | .open = ovl_dir_open, |
| 861 | .iterate = ovl_iterate, |
| 862 | .llseek = ovl_dir_llseek, |
| 863 | .fsync = ovl_dir_fsync, |
| 864 | .release = ovl_dir_release, |
| 865 | }; |
| 866 | |
| 867 | int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list) |
| 868 | { |
| 869 | int err; |
zhangyi (F) | 95e598e | 2017-10-31 22:57:00 +0200 | [diff] [blame] | 870 | struct ovl_cache_entry *p, *n; |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 871 | struct rb_root root = RB_ROOT; |
Amir Goldstein | 6d0a8a9 | 2017-11-10 13:18:07 +0200 | [diff] [blame] | 872 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 873 | |
Amir Goldstein | 6d0a8a9 | 2017-11-10 13:18:07 +0200 | [diff] [blame] | 874 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 875 | err = ovl_dir_read_merged(dentry, list, &root); |
Amir Goldstein | 6d0a8a9 | 2017-11-10 13:18:07 +0200 | [diff] [blame] | 876 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 877 | if (err) |
| 878 | return err; |
| 879 | |
| 880 | err = 0; |
| 881 | |
zhangyi (F) | 95e598e | 2017-10-31 22:57:00 +0200 | [diff] [blame] | 882 | list_for_each_entry_safe(p, n, list, l_node) { |
| 883 | /* |
| 884 | * Select whiteouts in upperdir, they should |
| 885 | * be cleared when deleting this directory. |
| 886 | */ |
| 887 | if (p->is_whiteout) { |
| 888 | if (p->is_upper) |
| 889 | continue; |
| 890 | goto del_entry; |
| 891 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 892 | |
| 893 | if (p->name[0] == '.') { |
| 894 | if (p->len == 1) |
zhangyi (F) | 95e598e | 2017-10-31 22:57:00 +0200 | [diff] [blame] | 895 | goto del_entry; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 896 | if (p->len == 2 && p->name[1] == '.') |
zhangyi (F) | 95e598e | 2017-10-31 22:57:00 +0200 | [diff] [blame] | 897 | goto del_entry; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 898 | } |
| 899 | err = -ENOTEMPTY; |
| 900 | break; |
zhangyi (F) | 95e598e | 2017-10-31 22:57:00 +0200 | [diff] [blame] | 901 | |
| 902 | del_entry: |
| 903 | list_del(&p->l_node); |
| 904 | kfree(p); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | return err; |
| 908 | } |
| 909 | |
| 910 | void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list) |
| 911 | { |
| 912 | struct ovl_cache_entry *p; |
| 913 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 914 | inode_lock_nested(upper->d_inode, I_MUTEX_CHILD); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 915 | list_for_each_entry(p, list, l_node) { |
| 916 | struct dentry *dentry; |
| 917 | |
zhangyi (F) | 95e598e | 2017-10-31 22:57:00 +0200 | [diff] [blame] | 918 | if (WARN_ON(!p->is_whiteout || !p->is_upper)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 919 | continue; |
| 920 | |
| 921 | dentry = lookup_one_len(p->name, upper, p->len); |
| 922 | if (IS_ERR(dentry)) { |
| 923 | pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n", |
| 924 | upper->d_name.name, p->len, p->name, |
| 925 | (int) PTR_ERR(dentry)); |
| 926 | continue; |
| 927 | } |
Konstantin Khlebnikov | 84889d4 | 2015-11-16 18:44:11 +0300 | [diff] [blame] | 928 | if (dentry->d_inode) |
| 929 | ovl_cleanup(upper->d_inode, dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 930 | dput(dentry); |
| 931 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 932 | inode_unlock(upper->d_inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 933 | } |
Vivek Goyal | 45aebea | 2016-02-22 09:28:34 -0500 | [diff] [blame] | 934 | |
| 935 | static int ovl_check_d_type(struct dir_context *ctx, const char *name, |
| 936 | int namelen, loff_t offset, u64 ino, |
| 937 | unsigned int d_type) |
| 938 | { |
| 939 | struct ovl_readdir_data *rdd = |
| 940 | container_of(ctx, struct ovl_readdir_data, ctx); |
| 941 | |
| 942 | /* Even if d_type is not supported, DT_DIR is returned for . and .. */ |
| 943 | if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen)) |
| 944 | return 0; |
| 945 | |
| 946 | if (d_type != DT_UNKNOWN) |
| 947 | rdd->d_type_supported = true; |
| 948 | |
| 949 | return 0; |
| 950 | } |
| 951 | |
| 952 | /* |
| 953 | * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values |
| 954 | * if error is encountered. |
| 955 | */ |
| 956 | int ovl_check_d_type_supported(struct path *realpath) |
| 957 | { |
| 958 | int err; |
| 959 | struct ovl_readdir_data rdd = { |
| 960 | .ctx.actor = ovl_check_d_type, |
| 961 | .d_type_supported = false, |
| 962 | }; |
| 963 | |
| 964 | err = ovl_dir_read(realpath, &rdd); |
| 965 | if (err) |
| 966 | return err; |
| 967 | |
| 968 | return rdd.d_type_supported; |
| 969 | } |
Miklos Szeredi | eea2fb4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 970 | |
| 971 | static void ovl_workdir_cleanup_recurse(struct path *path, int level) |
| 972 | { |
| 973 | int err; |
| 974 | struct inode *dir = path->dentry->d_inode; |
| 975 | LIST_HEAD(list); |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 976 | struct rb_root root = RB_ROOT; |
Miklos Szeredi | eea2fb4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 977 | struct ovl_cache_entry *p; |
| 978 | struct ovl_readdir_data rdd = { |
| 979 | .ctx.actor = ovl_fill_merge, |
| 980 | .dentry = NULL, |
| 981 | .list = &list, |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 982 | .root = &root, |
Miklos Szeredi | eea2fb4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 983 | .is_lowest = false, |
| 984 | }; |
| 985 | |
| 986 | err = ovl_dir_read(path, &rdd); |
| 987 | if (err) |
| 988 | goto out; |
| 989 | |
| 990 | inode_lock_nested(dir, I_MUTEX_PARENT); |
| 991 | list_for_each_entry(p, &list, l_node) { |
| 992 | struct dentry *dentry; |
| 993 | |
| 994 | if (p->name[0] == '.') { |
| 995 | if (p->len == 1) |
| 996 | continue; |
| 997 | if (p->len == 2 && p->name[1] == '.') |
| 998 | continue; |
| 999 | } |
| 1000 | dentry = lookup_one_len(p->name, path->dentry, p->len); |
| 1001 | if (IS_ERR(dentry)) |
| 1002 | continue; |
| 1003 | if (dentry->d_inode) |
| 1004 | ovl_workdir_cleanup(dir, path->mnt, dentry, level); |
| 1005 | dput(dentry); |
| 1006 | } |
| 1007 | inode_unlock(dir); |
| 1008 | out: |
| 1009 | ovl_cache_free(&list); |
| 1010 | } |
| 1011 | |
| 1012 | void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt, |
| 1013 | struct dentry *dentry, int level) |
| 1014 | { |
| 1015 | int err; |
| 1016 | |
| 1017 | if (!d_is_dir(dentry) || level > 1) { |
| 1018 | ovl_cleanup(dir, dentry); |
| 1019 | return; |
| 1020 | } |
| 1021 | |
| 1022 | err = ovl_do_rmdir(dir, dentry); |
| 1023 | if (err) { |
| 1024 | struct path path = { .mnt = mnt, .dentry = dentry }; |
| 1025 | |
| 1026 | inode_unlock(dir); |
| 1027 | ovl_workdir_cleanup_recurse(&path, level + 1); |
| 1028 | inode_lock_nested(dir, I_MUTEX_PARENT); |
| 1029 | ovl_cleanup(dir, dentry); |
| 1030 | } |
| 1031 | } |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 1032 | |
Amir Goldstein | 1eff1a1 | 2017-12-12 22:40:46 +0200 | [diff] [blame^] | 1033 | int ovl_indexdir_cleanup(struct ovl_fs *ofs) |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 1034 | { |
| 1035 | int err; |
Amir Goldstein | 1eff1a1 | 2017-12-12 22:40:46 +0200 | [diff] [blame^] | 1036 | struct dentry *indexdir = ofs->indexdir; |
Amir Goldstein | dc7ab67 | 2017-09-24 22:19:10 +0300 | [diff] [blame] | 1037 | struct dentry *index = NULL; |
Amir Goldstein | 1eff1a1 | 2017-12-12 22:40:46 +0200 | [diff] [blame^] | 1038 | struct inode *dir = indexdir->d_inode; |
| 1039 | struct path path = { .mnt = ofs->upper_mnt, .dentry = indexdir }; |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 1040 | LIST_HEAD(list); |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 1041 | struct rb_root root = RB_ROOT; |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 1042 | struct ovl_cache_entry *p; |
| 1043 | struct ovl_readdir_data rdd = { |
| 1044 | .ctx.actor = ovl_fill_merge, |
| 1045 | .dentry = NULL, |
| 1046 | .list = &list, |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 1047 | .root = &root, |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 1048 | .is_lowest = false, |
| 1049 | }; |
| 1050 | |
| 1051 | err = ovl_dir_read(&path, &rdd); |
| 1052 | if (err) |
| 1053 | goto out; |
| 1054 | |
| 1055 | inode_lock_nested(dir, I_MUTEX_PARENT); |
| 1056 | list_for_each_entry(p, &list, l_node) { |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 1057 | if (p->name[0] == '.') { |
| 1058 | if (p->len == 1) |
| 1059 | continue; |
| 1060 | if (p->len == 2 && p->name[1] == '.') |
| 1061 | continue; |
| 1062 | } |
Amir Goldstein | 1eff1a1 | 2017-12-12 22:40:46 +0200 | [diff] [blame^] | 1063 | index = lookup_one_len(p->name, indexdir, p->len); |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 1064 | if (IS_ERR(index)) { |
| 1065 | err = PTR_ERR(index); |
Amir Goldstein | dc7ab67 | 2017-09-24 22:19:10 +0300 | [diff] [blame] | 1066 | index = NULL; |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 1067 | break; |
| 1068 | } |
Amir Goldstein | 1eff1a1 | 2017-12-12 22:40:46 +0200 | [diff] [blame^] | 1069 | err = ovl_verify_index(ofs, index); |
Amir Goldstein | fa0096e | 2017-10-24 12:24:11 +0300 | [diff] [blame] | 1070 | /* Cleanup stale and orphan index entries */ |
| 1071 | if (err && (err == -ESTALE || err == -ENOENT)) |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 1072 | err = ovl_cleanup(dir, index); |
Amir Goldstein | fa0096e | 2017-10-24 12:24:11 +0300 | [diff] [blame] | 1073 | if (err) |
| 1074 | break; |
| 1075 | |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 1076 | dput(index); |
Amir Goldstein | dc7ab67 | 2017-09-24 22:19:10 +0300 | [diff] [blame] | 1077 | index = NULL; |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 1078 | } |
Amir Goldstein | dc7ab67 | 2017-09-24 22:19:10 +0300 | [diff] [blame] | 1079 | dput(index); |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 1080 | inode_unlock(dir); |
| 1081 | out: |
| 1082 | ovl_cache_free(&list); |
| 1083 | if (err) |
| 1084 | pr_err("overlayfs: failed index dir cleanup (%i)\n", err); |
| 1085 | return err; |
| 1086 | } |