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 | { |
Andreas Gruenbacher | 58ed4e7 | 2016-05-26 02:01:47 +0200 | [diff] [blame] | 120 | struct ovl_entry *oe = inode->i_private; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 121 | bool is_upper; |
Andreas Gruenbacher | 58ed4e7 | 2016-05-26 02:01:47 +0200 | [diff] [blame] | 122 | struct dentry *realdentry = ovl_entry_real(oe, &is_upper); |
| 123 | struct inode *realinode; |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 124 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 125 | int err; |
| 126 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 127 | /* Careful in RCU walk mode */ |
Andreas Gruenbacher | 58ed4e7 | 2016-05-26 02:01:47 +0200 | [diff] [blame] | 128 | realinode = d_inode_rcu(realdentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 129 | if (!realinode) { |
| 130 | WARN_ON(!(mask & MAY_NOT_BLOCK)); |
Andreas Gruenbacher | 58ed4e7 | 2016-05-26 02:01:47 +0200 | [diff] [blame] | 131 | return -ENOENT; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 134 | /* |
| 135 | * Check overlay inode with the creds of task and underlying inode |
| 136 | * with creds of mounter |
| 137 | */ |
| 138 | err = generic_permission(inode, mask); |
| 139 | if (err) |
| 140 | return err; |
| 141 | |
| 142 | old_cred = ovl_override_creds(inode->i_sb); |
Vivek Goyal | 754f8cb | 2016-07-01 16:34:29 -0400 | [diff] [blame] | 143 | if (!is_upper) |
| 144 | mask &= ~(MAY_WRITE | MAY_APPEND); |
Miklos Szeredi | 9c630eb | 2016-07-29 12:05:23 +0200 | [diff] [blame^] | 145 | err = inode_permission(realinode, mask); |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 146 | revert_creds(old_cred); |
| 147 | |
| 148 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 151 | static const char *ovl_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 152 | struct inode *inode, |
| 153 | struct delayed_call *done) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 154 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 155 | struct dentry *realdentry; |
| 156 | struct inode *realinode; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 157 | const struct cred *old_cred; |
| 158 | const char *p; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 159 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 160 | if (!dentry) |
| 161 | return ERR_PTR(-ECHILD); |
| 162 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 163 | realdentry = ovl_dentry_real(dentry); |
| 164 | realinode = realdentry->d_inode; |
| 165 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 166 | if (WARN_ON(!realinode->i_op->get_link)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 167 | return ERR_PTR(-EPERM); |
| 168 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 169 | old_cred = ovl_override_creds(dentry->d_sb); |
| 170 | p = realinode->i_op->get_link(realdentry, realinode, done); |
| 171 | revert_creds(old_cred); |
| 172 | return p; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz) |
| 176 | { |
| 177 | struct path realpath; |
| 178 | struct inode *realinode; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 179 | const struct cred *old_cred; |
| 180 | int err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 181 | |
| 182 | ovl_path_real(dentry, &realpath); |
| 183 | realinode = realpath.dentry->d_inode; |
| 184 | |
| 185 | if (!realinode->i_op->readlink) |
| 186 | return -EINVAL; |
| 187 | |
| 188 | touch_atime(&realpath); |
| 189 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 190 | old_cred = ovl_override_creds(dentry->d_sb); |
| 191 | err = realinode->i_op->readlink(realpath.dentry, buf, bufsiz); |
| 192 | revert_creds(old_cred); |
| 193 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 194 | } |
| 195 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 196 | static bool ovl_is_private_xattr(const char *name) |
| 197 | { |
hujianyang | cead89b | 2014-11-24 18:25:21 +0800 | [diff] [blame] | 198 | return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Al Viro | 3767e25 | 2016-05-27 11:06:05 -0400 | [diff] [blame] | 201 | int ovl_setxattr(struct dentry *dentry, struct inode *inode, |
| 202 | const char *name, const void *value, |
| 203 | size_t size, int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 204 | { |
| 205 | int err; |
| 206 | struct dentry *upperdentry; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 207 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 208 | |
| 209 | err = ovl_want_write(dentry); |
| 210 | if (err) |
| 211 | goto out; |
| 212 | |
| 213 | err = -EPERM; |
| 214 | if (ovl_is_private_xattr(name)) |
| 215 | goto out_drop_write; |
| 216 | |
| 217 | err = ovl_copy_up(dentry); |
| 218 | if (err) |
| 219 | goto out_drop_write; |
| 220 | |
| 221 | upperdentry = ovl_dentry_upper(dentry); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 222 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 223 | err = vfs_setxattr(upperdentry, name, value, size, flags); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 224 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 225 | |
| 226 | out_drop_write: |
| 227 | ovl_drop_write(dentry); |
| 228 | out: |
| 229 | return err; |
| 230 | } |
| 231 | |
Al Viro | ce23e64 | 2016-04-11 00:48:00 -0400 | [diff] [blame] | 232 | ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode, |
| 233 | const char *name, void *value, size_t size) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 234 | { |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 235 | struct dentry *realdentry = ovl_dentry_real(dentry); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 236 | ssize_t res; |
| 237 | const struct cred *old_cred; |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 238 | |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 239 | if (ovl_is_private_xattr(name)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 240 | return -ENODATA; |
| 241 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 242 | old_cred = ovl_override_creds(dentry->d_sb); |
| 243 | res = vfs_getxattr(realdentry, name, value, size); |
| 244 | revert_creds(old_cred); |
| 245 | return res; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) |
| 249 | { |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 250 | struct dentry *realdentry = ovl_dentry_real(dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 251 | ssize_t res; |
| 252 | int off; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 253 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 254 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 255 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 256 | res = vfs_listxattr(realdentry, list, size); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 257 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 258 | if (res <= 0 || size == 0) |
| 259 | return res; |
| 260 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 261 | /* filter out private xattrs */ |
| 262 | for (off = 0; off < res;) { |
| 263 | char *s = list + off; |
| 264 | size_t slen = strlen(s) + 1; |
| 265 | |
| 266 | BUG_ON(off + slen > res); |
| 267 | |
| 268 | if (ovl_is_private_xattr(s)) { |
| 269 | res -= slen; |
| 270 | memmove(s, s + slen, res - off); |
| 271 | } else { |
| 272 | off += slen; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return res; |
| 277 | } |
| 278 | |
| 279 | int ovl_removexattr(struct dentry *dentry, const char *name) |
| 280 | { |
| 281 | int err; |
| 282 | struct path realpath; |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 283 | enum ovl_path_type type = ovl_path_real(dentry, &realpath); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 284 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 285 | |
| 286 | err = ovl_want_write(dentry); |
| 287 | if (err) |
| 288 | goto out; |
| 289 | |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 290 | err = -ENODATA; |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 291 | if (ovl_is_private_xattr(name)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 292 | goto out_drop_write; |
| 293 | |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 294 | if (!OVL_TYPE_UPPER(type)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 295 | err = vfs_getxattr(realpath.dentry, name, NULL, 0); |
| 296 | if (err < 0) |
| 297 | goto out_drop_write; |
| 298 | |
| 299 | err = ovl_copy_up(dentry); |
| 300 | if (err) |
| 301 | goto out_drop_write; |
| 302 | |
| 303 | ovl_path_upper(dentry, &realpath); |
| 304 | } |
| 305 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 306 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 307 | err = vfs_removexattr(realpath.dentry, name); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 308 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 309 | out_drop_write: |
| 310 | ovl_drop_write(dentry); |
| 311 | out: |
| 312 | return err; |
| 313 | } |
| 314 | |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 315 | struct posix_acl *ovl_get_acl(struct inode *inode, int type) |
| 316 | { |
| 317 | struct inode *realinode = ovl_inode_real(inode); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 318 | const struct cred *old_cred; |
| 319 | struct posix_acl *acl; |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 320 | |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 321 | if (!IS_POSIXACL(realinode)) |
| 322 | return NULL; |
| 323 | |
| 324 | if (!realinode->i_op->get_acl) |
| 325 | return NULL; |
| 326 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 327 | old_cred = ovl_override_creds(inode->i_sb); |
| 328 | acl = realinode->i_op->get_acl(realinode, type); |
| 329 | revert_creds(old_cred); |
| 330 | |
| 331 | return acl; |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 332 | } |
| 333 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 334 | static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type, |
| 335 | struct dentry *realdentry) |
| 336 | { |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 337 | if (OVL_TYPE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 338 | return false; |
| 339 | |
| 340 | if (special_file(realdentry->d_inode->i_mode)) |
| 341 | return false; |
| 342 | |
| 343 | if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC)) |
| 344 | return false; |
| 345 | |
| 346 | return true; |
| 347 | } |
| 348 | |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 349 | 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] | 350 | { |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 351 | int err = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 352 | struct path realpath; |
| 353 | enum ovl_path_type type; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 354 | |
| 355 | type = ovl_path_real(dentry, &realpath); |
David Howells | 4bacc9c | 2015-06-18 14:32:31 +0100 | [diff] [blame] | 356 | if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 357 | err = ovl_want_write(dentry); |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 358 | if (!err) { |
| 359 | if (file_flags & O_TRUNC) |
| 360 | err = ovl_copy_up_truncate(dentry); |
| 361 | else |
| 362 | err = ovl_copy_up(dentry); |
| 363 | ovl_drop_write(dentry); |
| 364 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 367 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static const struct inode_operations ovl_file_inode_operations = { |
| 371 | .setattr = ovl_setattr, |
| 372 | .permission = ovl_permission, |
| 373 | .getattr = ovl_getattr, |
| 374 | .setxattr = ovl_setxattr, |
| 375 | .getxattr = ovl_getxattr, |
| 376 | .listxattr = ovl_listxattr, |
| 377 | .removexattr = ovl_removexattr, |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 378 | .get_acl = ovl_get_acl, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | static const struct inode_operations ovl_symlink_inode_operations = { |
| 382 | .setattr = ovl_setattr, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 383 | .get_link = ovl_get_link, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 384 | .readlink = ovl_readlink, |
| 385 | .getattr = ovl_getattr, |
| 386 | .setxattr = ovl_setxattr, |
| 387 | .getxattr = ovl_getxattr, |
| 388 | .listxattr = ovl_listxattr, |
| 389 | .removexattr = ovl_removexattr, |
| 390 | }; |
| 391 | |
| 392 | struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, |
| 393 | struct ovl_entry *oe) |
| 394 | { |
| 395 | struct inode *inode; |
| 396 | |
| 397 | inode = new_inode(sb); |
| 398 | if (!inode) |
| 399 | return NULL; |
| 400 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 401 | inode->i_ino = get_next_ino(); |
| 402 | inode->i_mode = mode; |
| 403 | inode->i_flags |= S_NOATIME | S_NOCMTIME; |
Andreas Gruenbacher | 58ed4e7 | 2016-05-26 02:01:47 +0200 | [diff] [blame] | 404 | inode->i_private = oe; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 405 | |
Vivek Goyal | 07a2daa | 2016-07-01 16:34:25 -0400 | [diff] [blame] | 406 | mode &= S_IFMT; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 407 | switch (mode) { |
| 408 | case S_IFDIR: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 409 | inode->i_op = &ovl_dir_inode_operations; |
| 410 | inode->i_fop = &ovl_dir_operations; |
| 411 | break; |
| 412 | |
| 413 | case S_IFLNK: |
| 414 | inode->i_op = &ovl_symlink_inode_operations; |
| 415 | break; |
| 416 | |
| 417 | case S_IFREG: |
| 418 | case S_IFSOCK: |
| 419 | case S_IFBLK: |
| 420 | case S_IFCHR: |
| 421 | case S_IFIFO: |
| 422 | inode->i_op = &ovl_file_inode_operations; |
| 423 | break; |
| 424 | |
| 425 | default: |
| 426 | WARN(1, "illegal file type: %i\n", mode); |
| 427 | iput(inode); |
| 428 | inode = NULL; |
| 429 | } |
| 430 | |
| 431 | return inode; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 432 | } |