blob: 9008ab9fbd2ebe89d419c249455eb740c48a9eb1 [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"
Amir Goldsteind8514d82017-01-17 06:34:55 +020025#include "ovl_entry.h"
Miklos Szeredie9be9d52014-10-24 00:14:38 +020026
27#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
28
David Howellsfb5bb2c32015-07-07 15:04:44 +010029static bool __read_mostly ovl_check_copy_up;
30module_param_named(check_copy_up, ovl_check_copy_up, bool,
31 S_IWUSR | S_IRUGO);
32MODULE_PARM_DESC(ovl_check_copy_up,
33 "Warn on copy-up when causing process also has a R/O fd open");
34
35static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
36{
37 const struct dentry *dentry = data;
38
Al Viro45063092016-12-04 18:24:56 -050039 if (file_inode(f) == d_inode(dentry))
David Howellsfb5bb2c32015-07-07 15:04:44 +010040 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",
41 f, fd, current->pid, current->comm);
42 return 0;
43}
44
45/*
46 * Check the fds open by this process and warn if something like the following
47 * scenario is about to occur:
48 *
49 * fd1 = open("foo", O_RDONLY);
50 * fd2 = open("foo", O_RDWR);
51 */
52static void ovl_do_check_copy_up(struct dentry *dentry)
53{
54 if (ovl_check_copy_up)
55 iterate_fd(current->files, 0, ovl_check_fd, dentry);
56}
57
Miklos Szeredie9be9d52014-10-24 00:14:38 +020058int ovl_copy_xattr(struct dentry *old, struct dentry *new)
59{
Vito Caputoe4ad29f2015-10-24 07:19:46 -050060 ssize_t list_size, size, value_size = 0;
61 char *buf, *name, *value = NULL;
62 int uninitialized_var(error);
Miklos Szeredi8b326c62016-09-16 14:12:11 +020063 size_t slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020064
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +020065 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
66 !(new->d_inode->i_opflags & IOP_XATTR))
Miklos Szeredie9be9d52014-10-24 00:14:38 +020067 return 0;
68
69 list_size = vfs_listxattr(old, NULL, 0);
70 if (list_size <= 0) {
71 if (list_size == -EOPNOTSUPP)
72 return 0;
73 return list_size;
74 }
75
76 buf = kzalloc(list_size, GFP_KERNEL);
77 if (!buf)
78 return -ENOMEM;
79
Miklos Szeredie9be9d52014-10-24 00:14:38 +020080 list_size = vfs_listxattr(old, buf, list_size);
81 if (list_size <= 0) {
82 error = list_size;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050083 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020084 }
85
Miklos Szeredi8b326c62016-09-16 14:12:11 +020086 for (name = buf; list_size; name += slen) {
87 slen = strnlen(name, list_size) + 1;
88
89 /* underlying fs providing us with an broken xattr list? */
90 if (WARN_ON(slen > list_size)) {
91 error = -EIO;
92 break;
93 }
94 list_size -= slen;
95
Miklos Szeredi09562542016-08-08 15:08:49 +020096 if (ovl_is_private_xattr(name))
97 continue;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050098retry:
99 size = vfs_getxattr(old, name, value, value_size);
100 if (size == -ERANGE)
101 size = vfs_getxattr(old, name, NULL, 0);
102
Miklos Szeredi97daf8b2015-11-10 17:08:41 +0100103 if (size < 0) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200104 error = size;
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500105 break;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200106 }
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500107
108 if (size > value_size) {
109 void *new;
110
111 new = krealloc(value, size, GFP_KERNEL);
112 if (!new) {
113 error = -ENOMEM;
114 break;
115 }
116 value = new;
117 value_size = size;
118 goto retry;
119 }
120
Vivek Goyal121ab822016-07-13 10:44:49 -0400121 error = security_inode_copy_up_xattr(name);
122 if (error < 0 && error != -EOPNOTSUPP)
123 break;
124 if (error == 1) {
125 error = 0;
126 continue; /* Discard */
127 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200128 error = vfs_setxattr(new, name, value, size, 0);
129 if (error)
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500130 break;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200131 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200132 kfree(value);
133out:
134 kfree(buf);
135 return error;
136}
137
138static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
139{
140 struct file *old_file;
141 struct file *new_file;
142 loff_t old_pos = 0;
143 loff_t new_pos = 0;
144 int error = 0;
145
146 if (len == 0)
147 return 0;
148
David Howells04803342015-09-18 11:45:12 +0100149 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200150 if (IS_ERR(old_file))
151 return PTR_ERR(old_file);
152
David Howells04803342015-09-18 11:45:12 +0100153 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200154 if (IS_ERR(new_file)) {
155 error = PTR_ERR(new_file);
156 goto out_fput;
157 }
158
Amir Goldstein2ea98462016-09-23 11:38:12 +0300159 /* Try to use clone_file_range to clone up within the same fs */
160 error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
161 if (!error)
162 goto out;
163 /* Couldn't clone, so now we try to copy the data */
164 error = 0;
165
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200166 /* FIXME: copy up sparse files efficiently */
167 while (len) {
168 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
169 long bytes;
170
171 if (len < this_len)
172 this_len = len;
173
174 if (signal_pending_state(TASK_KILLABLE, current)) {
175 error = -EINTR;
176 break;
177 }
178
179 bytes = do_splice_direct(old_file, &old_pos,
180 new_file, &new_pos,
181 this_len, SPLICE_F_MOVE);
182 if (bytes <= 0) {
183 error = bytes;
184 break;
185 }
186 WARN_ON(old_pos != new_pos);
187
188 len -= bytes;
189 }
Amir Goldstein2ea98462016-09-23 11:38:12 +0300190out:
Miklos Szeredi641089c2016-10-31 14:42:14 +0100191 if (!error)
192 error = vfs_fsync(new_file, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200193 fput(new_file);
194out_fput:
195 fput(old_file);
196 return error;
197}
198
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200199static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
200{
201 struct iattr attr = {
202 .ia_valid =
203 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
204 .ia_atime = stat->atime,
205 .ia_mtime = stat->mtime,
206 };
207
208 return notify_change(upperdentry, &attr, NULL);
209}
210
211int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
212{
213 int err = 0;
214
215 if (!S_ISLNK(stat->mode)) {
216 struct iattr attr = {
217 .ia_valid = ATTR_MODE,
218 .ia_mode = stat->mode,
219 };
220 err = notify_change(upperdentry, &attr, NULL);
221 }
222 if (!err) {
223 struct iattr attr = {
224 .ia_valid = ATTR_UID | ATTR_GID,
225 .ia_uid = stat->uid,
226 .ia_gid = stat->gid,
227 };
228 err = notify_change(upperdentry, &attr, NULL);
229 }
230 if (!err)
231 ovl_set_timestamps(upperdentry, stat);
232
233 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200234}
235
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300236static struct ovl_fh *ovl_encode_fh(struct dentry *lower, uuid_be *uuid)
237{
238 struct ovl_fh *fh;
239 int fh_type, fh_len, dwords;
240 void *buf;
241 int buflen = MAX_HANDLE_SZ;
242
243 buf = kmalloc(buflen, GFP_TEMPORARY);
244 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;
274 fh->len = fh_len;
275 fh->uuid = *uuid;
276 memcpy(fh->fid, buf, buflen);
277
278out:
279 kfree(buf);
280 return fh;
281}
282
283static int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
284 struct dentry *upper)
285{
286 struct super_block *sb = lower->d_sb;
287 uuid_be *uuid = (uuid_be *) &sb->s_uuid;
288 const struct ovl_fh *fh = NULL;
289 int err;
290
291 /*
292 * When lower layer doesn't support export operations store a 'null' fh,
293 * so we can use the overlay.origin xattr to distignuish between a copy
294 * up and a pure upper inode.
295 */
296 if (sb->s_export_op && sb->s_export_op->fh_to_dentry &&
297 uuid_be_cmp(*uuid, NULL_UUID_BE)) {
298 fh = ovl_encode_fh(lower, uuid);
299 if (IS_ERR(fh))
300 return PTR_ERR(fh);
301 }
302
303 err = ovl_do_setxattr(upper, OVL_XATTR_ORIGIN, fh, fh ? fh->len : 0, 0);
304 kfree(fh);
305
306 return err;
307}
308
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200309static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
310 struct dentry *dentry, struct path *lowerpath,
Amir Goldstein42f269b2017-01-17 06:34:54 +0200311 struct kstat *stat, const char *link,
Amir Goldsteind8514d82017-01-17 06:34:55 +0200312 struct kstat *pstat, bool tmpfile)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200313{
314 struct inode *wdir = workdir->d_inode;
315 struct inode *udir = upperdir->d_inode;
316 struct dentry *newdentry = NULL;
317 struct dentry *upper = NULL;
Amir Goldstein42f269b2017-01-17 06:34:54 +0200318 struct dentry *temp = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200319 int err;
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400320 const struct cred *old_creds = NULL;
321 struct cred *new_creds = NULL;
Al Viro32a3d842016-12-04 17:33:17 +0000322 struct cattr cattr = {
323 /* Can't properly set mode on creation because of the umask */
324 .mode = stat->mode & S_IFMT,
325 .rdev = stat->rdev,
326 .link = link
327 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200328
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200329 upper = lookup_one_len(dentry->d_name.name, upperdir,
330 dentry->d_name.len);
331 err = PTR_ERR(upper);
332 if (IS_ERR(upper))
Amir Goldstein42f269b2017-01-17 06:34:54 +0200333 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200334
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400335 err = security_inode_copy_up(dentry, &new_creds);
336 if (err < 0)
Amir Goldstein42f269b2017-01-17 06:34:54 +0200337 goto out1;
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400338
339 if (new_creds)
340 old_creds = override_creds(new_creds);
341
Amir Goldsteind8514d82017-01-17 06:34:55 +0200342 if (tmpfile)
343 temp = ovl_do_tmpfile(upperdir, stat->mode);
344 else
345 temp = ovl_lookup_temp(workdir, dentry);
Amir Goldstein42f269b2017-01-17 06:34:54 +0200346 err = PTR_ERR(temp);
347 if (IS_ERR(temp))
348 goto out1;
349
Amir Goldsteind8514d82017-01-17 06:34:55 +0200350 err = 0;
351 if (!tmpfile)
352 err = ovl_create_real(wdir, temp, &cattr, NULL, true);
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400353
354 if (new_creds) {
355 revert_creds(old_creds);
356 put_cred(new_creds);
357 }
358
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200359 if (err)
360 goto out2;
361
362 if (S_ISREG(stat->mode)) {
363 struct path upperpath;
Sohom Bhattacharjeef134f242016-03-15 20:57:59 +0530364
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200365 ovl_path_upper(dentry, &upperpath);
366 BUG_ON(upperpath.dentry != NULL);
Amir Goldstein42f269b2017-01-17 06:34:54 +0200367 upperpath.dentry = temp;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200368
Amir Goldstein01ad3eb2017-01-17 06:34:57 +0200369 if (tmpfile) {
370 inode_unlock(udir);
371 err = ovl_copy_up_data(lowerpath, &upperpath,
372 stat->size);
373 inode_lock_nested(udir, I_MUTEX_PARENT);
374 } else {
375 err = ovl_copy_up_data(lowerpath, &upperpath,
376 stat->size);
377 }
378
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200379 if (err)
380 goto out_cleanup;
381 }
382
Amir Goldstein42f269b2017-01-17 06:34:54 +0200383 err = ovl_copy_xattr(lowerpath->dentry, temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200384 if (err)
385 goto out_cleanup;
386
Amir Goldstein42f269b2017-01-17 06:34:54 +0200387 inode_lock(temp->d_inode);
388 err = ovl_set_attr(temp, stat);
389 inode_unlock(temp->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200390 if (err)
391 goto out_cleanup;
392
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300393 /*
394 * Store identifier of lower inode in upper inode xattr to
395 * allow lookup of the copy up origin inode.
396 */
397 err = ovl_set_origin(dentry, lowerpath->dentry, temp);
398 if (err)
399 goto out_cleanup;
400
Amir Goldsteind8514d82017-01-17 06:34:55 +0200401 if (tmpfile)
402 err = ovl_do_link(temp, udir, upper, true);
403 else
404 err = ovl_do_rename(wdir, temp, udir, upper, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200405 if (err)
406 goto out_cleanup;
407
Amir Goldsteind8514d82017-01-17 06:34:55 +0200408 newdentry = dget(tmpfile ? upper : temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200409 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200410 ovl_inode_update(d_inode(dentry), d_inode(newdentry));
Amir Goldstein42f269b2017-01-17 06:34:54 +0200411
412 /* Restore timestamps on parent (best effort) */
413 ovl_set_timestamps(upperdir, pstat);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200414out2:
Amir Goldstein42f269b2017-01-17 06:34:54 +0200415 dput(temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200416out1:
Amir Goldstein42f269b2017-01-17 06:34:54 +0200417 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200418out:
419 return err;
420
421out_cleanup:
Amir Goldsteind8514d82017-01-17 06:34:55 +0200422 if (!tmpfile)
423 ovl_cleanup(wdir, temp);
David Howellsab79efa2015-09-18 11:45:22 +0100424 goto out2;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200425}
426
427/*
428 * Copy up a single dentry
429 *
Miklos Szeredia6c60652016-12-16 11:02:56 +0100430 * All renames start with copy up of source if necessary. The actual
431 * rename will only proceed once the copy up was successful. Copy up uses
432 * upper parent i_mutex for exclusion. Since rename can change d_parent it
433 * is possible that the copy up will lock the old parent. At that point
434 * the file will have already been copied up anyway.
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200435 */
Amir Goldstein9aba6522016-11-12 21:36:03 +0200436static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
437 struct path *lowerpath, struct kstat *stat)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200438{
Miklos Szeredi77642352016-10-04 14:40:45 +0200439 DEFINE_DELAYED_CALL(done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200440 struct dentry *workdir = ovl_workdir(dentry);
441 int err;
442 struct kstat pstat;
443 struct path parentpath;
Miklos Szeredi77642352016-10-04 14:40:45 +0200444 struct dentry *lowerdentry = lowerpath->dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200445 struct dentry *upperdir;
Miklos Szeredi77642352016-10-04 14:40:45 +0200446 const char *link = NULL;
Amir Goldsteind8514d82017-01-17 06:34:55 +0200447 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200448
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200449 if (WARN_ON(!workdir))
450 return -EROFS;
451
Miklos Szeredi77642352016-10-04 14:40:45 +0200452 ovl_do_check_copy_up(lowerdentry);
David Howellsfb5bb2c32015-07-07 15:04:44 +0100453
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200454 ovl_path_upper(parent, &parentpath);
455 upperdir = parentpath.dentry;
456
David Howellsa528d352017-01-31 16:46:22 +0000457 err = vfs_getattr(&parentpath, &pstat,
458 STATX_ATIME | STATX_MTIME, AT_STATX_SYNC_AS_STAT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200459 if (err)
460 return err;
461
462 if (S_ISLNK(stat->mode)) {
Miklos Szeredi77642352016-10-04 14:40:45 +0200463 link = vfs_get_link(lowerdentry, &done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200464 if (IS_ERR(link))
465 return PTR_ERR(link);
466 }
467
Amir Goldstein01ad3eb2017-01-17 06:34:57 +0200468 /* Should we copyup with O_TMPFILE or with workdir? */
469 if (S_ISREG(stat->mode) && ofs->tmpfile) {
470 err = ovl_copy_up_start(dentry);
471 /* err < 0: interrupted, err > 0: raced with another copy-up */
472 if (unlikely(err)) {
473 pr_debug("ovl_copy_up_start(%pd2) = %i\n", dentry, err);
474 if (err > 0)
475 err = 0;
476 goto out_done;
477 }
478
479 inode_lock_nested(upperdir->d_inode, I_MUTEX_PARENT);
480 err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
481 stat, link, &pstat, true);
482 inode_unlock(upperdir->d_inode);
483 ovl_copy_up_end(dentry);
484 goto out_done;
485 }
486
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200487 err = -EIO;
488 if (lock_rename(workdir, upperdir) != NULL) {
489 pr_err("overlayfs: failed to lock workdir+upperdir\n");
490 goto out_unlock;
491 }
Miklos Szeredia6c60652016-12-16 11:02:56 +0100492 if (ovl_dentry_upper(dentry)) {
Al Viro0f7ff2d2015-12-06 12:31:07 -0500493 /* Raced with another copy-up? Nothing to do, then... */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200494 err = 0;
Al Viro0f7ff2d2015-12-06 12:31:07 -0500495 goto out_unlock;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200496 }
497
498 err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
Amir Goldstein01ad3eb2017-01-17 06:34:57 +0200499 stat, link, &pstat, false);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200500out_unlock:
501 unlock_rename(workdir, upperdir);
Amir Goldstein01ad3eb2017-01-17 06:34:57 +0200502out_done:
Miklos Szeredi77642352016-10-04 14:40:45 +0200503 do_delayed_call(&done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200504
505 return err;
506}
507
Amir Goldstein9aba6522016-11-12 21:36:03 +0200508int ovl_copy_up_flags(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200509{
Vivek Goyal8eac98b2016-09-06 13:40:32 -0400510 int err = 0;
511 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200512
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200513 while (!err) {
514 struct dentry *next;
515 struct dentry *parent;
516 struct path lowerpath;
517 struct kstat stat;
518 enum ovl_path_type type = ovl_path_type(dentry);
519
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100520 if (OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200521 break;
522
523 next = dget(dentry);
524 /* find the topmost dentry not yet copied up */
525 for (;;) {
526 parent = dget_parent(next);
527
528 type = ovl_path_type(parent);
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100529 if (OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200530 break;
531
532 dput(next);
533 next = parent;
534 }
535
536 ovl_path_lower(next, &lowerpath);
David Howellsa528d352017-01-31 16:46:22 +0000537 err = vfs_getattr(&lowerpath, &stat,
538 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Amir Goldstein9aba6522016-11-12 21:36:03 +0200539 /* maybe truncate regular file. this has no effect on dirs */
540 if (flags & O_TRUNC)
541 stat.size = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200542 if (!err)
Al Viro0f7ff2d2015-12-06 12:31:07 -0500543 err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200544
545 dput(parent);
546 dput(next);
547 }
Vivek Goyal8eac98b2016-09-06 13:40:32 -0400548 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200549
550 return err;
551}
Amir Goldstein9aba6522016-11-12 21:36:03 +0200552
553int ovl_copy_up(struct dentry *dentry)
554{
555 return ovl_copy_up_flags(dentry, 0);
556}