blob: c1de84c1c5ec924df2c13d96b892552b39fd188d [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>
Richard Weinberger6a45b362016-09-16 11:45:24 +020017#include <linux/atomic.h>
Miklos Szeredia6c60652016-12-16 11:02:56 +010018#include <linux/ratelimit.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020019#include "overlayfs.h"
20
21void ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
22{
23 int err;
24
25 dget(wdentry);
David Howellse36cb0b2015-01-29 12:02:35 +000026 if (d_is_dir(wdentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +020027 err = ovl_do_rmdir(wdir, wdentry);
28 else
29 err = ovl_do_unlink(wdir, wdentry);
30 dput(wdentry);
31
32 if (err) {
33 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
34 wdentry, err);
35 }
36}
37
38struct dentry *ovl_lookup_temp(struct dentry *workdir, struct dentry *dentry)
39{
40 struct dentry *temp;
41 char name[20];
Richard Weinberger6a45b362016-09-16 11:45:24 +020042 static atomic_t temp_id = ATOMIC_INIT(0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020043
Richard Weinberger6a45b362016-09-16 11:45:24 +020044 /* counter is allowed to wrap, since temp dentries are ephemeral */
45 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
Miklos Szeredie9be9d52014-10-24 00:14:38 +020046
47 temp = lookup_one_len(name, workdir, strlen(name));
48 if (!IS_ERR(temp) && temp->d_inode) {
49 pr_err("overlayfs: workdir/%s already exists\n", name);
50 dput(temp);
51 temp = ERR_PTR(-EIO);
52 }
53
54 return temp;
55}
56
57/* caller holds i_mutex on workdir */
58static struct dentry *ovl_whiteout(struct dentry *workdir,
59 struct dentry *dentry)
60{
61 int err;
62 struct dentry *whiteout;
63 struct inode *wdir = workdir->d_inode;
64
65 whiteout = ovl_lookup_temp(workdir, dentry);
66 if (IS_ERR(whiteout))
67 return whiteout;
68
69 err = ovl_do_whiteout(wdir, whiteout);
70 if (err) {
71 dput(whiteout);
72 whiteout = ERR_PTR(err);
73 }
74
75 return whiteout;
76}
77
78int ovl_create_real(struct inode *dir, struct dentry *newdentry,
79 struct kstat *stat, const char *link,
80 struct dentry *hardlink, bool debug)
81{
82 int err;
83
84 if (newdentry->d_inode)
85 return -ESTALE;
86
87 if (hardlink) {
88 err = ovl_do_link(hardlink, dir, newdentry, debug);
89 } else {
90 switch (stat->mode & S_IFMT) {
91 case S_IFREG:
92 err = ovl_do_create(dir, newdentry, stat->mode, debug);
93 break;
94
95 case S_IFDIR:
96 err = ovl_do_mkdir(dir, newdentry, stat->mode, debug);
97 break;
98
99 case S_IFCHR:
100 case S_IFBLK:
101 case S_IFIFO:
102 case S_IFSOCK:
103 err = ovl_do_mknod(dir, newdentry,
104 stat->mode, stat->rdev, debug);
105 break;
106
107 case S_IFLNK:
108 err = ovl_do_symlink(dir, newdentry, link, debug);
109 break;
110
111 default:
112 err = -EPERM;
113 }
114 }
115 if (!err && WARN_ON(!newdentry->d_inode)) {
116 /*
117 * Not quite sure if non-instantiated dentry is legal or not.
118 * VFS doesn't seem to care so check and warn here.
119 */
120 err = -ENOENT;
121 }
122 return err;
123}
124
125static int ovl_set_opaque(struct dentry *upperdentry)
126{
hujianyangcead89b2014-11-24 18:25:21 +0800127 return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200128}
129
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200130static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
131 struct kstat *stat)
132{
133 int err;
134 enum ovl_path_type type;
135 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400136 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200137
138 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400139 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200140 err = vfs_getattr(&realpath, stat);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400141 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200142 if (err)
143 return err;
144
145 stat->dev = dentry->d_sb->s_dev;
146 stat->ino = dentry->d_inode->i_ino;
147
148 /*
149 * It's probably not worth it to count subdirs to get the
150 * correct link count. nlink=1 seems to pacify 'find' and
151 * other utilities.
152 */
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100153 if (OVL_TYPE_MERGE(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200154 stat->nlink = 1;
155
156 return 0;
157}
158
Vivek Goyal72e48482016-06-16 10:09:14 -0400159/* Common operations required to be done after creation of file on upper */
160static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200161 struct dentry *newdentry, bool hardlink)
Vivek Goyal72e48482016-06-16 10:09:14 -0400162{
163 ovl_dentry_version_inc(dentry->d_parent);
164 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200165 if (!hardlink) {
166 ovl_inode_update(inode, d_inode(newdentry));
167 ovl_copyattr(newdentry->d_inode, inode);
168 } else {
169 WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
170 inc_nlink(inode);
171 }
Vivek Goyal72e48482016-06-16 10:09:14 -0400172 d_instantiate(dentry, inode);
173}
174
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200175static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
176 struct kstat *stat, const char *link,
177 struct dentry *hardlink)
178{
179 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
180 struct inode *udir = upperdir->d_inode;
181 struct dentry *newdentry;
182 int err;
183
Miklos Szeredi38b25692016-09-01 11:11:59 +0200184 if (!hardlink && !IS_POSIXACL(udir))
185 stat->mode &= ~current_umask();
186
Al Viro59551022016-01-22 15:40:57 -0500187 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200188 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
189 dentry->d_name.len);
190 err = PTR_ERR(newdentry);
191 if (IS_ERR(newdentry))
192 goto out_unlock;
193 err = ovl_create_real(udir, newdentry, stat, link, hardlink, false);
194 if (err)
195 goto out_dput;
196
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200197 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200198 newdentry = NULL;
199out_dput:
200 dput(newdentry);
201out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500202 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200203 return err;
204}
205
206static int ovl_lock_rename_workdir(struct dentry *workdir,
207 struct dentry *upperdir)
208{
209 /* Workdir should not be the same as upperdir */
210 if (workdir == upperdir)
211 goto err;
212
213 /* Workdir should not be subdir of upperdir and vice versa */
214 if (lock_rename(workdir, upperdir) != NULL)
215 goto err_unlock;
216
217 return 0;
218
219err_unlock:
220 unlock_rename(workdir, upperdir);
221err:
222 pr_err("overlayfs: failed to lock workdir+upperdir\n");
223 return -EIO;
224}
225
226static struct dentry *ovl_clear_empty(struct dentry *dentry,
227 struct list_head *list)
228{
229 struct dentry *workdir = ovl_workdir(dentry);
230 struct inode *wdir = workdir->d_inode;
231 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
232 struct inode *udir = upperdir->d_inode;
233 struct path upperpath;
234 struct dentry *upper;
235 struct dentry *opaquedir;
236 struct kstat stat;
237 int err;
238
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200239 if (WARN_ON(!workdir))
240 return ERR_PTR(-EROFS);
241
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200242 err = ovl_lock_rename_workdir(workdir, upperdir);
243 if (err)
244 goto out;
245
246 ovl_path_upper(dentry, &upperpath);
247 err = vfs_getattr(&upperpath, &stat);
248 if (err)
249 goto out_unlock;
250
251 err = -ESTALE;
252 if (!S_ISDIR(stat.mode))
253 goto out_unlock;
254 upper = upperpath.dentry;
255 if (upper->d_parent->d_inode != udir)
256 goto out_unlock;
257
258 opaquedir = ovl_lookup_temp(workdir, dentry);
259 err = PTR_ERR(opaquedir);
260 if (IS_ERR(opaquedir))
261 goto out_unlock;
262
263 err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true);
264 if (err)
265 goto out_dput;
266
267 err = ovl_copy_xattr(upper, opaquedir);
268 if (err)
269 goto out_cleanup;
270
271 err = ovl_set_opaque(opaquedir);
272 if (err)
273 goto out_cleanup;
274
Al Viro59551022016-01-22 15:40:57 -0500275 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200276 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500277 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200278 if (err)
279 goto out_cleanup;
280
281 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
282 if (err)
283 goto out_cleanup;
284
285 ovl_cleanup_whiteouts(upper, list);
286 ovl_cleanup(wdir, upper);
287 unlock_rename(workdir, upperdir);
288
289 /* dentry's upper doesn't match now, get rid of it */
290 d_drop(dentry);
291
292 return opaquedir;
293
294out_cleanup:
295 ovl_cleanup(wdir, opaquedir);
296out_dput:
297 dput(opaquedir);
298out_unlock:
299 unlock_rename(workdir, upperdir);
300out:
301 return ERR_PTR(err);
302}
303
Miklos Szeredia105d682014-11-20 16:39:59 +0100304static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200305{
306 int err;
307 struct dentry *ret = NULL;
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200308 enum ovl_path_type type = ovl_path_type(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200309 LIST_HEAD(list);
310
311 err = ovl_check_empty_dir(dentry, &list);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200312 if (err) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200313 ret = ERR_PTR(err);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200314 goto out_free;
Miklos Szeredia105d682014-11-20 16:39:59 +0100315 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200316
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200317 /*
318 * When removing an empty opaque directory, then it makes no sense to
319 * replace it with an exact replica of itself.
320 *
321 * If no upperdentry then skip clearing whiteouts.
322 *
323 * Can race with copy-up, since we don't hold the upperdir mutex.
324 * Doesn't matter, since copy-up can't create a non-empty directory
325 * from an empty one.
326 */
327 if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
328 ret = ovl_clear_empty(dentry, &list);
329
330out_free:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200331 ovl_cache_free(&list);
332
333 return ret;
334}
335
Miklos Szeredi38b25692016-09-01 11:11:59 +0200336static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
337 const struct posix_acl *acl)
338{
339 void *buffer;
340 size_t size;
341 int err;
342
343 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
344 return 0;
345
346 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
347 buffer = kmalloc(size, GFP_KERNEL);
348 if (!buffer)
349 return -ENOMEM;
350
351 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
352 err = size;
353 if (err < 0)
354 goto out_free;
355
356 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
357out_free:
358 kfree(buffer);
359 return err;
360}
361
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200362static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
363 struct kstat *stat, const char *link,
364 struct dentry *hardlink)
365{
366 struct dentry *workdir = ovl_workdir(dentry);
367 struct inode *wdir = workdir->d_inode;
368 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
369 struct inode *udir = upperdir->d_inode;
370 struct dentry *upper;
371 struct dentry *newdentry;
372 int err;
Miklos Szeredi38b25692016-09-01 11:11:59 +0200373 struct posix_acl *acl, *default_acl;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200374
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200375 if (WARN_ON(!workdir))
376 return -EROFS;
377
Miklos Szeredi38b25692016-09-01 11:11:59 +0200378 if (!hardlink) {
379 err = posix_acl_create(dentry->d_parent->d_inode,
380 &stat->mode, &default_acl, &acl);
381 if (err)
382 return err;
383 }
384
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200385 err = ovl_lock_rename_workdir(workdir, upperdir);
386 if (err)
387 goto out;
388
389 newdentry = ovl_lookup_temp(workdir, dentry);
390 err = PTR_ERR(newdentry);
391 if (IS_ERR(newdentry))
392 goto out_unlock;
393
394 upper = lookup_one_len(dentry->d_name.name, upperdir,
395 dentry->d_name.len);
396 err = PTR_ERR(upper);
397 if (IS_ERR(upper))
398 goto out_dput;
399
400 err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
401 if (err)
402 goto out_dput2;
403
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200404 /*
405 * mode could have been mutilated due to umask (e.g. sgid directory)
406 */
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200407 if (!hardlink &&
408 !S_ISLNK(stat->mode) && newdentry->d_inode->i_mode != stat->mode) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200409 struct iattr attr = {
410 .ia_valid = ATTR_MODE,
411 .ia_mode = stat->mode,
412 };
413 inode_lock(newdentry->d_inode);
414 err = notify_change(newdentry, &attr, NULL);
415 inode_unlock(newdentry->d_inode);
416 if (err)
417 goto out_cleanup;
418 }
Miklos Szeredi38b25692016-09-01 11:11:59 +0200419 if (!hardlink) {
420 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
421 acl);
422 if (err)
423 goto out_cleanup;
424
425 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
426 default_acl);
427 if (err)
428 goto out_cleanup;
429 }
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200430
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200431 if (!hardlink && S_ISDIR(stat->mode)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200432 err = ovl_set_opaque(newdentry);
433 if (err)
434 goto out_cleanup;
435
436 err = ovl_do_rename(wdir, newdentry, udir, upper,
437 RENAME_EXCHANGE);
438 if (err)
439 goto out_cleanup;
440
441 ovl_cleanup(wdir, upper);
442 } else {
443 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
444 if (err)
445 goto out_cleanup;
446 }
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200447 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200448 newdentry = NULL;
449out_dput2:
450 dput(upper);
451out_dput:
452 dput(newdentry);
453out_unlock:
454 unlock_rename(workdir, upperdir);
455out:
Miklos Szeredi38b25692016-09-01 11:11:59 +0200456 if (!hardlink) {
457 posix_acl_release(acl);
458 posix_acl_release(default_acl);
459 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200460 return err;
461
462out_cleanup:
463 ovl_cleanup(wdir, newdentry);
464 goto out_dput2;
465}
466
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200467static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
468 struct kstat *stat, const char *link,
469 struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200470{
471 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400472 const struct cred *old_cred;
473 struct cred *override_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200474
475 err = ovl_copy_up(dentry->d_parent);
476 if (err)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200477 return err;
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200478
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400479 old_cred = ovl_override_creds(dentry->d_sb);
480 err = -ENOMEM;
481 override_cred = prepare_creds();
482 if (override_cred) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200483 override_cred->fsuid = inode->i_uid;
484 override_cred->fsgid = inode->i_gid;
Vivek Goyal26026252016-07-13 10:44:52 -0400485 if (!hardlink) {
486 err = security_dentry_create_files_as(dentry,
487 stat->mode, &dentry->d_name, old_cred,
488 override_cred);
489 if (err) {
490 put_cred(override_cred);
491 goto out_revert_creds;
492 }
493 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400494 put_cred(override_creds(override_cred));
495 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200496
Miklos Szeredic412ce42016-12-16 11:02:55 +0100497 if (!ovl_dentry_is_whiteout(dentry))
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200498 err = ovl_create_upper(dentry, inode, stat, link,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400499 hardlink);
500 else
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200501 err = ovl_create_over_whiteout(dentry, inode, stat,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400502 link, hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200503 }
Vivek Goyal26026252016-07-13 10:44:52 -0400504out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400505 revert_creds(old_cred);
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200506 if (!err) {
507 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
508
509 WARN_ON(inode->i_mode != realinode->i_mode);
510 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
511 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200512 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200513 return err;
514}
515
516static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
517 const char *link)
518{
519 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200520 struct inode *inode;
521 struct kstat stat = {
522 .rdev = rdev,
523 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200524
525 err = ovl_want_write(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200526 if (err)
527 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200528
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200529 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100530 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200531 if (!inode)
532 goto out_drop_write;
533
534 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
535 stat.mode = inode->i_mode;
536
537 err = ovl_create_or_link(dentry, inode, &stat, link, NULL);
538 if (err)
539 iput(inode);
540
541out_drop_write:
542 ovl_drop_write(dentry);
543out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200544 return err;
545}
546
547static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
548 bool excl)
549{
550 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
551}
552
553static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
554{
555 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
556}
557
558static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
559 dev_t rdev)
560{
561 /* Don't allow creation of "whiteout" on overlay */
562 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
563 return -EPERM;
564
565 return ovl_create_object(dentry, mode, rdev, NULL);
566}
567
568static int ovl_symlink(struct inode *dir, struct dentry *dentry,
569 const char *link)
570{
571 return ovl_create_object(dentry, S_IFLNK, 0, link);
572}
573
574static int ovl_link(struct dentry *old, struct inode *newdir,
575 struct dentry *new)
576{
577 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200578 struct inode *inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200579
580 err = ovl_want_write(old);
581 if (err)
582 goto out;
583
584 err = ovl_copy_up(old);
585 if (err)
586 goto out_drop_write;
587
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200588 inode = d_inode(old);
589 ihold(inode);
590
591 err = ovl_create_or_link(new, inode, NULL, NULL, ovl_dentry_upper(old));
592 if (err)
593 iput(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200594
595out_drop_write:
596 ovl_drop_write(old);
597out:
598 return err;
599}
600
Miklos Szeredia105d682014-11-20 16:39:59 +0100601static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200602{
603 struct dentry *workdir = ovl_workdir(dentry);
604 struct inode *wdir = workdir->d_inode;
605 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
606 struct inode *udir = upperdir->d_inode;
607 struct dentry *whiteout;
608 struct dentry *upper;
609 struct dentry *opaquedir = NULL;
610 int err;
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700611 int flags = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200612
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200613 if (WARN_ON(!workdir))
614 return -EROFS;
615
Miklos Szeredid377c5e2015-05-14 10:04:44 +0200616 if (is_dir) {
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200617 opaquedir = ovl_check_empty_and_clear(dentry);
618 err = PTR_ERR(opaquedir);
619 if (IS_ERR(opaquedir))
620 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200621 }
622
623 err = ovl_lock_rename_workdir(workdir, upperdir);
624 if (err)
625 goto out_dput;
626
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700627 upper = lookup_one_len(dentry->d_name.name, upperdir,
628 dentry->d_name.len);
629 err = PTR_ERR(upper);
630 if (IS_ERR(upper))
631 goto out_unlock;
632
633 err = -ESTALE;
634 if ((opaquedir && upper != opaquedir) ||
635 (!opaquedir && ovl_dentry_upper(dentry) &&
636 upper != ovl_dentry_upper(dentry))) {
637 goto out_dput_upper;
638 }
639
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200640 whiteout = ovl_whiteout(workdir, dentry);
641 err = PTR_ERR(whiteout);
642 if (IS_ERR(whiteout))
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700643 goto out_dput_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200644
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700645 if (d_is_dir(upper))
646 flags = RENAME_EXCHANGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200647
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700648 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
649 if (err)
650 goto kill_whiteout;
651 if (flags)
652 ovl_cleanup(wdir, upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200653
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200654 ovl_dentry_version_inc(dentry->d_parent);
655out_d_drop:
656 d_drop(dentry);
657 dput(whiteout);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700658out_dput_upper:
659 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200660out_unlock:
661 unlock_rename(workdir, upperdir);
662out_dput:
663 dput(opaquedir);
664out:
665 return err;
666
667kill_whiteout:
668 ovl_cleanup(wdir, whiteout);
669 goto out_d_drop;
670}
671
672static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
673{
674 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
675 struct inode *dir = upperdir->d_inode;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100676 struct dentry *upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200677 int err;
678
Al Viro59551022016-01-22 15:40:57 -0500679 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100680 upper = lookup_one_len(dentry->d_name.name, upperdir,
681 dentry->d_name.len);
682 err = PTR_ERR(upper);
683 if (IS_ERR(upper))
684 goto out_unlock;
685
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200686 err = -ESTALE;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100687 if (upper == ovl_dentry_upper(dentry)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200688 if (is_dir)
689 err = vfs_rmdir(dir, upper);
690 else
691 err = vfs_unlink(dir, upper, NULL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200692 ovl_dentry_version_inc(dentry->d_parent);
693 }
Miklos Szeredi11f37102016-03-21 17:31:44 +0100694 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200695
696 /*
697 * Keeping this dentry hashed would mean having to release
698 * upperpath/lowerpath, which could only be done if we are the
699 * sole user of this dentry. Too tricky... Just unhash for
700 * now.
701 */
Rui Wangce9113b2016-01-08 23:09:59 +0800702 if (!err)
703 d_drop(dentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100704out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500705 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200706
707 return err;
708}
709
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200710static int ovl_do_remove(struct dentry *dentry, bool is_dir)
711{
712 enum ovl_path_type type;
713 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400714 const struct cred *old_cred;
715
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200716 err = ovl_want_write(dentry);
717 if (err)
718 goto out;
719
720 err = ovl_copy_up(dentry->d_parent);
721 if (err)
722 goto out_drop_write;
723
724 type = ovl_path_type(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400725
726 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100727 if (!ovl_lower_positive(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200728 err = ovl_remove_upper(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400729 else
Miklos Szeredia105d682014-11-20 16:39:59 +0100730 err = ovl_remove_and_whiteout(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400731 revert_creds(old_cred);
Miklos Szeredidbc816d2016-07-29 12:05:24 +0200732 if (!err) {
733 if (is_dir)
734 clear_nlink(dentry->d_inode);
735 else
736 drop_nlink(dentry->d_inode);
737 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200738out_drop_write:
739 ovl_drop_write(dentry);
740out:
741 return err;
742}
743
744static int ovl_unlink(struct inode *dir, struct dentry *dentry)
745{
746 return ovl_do_remove(dentry, false);
747}
748
749static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
750{
751 return ovl_do_remove(dentry, true);
752}
753
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100754static bool ovl_type_merge_or_lower(struct dentry *dentry)
755{
756 enum ovl_path_type type = ovl_path_type(dentry);
757
758 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
759}
760
Miklos Szeredia6c60652016-12-16 11:02:56 +0100761static bool ovl_can_move(struct dentry *dentry)
762{
763 return ovl_redirect_dir(dentry->d_sb) ||
764 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
765}
766
767#define OVL_REDIRECT_MAX 256
768
769static char *ovl_get_redirect(struct dentry *dentry, bool samedir)
770{
771 char *buf, *ret;
772 struct dentry *d, *tmp;
773 int buflen = OVL_REDIRECT_MAX + 1;
774
775 if (samedir) {
776 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
777 GFP_KERNEL);
778 goto out;
779 }
780
781 buf = ret = kmalloc(buflen, GFP_TEMPORARY);
782 if (!buf)
783 goto out;
784
785 buflen--;
786 buf[buflen] = '\0';
787 for (d = dget(dentry); !IS_ROOT(d);) {
788 const char *name;
789 int thislen;
790
791 spin_lock(&d->d_lock);
792 name = ovl_dentry_get_redirect(d);
793 if (name) {
794 thislen = strlen(name);
795 } else {
796 name = d->d_name.name;
797 thislen = d->d_name.len;
798 }
799
800 /* If path is too long, fall back to userspace move */
801 if (thislen + (name[0] != '/') > buflen) {
802 ret = ERR_PTR(-EXDEV);
803 spin_unlock(&d->d_lock);
804 goto out_put;
805 }
806
807 buflen -= thislen;
808 memcpy(&buf[buflen], name, thislen);
809 tmp = dget_dlock(d->d_parent);
810 spin_unlock(&d->d_lock);
811
812 dput(d);
813 d = tmp;
814
815 /* Absolute redirect: finished */
816 if (buf[buflen] == '/')
817 break;
818 buflen--;
819 buf[buflen] = '/';
820 }
821 ret = kstrdup(&buf[buflen], GFP_KERNEL);
822out_put:
823 dput(d);
824 kfree(buf);
825out:
826 return ret ? ret : ERR_PTR(-ENOMEM);
827}
828
829static int ovl_set_redirect(struct dentry *dentry, bool samedir)
830{
831 int err;
832 const char *redirect = ovl_dentry_get_redirect(dentry);
833
834 if (redirect && (samedir || redirect[0] == '/'))
835 return 0;
836
837 redirect = ovl_get_redirect(dentry, samedir);
838 if (IS_ERR(redirect))
839 return PTR_ERR(redirect);
840
841 err = ovl_do_setxattr(ovl_dentry_upper(dentry), OVL_XATTR_REDIRECT,
842 redirect, strlen(redirect), 0);
843 if (!err) {
844 spin_lock(&dentry->d_lock);
845 ovl_dentry_set_redirect(dentry, redirect);
846 spin_unlock(&dentry->d_lock);
847 } else {
848 kfree(redirect);
849 if (err == -EOPNOTSUPP)
850 ovl_clear_redirect_dir(dentry->d_sb);
851 else
852 pr_warn_ratelimited("overlay: failed to set redirect (%i)\n", err);
853 /* Fall back to userspace copy-up */
854 err = -EXDEV;
855 }
856 return err;
857}
858
Miklos Szeredi6c02cb52016-12-16 11:02:55 +0100859static int ovl_rename(struct inode *olddir, struct dentry *old,
860 struct inode *newdir, struct dentry *new,
861 unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200862{
863 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200864 struct dentry *old_upperdir;
865 struct dentry *new_upperdir;
866 struct dentry *olddentry;
867 struct dentry *newdentry;
868 struct dentry *trap;
869 bool old_opaque;
870 bool new_opaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200871 bool cleanup_whiteout = false;
872 bool overwrite = !(flags & RENAME_EXCHANGE);
David Howellse36cb0b2015-01-29 12:02:35 +0000873 bool is_dir = d_is_dir(old);
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100874 bool new_is_dir = d_is_dir(new);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100875 bool samedir = olddir == newdir;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200876 struct dentry *opaquedir = NULL;
877 const struct cred *old_cred = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200878
879 err = -EINVAL;
880 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
881 goto out;
882
883 flags &= ~RENAME_NOREPLACE;
884
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200885 /* Don't copy up directory trees */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200886 err = -EXDEV;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100887 if (!ovl_can_move(old))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200888 goto out;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100889 if (!overwrite && !ovl_can_move(new))
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100890 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200891
892 err = ovl_want_write(old);
893 if (err)
894 goto out;
895
896 err = ovl_copy_up(old);
897 if (err)
898 goto out_drop_write;
899
900 err = ovl_copy_up(new->d_parent);
901 if (err)
902 goto out_drop_write;
903 if (!overwrite) {
904 err = ovl_copy_up(new);
905 if (err)
906 goto out_drop_write;
907 }
908
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400909 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200910
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100911 if (overwrite && new_is_dir && ovl_type_merge_or_lower(new)) {
Miklos Szeredia105d682014-11-20 16:39:59 +0100912 opaquedir = ovl_check_empty_and_clear(new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200913 err = PTR_ERR(opaquedir);
914 if (IS_ERR(opaquedir)) {
915 opaquedir = NULL;
916 goto out_revert_creds;
917 }
918 }
919
920 if (overwrite) {
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100921 if (ovl_lower_positive(old)) {
Miklos Szeredic412ce42016-12-16 11:02:55 +0100922 if (!ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200923 /* Whiteout source */
924 flags |= RENAME_WHITEOUT;
925 } else {
926 /* Switch whiteouts */
927 flags |= RENAME_EXCHANGE;
928 }
Miklos Szeredic412ce42016-12-16 11:02:55 +0100929 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200930 flags |= RENAME_EXCHANGE;
931 cleanup_whiteout = true;
932 }
933 }
934
935 old_upperdir = ovl_dentry_upper(old->d_parent);
936 new_upperdir = ovl_dentry_upper(new->d_parent);
937
938 trap = lock_rename(new_upperdir, old_upperdir);
939
Miklos Szeredi11f37102016-03-21 17:31:44 +0100940 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
941 old->d_name.len);
942 err = PTR_ERR(olddentry);
943 if (IS_ERR(olddentry))
944 goto out_unlock;
945
946 err = -ESTALE;
947 if (olddentry != ovl_dentry_upper(old))
948 goto out_dput_old;
949
950 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
951 new->d_name.len);
952 err = PTR_ERR(newdentry);
953 if (IS_ERR(newdentry))
954 goto out_dput_old;
955
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +0100956 old_opaque = ovl_dentry_is_opaque(old);
957 new_opaque = ovl_dentry_is_opaque(new);
958
Miklos Szeredi11f37102016-03-21 17:31:44 +0100959 err = -ESTALE;
960 if (ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200961 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100962 if (newdentry != opaquedir)
963 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200964 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100965 if (newdentry != ovl_dentry_upper(new))
966 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200967 }
968 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100969 if (!d_is_negative(newdentry) &&
970 (!new_opaque || !ovl_is_whiteout(newdentry)))
971 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200972 }
973
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200974 if (olddentry == trap)
975 goto out_dput;
976 if (newdentry == trap)
977 goto out_dput;
978
Miklos Szeredi804032f2016-12-16 11:02:55 +0100979 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
980 goto out_dput;
981
Miklos Szeredia6c60652016-12-16 11:02:56 +0100982 if (is_dir) {
983 if (ovl_type_merge_or_lower(old)) {
984 err = ovl_set_redirect(old, samedir);
985 if (err)
986 goto out_dput;
987 } else if (!old_opaque && ovl_lower_positive(new)) {
988 err = ovl_set_opaque(olddentry);
989 if (err)
990 goto out_dput;
991 ovl_dentry_set_opaque(old, true);
992 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200993 }
Miklos Szeredia6c60652016-12-16 11:02:56 +0100994 if (!overwrite && new_is_dir) {
995 if (ovl_type_merge_or_lower(new)) {
996 err = ovl_set_redirect(new, samedir);
997 if (err)
998 goto out_dput;
999 } else if (!new_opaque && ovl_lower_positive(old)) {
1000 err = ovl_set_opaque(newdentry);
1001 if (err)
1002 goto out_dput;
1003 ovl_dentry_set_opaque(new, true);
1004 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001005 }
1006
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +01001007 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1008 new_upperdir->d_inode, newdentry, flags);
1009 if (err)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001010 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001011
1012 if (cleanup_whiteout)
1013 ovl_cleanup(old_upperdir->d_inode, newdentry);
1014
1015 ovl_dentry_version_inc(old->d_parent);
1016 ovl_dentry_version_inc(new->d_parent);
1017
1018out_dput:
1019 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +01001020out_dput_old:
1021 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001022out_unlock:
1023 unlock_rename(new_upperdir, old_upperdir);
1024out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -04001025 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001026out_drop_write:
1027 ovl_drop_write(old);
1028out:
1029 dput(opaquedir);
1030 return err;
1031}
1032
1033const struct inode_operations ovl_dir_inode_operations = {
1034 .lookup = ovl_lookup,
1035 .mkdir = ovl_mkdir,
1036 .symlink = ovl_symlink,
1037 .unlink = ovl_unlink,
1038 .rmdir = ovl_rmdir,
Miklos Szeredi6c02cb52016-12-16 11:02:55 +01001039 .rename = ovl_rename,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001040 .link = ovl_link,
1041 .setattr = ovl_setattr,
1042 .create = ovl_create,
1043 .mknod = ovl_mknod,
1044 .permission = ovl_permission,
1045 .getattr = ovl_dir_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001046 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -04001047 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001048 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001049};