1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2e9be9d5eSMiklos Szeredi /* 3e9be9d5eSMiklos Szeredi * 4e9be9d5eSMiklos Szeredi * Copyright (C) 2011 Novell Inc. 5e9be9d5eSMiklos Szeredi */ 6e9be9d5eSMiklos Szeredi 7fb5bb2c3SDavid Howells #include <linux/module.h> 8e9be9d5eSMiklos Szeredi #include <linux/fs.h> 9e9be9d5eSMiklos Szeredi #include <linux/slab.h> 10e9be9d5eSMiklos Szeredi #include <linux/file.h> 11e9be9d5eSMiklos Szeredi #include <linux/splice.h> 12e9be9d5eSMiklos Szeredi #include <linux/xattr.h> 13e9be9d5eSMiklos Szeredi #include <linux/security.h> 14e9be9d5eSMiklos Szeredi #include <linux/uaccess.h> 15174cd4b1SIngo Molnar #include <linux/sched/signal.h> 165b825c3aSIngo Molnar #include <linux/cred.h> 17e9be9d5eSMiklos Szeredi #include <linux/namei.h> 18fb5bb2c3SDavid Howells #include <linux/fdtable.h> 19fb5bb2c3SDavid Howells #include <linux/ratelimit.h> 203a1e819bSAmir Goldstein #include <linux/exportfs.h> 21e9be9d5eSMiklos Szeredi #include "overlayfs.h" 22e9be9d5eSMiklos Szeredi 23e9be9d5eSMiklos Szeredi #define OVL_COPY_UP_CHUNK_SIZE (1 << 20) 24e9be9d5eSMiklos Szeredi 25670c2324SMiklos Szeredi static int ovl_ccup_set(const char *buf, const struct kernel_param *param) 26fb5bb2c3SDavid Howells { 271bd0a3aeSlijiazi pr_warn("\"check_copy_up\" module option is obsolete\n"); 28fb5bb2c3SDavid Howells return 0; 29fb5bb2c3SDavid Howells } 30fb5bb2c3SDavid Howells 31670c2324SMiklos Szeredi static int ovl_ccup_get(char *buf, const struct kernel_param *param) 32fb5bb2c3SDavid Howells { 33670c2324SMiklos Szeredi return sprintf(buf, "N\n"); 34fb5bb2c3SDavid Howells } 35fb5bb2c3SDavid Howells 36670c2324SMiklos Szeredi module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644); 37253e7483SNicolas Schier MODULE_PARM_DESC(check_copy_up, "Obsolete; does nothing"); 38670c2324SMiklos Szeredi 39c61ca557SMiklos Szeredi static bool ovl_must_copy_xattr(const char *name) 40c61ca557SMiklos Szeredi { 41c61ca557SMiklos Szeredi return !strcmp(name, XATTR_POSIX_ACL_ACCESS) || 42c61ca557SMiklos Szeredi !strcmp(name, XATTR_POSIX_ACL_DEFAULT) || 43c61ca557SMiklos Szeredi !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN); 44c61ca557SMiklos Szeredi } 45c61ca557SMiklos Szeredi 46e9be9d5eSMiklos Szeredi int ovl_copy_xattr(struct dentry *old, struct dentry *new) 47e9be9d5eSMiklos Szeredi { 48e4ad29faSVito Caputo ssize_t list_size, size, value_size = 0; 49e4ad29faSVito Caputo char *buf, *name, *value = NULL; 50520da69dSYuxuan Shui int error = 0; 518b326c61SMiklos Szeredi size_t slen; 52e9be9d5eSMiklos Szeredi 535d6c3191SAndreas Gruenbacher if (!(old->d_inode->i_opflags & IOP_XATTR) || 545d6c3191SAndreas Gruenbacher !(new->d_inode->i_opflags & IOP_XATTR)) 55e9be9d5eSMiklos Szeredi return 0; 56e9be9d5eSMiklos Szeredi 57e9be9d5eSMiklos Szeredi list_size = vfs_listxattr(old, NULL, 0); 58e9be9d5eSMiklos Szeredi if (list_size <= 0) { 59e9be9d5eSMiklos Szeredi if (list_size == -EOPNOTSUPP) 60e9be9d5eSMiklos Szeredi return 0; 61e9be9d5eSMiklos Szeredi return list_size; 62e9be9d5eSMiklos Szeredi } 63e9be9d5eSMiklos Szeredi 64e9be9d5eSMiklos Szeredi buf = kzalloc(list_size, GFP_KERNEL); 65e9be9d5eSMiklos Szeredi if (!buf) 66e9be9d5eSMiklos Szeredi return -ENOMEM; 67e9be9d5eSMiklos Szeredi 68e9be9d5eSMiklos Szeredi list_size = vfs_listxattr(old, buf, list_size); 69e9be9d5eSMiklos Szeredi if (list_size <= 0) { 70e9be9d5eSMiklos Szeredi error = list_size; 71e4ad29faSVito Caputo goto out; 72e9be9d5eSMiklos Szeredi } 73e9be9d5eSMiklos Szeredi 748b326c61SMiklos Szeredi for (name = buf; list_size; name += slen) { 758b326c61SMiklos Szeredi slen = strnlen(name, list_size) + 1; 768b326c61SMiklos Szeredi 778b326c61SMiklos Szeredi /* underlying fs providing us with an broken xattr list? */ 788b326c61SMiklos Szeredi if (WARN_ON(slen > list_size)) { 798b326c61SMiklos Szeredi error = -EIO; 808b326c61SMiklos Szeredi break; 818b326c61SMiklos Szeredi } 828b326c61SMiklos Szeredi list_size -= slen; 838b326c61SMiklos Szeredi 840956254aSMiklos Szeredi if (ovl_is_private_xattr(name)) 850956254aSMiklos Szeredi continue; 86e4ad29faSVito Caputo retry: 87e4ad29faSVito Caputo size = vfs_getxattr(old, name, value, value_size); 88e4ad29faSVito Caputo if (size == -ERANGE) 89e4ad29faSVito Caputo size = vfs_getxattr(old, name, NULL, 0); 90e4ad29faSVito Caputo 9197daf8b9SMiklos Szeredi if (size < 0) { 92e9be9d5eSMiklos Szeredi error = size; 93e4ad29faSVito Caputo break; 94e9be9d5eSMiklos Szeredi } 95e9be9d5eSMiklos Szeredi 96e4ad29faSVito Caputo if (size > value_size) { 97e4ad29faSVito Caputo void *new; 98e4ad29faSVito Caputo 99e4ad29faSVito Caputo new = krealloc(value, size, GFP_KERNEL); 100e4ad29faSVito Caputo if (!new) { 101e4ad29faSVito Caputo error = -ENOMEM; 102e4ad29faSVito Caputo break; 103e4ad29faSVito Caputo } 104e4ad29faSVito Caputo value = new; 105e4ad29faSVito Caputo value_size = size; 106e4ad29faSVito Caputo goto retry; 107e4ad29faSVito Caputo } 108e4ad29faSVito Caputo 109121ab822SVivek Goyal error = security_inode_copy_up_xattr(name); 110121ab822SVivek Goyal if (error < 0 && error != -EOPNOTSUPP) 111121ab822SVivek Goyal break; 112121ab822SVivek Goyal if (error == 1) { 113121ab822SVivek Goyal error = 0; 114121ab822SVivek Goyal continue; /* Discard */ 115121ab822SVivek Goyal } 116e4ad29faSVito Caputo error = vfs_setxattr(new, name, value, size, 0); 117c61ca557SMiklos Szeredi if (error) { 118c61ca557SMiklos Szeredi if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name)) 119e4ad29faSVito Caputo break; 120c61ca557SMiklos Szeredi 121c61ca557SMiklos Szeredi /* Ignore failure to copy unknown xattrs */ 122c61ca557SMiklos Szeredi error = 0; 123c61ca557SMiklos Szeredi } 124e4ad29faSVito Caputo } 125e9be9d5eSMiklos Szeredi kfree(value); 126e9be9d5eSMiklos Szeredi out: 127e9be9d5eSMiklos Szeredi kfree(buf); 128e9be9d5eSMiklos Szeredi return error; 129e9be9d5eSMiklos Szeredi } 130e9be9d5eSMiklos Szeredi 131c86243b0SVivek Goyal static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old, 132c86243b0SVivek Goyal struct path *new, loff_t len) 133e9be9d5eSMiklos Szeredi { 134e9be9d5eSMiklos Szeredi struct file *old_file; 135e9be9d5eSMiklos Szeredi struct file *new_file; 136e9be9d5eSMiklos Szeredi loff_t old_pos = 0; 137e9be9d5eSMiklos Szeredi loff_t new_pos = 0; 13842ec3d4cSDarrick J. Wong loff_t cloned; 139b504c654SChengguang Xu loff_t data_pos = -1; 140b504c654SChengguang Xu loff_t hole_len; 141b504c654SChengguang Xu bool skip_hole = false; 142e9be9d5eSMiklos Szeredi int error = 0; 143e9be9d5eSMiklos Szeredi 144e9be9d5eSMiklos Szeredi if (len == 0) 145e9be9d5eSMiklos Szeredi return 0; 146e9be9d5eSMiklos Szeredi 1470480334fSDavid Howells old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY); 148e9be9d5eSMiklos Szeredi if (IS_ERR(old_file)) 149e9be9d5eSMiklos Szeredi return PTR_ERR(old_file); 150e9be9d5eSMiklos Szeredi 1510480334fSDavid Howells new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY); 152e9be9d5eSMiklos Szeredi if (IS_ERR(new_file)) { 153e9be9d5eSMiklos Szeredi error = PTR_ERR(new_file); 154e9be9d5eSMiklos Szeredi goto out_fput; 155e9be9d5eSMiklos Szeredi } 156e9be9d5eSMiklos Szeredi 1572ea98466SAmir Goldstein /* Try to use clone_file_range to clone up within the same fs */ 158452ce659SDarrick J. Wong cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0); 15942ec3d4cSDarrick J. Wong if (cloned == len) 1602ea98466SAmir Goldstein goto out; 1612ea98466SAmir Goldstein /* Couldn't clone, so now we try to copy the data */ 1622ea98466SAmir Goldstein 163b504c654SChengguang Xu /* Check if lower fs supports seek operation */ 164b504c654SChengguang Xu if (old_file->f_mode & FMODE_LSEEK && 165b504c654SChengguang Xu old_file->f_op->llseek) 166b504c654SChengguang Xu skip_hole = true; 167b504c654SChengguang Xu 168e9be9d5eSMiklos Szeredi while (len) { 169e9be9d5eSMiklos Szeredi size_t this_len = OVL_COPY_UP_CHUNK_SIZE; 170e9be9d5eSMiklos Szeredi long bytes; 171e9be9d5eSMiklos Szeredi 172e9be9d5eSMiklos Szeredi if (len < this_len) 173e9be9d5eSMiklos Szeredi this_len = len; 174e9be9d5eSMiklos Szeredi 175e9be9d5eSMiklos Szeredi if (signal_pending_state(TASK_KILLABLE, current)) { 176e9be9d5eSMiklos Szeredi error = -EINTR; 177e9be9d5eSMiklos Szeredi break; 178e9be9d5eSMiklos Szeredi } 179e9be9d5eSMiklos Szeredi 180b504c654SChengguang Xu /* 181b504c654SChengguang Xu * Fill zero for hole will cost unnecessary disk space 182b504c654SChengguang Xu * and meanwhile slow down the copy-up speed, so we do 183b504c654SChengguang Xu * an optimization for hole during copy-up, it relies 184b504c654SChengguang Xu * on SEEK_DATA implementation in lower fs so if lower 185b504c654SChengguang Xu * fs does not support it, copy-up will behave as before. 186b504c654SChengguang Xu * 187b504c654SChengguang Xu * Detail logic of hole detection as below: 188b504c654SChengguang Xu * When we detect next data position is larger than current 189b504c654SChengguang Xu * position we will skip that hole, otherwise we copy 190b504c654SChengguang Xu * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually, 191b504c654SChengguang Xu * it may not recognize all kind of holes and sometimes 192b504c654SChengguang Xu * only skips partial of hole area. However, it will be 193b504c654SChengguang Xu * enough for most of the use cases. 194b504c654SChengguang Xu */ 195b504c654SChengguang Xu 196b504c654SChengguang Xu if (skip_hole && data_pos < old_pos) { 197b504c654SChengguang Xu data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA); 198b504c654SChengguang Xu if (data_pos > old_pos) { 199b504c654SChengguang Xu hole_len = data_pos - old_pos; 200b504c654SChengguang Xu len -= hole_len; 201b504c654SChengguang Xu old_pos = new_pos = data_pos; 202b504c654SChengguang Xu continue; 203b504c654SChengguang Xu } else if (data_pos == -ENXIO) { 204b504c654SChengguang Xu break; 205b504c654SChengguang Xu } else if (data_pos < 0) { 206b504c654SChengguang Xu skip_hole = false; 207b504c654SChengguang Xu } 208b504c654SChengguang Xu } 209b504c654SChengguang Xu 210e9be9d5eSMiklos Szeredi bytes = do_splice_direct(old_file, &old_pos, 211e9be9d5eSMiklos Szeredi new_file, &new_pos, 212e9be9d5eSMiklos Szeredi this_len, SPLICE_F_MOVE); 213e9be9d5eSMiklos Szeredi if (bytes <= 0) { 214e9be9d5eSMiklos Szeredi error = bytes; 215e9be9d5eSMiklos Szeredi break; 216e9be9d5eSMiklos Szeredi } 217e9be9d5eSMiklos Szeredi WARN_ON(old_pos != new_pos); 218e9be9d5eSMiklos Szeredi 219e9be9d5eSMiklos Szeredi len -= bytes; 220e9be9d5eSMiklos Szeredi } 2212ea98466SAmir Goldstein out: 222c86243b0SVivek Goyal if (!error && ovl_should_sync(ofs)) 223641089c1SMiklos Szeredi error = vfs_fsync(new_file, 0); 224e9be9d5eSMiklos Szeredi fput(new_file); 225e9be9d5eSMiklos Szeredi out_fput: 226e9be9d5eSMiklos Szeredi fput(old_file); 227e9be9d5eSMiklos Szeredi return error; 228e9be9d5eSMiklos Szeredi } 229e9be9d5eSMiklos Szeredi 2300c288874SVivek Goyal static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat) 2310c288874SVivek Goyal { 2320c288874SVivek Goyal struct iattr attr = { 2330c288874SVivek Goyal .ia_valid = ATTR_SIZE, 2340c288874SVivek Goyal .ia_size = stat->size, 2350c288874SVivek Goyal }; 2360c288874SVivek Goyal 2370c288874SVivek Goyal return notify_change(upperdentry, &attr, NULL); 2380c288874SVivek Goyal } 2390c288874SVivek Goyal 240e9be9d5eSMiklos Szeredi static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat) 241e9be9d5eSMiklos Szeredi { 242e9be9d5eSMiklos Szeredi struct iattr attr = { 243e9be9d5eSMiklos Szeredi .ia_valid = 244e9be9d5eSMiklos Szeredi ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET, 245e9be9d5eSMiklos Szeredi .ia_atime = stat->atime, 246e9be9d5eSMiklos Szeredi .ia_mtime = stat->mtime, 247e9be9d5eSMiklos Szeredi }; 248e9be9d5eSMiklos Szeredi 249e9be9d5eSMiklos Szeredi return notify_change(upperdentry, &attr, NULL); 250e9be9d5eSMiklos Szeredi } 251e9be9d5eSMiklos Szeredi 252e9be9d5eSMiklos Szeredi int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat) 253e9be9d5eSMiklos Szeredi { 254e9be9d5eSMiklos Szeredi int err = 0; 255e9be9d5eSMiklos Szeredi 256e9be9d5eSMiklos Szeredi if (!S_ISLNK(stat->mode)) { 257e9be9d5eSMiklos Szeredi struct iattr attr = { 258e9be9d5eSMiklos Szeredi .ia_valid = ATTR_MODE, 259e9be9d5eSMiklos Szeredi .ia_mode = stat->mode, 260e9be9d5eSMiklos Szeredi }; 261e9be9d5eSMiklos Szeredi err = notify_change(upperdentry, &attr, NULL); 262e9be9d5eSMiklos Szeredi } 263e9be9d5eSMiklos Szeredi if (!err) { 264e9be9d5eSMiklos Szeredi struct iattr attr = { 265e9be9d5eSMiklos Szeredi .ia_valid = ATTR_UID | ATTR_GID, 266e9be9d5eSMiklos Szeredi .ia_uid = stat->uid, 267e9be9d5eSMiklos Szeredi .ia_gid = stat->gid, 268e9be9d5eSMiklos Szeredi }; 269e9be9d5eSMiklos Szeredi err = notify_change(upperdentry, &attr, NULL); 270e9be9d5eSMiklos Szeredi } 271e9be9d5eSMiklos Szeredi if (!err) 272e9be9d5eSMiklos Szeredi ovl_set_timestamps(upperdentry, stat); 273e9be9d5eSMiklos Szeredi 274e9be9d5eSMiklos Szeredi return err; 275e9be9d5eSMiklos Szeredi } 276e9be9d5eSMiklos Szeredi 2775b2cccd3SAmir Goldstein struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper) 2783a1e819bSAmir Goldstein { 2793a1e819bSAmir Goldstein struct ovl_fh *fh; 280ec7bbb53SAmir Goldstein int fh_type, dwords; 2813a1e819bSAmir Goldstein int buflen = MAX_HANDLE_SZ; 28205122443SAmir Goldstein uuid_t *uuid = &real->d_sb->s_uuid; 283ec7bbb53SAmir Goldstein int err; 2843a1e819bSAmir Goldstein 285ec7bbb53SAmir Goldstein /* Make sure the real fid stays 32bit aligned */ 286ec7bbb53SAmir Goldstein BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4); 287ec7bbb53SAmir Goldstein BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255); 288ec7bbb53SAmir Goldstein 289ec7bbb53SAmir Goldstein fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL); 290ec7bbb53SAmir Goldstein if (!fh) 2913a1e819bSAmir Goldstein return ERR_PTR(-ENOMEM); 2923a1e819bSAmir Goldstein 2933a1e819bSAmir Goldstein /* 2943a1e819bSAmir Goldstein * We encode a non-connectable file handle for non-dir, because we 2953a1e819bSAmir Goldstein * only need to find the lower inode number and we don't want to pay 2963a1e819bSAmir Goldstein * the price or reconnecting the dentry. 2973a1e819bSAmir Goldstein */ 2983a1e819bSAmir Goldstein dwords = buflen >> 2; 299ec7bbb53SAmir Goldstein fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0); 3003a1e819bSAmir Goldstein buflen = (dwords << 2); 3013a1e819bSAmir Goldstein 302ec7bbb53SAmir Goldstein err = -EIO; 3033a1e819bSAmir Goldstein if (WARN_ON(fh_type < 0) || 3043a1e819bSAmir Goldstein WARN_ON(buflen > MAX_HANDLE_SZ) || 3053a1e819bSAmir Goldstein WARN_ON(fh_type == FILEID_INVALID)) 306ec7bbb53SAmir Goldstein goto out_err; 3073a1e819bSAmir Goldstein 308cbe7fba8SAmir Goldstein fh->fb.version = OVL_FH_VERSION; 309cbe7fba8SAmir Goldstein fh->fb.magic = OVL_FH_MAGIC; 310cbe7fba8SAmir Goldstein fh->fb.type = fh_type; 311cbe7fba8SAmir Goldstein fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN; 31254fb347eSAmir Goldstein /* 31354fb347eSAmir Goldstein * When we will want to decode an overlay dentry from this handle 31454fb347eSAmir Goldstein * and all layers are on the same fs, if we get a disconncted real 31554fb347eSAmir Goldstein * dentry when we decode fid, the only way to tell if we should assign 31654fb347eSAmir Goldstein * it to upperdentry or to lowerstack is by checking this flag. 31754fb347eSAmir Goldstein */ 31854fb347eSAmir Goldstein if (is_upper) 319cbe7fba8SAmir Goldstein fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER; 320ec7bbb53SAmir Goldstein fh->fb.len = sizeof(fh->fb) + buflen; 321cbe7fba8SAmir Goldstein fh->fb.uuid = *uuid; 3223a1e819bSAmir Goldstein 3233a1e819bSAmir Goldstein return fh; 324ec7bbb53SAmir Goldstein 325ec7bbb53SAmir Goldstein out_err: 326ec7bbb53SAmir Goldstein kfree(fh); 327ec7bbb53SAmir Goldstein return ERR_PTR(err); 3283a1e819bSAmir Goldstein } 3293a1e819bSAmir Goldstein 3309678e630SAmir Goldstein int ovl_set_origin(struct dentry *dentry, struct dentry *lower, 3313a1e819bSAmir Goldstein struct dentry *upper) 3323a1e819bSAmir Goldstein { 3333a1e819bSAmir Goldstein const struct ovl_fh *fh = NULL; 3343a1e819bSAmir Goldstein int err; 3353a1e819bSAmir Goldstein 3363a1e819bSAmir Goldstein /* 3373a1e819bSAmir Goldstein * When lower layer doesn't support export operations store a 'null' fh, 3383a1e819bSAmir Goldstein * so we can use the overlay.origin xattr to distignuish between a copy 3393a1e819bSAmir Goldstein * up and a pure upper inode. 3403a1e819bSAmir Goldstein */ 34102bcd157SAmir Goldstein if (ovl_can_decode_fh(lower->d_sb)) { 3425b2cccd3SAmir Goldstein fh = ovl_encode_real_fh(lower, false); 3433a1e819bSAmir Goldstein if (IS_ERR(fh)) 3443a1e819bSAmir Goldstein return PTR_ERR(fh); 3453a1e819bSAmir Goldstein } 3463a1e819bSAmir Goldstein 3476266d465SMiklos Szeredi /* 3486266d465SMiklos Szeredi * Do not fail when upper doesn't support xattrs. 3496266d465SMiklos Szeredi */ 350cbe7fba8SAmir Goldstein err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh->buf, 351cbe7fba8SAmir Goldstein fh ? fh->fb.len : 0, 0); 3523a1e819bSAmir Goldstein kfree(fh); 3533a1e819bSAmir Goldstein 3543a1e819bSAmir Goldstein return err; 3553a1e819bSAmir Goldstein } 3563a1e819bSAmir Goldstein 357016b720fSAmir Goldstein /* Store file handle of @upper dir in @index dir entry */ 358016b720fSAmir Goldstein static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index) 359016b720fSAmir Goldstein { 360016b720fSAmir Goldstein const struct ovl_fh *fh; 361016b720fSAmir Goldstein int err; 362016b720fSAmir Goldstein 3635b2cccd3SAmir Goldstein fh = ovl_encode_real_fh(upper, true); 364016b720fSAmir Goldstein if (IS_ERR(fh)) 365016b720fSAmir Goldstein return PTR_ERR(fh); 366016b720fSAmir Goldstein 367cbe7fba8SAmir Goldstein err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh->buf, fh->fb.len, 0); 368016b720fSAmir Goldstein 369016b720fSAmir Goldstein kfree(fh); 370016b720fSAmir Goldstein return err; 371016b720fSAmir Goldstein } 372016b720fSAmir Goldstein 373016b720fSAmir Goldstein /* 374016b720fSAmir Goldstein * Create and install index entry. 375016b720fSAmir Goldstein * 376016b720fSAmir Goldstein * Caller must hold i_mutex on indexdir. 377016b720fSAmir Goldstein */ 378016b720fSAmir Goldstein static int ovl_create_index(struct dentry *dentry, struct dentry *origin, 379016b720fSAmir Goldstein struct dentry *upper) 380016b720fSAmir Goldstein { 381016b720fSAmir Goldstein struct dentry *indexdir = ovl_indexdir(dentry->d_sb); 382016b720fSAmir Goldstein struct inode *dir = d_inode(indexdir); 383016b720fSAmir Goldstein struct dentry *index = NULL; 384016b720fSAmir Goldstein struct dentry *temp = NULL; 385016b720fSAmir Goldstein struct qstr name = { }; 386016b720fSAmir Goldstein int err; 387016b720fSAmir Goldstein 388016b720fSAmir Goldstein /* 389016b720fSAmir Goldstein * For now this is only used for creating index entry for directories, 390016b720fSAmir Goldstein * because non-dir are copied up directly to index and then hardlinked 391016b720fSAmir Goldstein * to upper dir. 392016b720fSAmir Goldstein * 393016b720fSAmir Goldstein * TODO: implement create index for non-dir, so we can call it when 394016b720fSAmir Goldstein * encoding file handle for non-dir in case index does not exist. 395016b720fSAmir Goldstein */ 396016b720fSAmir Goldstein if (WARN_ON(!d_is_dir(dentry))) 397016b720fSAmir Goldstein return -EIO; 398016b720fSAmir Goldstein 399016b720fSAmir Goldstein /* Directory not expected to be indexed before copy up */ 400016b720fSAmir Goldstein if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry)))) 401016b720fSAmir Goldstein return -EIO; 402016b720fSAmir Goldstein 403016b720fSAmir Goldstein err = ovl_get_index_name(origin, &name); 404016b720fSAmir Goldstein if (err) 405016b720fSAmir Goldstein return err; 406016b720fSAmir Goldstein 407137ec526SAmir Goldstein temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0)); 408b148cba4SMiklos Szeredi err = PTR_ERR(temp); 409016b720fSAmir Goldstein if (IS_ERR(temp)) 410b148cba4SMiklos Szeredi goto free_name; 411016b720fSAmir Goldstein 412016b720fSAmir Goldstein err = ovl_set_upper_fh(upper, temp); 413016b720fSAmir Goldstein if (err) 414b148cba4SMiklos Szeredi goto out; 415016b720fSAmir Goldstein 416016b720fSAmir Goldstein index = lookup_one_len(name.name, indexdir, name.len); 417016b720fSAmir Goldstein if (IS_ERR(index)) { 418016b720fSAmir Goldstein err = PTR_ERR(index); 419016b720fSAmir Goldstein } else { 420016b720fSAmir Goldstein err = ovl_do_rename(dir, temp, dir, index, 0); 421016b720fSAmir Goldstein dput(index); 422016b720fSAmir Goldstein } 423016b720fSAmir Goldstein out: 424b148cba4SMiklos Szeredi if (err) 425b148cba4SMiklos Szeredi ovl_cleanup(dir, temp); 426016b720fSAmir Goldstein dput(temp); 427b148cba4SMiklos Szeredi free_name: 428016b720fSAmir Goldstein kfree(name.name); 429016b720fSAmir Goldstein return err; 430016b720fSAmir Goldstein } 431016b720fSAmir Goldstein 43223f0ab13SMiklos Szeredi struct ovl_copy_up_ctx { 433a6fb235aSMiklos Szeredi struct dentry *parent; 43423f0ab13SMiklos Szeredi struct dentry *dentry; 43523f0ab13SMiklos Szeredi struct path lowerpath; 43623f0ab13SMiklos Szeredi struct kstat stat; 43723f0ab13SMiklos Szeredi struct kstat pstat; 43823f0ab13SMiklos Szeredi const char *link; 43959be0971SAmir Goldstein struct dentry *destdir; 44059be0971SAmir Goldstein struct qstr destname; 44123f0ab13SMiklos Szeredi struct dentry *workdir; 44259be0971SAmir Goldstein bool origin; 443016b720fSAmir Goldstein bool indexed; 44444d5bf10SVivek Goyal bool metacopy; 44523f0ab13SMiklos Szeredi }; 44623f0ab13SMiklos Szeredi 447f4439de1SAmir Goldstein static int ovl_link_up(struct ovl_copy_up_ctx *c) 448f4439de1SAmir Goldstein { 449f4439de1SAmir Goldstein int err; 450f4439de1SAmir Goldstein struct dentry *upper; 451f4439de1SAmir Goldstein struct dentry *upperdir = ovl_dentry_upper(c->parent); 452f4439de1SAmir Goldstein struct inode *udir = d_inode(upperdir); 453f4439de1SAmir Goldstein 454f4439de1SAmir Goldstein /* Mark parent "impure" because it may now contain non-pure upper */ 455f4439de1SAmir Goldstein err = ovl_set_impure(c->parent, upperdir); 456f4439de1SAmir Goldstein if (err) 457f4439de1SAmir Goldstein return err; 458f4439de1SAmir Goldstein 459f4439de1SAmir Goldstein err = ovl_set_nlink_lower(c->dentry); 460f4439de1SAmir Goldstein if (err) 461f4439de1SAmir Goldstein return err; 462f4439de1SAmir Goldstein 463f4439de1SAmir Goldstein inode_lock_nested(udir, I_MUTEX_PARENT); 464f4439de1SAmir Goldstein upper = lookup_one_len(c->dentry->d_name.name, upperdir, 465f4439de1SAmir Goldstein c->dentry->d_name.len); 466f4439de1SAmir Goldstein err = PTR_ERR(upper); 467f4439de1SAmir Goldstein if (!IS_ERR(upper)) { 4686cf00764SAmir Goldstein err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper); 469f4439de1SAmir Goldstein dput(upper); 470f4439de1SAmir Goldstein 471f4439de1SAmir Goldstein if (!err) { 472f4439de1SAmir Goldstein /* Restore timestamps on parent (best effort) */ 473f4439de1SAmir Goldstein ovl_set_timestamps(upperdir, &c->pstat); 474f4439de1SAmir Goldstein ovl_dentry_set_upper_alias(c->dentry); 475f4439de1SAmir Goldstein } 476f4439de1SAmir Goldstein } 477f4439de1SAmir Goldstein inode_unlock(udir); 478aa3ff3c1SAmir Goldstein if (err) 479aa3ff3c1SAmir Goldstein return err; 480aa3ff3c1SAmir Goldstein 481aa3ff3c1SAmir Goldstein err = ovl_set_nlink_upper(c->dentry); 482f4439de1SAmir Goldstein 483f4439de1SAmir Goldstein return err; 484f4439de1SAmir Goldstein } 485f4439de1SAmir Goldstein 48623f0ab13SMiklos Szeredi static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp) 48702209d10SAmir Goldstein { 488c86243b0SVivek Goyal struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 48902209d10SAmir Goldstein int err; 49002209d10SAmir Goldstein 4915f32879eSVivek Goyal /* 4925f32879eSVivek Goyal * Copy up data first and then xattrs. Writing data after 4935f32879eSVivek Goyal * xattrs will remove security.capability xattr automatically. 4945f32879eSVivek Goyal */ 4955f32879eSVivek Goyal if (S_ISREG(c->stat.mode) && !c->metacopy) { 4965f32879eSVivek Goyal struct path upperpath, datapath; 4975f32879eSVivek Goyal 4985f32879eSVivek Goyal ovl_path_upper(c->dentry, &upperpath); 4995f32879eSVivek Goyal if (WARN_ON(upperpath.dentry != NULL)) 5005f32879eSVivek Goyal return -EIO; 5015f32879eSVivek Goyal upperpath.dentry = temp; 5025f32879eSVivek Goyal 5035f32879eSVivek Goyal ovl_path_lowerdata(c->dentry, &datapath); 504c86243b0SVivek Goyal err = ovl_copy_up_data(ofs, &datapath, &upperpath, 505c86243b0SVivek Goyal c->stat.size); 5065f32879eSVivek Goyal if (err) 5075f32879eSVivek Goyal return err; 5085f32879eSVivek Goyal } 5095f32879eSVivek Goyal 51023f0ab13SMiklos Szeredi err = ovl_copy_xattr(c->lowerpath.dentry, temp); 51102209d10SAmir Goldstein if (err) 51202209d10SAmir Goldstein return err; 51302209d10SAmir Goldstein 51402209d10SAmir Goldstein /* 51502209d10SAmir Goldstein * Store identifier of lower inode in upper inode xattr to 51602209d10SAmir Goldstein * allow lookup of the copy up origin inode. 51702209d10SAmir Goldstein * 51802209d10SAmir Goldstein * Don't set origin when we are breaking the association with a lower 51902209d10SAmir Goldstein * hard link. 52002209d10SAmir Goldstein */ 52159be0971SAmir Goldstein if (c->origin) { 52223f0ab13SMiklos Szeredi err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp); 52302209d10SAmir Goldstein if (err) 52402209d10SAmir Goldstein return err; 52502209d10SAmir Goldstein } 52602209d10SAmir Goldstein 5270c288874SVivek Goyal if (c->metacopy) { 5280c288874SVivek Goyal err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY, 5290c288874SVivek Goyal NULL, 0, -EOPNOTSUPP); 5300c288874SVivek Goyal if (err) 5310c288874SVivek Goyal return err; 5320c288874SVivek Goyal } 5330c288874SVivek Goyal 534bd64e575SVivek Goyal inode_lock(temp->d_inode); 535b504c654SChengguang Xu if (S_ISREG(c->stat.mode)) 5360c288874SVivek Goyal err = ovl_set_size(temp, &c->stat); 5370c288874SVivek Goyal if (!err) 538bd64e575SVivek Goyal err = ovl_set_attr(temp, &c->stat); 539bd64e575SVivek Goyal inode_unlock(temp->d_inode); 540bd64e575SVivek Goyal 541bd64e575SVivek Goyal return err; 54202209d10SAmir Goldstein } 54302209d10SAmir Goldstein 5446b52243fSMiklos Szeredi struct ovl_cu_creds { 5456b52243fSMiklos Szeredi const struct cred *old; 5466b52243fSMiklos Szeredi struct cred *new; 547b10cdcdcSAmir Goldstein }; 548b10cdcdcSAmir Goldstein 5496b52243fSMiklos Szeredi static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc) 550b10cdcdcSAmir Goldstein { 551b10cdcdcSAmir Goldstein int err; 552b10cdcdcSAmir Goldstein 5536b52243fSMiklos Szeredi cc->old = cc->new = NULL; 5546b52243fSMiklos Szeredi err = security_inode_copy_up(dentry, &cc->new); 5556b52243fSMiklos Szeredi if (err < 0) 556b10cdcdcSAmir Goldstein return err; 5576b52243fSMiklos Szeredi 5586b52243fSMiklos Szeredi if (cc->new) 5596b52243fSMiklos Szeredi cc->old = override_creds(cc->new); 5606b52243fSMiklos Szeredi 5616b52243fSMiklos Szeredi return 0; 5626b52243fSMiklos Szeredi } 5636b52243fSMiklos Szeredi 5646b52243fSMiklos Szeredi static void ovl_revert_cu_creds(struct ovl_cu_creds *cc) 5656b52243fSMiklos Szeredi { 5666b52243fSMiklos Szeredi if (cc->new) { 5676b52243fSMiklos Szeredi revert_creds(cc->old); 5686b52243fSMiklos Szeredi put_cred(cc->new); 5696b52243fSMiklos Szeredi } 570b10cdcdcSAmir Goldstein } 571b10cdcdcSAmir Goldstein 572b10cdcdcSAmir Goldstein /* 573b10cdcdcSAmir Goldstein * Copyup using workdir to prepare temp file. Used when copying up directories, 574b10cdcdcSAmir Goldstein * special files or when upper fs doesn't support O_TMPFILE. 575b10cdcdcSAmir Goldstein */ 576b10cdcdcSAmir Goldstein static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c) 577b10cdcdcSAmir Goldstein { 578b79e05aaSAmir Goldstein struct inode *inode; 5796b52243fSMiklos Szeredi struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir); 5806b52243fSMiklos Szeredi struct dentry *temp, *upper; 5816b52243fSMiklos Szeredi struct ovl_cu_creds cc; 5827d90b853SMiklos Szeredi int err; 5836b52243fSMiklos Szeredi struct ovl_cattr cattr = { 5846b52243fSMiklos Szeredi /* Can't properly set mode on creation because of the umask */ 5856b52243fSMiklos Szeredi .mode = c->stat.mode & S_IFMT, 5866b52243fSMiklos Szeredi .rdev = c->stat.rdev, 5876b52243fSMiklos Szeredi .link = c->link 5886b52243fSMiklos Szeredi }; 5897d90b853SMiklos Szeredi 590773cb4c5SAmir Goldstein /* workdir and destdir could be the same when copying up to indexdir */ 591773cb4c5SAmir Goldstein err = -EIO; 592773cb4c5SAmir Goldstein if (lock_rename(c->workdir, c->destdir) != NULL) 593773cb4c5SAmir Goldstein goto unlock; 594b10cdcdcSAmir Goldstein 5956b52243fSMiklos Szeredi err = ovl_prep_cu_creds(c->dentry, &cc); 5966b52243fSMiklos Szeredi if (err) 5976b52243fSMiklos Szeredi goto unlock; 5986b52243fSMiklos Szeredi 5996b52243fSMiklos Szeredi temp = ovl_create_temp(c->workdir, &cattr); 6006b52243fSMiklos Szeredi ovl_revert_cu_creds(&cc); 6016b52243fSMiklos Szeredi 602b10cdcdcSAmir Goldstein err = PTR_ERR(temp); 603b10cdcdcSAmir Goldstein if (IS_ERR(temp)) 604b10cdcdcSAmir Goldstein goto unlock; 605b10cdcdcSAmir Goldstein 606b10cdcdcSAmir Goldstein err = ovl_copy_up_inode(c, temp); 607b10cdcdcSAmir Goldstein if (err) 608b10cdcdcSAmir Goldstein goto cleanup; 609b10cdcdcSAmir Goldstein 610b10cdcdcSAmir Goldstein if (S_ISDIR(c->stat.mode) && c->indexed) { 611b10cdcdcSAmir Goldstein err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp); 612b10cdcdcSAmir Goldstein if (err) 613b10cdcdcSAmir Goldstein goto cleanup; 614b10cdcdcSAmir Goldstein } 615b10cdcdcSAmir Goldstein 6166b52243fSMiklos Szeredi upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len); 6176b52243fSMiklos Szeredi err = PTR_ERR(upper); 6186b52243fSMiklos Szeredi if (IS_ERR(upper)) 6196b52243fSMiklos Szeredi goto cleanup; 6206b52243fSMiklos Szeredi 6216b52243fSMiklos Szeredi err = ovl_do_rename(wdir, temp, udir, upper, 0); 6226b52243fSMiklos Szeredi dput(upper); 623b10cdcdcSAmir Goldstein if (err) 624b10cdcdcSAmir Goldstein goto cleanup; 625b10cdcdcSAmir Goldstein 626b10cdcdcSAmir Goldstein if (!c->metacopy) 627b10cdcdcSAmir Goldstein ovl_set_upperdata(d_inode(c->dentry)); 628b10cdcdcSAmir Goldstein inode = d_inode(c->dentry); 6296b52243fSMiklos Szeredi ovl_inode_update(inode, temp); 630b10cdcdcSAmir Goldstein if (S_ISDIR(inode->i_mode)) 631b10cdcdcSAmir Goldstein ovl_set_flag(OVL_WHITEOUTS, inode); 632b10cdcdcSAmir Goldstein unlock: 633b10cdcdcSAmir Goldstein unlock_rename(c->workdir, c->destdir); 634b10cdcdcSAmir Goldstein 635b10cdcdcSAmir Goldstein return err; 636b10cdcdcSAmir Goldstein 637b10cdcdcSAmir Goldstein cleanup: 6386b52243fSMiklos Szeredi ovl_cleanup(wdir, temp); 6396b52243fSMiklos Szeredi dput(temp); 640b10cdcdcSAmir Goldstein goto unlock; 641b10cdcdcSAmir Goldstein } 642b10cdcdcSAmir Goldstein 643b10cdcdcSAmir Goldstein /* Copyup using O_TMPFILE which does not require cross dir locking */ 644b10cdcdcSAmir Goldstein static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c) 645b10cdcdcSAmir Goldstein { 6466b52243fSMiklos Szeredi struct inode *udir = d_inode(c->destdir); 6476b52243fSMiklos Szeredi struct dentry *temp, *upper; 6486b52243fSMiklos Szeredi struct ovl_cu_creds cc; 649b10cdcdcSAmir Goldstein int err; 650b10cdcdcSAmir Goldstein 6516b52243fSMiklos Szeredi err = ovl_prep_cu_creds(c->dentry, &cc); 6526b52243fSMiklos Szeredi if (err) 6536b52243fSMiklos Szeredi return err; 6546b52243fSMiklos Szeredi 6556b52243fSMiklos Szeredi temp = ovl_do_tmpfile(c->workdir, c->stat.mode); 6566b52243fSMiklos Szeredi ovl_revert_cu_creds(&cc); 6576b52243fSMiklos Szeredi 658b148cba4SMiklos Szeredi if (IS_ERR(temp)) 659b148cba4SMiklos Szeredi return PTR_ERR(temp); 660e9be9d5eSMiklos Szeredi 66123f0ab13SMiklos Szeredi err = ovl_copy_up_inode(c, temp); 662e9be9d5eSMiklos Szeredi if (err) 6636b52243fSMiklos Szeredi goto out_dput; 6643a1e819bSAmir Goldstein 6656b52243fSMiklos Szeredi inode_lock_nested(udir, I_MUTEX_PARENT); 6666b52243fSMiklos Szeredi 6676b52243fSMiklos Szeredi upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len); 6686b52243fSMiklos Szeredi err = PTR_ERR(upper); 6696b52243fSMiklos Szeredi if (!IS_ERR(upper)) { 6706b52243fSMiklos Szeredi err = ovl_do_link(temp, udir, upper); 6716b52243fSMiklos Szeredi dput(upper); 6726b52243fSMiklos Szeredi } 6736b52243fSMiklos Szeredi inode_unlock(udir); 6746b52243fSMiklos Szeredi 675e9be9d5eSMiklos Szeredi if (err) 6766b52243fSMiklos Szeredi goto out_dput; 677e9be9d5eSMiklos Szeredi 6780c288874SVivek Goyal if (!c->metacopy) 6790c288874SVivek Goyal ovl_set_upperdata(d_inode(c->dentry)); 6806b52243fSMiklos Szeredi ovl_inode_update(d_inode(c->dentry), temp); 681b79e05aaSAmir Goldstein 6826b52243fSMiklos Szeredi return 0; 6836b52243fSMiklos Szeredi 6846b52243fSMiklos Szeredi out_dput: 685e85f82ffSMiklos Szeredi dput(temp); 686e9be9d5eSMiklos Szeredi return err; 687e9be9d5eSMiklos Szeredi } 688e9be9d5eSMiklos Szeredi 689e9be9d5eSMiklos Szeredi /* 690e9be9d5eSMiklos Szeredi * Copy up a single dentry 691e9be9d5eSMiklos Szeredi * 692a6c60655SMiklos Szeredi * All renames start with copy up of source if necessary. The actual 693a6c60655SMiklos Szeredi * rename will only proceed once the copy up was successful. Copy up uses 694a6c60655SMiklos Szeredi * upper parent i_mutex for exclusion. Since rename can change d_parent it 695a6c60655SMiklos Szeredi * is possible that the copy up will lock the old parent. At that point 696a6c60655SMiklos Szeredi * the file will have already been copied up anyway. 697e9be9d5eSMiklos Szeredi */ 698a6fb235aSMiklos Szeredi static int ovl_do_copy_up(struct ovl_copy_up_ctx *c) 699e9be9d5eSMiklos Szeredi { 700e9be9d5eSMiklos Szeredi int err; 70123f0ab13SMiklos Szeredi struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info; 702016b720fSAmir Goldstein bool to_index = false; 70359be0971SAmir Goldstein 704016b720fSAmir Goldstein /* 705016b720fSAmir Goldstein * Indexed non-dir is copied up directly to the index entry and then 706016b720fSAmir Goldstein * hardlinked to upper dir. Indexed dir is copied up to indexdir, 707016b720fSAmir Goldstein * then index entry is created and then copied up dir installed. 708016b720fSAmir Goldstein * Copying dir up to indexdir instead of workdir simplifies locking. 709016b720fSAmir Goldstein */ 710016b720fSAmir Goldstein if (ovl_need_index(c->dentry)) { 711016b720fSAmir Goldstein c->indexed = true; 712016b720fSAmir Goldstein if (S_ISDIR(c->stat.mode)) 713016b720fSAmir Goldstein c->workdir = ovl_indexdir(c->dentry->d_sb); 714016b720fSAmir Goldstein else 715016b720fSAmir Goldstein to_index = true; 716016b720fSAmir Goldstein } 717016b720fSAmir Goldstein 718016b720fSAmir Goldstein if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index) 71959be0971SAmir Goldstein c->origin = true; 72059be0971SAmir Goldstein 721016b720fSAmir Goldstein if (to_index) { 72259be0971SAmir Goldstein c->destdir = ovl_indexdir(c->dentry->d_sb); 72359be0971SAmir Goldstein err = ovl_get_index_name(c->lowerpath.dentry, &c->destname); 724f3a15685SAmir Goldstein if (err) 725f3a15685SAmir Goldstein return err; 726aa3ff3c1SAmir Goldstein } else if (WARN_ON(!c->parent)) { 727aa3ff3c1SAmir Goldstein /* Disconnected dentry must be copied up to index dir */ 728aa3ff3c1SAmir Goldstein return -EIO; 72959be0971SAmir Goldstein } else { 73059be0971SAmir Goldstein /* 73159be0971SAmir Goldstein * Mark parent "impure" because it may now contain non-pure 73259be0971SAmir Goldstein * upper 73359be0971SAmir Goldstein */ 73459be0971SAmir Goldstein err = ovl_set_impure(c->parent, c->destdir); 73559be0971SAmir Goldstein if (err) 73659be0971SAmir Goldstein return err; 73759be0971SAmir Goldstein } 738f3a15685SAmir Goldstein 73901ad3eb8SAmir Goldstein /* Should we copyup with O_TMPFILE or with workdir? */ 740b10cdcdcSAmir Goldstein if (S_ISREG(c->stat.mode) && ofs->tmpfile) 741b10cdcdcSAmir Goldstein err = ovl_copy_up_tmpfile(c); 742b10cdcdcSAmir Goldstein else 743b10cdcdcSAmir Goldstein err = ovl_copy_up_workdir(c); 744aa3ff3c1SAmir Goldstein if (err) 745aa3ff3c1SAmir Goldstein goto out; 746aa3ff3c1SAmir Goldstein 747aa3ff3c1SAmir Goldstein if (c->indexed) 74859be0971SAmir Goldstein ovl_set_flag(OVL_INDEX, d_inode(c->dentry)); 749016b720fSAmir Goldstein 750016b720fSAmir Goldstein if (to_index) { 751aa3ff3c1SAmir Goldstein /* Initialize nlink for copy up of disconnected dentry */ 752aa3ff3c1SAmir Goldstein err = ovl_set_nlink_upper(c->dentry); 753aa3ff3c1SAmir Goldstein } else { 75459be0971SAmir Goldstein struct inode *udir = d_inode(c->destdir); 75559be0971SAmir Goldstein 75659be0971SAmir Goldstein /* Restore timestamps on parent (best effort) */ 75759be0971SAmir Goldstein inode_lock(udir); 75859be0971SAmir Goldstein ovl_set_timestamps(c->destdir, &c->pstat); 75959be0971SAmir Goldstein inode_unlock(udir); 76059be0971SAmir Goldstein 76159be0971SAmir Goldstein ovl_dentry_set_upper_alias(c->dentry); 762fd210b7dSMiklos Szeredi } 763a6fb235aSMiklos Szeredi 764aa3ff3c1SAmir Goldstein out: 765aa3ff3c1SAmir Goldstein if (to_index) 766aa3ff3c1SAmir Goldstein kfree(c->destname.name); 767a6fb235aSMiklos Szeredi return err; 768a6fb235aSMiklos Szeredi } 769a6fb235aSMiklos Szeredi 77044d5bf10SVivek Goyal static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode, 77144d5bf10SVivek Goyal int flags) 77244d5bf10SVivek Goyal { 77344d5bf10SVivek Goyal struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 77444d5bf10SVivek Goyal 77544d5bf10SVivek Goyal if (!ofs->config.metacopy) 77644d5bf10SVivek Goyal return false; 77744d5bf10SVivek Goyal 77844d5bf10SVivek Goyal if (!S_ISREG(mode)) 77944d5bf10SVivek Goyal return false; 78044d5bf10SVivek Goyal 78144d5bf10SVivek Goyal if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC))) 78244d5bf10SVivek Goyal return false; 78344d5bf10SVivek Goyal 78444d5bf10SVivek Goyal return true; 78544d5bf10SVivek Goyal } 78644d5bf10SVivek Goyal 7870c288874SVivek Goyal /* Copy up data of an inode which was copied up metadata only in the past. */ 7880c288874SVivek Goyal static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c) 7890c288874SVivek Goyal { 790c86243b0SVivek Goyal struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 7914f93b426SVivek Goyal struct path upperpath, datapath; 7920c288874SVivek Goyal int err; 793993a0b2aSVivek Goyal char *capability = NULL; 7943f649ab7SKees Cook ssize_t cap_size; 7950c288874SVivek Goyal 7960c288874SVivek Goyal ovl_path_upper(c->dentry, &upperpath); 7970c288874SVivek Goyal if (WARN_ON(upperpath.dentry == NULL)) 7980c288874SVivek Goyal return -EIO; 7990c288874SVivek Goyal 8004f93b426SVivek Goyal ovl_path_lowerdata(c->dentry, &datapath); 8014f93b426SVivek Goyal if (WARN_ON(datapath.dentry == NULL)) 8024f93b426SVivek Goyal return -EIO; 8034f93b426SVivek Goyal 804993a0b2aSVivek Goyal if (c->stat.size) { 805993a0b2aSVivek Goyal err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS, 806993a0b2aSVivek Goyal &capability, 0); 807993a0b2aSVivek Goyal if (err < 0 && err != -ENODATA) 808993a0b2aSVivek Goyal goto out; 809993a0b2aSVivek Goyal } 810993a0b2aSVivek Goyal 811c86243b0SVivek Goyal err = ovl_copy_up_data(ofs, &datapath, &upperpath, c->stat.size); 8120c288874SVivek Goyal if (err) 813993a0b2aSVivek Goyal goto out_free; 814993a0b2aSVivek Goyal 815993a0b2aSVivek Goyal /* 816993a0b2aSVivek Goyal * Writing to upper file will clear security.capability xattr. We 817993a0b2aSVivek Goyal * don't want that to happen for normal copy-up operation. 818993a0b2aSVivek Goyal */ 819993a0b2aSVivek Goyal if (capability) { 820993a0b2aSVivek Goyal err = ovl_do_setxattr(upperpath.dentry, XATTR_NAME_CAPS, 821993a0b2aSVivek Goyal capability, cap_size, 0); 822993a0b2aSVivek Goyal if (err) 823993a0b2aSVivek Goyal goto out_free; 824993a0b2aSVivek Goyal } 825993a0b2aSVivek Goyal 8260c288874SVivek Goyal 8270c288874SVivek Goyal err = vfs_removexattr(upperpath.dentry, OVL_XATTR_METACOPY); 8280c288874SVivek Goyal if (err) 829993a0b2aSVivek Goyal goto out_free; 8300c288874SVivek Goyal 8310c288874SVivek Goyal ovl_set_upperdata(d_inode(c->dentry)); 832993a0b2aSVivek Goyal out_free: 833993a0b2aSVivek Goyal kfree(capability); 834993a0b2aSVivek Goyal out: 8350c288874SVivek Goyal return err; 8360c288874SVivek Goyal } 8370c288874SVivek Goyal 838a6fb235aSMiklos Szeredi static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, 839a6fb235aSMiklos Szeredi int flags) 840a6fb235aSMiklos Szeredi { 841a6fb235aSMiklos Szeredi int err; 842a6fb235aSMiklos Szeredi DEFINE_DELAYED_CALL(done); 843a6fb235aSMiklos Szeredi struct path parentpath; 844a6fb235aSMiklos Szeredi struct ovl_copy_up_ctx ctx = { 845a6fb235aSMiklos Szeredi .parent = parent, 846a6fb235aSMiklos Szeredi .dentry = dentry, 847a6fb235aSMiklos Szeredi .workdir = ovl_workdir(dentry), 848a6fb235aSMiklos Szeredi }; 849a6fb235aSMiklos Szeredi 850a6fb235aSMiklos Szeredi if (WARN_ON(!ctx.workdir)) 851a6fb235aSMiklos Szeredi return -EROFS; 852a6fb235aSMiklos Szeredi 853a6fb235aSMiklos Szeredi ovl_path_lower(dentry, &ctx.lowerpath); 854a6fb235aSMiklos Szeredi err = vfs_getattr(&ctx.lowerpath, &ctx.stat, 855a6fb235aSMiklos Szeredi STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT); 856a6fb235aSMiklos Szeredi if (err) 857a6fb235aSMiklos Szeredi return err; 858a6fb235aSMiklos Szeredi 85944d5bf10SVivek Goyal ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags); 86044d5bf10SVivek Goyal 861aa3ff3c1SAmir Goldstein if (parent) { 862a6fb235aSMiklos Szeredi ovl_path_upper(parent, &parentpath); 86359be0971SAmir Goldstein ctx.destdir = parentpath.dentry; 86459be0971SAmir Goldstein ctx.destname = dentry->d_name; 865a6fb235aSMiklos Szeredi 866a6fb235aSMiklos Szeredi err = vfs_getattr(&parentpath, &ctx.pstat, 867aa3ff3c1SAmir Goldstein STATX_ATIME | STATX_MTIME, 868aa3ff3c1SAmir Goldstein AT_STATX_SYNC_AS_STAT); 869a6fb235aSMiklos Szeredi if (err) 870a6fb235aSMiklos Szeredi return err; 871aa3ff3c1SAmir Goldstein } 872a6fb235aSMiklos Szeredi 873a6fb235aSMiklos Szeredi /* maybe truncate regular file. this has no effect on dirs */ 874a6fb235aSMiklos Szeredi if (flags & O_TRUNC) 875a6fb235aSMiklos Szeredi ctx.stat.size = 0; 876a6fb235aSMiklos Szeredi 877a6fb235aSMiklos Szeredi if (S_ISLNK(ctx.stat.mode)) { 878a6fb235aSMiklos Szeredi ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done); 879a6fb235aSMiklos Szeredi if (IS_ERR(ctx.link)) 880a6fb235aSMiklos Szeredi return PTR_ERR(ctx.link); 881a6fb235aSMiklos Szeredi } 882a6fb235aSMiklos Szeredi 8830c288874SVivek Goyal err = ovl_copy_up_start(dentry, flags); 884fd210b7dSMiklos Szeredi /* err < 0: interrupted, err > 0: raced with another copy-up */ 885fd210b7dSMiklos Szeredi if (unlikely(err)) { 886fd210b7dSMiklos Szeredi if (err > 0) 887fd210b7dSMiklos Szeredi err = 0; 888fd210b7dSMiklos Szeredi } else { 88959be0971SAmir Goldstein if (!ovl_dentry_upper(dentry)) 890a6fb235aSMiklos Szeredi err = ovl_do_copy_up(&ctx); 891aa3ff3c1SAmir Goldstein if (!err && parent && !ovl_dentry_has_upper_alias(dentry)) 892f4439de1SAmir Goldstein err = ovl_link_up(&ctx); 8930c288874SVivek Goyal if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags)) 8940c288874SVivek Goyal err = ovl_copy_up_meta_inode_data(&ctx); 895fd210b7dSMiklos Szeredi ovl_copy_up_end(dentry); 896fd210b7dSMiklos Szeredi } 8977764235bSMiklos Szeredi do_delayed_call(&done); 898e9be9d5eSMiklos Szeredi 899e9be9d5eSMiklos Szeredi return err; 900e9be9d5eSMiklos Szeredi } 901e9be9d5eSMiklos Szeredi 9025ac8e802Syoungjun static int ovl_copy_up_flags(struct dentry *dentry, int flags) 903e9be9d5eSMiklos Szeredi { 9048eac98b8SVivek Goyal int err = 0; 9058eac98b8SVivek Goyal const struct cred *old_cred = ovl_override_creds(dentry->d_sb); 906aa3ff3c1SAmir Goldstein bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED); 907aa3ff3c1SAmir Goldstein 908aa3ff3c1SAmir Goldstein /* 909aa3ff3c1SAmir Goldstein * With NFS export, copy up can get called for a disconnected non-dir. 910aa3ff3c1SAmir Goldstein * In this case, we will copy up lower inode to index dir without 911aa3ff3c1SAmir Goldstein * linking it to upper dir. 912aa3ff3c1SAmir Goldstein */ 913aa3ff3c1SAmir Goldstein if (WARN_ON(disconnected && d_is_dir(dentry))) 914aa3ff3c1SAmir Goldstein return -EIO; 915e9be9d5eSMiklos Szeredi 916e9be9d5eSMiklos Szeredi while (!err) { 917e9be9d5eSMiklos Szeredi struct dentry *next; 918aa3ff3c1SAmir Goldstein struct dentry *parent = NULL; 919e9be9d5eSMiklos Szeredi 9200c288874SVivek Goyal if (ovl_already_copied_up(dentry, flags)) 921e9be9d5eSMiklos Szeredi break; 922e9be9d5eSMiklos Szeredi 923e9be9d5eSMiklos Szeredi next = dget(dentry); 924e9be9d5eSMiklos Szeredi /* find the topmost dentry not yet copied up */ 925aa3ff3c1SAmir Goldstein for (; !disconnected;) { 926e9be9d5eSMiklos Szeredi parent = dget_parent(next); 927e9be9d5eSMiklos Szeredi 92859be0971SAmir Goldstein if (ovl_dentry_upper(parent)) 929e9be9d5eSMiklos Szeredi break; 930e9be9d5eSMiklos Szeredi 931e9be9d5eSMiklos Szeredi dput(next); 932e9be9d5eSMiklos Szeredi next = parent; 933e9be9d5eSMiklos Szeredi } 934e9be9d5eSMiklos Szeredi 935a6fb235aSMiklos Szeredi err = ovl_copy_up_one(parent, next, flags); 936e9be9d5eSMiklos Szeredi 937e9be9d5eSMiklos Szeredi dput(parent); 938e9be9d5eSMiklos Szeredi dput(next); 939e9be9d5eSMiklos Szeredi } 9408eac98b8SVivek Goyal revert_creds(old_cred); 941e9be9d5eSMiklos Szeredi 942e9be9d5eSMiklos Szeredi return err; 943e9be9d5eSMiklos Szeredi } 9449aba6521SAmir Goldstein 945d6eac039SVivek Goyal static bool ovl_open_need_copy_up(struct dentry *dentry, int flags) 946d6eac039SVivek Goyal { 947d6eac039SVivek Goyal /* Copy up of disconnected dentry does not set upper alias */ 9480c288874SVivek Goyal if (ovl_already_copied_up(dentry, flags)) 949d6eac039SVivek Goyal return false; 950d6eac039SVivek Goyal 951d6eac039SVivek Goyal if (special_file(d_inode(dentry)->i_mode)) 952d6eac039SVivek Goyal return false; 953d6eac039SVivek Goyal 9540c288874SVivek Goyal if (!ovl_open_flags_need_copy_up(flags)) 955d6eac039SVivek Goyal return false; 956d6eac039SVivek Goyal 957d6eac039SVivek Goyal return true; 958d6eac039SVivek Goyal } 959d6eac039SVivek Goyal 9603428030dSAmir Goldstein int ovl_maybe_copy_up(struct dentry *dentry, int flags) 961d6eac039SVivek Goyal { 962d6eac039SVivek Goyal int err = 0; 963d6eac039SVivek Goyal 9643428030dSAmir Goldstein if (ovl_open_need_copy_up(dentry, flags)) { 965d6eac039SVivek Goyal err = ovl_want_write(dentry); 966d6eac039SVivek Goyal if (!err) { 9673428030dSAmir Goldstein err = ovl_copy_up_flags(dentry, flags); 968d6eac039SVivek Goyal ovl_drop_write(dentry); 969d6eac039SVivek Goyal } 970d6eac039SVivek Goyal } 971d6eac039SVivek Goyal 972d6eac039SVivek Goyal return err; 973d6eac039SVivek Goyal } 974d6eac039SVivek Goyal 975d1e6f6a9SVivek Goyal int ovl_copy_up_with_data(struct dentry *dentry) 976d1e6f6a9SVivek Goyal { 977d1e6f6a9SVivek Goyal return ovl_copy_up_flags(dentry, O_WRONLY); 978d1e6f6a9SVivek Goyal } 979d1e6f6a9SVivek Goyal 9809aba6521SAmir Goldstein int ovl_copy_up(struct dentry *dentry) 9819aba6521SAmir Goldstein { 9829aba6521SAmir Goldstein return ovl_copy_up_flags(dentry, 0); 9839aba6521SAmir Goldstein } 984