blob: b193d08a3dc367e23213d22eedd5dcbcf8a09d67 [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
Miklos Szeredif945ca12021-07-22 14:18:14 +020066 buf = kvzalloc(list_size, GFP_KERNEL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020067 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
Miklos Szeredif945ca12021-07-22 14:18:14 +0200109 new = kvmalloc(size, GFP_KERNEL);
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500110 if (!new) {
111 error = -ENOMEM;
112 break;
113 }
Miklos Szeredif945ca12021-07-22 14:18:14 +0200114 kvfree(value);
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500115 value = new;
116 value_size = size;
117 goto retry;
118 }
119
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +0100120 error = vfs_setxattr(&init_user_ns, new, name, value, size, 0);
Miklos Szeredic61ca552020-03-17 15:04:22 +0100121 if (error) {
122 if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name))
123 break;
124
125 /* Ignore failure to copy unknown xattrs */
126 error = 0;
127 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200128 }
Miklos Szeredif945ca12021-07-22 14:18:14 +0200129 kvfree(value);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200130out:
Miklos Szeredif945ca12021-07-22 14:18:14 +0200131 kvfree(buf);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200132 return error;
133}
134
Amir Goldstein096a2182021-06-19 12:26:19 +0300135static int ovl_copy_fileattr(struct inode *inode, struct path *old,
136 struct path *new)
Amir Goldstein72db8212021-06-19 12:26:18 +0300137{
138 struct fileattr oldfa = { .flags_valid = true };
139 struct fileattr newfa = { .flags_valid = true };
140 int err;
141
142 err = ovl_real_fileattr_get(old, &oldfa);
Miklos Szeredi5b0a4142021-11-04 14:04:52 +0100143 if (err) {
144 /* Ntfs-3g returns -EINVAL for "no fileattr support" */
145 if (err == -ENOTTY || err == -EINVAL)
146 return 0;
147 pr_warn("failed to retrieve lower fileattr (%pd2, err=%i)\n",
148 old, err);
Amir Goldstein72db8212021-06-19 12:26:18 +0300149 return err;
Miklos Szeredi5b0a4142021-11-04 14:04:52 +0100150 }
Amir Goldstein72db8212021-06-19 12:26:18 +0300151
Amir Goldstein096a2182021-06-19 12:26:19 +0300152 /*
153 * We cannot set immutable and append-only flags on upper inode,
154 * because we would not be able to link upper inode to upper dir
155 * not set overlay private xattr on upper inode.
156 * Store these flags in overlay.protattr xattr instead.
157 */
158 if (oldfa.flags & OVL_PROT_FS_FLAGS_MASK) {
159 err = ovl_set_protattr(inode, new->dentry, &oldfa);
160 if (err)
161 return err;
162 }
163
Miklos Szeredi5b0a4142021-11-04 14:04:52 +0100164 /* Don't bother copying flags if none are set */
165 if (!(oldfa.flags & OVL_COPY_FS_FLAGS_MASK))
166 return 0;
167
168 err = ovl_real_fileattr_get(new, &newfa);
169 if (err) {
170 pr_warn("failed to retrieve upper fileattr (%pd2, err=%i)\n",
171 new, err);
172 return err;
173 }
174
Amir Goldstein72db8212021-06-19 12:26:18 +0300175 BUILD_BUG_ON(OVL_COPY_FS_FLAGS_MASK & ~FS_COMMON_FL);
176 newfa.flags &= ~OVL_COPY_FS_FLAGS_MASK;
177 newfa.flags |= (oldfa.flags & OVL_COPY_FS_FLAGS_MASK);
178
179 BUILD_BUG_ON(OVL_COPY_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON);
180 newfa.fsx_xflags &= ~OVL_COPY_FSX_FLAGS_MASK;
181 newfa.fsx_xflags |= (oldfa.fsx_xflags & OVL_COPY_FSX_FLAGS_MASK);
182
183 return ovl_real_fileattr_set(new, &newfa);
184}
185
Vivek Goyalc86243b02020-08-31 14:15:29 -0400186static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old,
187 struct path *new, loff_t len)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200188{
189 struct file *old_file;
190 struct file *new_file;
191 loff_t old_pos = 0;
192 loff_t new_pos = 0;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100193 loff_t cloned;
Chengguang Xub504c652019-11-01 20:35:51 +0800194 loff_t data_pos = -1;
195 loff_t hole_len;
196 bool skip_hole = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200197 int error = 0;
198
199 if (len == 0)
200 return 0;
201
David Howells04803342015-09-18 11:45:12 +0100202 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200203 if (IS_ERR(old_file))
204 return PTR_ERR(old_file);
205
David Howells04803342015-09-18 11:45:12 +0100206 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200207 if (IS_ERR(new_file)) {
208 error = PTR_ERR(new_file);
209 goto out_fput;
210 }
211
Amir Goldstein2ea98462016-09-23 11:38:12 +0300212 /* Try to use clone_file_range to clone up within the same fs */
Darrick J. Wong452ce652018-10-30 10:41:56 +1100213 cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100214 if (cloned == len)
Amir Goldstein2ea98462016-09-23 11:38:12 +0300215 goto out;
216 /* Couldn't clone, so now we try to copy the data */
Amir Goldstein2ea98462016-09-23 11:38:12 +0300217
Chengguang Xub504c652019-11-01 20:35:51 +0800218 /* Check if lower fs supports seek operation */
219 if (old_file->f_mode & FMODE_LSEEK &&
220 old_file->f_op->llseek)
221 skip_hole = true;
222
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200223 while (len) {
224 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
225 long bytes;
226
227 if (len < this_len)
228 this_len = len;
229
230 if (signal_pending_state(TASK_KILLABLE, current)) {
231 error = -EINTR;
232 break;
233 }
234
Chengguang Xub504c652019-11-01 20:35:51 +0800235 /*
236 * Fill zero for hole will cost unnecessary disk space
237 * and meanwhile slow down the copy-up speed, so we do
238 * an optimization for hole during copy-up, it relies
239 * on SEEK_DATA implementation in lower fs so if lower
240 * fs does not support it, copy-up will behave as before.
241 *
242 * Detail logic of hole detection as below:
243 * When we detect next data position is larger than current
244 * position we will skip that hole, otherwise we copy
245 * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually,
246 * it may not recognize all kind of holes and sometimes
247 * only skips partial of hole area. However, it will be
248 * enough for most of the use cases.
249 */
250
251 if (skip_hole && data_pos < old_pos) {
252 data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
253 if (data_pos > old_pos) {
254 hole_len = data_pos - old_pos;
255 len -= hole_len;
256 old_pos = new_pos = data_pos;
257 continue;
258 } else if (data_pos == -ENXIO) {
259 break;
260 } else if (data_pos < 0) {
261 skip_hole = false;
262 }
263 }
264
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200265 bytes = do_splice_direct(old_file, &old_pos,
266 new_file, &new_pos,
267 this_len, SPLICE_F_MOVE);
268 if (bytes <= 0) {
269 error = bytes;
270 break;
271 }
272 WARN_ON(old_pos != new_pos);
273
274 len -= bytes;
275 }
Amir Goldstein2ea98462016-09-23 11:38:12 +0300276out:
Vivek Goyalc86243b02020-08-31 14:15:29 -0400277 if (!error && ovl_should_sync(ofs))
Miklos Szeredi641089c2016-10-31 14:42:14 +0100278 error = vfs_fsync(new_file, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200279 fput(new_file);
280out_fput:
281 fput(old_file);
282 return error;
283}
284
Vivek Goyal0c288872018-05-11 11:49:28 -0400285static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
286{
287 struct iattr attr = {
288 .ia_valid = ATTR_SIZE,
289 .ia_size = stat->size,
290 };
291
Christian Brauner2f221d62021-01-21 14:19:26 +0100292 return notify_change(&init_user_ns, upperdentry, &attr, NULL);
Vivek Goyal0c288872018-05-11 11:49:28 -0400293}
294
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200295static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
296{
297 struct iattr attr = {
298 .ia_valid =
299 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
300 .ia_atime = stat->atime,
301 .ia_mtime = stat->mtime,
302 };
303
Christian Brauner2f221d62021-01-21 14:19:26 +0100304 return notify_change(&init_user_ns, upperdentry, &attr, NULL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200305}
306
307int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
308{
309 int err = 0;
310
311 if (!S_ISLNK(stat->mode)) {
312 struct iattr attr = {
313 .ia_valid = ATTR_MODE,
314 .ia_mode = stat->mode,
315 };
Christian Brauner2f221d62021-01-21 14:19:26 +0100316 err = notify_change(&init_user_ns, upperdentry, &attr, NULL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200317 }
318 if (!err) {
319 struct iattr attr = {
320 .ia_valid = ATTR_UID | ATTR_GID,
321 .ia_uid = stat->uid,
322 .ia_gid = stat->gid,
323 };
Christian Brauner2f221d62021-01-21 14:19:26 +0100324 err = notify_change(&init_user_ns, upperdentry, &attr, NULL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200325 }
326 if (!err)
327 ovl_set_timestamps(upperdentry, stat);
328
329 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200330}
331
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300332struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real,
333 bool is_upper)
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300334{
335 struct ovl_fh *fh;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200336 int fh_type, dwords;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300337 int buflen = MAX_HANDLE_SZ;
Amir Goldstein05122442018-01-11 08:25:32 +0200338 uuid_t *uuid = &real->d_sb->s_uuid;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200339 int err;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300340
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200341 /* Make sure the real fid stays 32bit aligned */
342 BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4);
343 BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255);
344
345 fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL);
346 if (!fh)
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300347 return ERR_PTR(-ENOMEM);
348
349 /*
350 * We encode a non-connectable file handle for non-dir, because we
351 * only need to find the lower inode number and we don't want to pay
352 * the price or reconnecting the dentry.
353 */
354 dwords = buflen >> 2;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200355 fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300356 buflen = (dwords << 2);
357
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200358 err = -EIO;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300359 if (WARN_ON(fh_type < 0) ||
360 WARN_ON(buflen > MAX_HANDLE_SZ) ||
361 WARN_ON(fh_type == FILEID_INVALID))
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200362 goto out_err;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300363
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200364 fh->fb.version = OVL_FH_VERSION;
365 fh->fb.magic = OVL_FH_MAGIC;
366 fh->fb.type = fh_type;
367 fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN;
Amir Goldstein54fb3472017-06-21 15:28:38 +0300368 /*
369 * When we will want to decode an overlay dentry from this handle
370 * and all layers are on the same fs, if we get a disconncted real
371 * dentry when we decode fid, the only way to tell if we should assign
372 * it to upperdentry or to lowerstack is by checking this flag.
373 */
374 if (is_upper)
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200375 fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200376 fh->fb.len = sizeof(fh->fb) + buflen;
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +0300377 if (ofs->config.uuid)
378 fh->fb.uuid = *uuid;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300379
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300380 return fh;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200381
382out_err:
383 kfree(fh);
384 return ERR_PTR(err);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300385}
386
Amir Goldsteina0c236b2021-06-19 12:26:17 +0300387int ovl_set_origin(struct ovl_fs *ofs, struct dentry *lower,
388 struct dentry *upper)
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300389{
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300390 const struct ovl_fh *fh = NULL;
391 int err;
392
393 /*
394 * When lower layer doesn't support export operations store a 'null' fh,
395 * so we can use the overlay.origin xattr to distignuish between a copy
396 * up and a pure upper inode.
397 */
Amir Goldstein02bcd152017-06-21 15:28:36 +0300398 if (ovl_can_decode_fh(lower->d_sb)) {
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300399 fh = ovl_encode_real_fh(ofs, lower, false);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300400 if (IS_ERR(fh))
401 return PTR_ERR(fh);
402 }
403
Miklos Szeredi6266d462017-05-18 16:11:24 +0200404 /*
405 * Do not fail when upper doesn't support xattrs.
406 */
Amir Goldsteina0c236b2021-06-19 12:26:17 +0300407 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200408 fh ? fh->fb.len : 0, 0);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300409 kfree(fh);
410
Miklos Szeredi6939f972020-12-14 15:26:14 +0100411 /* Ignore -EPERM from setting "user.*" on symlink/special */
412 return err == -EPERM ? 0 : err;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300413}
414
Amir Goldstein016b7202018-01-11 14:01:08 +0200415/* Store file handle of @upper dir in @index dir entry */
Miklos Szeredi610afc02020-09-02 10:58:49 +0200416static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper,
417 struct dentry *index)
Amir Goldstein016b7202018-01-11 14:01:08 +0200418{
419 const struct ovl_fh *fh;
420 int err;
421
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300422 fh = ovl_encode_real_fh(ofs, upper, true);
Amir Goldstein016b7202018-01-11 14:01:08 +0200423 if (IS_ERR(fh))
424 return PTR_ERR(fh);
425
Miklos Szeredi610afc02020-09-02 10:58:49 +0200426 err = ovl_do_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len);
Amir Goldstein016b7202018-01-11 14:01:08 +0200427
428 kfree(fh);
429 return err;
430}
431
432/*
433 * Create and install index entry.
434 *
435 * Caller must hold i_mutex on indexdir.
436 */
437static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
438 struct dentry *upper)
439{
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300440 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
Amir Goldstein016b7202018-01-11 14:01:08 +0200441 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
442 struct inode *dir = d_inode(indexdir);
443 struct dentry *index = NULL;
444 struct dentry *temp = NULL;
445 struct qstr name = { };
446 int err;
447
448 /*
449 * For now this is only used for creating index entry for directories,
450 * because non-dir are copied up directly to index and then hardlinked
451 * to upper dir.
452 *
453 * TODO: implement create index for non-dir, so we can call it when
454 * encoding file handle for non-dir in case index does not exist.
455 */
456 if (WARN_ON(!d_is_dir(dentry)))
457 return -EIO;
458
459 /* Directory not expected to be indexed before copy up */
460 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
461 return -EIO;
462
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300463 err = ovl_get_index_name(ofs, origin, &name);
Amir Goldstein016b7202018-01-11 14:01:08 +0200464 if (err)
465 return err;
466
Amir Goldstein137ec522018-05-16 17:51:25 +0300467 temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
Miklos Szeredib148cba2018-05-31 11:06:11 +0200468 err = PTR_ERR(temp);
Amir Goldstein016b7202018-01-11 14:01:08 +0200469 if (IS_ERR(temp))
Miklos Szeredib148cba2018-05-31 11:06:11 +0200470 goto free_name;
Amir Goldstein016b7202018-01-11 14:01:08 +0200471
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300472 err = ovl_set_upper_fh(ofs, upper, temp);
Amir Goldstein016b7202018-01-11 14:01:08 +0200473 if (err)
Miklos Szeredib148cba2018-05-31 11:06:11 +0200474 goto out;
Amir Goldstein016b7202018-01-11 14:01:08 +0200475
476 index = lookup_one_len(name.name, indexdir, name.len);
477 if (IS_ERR(index)) {
478 err = PTR_ERR(index);
479 } else {
480 err = ovl_do_rename(dir, temp, dir, index, 0);
481 dput(index);
482 }
Amir Goldstein016b7202018-01-11 14:01:08 +0200483out:
Miklos Szeredib148cba2018-05-31 11:06:11 +0200484 if (err)
485 ovl_cleanup(dir, temp);
Amir Goldstein016b7202018-01-11 14:01:08 +0200486 dput(temp);
Miklos Szeredib148cba2018-05-31 11:06:11 +0200487free_name:
Amir Goldstein016b7202018-01-11 14:01:08 +0200488 kfree(name.name);
489 return err;
Amir Goldstein016b7202018-01-11 14:01:08 +0200490}
491
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200492struct ovl_copy_up_ctx {
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200493 struct dentry *parent;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200494 struct dentry *dentry;
495 struct path lowerpath;
496 struct kstat stat;
497 struct kstat pstat;
498 const char *link;
Amir Goldstein59be0972017-06-20 15:25:46 +0300499 struct dentry *destdir;
500 struct qstr destname;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200501 struct dentry *workdir;
Amir Goldstein59be0972017-06-20 15:25:46 +0300502 bool origin;
Amir Goldstein016b7202018-01-11 14:01:08 +0200503 bool indexed;
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400504 bool metacopy;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200505};
506
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300507static int ovl_link_up(struct ovl_copy_up_ctx *c)
508{
509 int err;
510 struct dentry *upper;
511 struct dentry *upperdir = ovl_dentry_upper(c->parent);
512 struct inode *udir = d_inode(upperdir);
513
514 /* Mark parent "impure" because it may now contain non-pure upper */
515 err = ovl_set_impure(c->parent, upperdir);
516 if (err)
517 return err;
518
519 err = ovl_set_nlink_lower(c->dentry);
520 if (err)
521 return err;
522
523 inode_lock_nested(udir, I_MUTEX_PARENT);
524 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
525 c->dentry->d_name.len);
526 err = PTR_ERR(upper);
527 if (!IS_ERR(upper)) {
Amir Goldstein6cf00762018-05-16 17:04:00 +0300528 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300529 dput(upper);
530
531 if (!err) {
532 /* Restore timestamps on parent (best effort) */
533 ovl_set_timestamps(upperdir, &c->pstat);
534 ovl_dentry_set_upper_alias(c->dentry);
535 }
536 }
537 inode_unlock(udir);
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300538 if (err)
539 return err;
540
541 err = ovl_set_nlink_upper(c->dentry);
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300542
543 return err;
544}
545
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200546static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
Amir Goldstein02209d12017-05-19 15:16:21 +0300547{
Vivek Goyalc86243b02020-08-31 14:15:29 -0400548 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
Amir Goldstein72db8212021-06-19 12:26:18 +0300549 struct inode *inode = d_inode(c->dentry);
550 struct path upperpath, datapath;
Amir Goldstein02209d12017-05-19 15:16:21 +0300551 int err;
552
Amir Goldstein72db8212021-06-19 12:26:18 +0300553 ovl_path_upper(c->dentry, &upperpath);
554 if (WARN_ON(upperpath.dentry != NULL))
555 return -EIO;
556
557 upperpath.dentry = temp;
558
Vivek Goyal5f328792019-01-11 19:37:00 +0100559 /*
560 * Copy up data first and then xattrs. Writing data after
561 * xattrs will remove security.capability xattr automatically.
562 */
563 if (S_ISREG(c->stat.mode) && !c->metacopy) {
Vivek Goyal5f328792019-01-11 19:37:00 +0100564 ovl_path_lowerdata(c->dentry, &datapath);
Vivek Goyalc86243b02020-08-31 14:15:29 -0400565 err = ovl_copy_up_data(ofs, &datapath, &upperpath,
566 c->stat.size);
Vivek Goyal5f328792019-01-11 19:37:00 +0100567 if (err)
568 return err;
569 }
570
Miklos Szeredi610afc02020-09-02 10:58:49 +0200571 err = ovl_copy_xattr(c->dentry->d_sb, c->lowerpath.dentry, temp);
Amir Goldstein02209d12017-05-19 15:16:21 +0300572 if (err)
573 return err;
574
Amir Goldstein72db8212021-06-19 12:26:18 +0300575 if (inode->i_flags & OVL_COPY_I_FLAGS_MASK) {
576 /*
577 * Copy the fileattr inode flags that are the source of already
578 * copied i_flags
579 */
Amir Goldstein096a2182021-06-19 12:26:19 +0300580 err = ovl_copy_fileattr(inode, &c->lowerpath, &upperpath);
Amir Goldstein72db8212021-06-19 12:26:18 +0300581 if (err)
582 return err;
583 }
584
Amir Goldstein02209d12017-05-19 15:16:21 +0300585 /*
586 * Store identifier of lower inode in upper inode xattr to
587 * allow lookup of the copy up origin inode.
588 *
589 * Don't set origin when we are breaking the association with a lower
590 * hard link.
591 */
Amir Goldstein59be0972017-06-20 15:25:46 +0300592 if (c->origin) {
Amir Goldsteina0c236b2021-06-19 12:26:17 +0300593 err = ovl_set_origin(ofs, c->lowerpath.dentry, temp);
Amir Goldstein02209d12017-05-19 15:16:21 +0300594 if (err)
595 return err;
596 }
597
Vivek Goyal0c288872018-05-11 11:49:28 -0400598 if (c->metacopy) {
Amir Goldsteina0c236b2021-06-19 12:26:17 +0300599 err = ovl_check_setxattr(ofs, temp, OVL_XATTR_METACOPY,
Vivek Goyal0c288872018-05-11 11:49:28 -0400600 NULL, 0, -EOPNOTSUPP);
601 if (err)
602 return err;
603 }
604
Vivek Goyalbd64e572018-05-11 11:49:27 -0400605 inode_lock(temp->d_inode);
Chengguang Xub504c652019-11-01 20:35:51 +0800606 if (S_ISREG(c->stat.mode))
Vivek Goyal0c288872018-05-11 11:49:28 -0400607 err = ovl_set_size(temp, &c->stat);
608 if (!err)
609 err = ovl_set_attr(temp, &c->stat);
Vivek Goyalbd64e572018-05-11 11:49:27 -0400610 inode_unlock(temp->d_inode);
611
612 return err;
Amir Goldstein02209d12017-05-19 15:16:21 +0300613}
614
Miklos Szeredi6b522432018-10-26 23:34:39 +0200615struct ovl_cu_creds {
616 const struct cred *old;
617 struct cred *new;
618};
619
620static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200621{
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300622 int err;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300623
Miklos Szeredi6b522432018-10-26 23:34:39 +0200624 cc->old = cc->new = NULL;
625 err = security_inode_copy_up(dentry, &cc->new);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300626 if (err < 0)
Miklos Szeredi6b522432018-10-26 23:34:39 +0200627 return err;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300628
Miklos Szeredi6b522432018-10-26 23:34:39 +0200629 if (cc->new)
630 cc->old = override_creds(cc->new);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300631
Miklos Szeredi6b522432018-10-26 23:34:39 +0200632 return 0;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300633}
634
Miklos Szeredi6b522432018-10-26 23:34:39 +0200635static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300636{
Miklos Szeredi6b522432018-10-26 23:34:39 +0200637 if (cc->new) {
638 revert_creds(cc->old);
639 put_cred(cc->new);
640 }
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300641}
642
643/*
644 * Copyup using workdir to prepare temp file. Used when copying up directories,
645 * special files or when upper fs doesn't support O_TMPFILE.
646 */
647static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
648{
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300649 struct inode *inode;
Miklos Szeredi6b522432018-10-26 23:34:39 +0200650 struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
651 struct dentry *temp, *upper;
652 struct ovl_cu_creds cc;
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200653 int err;
Miklos Szeredi6b522432018-10-26 23:34:39 +0200654 struct ovl_cattr cattr = {
655 /* Can't properly set mode on creation because of the umask */
656 .mode = c->stat.mode & S_IFMT,
657 .rdev = c->stat.rdev,
658 .link = c->link
659 };
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200660
Amir Goldstein773cb4c2020-04-03 08:43:12 +0300661 /* workdir and destdir could be the same when copying up to indexdir */
662 err = -EIO;
663 if (lock_rename(c->workdir, c->destdir) != NULL)
664 goto unlock;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300665
Miklos Szeredi6b522432018-10-26 23:34:39 +0200666 err = ovl_prep_cu_creds(c->dentry, &cc);
667 if (err)
668 goto unlock;
669
670 temp = ovl_create_temp(c->workdir, &cattr);
671 ovl_revert_cu_creds(&cc);
672
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300673 err = PTR_ERR(temp);
674 if (IS_ERR(temp))
675 goto unlock;
676
677 err = ovl_copy_up_inode(c, temp);
678 if (err)
679 goto cleanup;
680
681 if (S_ISDIR(c->stat.mode) && c->indexed) {
682 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
683 if (err)
684 goto cleanup;
685 }
686
Miklos Szeredi6b522432018-10-26 23:34:39 +0200687 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
688 err = PTR_ERR(upper);
689 if (IS_ERR(upper))
690 goto cleanup;
691
692 err = ovl_do_rename(wdir, temp, udir, upper, 0);
693 dput(upper);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300694 if (err)
695 goto cleanup;
696
697 if (!c->metacopy)
698 ovl_set_upperdata(d_inode(c->dentry));
699 inode = d_inode(c->dentry);
Miklos Szeredi6b522432018-10-26 23:34:39 +0200700 ovl_inode_update(inode, temp);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300701 if (S_ISDIR(inode->i_mode))
702 ovl_set_flag(OVL_WHITEOUTS, inode);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300703unlock:
704 unlock_rename(c->workdir, c->destdir);
705
706 return err;
707
708cleanup:
Miklos Szeredi6b522432018-10-26 23:34:39 +0200709 ovl_cleanup(wdir, temp);
710 dput(temp);
711 goto unlock;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300712}
713
714/* Copyup using O_TMPFILE which does not require cross dir locking */
715static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
716{
Miklos Szeredi6b522432018-10-26 23:34:39 +0200717 struct inode *udir = d_inode(c->destdir);
718 struct dentry *temp, *upper;
719 struct ovl_cu_creds cc;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300720 int err;
721
Miklos Szeredi6b522432018-10-26 23:34:39 +0200722 err = ovl_prep_cu_creds(c->dentry, &cc);
723 if (err)
724 return err;
725
726 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
727 ovl_revert_cu_creds(&cc);
728
Miklos Szeredib148cba2018-05-31 11:06:11 +0200729 if (IS_ERR(temp))
730 return PTR_ERR(temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200731
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200732 err = ovl_copy_up_inode(c, temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200733 if (err)
Miklos Szeredi6b522432018-10-26 23:34:39 +0200734 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200735
Miklos Szeredi6b522432018-10-26 23:34:39 +0200736 inode_lock_nested(udir, I_MUTEX_PARENT);
737
738 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
739 err = PTR_ERR(upper);
740 if (!IS_ERR(upper)) {
741 err = ovl_do_link(temp, udir, upper);
742 dput(upper);
743 }
744 inode_unlock(udir);
745
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200746 if (err)
Miklos Szeredi6b522432018-10-26 23:34:39 +0200747 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200748
Vivek Goyal0c288872018-05-11 11:49:28 -0400749 if (!c->metacopy)
750 ovl_set_upperdata(d_inode(c->dentry));
Miklos Szeredi6b522432018-10-26 23:34:39 +0200751 ovl_inode_update(d_inode(c->dentry), temp);
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300752
Miklos Szeredi6b522432018-10-26 23:34:39 +0200753 return 0;
754
755out_dput:
Miklos Szeredie85f82f2017-06-28 13:41:22 +0200756 dput(temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200757 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200758}
759
760/*
761 * Copy up a single dentry
762 *
Miklos Szeredia6c60652016-12-16 11:02:56 +0100763 * All renames start with copy up of source if necessary. The actual
764 * rename will only proceed once the copy up was successful. Copy up uses
765 * upper parent i_mutex for exclusion. Since rename can change d_parent it
766 * is possible that the copy up will lock the old parent. At that point
767 * the file will have already been copied up anyway.
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200768 */
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200769static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200770{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200771 int err;
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300772 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
Amir Goldstein016b7202018-01-11 14:01:08 +0200773 bool to_index = false;
Amir Goldstein59be0972017-06-20 15:25:46 +0300774
Amir Goldstein016b7202018-01-11 14:01:08 +0200775 /*
776 * Indexed non-dir is copied up directly to the index entry and then
777 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
778 * then index entry is created and then copied up dir installed.
779 * Copying dir up to indexdir instead of workdir simplifies locking.
780 */
781 if (ovl_need_index(c->dentry)) {
782 c->indexed = true;
783 if (S_ISDIR(c->stat.mode))
784 c->workdir = ovl_indexdir(c->dentry->d_sb);
785 else
786 to_index = true;
787 }
788
789 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
Amir Goldstein59be0972017-06-20 15:25:46 +0300790 c->origin = true;
791
Amir Goldstein016b7202018-01-11 14:01:08 +0200792 if (to_index) {
Amir Goldstein59be0972017-06-20 15:25:46 +0300793 c->destdir = ovl_indexdir(c->dentry->d_sb);
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300794 err = ovl_get_index_name(ofs, c->lowerpath.dentry, &c->destname);
Amir Goldstein59be0972017-06-20 15:25:46 +0300795 if (err)
796 return err;
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300797 } else if (WARN_ON(!c->parent)) {
798 /* Disconnected dentry must be copied up to index dir */
799 return -EIO;
Amir Goldstein59be0972017-06-20 15:25:46 +0300800 } else {
801 /*
802 * Mark parent "impure" because it may now contain non-pure
803 * upper
804 */
805 err = ovl_set_impure(c->parent, c->destdir);
806 if (err)
807 return err;
808 }
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300809
Amir Goldstein01ad3eb2017-01-17 06:34:57 +0200810 /* Should we copyup with O_TMPFILE or with workdir? */
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300811 if (S_ISREG(c->stat.mode) && ofs->tmpfile)
812 err = ovl_copy_up_tmpfile(c);
813 else
814 err = ovl_copy_up_workdir(c);
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300815 if (err)
816 goto out;
817
818 if (c->indexed)
Amir Goldstein016b7202018-01-11 14:01:08 +0200819 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
820
821 if (to_index) {
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300822 /* Initialize nlink for copy up of disconnected dentry */
823 err = ovl_set_nlink_upper(c->dentry);
824 } else {
Amir Goldstein59be0972017-06-20 15:25:46 +0300825 struct inode *udir = d_inode(c->destdir);
826
827 /* Restore timestamps on parent (best effort) */
828 inode_lock(udir);
829 ovl_set_timestamps(c->destdir, &c->pstat);
830 inode_unlock(udir);
831
832 ovl_dentry_set_upper_alias(c->dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200833 }
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200834
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300835out:
836 if (to_index)
837 kfree(c->destname.name);
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200838 return err;
839}
840
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400841static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
842 int flags)
843{
844 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
845
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400846 if (!ofs->config.metacopy)
847 return false;
848
849 if (!S_ISREG(mode))
850 return false;
851
852 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
853 return false;
854
855 return true;
856}
857
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200858static ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value)
Miklos Szeredifee0f292020-09-02 10:58:48 +0200859{
860 ssize_t res;
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200861 char *buf;
Miklos Szeredifee0f292020-09-02 10:58:48 +0200862
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +0100863 res = vfs_getxattr(&init_user_ns, dentry, name, NULL, 0);
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200864 if (res == -ENODATA || res == -EOPNOTSUPP)
865 res = 0;
Miklos Szeredifee0f292020-09-02 10:58:48 +0200866
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200867 if (res > 0) {
868 buf = kzalloc(res, GFP_KERNEL);
Miklos Szeredifee0f292020-09-02 10:58:48 +0200869 if (!buf)
870 return -ENOMEM;
871
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +0100872 res = vfs_getxattr(&init_user_ns, dentry, name, buf, res);
Miklos Szeredifee0f292020-09-02 10:58:48 +0200873 if (res < 0)
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200874 kfree(buf);
875 else
876 *value = buf;
Miklos Szeredifee0f292020-09-02 10:58:48 +0200877 }
Miklos Szeredifee0f292020-09-02 10:58:48 +0200878 return res;
879}
880
Vivek Goyal0c288872018-05-11 11:49:28 -0400881/* Copy up data of an inode which was copied up metadata only in the past. */
882static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
883{
Vivek Goyalc86243b02020-08-31 14:15:29 -0400884 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
Vivek Goyal4f93b422018-05-11 11:49:30 -0400885 struct path upperpath, datapath;
Vivek Goyal0c288872018-05-11 11:49:28 -0400886 int err;
Vivek Goyal993a0b22019-01-30 14:01:57 -0500887 char *capability = NULL;
Kees Cook3f649ab2020-06-03 13:09:38 -0700888 ssize_t cap_size;
Vivek Goyal0c288872018-05-11 11:49:28 -0400889
890 ovl_path_upper(c->dentry, &upperpath);
891 if (WARN_ON(upperpath.dentry == NULL))
892 return -EIO;
893
Vivek Goyal4f93b422018-05-11 11:49:30 -0400894 ovl_path_lowerdata(c->dentry, &datapath);
895 if (WARN_ON(datapath.dentry == NULL))
896 return -EIO;
897
Vivek Goyal993a0b22019-01-30 14:01:57 -0500898 if (c->stat.size) {
899 err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS,
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200900 &capability);
901 if (cap_size < 0)
Vivek Goyal993a0b22019-01-30 14:01:57 -0500902 goto out;
903 }
904
Vivek Goyalc86243b02020-08-31 14:15:29 -0400905 err = ovl_copy_up_data(ofs, &datapath, &upperpath, c->stat.size);
Vivek Goyal0c288872018-05-11 11:49:28 -0400906 if (err)
Vivek Goyal993a0b22019-01-30 14:01:57 -0500907 goto out_free;
908
909 /*
910 * Writing to upper file will clear security.capability xattr. We
911 * don't want that to happen for normal copy-up operation.
912 */
913 if (capability) {
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +0100914 err = vfs_setxattr(&init_user_ns, upperpath.dentry,
915 XATTR_NAME_CAPS, capability, cap_size, 0);
Vivek Goyal993a0b22019-01-30 14:01:57 -0500916 if (err)
917 goto out_free;
918 }
919
Vivek Goyal0c288872018-05-11 11:49:28 -0400920
Miklos Szeredi610afc02020-09-02 10:58:49 +0200921 err = ovl_do_removexattr(ofs, upperpath.dentry, OVL_XATTR_METACOPY);
Vivek Goyal0c288872018-05-11 11:49:28 -0400922 if (err)
Vivek Goyal993a0b22019-01-30 14:01:57 -0500923 goto out_free;
Vivek Goyal0c288872018-05-11 11:49:28 -0400924
925 ovl_set_upperdata(d_inode(c->dentry));
Vivek Goyal993a0b22019-01-30 14:01:57 -0500926out_free:
927 kfree(capability);
928out:
Vivek Goyal0c288872018-05-11 11:49:28 -0400929 return err;
930}
931
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200932static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
933 int flags)
934{
935 int err;
936 DEFINE_DELAYED_CALL(done);
937 struct path parentpath;
938 struct ovl_copy_up_ctx ctx = {
939 .parent = parent,
940 .dentry = dentry,
941 .workdir = ovl_workdir(dentry),
942 };
943
944 if (WARN_ON(!ctx.workdir))
945 return -EROFS;
946
947 ovl_path_lower(dentry, &ctx.lowerpath);
948 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
949 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
950 if (err)
951 return err;
952
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400953 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
954
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300955 if (parent) {
956 ovl_path_upper(parent, &parentpath);
957 ctx.destdir = parentpath.dentry;
958 ctx.destname = dentry->d_name;
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200959
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300960 err = vfs_getattr(&parentpath, &ctx.pstat,
961 STATX_ATIME | STATX_MTIME,
962 AT_STATX_SYNC_AS_STAT);
963 if (err)
964 return err;
965 }
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200966
967 /* maybe truncate regular file. this has no effect on dirs */
968 if (flags & O_TRUNC)
969 ctx.stat.size = 0;
970
971 if (S_ISLNK(ctx.stat.mode)) {
972 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
973 if (IS_ERR(ctx.link))
974 return PTR_ERR(ctx.link);
975 }
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200976
Vivek Goyal0c288872018-05-11 11:49:28 -0400977 err = ovl_copy_up_start(dentry, flags);
Miklos Szeredifd210b72017-07-04 22:03:18 +0200978 /* err < 0: interrupted, err > 0: raced with another copy-up */
979 if (unlikely(err)) {
980 if (err > 0)
981 err = 0;
982 } else {
Amir Goldstein59be0972017-06-20 15:25:46 +0300983 if (!ovl_dentry_upper(dentry))
984 err = ovl_do_copy_up(&ctx);
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300985 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300986 err = ovl_link_up(&ctx);
Vivek Goyal0c288872018-05-11 11:49:28 -0400987 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
988 err = ovl_copy_up_meta_inode_data(&ctx);
Miklos Szeredifd210b72017-07-04 22:03:18 +0200989 ovl_copy_up_end(dentry);
990 }
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200991 do_delayed_call(&done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200992
993 return err;
994}
995
youngjun5ac8e802020-06-21 07:30:59 -0700996static int ovl_copy_up_flags(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200997{
Vivek Goyal8eac98b2016-09-06 13:40:32 -0400998 int err = 0;
Dan Carpenter7b279bb2021-03-23 16:19:35 +0300999 const struct cred *old_cred;
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +03001000 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
1001
1002 /*
1003 * With NFS export, copy up can get called for a disconnected non-dir.
1004 * In this case, we will copy up lower inode to index dir without
1005 * linking it to upper dir.
1006 */
1007 if (WARN_ON(disconnected && d_is_dir(dentry)))
1008 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001009
Dan Carpenter7b279bb2021-03-23 16:19:35 +03001010 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001011 while (!err) {
1012 struct dentry *next;
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +03001013 struct dentry *parent = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001014
Vivek Goyal0c288872018-05-11 11:49:28 -04001015 if (ovl_already_copied_up(dentry, flags))
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001016 break;
1017
1018 next = dget(dentry);
1019 /* find the topmost dentry not yet copied up */
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +03001020 for (; !disconnected;) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001021 parent = dget_parent(next);
1022
Amir Goldstein59be0972017-06-20 15:25:46 +03001023 if (ovl_dentry_upper(parent))
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001024 break;
1025
1026 dput(next);
1027 next = parent;
1028 }
1029
Miklos Szeredia6fb2352017-07-04 22:03:18 +02001030 err = ovl_copy_up_one(parent, next, flags);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001031
1032 dput(parent);
1033 dput(next);
1034 }
Vivek Goyal8eac98b2016-09-06 13:40:32 -04001035 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001036
1037 return err;
1038}
Amir Goldstein9aba6522016-11-12 21:36:03 +02001039
Vivek Goyald6eac032018-05-11 11:49:27 -04001040static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
1041{
1042 /* Copy up of disconnected dentry does not set upper alias */
Vivek Goyal0c288872018-05-11 11:49:28 -04001043 if (ovl_already_copied_up(dentry, flags))
Vivek Goyald6eac032018-05-11 11:49:27 -04001044 return false;
1045
1046 if (special_file(d_inode(dentry)->i_mode))
1047 return false;
1048
Vivek Goyal0c288872018-05-11 11:49:28 -04001049 if (!ovl_open_flags_need_copy_up(flags))
Vivek Goyald6eac032018-05-11 11:49:27 -04001050 return false;
1051
1052 return true;
1053}
1054
Amir Goldstein34280302019-01-22 07:01:39 +02001055int ovl_maybe_copy_up(struct dentry *dentry, int flags)
Vivek Goyald6eac032018-05-11 11:49:27 -04001056{
1057 int err = 0;
1058
Amir Goldstein34280302019-01-22 07:01:39 +02001059 if (ovl_open_need_copy_up(dentry, flags)) {
Vivek Goyald6eac032018-05-11 11:49:27 -04001060 err = ovl_want_write(dentry);
1061 if (!err) {
Amir Goldstein34280302019-01-22 07:01:39 +02001062 err = ovl_copy_up_flags(dentry, flags);
Vivek Goyald6eac032018-05-11 11:49:27 -04001063 ovl_drop_write(dentry);
1064 }
1065 }
1066
1067 return err;
1068}
1069
Vivek Goyald1e6f6a2018-05-11 11:49:33 -04001070int ovl_copy_up_with_data(struct dentry *dentry)
1071{
1072 return ovl_copy_up_flags(dentry, O_WRONLY);
1073}
1074
Amir Goldstein9aba6522016-11-12 21:36:03 +02001075int ovl_copy_up(struct dentry *dentry)
1076{
1077 return ovl_copy_up_flags(dentry, 0);
1078}