blob: 9d8ebf0e72375af8acf3786678e470f8c786f594 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002/*
3 *
4 * Copyright (C) 2011 Novell Inc.
Miklos Szeredie9be9d52014-10-24 00:14:38 +02005 */
6
David Howellsfb5bb2c32015-07-07 15:04:44 +01007#include <linux/module.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +02008#include <linux/fs.h>
9#include <linux/slab.h>
10#include <linux/file.h>
Amir Goldstein72db8212021-06-19 12:26:18 +030011#include <linux/fileattr.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020012#include <linux/splice.h>
13#include <linux/xattr.h>
14#include <linux/security.h>
15#include <linux/uaccess.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010016#include <linux/sched/signal.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010017#include <linux/cred.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020018#include <linux/namei.h>
David Howellsfb5bb2c32015-07-07 15:04:44 +010019#include <linux/fdtable.h>
20#include <linux/ratelimit.h>
Amir Goldstein3a1e8192017-03-30 15:22:16 +030021#include <linux/exportfs.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020022#include "overlayfs.h"
23
24#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
25
Miklos Szeredi670c2322018-07-18 15:44:44 +020026static int ovl_ccup_set(const char *buf, const struct kernel_param *param)
David Howellsfb5bb2c32015-07-07 15:04:44 +010027{
lijiazi1bd0a3a2019-12-16 19:12:32 +080028 pr_warn("\"check_copy_up\" module option is obsolete\n");
David Howellsfb5bb2c32015-07-07 15:04:44 +010029 return 0;
30}
31
Miklos Szeredi670c2322018-07-18 15:44:44 +020032static int ovl_ccup_get(char *buf, const struct kernel_param *param)
David Howellsfb5bb2c32015-07-07 15:04:44 +010033{
Miklos Szeredi670c2322018-07-18 15:44:44 +020034 return sprintf(buf, "N\n");
David Howellsfb5bb2c32015-07-07 15:04:44 +010035}
36
Miklos Szeredi670c2322018-07-18 15:44:44 +020037module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644);
Nicolas Schier253e7482019-06-17 09:39:00 +020038MODULE_PARM_DESC(check_copy_up, "Obsolete; does nothing");
Miklos Szeredi670c2322018-07-18 15:44:44 +020039
Miklos Szeredic61ca552020-03-17 15:04:22 +010040static bool ovl_must_copy_xattr(const char *name)
41{
42 return !strcmp(name, XATTR_POSIX_ACL_ACCESS) ||
43 !strcmp(name, XATTR_POSIX_ACL_DEFAULT) ||
44 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN);
45}
46
Miklos Szeredi610afc02020-09-02 10:58:49 +020047int ovl_copy_xattr(struct super_block *sb, struct dentry *old,
48 struct dentry *new)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020049{
Vito Caputoe4ad29f2015-10-24 07:19:46 -050050 ssize_t list_size, size, value_size = 0;
51 char *buf, *name, *value = NULL;
Yuxuan Shui520da692020-05-27 04:08:02 +010052 int error = 0;
Miklos Szeredi8b326c62016-09-16 14:12:11 +020053 size_t slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020054
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +020055 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
56 !(new->d_inode->i_opflags & IOP_XATTR))
Miklos Szeredie9be9d52014-10-24 00:14:38 +020057 return 0;
58
59 list_size = vfs_listxattr(old, NULL, 0);
60 if (list_size <= 0) {
61 if (list_size == -EOPNOTSUPP)
62 return 0;
63 return list_size;
64 }
65
66 buf = kzalloc(list_size, GFP_KERNEL);
67 if (!buf)
68 return -ENOMEM;
69
Miklos Szeredie9be9d52014-10-24 00:14:38 +020070 list_size = vfs_listxattr(old, buf, list_size);
71 if (list_size <= 0) {
72 error = list_size;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050073 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020074 }
75
Miklos Szeredi8b326c62016-09-16 14:12:11 +020076 for (name = buf; list_size; name += slen) {
77 slen = strnlen(name, list_size) + 1;
78
79 /* underlying fs providing us with an broken xattr list? */
80 if (WARN_ON(slen > list_size)) {
81 error = -EIO;
82 break;
83 }
84 list_size -= slen;
85
Miklos Szeredi610afc02020-09-02 10:58:49 +020086 if (ovl_is_private_xattr(sb, name))
Miklos Szeredi09562542016-08-08 15:08:49 +020087 continue;
Amir Goldstein03fedf92020-12-19 12:16:08 +020088
89 error = security_inode_copy_up_xattr(name);
90 if (error < 0 && error != -EOPNOTSUPP)
91 break;
92 if (error == 1) {
93 error = 0;
94 continue; /* Discard */
95 }
Vito Caputoe4ad29f2015-10-24 07:19:46 -050096retry:
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +010097 size = vfs_getxattr(&init_user_ns, old, name, value, value_size);
Vito Caputoe4ad29f2015-10-24 07:19:46 -050098 if (size == -ERANGE)
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +010099 size = vfs_getxattr(&init_user_ns, old, name, NULL, 0);
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500100
Miklos Szeredi97daf8b2015-11-10 17:08:41 +0100101 if (size < 0) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200102 error = size;
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500103 break;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200104 }
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500105
106 if (size > value_size) {
107 void *new;
108
109 new = krealloc(value, size, GFP_KERNEL);
110 if (!new) {
111 error = -ENOMEM;
112 break;
113 }
114 value = new;
115 value_size = size;
116 goto retry;
117 }
118
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +0100119 error = vfs_setxattr(&init_user_ns, new, name, value, size, 0);
Miklos Szeredic61ca552020-03-17 15:04:22 +0100120 if (error) {
121 if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name))
122 break;
123
124 /* Ignore failure to copy unknown xattrs */
125 error = 0;
126 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200127 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200128 kfree(value);
129out:
130 kfree(buf);
131 return error;
132}
133
Amir Goldstein096a2182021-06-19 12:26:19 +0300134static int ovl_copy_fileattr(struct inode *inode, struct path *old,
135 struct path *new)
Amir Goldstein72db8212021-06-19 12:26:18 +0300136{
137 struct fileattr oldfa = { .flags_valid = true };
138 struct fileattr newfa = { .flags_valid = true };
139 int err;
140
141 err = ovl_real_fileattr_get(old, &oldfa);
142 if (err)
143 return err;
144
145 err = ovl_real_fileattr_get(new, &newfa);
146 if (err)
147 return err;
148
Amir Goldstein096a2182021-06-19 12:26:19 +0300149 /*
150 * We cannot set immutable and append-only flags on upper inode,
151 * because we would not be able to link upper inode to upper dir
152 * not set overlay private xattr on upper inode.
153 * Store these flags in overlay.protattr xattr instead.
154 */
155 if (oldfa.flags & OVL_PROT_FS_FLAGS_MASK) {
156 err = ovl_set_protattr(inode, new->dentry, &oldfa);
157 if (err)
158 return err;
159 }
160
Amir Goldstein72db8212021-06-19 12:26:18 +0300161 BUILD_BUG_ON(OVL_COPY_FS_FLAGS_MASK & ~FS_COMMON_FL);
162 newfa.flags &= ~OVL_COPY_FS_FLAGS_MASK;
163 newfa.flags |= (oldfa.flags & OVL_COPY_FS_FLAGS_MASK);
164
165 BUILD_BUG_ON(OVL_COPY_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON);
166 newfa.fsx_xflags &= ~OVL_COPY_FSX_FLAGS_MASK;
167 newfa.fsx_xflags |= (oldfa.fsx_xflags & OVL_COPY_FSX_FLAGS_MASK);
168
169 return ovl_real_fileattr_set(new, &newfa);
170}
171
Vivek Goyalc86243b02020-08-31 14:15:29 -0400172static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old,
173 struct path *new, loff_t len)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200174{
175 struct file *old_file;
176 struct file *new_file;
177 loff_t old_pos = 0;
178 loff_t new_pos = 0;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100179 loff_t cloned;
Chengguang Xub504c652019-11-01 20:35:51 +0800180 loff_t data_pos = -1;
181 loff_t hole_len;
182 bool skip_hole = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200183 int error = 0;
184
185 if (len == 0)
186 return 0;
187
David Howells04803342015-09-18 11:45:12 +0100188 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200189 if (IS_ERR(old_file))
190 return PTR_ERR(old_file);
191
David Howells04803342015-09-18 11:45:12 +0100192 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200193 if (IS_ERR(new_file)) {
194 error = PTR_ERR(new_file);
195 goto out_fput;
196 }
197
Amir Goldstein2ea98462016-09-23 11:38:12 +0300198 /* Try to use clone_file_range to clone up within the same fs */
Darrick J. Wong452ce652018-10-30 10:41:56 +1100199 cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100200 if (cloned == len)
Amir Goldstein2ea98462016-09-23 11:38:12 +0300201 goto out;
202 /* Couldn't clone, so now we try to copy the data */
Amir Goldstein2ea98462016-09-23 11:38:12 +0300203
Chengguang Xub504c652019-11-01 20:35:51 +0800204 /* Check if lower fs supports seek operation */
205 if (old_file->f_mode & FMODE_LSEEK &&
206 old_file->f_op->llseek)
207 skip_hole = true;
208
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200209 while (len) {
210 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
211 long bytes;
212
213 if (len < this_len)
214 this_len = len;
215
216 if (signal_pending_state(TASK_KILLABLE, current)) {
217 error = -EINTR;
218 break;
219 }
220
Chengguang Xub504c652019-11-01 20:35:51 +0800221 /*
222 * Fill zero for hole will cost unnecessary disk space
223 * and meanwhile slow down the copy-up speed, so we do
224 * an optimization for hole during copy-up, it relies
225 * on SEEK_DATA implementation in lower fs so if lower
226 * fs does not support it, copy-up will behave as before.
227 *
228 * Detail logic of hole detection as below:
229 * When we detect next data position is larger than current
230 * position we will skip that hole, otherwise we copy
231 * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually,
232 * it may not recognize all kind of holes and sometimes
233 * only skips partial of hole area. However, it will be
234 * enough for most of the use cases.
235 */
236
237 if (skip_hole && data_pos < old_pos) {
238 data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
239 if (data_pos > old_pos) {
240 hole_len = data_pos - old_pos;
241 len -= hole_len;
242 old_pos = new_pos = data_pos;
243 continue;
244 } else if (data_pos == -ENXIO) {
245 break;
246 } else if (data_pos < 0) {
247 skip_hole = false;
248 }
249 }
250
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200251 bytes = do_splice_direct(old_file, &old_pos,
252 new_file, &new_pos,
253 this_len, SPLICE_F_MOVE);
254 if (bytes <= 0) {
255 error = bytes;
256 break;
257 }
258 WARN_ON(old_pos != new_pos);
259
260 len -= bytes;
261 }
Amir Goldstein2ea98462016-09-23 11:38:12 +0300262out:
Vivek Goyalc86243b02020-08-31 14:15:29 -0400263 if (!error && ovl_should_sync(ofs))
Miklos Szeredi641089c2016-10-31 14:42:14 +0100264 error = vfs_fsync(new_file, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200265 fput(new_file);
266out_fput:
267 fput(old_file);
268 return error;
269}
270
Vivek Goyal0c288872018-05-11 11:49:28 -0400271static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
272{
273 struct iattr attr = {
274 .ia_valid = ATTR_SIZE,
275 .ia_size = stat->size,
276 };
277
Christian Brauner2f221d62021-01-21 14:19:26 +0100278 return notify_change(&init_user_ns, upperdentry, &attr, NULL);
Vivek Goyal0c288872018-05-11 11:49:28 -0400279}
280
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200281static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
282{
283 struct iattr attr = {
284 .ia_valid =
285 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
286 .ia_atime = stat->atime,
287 .ia_mtime = stat->mtime,
288 };
289
Christian Brauner2f221d62021-01-21 14:19:26 +0100290 return notify_change(&init_user_ns, upperdentry, &attr, NULL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200291}
292
293int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
294{
295 int err = 0;
296
297 if (!S_ISLNK(stat->mode)) {
298 struct iattr attr = {
299 .ia_valid = ATTR_MODE,
300 .ia_mode = stat->mode,
301 };
Christian Brauner2f221d62021-01-21 14:19:26 +0100302 err = notify_change(&init_user_ns, upperdentry, &attr, NULL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200303 }
304 if (!err) {
305 struct iattr attr = {
306 .ia_valid = ATTR_UID | ATTR_GID,
307 .ia_uid = stat->uid,
308 .ia_gid = stat->gid,
309 };
Christian Brauner2f221d62021-01-21 14:19:26 +0100310 err = notify_change(&init_user_ns, upperdentry, &attr, NULL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200311 }
312 if (!err)
313 ovl_set_timestamps(upperdentry, stat);
314
315 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200316}
317
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300318struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real,
319 bool is_upper)
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300320{
321 struct ovl_fh *fh;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200322 int fh_type, dwords;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300323 int buflen = MAX_HANDLE_SZ;
Amir Goldstein05122442018-01-11 08:25:32 +0200324 uuid_t *uuid = &real->d_sb->s_uuid;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200325 int err;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300326
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200327 /* Make sure the real fid stays 32bit aligned */
328 BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4);
329 BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255);
330
331 fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL);
332 if (!fh)
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300333 return ERR_PTR(-ENOMEM);
334
335 /*
336 * We encode a non-connectable file handle for non-dir, because we
337 * only need to find the lower inode number and we don't want to pay
338 * the price or reconnecting the dentry.
339 */
340 dwords = buflen >> 2;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200341 fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300342 buflen = (dwords << 2);
343
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200344 err = -EIO;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300345 if (WARN_ON(fh_type < 0) ||
346 WARN_ON(buflen > MAX_HANDLE_SZ) ||
347 WARN_ON(fh_type == FILEID_INVALID))
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200348 goto out_err;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300349
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200350 fh->fb.version = OVL_FH_VERSION;
351 fh->fb.magic = OVL_FH_MAGIC;
352 fh->fb.type = fh_type;
353 fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN;
Amir Goldstein54fb3472017-06-21 15:28:38 +0300354 /*
355 * When we will want to decode an overlay dentry from this handle
356 * and all layers are on the same fs, if we get a disconncted real
357 * dentry when we decode fid, the only way to tell if we should assign
358 * it to upperdentry or to lowerstack is by checking this flag.
359 */
360 if (is_upper)
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200361 fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200362 fh->fb.len = sizeof(fh->fb) + buflen;
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +0300363 if (ofs->config.uuid)
364 fh->fb.uuid = *uuid;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300365
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300366 return fh;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200367
368out_err:
369 kfree(fh);
370 return ERR_PTR(err);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300371}
372
Amir Goldsteina0c236b2021-06-19 12:26:17 +0300373int ovl_set_origin(struct ovl_fs *ofs, struct dentry *lower,
374 struct dentry *upper)
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300375{
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300376 const struct ovl_fh *fh = NULL;
377 int err;
378
379 /*
380 * When lower layer doesn't support export operations store a 'null' fh,
381 * so we can use the overlay.origin xattr to distignuish between a copy
382 * up and a pure upper inode.
383 */
Amir Goldstein02bcd152017-06-21 15:28:36 +0300384 if (ovl_can_decode_fh(lower->d_sb)) {
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300385 fh = ovl_encode_real_fh(ofs, lower, false);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300386 if (IS_ERR(fh))
387 return PTR_ERR(fh);
388 }
389
Miklos Szeredi6266d462017-05-18 16:11:24 +0200390 /*
391 * Do not fail when upper doesn't support xattrs.
392 */
Amir Goldsteina0c236b2021-06-19 12:26:17 +0300393 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200394 fh ? fh->fb.len : 0, 0);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300395 kfree(fh);
396
Miklos Szeredi6939f972020-12-14 15:26:14 +0100397 /* Ignore -EPERM from setting "user.*" on symlink/special */
398 return err == -EPERM ? 0 : err;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300399}
400
Amir Goldstein016b7202018-01-11 14:01:08 +0200401/* Store file handle of @upper dir in @index dir entry */
Miklos Szeredi610afc02020-09-02 10:58:49 +0200402static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper,
403 struct dentry *index)
Amir Goldstein016b7202018-01-11 14:01:08 +0200404{
405 const struct ovl_fh *fh;
406 int err;
407
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300408 fh = ovl_encode_real_fh(ofs, upper, true);
Amir Goldstein016b7202018-01-11 14:01:08 +0200409 if (IS_ERR(fh))
410 return PTR_ERR(fh);
411
Miklos Szeredi610afc02020-09-02 10:58:49 +0200412 err = ovl_do_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len);
Amir Goldstein016b7202018-01-11 14:01:08 +0200413
414 kfree(fh);
415 return err;
416}
417
418/*
419 * Create and install index entry.
420 *
421 * Caller must hold i_mutex on indexdir.
422 */
423static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
424 struct dentry *upper)
425{
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300426 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
Amir Goldstein016b7202018-01-11 14:01:08 +0200427 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
428 struct inode *dir = d_inode(indexdir);
429 struct dentry *index = NULL;
430 struct dentry *temp = NULL;
431 struct qstr name = { };
432 int err;
433
434 /*
435 * For now this is only used for creating index entry for directories,
436 * because non-dir are copied up directly to index and then hardlinked
437 * to upper dir.
438 *
439 * TODO: implement create index for non-dir, so we can call it when
440 * encoding file handle for non-dir in case index does not exist.
441 */
442 if (WARN_ON(!d_is_dir(dentry)))
443 return -EIO;
444
445 /* Directory not expected to be indexed before copy up */
446 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
447 return -EIO;
448
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300449 err = ovl_get_index_name(ofs, origin, &name);
Amir Goldstein016b7202018-01-11 14:01:08 +0200450 if (err)
451 return err;
452
Amir Goldstein137ec522018-05-16 17:51:25 +0300453 temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
Miklos Szeredib148cba2018-05-31 11:06:11 +0200454 err = PTR_ERR(temp);
Amir Goldstein016b7202018-01-11 14:01:08 +0200455 if (IS_ERR(temp))
Miklos Szeredib148cba2018-05-31 11:06:11 +0200456 goto free_name;
Amir Goldstein016b7202018-01-11 14:01:08 +0200457
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300458 err = ovl_set_upper_fh(ofs, upper, temp);
Amir Goldstein016b7202018-01-11 14:01:08 +0200459 if (err)
Miklos Szeredib148cba2018-05-31 11:06:11 +0200460 goto out;
Amir Goldstein016b7202018-01-11 14:01:08 +0200461
462 index = lookup_one_len(name.name, indexdir, name.len);
463 if (IS_ERR(index)) {
464 err = PTR_ERR(index);
465 } else {
466 err = ovl_do_rename(dir, temp, dir, index, 0);
467 dput(index);
468 }
Amir Goldstein016b7202018-01-11 14:01:08 +0200469out:
Miklos Szeredib148cba2018-05-31 11:06:11 +0200470 if (err)
471 ovl_cleanup(dir, temp);
Amir Goldstein016b7202018-01-11 14:01:08 +0200472 dput(temp);
Miklos Szeredib148cba2018-05-31 11:06:11 +0200473free_name:
Amir Goldstein016b7202018-01-11 14:01:08 +0200474 kfree(name.name);
475 return err;
Amir Goldstein016b7202018-01-11 14:01:08 +0200476}
477
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200478struct ovl_copy_up_ctx {
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200479 struct dentry *parent;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200480 struct dentry *dentry;
481 struct path lowerpath;
482 struct kstat stat;
483 struct kstat pstat;
484 const char *link;
Amir Goldstein59be0972017-06-20 15:25:46 +0300485 struct dentry *destdir;
486 struct qstr destname;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200487 struct dentry *workdir;
Amir Goldstein59be0972017-06-20 15:25:46 +0300488 bool origin;
Amir Goldstein016b7202018-01-11 14:01:08 +0200489 bool indexed;
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400490 bool metacopy;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200491};
492
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300493static int ovl_link_up(struct ovl_copy_up_ctx *c)
494{
495 int err;
496 struct dentry *upper;
497 struct dentry *upperdir = ovl_dentry_upper(c->parent);
498 struct inode *udir = d_inode(upperdir);
499
500 /* Mark parent "impure" because it may now contain non-pure upper */
501 err = ovl_set_impure(c->parent, upperdir);
502 if (err)
503 return err;
504
505 err = ovl_set_nlink_lower(c->dentry);
506 if (err)
507 return err;
508
509 inode_lock_nested(udir, I_MUTEX_PARENT);
510 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
511 c->dentry->d_name.len);
512 err = PTR_ERR(upper);
513 if (!IS_ERR(upper)) {
Amir Goldstein6cf00762018-05-16 17:04:00 +0300514 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300515 dput(upper);
516
517 if (!err) {
518 /* Restore timestamps on parent (best effort) */
519 ovl_set_timestamps(upperdir, &c->pstat);
520 ovl_dentry_set_upper_alias(c->dentry);
521 }
522 }
523 inode_unlock(udir);
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300524 if (err)
525 return err;
526
527 err = ovl_set_nlink_upper(c->dentry);
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300528
529 return err;
530}
531
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200532static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
Amir Goldstein02209d12017-05-19 15:16:21 +0300533{
Vivek Goyalc86243b02020-08-31 14:15:29 -0400534 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
Amir Goldstein72db8212021-06-19 12:26:18 +0300535 struct inode *inode = d_inode(c->dentry);
536 struct path upperpath, datapath;
Amir Goldstein02209d12017-05-19 15:16:21 +0300537 int err;
538
Amir Goldstein72db8212021-06-19 12:26:18 +0300539 ovl_path_upper(c->dentry, &upperpath);
540 if (WARN_ON(upperpath.dentry != NULL))
541 return -EIO;
542
543 upperpath.dentry = temp;
544
Vivek Goyal5f328792019-01-11 19:37:00 +0100545 /*
546 * Copy up data first and then xattrs. Writing data after
547 * xattrs will remove security.capability xattr automatically.
548 */
549 if (S_ISREG(c->stat.mode) && !c->metacopy) {
Vivek Goyal5f328792019-01-11 19:37:00 +0100550 ovl_path_lowerdata(c->dentry, &datapath);
Vivek Goyalc86243b02020-08-31 14:15:29 -0400551 err = ovl_copy_up_data(ofs, &datapath, &upperpath,
552 c->stat.size);
Vivek Goyal5f328792019-01-11 19:37:00 +0100553 if (err)
554 return err;
555 }
556
Miklos Szeredi610afc02020-09-02 10:58:49 +0200557 err = ovl_copy_xattr(c->dentry->d_sb, c->lowerpath.dentry, temp);
Amir Goldstein02209d12017-05-19 15:16:21 +0300558 if (err)
559 return err;
560
Amir Goldstein72db8212021-06-19 12:26:18 +0300561 if (inode->i_flags & OVL_COPY_I_FLAGS_MASK) {
562 /*
563 * Copy the fileattr inode flags that are the source of already
564 * copied i_flags
565 */
Amir Goldstein096a2182021-06-19 12:26:19 +0300566 err = ovl_copy_fileattr(inode, &c->lowerpath, &upperpath);
Amir Goldstein72db8212021-06-19 12:26:18 +0300567 if (err)
568 return err;
569 }
570
Amir Goldstein02209d12017-05-19 15:16:21 +0300571 /*
572 * Store identifier of lower inode in upper inode xattr to
573 * allow lookup of the copy up origin inode.
574 *
575 * Don't set origin when we are breaking the association with a lower
576 * hard link.
577 */
Amir Goldstein59be0972017-06-20 15:25:46 +0300578 if (c->origin) {
Amir Goldsteina0c236b2021-06-19 12:26:17 +0300579 err = ovl_set_origin(ofs, c->lowerpath.dentry, temp);
Amir Goldstein02209d12017-05-19 15:16:21 +0300580 if (err)
581 return err;
582 }
583
Vivek Goyal0c288872018-05-11 11:49:28 -0400584 if (c->metacopy) {
Amir Goldsteina0c236b2021-06-19 12:26:17 +0300585 err = ovl_check_setxattr(ofs, temp, OVL_XATTR_METACOPY,
Vivek Goyal0c288872018-05-11 11:49:28 -0400586 NULL, 0, -EOPNOTSUPP);
587 if (err)
588 return err;
589 }
590
Vivek Goyalbd64e572018-05-11 11:49:27 -0400591 inode_lock(temp->d_inode);
Chengguang Xub504c652019-11-01 20:35:51 +0800592 if (S_ISREG(c->stat.mode))
Vivek Goyal0c288872018-05-11 11:49:28 -0400593 err = ovl_set_size(temp, &c->stat);
594 if (!err)
595 err = ovl_set_attr(temp, &c->stat);
Vivek Goyalbd64e572018-05-11 11:49:27 -0400596 inode_unlock(temp->d_inode);
597
598 return err;
Amir Goldstein02209d12017-05-19 15:16:21 +0300599}
600
Miklos Szeredi6b522432018-10-26 23:34:39 +0200601struct ovl_cu_creds {
602 const struct cred *old;
603 struct cred *new;
604};
605
606static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200607{
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300608 int err;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300609
Miklos Szeredi6b522432018-10-26 23:34:39 +0200610 cc->old = cc->new = NULL;
611 err = security_inode_copy_up(dentry, &cc->new);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300612 if (err < 0)
Miklos Szeredi6b522432018-10-26 23:34:39 +0200613 return err;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300614
Miklos Szeredi6b522432018-10-26 23:34:39 +0200615 if (cc->new)
616 cc->old = override_creds(cc->new);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300617
Miklos Szeredi6b522432018-10-26 23:34:39 +0200618 return 0;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300619}
620
Miklos Szeredi6b522432018-10-26 23:34:39 +0200621static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300622{
Miklos Szeredi6b522432018-10-26 23:34:39 +0200623 if (cc->new) {
624 revert_creds(cc->old);
625 put_cred(cc->new);
626 }
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300627}
628
629/*
630 * Copyup using workdir to prepare temp file. Used when copying up directories,
631 * special files or when upper fs doesn't support O_TMPFILE.
632 */
633static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
634{
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300635 struct inode *inode;
Miklos Szeredi6b522432018-10-26 23:34:39 +0200636 struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
637 struct dentry *temp, *upper;
638 struct ovl_cu_creds cc;
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200639 int err;
Miklos Szeredi6b522432018-10-26 23:34:39 +0200640 struct ovl_cattr cattr = {
641 /* Can't properly set mode on creation because of the umask */
642 .mode = c->stat.mode & S_IFMT,
643 .rdev = c->stat.rdev,
644 .link = c->link
645 };
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200646
Amir Goldstein773cb4c2020-04-03 08:43:12 +0300647 /* workdir and destdir could be the same when copying up to indexdir */
648 err = -EIO;
649 if (lock_rename(c->workdir, c->destdir) != NULL)
650 goto unlock;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300651
Miklos Szeredi6b522432018-10-26 23:34:39 +0200652 err = ovl_prep_cu_creds(c->dentry, &cc);
653 if (err)
654 goto unlock;
655
656 temp = ovl_create_temp(c->workdir, &cattr);
657 ovl_revert_cu_creds(&cc);
658
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300659 err = PTR_ERR(temp);
660 if (IS_ERR(temp))
661 goto unlock;
662
663 err = ovl_copy_up_inode(c, temp);
664 if (err)
665 goto cleanup;
666
667 if (S_ISDIR(c->stat.mode) && c->indexed) {
668 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
669 if (err)
670 goto cleanup;
671 }
672
Miklos Szeredi6b522432018-10-26 23:34:39 +0200673 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
674 err = PTR_ERR(upper);
675 if (IS_ERR(upper))
676 goto cleanup;
677
678 err = ovl_do_rename(wdir, temp, udir, upper, 0);
679 dput(upper);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300680 if (err)
681 goto cleanup;
682
683 if (!c->metacopy)
684 ovl_set_upperdata(d_inode(c->dentry));
685 inode = d_inode(c->dentry);
Miklos Szeredi6b522432018-10-26 23:34:39 +0200686 ovl_inode_update(inode, temp);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300687 if (S_ISDIR(inode->i_mode))
688 ovl_set_flag(OVL_WHITEOUTS, inode);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300689unlock:
690 unlock_rename(c->workdir, c->destdir);
691
692 return err;
693
694cleanup:
Miklos Szeredi6b522432018-10-26 23:34:39 +0200695 ovl_cleanup(wdir, temp);
696 dput(temp);
697 goto unlock;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300698}
699
700/* Copyup using O_TMPFILE which does not require cross dir locking */
701static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
702{
Miklos Szeredi6b522432018-10-26 23:34:39 +0200703 struct inode *udir = d_inode(c->destdir);
704 struct dentry *temp, *upper;
705 struct ovl_cu_creds cc;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300706 int err;
707
Miklos Szeredi6b522432018-10-26 23:34:39 +0200708 err = ovl_prep_cu_creds(c->dentry, &cc);
709 if (err)
710 return err;
711
712 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
713 ovl_revert_cu_creds(&cc);
714
Miklos Szeredib148cba2018-05-31 11:06:11 +0200715 if (IS_ERR(temp))
716 return PTR_ERR(temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200717
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200718 err = ovl_copy_up_inode(c, temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200719 if (err)
Miklos Szeredi6b522432018-10-26 23:34:39 +0200720 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200721
Miklos Szeredi6b522432018-10-26 23:34:39 +0200722 inode_lock_nested(udir, I_MUTEX_PARENT);
723
724 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
725 err = PTR_ERR(upper);
726 if (!IS_ERR(upper)) {
727 err = ovl_do_link(temp, udir, upper);
728 dput(upper);
729 }
730 inode_unlock(udir);
731
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200732 if (err)
Miklos Szeredi6b522432018-10-26 23:34:39 +0200733 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200734
Vivek Goyal0c288872018-05-11 11:49:28 -0400735 if (!c->metacopy)
736 ovl_set_upperdata(d_inode(c->dentry));
Miklos Szeredi6b522432018-10-26 23:34:39 +0200737 ovl_inode_update(d_inode(c->dentry), temp);
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300738
Miklos Szeredi6b522432018-10-26 23:34:39 +0200739 return 0;
740
741out_dput:
Miklos Szeredie85f82f2017-06-28 13:41:22 +0200742 dput(temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200743 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200744}
745
746/*
747 * Copy up a single dentry
748 *
Miklos Szeredia6c60652016-12-16 11:02:56 +0100749 * All renames start with copy up of source if necessary. The actual
750 * rename will only proceed once the copy up was successful. Copy up uses
751 * upper parent i_mutex for exclusion. Since rename can change d_parent it
752 * is possible that the copy up will lock the old parent. At that point
753 * the file will have already been copied up anyway.
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200754 */
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200755static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200756{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200757 int err;
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300758 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
Amir Goldstein016b7202018-01-11 14:01:08 +0200759 bool to_index = false;
Amir Goldstein59be0972017-06-20 15:25:46 +0300760
Amir Goldstein016b7202018-01-11 14:01:08 +0200761 /*
762 * Indexed non-dir is copied up directly to the index entry and then
763 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
764 * then index entry is created and then copied up dir installed.
765 * Copying dir up to indexdir instead of workdir simplifies locking.
766 */
767 if (ovl_need_index(c->dentry)) {
768 c->indexed = true;
769 if (S_ISDIR(c->stat.mode))
770 c->workdir = ovl_indexdir(c->dentry->d_sb);
771 else
772 to_index = true;
773 }
774
775 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
Amir Goldstein59be0972017-06-20 15:25:46 +0300776 c->origin = true;
777
Amir Goldstein016b7202018-01-11 14:01:08 +0200778 if (to_index) {
Amir Goldstein59be0972017-06-20 15:25:46 +0300779 c->destdir = ovl_indexdir(c->dentry->d_sb);
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300780 err = ovl_get_index_name(ofs, c->lowerpath.dentry, &c->destname);
Amir Goldstein59be0972017-06-20 15:25:46 +0300781 if (err)
782 return err;
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300783 } else if (WARN_ON(!c->parent)) {
784 /* Disconnected dentry must be copied up to index dir */
785 return -EIO;
Amir Goldstein59be0972017-06-20 15:25:46 +0300786 } else {
787 /*
788 * Mark parent "impure" because it may now contain non-pure
789 * upper
790 */
791 err = ovl_set_impure(c->parent, c->destdir);
792 if (err)
793 return err;
794 }
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300795
Amir Goldstein01ad3eb2017-01-17 06:34:57 +0200796 /* Should we copyup with O_TMPFILE or with workdir? */
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300797 if (S_ISREG(c->stat.mode) && ofs->tmpfile)
798 err = ovl_copy_up_tmpfile(c);
799 else
800 err = ovl_copy_up_workdir(c);
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300801 if (err)
802 goto out;
803
804 if (c->indexed)
Amir Goldstein016b7202018-01-11 14:01:08 +0200805 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
806
807 if (to_index) {
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300808 /* Initialize nlink for copy up of disconnected dentry */
809 err = ovl_set_nlink_upper(c->dentry);
810 } else {
Amir Goldstein59be0972017-06-20 15:25:46 +0300811 struct inode *udir = d_inode(c->destdir);
812
813 /* Restore timestamps on parent (best effort) */
814 inode_lock(udir);
815 ovl_set_timestamps(c->destdir, &c->pstat);
816 inode_unlock(udir);
817
818 ovl_dentry_set_upper_alias(c->dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200819 }
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200820
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300821out:
822 if (to_index)
823 kfree(c->destname.name);
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200824 return err;
825}
826
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400827static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
828 int flags)
829{
830 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
831
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400832 if (!ofs->config.metacopy)
833 return false;
834
835 if (!S_ISREG(mode))
836 return false;
837
838 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
839 return false;
840
841 return true;
842}
843
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200844static ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value)
Miklos Szeredifee0f292020-09-02 10:58:48 +0200845{
846 ssize_t res;
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200847 char *buf;
Miklos Szeredifee0f292020-09-02 10:58:48 +0200848
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +0100849 res = vfs_getxattr(&init_user_ns, dentry, name, NULL, 0);
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200850 if (res == -ENODATA || res == -EOPNOTSUPP)
851 res = 0;
Miklos Szeredifee0f292020-09-02 10:58:48 +0200852
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200853 if (res > 0) {
854 buf = kzalloc(res, GFP_KERNEL);
Miklos Szeredifee0f292020-09-02 10:58:48 +0200855 if (!buf)
856 return -ENOMEM;
857
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +0100858 res = vfs_getxattr(&init_user_ns, dentry, name, buf, res);
Miklos Szeredifee0f292020-09-02 10:58:48 +0200859 if (res < 0)
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200860 kfree(buf);
861 else
862 *value = buf;
Miklos Szeredifee0f292020-09-02 10:58:48 +0200863 }
Miklos Szeredifee0f292020-09-02 10:58:48 +0200864 return res;
865}
866
Vivek Goyal0c288872018-05-11 11:49:28 -0400867/* Copy up data of an inode which was copied up metadata only in the past. */
868static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
869{
Vivek Goyalc86243b02020-08-31 14:15:29 -0400870 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
Vivek Goyal4f93b422018-05-11 11:49:30 -0400871 struct path upperpath, datapath;
Vivek Goyal0c288872018-05-11 11:49:28 -0400872 int err;
Vivek Goyal993a0b22019-01-30 14:01:57 -0500873 char *capability = NULL;
Kees Cook3f649ab2020-06-03 13:09:38 -0700874 ssize_t cap_size;
Vivek Goyal0c288872018-05-11 11:49:28 -0400875
876 ovl_path_upper(c->dentry, &upperpath);
877 if (WARN_ON(upperpath.dentry == NULL))
878 return -EIO;
879
Vivek Goyal4f93b422018-05-11 11:49:30 -0400880 ovl_path_lowerdata(c->dentry, &datapath);
881 if (WARN_ON(datapath.dentry == NULL))
882 return -EIO;
883
Vivek Goyal993a0b22019-01-30 14:01:57 -0500884 if (c->stat.size) {
885 err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS,
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200886 &capability);
887 if (cap_size < 0)
Vivek Goyal993a0b22019-01-30 14:01:57 -0500888 goto out;
889 }
890
Vivek Goyalc86243b02020-08-31 14:15:29 -0400891 err = ovl_copy_up_data(ofs, &datapath, &upperpath, c->stat.size);
Vivek Goyal0c288872018-05-11 11:49:28 -0400892 if (err)
Vivek Goyal993a0b22019-01-30 14:01:57 -0500893 goto out_free;
894
895 /*
896 * Writing to upper file will clear security.capability xattr. We
897 * don't want that to happen for normal copy-up operation.
898 */
899 if (capability) {
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +0100900 err = vfs_setxattr(&init_user_ns, upperpath.dentry,
901 XATTR_NAME_CAPS, capability, cap_size, 0);
Vivek Goyal993a0b22019-01-30 14:01:57 -0500902 if (err)
903 goto out_free;
904 }
905
Vivek Goyal0c288872018-05-11 11:49:28 -0400906
Miklos Szeredi610afc02020-09-02 10:58:49 +0200907 err = ovl_do_removexattr(ofs, upperpath.dentry, OVL_XATTR_METACOPY);
Vivek Goyal0c288872018-05-11 11:49:28 -0400908 if (err)
Vivek Goyal993a0b22019-01-30 14:01:57 -0500909 goto out_free;
Vivek Goyal0c288872018-05-11 11:49:28 -0400910
911 ovl_set_upperdata(d_inode(c->dentry));
Vivek Goyal993a0b22019-01-30 14:01:57 -0500912out_free:
913 kfree(capability);
914out:
Vivek Goyal0c288872018-05-11 11:49:28 -0400915 return err;
916}
917
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200918static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
919 int flags)
920{
921 int err;
922 DEFINE_DELAYED_CALL(done);
923 struct path parentpath;
924 struct ovl_copy_up_ctx ctx = {
925 .parent = parent,
926 .dentry = dentry,
927 .workdir = ovl_workdir(dentry),
928 };
929
930 if (WARN_ON(!ctx.workdir))
931 return -EROFS;
932
933 ovl_path_lower(dentry, &ctx.lowerpath);
934 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
935 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
936 if (err)
937 return err;
938
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400939 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
940
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300941 if (parent) {
942 ovl_path_upper(parent, &parentpath);
943 ctx.destdir = parentpath.dentry;
944 ctx.destname = dentry->d_name;
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200945
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300946 err = vfs_getattr(&parentpath, &ctx.pstat,
947 STATX_ATIME | STATX_MTIME,
948 AT_STATX_SYNC_AS_STAT);
949 if (err)
950 return err;
951 }
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200952
953 /* maybe truncate regular file. this has no effect on dirs */
954 if (flags & O_TRUNC)
955 ctx.stat.size = 0;
956
957 if (S_ISLNK(ctx.stat.mode)) {
958 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
959 if (IS_ERR(ctx.link))
960 return PTR_ERR(ctx.link);
961 }
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200962
Vivek Goyal0c288872018-05-11 11:49:28 -0400963 err = ovl_copy_up_start(dentry, flags);
Miklos Szeredifd210b72017-07-04 22:03:18 +0200964 /* err < 0: interrupted, err > 0: raced with another copy-up */
965 if (unlikely(err)) {
966 if (err > 0)
967 err = 0;
968 } else {
Amir Goldstein59be0972017-06-20 15:25:46 +0300969 if (!ovl_dentry_upper(dentry))
970 err = ovl_do_copy_up(&ctx);
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300971 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300972 err = ovl_link_up(&ctx);
Vivek Goyal0c288872018-05-11 11:49:28 -0400973 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
974 err = ovl_copy_up_meta_inode_data(&ctx);
Miklos Szeredifd210b72017-07-04 22:03:18 +0200975 ovl_copy_up_end(dentry);
976 }
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200977 do_delayed_call(&done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200978
979 return err;
980}
981
youngjun5ac8e802020-06-21 07:30:59 -0700982static int ovl_copy_up_flags(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200983{
Vivek Goyal8eac98b2016-09-06 13:40:32 -0400984 int err = 0;
Dan Carpenter7b279bb2021-03-23 16:19:35 +0300985 const struct cred *old_cred;
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300986 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
987
988 /*
989 * With NFS export, copy up can get called for a disconnected non-dir.
990 * In this case, we will copy up lower inode to index dir without
991 * linking it to upper dir.
992 */
993 if (WARN_ON(disconnected && d_is_dir(dentry)))
994 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200995
Dan Carpenter7b279bb2021-03-23 16:19:35 +0300996 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200997 while (!err) {
998 struct dentry *next;
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300999 struct dentry *parent = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001000
Vivek Goyal0c288872018-05-11 11:49:28 -04001001 if (ovl_already_copied_up(dentry, flags))
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001002 break;
1003
1004 next = dget(dentry);
1005 /* find the topmost dentry not yet copied up */
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +03001006 for (; !disconnected;) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001007 parent = dget_parent(next);
1008
Amir Goldstein59be0972017-06-20 15:25:46 +03001009 if (ovl_dentry_upper(parent))
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001010 break;
1011
1012 dput(next);
1013 next = parent;
1014 }
1015
Miklos Szeredia6fb2352017-07-04 22:03:18 +02001016 err = ovl_copy_up_one(parent, next, flags);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001017
1018 dput(parent);
1019 dput(next);
1020 }
Vivek Goyal8eac98b2016-09-06 13:40:32 -04001021 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001022
1023 return err;
1024}
Amir Goldstein9aba6522016-11-12 21:36:03 +02001025
Vivek Goyald6eac032018-05-11 11:49:27 -04001026static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
1027{
1028 /* Copy up of disconnected dentry does not set upper alias */
Vivek Goyal0c288872018-05-11 11:49:28 -04001029 if (ovl_already_copied_up(dentry, flags))
Vivek Goyald6eac032018-05-11 11:49:27 -04001030 return false;
1031
1032 if (special_file(d_inode(dentry)->i_mode))
1033 return false;
1034
Vivek Goyal0c288872018-05-11 11:49:28 -04001035 if (!ovl_open_flags_need_copy_up(flags))
Vivek Goyald6eac032018-05-11 11:49:27 -04001036 return false;
1037
1038 return true;
1039}
1040
Amir Goldstein34280302019-01-22 07:01:39 +02001041int ovl_maybe_copy_up(struct dentry *dentry, int flags)
Vivek Goyald6eac032018-05-11 11:49:27 -04001042{
1043 int err = 0;
1044
Amir Goldstein34280302019-01-22 07:01:39 +02001045 if (ovl_open_need_copy_up(dentry, flags)) {
Vivek Goyald6eac032018-05-11 11:49:27 -04001046 err = ovl_want_write(dentry);
1047 if (!err) {
Amir Goldstein34280302019-01-22 07:01:39 +02001048 err = ovl_copy_up_flags(dentry, flags);
Vivek Goyald6eac032018-05-11 11:49:27 -04001049 ovl_drop_write(dentry);
1050 }
1051 }
1052
1053 return err;
1054}
1055
Vivek Goyald1e6f6a2018-05-11 11:49:33 -04001056int ovl_copy_up_with_data(struct dentry *dentry)
1057{
1058 return ovl_copy_up_flags(dentry, O_WRONLY);
1059}
1060
Amir Goldstein9aba6522016-11-12 21:36:03 +02001061int ovl_copy_up(struct dentry *dentry)
1062{
1063 return ovl_copy_up_flags(dentry, 0);
1064}