blob: 76e39aaaa0386f90d3f5d28a37d91cedf3340e4a [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
2 *
3 * Copyright (C) 2011 Novell Inc.
4 *
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 */
9
10#include <linux/fs.h>
11#include <linux/namei.h>
12#include <linux/xattr.h>
13#include <linux/security.h>
14#include <linux/cred.h>
Miklos Szeredi3ea22a72016-12-16 11:02:57 +010015#include <linux/module.h>
Miklos Szeredi38b25692016-09-01 11:11:59 +020016#include <linux/posix_acl.h>
17#include <linux/posix_acl_xattr.h>
Richard Weinberger6a45b362016-09-16 11:45:24 +020018#include <linux/atomic.h>
Miklos Szeredia6c60652016-12-16 11:02:56 +010019#include <linux/ratelimit.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020020#include "overlayfs.h"
21
Miklos Szeredi3ea22a72016-12-16 11:02:57 +010022static unsigned short ovl_redirect_max = 256;
23module_param_named(redirect_max, ovl_redirect_max, ushort, 0644);
24MODULE_PARM_DESC(ovl_redirect_max,
25 "Maximum length of absolute redirect xattr value");
26
Miklos Szeredie9be9d52014-10-24 00:14:38 +020027void ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
28{
29 int err;
30
31 dget(wdentry);
David Howellse36cb0b2015-01-29 12:02:35 +000032 if (d_is_dir(wdentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +020033 err = ovl_do_rmdir(wdir, wdentry);
34 else
35 err = ovl_do_unlink(wdir, wdentry);
36 dput(wdentry);
37
38 if (err) {
39 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
40 wdentry, err);
41 }
42}
43
44struct dentry *ovl_lookup_temp(struct dentry *workdir, struct dentry *dentry)
45{
46 struct dentry *temp;
47 char name[20];
Richard Weinberger6a45b362016-09-16 11:45:24 +020048 static atomic_t temp_id = ATOMIC_INIT(0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020049
Richard Weinberger6a45b362016-09-16 11:45:24 +020050 /* counter is allowed to wrap, since temp dentries are ephemeral */
51 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
Miklos Szeredie9be9d52014-10-24 00:14:38 +020052
53 temp = lookup_one_len(name, workdir, strlen(name));
54 if (!IS_ERR(temp) && temp->d_inode) {
55 pr_err("overlayfs: workdir/%s already exists\n", name);
56 dput(temp);
57 temp = ERR_PTR(-EIO);
58 }
59
60 return temp;
61}
62
63/* caller holds i_mutex on workdir */
64static struct dentry *ovl_whiteout(struct dentry *workdir,
65 struct dentry *dentry)
66{
67 int err;
68 struct dentry *whiteout;
69 struct inode *wdir = workdir->d_inode;
70
71 whiteout = ovl_lookup_temp(workdir, dentry);
72 if (IS_ERR(whiteout))
73 return whiteout;
74
75 err = ovl_do_whiteout(wdir, whiteout);
76 if (err) {
77 dput(whiteout);
78 whiteout = ERR_PTR(err);
79 }
80
81 return whiteout;
82}
83
84int ovl_create_real(struct inode *dir, struct dentry *newdentry,
85 struct kstat *stat, const char *link,
86 struct dentry *hardlink, bool debug)
87{
88 int err;
89
90 if (newdentry->d_inode)
91 return -ESTALE;
92
93 if (hardlink) {
94 err = ovl_do_link(hardlink, dir, newdentry, debug);
95 } else {
96 switch (stat->mode & S_IFMT) {
97 case S_IFREG:
98 err = ovl_do_create(dir, newdentry, stat->mode, debug);
99 break;
100
101 case S_IFDIR:
102 err = ovl_do_mkdir(dir, newdentry, stat->mode, debug);
103 break;
104
105 case S_IFCHR:
106 case S_IFBLK:
107 case S_IFIFO:
108 case S_IFSOCK:
109 err = ovl_do_mknod(dir, newdentry,
110 stat->mode, stat->rdev, debug);
111 break;
112
113 case S_IFLNK:
114 err = ovl_do_symlink(dir, newdentry, link, debug);
115 break;
116
117 default:
118 err = -EPERM;
119 }
120 }
121 if (!err && WARN_ON(!newdentry->d_inode)) {
122 /*
123 * Not quite sure if non-instantiated dentry is legal or not.
124 * VFS doesn't seem to care so check and warn here.
125 */
126 err = -ENOENT;
127 }
128 return err;
129}
130
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100131static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200132{
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100133 int err;
134
135 err = ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
136 if (!err)
137 ovl_dentry_set_opaque(dentry);
138
139 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200140}
141
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200142static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
143 struct kstat *stat)
144{
145 int err;
146 enum ovl_path_type type;
147 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400148 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200149
150 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400151 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200152 err = vfs_getattr(&realpath, stat);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400153 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200154 if (err)
155 return err;
156
157 stat->dev = dentry->d_sb->s_dev;
158 stat->ino = dentry->d_inode->i_ino;
159
160 /*
161 * It's probably not worth it to count subdirs to get the
162 * correct link count. nlink=1 seems to pacify 'find' and
163 * other utilities.
164 */
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100165 if (OVL_TYPE_MERGE(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200166 stat->nlink = 1;
167
168 return 0;
169}
170
Vivek Goyal72e48482016-06-16 10:09:14 -0400171/* Common operations required to be done after creation of file on upper */
172static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200173 struct dentry *newdentry, bool hardlink)
Vivek Goyal72e48482016-06-16 10:09:14 -0400174{
175 ovl_dentry_version_inc(dentry->d_parent);
176 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200177 if (!hardlink) {
178 ovl_inode_update(inode, d_inode(newdentry));
179 ovl_copyattr(newdentry->d_inode, inode);
180 } else {
181 WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
182 inc_nlink(inode);
183 }
Vivek Goyal72e48482016-06-16 10:09:14 -0400184 d_instantiate(dentry, inode);
185}
186
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200187static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
188 struct kstat *stat, const char *link,
189 struct dentry *hardlink)
190{
191 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
192 struct inode *udir = upperdir->d_inode;
193 struct dentry *newdentry;
194 int err;
195
Miklos Szeredi38b25692016-09-01 11:11:59 +0200196 if (!hardlink && !IS_POSIXACL(udir))
197 stat->mode &= ~current_umask();
198
Al Viro59551022016-01-22 15:40:57 -0500199 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200200 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
201 dentry->d_name.len);
202 err = PTR_ERR(newdentry);
203 if (IS_ERR(newdentry))
204 goto out_unlock;
205 err = ovl_create_real(udir, newdentry, stat, link, hardlink, false);
206 if (err)
207 goto out_dput;
208
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200209 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200210 newdentry = NULL;
211out_dput:
212 dput(newdentry);
213out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500214 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200215 return err;
216}
217
218static int ovl_lock_rename_workdir(struct dentry *workdir,
219 struct dentry *upperdir)
220{
221 /* Workdir should not be the same as upperdir */
222 if (workdir == upperdir)
223 goto err;
224
225 /* Workdir should not be subdir of upperdir and vice versa */
226 if (lock_rename(workdir, upperdir) != NULL)
227 goto err_unlock;
228
229 return 0;
230
231err_unlock:
232 unlock_rename(workdir, upperdir);
233err:
234 pr_err("overlayfs: failed to lock workdir+upperdir\n");
235 return -EIO;
236}
237
238static struct dentry *ovl_clear_empty(struct dentry *dentry,
239 struct list_head *list)
240{
241 struct dentry *workdir = ovl_workdir(dentry);
242 struct inode *wdir = workdir->d_inode;
243 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
244 struct inode *udir = upperdir->d_inode;
245 struct path upperpath;
246 struct dentry *upper;
247 struct dentry *opaquedir;
248 struct kstat stat;
249 int err;
250
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200251 if (WARN_ON(!workdir))
252 return ERR_PTR(-EROFS);
253
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200254 err = ovl_lock_rename_workdir(workdir, upperdir);
255 if (err)
256 goto out;
257
258 ovl_path_upper(dentry, &upperpath);
259 err = vfs_getattr(&upperpath, &stat);
260 if (err)
261 goto out_unlock;
262
263 err = -ESTALE;
264 if (!S_ISDIR(stat.mode))
265 goto out_unlock;
266 upper = upperpath.dentry;
267 if (upper->d_parent->d_inode != udir)
268 goto out_unlock;
269
270 opaquedir = ovl_lookup_temp(workdir, dentry);
271 err = PTR_ERR(opaquedir);
272 if (IS_ERR(opaquedir))
273 goto out_unlock;
274
275 err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true);
276 if (err)
277 goto out_dput;
278
279 err = ovl_copy_xattr(upper, opaquedir);
280 if (err)
281 goto out_cleanup;
282
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100283 err = ovl_set_opaque(dentry, opaquedir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200284 if (err)
285 goto out_cleanup;
286
Al Viro59551022016-01-22 15:40:57 -0500287 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200288 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500289 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200290 if (err)
291 goto out_cleanup;
292
293 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
294 if (err)
295 goto out_cleanup;
296
297 ovl_cleanup_whiteouts(upper, list);
298 ovl_cleanup(wdir, upper);
299 unlock_rename(workdir, upperdir);
300
301 /* dentry's upper doesn't match now, get rid of it */
302 d_drop(dentry);
303
304 return opaquedir;
305
306out_cleanup:
307 ovl_cleanup(wdir, opaquedir);
308out_dput:
309 dput(opaquedir);
310out_unlock:
311 unlock_rename(workdir, upperdir);
312out:
313 return ERR_PTR(err);
314}
315
Miklos Szeredia105d682014-11-20 16:39:59 +0100316static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200317{
318 int err;
319 struct dentry *ret = NULL;
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200320 enum ovl_path_type type = ovl_path_type(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200321 LIST_HEAD(list);
322
323 err = ovl_check_empty_dir(dentry, &list);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200324 if (err) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200325 ret = ERR_PTR(err);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200326 goto out_free;
Miklos Szeredia105d682014-11-20 16:39:59 +0100327 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200328
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200329 /*
330 * When removing an empty opaque directory, then it makes no sense to
331 * replace it with an exact replica of itself.
332 *
333 * If no upperdentry then skip clearing whiteouts.
334 *
335 * Can race with copy-up, since we don't hold the upperdir mutex.
336 * Doesn't matter, since copy-up can't create a non-empty directory
337 * from an empty one.
338 */
339 if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
340 ret = ovl_clear_empty(dentry, &list);
341
342out_free:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200343 ovl_cache_free(&list);
344
345 return ret;
346}
347
Miklos Szeredi38b25692016-09-01 11:11:59 +0200348static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
349 const struct posix_acl *acl)
350{
351 void *buffer;
352 size_t size;
353 int err;
354
355 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
356 return 0;
357
358 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
359 buffer = kmalloc(size, GFP_KERNEL);
360 if (!buffer)
361 return -ENOMEM;
362
363 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
364 err = size;
365 if (err < 0)
366 goto out_free;
367
368 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
369out_free:
370 kfree(buffer);
371 return err;
372}
373
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200374static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
375 struct kstat *stat, const char *link,
376 struct dentry *hardlink)
377{
378 struct dentry *workdir = ovl_workdir(dentry);
379 struct inode *wdir = workdir->d_inode;
380 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
381 struct inode *udir = upperdir->d_inode;
382 struct dentry *upper;
383 struct dentry *newdentry;
384 int err;
Miklos Szeredi38b25692016-09-01 11:11:59 +0200385 struct posix_acl *acl, *default_acl;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200386
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200387 if (WARN_ON(!workdir))
388 return -EROFS;
389
Miklos Szeredi38b25692016-09-01 11:11:59 +0200390 if (!hardlink) {
391 err = posix_acl_create(dentry->d_parent->d_inode,
392 &stat->mode, &default_acl, &acl);
393 if (err)
394 return err;
395 }
396
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200397 err = ovl_lock_rename_workdir(workdir, upperdir);
398 if (err)
399 goto out;
400
401 newdentry = ovl_lookup_temp(workdir, dentry);
402 err = PTR_ERR(newdentry);
403 if (IS_ERR(newdentry))
404 goto out_unlock;
405
406 upper = lookup_one_len(dentry->d_name.name, upperdir,
407 dentry->d_name.len);
408 err = PTR_ERR(upper);
409 if (IS_ERR(upper))
410 goto out_dput;
411
412 err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
413 if (err)
414 goto out_dput2;
415
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200416 /*
417 * mode could have been mutilated due to umask (e.g. sgid directory)
418 */
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200419 if (!hardlink &&
420 !S_ISLNK(stat->mode) && newdentry->d_inode->i_mode != stat->mode) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200421 struct iattr attr = {
422 .ia_valid = ATTR_MODE,
423 .ia_mode = stat->mode,
424 };
425 inode_lock(newdentry->d_inode);
426 err = notify_change(newdentry, &attr, NULL);
427 inode_unlock(newdentry->d_inode);
428 if (err)
429 goto out_cleanup;
430 }
Miklos Szeredi38b25692016-09-01 11:11:59 +0200431 if (!hardlink) {
432 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
433 acl);
434 if (err)
435 goto out_cleanup;
436
437 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
438 default_acl);
439 if (err)
440 goto out_cleanup;
441 }
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200442
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200443 if (!hardlink && S_ISDIR(stat->mode)) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100444 err = ovl_set_opaque(dentry, newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200445 if (err)
446 goto out_cleanup;
447
448 err = ovl_do_rename(wdir, newdentry, udir, upper,
449 RENAME_EXCHANGE);
450 if (err)
451 goto out_cleanup;
452
453 ovl_cleanup(wdir, upper);
454 } else {
455 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
456 if (err)
457 goto out_cleanup;
458 }
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200459 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200460 newdentry = NULL;
461out_dput2:
462 dput(upper);
463out_dput:
464 dput(newdentry);
465out_unlock:
466 unlock_rename(workdir, upperdir);
467out:
Miklos Szeredi38b25692016-09-01 11:11:59 +0200468 if (!hardlink) {
469 posix_acl_release(acl);
470 posix_acl_release(default_acl);
471 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200472 return err;
473
474out_cleanup:
475 ovl_cleanup(wdir, newdentry);
476 goto out_dput2;
477}
478
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200479static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
480 struct kstat *stat, const char *link,
481 struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200482{
483 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400484 const struct cred *old_cred;
485 struct cred *override_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200486
487 err = ovl_copy_up(dentry->d_parent);
488 if (err)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200489 return err;
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200490
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400491 old_cred = ovl_override_creds(dentry->d_sb);
492 err = -ENOMEM;
493 override_cred = prepare_creds();
494 if (override_cred) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200495 override_cred->fsuid = inode->i_uid;
496 override_cred->fsgid = inode->i_gid;
Vivek Goyal26026252016-07-13 10:44:52 -0400497 if (!hardlink) {
498 err = security_dentry_create_files_as(dentry,
499 stat->mode, &dentry->d_name, old_cred,
500 override_cred);
501 if (err) {
502 put_cred(override_cred);
503 goto out_revert_creds;
504 }
505 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400506 put_cred(override_creds(override_cred));
507 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200508
Miklos Szeredic412ce42016-12-16 11:02:55 +0100509 if (!ovl_dentry_is_whiteout(dentry))
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200510 err = ovl_create_upper(dentry, inode, stat, link,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400511 hardlink);
512 else
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200513 err = ovl_create_over_whiteout(dentry, inode, stat,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400514 link, hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200515 }
Vivek Goyal26026252016-07-13 10:44:52 -0400516out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400517 revert_creds(old_cred);
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200518 if (!err) {
519 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
520
521 WARN_ON(inode->i_mode != realinode->i_mode);
522 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
523 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200524 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200525 return err;
526}
527
528static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
529 const char *link)
530{
531 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200532 struct inode *inode;
533 struct kstat stat = {
534 .rdev = rdev,
535 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200536
537 err = ovl_want_write(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200538 if (err)
539 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200540
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200541 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100542 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200543 if (!inode)
544 goto out_drop_write;
545
546 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
547 stat.mode = inode->i_mode;
548
549 err = ovl_create_or_link(dentry, inode, &stat, link, NULL);
550 if (err)
551 iput(inode);
552
553out_drop_write:
554 ovl_drop_write(dentry);
555out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200556 return err;
557}
558
559static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
560 bool excl)
561{
562 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
563}
564
565static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
566{
567 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
568}
569
570static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
571 dev_t rdev)
572{
573 /* Don't allow creation of "whiteout" on overlay */
574 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
575 return -EPERM;
576
577 return ovl_create_object(dentry, mode, rdev, NULL);
578}
579
580static int ovl_symlink(struct inode *dir, struct dentry *dentry,
581 const char *link)
582{
583 return ovl_create_object(dentry, S_IFLNK, 0, link);
584}
585
586static int ovl_link(struct dentry *old, struct inode *newdir,
587 struct dentry *new)
588{
589 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200590 struct inode *inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200591
592 err = ovl_want_write(old);
593 if (err)
594 goto out;
595
596 err = ovl_copy_up(old);
597 if (err)
598 goto out_drop_write;
599
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200600 inode = d_inode(old);
601 ihold(inode);
602
603 err = ovl_create_or_link(new, inode, NULL, NULL, ovl_dentry_upper(old));
604 if (err)
605 iput(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200606
607out_drop_write:
608 ovl_drop_write(old);
609out:
610 return err;
611}
612
Miklos Szeredia105d682014-11-20 16:39:59 +0100613static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200614{
615 struct dentry *workdir = ovl_workdir(dentry);
616 struct inode *wdir = workdir->d_inode;
617 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
618 struct inode *udir = upperdir->d_inode;
619 struct dentry *whiteout;
620 struct dentry *upper;
621 struct dentry *opaquedir = NULL;
622 int err;
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700623 int flags = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200624
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200625 if (WARN_ON(!workdir))
626 return -EROFS;
627
Miklos Szeredid377c5e2015-05-14 10:04:44 +0200628 if (is_dir) {
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200629 opaquedir = ovl_check_empty_and_clear(dentry);
630 err = PTR_ERR(opaquedir);
631 if (IS_ERR(opaquedir))
632 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200633 }
634
635 err = ovl_lock_rename_workdir(workdir, upperdir);
636 if (err)
637 goto out_dput;
638
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700639 upper = lookup_one_len(dentry->d_name.name, upperdir,
640 dentry->d_name.len);
641 err = PTR_ERR(upper);
642 if (IS_ERR(upper))
643 goto out_unlock;
644
645 err = -ESTALE;
646 if ((opaquedir && upper != opaquedir) ||
647 (!opaquedir && ovl_dentry_upper(dentry) &&
648 upper != ovl_dentry_upper(dentry))) {
649 goto out_dput_upper;
650 }
651
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200652 whiteout = ovl_whiteout(workdir, dentry);
653 err = PTR_ERR(whiteout);
654 if (IS_ERR(whiteout))
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700655 goto out_dput_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200656
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700657 if (d_is_dir(upper))
658 flags = RENAME_EXCHANGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200659
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700660 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
661 if (err)
662 goto kill_whiteout;
663 if (flags)
664 ovl_cleanup(wdir, upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200665
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200666 ovl_dentry_version_inc(dentry->d_parent);
667out_d_drop:
668 d_drop(dentry);
669 dput(whiteout);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700670out_dput_upper:
671 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200672out_unlock:
673 unlock_rename(workdir, upperdir);
674out_dput:
675 dput(opaquedir);
676out:
677 return err;
678
679kill_whiteout:
680 ovl_cleanup(wdir, whiteout);
681 goto out_d_drop;
682}
683
684static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
685{
686 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
687 struct inode *dir = upperdir->d_inode;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100688 struct dentry *upper;
Amir Goldsteind1595112016-10-26 12:34:06 +0300689 struct dentry *opaquedir = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200690 int err;
691
Amir Goldsteind1595112016-10-26 12:34:06 +0300692 /* Redirect dir can be !ovl_lower_positive && OVL_TYPE_MERGE */
693 if (is_dir && ovl_dentry_get_redirect(dentry)) {
694 opaquedir = ovl_check_empty_and_clear(dentry);
695 err = PTR_ERR(opaquedir);
696 if (IS_ERR(opaquedir))
697 goto out;
698 }
699
Al Viro59551022016-01-22 15:40:57 -0500700 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100701 upper = lookup_one_len(dentry->d_name.name, upperdir,
702 dentry->d_name.len);
703 err = PTR_ERR(upper);
704 if (IS_ERR(upper))
705 goto out_unlock;
706
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200707 err = -ESTALE;
Amir Goldsteind1595112016-10-26 12:34:06 +0300708 if ((opaquedir && upper != opaquedir) ||
709 (!opaquedir && upper != ovl_dentry_upper(dentry)))
710 goto out_dput_upper;
711
712 if (is_dir)
713 err = vfs_rmdir(dir, upper);
714 else
715 err = vfs_unlink(dir, upper, NULL);
716 ovl_dentry_version_inc(dentry->d_parent);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200717
718 /*
719 * Keeping this dentry hashed would mean having to release
720 * upperpath/lowerpath, which could only be done if we are the
721 * sole user of this dentry. Too tricky... Just unhash for
722 * now.
723 */
Rui Wangce9113b2016-01-08 23:09:59 +0800724 if (!err)
725 d_drop(dentry);
Amir Goldsteind1595112016-10-26 12:34:06 +0300726out_dput_upper:
727 dput(upper);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100728out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500729 inode_unlock(dir);
Amir Goldsteind1595112016-10-26 12:34:06 +0300730 dput(opaquedir);
731out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200732 return err;
733}
734
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200735static int ovl_do_remove(struct dentry *dentry, bool is_dir)
736{
737 enum ovl_path_type type;
738 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400739 const struct cred *old_cred;
740
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200741 err = ovl_want_write(dentry);
742 if (err)
743 goto out;
744
745 err = ovl_copy_up(dentry->d_parent);
746 if (err)
747 goto out_drop_write;
748
749 type = ovl_path_type(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400750
751 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100752 if (!ovl_lower_positive(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200753 err = ovl_remove_upper(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400754 else
Miklos Szeredia105d682014-11-20 16:39:59 +0100755 err = ovl_remove_and_whiteout(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400756 revert_creds(old_cred);
Miklos Szeredidbc816d2016-07-29 12:05:24 +0200757 if (!err) {
758 if (is_dir)
759 clear_nlink(dentry->d_inode);
760 else
761 drop_nlink(dentry->d_inode);
762 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200763out_drop_write:
764 ovl_drop_write(dentry);
765out:
766 return err;
767}
768
769static int ovl_unlink(struct inode *dir, struct dentry *dentry)
770{
771 return ovl_do_remove(dentry, false);
772}
773
774static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
775{
776 return ovl_do_remove(dentry, true);
777}
778
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100779static bool ovl_type_merge_or_lower(struct dentry *dentry)
780{
781 enum ovl_path_type type = ovl_path_type(dentry);
782
783 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
784}
785
Miklos Szeredia6c60652016-12-16 11:02:56 +0100786static bool ovl_can_move(struct dentry *dentry)
787{
788 return ovl_redirect_dir(dentry->d_sb) ||
789 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
790}
791
Miklos Szeredia6c60652016-12-16 11:02:56 +0100792static char *ovl_get_redirect(struct dentry *dentry, bool samedir)
793{
794 char *buf, *ret;
795 struct dentry *d, *tmp;
Miklos Szeredi3ea22a72016-12-16 11:02:57 +0100796 int buflen = ovl_redirect_max + 1;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100797
798 if (samedir) {
799 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
800 GFP_KERNEL);
801 goto out;
802 }
803
804 buf = ret = kmalloc(buflen, GFP_TEMPORARY);
805 if (!buf)
806 goto out;
807
808 buflen--;
809 buf[buflen] = '\0';
810 for (d = dget(dentry); !IS_ROOT(d);) {
811 const char *name;
812 int thislen;
813
814 spin_lock(&d->d_lock);
815 name = ovl_dentry_get_redirect(d);
816 if (name) {
817 thislen = strlen(name);
818 } else {
819 name = d->d_name.name;
820 thislen = d->d_name.len;
821 }
822
823 /* If path is too long, fall back to userspace move */
824 if (thislen + (name[0] != '/') > buflen) {
825 ret = ERR_PTR(-EXDEV);
826 spin_unlock(&d->d_lock);
827 goto out_put;
828 }
829
830 buflen -= thislen;
831 memcpy(&buf[buflen], name, thislen);
832 tmp = dget_dlock(d->d_parent);
833 spin_unlock(&d->d_lock);
834
835 dput(d);
836 d = tmp;
837
838 /* Absolute redirect: finished */
839 if (buf[buflen] == '/')
840 break;
841 buflen--;
842 buf[buflen] = '/';
843 }
844 ret = kstrdup(&buf[buflen], GFP_KERNEL);
845out_put:
846 dput(d);
847 kfree(buf);
848out:
849 return ret ? ret : ERR_PTR(-ENOMEM);
850}
851
852static int ovl_set_redirect(struct dentry *dentry, bool samedir)
853{
854 int err;
855 const char *redirect = ovl_dentry_get_redirect(dentry);
856
857 if (redirect && (samedir || redirect[0] == '/'))
858 return 0;
859
860 redirect = ovl_get_redirect(dentry, samedir);
861 if (IS_ERR(redirect))
862 return PTR_ERR(redirect);
863
864 err = ovl_do_setxattr(ovl_dentry_upper(dentry), OVL_XATTR_REDIRECT,
865 redirect, strlen(redirect), 0);
866 if (!err) {
867 spin_lock(&dentry->d_lock);
868 ovl_dentry_set_redirect(dentry, redirect);
869 spin_unlock(&dentry->d_lock);
870 } else {
871 kfree(redirect);
872 if (err == -EOPNOTSUPP)
873 ovl_clear_redirect_dir(dentry->d_sb);
874 else
875 pr_warn_ratelimited("overlay: failed to set redirect (%i)\n", err);
876 /* Fall back to userspace copy-up */
877 err = -EXDEV;
878 }
879 return err;
880}
881
Miklos Szeredi6c02cb52016-12-16 11:02:55 +0100882static int ovl_rename(struct inode *olddir, struct dentry *old,
883 struct inode *newdir, struct dentry *new,
884 unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200885{
886 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200887 struct dentry *old_upperdir;
888 struct dentry *new_upperdir;
889 struct dentry *olddentry;
890 struct dentry *newdentry;
891 struct dentry *trap;
892 bool old_opaque;
893 bool new_opaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200894 bool cleanup_whiteout = false;
895 bool overwrite = !(flags & RENAME_EXCHANGE);
David Howellse36cb0b2015-01-29 12:02:35 +0000896 bool is_dir = d_is_dir(old);
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100897 bool new_is_dir = d_is_dir(new);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100898 bool samedir = olddir == newdir;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200899 struct dentry *opaquedir = NULL;
900 const struct cred *old_cred = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200901
902 err = -EINVAL;
903 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
904 goto out;
905
906 flags &= ~RENAME_NOREPLACE;
907
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200908 /* Don't copy up directory trees */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200909 err = -EXDEV;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100910 if (!ovl_can_move(old))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200911 goto out;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100912 if (!overwrite && !ovl_can_move(new))
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100913 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200914
915 err = ovl_want_write(old);
916 if (err)
917 goto out;
918
919 err = ovl_copy_up(old);
920 if (err)
921 goto out_drop_write;
922
923 err = ovl_copy_up(new->d_parent);
924 if (err)
925 goto out_drop_write;
926 if (!overwrite) {
927 err = ovl_copy_up(new);
928 if (err)
929 goto out_drop_write;
930 }
931
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400932 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200933
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100934 if (overwrite && new_is_dir && ovl_type_merge_or_lower(new)) {
Miklos Szeredia105d682014-11-20 16:39:59 +0100935 opaquedir = ovl_check_empty_and_clear(new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200936 err = PTR_ERR(opaquedir);
937 if (IS_ERR(opaquedir)) {
938 opaquedir = NULL;
939 goto out_revert_creds;
940 }
941 }
942
943 if (overwrite) {
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100944 if (ovl_lower_positive(old)) {
Miklos Szeredic412ce42016-12-16 11:02:55 +0100945 if (!ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200946 /* Whiteout source */
947 flags |= RENAME_WHITEOUT;
948 } else {
949 /* Switch whiteouts */
950 flags |= RENAME_EXCHANGE;
951 }
Miklos Szeredic412ce42016-12-16 11:02:55 +0100952 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200953 flags |= RENAME_EXCHANGE;
954 cleanup_whiteout = true;
955 }
956 }
957
958 old_upperdir = ovl_dentry_upper(old->d_parent);
959 new_upperdir = ovl_dentry_upper(new->d_parent);
960
961 trap = lock_rename(new_upperdir, old_upperdir);
962
Miklos Szeredi11f37102016-03-21 17:31:44 +0100963 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
964 old->d_name.len);
965 err = PTR_ERR(olddentry);
966 if (IS_ERR(olddentry))
967 goto out_unlock;
968
969 err = -ESTALE;
970 if (olddentry != ovl_dentry_upper(old))
971 goto out_dput_old;
972
973 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
974 new->d_name.len);
975 err = PTR_ERR(newdentry);
976 if (IS_ERR(newdentry))
977 goto out_dput_old;
978
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +0100979 old_opaque = ovl_dentry_is_opaque(old);
980 new_opaque = ovl_dentry_is_opaque(new);
981
Miklos Szeredi11f37102016-03-21 17:31:44 +0100982 err = -ESTALE;
983 if (ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200984 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100985 if (newdentry != opaquedir)
986 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200987 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100988 if (newdentry != ovl_dentry_upper(new))
989 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200990 }
991 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100992 if (!d_is_negative(newdentry) &&
993 (!new_opaque || !ovl_is_whiteout(newdentry)))
994 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200995 }
996
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200997 if (olddentry == trap)
998 goto out_dput;
999 if (newdentry == trap)
1000 goto out_dput;
1001
Miklos Szeredi804032f2016-12-16 11:02:55 +01001002 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
1003 goto out_dput;
1004
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001005 err = 0;
Miklos Szeredia6c60652016-12-16 11:02:56 +01001006 if (is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001007 if (ovl_type_merge_or_lower(old))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001008 err = ovl_set_redirect(old, samedir);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001009 else if (!old_opaque && ovl_lower_positive(new))
1010 err = ovl_set_opaque(old, olddentry);
1011 if (err)
1012 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001013 }
Miklos Szeredia6c60652016-12-16 11:02:56 +01001014 if (!overwrite && new_is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001015 if (ovl_type_merge_or_lower(new))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001016 err = ovl_set_redirect(new, samedir);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001017 else if (!new_opaque && ovl_lower_positive(old))
1018 err = ovl_set_opaque(new, newdentry);
1019 if (err)
1020 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001021 }
1022
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +01001023 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1024 new_upperdir->d_inode, newdentry, flags);
1025 if (err)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001026 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001027
1028 if (cleanup_whiteout)
1029 ovl_cleanup(old_upperdir->d_inode, newdentry);
1030
1031 ovl_dentry_version_inc(old->d_parent);
1032 ovl_dentry_version_inc(new->d_parent);
1033
1034out_dput:
1035 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +01001036out_dput_old:
1037 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001038out_unlock:
1039 unlock_rename(new_upperdir, old_upperdir);
1040out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -04001041 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001042out_drop_write:
1043 ovl_drop_write(old);
1044out:
1045 dput(opaquedir);
1046 return err;
1047}
1048
1049const struct inode_operations ovl_dir_inode_operations = {
1050 .lookup = ovl_lookup,
1051 .mkdir = ovl_mkdir,
1052 .symlink = ovl_symlink,
1053 .unlink = ovl_unlink,
1054 .rmdir = ovl_rmdir,
Miklos Szeredi6c02cb52016-12-16 11:02:55 +01001055 .rename = ovl_rename,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001056 .link = ovl_link,
1057 .setattr = ovl_setattr,
1058 .create = ovl_create,
1059 .mknod = ovl_mknod,
1060 .permission = ovl_permission,
1061 .getattr = ovl_dir_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001062 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -04001063 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001064 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001065};