Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1 | /* |
| 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 Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 10 | #include <linux/module.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 11 | #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 Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 18 | #include <linux/sched/signal.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 19 | #include <linux/cred.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 20 | #include <linux/namei.h> |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 21 | #include <linux/fdtable.h> |
| 22 | #include <linux/ratelimit.h> |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 23 | #include <linux/exportfs.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 24 | #include "overlayfs.h" |
| 25 | |
| 26 | #define OVL_COPY_UP_CHUNK_SIZE (1 << 20) |
| 27 | |
Miklos Szeredi | 670c232 | 2018-07-18 15:44:44 +0200 | [diff] [blame] | 28 | static int ovl_ccup_set(const char *buf, const struct kernel_param *param) |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 29 | { |
Miklos Szeredi | 670c232 | 2018-07-18 15:44:44 +0200 | [diff] [blame] | 30 | pr_warn("overlayfs: \"check_copy_up\" module option is obsolete\n"); |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 31 | return 0; |
| 32 | } |
| 33 | |
Miklos Szeredi | 670c232 | 2018-07-18 15:44:44 +0200 | [diff] [blame] | 34 | static int ovl_ccup_get(char *buf, const struct kernel_param *param) |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 35 | { |
Miklos Szeredi | 670c232 | 2018-07-18 15:44:44 +0200 | [diff] [blame] | 36 | return sprintf(buf, "N\n"); |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Miklos Szeredi | 670c232 | 2018-07-18 15:44:44 +0200 | [diff] [blame] | 39 | module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644); |
| 40 | MODULE_PARM_DESC(ovl_check_copy_up, "Obsolete; does nothing"); |
| 41 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 42 | int ovl_copy_xattr(struct dentry *old, struct dentry *new) |
| 43 | { |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 44 | ssize_t list_size, size, value_size = 0; |
| 45 | char *buf, *name, *value = NULL; |
| 46 | int uninitialized_var(error); |
Miklos Szeredi | 8b326c6 | 2016-09-16 14:12:11 +0200 | [diff] [blame] | 47 | size_t slen; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 48 | |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 49 | if (!(old->d_inode->i_opflags & IOP_XATTR) || |
| 50 | !(new->d_inode->i_opflags & IOP_XATTR)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 51 | 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 Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 64 | list_size = vfs_listxattr(old, buf, list_size); |
| 65 | if (list_size <= 0) { |
| 66 | error = list_size; |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 67 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Miklos Szeredi | 8b326c6 | 2016-09-16 14:12:11 +0200 | [diff] [blame] | 70 | 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 Szeredi | 0956254 | 2016-08-08 15:08:49 +0200 | [diff] [blame] | 80 | if (ovl_is_private_xattr(name)) |
| 81 | continue; |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 82 | retry: |
| 83 | size = vfs_getxattr(old, name, value, value_size); |
| 84 | if (size == -ERANGE) |
| 85 | size = vfs_getxattr(old, name, NULL, 0); |
| 86 | |
Miklos Szeredi | 97daf8b | 2015-11-10 17:08:41 +0100 | [diff] [blame] | 87 | if (size < 0) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 88 | error = size; |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 89 | break; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 90 | } |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 91 | |
| 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 Goyal | 121ab82 | 2016-07-13 10:44:49 -0400 | [diff] [blame] | 105 | 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 Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 112 | error = vfs_setxattr(new, name, value, size, 0); |
| 113 | if (error) |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 114 | break; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 115 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 116 | kfree(value); |
| 117 | out: |
| 118 | kfree(buf); |
| 119 | return error; |
| 120 | } |
| 121 | |
| 122 | static 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 Howells | 0480334 | 2015-09-18 11:45:12 +0100 | [diff] [blame] | 133 | old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 134 | if (IS_ERR(old_file)) |
| 135 | return PTR_ERR(old_file); |
| 136 | |
David Howells | 0480334 | 2015-09-18 11:45:12 +0100 | [diff] [blame] | 137 | new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 138 | if (IS_ERR(new_file)) { |
| 139 | error = PTR_ERR(new_file); |
| 140 | goto out_fput; |
| 141 | } |
| 142 | |
Amir Goldstein | 2ea9846 | 2016-09-23 11:38:12 +0300 | [diff] [blame] | 143 | /* Try to use clone_file_range to clone up within the same fs */ |
| 144 | error = vfs_clone_file_range(old_file, 0, new_file, 0, len); |
| 145 | if (!error) |
| 146 | goto out; |
| 147 | /* Couldn't clone, so now we try to copy the data */ |
| 148 | error = 0; |
| 149 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 150 | /* 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 Goldstein | 2ea9846 | 2016-09-23 11:38:12 +0300 | [diff] [blame] | 174 | out: |
Miklos Szeredi | 641089c | 2016-10-31 14:42:14 +0100 | [diff] [blame] | 175 | if (!error) |
| 176 | error = vfs_fsync(new_file, 0); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 177 | fput(new_file); |
| 178 | out_fput: |
| 179 | fput(old_file); |
| 180 | return error; |
| 181 | } |
| 182 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 183 | static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat) |
| 184 | { |
| 185 | struct iattr attr = { |
| 186 | .ia_valid = |
| 187 | ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET, |
| 188 | .ia_atime = stat->atime, |
| 189 | .ia_mtime = stat->mtime, |
| 190 | }; |
| 191 | |
| 192 | return notify_change(upperdentry, &attr, NULL); |
| 193 | } |
| 194 | |
| 195 | int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat) |
| 196 | { |
| 197 | int err = 0; |
| 198 | |
| 199 | if (!S_ISLNK(stat->mode)) { |
| 200 | struct iattr attr = { |
| 201 | .ia_valid = ATTR_MODE, |
| 202 | .ia_mode = stat->mode, |
| 203 | }; |
| 204 | err = notify_change(upperdentry, &attr, NULL); |
| 205 | } |
| 206 | if (!err) { |
| 207 | struct iattr attr = { |
| 208 | .ia_valid = ATTR_UID | ATTR_GID, |
| 209 | .ia_uid = stat->uid, |
| 210 | .ia_gid = stat->gid, |
| 211 | }; |
| 212 | err = notify_change(upperdentry, &attr, NULL); |
| 213 | } |
| 214 | if (!err) |
| 215 | ovl_set_timestamps(upperdentry, stat); |
| 216 | |
| 217 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Amir Goldstein | 5b2cccd | 2018-02-02 10:42:03 +0200 | [diff] [blame] | 220 | struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper) |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 221 | { |
| 222 | struct ovl_fh *fh; |
| 223 | int fh_type, fh_len, dwords; |
| 224 | void *buf; |
| 225 | int buflen = MAX_HANDLE_SZ; |
Amir Goldstein | 0512244 | 2018-01-11 08:25:32 +0200 | [diff] [blame] | 226 | uuid_t *uuid = &real->d_sb->s_uuid; |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 227 | |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 228 | buf = kmalloc(buflen, GFP_KERNEL); |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 229 | if (!buf) |
| 230 | return ERR_PTR(-ENOMEM); |
| 231 | |
| 232 | /* |
| 233 | * We encode a non-connectable file handle for non-dir, because we |
| 234 | * only need to find the lower inode number and we don't want to pay |
| 235 | * the price or reconnecting the dentry. |
| 236 | */ |
| 237 | dwords = buflen >> 2; |
Amir Goldstein | 0512244 | 2018-01-11 08:25:32 +0200 | [diff] [blame] | 238 | fh_type = exportfs_encode_fh(real, buf, &dwords, 0); |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 239 | buflen = (dwords << 2); |
| 240 | |
| 241 | fh = ERR_PTR(-EIO); |
| 242 | if (WARN_ON(fh_type < 0) || |
| 243 | WARN_ON(buflen > MAX_HANDLE_SZ) || |
| 244 | WARN_ON(fh_type == FILEID_INVALID)) |
| 245 | goto out; |
| 246 | |
| 247 | BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255); |
| 248 | fh_len = offsetof(struct ovl_fh, fid) + buflen; |
| 249 | fh = kmalloc(fh_len, GFP_KERNEL); |
| 250 | if (!fh) { |
| 251 | fh = ERR_PTR(-ENOMEM); |
| 252 | goto out; |
| 253 | } |
| 254 | |
| 255 | fh->version = OVL_FH_VERSION; |
| 256 | fh->magic = OVL_FH_MAGIC; |
| 257 | fh->type = fh_type; |
| 258 | fh->flags = OVL_FH_FLAG_CPU_ENDIAN; |
Amir Goldstein | 54fb347 | 2017-06-21 15:28:38 +0300 | [diff] [blame] | 259 | /* |
| 260 | * When we will want to decode an overlay dentry from this handle |
| 261 | * and all layers are on the same fs, if we get a disconncted real |
| 262 | * dentry when we decode fid, the only way to tell if we should assign |
| 263 | * it to upperdentry or to lowerstack is by checking this flag. |
| 264 | */ |
| 265 | if (is_upper) |
| 266 | fh->flags |= OVL_FH_FLAG_PATH_UPPER; |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 267 | fh->len = fh_len; |
| 268 | fh->uuid = *uuid; |
| 269 | memcpy(fh->fid, buf, buflen); |
| 270 | |
| 271 | out: |
| 272 | kfree(buf); |
| 273 | return fh; |
| 274 | } |
| 275 | |
Amir Goldstein | 9678e63 | 2018-01-03 19:34:45 +0200 | [diff] [blame] | 276 | int ovl_set_origin(struct dentry *dentry, struct dentry *lower, |
| 277 | struct dentry *upper) |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 278 | { |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 279 | const struct ovl_fh *fh = NULL; |
| 280 | int err; |
| 281 | |
| 282 | /* |
| 283 | * When lower layer doesn't support export operations store a 'null' fh, |
| 284 | * so we can use the overlay.origin xattr to distignuish between a copy |
| 285 | * up and a pure upper inode. |
| 286 | */ |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 287 | if (ovl_can_decode_fh(lower->d_sb)) { |
Amir Goldstein | 5b2cccd | 2018-02-02 10:42:03 +0200 | [diff] [blame] | 288 | fh = ovl_encode_real_fh(lower, false); |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 289 | if (IS_ERR(fh)) |
| 290 | return PTR_ERR(fh); |
| 291 | } |
| 292 | |
Miklos Szeredi | 6266d46 | 2017-05-18 16:11:24 +0200 | [diff] [blame] | 293 | /* |
| 294 | * Do not fail when upper doesn't support xattrs. |
| 295 | */ |
| 296 | err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh, |
| 297 | fh ? fh->len : 0, 0); |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 298 | kfree(fh); |
| 299 | |
| 300 | return err; |
| 301 | } |
| 302 | |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 303 | /* Store file handle of @upper dir in @index dir entry */ |
| 304 | static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index) |
| 305 | { |
| 306 | const struct ovl_fh *fh; |
| 307 | int err; |
| 308 | |
Amir Goldstein | 5b2cccd | 2018-02-02 10:42:03 +0200 | [diff] [blame] | 309 | fh = ovl_encode_real_fh(upper, true); |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 310 | if (IS_ERR(fh)) |
| 311 | return PTR_ERR(fh); |
| 312 | |
| 313 | err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh, fh->len, 0); |
| 314 | |
| 315 | kfree(fh); |
| 316 | return err; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Create and install index entry. |
| 321 | * |
| 322 | * Caller must hold i_mutex on indexdir. |
| 323 | */ |
| 324 | static int ovl_create_index(struct dentry *dentry, struct dentry *origin, |
| 325 | struct dentry *upper) |
| 326 | { |
| 327 | struct dentry *indexdir = ovl_indexdir(dentry->d_sb); |
| 328 | struct inode *dir = d_inode(indexdir); |
| 329 | struct dentry *index = NULL; |
| 330 | struct dentry *temp = NULL; |
| 331 | struct qstr name = { }; |
| 332 | int err; |
| 333 | |
| 334 | /* |
| 335 | * For now this is only used for creating index entry for directories, |
| 336 | * because non-dir are copied up directly to index and then hardlinked |
| 337 | * to upper dir. |
| 338 | * |
| 339 | * TODO: implement create index for non-dir, so we can call it when |
| 340 | * encoding file handle for non-dir in case index does not exist. |
| 341 | */ |
| 342 | if (WARN_ON(!d_is_dir(dentry))) |
| 343 | return -EIO; |
| 344 | |
| 345 | /* Directory not expected to be indexed before copy up */ |
| 346 | if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry)))) |
| 347 | return -EIO; |
| 348 | |
| 349 | err = ovl_get_index_name(origin, &name); |
| 350 | if (err) |
| 351 | return err; |
| 352 | |
Amir Goldstein | 137ec52 | 2018-05-16 17:51:25 +0300 | [diff] [blame] | 353 | temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0)); |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 354 | err = PTR_ERR(temp); |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 355 | if (IS_ERR(temp)) |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 356 | goto free_name; |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 357 | |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 358 | err = ovl_set_upper_fh(upper, temp); |
| 359 | if (err) |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 360 | goto out; |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 361 | |
| 362 | index = lookup_one_len(name.name, indexdir, name.len); |
| 363 | if (IS_ERR(index)) { |
| 364 | err = PTR_ERR(index); |
| 365 | } else { |
| 366 | err = ovl_do_rename(dir, temp, dir, index, 0); |
| 367 | dput(index); |
| 368 | } |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 369 | out: |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 370 | if (err) |
| 371 | ovl_cleanup(dir, temp); |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 372 | dput(temp); |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 373 | free_name: |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 374 | kfree(name.name); |
| 375 | return err; |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 378 | struct ovl_copy_up_ctx { |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 379 | struct dentry *parent; |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 380 | struct dentry *dentry; |
| 381 | struct path lowerpath; |
| 382 | struct kstat stat; |
| 383 | struct kstat pstat; |
| 384 | const char *link; |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 385 | struct dentry *destdir; |
| 386 | struct qstr destname; |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 387 | struct dentry *workdir; |
| 388 | bool tmpfile; |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 389 | bool origin; |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 390 | bool indexed; |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 391 | }; |
| 392 | |
Amir Goldstein | f4439de | 2017-07-04 22:04:06 +0300 | [diff] [blame] | 393 | static int ovl_link_up(struct ovl_copy_up_ctx *c) |
| 394 | { |
| 395 | int err; |
| 396 | struct dentry *upper; |
| 397 | struct dentry *upperdir = ovl_dentry_upper(c->parent); |
| 398 | struct inode *udir = d_inode(upperdir); |
| 399 | |
| 400 | /* Mark parent "impure" because it may now contain non-pure upper */ |
| 401 | err = ovl_set_impure(c->parent, upperdir); |
| 402 | if (err) |
| 403 | return err; |
| 404 | |
| 405 | err = ovl_set_nlink_lower(c->dentry); |
| 406 | if (err) |
| 407 | return err; |
| 408 | |
| 409 | inode_lock_nested(udir, I_MUTEX_PARENT); |
| 410 | upper = lookup_one_len(c->dentry->d_name.name, upperdir, |
| 411 | c->dentry->d_name.len); |
| 412 | err = PTR_ERR(upper); |
| 413 | if (!IS_ERR(upper)) { |
Amir Goldstein | 6cf0076 | 2018-05-16 17:04:00 +0300 | [diff] [blame] | 414 | err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper); |
Amir Goldstein | f4439de | 2017-07-04 22:04:06 +0300 | [diff] [blame] | 415 | dput(upper); |
| 416 | |
| 417 | if (!err) { |
| 418 | /* Restore timestamps on parent (best effort) */ |
| 419 | ovl_set_timestamps(upperdir, &c->pstat); |
| 420 | ovl_dentry_set_upper_alias(c->dentry); |
| 421 | } |
| 422 | } |
| 423 | inode_unlock(udir); |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 424 | if (err) |
| 425 | return err; |
| 426 | |
| 427 | err = ovl_set_nlink_upper(c->dentry); |
Amir Goldstein | f4439de | 2017-07-04 22:04:06 +0300 | [diff] [blame] | 428 | |
| 429 | return err; |
| 430 | } |
| 431 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 432 | static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp, |
| 433 | struct dentry **newdentry) |
Amir Goldstein | 15932c4 | 2017-05-16 01:26:49 +0300 | [diff] [blame] | 434 | { |
| 435 | int err; |
| 436 | struct dentry *upper; |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 437 | struct inode *udir = d_inode(c->destdir); |
Amir Goldstein | 15932c4 | 2017-05-16 01:26:49 +0300 | [diff] [blame] | 438 | |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 439 | upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len); |
Amir Goldstein | 15932c4 | 2017-05-16 01:26:49 +0300 | [diff] [blame] | 440 | if (IS_ERR(upper)) |
| 441 | return PTR_ERR(upper); |
| 442 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 443 | if (c->tmpfile) |
Amir Goldstein | 6cf0076 | 2018-05-16 17:04:00 +0300 | [diff] [blame] | 444 | err = ovl_do_link(temp, udir, upper); |
Amir Goldstein | 15932c4 | 2017-05-16 01:26:49 +0300 | [diff] [blame] | 445 | else |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 446 | err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0); |
Amir Goldstein | 15932c4 | 2017-05-16 01:26:49 +0300 | [diff] [blame] | 447 | |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 448 | if (!err) |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 449 | *newdentry = dget(c->tmpfile ? upper : temp); |
Amir Goldstein | 15932c4 | 2017-05-16 01:26:49 +0300 | [diff] [blame] | 450 | dput(upper); |
| 451 | |
| 452 | return err; |
| 453 | } |
| 454 | |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 455 | static struct dentry *ovl_get_tmpfile(struct ovl_copy_up_ctx *c) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 456 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 457 | int err; |
Miklos Szeredi | 7d90b85 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 458 | struct dentry *temp; |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 459 | const struct cred *old_creds = NULL; |
| 460 | struct cred *new_creds = NULL; |
Amir Goldstein | 471ec5d | 2018-05-16 17:35:02 +0300 | [diff] [blame] | 461 | struct ovl_cattr cattr = { |
Al Viro | 32a3d84 | 2016-12-04 17:33:17 +0000 | [diff] [blame] | 462 | /* Can't properly set mode on creation because of the umask */ |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 463 | .mode = c->stat.mode & S_IFMT, |
| 464 | .rdev = c->stat.rdev, |
| 465 | .link = c->link |
Al Viro | 32a3d84 | 2016-12-04 17:33:17 +0000 | [diff] [blame] | 466 | }; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 467 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 468 | err = security_inode_copy_up(c->dentry, &new_creds); |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 469 | temp = ERR_PTR(err); |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 470 | if (err < 0) |
Miklos Szeredi | e85f82f | 2017-06-28 13:41:22 +0200 | [diff] [blame] | 471 | goto out; |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 472 | |
| 473 | if (new_creds) |
| 474 | old_creds = override_creds(new_creds); |
| 475 | |
Amir Goldstein | 137ec52 | 2018-05-16 17:51:25 +0300 | [diff] [blame] | 476 | if (c->tmpfile) |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 477 | temp = ovl_do_tmpfile(c->workdir, c->stat.mode); |
Amir Goldstein | 137ec52 | 2018-05-16 17:51:25 +0300 | [diff] [blame] | 478 | else |
| 479 | temp = ovl_create_temp(c->workdir, &cattr); |
Miklos Szeredi | 7d90b85 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 480 | out: |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 481 | if (new_creds) { |
| 482 | revert_creds(old_creds); |
| 483 | put_cred(new_creds); |
| 484 | } |
| 485 | |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 486 | return temp; |
Miklos Szeredi | 7d90b85 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 487 | } |
| 488 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 489 | static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp) |
Amir Goldstein | 02209d1 | 2017-05-19 15:16:21 +0300 | [diff] [blame] | 490 | { |
| 491 | int err; |
| 492 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 493 | if (S_ISREG(c->stat.mode)) { |
Amir Goldstein | 02209d1 | 2017-05-19 15:16:21 +0300 | [diff] [blame] | 494 | struct path upperpath; |
| 495 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 496 | ovl_path_upper(c->dentry, &upperpath); |
Amir Goldstein | 02209d1 | 2017-05-19 15:16:21 +0300 | [diff] [blame] | 497 | BUG_ON(upperpath.dentry != NULL); |
| 498 | upperpath.dentry = temp; |
| 499 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 500 | err = ovl_copy_up_data(&c->lowerpath, &upperpath, c->stat.size); |
Amir Goldstein | 02209d1 | 2017-05-19 15:16:21 +0300 | [diff] [blame] | 501 | if (err) |
| 502 | return err; |
| 503 | } |
| 504 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 505 | err = ovl_copy_xattr(c->lowerpath.dentry, temp); |
Amir Goldstein | 02209d1 | 2017-05-19 15:16:21 +0300 | [diff] [blame] | 506 | if (err) |
| 507 | return err; |
| 508 | |
| 509 | inode_lock(temp->d_inode); |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 510 | err = ovl_set_attr(temp, &c->stat); |
Amir Goldstein | 02209d1 | 2017-05-19 15:16:21 +0300 | [diff] [blame] | 511 | inode_unlock(temp->d_inode); |
| 512 | if (err) |
| 513 | return err; |
| 514 | |
| 515 | /* |
| 516 | * Store identifier of lower inode in upper inode xattr to |
| 517 | * allow lookup of the copy up origin inode. |
| 518 | * |
| 519 | * Don't set origin when we are breaking the association with a lower |
| 520 | * hard link. |
| 521 | */ |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 522 | if (c->origin) { |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 523 | err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp); |
Amir Goldstein | 02209d1 | 2017-05-19 15:16:21 +0300 | [diff] [blame] | 524 | if (err) |
| 525 | return err; |
| 526 | } |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 531 | static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c) |
Miklos Szeredi | 7d90b85 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 532 | { |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 533 | struct inode *udir = c->destdir->d_inode; |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 534 | struct inode *inode; |
Miklos Szeredi | 7d90b85 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 535 | struct dentry *newdentry = NULL; |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 536 | struct dentry *temp; |
Miklos Szeredi | 7d90b85 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 537 | int err; |
| 538 | |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 539 | temp = ovl_get_tmpfile(c); |
| 540 | if (IS_ERR(temp)) |
| 541 | return PTR_ERR(temp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 542 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 543 | err = ovl_copy_up_inode(c, temp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 544 | if (err) |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 545 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 546 | |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 547 | if (S_ISDIR(c->stat.mode) && c->indexed) { |
| 548 | err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp); |
| 549 | if (err) |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 550 | goto out; |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 551 | } |
| 552 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 553 | if (c->tmpfile) { |
Amir Goldstein | 15932c4 | 2017-05-16 01:26:49 +0300 | [diff] [blame] | 554 | inode_lock_nested(udir, I_MUTEX_PARENT); |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 555 | err = ovl_install_temp(c, temp, &newdentry); |
Amir Goldstein | 15932c4 | 2017-05-16 01:26:49 +0300 | [diff] [blame] | 556 | inode_unlock(udir); |
| 557 | } else { |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 558 | err = ovl_install_temp(c, temp, &newdentry); |
Miklos Szeredi | e85f82f | 2017-06-28 13:41:22 +0200 | [diff] [blame] | 559 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 560 | if (err) |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 561 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 562 | |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 563 | inode = d_inode(c->dentry); |
| 564 | ovl_inode_update(inode, newdentry); |
| 565 | if (S_ISDIR(inode->i_mode)) |
| 566 | ovl_set_flag(OVL_WHITEOUTS, inode); |
| 567 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 568 | out: |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 569 | if (err && !c->tmpfile) |
| 570 | ovl_cleanup(d_inode(c->workdir), temp); |
Miklos Szeredi | e85f82f | 2017-06-28 13:41:22 +0200 | [diff] [blame] | 571 | dput(temp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 572 | return err; |
| 573 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Copy up a single dentry |
| 578 | * |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 579 | * All renames start with copy up of source if necessary. The actual |
| 580 | * rename will only proceed once the copy up was successful. Copy up uses |
| 581 | * upper parent i_mutex for exclusion. Since rename can change d_parent it |
| 582 | * is possible that the copy up will lock the old parent. At that point |
| 583 | * the file will have already been copied up anyway. |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 584 | */ |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 585 | static int ovl_do_copy_up(struct ovl_copy_up_ctx *c) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 586 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 587 | int err; |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 588 | struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info; |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 589 | bool to_index = false; |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 590 | |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 591 | /* |
| 592 | * Indexed non-dir is copied up directly to the index entry and then |
| 593 | * hardlinked to upper dir. Indexed dir is copied up to indexdir, |
| 594 | * then index entry is created and then copied up dir installed. |
| 595 | * Copying dir up to indexdir instead of workdir simplifies locking. |
| 596 | */ |
| 597 | if (ovl_need_index(c->dentry)) { |
| 598 | c->indexed = true; |
| 599 | if (S_ISDIR(c->stat.mode)) |
| 600 | c->workdir = ovl_indexdir(c->dentry->d_sb); |
| 601 | else |
| 602 | to_index = true; |
| 603 | } |
| 604 | |
| 605 | if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index) |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 606 | c->origin = true; |
| 607 | |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 608 | if (to_index) { |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 609 | c->destdir = ovl_indexdir(c->dentry->d_sb); |
| 610 | err = ovl_get_index_name(c->lowerpath.dentry, &c->destname); |
| 611 | if (err) |
| 612 | return err; |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 613 | } else if (WARN_ON(!c->parent)) { |
| 614 | /* Disconnected dentry must be copied up to index dir */ |
| 615 | return -EIO; |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 616 | } else { |
| 617 | /* |
| 618 | * Mark parent "impure" because it may now contain non-pure |
| 619 | * upper |
| 620 | */ |
| 621 | err = ovl_set_impure(c->parent, c->destdir); |
| 622 | if (err) |
| 623 | return err; |
| 624 | } |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 625 | |
Amir Goldstein | 01ad3eb | 2017-01-17 06:34:57 +0200 | [diff] [blame] | 626 | /* Should we copyup with O_TMPFILE or with workdir? */ |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 627 | if (S_ISREG(c->stat.mode) && ofs->tmpfile) { |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 628 | c->tmpfile = true; |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 629 | err = ovl_copy_up_locked(c); |
| 630 | } else { |
Amir Goldstein | 5820dc0 | 2017-09-25 16:39:55 +0300 | [diff] [blame] | 631 | err = ovl_lock_rename_workdir(c->workdir, c->destdir); |
| 632 | if (!err) { |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 633 | err = ovl_copy_up_locked(c); |
| 634 | unlock_rename(c->workdir, c->destdir); |
| 635 | } |
Amir Goldstein | 01ad3eb | 2017-01-17 06:34:57 +0200 | [diff] [blame] | 636 | } |
| 637 | |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 638 | |
| 639 | if (err) |
| 640 | goto out; |
| 641 | |
| 642 | if (c->indexed) |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 643 | ovl_set_flag(OVL_INDEX, d_inode(c->dentry)); |
| 644 | |
| 645 | if (to_index) { |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 646 | /* Initialize nlink for copy up of disconnected dentry */ |
| 647 | err = ovl_set_nlink_upper(c->dentry); |
| 648 | } else { |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 649 | struct inode *udir = d_inode(c->destdir); |
| 650 | |
| 651 | /* Restore timestamps on parent (best effort) */ |
| 652 | inode_lock(udir); |
| 653 | ovl_set_timestamps(c->destdir, &c->pstat); |
| 654 | inode_unlock(udir); |
| 655 | |
| 656 | ovl_dentry_set_upper_alias(c->dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 657 | } |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 658 | |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 659 | out: |
| 660 | if (to_index) |
| 661 | kfree(c->destname.name); |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 662 | return err; |
| 663 | } |
| 664 | |
| 665 | static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, |
| 666 | int flags) |
| 667 | { |
| 668 | int err; |
| 669 | DEFINE_DELAYED_CALL(done); |
| 670 | struct path parentpath; |
| 671 | struct ovl_copy_up_ctx ctx = { |
| 672 | .parent = parent, |
| 673 | .dentry = dentry, |
| 674 | .workdir = ovl_workdir(dentry), |
| 675 | }; |
| 676 | |
| 677 | if (WARN_ON(!ctx.workdir)) |
| 678 | return -EROFS; |
| 679 | |
| 680 | ovl_path_lower(dentry, &ctx.lowerpath); |
| 681 | err = vfs_getattr(&ctx.lowerpath, &ctx.stat, |
| 682 | STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT); |
| 683 | if (err) |
| 684 | return err; |
| 685 | |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 686 | if (parent) { |
| 687 | ovl_path_upper(parent, &parentpath); |
| 688 | ctx.destdir = parentpath.dentry; |
| 689 | ctx.destname = dentry->d_name; |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 690 | |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 691 | err = vfs_getattr(&parentpath, &ctx.pstat, |
| 692 | STATX_ATIME | STATX_MTIME, |
| 693 | AT_STATX_SYNC_AS_STAT); |
| 694 | if (err) |
| 695 | return err; |
| 696 | } |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 697 | |
| 698 | /* maybe truncate regular file. this has no effect on dirs */ |
| 699 | if (flags & O_TRUNC) |
| 700 | ctx.stat.size = 0; |
| 701 | |
| 702 | if (S_ISLNK(ctx.stat.mode)) { |
| 703 | ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done); |
| 704 | if (IS_ERR(ctx.link)) |
| 705 | return PTR_ERR(ctx.link); |
| 706 | } |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 707 | |
Miklos Szeredi | fd210b7 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 708 | err = ovl_copy_up_start(dentry); |
| 709 | /* err < 0: interrupted, err > 0: raced with another copy-up */ |
| 710 | if (unlikely(err)) { |
| 711 | if (err > 0) |
| 712 | err = 0; |
| 713 | } else { |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 714 | if (!ovl_dentry_upper(dentry)) |
| 715 | err = ovl_do_copy_up(&ctx); |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 716 | if (!err && parent && !ovl_dentry_has_upper_alias(dentry)) |
Amir Goldstein | f4439de | 2017-07-04 22:04:06 +0300 | [diff] [blame] | 717 | err = ovl_link_up(&ctx); |
Miklos Szeredi | fd210b7 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 718 | ovl_copy_up_end(dentry); |
| 719 | } |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 720 | do_delayed_call(&done); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 721 | |
| 722 | return err; |
| 723 | } |
| 724 | |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 725 | int ovl_copy_up_flags(struct dentry *dentry, int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 726 | { |
Vivek Goyal | 8eac98b | 2016-09-06 13:40:32 -0400 | [diff] [blame] | 727 | int err = 0; |
| 728 | const struct cred *old_cred = ovl_override_creds(dentry->d_sb); |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 729 | bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED); |
| 730 | |
| 731 | /* |
| 732 | * With NFS export, copy up can get called for a disconnected non-dir. |
| 733 | * In this case, we will copy up lower inode to index dir without |
| 734 | * linking it to upper dir. |
| 735 | */ |
| 736 | if (WARN_ON(disconnected && d_is_dir(dentry))) |
| 737 | return -EIO; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 738 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 739 | while (!err) { |
| 740 | struct dentry *next; |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 741 | struct dentry *parent = NULL; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 742 | |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 743 | /* |
| 744 | * Check if copy-up has happened as well as for upper alias (in |
| 745 | * case of hard links) is there. |
| 746 | * |
| 747 | * Both checks are lockless: |
| 748 | * - false negatives: will recheck under oi->lock |
| 749 | * - false positives: |
| 750 | * + ovl_dentry_upper() uses memory barriers to ensure the |
| 751 | * upper dentry is up-to-date |
| 752 | * + ovl_dentry_has_upper_alias() relies on locking of |
| 753 | * upper parent i_rwsem to prevent reordering copy-up |
| 754 | * with rename. |
| 755 | */ |
| 756 | if (ovl_dentry_upper(dentry) && |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 757 | (ovl_dentry_has_upper_alias(dentry) || disconnected)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 758 | break; |
| 759 | |
| 760 | next = dget(dentry); |
| 761 | /* find the topmost dentry not yet copied up */ |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 762 | for (; !disconnected;) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 763 | parent = dget_parent(next); |
| 764 | |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 765 | if (ovl_dentry_upper(parent)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 766 | break; |
| 767 | |
| 768 | dput(next); |
| 769 | next = parent; |
| 770 | } |
| 771 | |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 772 | err = ovl_copy_up_one(parent, next, flags); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 773 | |
| 774 | dput(parent); |
| 775 | dput(next); |
| 776 | } |
Vivek Goyal | 8eac98b | 2016-09-06 13:40:32 -0400 | [diff] [blame] | 777 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 778 | |
| 779 | return err; |
| 780 | } |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 781 | |
| 782 | int ovl_copy_up(struct dentry *dentry) |
| 783 | { |
| 784 | return ovl_copy_up_flags(dentry, 0); |
| 785 | } |