blob: b9d97e7efd5c6ebb05ab47ea856665af21ff4227 [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>
11#include <linux/splice.h>
12#include <linux/xattr.h>
13#include <linux/security.h>
14#include <linux/uaccess.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010015#include <linux/sched/signal.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010016#include <linux/cred.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020017#include <linux/namei.h>
David Howellsfb5bb2c32015-07-07 15:04:44 +010018#include <linux/fdtable.h>
19#include <linux/ratelimit.h>
Amir Goldstein3a1e8192017-03-30 15:22:16 +030020#include <linux/exportfs.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020021#include "overlayfs.h"
22
23#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
24
Miklos Szeredi670c2322018-07-18 15:44:44 +020025static int ovl_ccup_set(const char *buf, const struct kernel_param *param)
David Howellsfb5bb2c32015-07-07 15:04:44 +010026{
lijiazi1bd0a3a2019-12-16 19:12:32 +080027 pr_warn("\"check_copy_up\" module option is obsolete\n");
David Howellsfb5bb2c32015-07-07 15:04:44 +010028 return 0;
29}
30
Miklos Szeredi670c2322018-07-18 15:44:44 +020031static int ovl_ccup_get(char *buf, const struct kernel_param *param)
David Howellsfb5bb2c32015-07-07 15:04:44 +010032{
Miklos Szeredi670c2322018-07-18 15:44:44 +020033 return sprintf(buf, "N\n");
David Howellsfb5bb2c32015-07-07 15:04:44 +010034}
35
Miklos Szeredi670c2322018-07-18 15:44:44 +020036module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644);
Nicolas Schier253e7482019-06-17 09:39:00 +020037MODULE_PARM_DESC(check_copy_up, "Obsolete; does nothing");
Miklos Szeredi670c2322018-07-18 15:44:44 +020038
Miklos Szeredic61ca552020-03-17 15:04:22 +010039static bool ovl_must_copy_xattr(const char *name)
40{
41 return !strcmp(name, XATTR_POSIX_ACL_ACCESS) ||
42 !strcmp(name, XATTR_POSIX_ACL_DEFAULT) ||
43 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN);
44}
45
Miklos Szeredi610afc02020-09-02 10:58:49 +020046int ovl_copy_xattr(struct super_block *sb, struct dentry *old,
47 struct dentry *new)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020048{
Vito Caputoe4ad29f2015-10-24 07:19:46 -050049 ssize_t list_size, size, value_size = 0;
50 char *buf, *name, *value = NULL;
Yuxuan Shui520da692020-05-27 04:08:02 +010051 int error = 0;
Miklos Szeredi8b326c62016-09-16 14:12:11 +020052 size_t slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020053
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +020054 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
55 !(new->d_inode->i_opflags & IOP_XATTR))
Miklos Szeredie9be9d52014-10-24 00:14:38 +020056 return 0;
57
58 list_size = vfs_listxattr(old, NULL, 0);
59 if (list_size <= 0) {
60 if (list_size == -EOPNOTSUPP)
61 return 0;
62 return list_size;
63 }
64
65 buf = kzalloc(list_size, GFP_KERNEL);
66 if (!buf)
67 return -ENOMEM;
68
Miklos Szeredie9be9d52014-10-24 00:14:38 +020069 list_size = vfs_listxattr(old, buf, list_size);
70 if (list_size <= 0) {
71 error = list_size;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050072 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020073 }
74
Miklos Szeredi8b326c62016-09-16 14:12:11 +020075 for (name = buf; list_size; name += slen) {
76 slen = strnlen(name, list_size) + 1;
77
78 /* underlying fs providing us with an broken xattr list? */
79 if (WARN_ON(slen > list_size)) {
80 error = -EIO;
81 break;
82 }
83 list_size -= slen;
84
Miklos Szeredi610afc02020-09-02 10:58:49 +020085 if (ovl_is_private_xattr(sb, name))
Miklos Szeredi09562542016-08-08 15:08:49 +020086 continue;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050087retry:
88 size = vfs_getxattr(old, name, value, value_size);
89 if (size == -ERANGE)
90 size = vfs_getxattr(old, name, NULL, 0);
91
Miklos Szeredi97daf8b2015-11-10 17:08:41 +010092 if (size < 0) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +020093 error = size;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050094 break;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020095 }
Vito Caputoe4ad29f2015-10-24 07:19:46 -050096
97 if (size > value_size) {
98 void *new;
99
100 new = krealloc(value, size, GFP_KERNEL);
101 if (!new) {
102 error = -ENOMEM;
103 break;
104 }
105 value = new;
106 value_size = size;
107 goto retry;
108 }
109
Vivek Goyal121ab822016-07-13 10:44:49 -0400110 error = security_inode_copy_up_xattr(name);
111 if (error < 0 && error != -EOPNOTSUPP)
112 break;
113 if (error == 1) {
114 error = 0;
115 continue; /* Discard */
116 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200117 error = vfs_setxattr(new, name, value, size, 0);
Miklos Szeredic61ca552020-03-17 15:04:22 +0100118 if (error) {
119 if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name))
120 break;
121
122 /* Ignore failure to copy unknown xattrs */
123 error = 0;
124 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200125 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200126 kfree(value);
127out:
128 kfree(buf);
129 return error;
130}
131
Vivek Goyalc86243b02020-08-31 14:15:29 -0400132static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old,
133 struct path *new, loff_t len)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200134{
135 struct file *old_file;
136 struct file *new_file;
137 loff_t old_pos = 0;
138 loff_t new_pos = 0;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100139 loff_t cloned;
Chengguang Xub504c652019-11-01 20:35:51 +0800140 loff_t data_pos = -1;
141 loff_t hole_len;
142 bool skip_hole = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200143 int error = 0;
144
145 if (len == 0)
146 return 0;
147
David Howells04803342015-09-18 11:45:12 +0100148 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200149 if (IS_ERR(old_file))
150 return PTR_ERR(old_file);
151
David Howells04803342015-09-18 11:45:12 +0100152 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200153 if (IS_ERR(new_file)) {
154 error = PTR_ERR(new_file);
155 goto out_fput;
156 }
157
Amir Goldstein2ea98462016-09-23 11:38:12 +0300158 /* Try to use clone_file_range to clone up within the same fs */
Darrick J. Wong452ce652018-10-30 10:41:56 +1100159 cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100160 if (cloned == len)
Amir Goldstein2ea98462016-09-23 11:38:12 +0300161 goto out;
162 /* Couldn't clone, so now we try to copy the data */
Amir Goldstein2ea98462016-09-23 11:38:12 +0300163
Chengguang Xub504c652019-11-01 20:35:51 +0800164 /* Check if lower fs supports seek operation */
165 if (old_file->f_mode & FMODE_LSEEK &&
166 old_file->f_op->llseek)
167 skip_hole = true;
168
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200169 while (len) {
170 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
171 long bytes;
172
173 if (len < this_len)
174 this_len = len;
175
176 if (signal_pending_state(TASK_KILLABLE, current)) {
177 error = -EINTR;
178 break;
179 }
180
Chengguang Xub504c652019-11-01 20:35:51 +0800181 /*
182 * Fill zero for hole will cost unnecessary disk space
183 * and meanwhile slow down the copy-up speed, so we do
184 * an optimization for hole during copy-up, it relies
185 * on SEEK_DATA implementation in lower fs so if lower
186 * fs does not support it, copy-up will behave as before.
187 *
188 * Detail logic of hole detection as below:
189 * When we detect next data position is larger than current
190 * position we will skip that hole, otherwise we copy
191 * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually,
192 * it may not recognize all kind of holes and sometimes
193 * only skips partial of hole area. However, it will be
194 * enough for most of the use cases.
195 */
196
197 if (skip_hole && data_pos < old_pos) {
198 data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
199 if (data_pos > old_pos) {
200 hole_len = data_pos - old_pos;
201 len -= hole_len;
202 old_pos = new_pos = data_pos;
203 continue;
204 } else if (data_pos == -ENXIO) {
205 break;
206 } else if (data_pos < 0) {
207 skip_hole = false;
208 }
209 }
210
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200211 bytes = do_splice_direct(old_file, &old_pos,
212 new_file, &new_pos,
213 this_len, SPLICE_F_MOVE);
214 if (bytes <= 0) {
215 error = bytes;
216 break;
217 }
218 WARN_ON(old_pos != new_pos);
219
220 len -= bytes;
221 }
Amir Goldstein2ea98462016-09-23 11:38:12 +0300222out:
Vivek Goyalc86243b02020-08-31 14:15:29 -0400223 if (!error && ovl_should_sync(ofs))
Miklos Szeredi641089c2016-10-31 14:42:14 +0100224 error = vfs_fsync(new_file, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200225 fput(new_file);
226out_fput:
227 fput(old_file);
228 return error;
229}
230
Vivek Goyal0c288872018-05-11 11:49:28 -0400231static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
232{
233 struct iattr attr = {
234 .ia_valid = ATTR_SIZE,
235 .ia_size = stat->size,
236 };
237
238 return notify_change(upperdentry, &attr, NULL);
239}
240
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200241static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
242{
243 struct iattr attr = {
244 .ia_valid =
245 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
246 .ia_atime = stat->atime,
247 .ia_mtime = stat->mtime,
248 };
249
250 return notify_change(upperdentry, &attr, NULL);
251}
252
253int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
254{
255 int err = 0;
256
257 if (!S_ISLNK(stat->mode)) {
258 struct iattr attr = {
259 .ia_valid = ATTR_MODE,
260 .ia_mode = stat->mode,
261 };
262 err = notify_change(upperdentry, &attr, NULL);
263 }
264 if (!err) {
265 struct iattr attr = {
266 .ia_valid = ATTR_UID | ATTR_GID,
267 .ia_uid = stat->uid,
268 .ia_gid = stat->gid,
269 };
270 err = notify_change(upperdentry, &attr, NULL);
271 }
272 if (!err)
273 ovl_set_timestamps(upperdentry, stat);
274
275 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200276}
277
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200278struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper)
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300279{
280 struct ovl_fh *fh;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200281 int fh_type, dwords;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300282 int buflen = MAX_HANDLE_SZ;
Amir Goldstein05122442018-01-11 08:25:32 +0200283 uuid_t *uuid = &real->d_sb->s_uuid;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200284 int err;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300285
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200286 /* Make sure the real fid stays 32bit aligned */
287 BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4);
288 BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255);
289
290 fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL);
291 if (!fh)
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300292 return ERR_PTR(-ENOMEM);
293
294 /*
295 * We encode a non-connectable file handle for non-dir, because we
296 * only need to find the lower inode number and we don't want to pay
297 * the price or reconnecting the dentry.
298 */
299 dwords = buflen >> 2;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200300 fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300301 buflen = (dwords << 2);
302
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200303 err = -EIO;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300304 if (WARN_ON(fh_type < 0) ||
305 WARN_ON(buflen > MAX_HANDLE_SZ) ||
306 WARN_ON(fh_type == FILEID_INVALID))
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200307 goto out_err;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300308
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200309 fh->fb.version = OVL_FH_VERSION;
310 fh->fb.magic = OVL_FH_MAGIC;
311 fh->fb.type = fh_type;
312 fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN;
Amir Goldstein54fb3472017-06-21 15:28:38 +0300313 /*
314 * When we will want to decode an overlay dentry from this handle
315 * and all layers are on the same fs, if we get a disconncted real
316 * dentry when we decode fid, the only way to tell if we should assign
317 * it to upperdentry or to lowerstack is by checking this flag.
318 */
319 if (is_upper)
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200320 fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200321 fh->fb.len = sizeof(fh->fb) + buflen;
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200322 fh->fb.uuid = *uuid;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300323
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300324 return fh;
Amir Goldsteinec7bbb532019-11-15 13:33:04 +0200325
326out_err:
327 kfree(fh);
328 return ERR_PTR(err);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300329}
330
Amir Goldstein9678e632018-01-03 19:34:45 +0200331int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
332 struct dentry *upper)
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300333{
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300334 const struct ovl_fh *fh = NULL;
335 int err;
336
337 /*
338 * When lower layer doesn't support export operations store a 'null' fh,
339 * so we can use the overlay.origin xattr to distignuish between a copy
340 * up and a pure upper inode.
341 */
Amir Goldstein02bcd152017-06-21 15:28:36 +0300342 if (ovl_can_decode_fh(lower->d_sb)) {
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200343 fh = ovl_encode_real_fh(lower, false);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300344 if (IS_ERR(fh))
345 return PTR_ERR(fh);
346 }
347
Miklos Szeredi6266d462017-05-18 16:11:24 +0200348 /*
349 * Do not fail when upper doesn't support xattrs.
350 */
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200351 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh->buf,
352 fh ? fh->fb.len : 0, 0);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300353 kfree(fh);
354
355 return err;
356}
357
Amir Goldstein016b7202018-01-11 14:01:08 +0200358/* Store file handle of @upper dir in @index dir entry */
Miklos Szeredi610afc02020-09-02 10:58:49 +0200359static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper,
360 struct dentry *index)
Amir Goldstein016b7202018-01-11 14:01:08 +0200361{
362 const struct ovl_fh *fh;
363 int err;
364
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200365 fh = ovl_encode_real_fh(upper, true);
Amir Goldstein016b7202018-01-11 14:01:08 +0200366 if (IS_ERR(fh))
367 return PTR_ERR(fh);
368
Miklos Szeredi610afc02020-09-02 10:58:49 +0200369 err = ovl_do_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len);
Amir Goldstein016b7202018-01-11 14:01:08 +0200370
371 kfree(fh);
372 return err;
373}
374
375/*
376 * Create and install index entry.
377 *
378 * Caller must hold i_mutex on indexdir.
379 */
380static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
381 struct dentry *upper)
382{
383 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
384 struct inode *dir = d_inode(indexdir);
385 struct dentry *index = NULL;
386 struct dentry *temp = NULL;
387 struct qstr name = { };
388 int err;
389
390 /*
391 * For now this is only used for creating index entry for directories,
392 * because non-dir are copied up directly to index and then hardlinked
393 * to upper dir.
394 *
395 * TODO: implement create index for non-dir, so we can call it when
396 * encoding file handle for non-dir in case index does not exist.
397 */
398 if (WARN_ON(!d_is_dir(dentry)))
399 return -EIO;
400
401 /* Directory not expected to be indexed before copy up */
402 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
403 return -EIO;
404
405 err = ovl_get_index_name(origin, &name);
406 if (err)
407 return err;
408
Amir Goldstein137ec522018-05-16 17:51:25 +0300409 temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
Miklos Szeredib148cba2018-05-31 11:06:11 +0200410 err = PTR_ERR(temp);
Amir Goldstein016b7202018-01-11 14:01:08 +0200411 if (IS_ERR(temp))
Miklos Szeredib148cba2018-05-31 11:06:11 +0200412 goto free_name;
Amir Goldstein016b7202018-01-11 14:01:08 +0200413
Miklos Szeredi610afc02020-09-02 10:58:49 +0200414 err = ovl_set_upper_fh(OVL_FS(dentry->d_sb), upper, temp);
Amir Goldstein016b7202018-01-11 14:01:08 +0200415 if (err)
Miklos Szeredib148cba2018-05-31 11:06:11 +0200416 goto out;
Amir Goldstein016b7202018-01-11 14:01:08 +0200417
418 index = lookup_one_len(name.name, indexdir, name.len);
419 if (IS_ERR(index)) {
420 err = PTR_ERR(index);
421 } else {
422 err = ovl_do_rename(dir, temp, dir, index, 0);
423 dput(index);
424 }
Amir Goldstein016b7202018-01-11 14:01:08 +0200425out:
Miklos Szeredib148cba2018-05-31 11:06:11 +0200426 if (err)
427 ovl_cleanup(dir, temp);
Amir Goldstein016b7202018-01-11 14:01:08 +0200428 dput(temp);
Miklos Szeredib148cba2018-05-31 11:06:11 +0200429free_name:
Amir Goldstein016b7202018-01-11 14:01:08 +0200430 kfree(name.name);
431 return err;
Amir Goldstein016b7202018-01-11 14:01:08 +0200432}
433
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200434struct ovl_copy_up_ctx {
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200435 struct dentry *parent;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200436 struct dentry *dentry;
437 struct path lowerpath;
438 struct kstat stat;
439 struct kstat pstat;
440 const char *link;
Amir Goldstein59be0972017-06-20 15:25:46 +0300441 struct dentry *destdir;
442 struct qstr destname;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200443 struct dentry *workdir;
Amir Goldstein59be0972017-06-20 15:25:46 +0300444 bool origin;
Amir Goldstein016b7202018-01-11 14:01:08 +0200445 bool indexed;
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400446 bool metacopy;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200447};
448
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300449static int ovl_link_up(struct ovl_copy_up_ctx *c)
450{
451 int err;
452 struct dentry *upper;
453 struct dentry *upperdir = ovl_dentry_upper(c->parent);
454 struct inode *udir = d_inode(upperdir);
455
456 /* Mark parent "impure" because it may now contain non-pure upper */
457 err = ovl_set_impure(c->parent, upperdir);
458 if (err)
459 return err;
460
461 err = ovl_set_nlink_lower(c->dentry);
462 if (err)
463 return err;
464
465 inode_lock_nested(udir, I_MUTEX_PARENT);
466 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
467 c->dentry->d_name.len);
468 err = PTR_ERR(upper);
469 if (!IS_ERR(upper)) {
Amir Goldstein6cf00762018-05-16 17:04:00 +0300470 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300471 dput(upper);
472
473 if (!err) {
474 /* Restore timestamps on parent (best effort) */
475 ovl_set_timestamps(upperdir, &c->pstat);
476 ovl_dentry_set_upper_alias(c->dentry);
477 }
478 }
479 inode_unlock(udir);
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300480 if (err)
481 return err;
482
483 err = ovl_set_nlink_upper(c->dentry);
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300484
485 return err;
486}
487
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200488static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
Amir Goldstein02209d12017-05-19 15:16:21 +0300489{
Vivek Goyalc86243b02020-08-31 14:15:29 -0400490 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
Amir Goldstein02209d12017-05-19 15:16:21 +0300491 int err;
492
Vivek Goyal5f328792019-01-11 19:37:00 +0100493 /*
494 * Copy up data first and then xattrs. Writing data after
495 * xattrs will remove security.capability xattr automatically.
496 */
497 if (S_ISREG(c->stat.mode) && !c->metacopy) {
498 struct path upperpath, datapath;
499
500 ovl_path_upper(c->dentry, &upperpath);
501 if (WARN_ON(upperpath.dentry != NULL))
502 return -EIO;
503 upperpath.dentry = temp;
504
505 ovl_path_lowerdata(c->dentry, &datapath);
Vivek Goyalc86243b02020-08-31 14:15:29 -0400506 err = ovl_copy_up_data(ofs, &datapath, &upperpath,
507 c->stat.size);
Vivek Goyal5f328792019-01-11 19:37:00 +0100508 if (err)
509 return err;
510 }
511
Miklos Szeredi610afc02020-09-02 10:58:49 +0200512 err = ovl_copy_xattr(c->dentry->d_sb, c->lowerpath.dentry, temp);
Amir Goldstein02209d12017-05-19 15:16:21 +0300513 if (err)
514 return err;
515
Amir Goldstein02209d12017-05-19 15:16:21 +0300516 /*
517 * Store identifier of lower inode in upper inode xattr to
518 * allow lookup of the copy up origin inode.
519 *
520 * Don't set origin when we are breaking the association with a lower
521 * hard link.
522 */
Amir Goldstein59be0972017-06-20 15:25:46 +0300523 if (c->origin) {
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200524 err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
Amir Goldstein02209d12017-05-19 15:16:21 +0300525 if (err)
526 return err;
527 }
528
Vivek Goyal0c288872018-05-11 11:49:28 -0400529 if (c->metacopy) {
530 err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
531 NULL, 0, -EOPNOTSUPP);
532 if (err)
533 return err;
534 }
535
Vivek Goyalbd64e572018-05-11 11:49:27 -0400536 inode_lock(temp->d_inode);
Chengguang Xub504c652019-11-01 20:35:51 +0800537 if (S_ISREG(c->stat.mode))
Vivek Goyal0c288872018-05-11 11:49:28 -0400538 err = ovl_set_size(temp, &c->stat);
539 if (!err)
540 err = ovl_set_attr(temp, &c->stat);
Vivek Goyalbd64e572018-05-11 11:49:27 -0400541 inode_unlock(temp->d_inode);
542
543 return err;
Amir Goldstein02209d12017-05-19 15:16:21 +0300544}
545
Miklos Szeredi6b522432018-10-26 23:34:39 +0200546struct ovl_cu_creds {
547 const struct cred *old;
548 struct cred *new;
549};
550
551static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200552{
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300553 int err;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300554
Miklos Szeredi6b522432018-10-26 23:34:39 +0200555 cc->old = cc->new = NULL;
556 err = security_inode_copy_up(dentry, &cc->new);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300557 if (err < 0)
Miklos Szeredi6b522432018-10-26 23:34:39 +0200558 return err;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300559
Miklos Szeredi6b522432018-10-26 23:34:39 +0200560 if (cc->new)
561 cc->old = override_creds(cc->new);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300562
Miklos Szeredi6b522432018-10-26 23:34:39 +0200563 return 0;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300564}
565
Miklos Szeredi6b522432018-10-26 23:34:39 +0200566static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300567{
Miklos Szeredi6b522432018-10-26 23:34:39 +0200568 if (cc->new) {
569 revert_creds(cc->old);
570 put_cred(cc->new);
571 }
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300572}
573
574/*
575 * Copyup using workdir to prepare temp file. Used when copying up directories,
576 * special files or when upper fs doesn't support O_TMPFILE.
577 */
578static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
579{
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300580 struct inode *inode;
Miklos Szeredi6b522432018-10-26 23:34:39 +0200581 struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
582 struct dentry *temp, *upper;
583 struct ovl_cu_creds cc;
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200584 int err;
Miklos Szeredi6b522432018-10-26 23:34:39 +0200585 struct ovl_cattr cattr = {
586 /* Can't properly set mode on creation because of the umask */
587 .mode = c->stat.mode & S_IFMT,
588 .rdev = c->stat.rdev,
589 .link = c->link
590 };
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200591
Amir Goldstein773cb4c2020-04-03 08:43:12 +0300592 /* workdir and destdir could be the same when copying up to indexdir */
593 err = -EIO;
594 if (lock_rename(c->workdir, c->destdir) != NULL)
595 goto unlock;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300596
Miklos Szeredi6b522432018-10-26 23:34:39 +0200597 err = ovl_prep_cu_creds(c->dentry, &cc);
598 if (err)
599 goto unlock;
600
601 temp = ovl_create_temp(c->workdir, &cattr);
602 ovl_revert_cu_creds(&cc);
603
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300604 err = PTR_ERR(temp);
605 if (IS_ERR(temp))
606 goto unlock;
607
608 err = ovl_copy_up_inode(c, temp);
609 if (err)
610 goto cleanup;
611
612 if (S_ISDIR(c->stat.mode) && c->indexed) {
613 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
614 if (err)
615 goto cleanup;
616 }
617
Miklos Szeredi6b522432018-10-26 23:34:39 +0200618 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
619 err = PTR_ERR(upper);
620 if (IS_ERR(upper))
621 goto cleanup;
622
623 err = ovl_do_rename(wdir, temp, udir, upper, 0);
624 dput(upper);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300625 if (err)
626 goto cleanup;
627
628 if (!c->metacopy)
629 ovl_set_upperdata(d_inode(c->dentry));
630 inode = d_inode(c->dentry);
Miklos Szeredi6b522432018-10-26 23:34:39 +0200631 ovl_inode_update(inode, temp);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300632 if (S_ISDIR(inode->i_mode))
633 ovl_set_flag(OVL_WHITEOUTS, inode);
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300634unlock:
635 unlock_rename(c->workdir, c->destdir);
636
637 return err;
638
639cleanup:
Miklos Szeredi6b522432018-10-26 23:34:39 +0200640 ovl_cleanup(wdir, temp);
641 dput(temp);
642 goto unlock;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300643}
644
645/* Copyup using O_TMPFILE which does not require cross dir locking */
646static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
647{
Miklos Szeredi6b522432018-10-26 23:34:39 +0200648 struct inode *udir = d_inode(c->destdir);
649 struct dentry *temp, *upper;
650 struct ovl_cu_creds cc;
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300651 int err;
652
Miklos Szeredi6b522432018-10-26 23:34:39 +0200653 err = ovl_prep_cu_creds(c->dentry, &cc);
654 if (err)
655 return err;
656
657 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
658 ovl_revert_cu_creds(&cc);
659
Miklos Szeredib148cba2018-05-31 11:06:11 +0200660 if (IS_ERR(temp))
661 return PTR_ERR(temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200662
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200663 err = ovl_copy_up_inode(c, temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200664 if (err)
Miklos Szeredi6b522432018-10-26 23:34:39 +0200665 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200666
Miklos Szeredi6b522432018-10-26 23:34:39 +0200667 inode_lock_nested(udir, I_MUTEX_PARENT);
668
669 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
670 err = PTR_ERR(upper);
671 if (!IS_ERR(upper)) {
672 err = ovl_do_link(temp, udir, upper);
673 dput(upper);
674 }
675 inode_unlock(udir);
676
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200677 if (err)
Miklos Szeredi6b522432018-10-26 23:34:39 +0200678 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200679
Vivek Goyal0c288872018-05-11 11:49:28 -0400680 if (!c->metacopy)
681 ovl_set_upperdata(d_inode(c->dentry));
Miklos Szeredi6b522432018-10-26 23:34:39 +0200682 ovl_inode_update(d_inode(c->dentry), temp);
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300683
Miklos Szeredi6b522432018-10-26 23:34:39 +0200684 return 0;
685
686out_dput:
Miklos Szeredie85f82f2017-06-28 13:41:22 +0200687 dput(temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200688 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200689}
690
691/*
692 * Copy up a single dentry
693 *
Miklos Szeredia6c60652016-12-16 11:02:56 +0100694 * All renames start with copy up of source if necessary. The actual
695 * rename will only proceed once the copy up was successful. Copy up uses
696 * upper parent i_mutex for exclusion. Since rename can change d_parent it
697 * is possible that the copy up will lock the old parent. At that point
698 * the file will have already been copied up anyway.
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200699 */
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200700static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200701{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200702 int err;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200703 struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
Amir Goldstein016b7202018-01-11 14:01:08 +0200704 bool to_index = false;
Amir Goldstein59be0972017-06-20 15:25:46 +0300705
Amir Goldstein016b7202018-01-11 14:01:08 +0200706 /*
707 * Indexed non-dir is copied up directly to the index entry and then
708 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
709 * then index entry is created and then copied up dir installed.
710 * Copying dir up to indexdir instead of workdir simplifies locking.
711 */
712 if (ovl_need_index(c->dentry)) {
713 c->indexed = true;
714 if (S_ISDIR(c->stat.mode))
715 c->workdir = ovl_indexdir(c->dentry->d_sb);
716 else
717 to_index = true;
718 }
719
720 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
Amir Goldstein59be0972017-06-20 15:25:46 +0300721 c->origin = true;
722
Amir Goldstein016b7202018-01-11 14:01:08 +0200723 if (to_index) {
Amir Goldstein59be0972017-06-20 15:25:46 +0300724 c->destdir = ovl_indexdir(c->dentry->d_sb);
725 err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
726 if (err)
727 return err;
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300728 } else if (WARN_ON(!c->parent)) {
729 /* Disconnected dentry must be copied up to index dir */
730 return -EIO;
Amir Goldstein59be0972017-06-20 15:25:46 +0300731 } else {
732 /*
733 * Mark parent "impure" because it may now contain non-pure
734 * upper
735 */
736 err = ovl_set_impure(c->parent, c->destdir);
737 if (err)
738 return err;
739 }
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300740
Amir Goldstein01ad3eb2017-01-17 06:34:57 +0200741 /* Should we copyup with O_TMPFILE or with workdir? */
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300742 if (S_ISREG(c->stat.mode) && ofs->tmpfile)
743 err = ovl_copy_up_tmpfile(c);
744 else
745 err = ovl_copy_up_workdir(c);
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300746 if (err)
747 goto out;
748
749 if (c->indexed)
Amir Goldstein016b7202018-01-11 14:01:08 +0200750 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
751
752 if (to_index) {
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300753 /* Initialize nlink for copy up of disconnected dentry */
754 err = ovl_set_nlink_upper(c->dentry);
755 } else {
Amir Goldstein59be0972017-06-20 15:25:46 +0300756 struct inode *udir = d_inode(c->destdir);
757
758 /* Restore timestamps on parent (best effort) */
759 inode_lock(udir);
760 ovl_set_timestamps(c->destdir, &c->pstat);
761 inode_unlock(udir);
762
763 ovl_dentry_set_upper_alias(c->dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200764 }
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200765
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300766out:
767 if (to_index)
768 kfree(c->destname.name);
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200769 return err;
770}
771
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400772static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
773 int flags)
774{
775 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
776
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400777 if (!ofs->config.metacopy)
778 return false;
779
780 if (!S_ISREG(mode))
781 return false;
782
783 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
784 return false;
785
786 return true;
787}
788
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200789static ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value)
Miklos Szeredifee0f292020-09-02 10:58:48 +0200790{
791 ssize_t res;
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200792 char *buf;
Miklos Szeredifee0f292020-09-02 10:58:48 +0200793
794 res = vfs_getxattr(dentry, name, NULL, 0);
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200795 if (res == -ENODATA || res == -EOPNOTSUPP)
796 res = 0;
Miklos Szeredifee0f292020-09-02 10:58:48 +0200797
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200798 if (res > 0) {
799 buf = kzalloc(res, GFP_KERNEL);
Miklos Szeredifee0f292020-09-02 10:58:48 +0200800 if (!buf)
801 return -ENOMEM;
802
803 res = vfs_getxattr(dentry, name, buf, res);
804 if (res < 0)
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200805 kfree(buf);
806 else
807 *value = buf;
Miklos Szeredifee0f292020-09-02 10:58:48 +0200808 }
Miklos Szeredifee0f292020-09-02 10:58:48 +0200809 return res;
810}
811
Vivek Goyal0c288872018-05-11 11:49:28 -0400812/* Copy up data of an inode which was copied up metadata only in the past. */
813static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
814{
Vivek Goyalc86243b02020-08-31 14:15:29 -0400815 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
Vivek Goyal4f93b422018-05-11 11:49:30 -0400816 struct path upperpath, datapath;
Vivek Goyal0c288872018-05-11 11:49:28 -0400817 int err;
Vivek Goyal993a0b22019-01-30 14:01:57 -0500818 char *capability = NULL;
Kees Cook3f649ab2020-06-03 13:09:38 -0700819 ssize_t cap_size;
Vivek Goyal0c288872018-05-11 11:49:28 -0400820
821 ovl_path_upper(c->dentry, &upperpath);
822 if (WARN_ON(upperpath.dentry == NULL))
823 return -EIO;
824
Vivek Goyal4f93b422018-05-11 11:49:30 -0400825 ovl_path_lowerdata(c->dentry, &datapath);
826 if (WARN_ON(datapath.dentry == NULL))
827 return -EIO;
828
Vivek Goyal993a0b22019-01-30 14:01:57 -0500829 if (c->stat.size) {
830 err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS,
Miklos Szeredide7a52c2020-09-02 10:58:48 +0200831 &capability);
832 if (cap_size < 0)
Vivek Goyal993a0b22019-01-30 14:01:57 -0500833 goto out;
834 }
835
Vivek Goyalc86243b02020-08-31 14:15:29 -0400836 err = ovl_copy_up_data(ofs, &datapath, &upperpath, c->stat.size);
Vivek Goyal0c288872018-05-11 11:49:28 -0400837 if (err)
Vivek Goyal993a0b22019-01-30 14:01:57 -0500838 goto out_free;
839
840 /*
841 * Writing to upper file will clear security.capability xattr. We
842 * don't want that to happen for normal copy-up operation.
843 */
844 if (capability) {
Miklos Szeredi71097042020-09-02 10:58:48 +0200845 err = vfs_setxattr(upperpath.dentry, XATTR_NAME_CAPS,
846 capability, cap_size, 0);
Vivek Goyal993a0b22019-01-30 14:01:57 -0500847 if (err)
848 goto out_free;
849 }
850
Vivek Goyal0c288872018-05-11 11:49:28 -0400851
Miklos Szeredi610afc02020-09-02 10:58:49 +0200852 err = ovl_do_removexattr(ofs, upperpath.dentry, OVL_XATTR_METACOPY);
Vivek Goyal0c288872018-05-11 11:49:28 -0400853 if (err)
Vivek Goyal993a0b22019-01-30 14:01:57 -0500854 goto out_free;
Vivek Goyal0c288872018-05-11 11:49:28 -0400855
856 ovl_set_upperdata(d_inode(c->dentry));
Vivek Goyal993a0b22019-01-30 14:01:57 -0500857out_free:
858 kfree(capability);
859out:
Vivek Goyal0c288872018-05-11 11:49:28 -0400860 return err;
861}
862
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200863static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
864 int flags)
865{
866 int err;
867 DEFINE_DELAYED_CALL(done);
868 struct path parentpath;
869 struct ovl_copy_up_ctx ctx = {
870 .parent = parent,
871 .dentry = dentry,
872 .workdir = ovl_workdir(dentry),
873 };
874
875 if (WARN_ON(!ctx.workdir))
876 return -EROFS;
877
878 ovl_path_lower(dentry, &ctx.lowerpath);
879 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
880 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
881 if (err)
882 return err;
883
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400884 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
885
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300886 if (parent) {
887 ovl_path_upper(parent, &parentpath);
888 ctx.destdir = parentpath.dentry;
889 ctx.destname = dentry->d_name;
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200890
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300891 err = vfs_getattr(&parentpath, &ctx.pstat,
892 STATX_ATIME | STATX_MTIME,
893 AT_STATX_SYNC_AS_STAT);
894 if (err)
895 return err;
896 }
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200897
898 /* maybe truncate regular file. this has no effect on dirs */
899 if (flags & O_TRUNC)
900 ctx.stat.size = 0;
901
902 if (S_ISLNK(ctx.stat.mode)) {
903 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
904 if (IS_ERR(ctx.link))
905 return PTR_ERR(ctx.link);
906 }
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200907
Vivek Goyal0c288872018-05-11 11:49:28 -0400908 err = ovl_copy_up_start(dentry, flags);
Miklos Szeredifd210b72017-07-04 22:03:18 +0200909 /* err < 0: interrupted, err > 0: raced with another copy-up */
910 if (unlikely(err)) {
911 if (err > 0)
912 err = 0;
913 } else {
Amir Goldstein59be0972017-06-20 15:25:46 +0300914 if (!ovl_dentry_upper(dentry))
915 err = ovl_do_copy_up(&ctx);
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300916 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300917 err = ovl_link_up(&ctx);
Vivek Goyal0c288872018-05-11 11:49:28 -0400918 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
919 err = ovl_copy_up_meta_inode_data(&ctx);
Miklos Szeredifd210b72017-07-04 22:03:18 +0200920 ovl_copy_up_end(dentry);
921 }
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200922 do_delayed_call(&done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200923
924 return err;
925}
926
youngjun5ac8e802020-06-21 07:30:59 -0700927static int ovl_copy_up_flags(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200928{
Vivek Goyal8eac98b2016-09-06 13:40:32 -0400929 int err = 0;
930 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300931 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
932
933 /*
934 * With NFS export, copy up can get called for a disconnected non-dir.
935 * In this case, we will copy up lower inode to index dir without
936 * linking it to upper dir.
937 */
938 if (WARN_ON(disconnected && d_is_dir(dentry)))
939 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200940
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200941 while (!err) {
942 struct dentry *next;
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300943 struct dentry *parent = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200944
Vivek Goyal0c288872018-05-11 11:49:28 -0400945 if (ovl_already_copied_up(dentry, flags))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200946 break;
947
948 next = dget(dentry);
949 /* find the topmost dentry not yet copied up */
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300950 for (; !disconnected;) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200951 parent = dget_parent(next);
952
Amir Goldstein59be0972017-06-20 15:25:46 +0300953 if (ovl_dentry_upper(parent))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200954 break;
955
956 dput(next);
957 next = parent;
958 }
959
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200960 err = ovl_copy_up_one(parent, next, flags);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200961
962 dput(parent);
963 dput(next);
964 }
Mark Salyzyn6120a4d2018-06-14 11:15:22 -0700965 ovl_revert_creds(dentry->d_sb, old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200966
967 return err;
968}
Amir Goldstein9aba6522016-11-12 21:36:03 +0200969
Vivek Goyald6eac032018-05-11 11:49:27 -0400970static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
971{
972 /* Copy up of disconnected dentry does not set upper alias */
Vivek Goyal0c288872018-05-11 11:49:28 -0400973 if (ovl_already_copied_up(dentry, flags))
Vivek Goyald6eac032018-05-11 11:49:27 -0400974 return false;
975
976 if (special_file(d_inode(dentry)->i_mode))
977 return false;
978
Vivek Goyal0c288872018-05-11 11:49:28 -0400979 if (!ovl_open_flags_need_copy_up(flags))
Vivek Goyald6eac032018-05-11 11:49:27 -0400980 return false;
981
982 return true;
983}
984
Amir Goldstein34280302019-01-22 07:01:39 +0200985int ovl_maybe_copy_up(struct dentry *dentry, int flags)
Vivek Goyald6eac032018-05-11 11:49:27 -0400986{
987 int err = 0;
988
Amir Goldstein34280302019-01-22 07:01:39 +0200989 if (ovl_open_need_copy_up(dentry, flags)) {
Vivek Goyald6eac032018-05-11 11:49:27 -0400990 err = ovl_want_write(dentry);
991 if (!err) {
Amir Goldstein34280302019-01-22 07:01:39 +0200992 err = ovl_copy_up_flags(dentry, flags);
Vivek Goyald6eac032018-05-11 11:49:27 -0400993 ovl_drop_write(dentry);
994 }
995 }
996
997 return err;
998}
999
Vivek Goyald1e6f6a2018-05-11 11:49:33 -04001000int ovl_copy_up_with_data(struct dentry *dentry)
1001{
1002 return ovl_copy_up_flags(dentry, O_WRONLY);
1003}
1004
Amir Goldstein9aba6522016-11-12 21:36:03 +02001005int ovl_copy_up(struct dentry *dentry)
1006{
1007 return ovl_copy_up_flags(dentry, 0);
1008}