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/cred.h>
12 #include <linux/namei.h>
13 #include <linux/xattr.h>
14 #include <linux/ratelimit.h>
15 #include "overlayfs.h"
16 #include "ovl_entry.h"
18 struct ovl_lookup_data {
27 static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
28 size_t prelen, const char *post)
31 char *s, *next, *buf = NULL;
33 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
35 if (res == -ENODATA || res == -EOPNOTSUPP)
39 buf = kzalloc(prelen + res + strlen(post) + 1, GFP_TEMPORARY);
46 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
52 for (s = buf; *s++ == '/'; s = next) {
53 next = strchrnul(s, '/');
58 if (strchr(buf, '/') != NULL)
61 memmove(buf + prelen, buf, res);
62 memcpy(buf, d->name.name, prelen);
68 d->name.name = d->redirect;
69 d->name.len = strlen(d->redirect);
77 pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
80 pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
84 static bool ovl_is_opaquedir(struct dentry *dentry)
89 if (!d_is_dir(dentry))
92 res = vfs_getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
93 if (res == 1 && val == 'y')
99 static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
100 const char *name, unsigned int namelen,
101 size_t prelen, const char *post,
107 this = lookup_one_len_unlocked(name, base, namelen);
111 if (err == -ENOENT || err == -ENAMETOOLONG)
118 if (ovl_dentry_weird(this)) {
119 /* Don't support traversing automounts and other weirdness */
123 if (ovl_is_whiteout(this)) {
124 d->stop = d->opaque = true;
127 if (!d_can_lookup(this)) {
134 if (!d->last && ovl_is_opaquedir(this)) {
135 d->stop = d->opaque = true;
138 err = ovl_check_redirect(this, d, prelen, post);
155 static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
158 /* Counting down from the end, since the prefix can change */
159 size_t rem = d->name.len - 1;
160 struct dentry *dentry = NULL;
163 if (d->name.name[0] != '/')
164 return ovl_lookup_single(base, d, d->name.name, d->name.len,
167 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
168 const char *s = d->name.name + d->name.len - rem;
169 const char *next = strchrnul(s, '/');
170 size_t thislen = next - s;
173 /* Verify we did not go off the rails */
174 if (WARN_ON(s[-1] != '/'))
177 err = ovl_lookup_single(base, d, s, thislen,
178 d->name.len - rem, next, &base);
188 if (WARN_ON(rem >= d->name.len))
196 * Returns next layer in stack starting from top.
197 * Returns -1 if this is the last layer.
199 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
201 struct ovl_entry *oe = dentry->d_fsdata;
205 ovl_path_upper(dentry, path);
207 return oe->numlower ? 1 : -1;
210 BUG_ON(idx > oe->numlower);
211 *path = oe->lowerstack[idx - 1];
213 return (idx < oe->numlower) ? idx + 1 : -1;
216 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
219 struct ovl_entry *oe;
220 const struct cred *old_cred;
221 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
222 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
223 struct path *stack = NULL;
224 struct dentry *upperdir, *upperdentry = NULL;
225 unsigned int ctr = 0;
226 struct inode *inode = NULL;
227 bool upperopaque = false;
228 char *upperredirect = NULL;
232 struct ovl_lookup_data d = {
233 .name = dentry->d_name,
237 .last = !poe->numlower,
241 if (dentry->d_name.len > ofs->namelen)
242 return ERR_PTR(-ENAMETOOLONG);
244 old_cred = ovl_override_creds(dentry->d_sb);
245 upperdir = ovl_upperdentry_dereference(poe);
247 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
251 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
258 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
261 if (d.redirect[0] == '/')
262 poe = dentry->d_sb->s_root->d_fsdata;
264 upperopaque = d.opaque;
267 if (!d.stop && poe->numlower) {
269 stack = kcalloc(ofs->numlower, sizeof(struct path),
275 for (i = 0; !d.stop && i < poe->numlower; i++) {
276 struct path lowerpath = poe->lowerstack[i];
278 d.last = i == poe->numlower - 1;
279 err = ovl_lookup_layer(lowerpath.dentry, &d, &this);
286 stack[ctr].dentry = this;
287 stack[ctr].mnt = lowerpath.mnt;
294 d.redirect[0] == '/' &&
295 poe != dentry->d_sb->s_root->d_fsdata) {
296 poe = dentry->d_sb->s_root->d_fsdata;
298 /* Find the current layer on the root dentry */
299 for (i = 0; i < poe->numlower; i++)
300 if (poe->lowerstack[i].mnt == lowerpath.mnt)
302 if (WARN_ON(i == poe->numlower))
307 oe = ovl_alloc_entry(ctr);
312 if (upperdentry || ctr) {
313 struct dentry *realdentry;
314 struct inode *realinode;
316 realdentry = upperdentry ? upperdentry : stack[0].dentry;
317 realinode = d_inode(realdentry);
320 if (upperdentry && !d_is_dir(upperdentry)) {
321 inode = ovl_get_inode(dentry->d_sb, realinode);
323 inode = ovl_new_inode(dentry->d_sb, realinode->i_mode,
326 ovl_inode_init(inode, realinode, !!upperdentry);
330 ovl_copyattr(realdentry->d_inode, inode);
333 revert_creds(old_cred);
334 oe->opaque = upperopaque;
335 oe->redirect = upperredirect;
336 oe->__upperdentry = upperdentry;
337 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
340 dentry->d_fsdata = oe;
341 d_add(dentry, inode);
348 for (i = 0; i < ctr; i++)
349 dput(stack[i].dentry);
353 kfree(upperredirect);
356 revert_creds(old_cred);
360 bool ovl_lower_positive(struct dentry *dentry)
362 struct ovl_entry *oe = dentry->d_fsdata;
363 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
364 const struct qstr *name = &dentry->d_name;
366 bool positive = false;
370 * If dentry is negative, then lower is positive iff this is a
373 if (!dentry->d_inode)
376 /* Negative upper -> positive lower */
377 if (!oe->__upperdentry)
380 /* Positive upper -> have to look up lower to see whether it exists */
381 for (i = 0; !done && !positive && i < poe->numlower; i++) {
383 struct dentry *lowerdir = poe->lowerstack[i].dentry;
385 this = lookup_one_len_unlocked(name->name, lowerdir,
388 switch (PTR_ERR(this)) {
395 * Assume something is there, we just couldn't
403 positive = !ovl_is_whiteout(this);