blob: 1560fdc09a5fabf12ed678a3e87e78bb3e43cce2 [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 Szeredi38b25692016-09-01 11:11:59 +020015#include <linux/posix_acl.h>
16#include <linux/posix_acl_xattr.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020017#include "overlayfs.h"
18
19void ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
20{
21 int err;
22
23 dget(wdentry);
David Howellse36cb0b2015-01-29 12:02:35 +000024 if (d_is_dir(wdentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +020025 err = ovl_do_rmdir(wdir, wdentry);
26 else
27 err = ovl_do_unlink(wdir, wdentry);
28 dput(wdentry);
29
30 if (err) {
31 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
32 wdentry, err);
33 }
34}
35
36struct dentry *ovl_lookup_temp(struct dentry *workdir, struct dentry *dentry)
37{
38 struct dentry *temp;
39 char name[20];
40
41 snprintf(name, sizeof(name), "#%lx", (unsigned long) dentry);
42
43 temp = lookup_one_len(name, workdir, strlen(name));
44 if (!IS_ERR(temp) && temp->d_inode) {
45 pr_err("overlayfs: workdir/%s already exists\n", name);
46 dput(temp);
47 temp = ERR_PTR(-EIO);
48 }
49
50 return temp;
51}
52
53/* caller holds i_mutex on workdir */
54static struct dentry *ovl_whiteout(struct dentry *workdir,
55 struct dentry *dentry)
56{
57 int err;
58 struct dentry *whiteout;
59 struct inode *wdir = workdir->d_inode;
60
61 whiteout = ovl_lookup_temp(workdir, dentry);
62 if (IS_ERR(whiteout))
63 return whiteout;
64
65 err = ovl_do_whiteout(wdir, whiteout);
66 if (err) {
67 dput(whiteout);
68 whiteout = ERR_PTR(err);
69 }
70
71 return whiteout;
72}
73
74int ovl_create_real(struct inode *dir, struct dentry *newdentry,
75 struct kstat *stat, const char *link,
76 struct dentry *hardlink, bool debug)
77{
78 int err;
79
80 if (newdentry->d_inode)
81 return -ESTALE;
82
83 if (hardlink) {
84 err = ovl_do_link(hardlink, dir, newdentry, debug);
85 } else {
86 switch (stat->mode & S_IFMT) {
87 case S_IFREG:
88 err = ovl_do_create(dir, newdentry, stat->mode, debug);
89 break;
90
91 case S_IFDIR:
92 err = ovl_do_mkdir(dir, newdentry, stat->mode, debug);
93 break;
94
95 case S_IFCHR:
96 case S_IFBLK:
97 case S_IFIFO:
98 case S_IFSOCK:
99 err = ovl_do_mknod(dir, newdentry,
100 stat->mode, stat->rdev, debug);
101 break;
102
103 case S_IFLNK:
104 err = ovl_do_symlink(dir, newdentry, link, debug);
105 break;
106
107 default:
108 err = -EPERM;
109 }
110 }
111 if (!err && WARN_ON(!newdentry->d_inode)) {
112 /*
113 * Not quite sure if non-instantiated dentry is legal or not.
114 * VFS doesn't seem to care so check and warn here.
115 */
116 err = -ENOENT;
117 }
118 return err;
119}
120
121static int ovl_set_opaque(struct dentry *upperdentry)
122{
hujianyangcead89b2014-11-24 18:25:21 +0800123 return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200124}
125
126static void ovl_remove_opaque(struct dentry *upperdentry)
127{
128 int err;
129
hujianyangcead89b2014-11-24 18:25:21 +0800130 err = ovl_do_removexattr(upperdentry, OVL_XATTR_OPAQUE);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200131 if (err) {
132 pr_warn("overlayfs: failed to remove opaque from '%s' (%i)\n",
133 upperdentry->d_name.name, err);
134 }
135}
136
137static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
138 struct kstat *stat)
139{
140 int err;
141 enum ovl_path_type type;
142 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400143 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200144
145 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400146 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200147 err = vfs_getattr(&realpath, stat);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400148 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200149 if (err)
150 return err;
151
152 stat->dev = dentry->d_sb->s_dev;
153 stat->ino = dentry->d_inode->i_ino;
154
155 /*
156 * It's probably not worth it to count subdirs to get the
157 * correct link count. nlink=1 seems to pacify 'find' and
158 * other utilities.
159 */
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100160 if (OVL_TYPE_MERGE(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200161 stat->nlink = 1;
162
163 return 0;
164}
165
Vivek Goyal72e48482016-06-16 10:09:14 -0400166/* Common operations required to be done after creation of file on upper */
167static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200168 struct dentry *newdentry, bool hardlink)
Vivek Goyal72e48482016-06-16 10:09:14 -0400169{
170 ovl_dentry_version_inc(dentry->d_parent);
171 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200172 if (!hardlink) {
173 ovl_inode_update(inode, d_inode(newdentry));
174 ovl_copyattr(newdentry->d_inode, inode);
175 } else {
176 WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
177 inc_nlink(inode);
178 }
Vivek Goyal72e48482016-06-16 10:09:14 -0400179 d_instantiate(dentry, inode);
180}
181
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200182static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
183 struct kstat *stat, const char *link,
184 struct dentry *hardlink)
185{
186 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
187 struct inode *udir = upperdir->d_inode;
188 struct dentry *newdentry;
189 int err;
190
Miklos Szeredi38b25692016-09-01 11:11:59 +0200191 if (!hardlink && !IS_POSIXACL(udir))
192 stat->mode &= ~current_umask();
193
Al Viro59551022016-01-22 15:40:57 -0500194 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200195 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
196 dentry->d_name.len);
197 err = PTR_ERR(newdentry);
198 if (IS_ERR(newdentry))
199 goto out_unlock;
200 err = ovl_create_real(udir, newdentry, stat, link, hardlink, false);
201 if (err)
202 goto out_dput;
203
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200204 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200205 newdentry = NULL;
206out_dput:
207 dput(newdentry);
208out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500209 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200210 return err;
211}
212
213static int ovl_lock_rename_workdir(struct dentry *workdir,
214 struct dentry *upperdir)
215{
216 /* Workdir should not be the same as upperdir */
217 if (workdir == upperdir)
218 goto err;
219
220 /* Workdir should not be subdir of upperdir and vice versa */
221 if (lock_rename(workdir, upperdir) != NULL)
222 goto err_unlock;
223
224 return 0;
225
226err_unlock:
227 unlock_rename(workdir, upperdir);
228err:
229 pr_err("overlayfs: failed to lock workdir+upperdir\n");
230 return -EIO;
231}
232
233static struct dentry *ovl_clear_empty(struct dentry *dentry,
234 struct list_head *list)
235{
236 struct dentry *workdir = ovl_workdir(dentry);
237 struct inode *wdir = workdir->d_inode;
238 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
239 struct inode *udir = upperdir->d_inode;
240 struct path upperpath;
241 struct dentry *upper;
242 struct dentry *opaquedir;
243 struct kstat stat;
244 int err;
245
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200246 if (WARN_ON(!workdir))
247 return ERR_PTR(-EROFS);
248
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200249 err = ovl_lock_rename_workdir(workdir, upperdir);
250 if (err)
251 goto out;
252
253 ovl_path_upper(dentry, &upperpath);
254 err = vfs_getattr(&upperpath, &stat);
255 if (err)
256 goto out_unlock;
257
258 err = -ESTALE;
259 if (!S_ISDIR(stat.mode))
260 goto out_unlock;
261 upper = upperpath.dentry;
262 if (upper->d_parent->d_inode != udir)
263 goto out_unlock;
264
265 opaquedir = ovl_lookup_temp(workdir, dentry);
266 err = PTR_ERR(opaquedir);
267 if (IS_ERR(opaquedir))
268 goto out_unlock;
269
270 err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true);
271 if (err)
272 goto out_dput;
273
274 err = ovl_copy_xattr(upper, opaquedir);
275 if (err)
276 goto out_cleanup;
277
278 err = ovl_set_opaque(opaquedir);
279 if (err)
280 goto out_cleanup;
281
Al Viro59551022016-01-22 15:40:57 -0500282 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200283 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500284 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200285 if (err)
286 goto out_cleanup;
287
288 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
289 if (err)
290 goto out_cleanup;
291
292 ovl_cleanup_whiteouts(upper, list);
293 ovl_cleanup(wdir, upper);
294 unlock_rename(workdir, upperdir);
295
296 /* dentry's upper doesn't match now, get rid of it */
297 d_drop(dentry);
298
299 return opaquedir;
300
301out_cleanup:
302 ovl_cleanup(wdir, opaquedir);
303out_dput:
304 dput(opaquedir);
305out_unlock:
306 unlock_rename(workdir, upperdir);
307out:
308 return ERR_PTR(err);
309}
310
Miklos Szeredia105d682014-11-20 16:39:59 +0100311static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200312{
313 int err;
314 struct dentry *ret = NULL;
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200315 enum ovl_path_type type = ovl_path_type(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200316 LIST_HEAD(list);
317
318 err = ovl_check_empty_dir(dentry, &list);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200319 if (err) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200320 ret = ERR_PTR(err);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200321 goto out_free;
Miklos Szeredia105d682014-11-20 16:39:59 +0100322 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200323
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200324 /*
325 * When removing an empty opaque directory, then it makes no sense to
326 * replace it with an exact replica of itself.
327 *
328 * If no upperdentry then skip clearing whiteouts.
329 *
330 * Can race with copy-up, since we don't hold the upperdir mutex.
331 * Doesn't matter, since copy-up can't create a non-empty directory
332 * from an empty one.
333 */
334 if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
335 ret = ovl_clear_empty(dentry, &list);
336
337out_free:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200338 ovl_cache_free(&list);
339
340 return ret;
341}
342
Miklos Szeredi38b25692016-09-01 11:11:59 +0200343static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
344 const struct posix_acl *acl)
345{
346 void *buffer;
347 size_t size;
348 int err;
349
350 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
351 return 0;
352
353 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
354 buffer = kmalloc(size, GFP_KERNEL);
355 if (!buffer)
356 return -ENOMEM;
357
358 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
359 err = size;
360 if (err < 0)
361 goto out_free;
362
363 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
364out_free:
365 kfree(buffer);
366 return err;
367}
368
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200369static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
370 struct kstat *stat, const char *link,
371 struct dentry *hardlink)
372{
373 struct dentry *workdir = ovl_workdir(dentry);
374 struct inode *wdir = workdir->d_inode;
375 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
376 struct inode *udir = upperdir->d_inode;
377 struct dentry *upper;
378 struct dentry *newdentry;
379 int err;
Miklos Szeredi38b25692016-09-01 11:11:59 +0200380 struct posix_acl *acl, *default_acl;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200381
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200382 if (WARN_ON(!workdir))
383 return -EROFS;
384
Miklos Szeredi38b25692016-09-01 11:11:59 +0200385 if (!hardlink) {
386 err = posix_acl_create(dentry->d_parent->d_inode,
387 &stat->mode, &default_acl, &acl);
388 if (err)
389 return err;
390 }
391
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200392 err = ovl_lock_rename_workdir(workdir, upperdir);
393 if (err)
394 goto out;
395
396 newdentry = ovl_lookup_temp(workdir, dentry);
397 err = PTR_ERR(newdentry);
398 if (IS_ERR(newdentry))
399 goto out_unlock;
400
401 upper = lookup_one_len(dentry->d_name.name, upperdir,
402 dentry->d_name.len);
403 err = PTR_ERR(upper);
404 if (IS_ERR(upper))
405 goto out_dput;
406
407 err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
408 if (err)
409 goto out_dput2;
410
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200411 /*
412 * mode could have been mutilated due to umask (e.g. sgid directory)
413 */
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200414 if (!hardlink &&
415 !S_ISLNK(stat->mode) && newdentry->d_inode->i_mode != stat->mode) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200416 struct iattr attr = {
417 .ia_valid = ATTR_MODE,
418 .ia_mode = stat->mode,
419 };
420 inode_lock(newdentry->d_inode);
421 err = notify_change(newdentry, &attr, NULL);
422 inode_unlock(newdentry->d_inode);
423 if (err)
424 goto out_cleanup;
425 }
Miklos Szeredi38b25692016-09-01 11:11:59 +0200426 if (!hardlink) {
427 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
428 acl);
429 if (err)
430 goto out_cleanup;
431
432 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
433 default_acl);
434 if (err)
435 goto out_cleanup;
436 }
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200437
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200438 if (!hardlink && S_ISDIR(stat->mode)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200439 err = ovl_set_opaque(newdentry);
440 if (err)
441 goto out_cleanup;
442
443 err = ovl_do_rename(wdir, newdentry, udir, upper,
444 RENAME_EXCHANGE);
445 if (err)
446 goto out_cleanup;
447
448 ovl_cleanup(wdir, upper);
449 } else {
450 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
451 if (err)
452 goto out_cleanup;
453 }
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200454 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200455 newdentry = NULL;
456out_dput2:
457 dput(upper);
458out_dput:
459 dput(newdentry);
460out_unlock:
461 unlock_rename(workdir, upperdir);
462out:
Miklos Szeredi38b25692016-09-01 11:11:59 +0200463 if (!hardlink) {
464 posix_acl_release(acl);
465 posix_acl_release(default_acl);
466 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200467 return err;
468
469out_cleanup:
470 ovl_cleanup(wdir, newdentry);
471 goto out_dput2;
472}
473
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200474static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
475 struct kstat *stat, const char *link,
476 struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200477{
478 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400479 const struct cred *old_cred;
480 struct cred *override_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200481
482 err = ovl_copy_up(dentry->d_parent);
483 if (err)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200484 return err;
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200485
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400486 old_cred = ovl_override_creds(dentry->d_sb);
487 err = -ENOMEM;
488 override_cred = prepare_creds();
489 if (override_cred) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200490 override_cred->fsuid = inode->i_uid;
491 override_cred->fsgid = inode->i_gid;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400492 put_cred(override_creds(override_cred));
493 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200494
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400495 if (!ovl_dentry_is_opaque(dentry))
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200496 err = ovl_create_upper(dentry, inode, stat, link,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400497 hardlink);
498 else
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200499 err = ovl_create_over_whiteout(dentry, inode, stat,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400500 link, hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200501 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400502 revert_creds(old_cred);
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200503 if (!err) {
504 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
505
506 WARN_ON(inode->i_mode != realinode->i_mode);
507 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
508 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200509 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200510 return err;
511}
512
513static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
514 const char *link)
515{
516 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200517 struct inode *inode;
518 struct kstat stat = {
519 .rdev = rdev,
520 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200521
522 err = ovl_want_write(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200523 if (err)
524 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200525
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200526 err = -ENOMEM;
527 inode = ovl_new_inode(dentry->d_sb, mode);
528 if (!inode)
529 goto out_drop_write;
530
531 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
532 stat.mode = inode->i_mode;
533
534 err = ovl_create_or_link(dentry, inode, &stat, link, NULL);
535 if (err)
536 iput(inode);
537
538out_drop_write:
539 ovl_drop_write(dentry);
540out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200541 return err;
542}
543
544static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
545 bool excl)
546{
547 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
548}
549
550static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
551{
552 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
553}
554
555static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
556 dev_t rdev)
557{
558 /* Don't allow creation of "whiteout" on overlay */
559 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
560 return -EPERM;
561
562 return ovl_create_object(dentry, mode, rdev, NULL);
563}
564
565static int ovl_symlink(struct inode *dir, struct dentry *dentry,
566 const char *link)
567{
568 return ovl_create_object(dentry, S_IFLNK, 0, link);
569}
570
571static int ovl_link(struct dentry *old, struct inode *newdir,
572 struct dentry *new)
573{
574 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200575 struct inode *inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200576
577 err = ovl_want_write(old);
578 if (err)
579 goto out;
580
581 err = ovl_copy_up(old);
582 if (err)
583 goto out_drop_write;
584
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200585 inode = d_inode(old);
586 ihold(inode);
587
588 err = ovl_create_or_link(new, inode, NULL, NULL, ovl_dentry_upper(old));
589 if (err)
590 iput(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200591
592out_drop_write:
593 ovl_drop_write(old);
594out:
595 return err;
596}
597
Miklos Szeredia105d682014-11-20 16:39:59 +0100598static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200599{
600 struct dentry *workdir = ovl_workdir(dentry);
601 struct inode *wdir = workdir->d_inode;
602 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
603 struct inode *udir = upperdir->d_inode;
604 struct dentry *whiteout;
605 struct dentry *upper;
606 struct dentry *opaquedir = NULL;
607 int err;
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700608 int flags = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200609
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200610 if (WARN_ON(!workdir))
611 return -EROFS;
612
Miklos Szeredid377c5e2015-05-14 10:04:44 +0200613 if (is_dir) {
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200614 opaquedir = ovl_check_empty_and_clear(dentry);
615 err = PTR_ERR(opaquedir);
616 if (IS_ERR(opaquedir))
617 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200618 }
619
620 err = ovl_lock_rename_workdir(workdir, upperdir);
621 if (err)
622 goto out_dput;
623
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700624 upper = lookup_one_len(dentry->d_name.name, upperdir,
625 dentry->d_name.len);
626 err = PTR_ERR(upper);
627 if (IS_ERR(upper))
628 goto out_unlock;
629
630 err = -ESTALE;
631 if ((opaquedir && upper != opaquedir) ||
632 (!opaquedir && ovl_dentry_upper(dentry) &&
633 upper != ovl_dentry_upper(dentry))) {
634 goto out_dput_upper;
635 }
636
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200637 whiteout = ovl_whiteout(workdir, dentry);
638 err = PTR_ERR(whiteout);
639 if (IS_ERR(whiteout))
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700640 goto out_dput_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200641
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700642 if (d_is_dir(upper))
643 flags = RENAME_EXCHANGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200644
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700645 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
646 if (err)
647 goto kill_whiteout;
648 if (flags)
649 ovl_cleanup(wdir, upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200650
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200651 ovl_dentry_version_inc(dentry->d_parent);
652out_d_drop:
653 d_drop(dentry);
654 dput(whiteout);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700655out_dput_upper:
656 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200657out_unlock:
658 unlock_rename(workdir, upperdir);
659out_dput:
660 dput(opaquedir);
661out:
662 return err;
663
664kill_whiteout:
665 ovl_cleanup(wdir, whiteout);
666 goto out_d_drop;
667}
668
669static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
670{
671 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
672 struct inode *dir = upperdir->d_inode;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100673 struct dentry *upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200674 int err;
675
Al Viro59551022016-01-22 15:40:57 -0500676 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100677 upper = lookup_one_len(dentry->d_name.name, upperdir,
678 dentry->d_name.len);
679 err = PTR_ERR(upper);
680 if (IS_ERR(upper))
681 goto out_unlock;
682
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200683 err = -ESTALE;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100684 if (upper == ovl_dentry_upper(dentry)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200685 if (is_dir)
686 err = vfs_rmdir(dir, upper);
687 else
688 err = vfs_unlink(dir, upper, NULL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200689 ovl_dentry_version_inc(dentry->d_parent);
690 }
Miklos Szeredi11f37102016-03-21 17:31:44 +0100691 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200692
693 /*
694 * Keeping this dentry hashed would mean having to release
695 * upperpath/lowerpath, which could only be done if we are the
696 * sole user of this dentry. Too tricky... Just unhash for
697 * now.
698 */
Rui Wangce9113b2016-01-08 23:09:59 +0800699 if (!err)
700 d_drop(dentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100701out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500702 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200703
704 return err;
705}
706
707static inline int ovl_check_sticky(struct dentry *dentry)
708{
709 struct inode *dir = ovl_dentry_real(dentry->d_parent)->d_inode;
710 struct inode *inode = ovl_dentry_real(dentry)->d_inode;
711
712 if (check_sticky(dir, inode))
713 return -EPERM;
714
715 return 0;
716}
717
718static int ovl_do_remove(struct dentry *dentry, bool is_dir)
719{
720 enum ovl_path_type type;
721 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400722 const struct cred *old_cred;
723
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200724
725 err = ovl_check_sticky(dentry);
726 if (err)
727 goto out;
728
729 err = ovl_want_write(dentry);
730 if (err)
731 goto out;
732
733 err = ovl_copy_up(dentry->d_parent);
734 if (err)
735 goto out_drop_write;
736
737 type = ovl_path_type(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400738
739 old_cred = ovl_override_creds(dentry->d_sb);
740 if (OVL_TYPE_PURE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200741 err = ovl_remove_upper(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400742 else
Miklos Szeredia105d682014-11-20 16:39:59 +0100743 err = ovl_remove_and_whiteout(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400744 revert_creds(old_cred);
Miklos Szeredidbc816d2016-07-29 12:05:24 +0200745 if (!err) {
746 if (is_dir)
747 clear_nlink(dentry->d_inode);
748 else
749 drop_nlink(dentry->d_inode);
750 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200751out_drop_write:
752 ovl_drop_write(dentry);
753out:
754 return err;
755}
756
757static int ovl_unlink(struct inode *dir, struct dentry *dentry)
758{
759 return ovl_do_remove(dentry, false);
760}
761
762static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
763{
764 return ovl_do_remove(dentry, true);
765}
766
767static int ovl_rename2(struct inode *olddir, struct dentry *old,
768 struct inode *newdir, struct dentry *new,
769 unsigned int flags)
770{
771 int err;
772 enum ovl_path_type old_type;
773 enum ovl_path_type new_type;
774 struct dentry *old_upperdir;
775 struct dentry *new_upperdir;
776 struct dentry *olddentry;
777 struct dentry *newdentry;
778 struct dentry *trap;
779 bool old_opaque;
780 bool new_opaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200781 bool cleanup_whiteout = false;
782 bool overwrite = !(flags & RENAME_EXCHANGE);
David Howellse36cb0b2015-01-29 12:02:35 +0000783 bool is_dir = d_is_dir(old);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200784 bool new_is_dir = false;
785 struct dentry *opaquedir = NULL;
786 const struct cred *old_cred = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200787
788 err = -EINVAL;
789 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
790 goto out;
791
792 flags &= ~RENAME_NOREPLACE;
793
794 err = ovl_check_sticky(old);
795 if (err)
796 goto out;
797
798 /* Don't copy up directory trees */
799 old_type = ovl_path_type(old);
800 err = -EXDEV;
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100801 if (OVL_TYPE_MERGE_OR_LOWER(old_type) && is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200802 goto out;
803
804 if (new->d_inode) {
805 err = ovl_check_sticky(new);
806 if (err)
807 goto out;
808
David Howellse36cb0b2015-01-29 12:02:35 +0000809 if (d_is_dir(new))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200810 new_is_dir = true;
811
812 new_type = ovl_path_type(new);
813 err = -EXDEV;
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100814 if (!overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200815 goto out;
816
817 err = 0;
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100818 if (!OVL_TYPE_UPPER(new_type) && !OVL_TYPE_UPPER(old_type)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200819 if (ovl_dentry_lower(old)->d_inode ==
820 ovl_dentry_lower(new)->d_inode)
821 goto out;
822 }
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100823 if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200824 if (ovl_dentry_upper(old)->d_inode ==
825 ovl_dentry_upper(new)->d_inode)
826 goto out;
827 }
828 } else {
829 if (ovl_dentry_is_opaque(new))
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100830 new_type = __OVL_PATH_UPPER;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200831 else
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100832 new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200833 }
834
835 err = ovl_want_write(old);
836 if (err)
837 goto out;
838
839 err = ovl_copy_up(old);
840 if (err)
841 goto out_drop_write;
842
843 err = ovl_copy_up(new->d_parent);
844 if (err)
845 goto out_drop_write;
846 if (!overwrite) {
847 err = ovl_copy_up(new);
848 if (err)
849 goto out_drop_write;
850 }
851
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100852 old_opaque = !OVL_TYPE_PURE_UPPER(old_type);
853 new_opaque = !OVL_TYPE_PURE_UPPER(new_type);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200854
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400855 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200856
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100857 if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) {
Miklos Szeredia105d682014-11-20 16:39:59 +0100858 opaquedir = ovl_check_empty_and_clear(new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200859 err = PTR_ERR(opaquedir);
860 if (IS_ERR(opaquedir)) {
861 opaquedir = NULL;
862 goto out_revert_creds;
863 }
864 }
865
866 if (overwrite) {
867 if (old_opaque) {
868 if (new->d_inode || !new_opaque) {
869 /* Whiteout source */
870 flags |= RENAME_WHITEOUT;
871 } else {
872 /* Switch whiteouts */
873 flags |= RENAME_EXCHANGE;
874 }
875 } else if (is_dir && !new->d_inode && new_opaque) {
876 flags |= RENAME_EXCHANGE;
877 cleanup_whiteout = true;
878 }
879 }
880
881 old_upperdir = ovl_dentry_upper(old->d_parent);
882 new_upperdir = ovl_dentry_upper(new->d_parent);
883
884 trap = lock_rename(new_upperdir, old_upperdir);
885
Miklos Szeredi11f37102016-03-21 17:31:44 +0100886
887 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
888 old->d_name.len);
889 err = PTR_ERR(olddentry);
890 if (IS_ERR(olddentry))
891 goto out_unlock;
892
893 err = -ESTALE;
894 if (olddentry != ovl_dentry_upper(old))
895 goto out_dput_old;
896
897 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
898 new->d_name.len);
899 err = PTR_ERR(newdentry);
900 if (IS_ERR(newdentry))
901 goto out_dput_old;
902
903 err = -ESTALE;
904 if (ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200905 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100906 if (newdentry != opaquedir)
907 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200908 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100909 if (newdentry != ovl_dentry_upper(new))
910 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200911 }
912 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100913 if (!d_is_negative(newdentry) &&
914 (!new_opaque || !ovl_is_whiteout(newdentry)))
915 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200916 }
917
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200918 if (olddentry == trap)
919 goto out_dput;
920 if (newdentry == trap)
921 goto out_dput;
922
923 if (is_dir && !old_opaque && new_opaque) {
924 err = ovl_set_opaque(olddentry);
925 if (err)
926 goto out_dput;
927 }
928 if (!overwrite && new_is_dir && old_opaque && !new_opaque) {
929 err = ovl_set_opaque(newdentry);
930 if (err)
931 goto out_dput;
932 }
933
934 if (old_opaque || new_opaque) {
935 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
936 new_upperdir->d_inode, newdentry,
937 flags);
938 } else {
939 /* No debug for the plain case */
940 BUG_ON(flags & ~RENAME_EXCHANGE);
941 err = vfs_rename(old_upperdir->d_inode, olddentry,
942 new_upperdir->d_inode, newdentry,
943 NULL, flags);
944 }
945
946 if (err) {
947 if (is_dir && !old_opaque && new_opaque)
948 ovl_remove_opaque(olddentry);
949 if (!overwrite && new_is_dir && old_opaque && !new_opaque)
950 ovl_remove_opaque(newdentry);
951 goto out_dput;
952 }
953
954 if (is_dir && old_opaque && !new_opaque)
955 ovl_remove_opaque(olddentry);
956 if (!overwrite && new_is_dir && !old_opaque && new_opaque)
957 ovl_remove_opaque(newdentry);
958
Konstantin Khlebnikov45d11732016-01-31 16:17:53 +0300959 /*
960 * Old dentry now lives in different location. Dentries in
961 * lowerstack are stale. We cannot drop them here because
962 * access to them is lockless. This could be only pure upper
963 * or opaque directory - numlower is zero. Or upper non-dir
964 * entry - its pureness is tracked by flag opaque.
965 */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200966 if (old_opaque != new_opaque) {
967 ovl_dentry_set_opaque(old, new_opaque);
968 if (!overwrite)
969 ovl_dentry_set_opaque(new, old_opaque);
970 }
971
972 if (cleanup_whiteout)
973 ovl_cleanup(old_upperdir->d_inode, newdentry);
974
975 ovl_dentry_version_inc(old->d_parent);
976 ovl_dentry_version_inc(new->d_parent);
977
978out_dput:
979 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100980out_dput_old:
981 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200982out_unlock:
983 unlock_rename(new_upperdir, old_upperdir);
984out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400985 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200986out_drop_write:
987 ovl_drop_write(old);
988out:
989 dput(opaquedir);
990 return err;
991}
992
993const struct inode_operations ovl_dir_inode_operations = {
994 .lookup = ovl_lookup,
995 .mkdir = ovl_mkdir,
996 .symlink = ovl_symlink,
997 .unlink = ovl_unlink,
998 .rmdir = ovl_rmdir,
999 .rename2 = ovl_rename2,
1000 .link = ovl_link,
1001 .setattr = ovl_setattr,
1002 .create = ovl_create,
1003 .mknod = ovl_mknod,
1004 .permission = ovl_permission,
1005 .getattr = ovl_dir_getattr,
Miklos Szeredid837a492016-07-29 12:05:24 +02001006 .setxattr = generic_setxattr,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001007 .getxattr = generic_getxattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001008 .listxattr = ovl_listxattr,
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +02001009 .removexattr = generic_removexattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -04001010 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001011 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001012};