blob: 618cffc14f91450a3a70b59ee49cc4dc6b88e63e [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
2 *
3 * Copyright (C) 2011 Novell Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
David Howellsfb5bb2c32015-07-07 15:04:44 +010010#include <linux/module.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020011#include <linux/fs.h>
12#include <linux/slab.h>
13#include <linux/file.h>
14#include <linux/splice.h>
15#include <linux/xattr.h>
16#include <linux/security.h>
17#include <linux/uaccess.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010018#include <linux/sched/signal.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010019#include <linux/cred.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020020#include <linux/namei.h>
David Howellsfb5bb2c32015-07-07 15:04:44 +010021#include <linux/fdtable.h>
22#include <linux/ratelimit.h>
Amir Goldstein3a1e8192017-03-30 15:22:16 +030023#include <linux/exportfs.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020024#include "overlayfs.h"
25
26#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
27
Miklos Szeredi670c2322018-07-18 15:44:44 +020028static int ovl_ccup_set(const char *buf, const struct kernel_param *param)
David Howellsfb5bb2c32015-07-07 15:04:44 +010029{
Miklos Szeredi670c2322018-07-18 15:44:44 +020030 pr_warn("overlayfs: \"check_copy_up\" module option is obsolete\n");
David Howellsfb5bb2c32015-07-07 15:04:44 +010031 return 0;
32}
33
Miklos Szeredi670c2322018-07-18 15:44:44 +020034static int ovl_ccup_get(char *buf, const struct kernel_param *param)
David Howellsfb5bb2c32015-07-07 15:04:44 +010035{
Miklos Szeredi670c2322018-07-18 15:44:44 +020036 return sprintf(buf, "N\n");
David Howellsfb5bb2c32015-07-07 15:04:44 +010037}
38
Miklos Szeredi670c2322018-07-18 15:44:44 +020039module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644);
40MODULE_PARM_DESC(ovl_check_copy_up, "Obsolete; does nothing");
41
Miklos Szeredie9be9d52014-10-24 00:14:38 +020042int ovl_copy_xattr(struct dentry *old, struct dentry *new)
43{
Vito Caputoe4ad29f2015-10-24 07:19:46 -050044 ssize_t list_size, size, value_size = 0;
45 char *buf, *name, *value = NULL;
46 int uninitialized_var(error);
Miklos Szeredi8b326c62016-09-16 14:12:11 +020047 size_t slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020048
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +020049 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
50 !(new->d_inode->i_opflags & IOP_XATTR))
Miklos Szeredie9be9d52014-10-24 00:14:38 +020051 return 0;
52
53 list_size = vfs_listxattr(old, NULL, 0);
54 if (list_size <= 0) {
55 if (list_size == -EOPNOTSUPP)
56 return 0;
57 return list_size;
58 }
59
60 buf = kzalloc(list_size, GFP_KERNEL);
61 if (!buf)
62 return -ENOMEM;
63
Miklos Szeredie9be9d52014-10-24 00:14:38 +020064 list_size = vfs_listxattr(old, buf, list_size);
65 if (list_size <= 0) {
66 error = list_size;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050067 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020068 }
69
Miklos Szeredi8b326c62016-09-16 14:12:11 +020070 for (name = buf; list_size; name += slen) {
71 slen = strnlen(name, list_size) + 1;
72
73 /* underlying fs providing us with an broken xattr list? */
74 if (WARN_ON(slen > list_size)) {
75 error = -EIO;
76 break;
77 }
78 list_size -= slen;
79
Miklos Szeredi09562542016-08-08 15:08:49 +020080 if (ovl_is_private_xattr(name))
81 continue;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050082retry:
83 size = vfs_getxattr(old, name, value, value_size);
84 if (size == -ERANGE)
85 size = vfs_getxattr(old, name, NULL, 0);
86
Miklos Szeredi97daf8b2015-11-10 17:08:41 +010087 if (size < 0) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +020088 error = size;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050089 break;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020090 }
Vito Caputoe4ad29f2015-10-24 07:19:46 -050091
92 if (size > value_size) {
93 void *new;
94
95 new = krealloc(value, size, GFP_KERNEL);
96 if (!new) {
97 error = -ENOMEM;
98 break;
99 }
100 value = new;
101 value_size = size;
102 goto retry;
103 }
104
Vivek Goyal121ab822016-07-13 10:44:49 -0400105 error = security_inode_copy_up_xattr(name);
106 if (error < 0 && error != -EOPNOTSUPP)
107 break;
108 if (error == 1) {
109 error = 0;
110 continue; /* Discard */
111 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200112 error = vfs_setxattr(new, name, value, size, 0);
113 if (error)
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500114 break;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200115 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200116 kfree(value);
117out:
118 kfree(buf);
119 return error;
120}
121
122static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
123{
124 struct file *old_file;
125 struct file *new_file;
126 loff_t old_pos = 0;
127 loff_t new_pos = 0;
128 int error = 0;
129
130 if (len == 0)
131 return 0;
132
David Howells04803342015-09-18 11:45:12 +0100133 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200134 if (IS_ERR(old_file))
135 return PTR_ERR(old_file);
136
David Howells04803342015-09-18 11:45:12 +0100137 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200138 if (IS_ERR(new_file)) {
139 error = PTR_ERR(new_file);
140 goto out_fput;
141 }
142
Amir Goldstein2ea98462016-09-23 11:38:12 +0300143 /* Try to use clone_file_range to clone up within the same fs */
Amir Goldsteina7253562018-09-18 16:34:34 +0300144 error = do_clone_file_range(old_file, 0, new_file, 0, len);
Amir Goldstein2ea98462016-09-23 11:38:12 +0300145 if (!error)
146 goto out;
147 /* Couldn't clone, so now we try to copy the data */
148 error = 0;
149
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200150 /* FIXME: copy up sparse files efficiently */
151 while (len) {
152 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
153 long bytes;
154
155 if (len < this_len)
156 this_len = len;
157
158 if (signal_pending_state(TASK_KILLABLE, current)) {
159 error = -EINTR;
160 break;
161 }
162
163 bytes = do_splice_direct(old_file, &old_pos,
164 new_file, &new_pos,
165 this_len, SPLICE_F_MOVE);
166 if (bytes <= 0) {
167 error = bytes;
168 break;
169 }
170 WARN_ON(old_pos != new_pos);
171
172 len -= bytes;
173 }
Amir Goldstein2ea98462016-09-23 11:38:12 +0300174out:
Miklos Szeredi641089c2016-10-31 14:42:14 +0100175 if (!error)
176 error = vfs_fsync(new_file, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200177 fput(new_file);
178out_fput:
179 fput(old_file);
180 return error;
181}
182
Vivek Goyal0c288872018-05-11 11:49:28 -0400183static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
184{
185 struct iattr attr = {
186 .ia_valid = ATTR_SIZE,
187 .ia_size = stat->size,
188 };
189
190 return notify_change(upperdentry, &attr, NULL);
191}
192
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200193static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
194{
195 struct iattr attr = {
196 .ia_valid =
197 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
198 .ia_atime = stat->atime,
199 .ia_mtime = stat->mtime,
200 };
201
202 return notify_change(upperdentry, &attr, NULL);
203}
204
205int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
206{
207 int err = 0;
208
209 if (!S_ISLNK(stat->mode)) {
210 struct iattr attr = {
211 .ia_valid = ATTR_MODE,
212 .ia_mode = stat->mode,
213 };
214 err = notify_change(upperdentry, &attr, NULL);
215 }
216 if (!err) {
217 struct iattr attr = {
218 .ia_valid = ATTR_UID | ATTR_GID,
219 .ia_uid = stat->uid,
220 .ia_gid = stat->gid,
221 };
222 err = notify_change(upperdentry, &attr, NULL);
223 }
224 if (!err)
225 ovl_set_timestamps(upperdentry, stat);
226
227 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200228}
229
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200230struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper)
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300231{
232 struct ovl_fh *fh;
233 int fh_type, fh_len, dwords;
234 void *buf;
235 int buflen = MAX_HANDLE_SZ;
Amir Goldstein05122442018-01-11 08:25:32 +0200236 uuid_t *uuid = &real->d_sb->s_uuid;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300237
Michal Hocko0ee931c2017-09-13 16:28:29 -0700238 buf = kmalloc(buflen, GFP_KERNEL);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300239 if (!buf)
240 return ERR_PTR(-ENOMEM);
241
242 /*
243 * We encode a non-connectable file handle for non-dir, because we
244 * only need to find the lower inode number and we don't want to pay
245 * the price or reconnecting the dentry.
246 */
247 dwords = buflen >> 2;
Amir Goldstein05122442018-01-11 08:25:32 +0200248 fh_type = exportfs_encode_fh(real, buf, &dwords, 0);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300249 buflen = (dwords << 2);
250
251 fh = ERR_PTR(-EIO);
252 if (WARN_ON(fh_type < 0) ||
253 WARN_ON(buflen > MAX_HANDLE_SZ) ||
254 WARN_ON(fh_type == FILEID_INVALID))
255 goto out;
256
257 BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255);
258 fh_len = offsetof(struct ovl_fh, fid) + buflen;
259 fh = kmalloc(fh_len, GFP_KERNEL);
260 if (!fh) {
261 fh = ERR_PTR(-ENOMEM);
262 goto out;
263 }
264
265 fh->version = OVL_FH_VERSION;
266 fh->magic = OVL_FH_MAGIC;
267 fh->type = fh_type;
268 fh->flags = OVL_FH_FLAG_CPU_ENDIAN;
Amir Goldstein54fb3472017-06-21 15:28:38 +0300269 /*
270 * When we will want to decode an overlay dentry from this handle
271 * and all layers are on the same fs, if we get a disconncted real
272 * dentry when we decode fid, the only way to tell if we should assign
273 * it to upperdentry or to lowerstack is by checking this flag.
274 */
275 if (is_upper)
276 fh->flags |= OVL_FH_FLAG_PATH_UPPER;
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300277 fh->len = fh_len;
278 fh->uuid = *uuid;
279 memcpy(fh->fid, buf, buflen);
280
281out:
282 kfree(buf);
283 return fh;
284}
285
Amir Goldstein9678e632018-01-03 19:34:45 +0200286int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
287 struct dentry *upper)
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300288{
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300289 const struct ovl_fh *fh = NULL;
290 int err;
291
292 /*
293 * When lower layer doesn't support export operations store a 'null' fh,
294 * so we can use the overlay.origin xattr to distignuish between a copy
295 * up and a pure upper inode.
296 */
Amir Goldstein02bcd152017-06-21 15:28:36 +0300297 if (ovl_can_decode_fh(lower->d_sb)) {
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200298 fh = ovl_encode_real_fh(lower, false);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300299 if (IS_ERR(fh))
300 return PTR_ERR(fh);
301 }
302
Miklos Szeredi6266d462017-05-18 16:11:24 +0200303 /*
304 * Do not fail when upper doesn't support xattrs.
305 */
306 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh,
307 fh ? fh->len : 0, 0);
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300308 kfree(fh);
309
310 return err;
311}
312
Amir Goldstein016b7202018-01-11 14:01:08 +0200313/* Store file handle of @upper dir in @index dir entry */
314static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index)
315{
316 const struct ovl_fh *fh;
317 int err;
318
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200319 fh = ovl_encode_real_fh(upper, true);
Amir Goldstein016b7202018-01-11 14:01:08 +0200320 if (IS_ERR(fh))
321 return PTR_ERR(fh);
322
323 err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh, fh->len, 0);
324
325 kfree(fh);
326 return err;
327}
328
329/*
330 * Create and install index entry.
331 *
332 * Caller must hold i_mutex on indexdir.
333 */
334static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
335 struct dentry *upper)
336{
337 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
338 struct inode *dir = d_inode(indexdir);
339 struct dentry *index = NULL;
340 struct dentry *temp = NULL;
341 struct qstr name = { };
342 int err;
343
344 /*
345 * For now this is only used for creating index entry for directories,
346 * because non-dir are copied up directly to index and then hardlinked
347 * to upper dir.
348 *
349 * TODO: implement create index for non-dir, so we can call it when
350 * encoding file handle for non-dir in case index does not exist.
351 */
352 if (WARN_ON(!d_is_dir(dentry)))
353 return -EIO;
354
355 /* Directory not expected to be indexed before copy up */
356 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
357 return -EIO;
358
359 err = ovl_get_index_name(origin, &name);
360 if (err)
361 return err;
362
Amir Goldstein137ec522018-05-16 17:51:25 +0300363 temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
Miklos Szeredib148cba2018-05-31 11:06:11 +0200364 err = PTR_ERR(temp);
Amir Goldstein016b7202018-01-11 14:01:08 +0200365 if (IS_ERR(temp))
Miklos Szeredib148cba2018-05-31 11:06:11 +0200366 goto free_name;
Amir Goldstein016b7202018-01-11 14:01:08 +0200367
Amir Goldstein016b7202018-01-11 14:01:08 +0200368 err = ovl_set_upper_fh(upper, temp);
369 if (err)
Miklos Szeredib148cba2018-05-31 11:06:11 +0200370 goto out;
Amir Goldstein016b7202018-01-11 14:01:08 +0200371
372 index = lookup_one_len(name.name, indexdir, name.len);
373 if (IS_ERR(index)) {
374 err = PTR_ERR(index);
375 } else {
376 err = ovl_do_rename(dir, temp, dir, index, 0);
377 dput(index);
378 }
Amir Goldstein016b7202018-01-11 14:01:08 +0200379out:
Miklos Szeredib148cba2018-05-31 11:06:11 +0200380 if (err)
381 ovl_cleanup(dir, temp);
Amir Goldstein016b7202018-01-11 14:01:08 +0200382 dput(temp);
Miklos Szeredib148cba2018-05-31 11:06:11 +0200383free_name:
Amir Goldstein016b7202018-01-11 14:01:08 +0200384 kfree(name.name);
385 return err;
Amir Goldstein016b7202018-01-11 14:01:08 +0200386}
387
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200388struct ovl_copy_up_ctx {
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200389 struct dentry *parent;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200390 struct dentry *dentry;
391 struct path lowerpath;
392 struct kstat stat;
393 struct kstat pstat;
394 const char *link;
Amir Goldstein59be0972017-06-20 15:25:46 +0300395 struct dentry *destdir;
396 struct qstr destname;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200397 struct dentry *workdir;
Amir Goldstein59be0972017-06-20 15:25:46 +0300398 bool origin;
Amir Goldstein016b7202018-01-11 14:01:08 +0200399 bool indexed;
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400400 bool metacopy;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200401};
402
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300403static int ovl_link_up(struct ovl_copy_up_ctx *c)
404{
405 int err;
406 struct dentry *upper;
407 struct dentry *upperdir = ovl_dentry_upper(c->parent);
408 struct inode *udir = d_inode(upperdir);
409
410 /* Mark parent "impure" because it may now contain non-pure upper */
411 err = ovl_set_impure(c->parent, upperdir);
412 if (err)
413 return err;
414
415 err = ovl_set_nlink_lower(c->dentry);
416 if (err)
417 return err;
418
419 inode_lock_nested(udir, I_MUTEX_PARENT);
420 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
421 c->dentry->d_name.len);
422 err = PTR_ERR(upper);
423 if (!IS_ERR(upper)) {
Amir Goldstein6cf00762018-05-16 17:04:00 +0300424 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300425 dput(upper);
426
427 if (!err) {
428 /* Restore timestamps on parent (best effort) */
429 ovl_set_timestamps(upperdir, &c->pstat);
430 ovl_dentry_set_upper_alias(c->dentry);
431 }
432 }
433 inode_unlock(udir);
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300434 if (err)
435 return err;
436
437 err = ovl_set_nlink_upper(c->dentry);
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300438
439 return err;
440}
441
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200442static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
Amir Goldstein02209d12017-05-19 15:16:21 +0300443{
444 int err;
445
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200446 err = ovl_copy_xattr(c->lowerpath.dentry, temp);
Amir Goldstein02209d12017-05-19 15:16:21 +0300447 if (err)
448 return err;
449
Amir Goldstein02209d12017-05-19 15:16:21 +0300450 /*
451 * Store identifier of lower inode in upper inode xattr to
452 * allow lookup of the copy up origin inode.
453 *
454 * Don't set origin when we are breaking the association with a lower
455 * hard link.
456 */
Amir Goldstein59be0972017-06-20 15:25:46 +0300457 if (c->origin) {
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200458 err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
Amir Goldstein02209d12017-05-19 15:16:21 +0300459 if (err)
460 return err;
461 }
462
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400463 if (S_ISREG(c->stat.mode) && !c->metacopy) {
Vivek Goyal4f93b422018-05-11 11:49:30 -0400464 struct path upperpath, datapath;
Vivek Goyalbd64e572018-05-11 11:49:27 -0400465
466 ovl_path_upper(c->dentry, &upperpath);
467 BUG_ON(upperpath.dentry != NULL);
468 upperpath.dentry = temp;
469
Vivek Goyal4f93b422018-05-11 11:49:30 -0400470 ovl_path_lowerdata(c->dentry, &datapath);
471 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
Vivek Goyalbd64e572018-05-11 11:49:27 -0400472 if (err)
473 return err;
474 }
475
Vivek Goyal0c288872018-05-11 11:49:28 -0400476 if (c->metacopy) {
477 err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
478 NULL, 0, -EOPNOTSUPP);
479 if (err)
480 return err;
481 }
482
Vivek Goyalbd64e572018-05-11 11:49:27 -0400483 inode_lock(temp->d_inode);
Vivek Goyal0c288872018-05-11 11:49:28 -0400484 if (c->metacopy)
485 err = ovl_set_size(temp, &c->stat);
486 if (!err)
487 err = ovl_set_attr(temp, &c->stat);
Vivek Goyalbd64e572018-05-11 11:49:27 -0400488 inode_unlock(temp->d_inode);
489
490 return err;
Amir Goldstein02209d12017-05-19 15:16:21 +0300491}
492
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300493static struct dentry *ovl_get_workdir_temp(struct ovl_copy_up_ctx *c)
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200494{
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300495 int err;
496 struct dentry *temp;
497 const struct cred *old_creds = NULL;
498 struct cred *new_creds = NULL;
499 struct ovl_cattr cattr = {
500 /* Can't properly set mode on creation because of the umask */
501 .mode = c->stat.mode & S_IFMT,
502 .rdev = c->stat.rdev,
503 .link = c->link
504 };
505
506 err = security_inode_copy_up(c->dentry, &new_creds);
507 if (err < 0)
508 return ERR_PTR(err);
509
510 if (new_creds)
511 old_creds = override_creds(new_creds);
512
513 temp = ovl_create_temp(c->workdir, &cattr);
514
515 if (new_creds) {
516 revert_creds(old_creds);
517 put_cred(new_creds);
518 }
519
520 return temp;
521}
522
523/*
524 * Move temp file from workdir into place on upper dir.
525 * Used when copying up directories and when upper fs doesn't support O_TMPFILE.
526 *
527 * Caller must hold ovl_lock_rename_workdir().
528 */
529static int ovl_rename_temp(struct ovl_copy_up_ctx *c, struct dentry *temp,
530 struct dentry **newdentry)
531{
532 int err;
533 struct dentry *upper;
534 struct inode *udir = d_inode(c->destdir);
535
536 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
537 if (IS_ERR(upper))
538 return PTR_ERR(upper);
539
540 err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0);
541 if (!err)
542 *newdentry = dget(temp);
543 dput(upper);
544
545 return err;
546}
547
548/*
549 * Copyup using workdir to prepare temp file. Used when copying up directories,
550 * special files or when upper fs doesn't support O_TMPFILE.
551 */
552static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
553{
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300554 struct inode *inode;
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200555 struct dentry *newdentry = NULL;
Miklos Szeredib148cba2018-05-31 11:06:11 +0200556 struct dentry *temp;
Miklos Szeredi7d90b852017-07-04 22:03:18 +0200557 int err;
558
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300559 err = ovl_lock_rename_workdir(c->workdir, c->destdir);
560 if (err)
561 return err;
562
563 temp = ovl_get_workdir_temp(c);
564 err = PTR_ERR(temp);
565 if (IS_ERR(temp))
566 goto unlock;
567
568 err = ovl_copy_up_inode(c, temp);
569 if (err)
570 goto cleanup;
571
572 if (S_ISDIR(c->stat.mode) && c->indexed) {
573 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
574 if (err)
575 goto cleanup;
576 }
577
578 err = ovl_rename_temp(c, temp, &newdentry);
579 if (err)
580 goto cleanup;
581
582 if (!c->metacopy)
583 ovl_set_upperdata(d_inode(c->dentry));
584 inode = d_inode(c->dentry);
585 ovl_inode_update(inode, newdentry);
586 if (S_ISDIR(inode->i_mode))
587 ovl_set_flag(OVL_WHITEOUTS, inode);
588out_dput:
589 dput(temp);
590unlock:
591 unlock_rename(c->workdir, c->destdir);
592
593 return err;
594
595cleanup:
596 ovl_cleanup(d_inode(c->workdir), temp);
597 goto out_dput;
598}
599
600static struct dentry *ovl_get_tmpfile(struct ovl_copy_up_ctx *c)
601{
602 int err;
603 struct dentry *temp;
604 const struct cred *old_creds = NULL;
605 struct cred *new_creds = NULL;
606
607 err = security_inode_copy_up(c->dentry, &new_creds);
608 if (err < 0)
609 return ERR_PTR(err);
610
611 if (new_creds)
612 old_creds = override_creds(new_creds);
613
614 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
615
616 if (new_creds) {
617 revert_creds(old_creds);
618 put_cred(new_creds);
619 }
620
621 return temp;
622}
623
624/* Link O_TMPFILE into place on upper dir */
625static int ovl_link_tmpfile(struct ovl_copy_up_ctx *c, struct dentry *temp,
626 struct dentry **newdentry)
627{
628 int err;
629 struct dentry *upper;
630 struct inode *udir = d_inode(c->destdir);
631
632 inode_lock_nested(udir, I_MUTEX_PARENT);
633
634 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
635 err = PTR_ERR(upper);
636 if (IS_ERR(upper))
637 goto unlock;
638
639 err = ovl_do_link(temp, udir, upper);
640 if (!err)
641 *newdentry = dget(upper);
642 dput(upper);
643
644unlock:
645 inode_unlock(udir);
646
647 return err;
648}
649
650/* Copyup using O_TMPFILE which does not require cross dir locking */
651static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
652{
653 struct dentry *newdentry = NULL;
654 struct dentry *temp;
655 int err;
656
Miklos Szeredib148cba2018-05-31 11:06:11 +0200657 temp = ovl_get_tmpfile(c);
658 if (IS_ERR(temp))
659 return PTR_ERR(temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200660
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200661 err = ovl_copy_up_inode(c, temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200662 if (err)
Miklos Szeredib148cba2018-05-31 11:06:11 +0200663 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200664
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300665 err = ovl_link_tmpfile(c, temp, &newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200666 if (err)
Miklos Szeredib148cba2018-05-31 11:06:11 +0200667 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200668
Vivek Goyal0c288872018-05-11 11:49:28 -0400669 if (!c->metacopy)
670 ovl_set_upperdata(d_inode(c->dentry));
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300671 ovl_inode_update(d_inode(c->dentry), newdentry);
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300672
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200673out:
Miklos Szeredie85f82f2017-06-28 13:41:22 +0200674 dput(temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200675 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200676}
677
678/*
679 * Copy up a single dentry
680 *
Miklos Szeredia6c60652016-12-16 11:02:56 +0100681 * All renames start with copy up of source if necessary. The actual
682 * rename will only proceed once the copy up was successful. Copy up uses
683 * upper parent i_mutex for exclusion. Since rename can change d_parent it
684 * is possible that the copy up will lock the old parent. At that point
685 * the file will have already been copied up anyway.
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200686 */
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200687static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200688{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200689 int err;
Miklos Szeredi23f0ab12017-07-04 22:03:18 +0200690 struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
Amir Goldstein016b7202018-01-11 14:01:08 +0200691 bool to_index = false;
Amir Goldstein59be0972017-06-20 15:25:46 +0300692
Amir Goldstein016b7202018-01-11 14:01:08 +0200693 /*
694 * Indexed non-dir is copied up directly to the index entry and then
695 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
696 * then index entry is created and then copied up dir installed.
697 * Copying dir up to indexdir instead of workdir simplifies locking.
698 */
699 if (ovl_need_index(c->dentry)) {
700 c->indexed = true;
701 if (S_ISDIR(c->stat.mode))
702 c->workdir = ovl_indexdir(c->dentry->d_sb);
703 else
704 to_index = true;
705 }
706
707 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
Amir Goldstein59be0972017-06-20 15:25:46 +0300708 c->origin = true;
709
Amir Goldstein016b7202018-01-11 14:01:08 +0200710 if (to_index) {
Amir Goldstein59be0972017-06-20 15:25:46 +0300711 c->destdir = ovl_indexdir(c->dentry->d_sb);
712 err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
713 if (err)
714 return err;
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300715 } else if (WARN_ON(!c->parent)) {
716 /* Disconnected dentry must be copied up to index dir */
717 return -EIO;
Amir Goldstein59be0972017-06-20 15:25:46 +0300718 } else {
719 /*
720 * Mark parent "impure" because it may now contain non-pure
721 * upper
722 */
723 err = ovl_set_impure(c->parent, c->destdir);
724 if (err)
725 return err;
726 }
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300727
Amir Goldstein01ad3eb2017-01-17 06:34:57 +0200728 /* Should we copyup with O_TMPFILE or with workdir? */
Amir Goldsteinb10cdcd2018-10-08 07:25:23 +0300729 if (S_ISREG(c->stat.mode) && ofs->tmpfile)
730 err = ovl_copy_up_tmpfile(c);
731 else
732 err = ovl_copy_up_workdir(c);
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300733 if (err)
734 goto out;
735
736 if (c->indexed)
Amir Goldstein016b7202018-01-11 14:01:08 +0200737 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
738
739 if (to_index) {
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300740 /* Initialize nlink for copy up of disconnected dentry */
741 err = ovl_set_nlink_upper(c->dentry);
742 } else {
Amir Goldstein59be0972017-06-20 15:25:46 +0300743 struct inode *udir = d_inode(c->destdir);
744
745 /* Restore timestamps on parent (best effort) */
746 inode_lock(udir);
747 ovl_set_timestamps(c->destdir, &c->pstat);
748 inode_unlock(udir);
749
750 ovl_dentry_set_upper_alias(c->dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200751 }
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200752
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300753out:
754 if (to_index)
755 kfree(c->destname.name);
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200756 return err;
757}
758
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400759static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
760 int flags)
761{
762 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
763
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400764 if (!ofs->config.metacopy)
765 return false;
766
767 if (!S_ISREG(mode))
768 return false;
769
770 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
771 return false;
772
773 return true;
774}
775
Vivek Goyal0c288872018-05-11 11:49:28 -0400776/* Copy up data of an inode which was copied up metadata only in the past. */
777static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
778{
Vivek Goyal4f93b422018-05-11 11:49:30 -0400779 struct path upperpath, datapath;
Vivek Goyal0c288872018-05-11 11:49:28 -0400780 int err;
781
782 ovl_path_upper(c->dentry, &upperpath);
783 if (WARN_ON(upperpath.dentry == NULL))
784 return -EIO;
785
Vivek Goyal4f93b422018-05-11 11:49:30 -0400786 ovl_path_lowerdata(c->dentry, &datapath);
787 if (WARN_ON(datapath.dentry == NULL))
788 return -EIO;
789
790 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
Vivek Goyal0c288872018-05-11 11:49:28 -0400791 if (err)
792 return err;
793
794 err = vfs_removexattr(upperpath.dentry, OVL_XATTR_METACOPY);
795 if (err)
796 return err;
797
798 ovl_set_upperdata(d_inode(c->dentry));
799 return err;
800}
801
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200802static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
803 int flags)
804{
805 int err;
806 DEFINE_DELAYED_CALL(done);
807 struct path parentpath;
808 struct ovl_copy_up_ctx ctx = {
809 .parent = parent,
810 .dentry = dentry,
811 .workdir = ovl_workdir(dentry),
812 };
813
814 if (WARN_ON(!ctx.workdir))
815 return -EROFS;
816
817 ovl_path_lower(dentry, &ctx.lowerpath);
818 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
819 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
820 if (err)
821 return err;
822
Vivek Goyal44d5bf12018-05-11 11:49:27 -0400823 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
824
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300825 if (parent) {
826 ovl_path_upper(parent, &parentpath);
827 ctx.destdir = parentpath.dentry;
828 ctx.destname = dentry->d_name;
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200829
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300830 err = vfs_getattr(&parentpath, &ctx.pstat,
831 STATX_ATIME | STATX_MTIME,
832 AT_STATX_SYNC_AS_STAT);
833 if (err)
834 return err;
835 }
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200836
837 /* maybe truncate regular file. this has no effect on dirs */
838 if (flags & O_TRUNC)
839 ctx.stat.size = 0;
840
841 if (S_ISLNK(ctx.stat.mode)) {
842 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
843 if (IS_ERR(ctx.link))
844 return PTR_ERR(ctx.link);
845 }
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200846
Vivek Goyal0c288872018-05-11 11:49:28 -0400847 err = ovl_copy_up_start(dentry, flags);
Miklos Szeredifd210b72017-07-04 22:03:18 +0200848 /* err < 0: interrupted, err > 0: raced with another copy-up */
849 if (unlikely(err)) {
850 if (err > 0)
851 err = 0;
852 } else {
Amir Goldstein59be0972017-06-20 15:25:46 +0300853 if (!ovl_dentry_upper(dentry))
854 err = ovl_do_copy_up(&ctx);
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300855 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
Amir Goldsteinf4439de2017-07-04 22:04:06 +0300856 err = ovl_link_up(&ctx);
Vivek Goyal0c288872018-05-11 11:49:28 -0400857 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
858 err = ovl_copy_up_meta_inode_data(&ctx);
Miklos Szeredifd210b72017-07-04 22:03:18 +0200859 ovl_copy_up_end(dentry);
860 }
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200861 do_delayed_call(&done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200862
863 return err;
864}
865
Amir Goldstein9aba6522016-11-12 21:36:03 +0200866int ovl_copy_up_flags(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200867{
Vivek Goyal8eac98b2016-09-06 13:40:32 -0400868 int err = 0;
869 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300870 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
871
872 /*
873 * With NFS export, copy up can get called for a disconnected non-dir.
874 * In this case, we will copy up lower inode to index dir without
875 * linking it to upper dir.
876 */
877 if (WARN_ON(disconnected && d_is_dir(dentry)))
878 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200879
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200880 while (!err) {
881 struct dentry *next;
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300882 struct dentry *parent = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200883
Vivek Goyal0c288872018-05-11 11:49:28 -0400884 if (ovl_already_copied_up(dentry, flags))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200885 break;
886
887 next = dget(dentry);
888 /* find the topmost dentry not yet copied up */
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300889 for (; !disconnected;) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200890 parent = dget_parent(next);
891
Amir Goldstein59be0972017-06-20 15:25:46 +0300892 if (ovl_dentry_upper(parent))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200893 break;
894
895 dput(next);
896 next = parent;
897 }
898
Miklos Szeredia6fb2352017-07-04 22:03:18 +0200899 err = ovl_copy_up_one(parent, next, flags);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200900
901 dput(parent);
902 dput(next);
903 }
Vivek Goyal8eac98b2016-09-06 13:40:32 -0400904 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200905
906 return err;
907}
Amir Goldstein9aba6522016-11-12 21:36:03 +0200908
Vivek Goyald6eac032018-05-11 11:49:27 -0400909static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
910{
911 /* Copy up of disconnected dentry does not set upper alias */
Vivek Goyal0c288872018-05-11 11:49:28 -0400912 if (ovl_already_copied_up(dentry, flags))
Vivek Goyald6eac032018-05-11 11:49:27 -0400913 return false;
914
915 if (special_file(d_inode(dentry)->i_mode))
916 return false;
917
Vivek Goyal0c288872018-05-11 11:49:28 -0400918 if (!ovl_open_flags_need_copy_up(flags))
Vivek Goyald6eac032018-05-11 11:49:27 -0400919 return false;
920
921 return true;
922}
923
924int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
925{
926 int err = 0;
927
928 if (ovl_open_need_copy_up(dentry, file_flags)) {
929 err = ovl_want_write(dentry);
930 if (!err) {
931 err = ovl_copy_up_flags(dentry, file_flags);
932 ovl_drop_write(dentry);
933 }
934 }
935
936 return err;
937}
938
Vivek Goyald1e6f6a2018-05-11 11:49:33 -0400939int ovl_copy_up_with_data(struct dentry *dentry)
940{
941 return ovl_copy_up_flags(dentry, O_WRONLY);
942}
943
Amir Goldstein9aba6522016-11-12 21:36:03 +0200944int ovl_copy_up(struct dentry *dentry)
945{
946 return ovl_copy_up_flags(dentry, 0);
947}