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 "overlayfs.h"
14 #include "ovl_entry.h"
16 static struct dentry *ovl_lookup_real(struct dentry *dir,
17 const struct qstr *name)
19 struct dentry *dentry;
21 dentry = lookup_one_len_unlocked(name->name, dir, name->len);
23 if (PTR_ERR(dentry) == -ENOENT ||
24 PTR_ERR(dentry) == -ENAMETOOLONG)
26 } else if (!dentry->d_inode) {
29 } else if (ovl_dentry_weird(dentry)) {
31 /* Don't support traversing automounts and other weirdness */
32 dentry = ERR_PTR(-EREMOTE);
37 static bool ovl_is_opaquedir(struct dentry *dentry)
42 if (!d_is_dir(dentry))
45 res = vfs_getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
46 if (res == 1 && val == 'y')
53 * Returns next layer in stack starting from top.
54 * Returns -1 if this is the last layer.
56 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
58 struct ovl_entry *oe = dentry->d_fsdata;
62 ovl_path_upper(dentry, path);
64 return oe->numlower ? 1 : -1;
67 BUG_ON(idx > oe->numlower);
68 *path = oe->lowerstack[idx - 1];
70 return (idx < oe->numlower) ? idx + 1 : -1;
73 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
77 const struct cred *old_cred;
78 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
79 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
80 struct path *stack = NULL;
81 struct dentry *upperdir, *upperdentry = NULL;
83 struct inode *inode = NULL;
84 bool upperopaque = false;
91 if (dentry->d_name.len > ofs->namelen)
92 return ERR_PTR(-ENAMETOOLONG);
94 old_cred = ovl_override_creds(dentry->d_sb);
95 upperdir = ovl_upperdentry_dereference(poe);
97 this = ovl_lookup_real(upperdir, &dentry->d_name);
103 if (unlikely(ovl_dentry_remote(this))) {
108 if (ovl_is_whiteout(this)) {
111 stop = upperopaque = true;
112 } else if (!d_is_dir(this)) {
116 if (poe->numlower && ovl_is_opaquedir(this))
117 stop = upperopaque = true;
123 if (!stop && poe->numlower) {
125 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
130 for (i = 0; !stop && i < poe->numlower; i++) {
131 struct path lowerpath = poe->lowerstack[i];
133 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
140 if (ovl_is_whiteout(this)) {
145 * If this is a non-directory then stop here.
147 if (!d_is_dir(this)) {
155 * Only makes sense to check opaque dir if this is not
156 * the lowermost layer.
158 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
162 stack[ctr].dentry = this;
163 stack[ctr].mnt = lowerpath.mnt;
167 oe = ovl_alloc_entry(ctr);
172 if (upperdentry || ctr) {
173 struct dentry *realdentry;
174 struct inode *realinode;
176 realdentry = upperdentry ? upperdentry : stack[0].dentry;
177 realinode = d_inode(realdentry);
180 if (upperdentry && !d_is_dir(upperdentry)) {
181 inode = ovl_get_inode(dentry->d_sb, realinode);
183 inode = ovl_new_inode(dentry->d_sb, realinode->i_mode,
186 ovl_inode_init(inode, realinode, !!upperdentry);
190 ovl_copyattr(realdentry->d_inode, inode);
193 revert_creds(old_cred);
194 oe->opaque = upperopaque;
195 oe->__upperdentry = upperdentry;
196 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
198 dentry->d_fsdata = oe;
199 d_add(dentry, inode);
206 for (i = 0; i < ctr; i++)
207 dput(stack[i].dentry);
212 revert_creds(old_cred);
216 bool ovl_lower_positive(struct dentry *dentry)
218 struct ovl_entry *oe = dentry->d_fsdata;
219 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
220 const struct qstr *name = &dentry->d_name;
222 bool positive = false;
226 * If dentry is negative, then lower is positive iff this is a
229 if (!dentry->d_inode)
232 /* Negative upper -> positive lower */
233 if (!oe->__upperdentry)
236 /* Positive upper -> have to look up lower to see whether it exists */
237 for (i = 0; !done && !positive && i < poe->numlower; i++) {
239 struct dentry *lowerdir = poe->lowerstack[i].dentry;
241 this = lookup_one_len_unlocked(name->name, lowerdir,
244 switch (PTR_ERR(this)) {
251 * Assume something is there, we just couldn't
259 positive = !ovl_is_whiteout(this);