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