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