blob: b84d61b353cd4c1a315b2fc78655a146905450e5 [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
131static int ovl_set_opaque(struct dentry *upperdentry)
132{
hujianyangcead89b2014-11-24 18:25:21 +0800133 return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200134}
135
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200136static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
137 struct kstat *stat)
138{
139 int err;
140 enum ovl_path_type type;
141 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400142 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200143
144 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400145 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200146 err = vfs_getattr(&realpath, stat);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400147 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200148 if (err)
149 return err;
150
151 stat->dev = dentry->d_sb->s_dev;
152 stat->ino = dentry->d_inode->i_ino;
153
154 /*
155 * It's probably not worth it to count subdirs to get the
156 * correct link count. nlink=1 seems to pacify 'find' and
157 * other utilities.
158 */
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100159 if (OVL_TYPE_MERGE(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200160 stat->nlink = 1;
161
162 return 0;
163}
164
Vivek Goyal72e48482016-06-16 10:09:14 -0400165/* Common operations required to be done after creation of file on upper */
166static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200167 struct dentry *newdentry, bool hardlink)
Vivek Goyal72e48482016-06-16 10:09:14 -0400168{
169 ovl_dentry_version_inc(dentry->d_parent);
170 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200171 if (!hardlink) {
172 ovl_inode_update(inode, d_inode(newdentry));
173 ovl_copyattr(newdentry->d_inode, inode);
174 } else {
175 WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
176 inc_nlink(inode);
177 }
Vivek Goyal72e48482016-06-16 10:09:14 -0400178 d_instantiate(dentry, inode);
179}
180
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200181static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
182 struct kstat *stat, const char *link,
183 struct dentry *hardlink)
184{
185 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
186 struct inode *udir = upperdir->d_inode;
187 struct dentry *newdentry;
188 int err;
189
Miklos Szeredi38b25692016-09-01 11:11:59 +0200190 if (!hardlink && !IS_POSIXACL(udir))
191 stat->mode &= ~current_umask();
192
Al Viro59551022016-01-22 15:40:57 -0500193 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200194 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
195 dentry->d_name.len);
196 err = PTR_ERR(newdentry);
197 if (IS_ERR(newdentry))
198 goto out_unlock;
199 err = ovl_create_real(udir, newdentry, stat, link, hardlink, false);
200 if (err)
201 goto out_dput;
202
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200203 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200204 newdentry = NULL;
205out_dput:
206 dput(newdentry);
207out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500208 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200209 return err;
210}
211
212static int ovl_lock_rename_workdir(struct dentry *workdir,
213 struct dentry *upperdir)
214{
215 /* Workdir should not be the same as upperdir */
216 if (workdir == upperdir)
217 goto err;
218
219 /* Workdir should not be subdir of upperdir and vice versa */
220 if (lock_rename(workdir, upperdir) != NULL)
221 goto err_unlock;
222
223 return 0;
224
225err_unlock:
226 unlock_rename(workdir, upperdir);
227err:
228 pr_err("overlayfs: failed to lock workdir+upperdir\n");
229 return -EIO;
230}
231
232static struct dentry *ovl_clear_empty(struct dentry *dentry,
233 struct list_head *list)
234{
235 struct dentry *workdir = ovl_workdir(dentry);
236 struct inode *wdir = workdir->d_inode;
237 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
238 struct inode *udir = upperdir->d_inode;
239 struct path upperpath;
240 struct dentry *upper;
241 struct dentry *opaquedir;
242 struct kstat stat;
243 int err;
244
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200245 if (WARN_ON(!workdir))
246 return ERR_PTR(-EROFS);
247
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200248 err = ovl_lock_rename_workdir(workdir, upperdir);
249 if (err)
250 goto out;
251
252 ovl_path_upper(dentry, &upperpath);
253 err = vfs_getattr(&upperpath, &stat);
254 if (err)
255 goto out_unlock;
256
257 err = -ESTALE;
258 if (!S_ISDIR(stat.mode))
259 goto out_unlock;
260 upper = upperpath.dentry;
261 if (upper->d_parent->d_inode != udir)
262 goto out_unlock;
263
264 opaquedir = ovl_lookup_temp(workdir, dentry);
265 err = PTR_ERR(opaquedir);
266 if (IS_ERR(opaquedir))
267 goto out_unlock;
268
269 err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true);
270 if (err)
271 goto out_dput;
272
273 err = ovl_copy_xattr(upper, opaquedir);
274 if (err)
275 goto out_cleanup;
276
277 err = ovl_set_opaque(opaquedir);
278 if (err)
279 goto out_cleanup;
280
Al Viro59551022016-01-22 15:40:57 -0500281 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200282 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500283 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200284 if (err)
285 goto out_cleanup;
286
287 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
288 if (err)
289 goto out_cleanup;
290
291 ovl_cleanup_whiteouts(upper, list);
292 ovl_cleanup(wdir, upper);
293 unlock_rename(workdir, upperdir);
294
295 /* dentry's upper doesn't match now, get rid of it */
296 d_drop(dentry);
297
298 return opaquedir;
299
300out_cleanup:
301 ovl_cleanup(wdir, opaquedir);
302out_dput:
303 dput(opaquedir);
304out_unlock:
305 unlock_rename(workdir, upperdir);
306out:
307 return ERR_PTR(err);
308}
309
Miklos Szeredia105d682014-11-20 16:39:59 +0100310static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200311{
312 int err;
313 struct dentry *ret = NULL;
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200314 enum ovl_path_type type = ovl_path_type(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200315 LIST_HEAD(list);
316
317 err = ovl_check_empty_dir(dentry, &list);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200318 if (err) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200319 ret = ERR_PTR(err);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200320 goto out_free;
Miklos Szeredia105d682014-11-20 16:39:59 +0100321 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200322
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200323 /*
324 * When removing an empty opaque directory, then it makes no sense to
325 * replace it with an exact replica of itself.
326 *
327 * If no upperdentry then skip clearing whiteouts.
328 *
329 * Can race with copy-up, since we don't hold the upperdir mutex.
330 * Doesn't matter, since copy-up can't create a non-empty directory
331 * from an empty one.
332 */
333 if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
334 ret = ovl_clear_empty(dentry, &list);
335
336out_free:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200337 ovl_cache_free(&list);
338
339 return ret;
340}
341
Miklos Szeredi38b25692016-09-01 11:11:59 +0200342static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
343 const struct posix_acl *acl)
344{
345 void *buffer;
346 size_t size;
347 int err;
348
349 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
350 return 0;
351
352 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
353 buffer = kmalloc(size, GFP_KERNEL);
354 if (!buffer)
355 return -ENOMEM;
356
357 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
358 err = size;
359 if (err < 0)
360 goto out_free;
361
362 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
363out_free:
364 kfree(buffer);
365 return err;
366}
367
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200368static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
369 struct kstat *stat, const char *link,
370 struct dentry *hardlink)
371{
372 struct dentry *workdir = ovl_workdir(dentry);
373 struct inode *wdir = workdir->d_inode;
374 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
375 struct inode *udir = upperdir->d_inode;
376 struct dentry *upper;
377 struct dentry *newdentry;
378 int err;
Miklos Szeredi38b25692016-09-01 11:11:59 +0200379 struct posix_acl *acl, *default_acl;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200380
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200381 if (WARN_ON(!workdir))
382 return -EROFS;
383
Miklos Szeredi38b25692016-09-01 11:11:59 +0200384 if (!hardlink) {
385 err = posix_acl_create(dentry->d_parent->d_inode,
386 &stat->mode, &default_acl, &acl);
387 if (err)
388 return err;
389 }
390
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200391 err = ovl_lock_rename_workdir(workdir, upperdir);
392 if (err)
393 goto out;
394
395 newdentry = ovl_lookup_temp(workdir, dentry);
396 err = PTR_ERR(newdentry);
397 if (IS_ERR(newdentry))
398 goto out_unlock;
399
400 upper = lookup_one_len(dentry->d_name.name, upperdir,
401 dentry->d_name.len);
402 err = PTR_ERR(upper);
403 if (IS_ERR(upper))
404 goto out_dput;
405
406 err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
407 if (err)
408 goto out_dput2;
409
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200410 /*
411 * mode could have been mutilated due to umask (e.g. sgid directory)
412 */
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200413 if (!hardlink &&
414 !S_ISLNK(stat->mode) && newdentry->d_inode->i_mode != stat->mode) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200415 struct iattr attr = {
416 .ia_valid = ATTR_MODE,
417 .ia_mode = stat->mode,
418 };
419 inode_lock(newdentry->d_inode);
420 err = notify_change(newdentry, &attr, NULL);
421 inode_unlock(newdentry->d_inode);
422 if (err)
423 goto out_cleanup;
424 }
Miklos Szeredi38b25692016-09-01 11:11:59 +0200425 if (!hardlink) {
426 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
427 acl);
428 if (err)
429 goto out_cleanup;
430
431 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
432 default_acl);
433 if (err)
434 goto out_cleanup;
435 }
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200436
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200437 if (!hardlink && S_ISDIR(stat->mode)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200438 err = ovl_set_opaque(newdentry);
439 if (err)
440 goto out_cleanup;
441
442 err = ovl_do_rename(wdir, newdentry, udir, upper,
443 RENAME_EXCHANGE);
444 if (err)
445 goto out_cleanup;
446
447 ovl_cleanup(wdir, upper);
448 } else {
449 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
450 if (err)
451 goto out_cleanup;
452 }
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200453 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200454 newdentry = NULL;
455out_dput2:
456 dput(upper);
457out_dput:
458 dput(newdentry);
459out_unlock:
460 unlock_rename(workdir, upperdir);
461out:
Miklos Szeredi38b25692016-09-01 11:11:59 +0200462 if (!hardlink) {
463 posix_acl_release(acl);
464 posix_acl_release(default_acl);
465 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200466 return err;
467
468out_cleanup:
469 ovl_cleanup(wdir, newdentry);
470 goto out_dput2;
471}
472
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200473static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
474 struct kstat *stat, const char *link,
475 struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200476{
477 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400478 const struct cred *old_cred;
479 struct cred *override_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200480
481 err = ovl_copy_up(dentry->d_parent);
482 if (err)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200483 return err;
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200484
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400485 old_cred = ovl_override_creds(dentry->d_sb);
486 err = -ENOMEM;
487 override_cred = prepare_creds();
488 if (override_cred) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200489 override_cred->fsuid = inode->i_uid;
490 override_cred->fsgid = inode->i_gid;
Vivek Goyal26026252016-07-13 10:44:52 -0400491 if (!hardlink) {
492 err = security_dentry_create_files_as(dentry,
493 stat->mode, &dentry->d_name, old_cred,
494 override_cred);
495 if (err) {
496 put_cred(override_cred);
497 goto out_revert_creds;
498 }
499 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400500 put_cred(override_creds(override_cred));
501 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200502
Miklos Szeredic412ce42016-12-16 11:02:55 +0100503 if (!ovl_dentry_is_whiteout(dentry))
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200504 err = ovl_create_upper(dentry, inode, stat, link,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400505 hardlink);
506 else
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200507 err = ovl_create_over_whiteout(dentry, inode, stat,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400508 link, hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200509 }
Vivek Goyal26026252016-07-13 10:44:52 -0400510out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400511 revert_creds(old_cred);
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200512 if (!err) {
513 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
514
515 WARN_ON(inode->i_mode != realinode->i_mode);
516 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
517 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200518 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200519 return err;
520}
521
522static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
523 const char *link)
524{
525 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200526 struct inode *inode;
527 struct kstat stat = {
528 .rdev = rdev,
529 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200530
531 err = ovl_want_write(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200532 if (err)
533 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200534
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200535 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100536 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200537 if (!inode)
538 goto out_drop_write;
539
540 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
541 stat.mode = inode->i_mode;
542
543 err = ovl_create_or_link(dentry, inode, &stat, link, NULL);
544 if (err)
545 iput(inode);
546
547out_drop_write:
548 ovl_drop_write(dentry);
549out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200550 return err;
551}
552
553static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
554 bool excl)
555{
556 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
557}
558
559static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
560{
561 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
562}
563
564static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
565 dev_t rdev)
566{
567 /* Don't allow creation of "whiteout" on overlay */
568 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
569 return -EPERM;
570
571 return ovl_create_object(dentry, mode, rdev, NULL);
572}
573
574static int ovl_symlink(struct inode *dir, struct dentry *dentry,
575 const char *link)
576{
577 return ovl_create_object(dentry, S_IFLNK, 0, link);
578}
579
580static int ovl_link(struct dentry *old, struct inode *newdir,
581 struct dentry *new)
582{
583 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200584 struct inode *inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200585
586 err = ovl_want_write(old);
587 if (err)
588 goto out;
589
590 err = ovl_copy_up(old);
591 if (err)
592 goto out_drop_write;
593
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200594 inode = d_inode(old);
595 ihold(inode);
596
597 err = ovl_create_or_link(new, inode, NULL, NULL, ovl_dentry_upper(old));
598 if (err)
599 iput(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200600
601out_drop_write:
602 ovl_drop_write(old);
603out:
604 return err;
605}
606
Miklos Szeredia105d682014-11-20 16:39:59 +0100607static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200608{
609 struct dentry *workdir = ovl_workdir(dentry);
610 struct inode *wdir = workdir->d_inode;
611 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
612 struct inode *udir = upperdir->d_inode;
613 struct dentry *whiteout;
614 struct dentry *upper;
615 struct dentry *opaquedir = NULL;
616 int err;
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700617 int flags = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200618
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200619 if (WARN_ON(!workdir))
620 return -EROFS;
621
Miklos Szeredid377c5e2015-05-14 10:04:44 +0200622 if (is_dir) {
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200623 opaquedir = ovl_check_empty_and_clear(dentry);
624 err = PTR_ERR(opaquedir);
625 if (IS_ERR(opaquedir))
626 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200627 }
628
629 err = ovl_lock_rename_workdir(workdir, upperdir);
630 if (err)
631 goto out_dput;
632
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700633 upper = lookup_one_len(dentry->d_name.name, upperdir,
634 dentry->d_name.len);
635 err = PTR_ERR(upper);
636 if (IS_ERR(upper))
637 goto out_unlock;
638
639 err = -ESTALE;
640 if ((opaquedir && upper != opaquedir) ||
641 (!opaquedir && ovl_dentry_upper(dentry) &&
642 upper != ovl_dentry_upper(dentry))) {
643 goto out_dput_upper;
644 }
645
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200646 whiteout = ovl_whiteout(workdir, dentry);
647 err = PTR_ERR(whiteout);
648 if (IS_ERR(whiteout))
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700649 goto out_dput_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200650
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700651 if (d_is_dir(upper))
652 flags = RENAME_EXCHANGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200653
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700654 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
655 if (err)
656 goto kill_whiteout;
657 if (flags)
658 ovl_cleanup(wdir, upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200659
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200660 ovl_dentry_version_inc(dentry->d_parent);
661out_d_drop:
662 d_drop(dentry);
663 dput(whiteout);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700664out_dput_upper:
665 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200666out_unlock:
667 unlock_rename(workdir, upperdir);
668out_dput:
669 dput(opaquedir);
670out:
671 return err;
672
673kill_whiteout:
674 ovl_cleanup(wdir, whiteout);
675 goto out_d_drop;
676}
677
678static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
679{
680 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
681 struct inode *dir = upperdir->d_inode;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100682 struct dentry *upper;
Amir Goldsteind1595112016-10-26 12:34:06 +0300683 struct dentry *opaquedir = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200684 int err;
685
Amir Goldsteind1595112016-10-26 12:34:06 +0300686 /* Redirect dir can be !ovl_lower_positive && OVL_TYPE_MERGE */
687 if (is_dir && ovl_dentry_get_redirect(dentry)) {
688 opaquedir = ovl_check_empty_and_clear(dentry);
689 err = PTR_ERR(opaquedir);
690 if (IS_ERR(opaquedir))
691 goto out;
692 }
693
Al Viro59551022016-01-22 15:40:57 -0500694 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100695 upper = lookup_one_len(dentry->d_name.name, upperdir,
696 dentry->d_name.len);
697 err = PTR_ERR(upper);
698 if (IS_ERR(upper))
699 goto out_unlock;
700
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200701 err = -ESTALE;
Amir Goldsteind1595112016-10-26 12:34:06 +0300702 if ((opaquedir && upper != opaquedir) ||
703 (!opaquedir && upper != ovl_dentry_upper(dentry)))
704 goto out_dput_upper;
705
706 if (is_dir)
707 err = vfs_rmdir(dir, upper);
708 else
709 err = vfs_unlink(dir, upper, NULL);
710 ovl_dentry_version_inc(dentry->d_parent);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200711
712 /*
713 * Keeping this dentry hashed would mean having to release
714 * upperpath/lowerpath, which could only be done if we are the
715 * sole user of this dentry. Too tricky... Just unhash for
716 * now.
717 */
Rui Wangce9113b2016-01-08 23:09:59 +0800718 if (!err)
719 d_drop(dentry);
Amir Goldsteind1595112016-10-26 12:34:06 +0300720out_dput_upper:
721 dput(upper);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100722out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500723 inode_unlock(dir);
Amir Goldsteind1595112016-10-26 12:34:06 +0300724 dput(opaquedir);
725out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200726 return err;
727}
728
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200729static int ovl_do_remove(struct dentry *dentry, bool is_dir)
730{
731 enum ovl_path_type type;
732 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400733 const struct cred *old_cred;
734
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200735 err = ovl_want_write(dentry);
736 if (err)
737 goto out;
738
739 err = ovl_copy_up(dentry->d_parent);
740 if (err)
741 goto out_drop_write;
742
743 type = ovl_path_type(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400744
745 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100746 if (!ovl_lower_positive(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200747 err = ovl_remove_upper(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400748 else
Miklos Szeredia105d682014-11-20 16:39:59 +0100749 err = ovl_remove_and_whiteout(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400750 revert_creds(old_cred);
Miklos Szeredidbc816d2016-07-29 12:05:24 +0200751 if (!err) {
752 if (is_dir)
753 clear_nlink(dentry->d_inode);
754 else
755 drop_nlink(dentry->d_inode);
756 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200757out_drop_write:
758 ovl_drop_write(dentry);
759out:
760 return err;
761}
762
763static int ovl_unlink(struct inode *dir, struct dentry *dentry)
764{
765 return ovl_do_remove(dentry, false);
766}
767
768static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
769{
770 return ovl_do_remove(dentry, true);
771}
772
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100773static bool ovl_type_merge_or_lower(struct dentry *dentry)
774{
775 enum ovl_path_type type = ovl_path_type(dentry);
776
777 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
778}
779
Miklos Szeredia6c60652016-12-16 11:02:56 +0100780static bool ovl_can_move(struct dentry *dentry)
781{
782 return ovl_redirect_dir(dentry->d_sb) ||
783 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
784}
785
Miklos Szeredia6c60652016-12-16 11:02:56 +0100786static char *ovl_get_redirect(struct dentry *dentry, bool samedir)
787{
788 char *buf, *ret;
789 struct dentry *d, *tmp;
Miklos Szeredi3ea22a72016-12-16 11:02:57 +0100790 int buflen = ovl_redirect_max + 1;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100791
792 if (samedir) {
793 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
794 GFP_KERNEL);
795 goto out;
796 }
797
798 buf = ret = kmalloc(buflen, GFP_TEMPORARY);
799 if (!buf)
800 goto out;
801
802 buflen--;
803 buf[buflen] = '\0';
804 for (d = dget(dentry); !IS_ROOT(d);) {
805 const char *name;
806 int thislen;
807
808 spin_lock(&d->d_lock);
809 name = ovl_dentry_get_redirect(d);
810 if (name) {
811 thislen = strlen(name);
812 } else {
813 name = d->d_name.name;
814 thislen = d->d_name.len;
815 }
816
817 /* If path is too long, fall back to userspace move */
818 if (thislen + (name[0] != '/') > buflen) {
819 ret = ERR_PTR(-EXDEV);
820 spin_unlock(&d->d_lock);
821 goto out_put;
822 }
823
824 buflen -= thislen;
825 memcpy(&buf[buflen], name, thislen);
826 tmp = dget_dlock(d->d_parent);
827 spin_unlock(&d->d_lock);
828
829 dput(d);
830 d = tmp;
831
832 /* Absolute redirect: finished */
833 if (buf[buflen] == '/')
834 break;
835 buflen--;
836 buf[buflen] = '/';
837 }
838 ret = kstrdup(&buf[buflen], GFP_KERNEL);
839out_put:
840 dput(d);
841 kfree(buf);
842out:
843 return ret ? ret : ERR_PTR(-ENOMEM);
844}
845
846static int ovl_set_redirect(struct dentry *dentry, bool samedir)
847{
848 int err;
849 const char *redirect = ovl_dentry_get_redirect(dentry);
850
851 if (redirect && (samedir || redirect[0] == '/'))
852 return 0;
853
854 redirect = ovl_get_redirect(dentry, samedir);
855 if (IS_ERR(redirect))
856 return PTR_ERR(redirect);
857
858 err = ovl_do_setxattr(ovl_dentry_upper(dentry), OVL_XATTR_REDIRECT,
859 redirect, strlen(redirect), 0);
860 if (!err) {
861 spin_lock(&dentry->d_lock);
862 ovl_dentry_set_redirect(dentry, redirect);
863 spin_unlock(&dentry->d_lock);
864 } else {
865 kfree(redirect);
866 if (err == -EOPNOTSUPP)
867 ovl_clear_redirect_dir(dentry->d_sb);
868 else
869 pr_warn_ratelimited("overlay: failed to set redirect (%i)\n", err);
870 /* Fall back to userspace copy-up */
871 err = -EXDEV;
872 }
873 return err;
874}
875
Miklos Szeredi6c02cb52016-12-16 11:02:55 +0100876static int ovl_rename(struct inode *olddir, struct dentry *old,
877 struct inode *newdir, struct dentry *new,
878 unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200879{
880 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200881 struct dentry *old_upperdir;
882 struct dentry *new_upperdir;
883 struct dentry *olddentry;
884 struct dentry *newdentry;
885 struct dentry *trap;
886 bool old_opaque;
887 bool new_opaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200888 bool cleanup_whiteout = false;
889 bool overwrite = !(flags & RENAME_EXCHANGE);
David Howellse36cb0b2015-01-29 12:02:35 +0000890 bool is_dir = d_is_dir(old);
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100891 bool new_is_dir = d_is_dir(new);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100892 bool samedir = olddir == newdir;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200893 struct dentry *opaquedir = NULL;
894 const struct cred *old_cred = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200895
896 err = -EINVAL;
897 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
898 goto out;
899
900 flags &= ~RENAME_NOREPLACE;
901
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200902 /* Don't copy up directory trees */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200903 err = -EXDEV;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100904 if (!ovl_can_move(old))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200905 goto out;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100906 if (!overwrite && !ovl_can_move(new))
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100907 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200908
909 err = ovl_want_write(old);
910 if (err)
911 goto out;
912
913 err = ovl_copy_up(old);
914 if (err)
915 goto out_drop_write;
916
917 err = ovl_copy_up(new->d_parent);
918 if (err)
919 goto out_drop_write;
920 if (!overwrite) {
921 err = ovl_copy_up(new);
922 if (err)
923 goto out_drop_write;
924 }
925
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400926 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200927
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100928 if (overwrite && new_is_dir && ovl_type_merge_or_lower(new)) {
Miklos Szeredia105d682014-11-20 16:39:59 +0100929 opaquedir = ovl_check_empty_and_clear(new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200930 err = PTR_ERR(opaquedir);
931 if (IS_ERR(opaquedir)) {
932 opaquedir = NULL;
933 goto out_revert_creds;
934 }
935 }
936
937 if (overwrite) {
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100938 if (ovl_lower_positive(old)) {
Miklos Szeredic412ce42016-12-16 11:02:55 +0100939 if (!ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200940 /* Whiteout source */
941 flags |= RENAME_WHITEOUT;
942 } else {
943 /* Switch whiteouts */
944 flags |= RENAME_EXCHANGE;
945 }
Miklos Szeredic412ce42016-12-16 11:02:55 +0100946 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200947 flags |= RENAME_EXCHANGE;
948 cleanup_whiteout = true;
949 }
950 }
951
952 old_upperdir = ovl_dentry_upper(old->d_parent);
953 new_upperdir = ovl_dentry_upper(new->d_parent);
954
955 trap = lock_rename(new_upperdir, old_upperdir);
956
Miklos Szeredi11f37102016-03-21 17:31:44 +0100957 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
958 old->d_name.len);
959 err = PTR_ERR(olddentry);
960 if (IS_ERR(olddentry))
961 goto out_unlock;
962
963 err = -ESTALE;
964 if (olddentry != ovl_dentry_upper(old))
965 goto out_dput_old;
966
967 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
968 new->d_name.len);
969 err = PTR_ERR(newdentry);
970 if (IS_ERR(newdentry))
971 goto out_dput_old;
972
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +0100973 old_opaque = ovl_dentry_is_opaque(old);
974 new_opaque = ovl_dentry_is_opaque(new);
975
Miklos Szeredi11f37102016-03-21 17:31:44 +0100976 err = -ESTALE;
977 if (ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200978 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100979 if (newdentry != opaquedir)
980 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200981 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100982 if (newdentry != ovl_dentry_upper(new))
983 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200984 }
985 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100986 if (!d_is_negative(newdentry) &&
987 (!new_opaque || !ovl_is_whiteout(newdentry)))
988 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200989 }
990
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200991 if (olddentry == trap)
992 goto out_dput;
993 if (newdentry == trap)
994 goto out_dput;
995
Miklos Szeredi804032f2016-12-16 11:02:55 +0100996 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
997 goto out_dput;
998
Miklos Szeredia6c60652016-12-16 11:02:56 +0100999 if (is_dir) {
1000 if (ovl_type_merge_or_lower(old)) {
1001 err = ovl_set_redirect(old, samedir);
1002 if (err)
1003 goto out_dput;
1004 } else if (!old_opaque && ovl_lower_positive(new)) {
1005 err = ovl_set_opaque(olddentry);
1006 if (err)
1007 goto out_dput;
1008 ovl_dentry_set_opaque(old, true);
1009 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001010 }
Miklos Szeredia6c60652016-12-16 11:02:56 +01001011 if (!overwrite && new_is_dir) {
1012 if (ovl_type_merge_or_lower(new)) {
1013 err = ovl_set_redirect(new, samedir);
1014 if (err)
1015 goto out_dput;
1016 } else if (!new_opaque && ovl_lower_positive(old)) {
1017 err = ovl_set_opaque(newdentry);
1018 if (err)
1019 goto out_dput;
1020 ovl_dentry_set_opaque(new, true);
1021 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001022 }
1023
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +01001024 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1025 new_upperdir->d_inode, newdentry, flags);
1026 if (err)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001027 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001028
1029 if (cleanup_whiteout)
1030 ovl_cleanup(old_upperdir->d_inode, newdentry);
1031
1032 ovl_dentry_version_inc(old->d_parent);
1033 ovl_dentry_version_inc(new->d_parent);
1034
1035out_dput:
1036 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +01001037out_dput_old:
1038 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001039out_unlock:
1040 unlock_rename(new_upperdir, old_upperdir);
1041out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -04001042 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001043out_drop_write:
1044 ovl_drop_write(old);
1045out:
1046 dput(opaquedir);
1047 return err;
1048}
1049
1050const struct inode_operations ovl_dir_inode_operations = {
1051 .lookup = ovl_lookup,
1052 .mkdir = ovl_mkdir,
1053 .symlink = ovl_symlink,
1054 .unlink = ovl_unlink,
1055 .rmdir = ovl_rmdir,
Miklos Szeredi6c02cb52016-12-16 11:02:55 +01001056 .rename = ovl_rename,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001057 .link = ovl_link,
1058 .setattr = ovl_setattr,
1059 .create = ovl_create,
1060 .mknod = ovl_mknod,
1061 .permission = ovl_permission,
1062 .getattr = ovl_dir_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001063 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -04001064 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001065 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001066};