x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / fs / overlayfs / namei.c
blobb8b077821fb03bea9d63b3bf3508039836409e1b
1 /*
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.
8 */
10 #include <linux/fs.h>
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 {
19 struct qstr name;
20 bool is_dir;
21 bool opaque;
22 bool stop;
23 bool last;
24 char *redirect;
27 static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
28 size_t prelen, const char *post)
30 int res;
31 char *s, *next, *buf = NULL;
33 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
34 if (res < 0) {
35 if (res == -ENODATA || res == -EOPNOTSUPP)
36 return 0;
37 goto fail;
39 buf = kzalloc(prelen + res + strlen(post) + 1, GFP_TEMPORARY);
40 if (!buf)
41 return -ENOMEM;
43 if (res == 0)
44 goto invalid;
46 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
47 if (res < 0)
48 goto fail;
49 if (res == 0)
50 goto invalid;
51 if (buf[0] == '/') {
52 for (s = buf; *s++ == '/'; s = next) {
53 next = strchrnul(s, '/');
54 if (s == next)
55 goto invalid;
57 } else {
58 if (strchr(buf, '/') != NULL)
59 goto invalid;
61 memmove(buf + prelen, buf, res);
62 memcpy(buf, d->name.name, prelen);
65 strcat(buf, post);
66 kfree(d->redirect);
67 d->redirect = buf;
68 d->name.name = d->redirect;
69 d->name.len = strlen(d->redirect);
71 return 0;
73 err_free:
74 kfree(buf);
75 return 0;
76 fail:
77 pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
78 goto err_free;
79 invalid:
80 pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
81 goto err_free;
84 static bool ovl_is_opaquedir(struct dentry *dentry)
86 int res;
87 char val;
89 if (!d_is_dir(dentry))
90 return false;
92 res = vfs_getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
93 if (res == 1 && val == 'y')
94 return true;
96 return false;
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,
102 struct dentry **ret)
104 struct dentry *this;
105 int err;
107 this = lookup_one_len_unlocked(name, base, namelen);
108 if (IS_ERR(this)) {
109 err = PTR_ERR(this);
110 this = NULL;
111 if (err == -ENOENT || err == -ENAMETOOLONG)
112 goto out;
113 goto out_err;
115 if (!this->d_inode)
116 goto put_and_out;
118 if (ovl_dentry_weird(this)) {
119 /* Don't support traversing automounts and other weirdness */
120 err = -EREMOTE;
121 goto out_err;
123 if (ovl_is_whiteout(this)) {
124 d->stop = d->opaque = true;
125 goto put_and_out;
127 if (!d_can_lookup(this)) {
128 d->stop = true;
129 if (d->is_dir)
130 goto put_and_out;
131 goto out;
133 d->is_dir = true;
134 if (!d->last && ovl_is_opaquedir(this)) {
135 d->stop = d->opaque = true;
136 goto out;
138 err = ovl_check_redirect(this, d, prelen, post);
139 if (err)
140 goto out_err;
141 out:
142 *ret = this;
143 return 0;
145 put_and_out:
146 dput(this);
147 this = NULL;
148 goto out;
150 out_err:
151 dput(this);
152 return err;
155 static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
156 struct dentry **ret)
158 /* Counting down from the end, since the prefix can change */
159 size_t rem = d->name.len - 1;
160 struct dentry *dentry = NULL;
161 int err;
163 if (d->name.name[0] != '/')
164 return ovl_lookup_single(base, d, d->name.name, d->name.len,
165 0, "", ret);
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;
171 bool end = !next[0];
173 /* Verify we did not go off the rails */
174 if (WARN_ON(s[-1] != '/'))
175 return -EIO;
177 err = ovl_lookup_single(base, d, s, thislen,
178 d->name.len - rem, next, &base);
179 dput(dentry);
180 if (err)
181 return err;
182 dentry = base;
183 if (end)
184 break;
186 rem -= thislen + 1;
188 if (WARN_ON(rem >= d->name.len))
189 return -EIO;
191 *ret = dentry;
192 return 0;
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;
203 BUG_ON(idx < 0);
204 if (idx == 0) {
205 ovl_path_upper(dentry, path);
206 if (path->dentry)
207 return oe->numlower ? 1 : -1;
208 idx++;
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,
217 unsigned int flags)
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;
229 struct dentry *this;
230 unsigned int i;
231 int err;
232 struct ovl_lookup_data d = {
233 .name = dentry->d_name,
234 .is_dir = false,
235 .opaque = false,
236 .stop = false,
237 .last = !poe->numlower,
238 .redirect = NULL,
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);
246 if (upperdir) {
247 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
248 if (err)
249 goto out;
251 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
252 dput(upperdentry);
253 err = -EREMOTE;
254 goto out;
257 if (d.redirect) {
258 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
259 if (!upperredirect)
260 goto out_put_upper;
261 if (d.redirect[0] == '/')
262 poe = dentry->d_sb->s_root->d_fsdata;
264 upperopaque = d.opaque;
267 if (!d.stop && poe->numlower) {
268 err = -ENOMEM;
269 stack = kcalloc(ofs->numlower, sizeof(struct path),
270 GFP_TEMPORARY);
271 if (!stack)
272 goto out_put_upper;
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);
280 if (err)
281 goto out_put;
283 if (!this)
284 continue;
286 stack[ctr].dentry = this;
287 stack[ctr].mnt = lowerpath.mnt;
288 ctr++;
290 if (d.stop)
291 break;
293 if (d.redirect &&
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)
301 break;
302 if (WARN_ON(i == poe->numlower))
303 break;
307 oe = ovl_alloc_entry(ctr);
308 err = -ENOMEM;
309 if (!oe)
310 goto out_put;
312 if (upperdentry || ctr) {
313 struct dentry *realdentry;
314 struct inode *realinode;
316 realdentry = upperdentry ? upperdentry : stack[0].dentry;
317 realinode = d_inode(realdentry);
319 err = -ENOMEM;
320 if (upperdentry && !d_is_dir(upperdentry)) {
321 inode = ovl_get_inode(dentry->d_sb, realinode);
322 } else {
323 inode = ovl_new_inode(dentry->d_sb, realinode->i_mode,
324 realinode->i_rdev);
325 if (inode)
326 ovl_inode_init(inode, realinode, !!upperdentry);
328 if (!inode)
329 goto out_free_oe;
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);
338 kfree(stack);
339 kfree(d.redirect);
340 dentry->d_fsdata = oe;
341 d_add(dentry, inode);
343 return NULL;
345 out_free_oe:
346 kfree(oe);
347 out_put:
348 for (i = 0; i < ctr; i++)
349 dput(stack[i].dentry);
350 kfree(stack);
351 out_put_upper:
352 dput(upperdentry);
353 kfree(upperredirect);
354 out:
355 kfree(d.redirect);
356 revert_creds(old_cred);
357 return ERR_PTR(err);
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;
365 unsigned int i;
366 bool positive = false;
367 bool done = false;
370 * If dentry is negative, then lower is positive iff this is a
371 * whiteout.
373 if (!dentry->d_inode)
374 return oe->opaque;
376 /* Negative upper -> positive lower */
377 if (!oe->__upperdentry)
378 return true;
380 /* Positive upper -> have to look up lower to see whether it exists */
381 for (i = 0; !done && !positive && i < poe->numlower; i++) {
382 struct dentry *this;
383 struct dentry *lowerdir = poe->lowerstack[i].dentry;
385 this = lookup_one_len_unlocked(name->name, lowerdir,
386 name->len);
387 if (IS_ERR(this)) {
388 switch (PTR_ERR(this)) {
389 case -ENOENT:
390 case -ENAMETOOLONG:
391 break;
393 default:
395 * Assume something is there, we just couldn't
396 * access it.
398 positive = true;
399 break;
401 } else {
402 if (this->d_inode) {
403 positive = !ovl_is_whiteout(this);
404 done = true;
406 dput(this);
410 return positive;