blob: eb3b8d39fb6161f1c9de25840f9e608779b0cff7 [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
David Howellsfb5bb2c32015-07-07 15:04:44 +010010#include <linux/module.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020011#include <linux/fs.h>
12#include <linux/slab.h>
13#include <linux/file.h>
14#include <linux/splice.h>
15#include <linux/xattr.h>
16#include <linux/security.h>
17#include <linux/uaccess.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010018#include <linux/sched/signal.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010019#include <linux/cred.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020020#include <linux/namei.h>
David Howellsfb5bb2c32015-07-07 15:04:44 +010021#include <linux/fdtable.h>
22#include <linux/ratelimit.h>
Amir Goldstein3a1e8192017-03-30 15:22:16 +030023#include <linux/exportfs.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020024#include "overlayfs.h"
25
26#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
27
David Howellsfb5bb2c32015-07-07 15:04:44 +010028static bool __read_mostly ovl_check_copy_up;
29module_param_named(check_copy_up, ovl_check_copy_up, bool,
30 S_IWUSR | S_IRUGO);
31MODULE_PARM_DESC(ovl_check_copy_up,
32 "Warn on copy-up when causing process also has a R/O fd open");
33
34static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
35{
36 const struct dentry *dentry = data;
37
Al Viro45063092016-12-04 18:24:56 -050038 if (file_inode(f) == d_inode(dentry))
David Howellsfb5bb2c32015-07-07 15:04:44 +010039 pr_warn_ratelimited("overlayfs: Warning: Copying up %pD, but open R/O on fd %u which will cease to be coherent [pid=%d %s]\n",
40 f, fd, current->pid, current->comm);
41 return 0;
42}
43
44/*
45 * Check the fds open by this process and warn if something like the following
46 * scenario is about to occur:
47 *
48 * fd1 = open("foo", O_RDONLY);
49 * fd2 = open("foo", O_RDWR);
50 */
51static void ovl_do_check_copy_up(struct dentry *dentry)
52{
53 if (ovl_check_copy_up)
54 iterate_fd(current->files, 0, ovl_check_fd, dentry);
55}
56
Miklos Szeredie9be9d52014-10-24 00:14:38 +020057int ovl_copy_xattr(struct dentry *old, struct dentry *new)
58{
Vito Caputoe4ad29f2015-10-24 07:19:46 -050059 ssize_t list_size, size, value_size = 0;
60 char *buf, *name, *value = NULL;
61 int uninitialized_var(error);
Miklos Szeredi8b326c62016-09-16 14:12:11 +020062 size_t slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020063
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +020064 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
65 !(new->d_inode->i_opflags & IOP_XATTR))
Miklos Szeredie9be9d52014-10-24 00:14:38 +020066 return 0;
67
68 list_size = vfs_listxattr(old, NULL, 0);
69 if (list_size <= 0) {
70 if (list_size == -EOPNOTSUPP)
71 return 0;
72 return list_size;
73 }
74
75 buf = kzalloc(list_size, GFP_KERNEL);
76 if (!buf)
77 return -ENOMEM;
78
Miklos Szeredie9be9d52014-10-24 00:14:38 +020079 list_size = vfs_listxattr(old, buf, list_size);
80 if (list_size <= 0) {
81 error = list_size;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050082 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020083 }
84
Miklos Szeredi8b326c62016-09-16 14:12:11 +020085 for (name = buf; list_size; name += slen) {
86 slen = strnlen(name, list_size) + 1;
87
88 /* underlying fs providing us with an broken xattr list? */
89 if (WARN_ON(slen > list_size)) {
90 error = -EIO;
91 break;
92 }
93 list_size -= slen;
94
Miklos Szeredi09562542016-08-08 15:08:49 +020095 if (ovl_is_private_xattr(name))
96 continue;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050097retry:
98 size = vfs_getxattr(old, name, value, value_size);
99 if (size == -ERANGE)
100 size = vfs_getxattr(old, name, NULL, 0);
101
Miklos Szeredi97daf8b2015-11-10 17:08:41 +0100102 if (size < 0) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200103 error = size;
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500104 break;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200105 }
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500106
107 if (size > value_size) {
108 void *new;
109
110 new = krealloc(value, size, GFP_KERNEL);
111 if (!new) {
112 error = -ENOMEM;
113 break;
114 }
115 value = new;
116 value_size = size;
117 goto retry;
118 }
119
Vivek Goyal121ab822016-07-13 10:44:49 -0400120 error = security_inode_copy_up_xattr(name);
121 if (error < 0 && error != -EOPNOTSUPP)
122 break;
123 if (error == 1) {
124 error = 0;
125 continue; /* Discard */
126 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200127 error = vfs_setxattr(new, name, value, size, 0);
128 if (error)
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500129 break;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200130 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200131 kfree(value);
132out:
133 kfree(buf);
134 return error;
135}
136
137static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
138{
139 struct file *old_file;
140 struct file *new_file;
141 loff_t old_pos = 0;
142 loff_t new_pos = 0;
143 int error = 0;
144
145 if (len == 0)
146 return 0;
147
David Howells04803342015-09-18 11:45:12 +0100148 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200149 if (IS_ERR(old_file))
150 return PTR_ERR(old_file);
151
David Howells04803342015-09-18 11:45:12 +0100152 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200153 if (IS_ERR(new_file)) {
154 error = PTR_ERR(new_file);
155 goto out_fput;
156 }
157
Amir Goldstein2ea98462016-09-23 11:38:12 +0300158 /* Try to use clone_file_range to clone up within the same fs */
159 error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
160 if (!error)
161 goto out;
162 /* Couldn't clone, so now we try to copy the data */
163 error = 0;
164
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200165 /* FIXME: copy up sparse files efficiently */
166 while (len) {
167 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
168 long bytes;
169
170 if (len < this_len)
171 this_len = len;
172
173 if (signal_pending_state(TASK_KILLABLE, current)) {
174 error = -EINTR;
175 break;
176 }
177
178 bytes = do_splice_direct(old_file, &old_pos,
179 new_file, &new_pos,
180 this_len, SPLICE_F_MOVE);
181 if (bytes <= 0) {
182 error = bytes;
183 break;
184 }
185 WARN_ON(old_pos != new_pos);
186
187 len -= bytes;
188 }
Amir Goldstein2ea98462016-09-23 11:38:12 +0300189out:
Miklos Szeredi641089c2016-10-31 14:42:14 +0100190 if (!error)
191 error = vfs_fsync(new_file, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200192 fput(new_file);
193out_fput:
194 fput(old_file);
195 return error;
196}
197
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200198static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
199{
200 struct iattr attr = {
201 .ia_valid =
202 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
203 .ia_atime = stat->atime,
204 .ia_mtime = stat->mtime,
205 };
206
207 return notify_change(upperdentry, &attr, NULL);
208}
209
210int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
211{
212 int err = 0;
213
214 if (!S_ISLNK(stat->mode)) {
215 struct iattr attr = {
216 .ia_valid = ATTR_MODE,
217 .ia_mode = stat->mode,
218 };
219 err = notify_change(upperdentry, &attr, NULL);
220 }
221 if (!err) {
222 struct iattr attr = {
223 .ia_valid = ATTR_UID | ATTR_GID,
224 .ia_uid = stat->uid,
225 .ia_gid = stat->gid,
226 };
227 err = notify_change(upperdentry, &attr, NULL);
228 }
229 if (!err)
230 ovl_set_timestamps(upperdentry, stat);
231
232 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200233}
234
Amir Goldstein54fb3472017-06-21 15:28:38 +0300235struct ovl_fh *ovl_encode_fh(struct dentry *lower, bool is_upper)
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300236{
237 struct ovl_fh *fh;
238 int fh_type, fh_len, dwords;
239 void *buf;
240 int buflen = MAX_HANDLE_SZ;
Amir Goldstein02bcd152017-06-21 15:28:36 +0300241 uuid_t *uuid = &lower->d_sb->s_uuid;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300242
Michal Hocko0ee931c2017-09-13 16:28:29 -0700243 buf = kmalloc(buflen, GFP_KERNEL);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300244 if (!buf)
245 return ERR_PTR(-ENOMEM);
246
247 /*
248 * We encode a non-connectable file handle for non-dir, because we
249 * only need to find the lower inode number and we don't want to pay
250 * the price or reconnecting the dentry.
251 */
252 dwords = buflen >> 2;
253 fh_type = exportfs_encode_fh(lower, buf, &dwords, 0);
254 buflen = (dwords << 2);
255
256 fh = ERR_PTR(-EIO);
257 if (WARN_ON(fh_type < 0) ||
258 WARN_ON(buflen > MAX_HANDLE_SZ) ||
259 WARN_ON(fh_type == FILEID_INVALID))
260 goto out;
261
262 BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255);
263 fh_len = offsetof(struct ovl_fh, fid) + buflen;
264 fh = kmalloc(fh_len, GFP_KERNEL);
265 if (!fh) {
266 fh = ERR_PTR(-ENOMEM);
267 goto out;
268 }
269
270 fh->version = OVL_FH_VERSION;
271 fh->magic = OVL_FH_MAGIC;
272 fh->type = fh_type;
273 fh->flags = OVL_FH_FLAG_CPU_ENDIAN;
Amir Goldstein54fb3472017-06-21 15:28:38 +0300274 /*
275 * When we will want to decode an overlay dentry from this handle
276 * and all layers are on the same fs, if we get a disconncted real
277 * dentry when we decode fid, the only way to tell if we should assign
278 * it to upperdentry or to lowerstack is by checking this flag.
279 */
280 if (is_upper)
281 fh->flags |= OVL_FH_FLAG_PATH_UPPER;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300282 fh->len = fh_len;
283 fh->uuid = *uuid;
284 memcpy(fh->fid, buf, buflen);
285
286out:
287 kfree(buf);
288 return fh;
289}
290
291static int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
292 struct dentry *upper)
293{
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300294 const struct ovl_fh *fh = NULL;
295 int err;
296
297 /*
298 * When lower layer doesn't support export operations store a 'null' fh,
299 * so we can use the overlay.origin xattr to distignuish between a copy
300 * up and a pure upper inode.
301 */
Amir Goldstein02bcd152017-06-21 15:28:36 +0300302 if (ovl_can_decode_fh(lower->d_sb)) {
Amir Goldstein54fb3472017-06-21 15:28:38 +0300303 fh = ovl_encode_fh(lower, false);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300304 if (IS_ERR(fh))
305 return PTR_ERR(fh);
306 }
307
Miklos Szeredi6266d462017-05-18 16:11:24 +0200308 /*
309 * Do not fail when upper doesn't support xattrs.
310 */
311 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh,
312 fh ? fh->len : 0, 0);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300313 kfree(fh);
314
315 return err;
316}
317
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200318struct ovl_copy_up_ctx {
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200319 struct dentry *parent;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200320 struct dentry *dentry;
321 struct path lowerpath;
322 struct kstat stat;
323 struct kstat pstat;
324 const char *link;
Amir Goldstein59be0972017-06-20 15:25:46 +0300325 struct dentry *destdir;
326 struct qstr destname;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200327 struct dentry *workdir;
328 bool tmpfile;
Amir Goldstein59be0972017-06-20 15:25:46 +0300329 bool origin;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200330};
331
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300332static int ovl_link_up(struct ovl_copy_up_ctx *c)
333{
334 int err;
335 struct dentry *upper;
336 struct dentry *upperdir = ovl_dentry_upper(c->parent);
337 struct inode *udir = d_inode(upperdir);
338
339 /* Mark parent "impure" because it may now contain non-pure upper */
340 err = ovl_set_impure(c->parent, upperdir);
341 if (err)
342 return err;
343
344 err = ovl_set_nlink_lower(c->dentry);
345 if (err)
346 return err;
347
348 inode_lock_nested(udir, I_MUTEX_PARENT);
349 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
350 c->dentry->d_name.len);
351 err = PTR_ERR(upper);
352 if (!IS_ERR(upper)) {
353 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper,
354 true);
355 dput(upper);
356
357 if (!err) {
358 /* Restore timestamps on parent (best effort) */
359 ovl_set_timestamps(upperdir, &c->pstat);
360 ovl_dentry_set_upper_alias(c->dentry);
361 }
362 }
363 inode_unlock(udir);
364 ovl_set_nlink_upper(c->dentry);
365
366 return err;
367}
368
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200369static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp,
370 struct dentry **newdentry)
Amir Goldstein15932c42017-05-16 01:26:49 +0300371{
372 int err;
373 struct dentry *upper;
Amir Goldstein59be0972017-06-20 15:25:46 +0300374 struct inode *udir = d_inode(c->destdir);
Amir Goldstein15932c42017-05-16 01:26:49 +0300375
Amir Goldstein59be0972017-06-20 15:25:46 +0300376 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
Amir Goldstein15932c42017-05-16 01:26:49 +0300377 if (IS_ERR(upper))
378 return PTR_ERR(upper);
379
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200380 if (c->tmpfile)
Amir Goldstein15932c42017-05-16 01:26:49 +0300381 err = ovl_do_link(temp, udir, upper, true);
382 else
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200383 err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0);
Amir Goldstein15932c42017-05-16 01:26:49 +0300384
Amir Goldstein59be0972017-06-20 15:25:46 +0300385 if (!err)
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200386 *newdentry = dget(c->tmpfile ? upper : temp);
Amir Goldstein15932c42017-05-16 01:26:49 +0300387 dput(upper);
388
389 return err;
390}
391
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200392static int ovl_get_tmpfile(struct ovl_copy_up_ctx *c, struct dentry **tempp)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200393{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200394 int err;
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200395 struct dentry *temp;
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400396 const struct cred *old_creds = NULL;
397 struct cred *new_creds = NULL;
Al Viro32a3d842016-12-04 17:33:17 +0000398 struct cattr cattr = {
399 /* Can't properly set mode on creation because of the umask */
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200400 .mode = c->stat.mode & S_IFMT,
401 .rdev = c->stat.rdev,
402 .link = c->link
Al Viro32a3d842016-12-04 17:33:17 +0000403 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200404
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200405 err = security_inode_copy_up(c->dentry, &new_creds);
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400406 if (err < 0)
Miklos Szeredie85f82f2017-06-28 13:41:22 +0200407 goto out;
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400408
409 if (new_creds)
410 old_creds = override_creds(new_creds);
411
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200412 if (c->tmpfile) {
413 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200414 if (IS_ERR(temp))
415 goto temp_err;
416 } else {
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200417 temp = ovl_lookup_temp(c->workdir);
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200418 if (IS_ERR(temp))
419 goto temp_err;
420
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200421 err = ovl_create_real(d_inode(c->workdir), temp, &cattr,
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200422 NULL, true);
423 if (err) {
424 dput(temp);
425 goto out;
426 }
Amir Goldstein8137ae22017-05-16 08:45:46 +0300427 }
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200428 err = 0;
429 *tempp = temp;
430out:
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400431 if (new_creds) {
432 revert_creds(old_creds);
433 put_cred(new_creds);
434 }
435
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200436 return err;
437
438temp_err:
439 err = PTR_ERR(temp);
440 goto out;
441}
442
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200443static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
Amir Goldstein02209d12017-05-19 15:16:21 +0300444{
445 int err;
446
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200447 if (S_ISREG(c->stat.mode)) {
Amir Goldstein02209d12017-05-19 15:16:21 +0300448 struct path upperpath;
449
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200450 ovl_path_upper(c->dentry, &upperpath);
Amir Goldstein02209d12017-05-19 15:16:21 +0300451 BUG_ON(upperpath.dentry != NULL);
452 upperpath.dentry = temp;
453
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200454 err = ovl_copy_up_data(&c->lowerpath, &upperpath, c->stat.size);
Amir Goldstein02209d12017-05-19 15:16:21 +0300455 if (err)
456 return err;
457 }
458
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200459 err = ovl_copy_xattr(c->lowerpath.dentry, temp);
Amir Goldstein02209d12017-05-19 15:16:21 +0300460 if (err)
461 return err;
462
463 inode_lock(temp->d_inode);
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200464 err = ovl_set_attr(temp, &c->stat);
Amir Goldstein02209d12017-05-19 15:16:21 +0300465 inode_unlock(temp->d_inode);
466 if (err)
467 return err;
468
469 /*
470 * Store identifier of lower inode in upper inode xattr to
471 * allow lookup of the copy up origin inode.
472 *
473 * Don't set origin when we are breaking the association with a lower
474 * hard link.
475 */
Amir Goldstein59be0972017-06-20 15:25:46 +0300476 if (c->origin) {
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200477 err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
Amir Goldstein02209d12017-05-19 15:16:21 +0300478 if (err)
479 return err;
480 }
481
482 return 0;
483}
484
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200485static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200486{
Amir Goldstein59be0972017-06-20 15:25:46 +0300487 struct inode *udir = c->destdir->d_inode;
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300488 struct inode *inode;
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200489 struct dentry *newdentry = NULL;
490 struct dentry *temp = NULL;
491 int err;
492
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200493 err = ovl_get_tmpfile(c, &temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200494 if (err)
Miklos Szeredie85f82f2017-06-28 13:41:22 +0200495 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200496
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200497 err = ovl_copy_up_inode(c, temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200498 if (err)
499 goto out_cleanup;
500
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200501 if (c->tmpfile) {
Amir Goldstein15932c42017-05-16 01:26:49 +0300502 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200503 err = ovl_install_temp(c, temp, &newdentry);
Amir Goldstein15932c42017-05-16 01:26:49 +0300504 inode_unlock(udir);
505 } else {
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200506 err = ovl_install_temp(c, temp, &newdentry);
Miklos Szeredie85f82f2017-06-28 13:41:22 +0200507 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200508 if (err)
509 goto out_cleanup;
510
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300511 inode = d_inode(c->dentry);
512 ovl_inode_update(inode, newdentry);
513 if (S_ISDIR(inode->i_mode))
514 ovl_set_flag(OVL_WHITEOUTS, inode);
515
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200516out:
Miklos Szeredie85f82f2017-06-28 13:41:22 +0200517 dput(temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200518 return err;
519
520out_cleanup:
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200521 if (!c->tmpfile)
522 ovl_cleanup(d_inode(c->workdir), temp);
Miklos Szeredie85f82f2017-06-28 13:41:22 +0200523 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200524}
525
526/*
527 * Copy up a single dentry
528 *
Miklos Szeredia6c60652016-12-16 11:02:56 +0100529 * All renames start with copy up of source if necessary. The actual
530 * rename will only proceed once the copy up was successful. Copy up uses
531 * upper parent i_mutex for exclusion. Since rename can change d_parent it
532 * is possible that the copy up will lock the old parent. At that point
533 * the file will have already been copied up anyway.
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200534 */
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200535static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200536{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200537 int err;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200538 struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
Amir Goldstein59be0972017-06-20 15:25:46 +0300539 bool indexed = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200540
Amir Goldstein59be0972017-06-20 15:25:46 +0300541 if (ovl_indexdir(c->dentry->d_sb) && !S_ISDIR(c->stat.mode) &&
542 c->stat.nlink > 1)
543 indexed = true;
544
545 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || indexed)
546 c->origin = true;
547
548 if (indexed) {
549 c->destdir = ovl_indexdir(c->dentry->d_sb);
550 err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
551 if (err)
552 return err;
553 } else {
554 /*
555 * Mark parent "impure" because it may now contain non-pure
556 * upper
557 */
558 err = ovl_set_impure(c->parent, c->destdir);
559 if (err)
560 return err;
561 }
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300562
Amir Goldstein01ad3eb2017-01-17 06:34:57 +0200563 /* Should we copyup with O_TMPFILE or with workdir? */
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200564 if (S_ISREG(c->stat.mode) && ofs->tmpfile) {
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200565 c->tmpfile = true;
Amir Goldstein59be0972017-06-20 15:25:46 +0300566 err = ovl_copy_up_locked(c);
567 } else {
Amir Goldstein5820dc02017-09-25 16:39:55 +0300568 err = ovl_lock_rename_workdir(c->workdir, c->destdir);
569 if (!err) {
Amir Goldstein59be0972017-06-20 15:25:46 +0300570 err = ovl_copy_up_locked(c);
571 unlock_rename(c->workdir, c->destdir);
572 }
Amir Goldstein01ad3eb2017-01-17 06:34:57 +0200573 }
574
Amir Goldstein59be0972017-06-20 15:25:46 +0300575 if (indexed) {
576 if (!err)
577 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
578 kfree(c->destname.name);
579 } else if (!err) {
580 struct inode *udir = d_inode(c->destdir);
581
582 /* Restore timestamps on parent (best effort) */
583 inode_lock(udir);
584 ovl_set_timestamps(c->destdir, &c->pstat);
585 inode_unlock(udir);
586
587 ovl_dentry_set_upper_alias(c->dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200588 }
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200589
590 return err;
591}
592
593static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
594 int flags)
595{
596 int err;
597 DEFINE_DELAYED_CALL(done);
598 struct path parentpath;
599 struct ovl_copy_up_ctx ctx = {
600 .parent = parent,
601 .dentry = dentry,
602 .workdir = ovl_workdir(dentry),
603 };
604
605 if (WARN_ON(!ctx.workdir))
606 return -EROFS;
607
608 ovl_path_lower(dentry, &ctx.lowerpath);
609 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
610 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
611 if (err)
612 return err;
613
614 ovl_path_upper(parent, &parentpath);
Amir Goldstein59be0972017-06-20 15:25:46 +0300615 ctx.destdir = parentpath.dentry;
616 ctx.destname = dentry->d_name;
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200617
618 err = vfs_getattr(&parentpath, &ctx.pstat,
619 STATX_ATIME | STATX_MTIME, AT_STATX_SYNC_AS_STAT);
620 if (err)
621 return err;
622
623 /* maybe truncate regular file. this has no effect on dirs */
624 if (flags & O_TRUNC)
625 ctx.stat.size = 0;
626
627 if (S_ISLNK(ctx.stat.mode)) {
628 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
629 if (IS_ERR(ctx.link))
630 return PTR_ERR(ctx.link);
631 }
632 ovl_do_check_copy_up(ctx.lowerpath.dentry);
633
Miklos Szeredifd210b72017-07-04 22:03:18 +0200634 err = ovl_copy_up_start(dentry);
635 /* err < 0: interrupted, err > 0: raced with another copy-up */
636 if (unlikely(err)) {
637 if (err > 0)
638 err = 0;
639 } else {
Amir Goldstein59be0972017-06-20 15:25:46 +0300640 if (!ovl_dentry_upper(dentry))
641 err = ovl_do_copy_up(&ctx);
642 if (!err && !ovl_dentry_has_upper_alias(dentry))
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300643 err = ovl_link_up(&ctx);
Miklos Szeredifd210b72017-07-04 22:03:18 +0200644 ovl_copy_up_end(dentry);
645 }
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200646 do_delayed_call(&done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200647
648 return err;
649}
650
Amir Goldstein9aba6522016-11-12 21:36:03 +0200651int ovl_copy_up_flags(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200652{
Vivek Goyal8eac98b2016-09-06 13:40:32 -0400653 int err = 0;
654 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200655
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200656 while (!err) {
657 struct dentry *next;
658 struct dentry *parent;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200659
Amir Goldstein59be0972017-06-20 15:25:46 +0300660 /*
661 * Check if copy-up has happened as well as for upper alias (in
662 * case of hard links) is there.
663 *
664 * Both checks are lockless:
665 * - false negatives: will recheck under oi->lock
666 * - false positives:
667 * + ovl_dentry_upper() uses memory barriers to ensure the
668 * upper dentry is up-to-date
669 * + ovl_dentry_has_upper_alias() relies on locking of
670 * upper parent i_rwsem to prevent reordering copy-up
671 * with rename.
672 */
673 if (ovl_dentry_upper(dentry) &&
674 ovl_dentry_has_upper_alias(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200675 break;
676
677 next = dget(dentry);
678 /* find the topmost dentry not yet copied up */
679 for (;;) {
680 parent = dget_parent(next);
681
Amir Goldstein59be0972017-06-20 15:25:46 +0300682 if (ovl_dentry_upper(parent))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200683 break;
684
685 dput(next);
686 next = parent;
687 }
688
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200689 err = ovl_copy_up_one(parent, next, flags);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200690
691 dput(parent);
692 dput(next);
693 }
Vivek Goyal8eac98b2016-09-06 13:40:32 -0400694 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200695
696 return err;
697}
Amir Goldstein9aba6522016-11-12 21:36:03 +0200698
699int ovl_copy_up(struct dentry *dentry)
700{
701 return ovl_copy_up_flags(dentry, 0);
702}