2 * Copyright (C) 2011 Novell Inc.
3 * Copyright (C) 2016 Red Hat, Inc.
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.
11 #include <linux/namei.h>
12 #include <linux/xattr.h>
13 #include <linux/ratelimit.h>
14 #include "overlayfs.h"
15 #include "ovl_entry.h"
17 struct ovl_lookup_data {
26 static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
27 size_t prelen, const char *post)
30 char *s, *next, *buf = NULL;
32 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
34 if (res == -ENODATA || res == -EOPNOTSUPP)
38 buf = kzalloc(prelen + res + strlen(post) + 1, GFP_TEMPORARY);
45 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
51 for (s = buf; *s++ == '/'; s = next) {
52 next = strchrnul(s, '/');
57 if (strchr(buf, '/') != NULL)
60 memmove(buf + prelen, buf, res);
61 memcpy(buf, d->name.name, prelen);
67 d->name.name = d->redirect;
68 d->name.len = strlen(d->redirect);
76 pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
79 pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
83 static bool ovl_is_opaquedir(struct dentry *dentry)
88 if (!d_is_dir(dentry))
91 res = vfs_getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
92 if (res == 1 && val == 'y')
98 static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
99 const char *name, unsigned int namelen,
100 size_t prelen, const char *post,
106 this = lookup_one_len_unlocked(name, base, namelen);
110 if (err == -ENOENT || err == -ENAMETOOLONG)
117 if (ovl_dentry_weird(this)) {
118 /* Don't support traversing automounts and other weirdness */
122 if (ovl_is_whiteout(this)) {
123 d->stop = d->opaque = true;
126 if (!d_can_lookup(this)) {
133 if (!d->last && ovl_is_opaquedir(this)) {
134 d->stop = d->opaque = true;
137 err = ovl_check_redirect(this, d, prelen, post);
154 static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
157 const char *s = d->name.name;
158 struct dentry *dentry = NULL;
162 return ovl_lookup_single(base, d, d->name.name, d->name.len,
165 while (*s++ == '/' && !IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
166 const char *next = strchrnul(s, '/');
167 size_t slen = strlen(s);
169 if (WARN_ON(slen > d->name.len) ||
170 WARN_ON(strcmp(d->name.name + d->name.len - slen, s)))
173 err = ovl_lookup_single(base, d, s, next - s,
174 d->name.len - slen, next, &base);
186 * Returns next layer in stack starting from top.
187 * Returns -1 if this is the last layer.
189 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
191 struct ovl_entry *oe = dentry->d_fsdata;
195 ovl_path_upper(dentry, path);
197 return oe->numlower ? 1 : -1;
200 BUG_ON(idx > oe->numlower);
201 *path = oe->lowerstack[idx - 1];
203 return (idx < oe->numlower) ? idx + 1 : -1;
206 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
209 struct ovl_entry *oe;
210 const struct cred *old_cred;
211 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
212 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
213 struct path *stack = NULL;
214 struct dentry *upperdir, *upperdentry = NULL;
215 unsigned int ctr = 0;
216 struct inode *inode = NULL;
217 bool upperopaque = false;
218 char *upperredirect = NULL;
222 struct ovl_lookup_data d = {
223 .name = dentry->d_name,
227 .last = !poe->numlower,
231 if (dentry->d_name.len > ofs->namelen)
232 return ERR_PTR(-ENAMETOOLONG);
234 old_cred = ovl_override_creds(dentry->d_sb);
235 upperdir = ovl_upperdentry_dereference(poe);
237 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
241 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
248 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
251 if (d.redirect[0] == '/')
252 poe = dentry->d_sb->s_root->d_fsdata;
254 upperopaque = d.opaque;
257 if (!d.stop && poe->numlower) {
259 stack = kcalloc(ofs->numlower, sizeof(struct path),
265 for (i = 0; !d.stop && i < poe->numlower; i++) {
266 struct path lowerpath = poe->lowerstack[i];
268 d.last = i == poe->numlower - 1;
269 err = ovl_lookup_layer(lowerpath.dentry, &d, &this);
276 stack[ctr].dentry = this;
277 stack[ctr].mnt = lowerpath.mnt;
284 d.redirect[0] == '/' &&
285 poe != dentry->d_sb->s_root->d_fsdata) {
286 poe = dentry->d_sb->s_root->d_fsdata;
288 /* Find the current layer on the root dentry */
289 for (i = 0; i < poe->numlower; i++)
290 if (poe->lowerstack[i].mnt == lowerpath.mnt)
292 if (WARN_ON(i == poe->numlower))
297 oe = ovl_alloc_entry(ctr);
302 if (upperdentry || ctr) {
303 struct dentry *realdentry;
304 struct inode *realinode;
306 realdentry = upperdentry ? upperdentry : stack[0].dentry;
307 realinode = d_inode(realdentry);
310 if (upperdentry && !d_is_dir(upperdentry)) {
311 inode = ovl_get_inode(dentry->d_sb, realinode);
313 inode = ovl_new_inode(dentry->d_sb, realinode->i_mode,
316 ovl_inode_init(inode, realinode, !!upperdentry);
320 ovl_copyattr(realdentry->d_inode, inode);
323 revert_creds(old_cred);
324 oe->opaque = upperopaque;
325 oe->redirect = upperredirect;
326 oe->__upperdentry = upperdentry;
327 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
330 dentry->d_fsdata = oe;
331 d_add(dentry, inode);
338 for (i = 0; i < ctr; i++)
339 dput(stack[i].dentry);
343 kfree(upperredirect);
346 revert_creds(old_cred);
350 bool ovl_lower_positive(struct dentry *dentry)
352 struct ovl_entry *oe = dentry->d_fsdata;
353 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
354 const struct qstr *name = &dentry->d_name;
356 bool positive = false;
360 * If dentry is negative, then lower is positive iff this is a
363 if (!dentry->d_inode)
366 /* Negative upper -> positive lower */
367 if (!oe->__upperdentry)
370 /* Positive upper -> have to look up lower to see whether it exists */
371 for (i = 0; !done && !positive && i < poe->numlower; i++) {
373 struct dentry *lowerdir = poe->lowerstack[i].dentry;
375 this = lookup_one_len_unlocked(name->name, lowerdir,
378 switch (PTR_ERR(this)) {
385 * Assume something is there, we just couldn't
393 positive = !ovl_is_whiteout(this);