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 | |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/xattr.h> |
Miklos Szeredi | 5201dc4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 13 | #include <linux/posix_acl.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 14 | #include "overlayfs.h" |
| 15 | |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 16 | static int ovl_copy_up_truncate(struct dentry *dentry) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 17 | { |
| 18 | int err; |
| 19 | struct dentry *parent; |
| 20 | struct kstat stat; |
| 21 | struct path lowerpath; |
Vivek Goyal | 8eac98b | 2016-09-06 13:40:32 -0400 | [diff] [blame] | 22 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 23 | |
| 24 | parent = dget_parent(dentry); |
| 25 | err = ovl_copy_up(parent); |
| 26 | if (err) |
| 27 | goto out_dput_parent; |
| 28 | |
| 29 | ovl_path_lower(dentry, &lowerpath); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 30 | |
Vivek Goyal | 8eac98b | 2016-09-06 13:40:32 -0400 | [diff] [blame] | 31 | old_cred = ovl_override_creds(dentry->d_sb); |
| 32 | err = vfs_getattr(&lowerpath, &stat); |
| 33 | if (!err) { |
| 34 | stat.size = 0; |
| 35 | err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat); |
| 36 | } |
| 37 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 38 | |
| 39 | out_dput_parent: |
| 40 | dput(parent); |
| 41 | return err; |
| 42 | } |
| 43 | |
| 44 | int ovl_setattr(struct dentry *dentry, struct iattr *attr) |
| 45 | { |
| 46 | int err; |
| 47 | struct dentry *upperdentry; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 48 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 49 | |
Miklos Szeredi | cf9a678 | 2015-12-11 16:30:49 +0100 | [diff] [blame] | 50 | /* |
| 51 | * Check for permissions before trying to copy-up. This is redundant |
| 52 | * since it will be rechecked later by ->setattr() on upper dentry. But |
| 53 | * without this, copy-up can be triggered by just about anybody. |
| 54 | * |
| 55 | * We don't initialize inode->size, which just means that |
| 56 | * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not |
| 57 | * check for a swapfile (which this won't be anyway). |
| 58 | */ |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 59 | err = setattr_prepare(dentry, attr); |
Miklos Szeredi | cf9a678 | 2015-12-11 16:30:49 +0100 | [diff] [blame] | 60 | if (err) |
| 61 | return err; |
| 62 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 63 | err = ovl_want_write(dentry); |
| 64 | if (err) |
| 65 | goto out; |
| 66 | |
Miklos Szeredi | 03bea60 | 2016-06-29 16:03:55 +0200 | [diff] [blame] | 67 | if (attr->ia_valid & ATTR_SIZE) { |
| 68 | struct inode *realinode = d_inode(ovl_dentry_real(dentry)); |
| 69 | |
| 70 | err = -ETXTBSY; |
| 71 | if (atomic_read(&realinode->i_writecount) < 0) |
| 72 | goto out_drop_write; |
| 73 | } |
| 74 | |
Miklos Szeredi | acff81e | 2015-12-04 19:18:48 +0100 | [diff] [blame] | 75 | err = ovl_copy_up(dentry); |
| 76 | if (!err) { |
Miklos Szeredi | 03bea60 | 2016-06-29 16:03:55 +0200 | [diff] [blame] | 77 | struct inode *winode = NULL; |
| 78 | |
Miklos Szeredi | acff81e | 2015-12-04 19:18:48 +0100 | [diff] [blame] | 79 | upperdentry = ovl_dentry_upper(dentry); |
| 80 | |
Miklos Szeredi | 03bea60 | 2016-06-29 16:03:55 +0200 | [diff] [blame] | 81 | if (attr->ia_valid & ATTR_SIZE) { |
| 82 | winode = d_inode(upperdentry); |
| 83 | err = get_write_access(winode); |
| 84 | if (err) |
| 85 | goto out_drop_write; |
| 86 | } |
| 87 | |
Miklos Szeredi | b99c2d9 | 2016-07-04 16:49:48 +0200 | [diff] [blame] | 88 | if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
| 89 | attr->ia_valid &= ~ATTR_MODE; |
| 90 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 91 | inode_lock(upperdentry->d_inode); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 92 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 93 | err = notify_change(upperdentry, attr, NULL); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 94 | revert_creds(old_cred); |
Konstantin Khlebnikov | b81de06 | 2016-01-31 16:21:29 +0300 | [diff] [blame] | 95 | if (!err) |
| 96 | ovl_copyattr(upperdentry->d_inode, dentry->d_inode); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 97 | inode_unlock(upperdentry->d_inode); |
Miklos Szeredi | 03bea60 | 2016-06-29 16:03:55 +0200 | [diff] [blame] | 98 | |
| 99 | if (winode) |
| 100 | put_write_access(winode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 101 | } |
Miklos Szeredi | 03bea60 | 2016-06-29 16:03:55 +0200 | [diff] [blame] | 102 | out_drop_write: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 103 | ovl_drop_write(dentry); |
| 104 | out: |
| 105 | return err; |
| 106 | } |
| 107 | |
| 108 | static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 109 | struct kstat *stat) |
| 110 | { |
| 111 | struct path realpath; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 112 | const struct cred *old_cred; |
| 113 | int err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 114 | |
| 115 | ovl_path_real(dentry, &realpath); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 116 | old_cred = ovl_override_creds(dentry->d_sb); |
| 117 | err = vfs_getattr(&realpath, stat); |
| 118 | revert_creds(old_cred); |
| 119 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | int ovl_permission(struct inode *inode, int mask) |
| 123 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 124 | bool is_upper; |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 125 | struct inode *realinode = ovl_inode_real(inode, &is_upper); |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 126 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 127 | int err; |
| 128 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 129 | /* Careful in RCU walk mode */ |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 130 | if (!realinode) { |
| 131 | WARN_ON(!(mask & MAY_NOT_BLOCK)); |
Miklos Szeredi | a999d7e | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 132 | return -ECHILD; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 135 | /* |
| 136 | * Check overlay inode with the creds of task and underlying inode |
| 137 | * with creds of mounter |
| 138 | */ |
| 139 | err = generic_permission(inode, mask); |
| 140 | if (err) |
| 141 | return err; |
| 142 | |
| 143 | old_cred = ovl_override_creds(inode->i_sb); |
Vivek Goyal | 500cac3 | 2016-07-13 11:00:14 -0400 | [diff] [blame] | 144 | if (!is_upper && !special_file(realinode->i_mode) && mask & MAY_WRITE) { |
Vivek Goyal | 754f8cb | 2016-07-01 16:34:29 -0400 | [diff] [blame] | 145 | mask &= ~(MAY_WRITE | MAY_APPEND); |
Vivek Goyal | 500cac3 | 2016-07-13 11:00:14 -0400 | [diff] [blame] | 146 | /* Make sure mounter can read file for copy up later */ |
| 147 | mask |= MAY_READ; |
| 148 | } |
Miklos Szeredi | 9c630eb | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 149 | err = inode_permission(realinode, mask); |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 150 | revert_creds(old_cred); |
| 151 | |
| 152 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 155 | static const char *ovl_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 156 | struct inode *inode, |
| 157 | struct delayed_call *done) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 158 | { |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 159 | const struct cred *old_cred; |
| 160 | const char *p; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 161 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 162 | if (!dentry) |
| 163 | return ERR_PTR(-ECHILD); |
| 164 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 165 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | 7764235 | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 166 | p = vfs_get_link(ovl_dentry_real(dentry), done); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 167 | revert_creds(old_cred); |
| 168 | return p; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Miklos Szeredi | 0956254 | 2016-08-08 15:08:49 +0200 | [diff] [blame] | 171 | bool ovl_is_private_xattr(const char *name) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 172 | { |
Andreas Gruenbacher | fe2b759 | 2016-08-22 17:59:22 +0200 | [diff] [blame] | 173 | return strncmp(name, OVL_XATTR_PREFIX, |
| 174 | sizeof(OVL_XATTR_PREFIX) - 1) == 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 177 | int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value, |
| 178 | size_t size, int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 179 | { |
| 180 | int err; |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 181 | struct path realpath; |
| 182 | enum ovl_path_type type = ovl_path_real(dentry, &realpath); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 183 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 184 | |
| 185 | err = ovl_want_write(dentry); |
| 186 | if (err) |
| 187 | goto out; |
| 188 | |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 189 | if (!value && !OVL_TYPE_UPPER(type)) { |
| 190 | err = vfs_getxattr(realpath.dentry, name, NULL, 0); |
| 191 | if (err < 0) |
| 192 | goto out_drop_write; |
| 193 | } |
| 194 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 195 | err = ovl_copy_up(dentry); |
| 196 | if (err) |
| 197 | goto out_drop_write; |
| 198 | |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 199 | if (!OVL_TYPE_UPPER(type)) |
| 200 | ovl_path_upper(dentry, &realpath); |
| 201 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 202 | old_cred = ovl_override_creds(dentry->d_sb); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 203 | if (value) |
| 204 | err = vfs_setxattr(realpath.dentry, name, value, size, flags); |
| 205 | else { |
| 206 | WARN_ON(flags != XATTR_REPLACE); |
| 207 | err = vfs_removexattr(realpath.dentry, name); |
| 208 | } |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 209 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 210 | |
| 211 | out_drop_write: |
| 212 | ovl_drop_write(dentry); |
| 213 | out: |
| 214 | return err; |
| 215 | } |
| 216 | |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 217 | int ovl_xattr_get(struct dentry *dentry, const char *name, |
| 218 | void *value, size_t size) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 219 | { |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 220 | struct dentry *realdentry = ovl_dentry_real(dentry); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 221 | ssize_t res; |
| 222 | const struct cred *old_cred; |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 223 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 224 | old_cred = ovl_override_creds(dentry->d_sb); |
| 225 | res = vfs_getxattr(realdentry, name, value, size); |
| 226 | revert_creds(old_cred); |
| 227 | return res; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Miklos Szeredi | b2a40ea | 2017-05-29 15:15:27 +0200 | [diff] [blame] | 230 | static bool ovl_can_list(const char *s) |
| 231 | { |
| 232 | /* List all non-trusted xatts */ |
| 233 | if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0) |
| 234 | return true; |
| 235 | |
| 236 | /* Never list trusted.overlay, list other trusted for superuser only */ |
| 237 | return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN); |
| 238 | } |
| 239 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 240 | ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) |
| 241 | { |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 242 | struct dentry *realdentry = ovl_dentry_real(dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 243 | ssize_t res; |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 244 | size_t len; |
| 245 | char *s; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 246 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 247 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 248 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 249 | res = vfs_listxattr(realdentry, list, size); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 250 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 251 | if (res <= 0 || size == 0) |
| 252 | return res; |
| 253 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 254 | /* filter out private xattrs */ |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 255 | for (s = list, len = res; len;) { |
| 256 | size_t slen = strnlen(s, len) + 1; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 257 | |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 258 | /* underlying fs providing us with an broken xattr list? */ |
| 259 | if (WARN_ON(slen > len)) |
| 260 | return -EIO; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 261 | |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 262 | len -= slen; |
Miklos Szeredi | b2a40ea | 2017-05-29 15:15:27 +0200 | [diff] [blame] | 263 | if (!ovl_can_list(s)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 264 | res -= slen; |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 265 | memmove(s, s + slen, len); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 266 | } else { |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 267 | s += slen; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | return res; |
| 272 | } |
| 273 | |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 274 | struct posix_acl *ovl_get_acl(struct inode *inode, int type) |
| 275 | { |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 276 | struct inode *realinode = ovl_inode_real(inode, NULL); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 277 | const struct cred *old_cred; |
| 278 | struct posix_acl *acl; |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 279 | |
Miklos Szeredi | 5201dc4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 280 | if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode)) |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 281 | return NULL; |
| 282 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 283 | old_cred = ovl_override_creds(inode->i_sb); |
Miklos Szeredi | 5201dc4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 284 | acl = get_acl(realinode, type); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 285 | revert_creds(old_cred); |
| 286 | |
| 287 | return acl; |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 288 | } |
| 289 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 290 | static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type, |
| 291 | struct dentry *realdentry) |
| 292 | { |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 293 | if (OVL_TYPE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 294 | return false; |
| 295 | |
| 296 | if (special_file(realdentry->d_inode->i_mode)) |
| 297 | return false; |
| 298 | |
| 299 | if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC)) |
| 300 | return false; |
| 301 | |
| 302 | return true; |
| 303 | } |
| 304 | |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 305 | int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 306 | { |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 307 | int err = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 308 | struct path realpath; |
| 309 | enum ovl_path_type type; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 310 | |
| 311 | type = ovl_path_real(dentry, &realpath); |
David Howells | 4bacc9c | 2015-06-18 14:32:31 +0100 | [diff] [blame] | 312 | if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 313 | err = ovl_want_write(dentry); |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 314 | if (!err) { |
| 315 | if (file_flags & O_TRUNC) |
| 316 | err = ovl_copy_up_truncate(dentry); |
| 317 | else |
| 318 | err = ovl_copy_up(dentry); |
| 319 | ovl_drop_write(dentry); |
| 320 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 323 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 326 | int ovl_update_time(struct inode *inode, struct timespec *ts, int flags) |
| 327 | { |
| 328 | struct dentry *alias; |
| 329 | struct path upperpath; |
| 330 | |
| 331 | if (!(flags & S_ATIME)) |
| 332 | return 0; |
| 333 | |
| 334 | alias = d_find_any_alias(inode); |
| 335 | if (!alias) |
| 336 | return 0; |
| 337 | |
| 338 | ovl_path_upper(alias, &upperpath); |
| 339 | if (upperpath.dentry) { |
| 340 | touch_atime(&upperpath); |
| 341 | inode->i_atime = d_inode(upperpath.dentry)->i_atime; |
| 342 | } |
| 343 | |
| 344 | dput(alias); |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 349 | static const struct inode_operations ovl_file_inode_operations = { |
| 350 | .setattr = ovl_setattr, |
| 351 | .permission = ovl_permission, |
| 352 | .getattr = ovl_getattr, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 353 | .listxattr = ovl_listxattr, |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 354 | .get_acl = ovl_get_acl, |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 355 | .update_time = ovl_update_time, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | static const struct inode_operations ovl_symlink_inode_operations = { |
| 359 | .setattr = ovl_setattr, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 360 | .get_link = ovl_get_link, |
Miklos Szeredi | 78a3fa4 | 2016-10-04 14:40:44 +0200 | [diff] [blame] | 361 | .readlink = generic_readlink, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 362 | .getattr = ovl_getattr, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 363 | .listxattr = ovl_listxattr, |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 364 | .update_time = ovl_update_time, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 365 | }; |
| 366 | |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 367 | static void ovl_fill_inode(struct inode *inode, umode_t mode) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 368 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 369 | inode->i_ino = get_next_ino(); |
| 370 | inode->i_mode = mode; |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 371 | inode->i_flags |= S_NOCMTIME; |
Miklos Szeredi | 2a3a2a3 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 372 | #ifdef CONFIG_FS_POSIX_ACL |
| 373 | inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE; |
| 374 | #endif |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 375 | |
Vivek Goyal | 07a2daa | 2016-07-01 16:34:25 -0400 | [diff] [blame] | 376 | mode &= S_IFMT; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 377 | switch (mode) { |
| 378 | case S_IFDIR: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 379 | inode->i_op = &ovl_dir_inode_operations; |
| 380 | inode->i_fop = &ovl_dir_operations; |
| 381 | break; |
| 382 | |
| 383 | case S_IFLNK: |
| 384 | inode->i_op = &ovl_symlink_inode_operations; |
| 385 | break; |
| 386 | |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 387 | default: |
| 388 | WARN(1, "illegal file type: %i\n", mode); |
| 389 | /* Fall through */ |
| 390 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 391 | case S_IFREG: |
| 392 | case S_IFSOCK: |
| 393 | case S_IFBLK: |
| 394 | case S_IFCHR: |
| 395 | case S_IFIFO: |
| 396 | inode->i_op = &ovl_file_inode_operations; |
| 397 | break; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 398 | } |
| 399 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 400 | |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 401 | struct inode *ovl_new_inode(struct super_block *sb, umode_t mode) |
| 402 | { |
| 403 | struct inode *inode; |
| 404 | |
| 405 | inode = new_inode(sb); |
| 406 | if (inode) |
| 407 | ovl_fill_inode(inode, mode); |
| 408 | |
| 409 | return inode; |
| 410 | } |
| 411 | |
| 412 | static int ovl_inode_test(struct inode *inode, void *data) |
| 413 | { |
| 414 | return ovl_inode_real(inode, NULL) == data; |
| 415 | } |
| 416 | |
| 417 | static int ovl_inode_set(struct inode *inode, void *data) |
| 418 | { |
| 419 | inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode) |
| 424 | |
| 425 | { |
| 426 | struct inode *inode; |
| 427 | |
| 428 | inode = iget5_locked(sb, (unsigned long) realinode, |
| 429 | ovl_inode_test, ovl_inode_set, realinode); |
| 430 | if (inode && inode->i_state & I_NEW) { |
| 431 | ovl_fill_inode(inode, realinode->i_mode); |
| 432 | set_nlink(inode, realinode->i_nlink); |
| 433 | unlock_new_inode(inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | return inode; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 437 | } |