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 /* Counting down from the end, since the prefix can change */
158 size_t rem = d->name.len - 1;
159 struct dentry *dentry = NULL;
162 if (d->name.name[0] != '/')
163 return ovl_lookup_single(base, d, d->name.name, d->name.len,
166 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
167 const char *s = d->name.name + d->name.len - rem;
168 const char *next = strchrnul(s, '/');
169 size_t thislen = next - s;
172 /* Verify we did not go off the rails */
173 if (WARN_ON(s[-1] != '/'))
176 err = ovl_lookup_single(base, d, s, thislen,
177 d->name.len - rem, next, &base);
187 if (WARN_ON(rem >= d->name.len))
195 * Returns next layer in stack starting from top.
196 * Returns -1 if this is the last layer.
198 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
200 struct ovl_entry *oe = dentry->d_fsdata;
204 ovl_path_upper(dentry, path);
206 return oe->numlower ? 1 : -1;
209 BUG_ON(idx > oe->numlower);
210 *path = oe->lowerstack[idx - 1];
212 return (idx < oe->numlower) ? idx + 1 : -1;
215 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
218 struct ovl_entry *oe;
219 const struct cred *old_cred;
220 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
221 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
222 struct path *stack = NULL;
223 struct dentry *upperdir, *upperdentry = NULL;
224 unsigned int ctr = 0;
225 struct inode *inode = NULL;
226 bool upperopaque = false;
227 char *upperredirect = NULL;
231 struct ovl_lookup_data d = {
232 .name = dentry->d_name,
236 .last = !poe->numlower,
240 if (dentry->d_name.len > ofs->namelen)
241 return ERR_PTR(-ENAMETOOLONG);
243 old_cred = ovl_override_creds(dentry->d_sb);
244 upperdir = ovl_upperdentry_dereference(poe);
246 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
250 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
257 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
260 if (d.redirect[0] == '/')
261 poe = dentry->d_sb->s_root->d_fsdata;
263 upperopaque = d.opaque;
266 if (!d.stop && poe->numlower) {
268 stack = kcalloc(ofs->numlower, sizeof(struct path),
274 for (i = 0; !d.stop && i < poe->numlower; i++) {
275 struct path lowerpath = poe->lowerstack[i];
277 d.last = i == poe->numlower - 1;
278 err = ovl_lookup_layer(lowerpath.dentry, &d, &this);
285 stack[ctr].dentry = this;
286 stack[ctr].mnt = lowerpath.mnt;
293 d.redirect[0] == '/' &&
294 poe != dentry->d_sb->s_root->d_fsdata) {
295 poe = dentry->d_sb->s_root->d_fsdata;
297 /* Find the current layer on the root dentry */
298 for (i = 0; i < poe->numlower; i++)
299 if (poe->lowerstack[i].mnt == lowerpath.mnt)
301 if (WARN_ON(i == poe->numlower))
306 oe = ovl_alloc_entry(ctr);
311 if (upperdentry || ctr) {
312 struct dentry *realdentry;
313 struct inode *realinode;
315 realdentry = upperdentry ? upperdentry : stack[0].dentry;
316 realinode = d_inode(realdentry);
319 if (upperdentry && !d_is_dir(upperdentry)) {
320 inode = ovl_get_inode(dentry->d_sb, realinode);
322 inode = ovl_new_inode(dentry->d_sb, realinode->i_mode,
325 ovl_inode_init(inode, realinode, !!upperdentry);
329 ovl_copyattr(realdentry->d_inode, inode);
332 revert_creds(old_cred);
333 oe->opaque = upperopaque;
334 oe->redirect = upperredirect;
335 oe->__upperdentry = upperdentry;
336 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
339 dentry->d_fsdata = oe;
340 d_add(dentry, inode);
347 for (i = 0; i < ctr; i++)
348 dput(stack[i].dentry);
352 kfree(upperredirect);
355 revert_creds(old_cred);
359 bool ovl_lower_positive(struct dentry *dentry)
361 struct ovl_entry *oe = dentry->d_fsdata;
362 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
363 const struct qstr *name = &dentry->d_name;
365 bool positive = false;
369 * If dentry is negative, then lower is positive iff this is a
372 if (!dentry->d_inode)
375 /* Negative upper -> positive lower */
376 if (!oe->__upperdentry)
379 /* Positive upper -> have to look up lower to see whether it exists */
380 for (i = 0; !done && !positive && i < poe->numlower; i++) {
382 struct dentry *lowerdir = poe->lowerstack[i].dentry;
384 this = lookup_one_len_unlocked(name->name, lowerdir,
387 switch (PTR_ERR(this)) {
394 * Assume something is there, we just couldn't
402 positive = !ovl_is_whiteout(this);