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> |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 15 | #include <linux/ratelimit.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 16 | #include "overlayfs.h" |
| 17 | |
Chandan Rajendra | ba1e563 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 18 | |
| 19 | static dev_t ovl_get_pseudo_dev(struct dentry *dentry) |
| 20 | { |
| 21 | struct ovl_entry *oe = dentry->d_fsdata; |
| 22 | |
| 23 | return oe->lowerstack[0].layer->pseudo_dev; |
| 24 | } |
| 25 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 26 | int ovl_setattr(struct dentry *dentry, struct iattr *attr) |
| 27 | { |
| 28 | int err; |
| 29 | struct dentry *upperdentry; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 30 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 31 | |
Miklos Szeredi | cf9a678 | 2015-12-11 16:30:49 +0100 | [diff] [blame] | 32 | /* |
| 33 | * Check for permissions before trying to copy-up. This is redundant |
| 34 | * since it will be rechecked later by ->setattr() on upper dentry. But |
| 35 | * without this, copy-up can be triggered by just about anybody. |
| 36 | * |
| 37 | * We don't initialize inode->size, which just means that |
| 38 | * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not |
| 39 | * check for a swapfile (which this won't be anyway). |
| 40 | */ |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 41 | err = setattr_prepare(dentry, attr); |
Miklos Szeredi | cf9a678 | 2015-12-11 16:30:49 +0100 | [diff] [blame] | 42 | if (err) |
| 43 | return err; |
| 44 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 45 | err = ovl_want_write(dentry); |
| 46 | if (err) |
| 47 | goto out; |
| 48 | |
Miklos Szeredi | acff81e | 2015-12-04 19:18:48 +0100 | [diff] [blame] | 49 | err = ovl_copy_up(dentry); |
| 50 | if (!err) { |
| 51 | upperdentry = ovl_dentry_upper(dentry); |
| 52 | |
Miklos Szeredi | b99c2d9 | 2016-07-04 16:49:48 +0200 | [diff] [blame] | 53 | if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
| 54 | attr->ia_valid &= ~ATTR_MODE; |
| 55 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 56 | inode_lock(upperdentry->d_inode); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 57 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 58 | err = notify_change(upperdentry, attr, NULL); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 59 | revert_creds(old_cred); |
Konstantin Khlebnikov | b81de06 | 2016-01-31 16:21:29 +0300 | [diff] [blame] | 60 | if (!err) |
| 61 | ovl_copyattr(upperdentry->d_inode, dentry->d_inode); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 62 | inode_unlock(upperdentry->d_inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 63 | } |
| 64 | ovl_drop_write(dentry); |
| 65 | out: |
| 66 | return err; |
| 67 | } |
| 68 | |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 69 | int ovl_getattr(const struct path *path, struct kstat *stat, |
| 70 | u32 request_mask, unsigned int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 71 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 72 | struct dentry *dentry = path->dentry; |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 73 | enum ovl_path_type type; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 74 | struct path realpath; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 75 | const struct cred *old_cred; |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 76 | bool is_dir = S_ISDIR(dentry->d_inode->i_mode); |
Amir Goldstein | a0c5ad3 | 2017-11-01 00:45:40 +0200 | [diff] [blame] | 77 | bool samefs = ovl_same_sb(dentry->d_sb); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 78 | int err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 79 | |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 80 | type = ovl_path_real(dentry, &realpath); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 81 | old_cred = ovl_override_creds(dentry->d_sb); |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 82 | err = vfs_getattr(&realpath, stat, request_mask, flags); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 83 | if (err) |
| 84 | goto out; |
| 85 | |
| 86 | /* |
Amir Goldstein | a0c5ad3 | 2017-11-01 00:45:40 +0200 | [diff] [blame] | 87 | * For non-dir or same fs, we use st_ino of the copy up origin, if we |
| 88 | * know it. This guaranties constant st_dev/st_ino across copy up. |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 89 | * |
| 90 | * If filesystem supports NFS export ops, this also guaranties |
| 91 | * persistent st_ino across mount cycle. |
| 92 | */ |
Amir Goldstein | a0c5ad3 | 2017-11-01 00:45:40 +0200 | [diff] [blame] | 93 | if (!is_dir || samefs) { |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 94 | if (OVL_TYPE_ORIGIN(type)) { |
| 95 | struct kstat lowerstat; |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 96 | u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 97 | |
| 98 | ovl_path_lower(dentry, &realpath); |
| 99 | err = vfs_getattr(&realpath, &lowerstat, |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 100 | lowermask, flags); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 101 | if (err) |
| 102 | goto out; |
| 103 | |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 104 | /* |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 105 | * Lower hardlinks may be broken on copy up to different |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 106 | * upper files, so we cannot use the lower origin st_ino |
| 107 | * for those different files, even for the same fs case. |
Amir Goldstein | 86eaa13 | 2017-11-21 13:55:51 +0200 | [diff] [blame] | 108 | * |
| 109 | * Similarly, several redirected dirs can point to the |
| 110 | * same dir on a lower layer. With the "verify_lower" |
| 111 | * feature, we do not use the lower origin st_ino, if |
| 112 | * we haven't verified that this redirect is unique. |
| 113 | * |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 114 | * With inodes index enabled, it is safe to use st_ino |
Amir Goldstein | 86eaa13 | 2017-11-21 13:55:51 +0200 | [diff] [blame] | 115 | * of an indexed origin. The index validates that the |
| 116 | * upper hardlink is not broken and that a redirected |
| 117 | * dir is the only redirect to that origin. |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 118 | */ |
Amir Goldstein | 86eaa13 | 2017-11-21 13:55:51 +0200 | [diff] [blame] | 119 | if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) || |
| 120 | (!ovl_verify_lower(dentry->d_sb) && |
Amir Goldstein | 9f99e50 | 2018-04-11 20:09:29 +0300 | [diff] [blame] | 121 | (is_dir || lowerstat.nlink == 1))) { |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 122 | stat->ino = lowerstat.ino; |
Amir Goldstein | a0c5ad3 | 2017-11-01 00:45:40 +0200 | [diff] [blame] | 123 | stat->dev = ovl_get_pseudo_dev(dentry); |
Amir Goldstein | 9f99e50 | 2018-04-11 20:09:29 +0300 | [diff] [blame] | 124 | } |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 125 | } |
Amir Goldstein | a0c5ad3 | 2017-11-01 00:45:40 +0200 | [diff] [blame] | 126 | if (samefs) { |
| 127 | /* |
| 128 | * When all layers are on the same fs, all real inode |
| 129 | * number are unique, so we use the overlay st_dev, |
| 130 | * which is friendly to du -x. |
| 131 | */ |
| 132 | stat->dev = dentry->d_sb->s_dev; |
| 133 | } else if (!OVL_TYPE_UPPER(type)) { |
| 134 | /* |
| 135 | * For non-samefs setup, to make sure that st_dev/st_ino |
| 136 | * pair is unique across the system, we use a unique |
| 137 | * anonymous st_dev for lower layer inode. |
| 138 | */ |
| 139 | stat->dev = ovl_get_pseudo_dev(dentry); |
| 140 | } |
| 141 | } else { |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 142 | /* |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 143 | * Always use the overlay st_dev for directories, so 'find |
| 144 | * -xdev' will scan the entire overlay mount and won't cross the |
| 145 | * overlay mount boundaries. |
Amir Goldstein | a0c5ad3 | 2017-11-01 00:45:40 +0200 | [diff] [blame] | 146 | * |
| 147 | * If not all layers are on the same fs the pair {real st_ino; |
| 148 | * overlay st_dev} is not unique, so use the non persistent |
| 149 | * overlay st_ino for directories. |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 150 | */ |
| 151 | stat->dev = dentry->d_sb->s_dev; |
| 152 | stat->ino = dentry->d_inode->i_ino; |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 153 | } |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | * It's probably not worth it to count subdirs to get the |
| 157 | * correct link count. nlink=1 seems to pacify 'find' and |
| 158 | * other utilities. |
| 159 | */ |
| 160 | if (is_dir && OVL_TYPE_MERGE(type)) |
| 161 | stat->nlink = 1; |
| 162 | |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 163 | /* |
| 164 | * Return the overlay inode nlinks for indexed upper inodes. |
| 165 | * Overlay inode nlink counts the union of the upper hardlinks |
| 166 | * and non-covered lower hardlinks. It does not include the upper |
| 167 | * index hardlink. |
| 168 | */ |
| 169 | if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry))) |
| 170 | stat->nlink = dentry->d_inode->i_nlink; |
| 171 | |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 172 | out: |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 173 | revert_creds(old_cred); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 174 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 175 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | int ovl_permission(struct inode *inode, int mask) |
| 179 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 180 | struct inode *upperinode = ovl_inode_upper(inode); |
| 181 | struct inode *realinode = upperinode ?: ovl_inode_lower(inode); |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 182 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 183 | int err; |
| 184 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 185 | /* Careful in RCU walk mode */ |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 186 | if (!realinode) { |
| 187 | WARN_ON(!(mask & MAY_NOT_BLOCK)); |
Miklos Szeredi | a999d7e | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 188 | return -ECHILD; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 191 | /* |
| 192 | * Check overlay inode with the creds of task and underlying inode |
| 193 | * with creds of mounter |
| 194 | */ |
| 195 | err = generic_permission(inode, mask); |
| 196 | if (err) |
| 197 | return err; |
| 198 | |
| 199 | old_cred = ovl_override_creds(inode->i_sb); |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 200 | if (!upperinode && |
| 201 | !special_file(realinode->i_mode) && mask & MAY_WRITE) { |
Vivek Goyal | 754f8cb | 2016-07-01 16:34:29 -0400 | [diff] [blame] | 202 | mask &= ~(MAY_WRITE | MAY_APPEND); |
Vivek Goyal | 500cac3 | 2016-07-13 11:00:14 -0400 | [diff] [blame] | 203 | /* Make sure mounter can read file for copy up later */ |
| 204 | mask |= MAY_READ; |
| 205 | } |
Miklos Szeredi | 9c630eb | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 206 | err = inode_permission(realinode, mask); |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 207 | revert_creds(old_cred); |
| 208 | |
| 209 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 212 | static const char *ovl_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 213 | struct inode *inode, |
| 214 | struct delayed_call *done) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 215 | { |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 216 | const struct cred *old_cred; |
| 217 | const char *p; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 218 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 219 | if (!dentry) |
| 220 | return ERR_PTR(-ECHILD); |
| 221 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 222 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 223 | p = vfs_get_link(ovl_dentry_real(dentry), done); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 224 | revert_creds(old_cred); |
| 225 | return p; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Miklos Szeredi | 0956254 | 2016-08-08 15:08:49 +0200 | [diff] [blame] | 228 | bool ovl_is_private_xattr(const char *name) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 229 | { |
Andreas Gruenbacher | fe2b759 | 2016-08-22 17:59:22 +0200 | [diff] [blame] | 230 | return strncmp(name, OVL_XATTR_PREFIX, |
| 231 | sizeof(OVL_XATTR_PREFIX) - 1) == 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 234 | int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name, |
| 235 | const void *value, size_t size, int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 236 | { |
| 237 | int err; |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 238 | struct dentry *upperdentry = ovl_i_dentry_upper(inode); |
| 239 | struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 240 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 241 | |
| 242 | err = ovl_want_write(dentry); |
| 243 | if (err) |
| 244 | goto out; |
| 245 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 246 | if (!value && !upperdentry) { |
| 247 | err = vfs_getxattr(realdentry, name, NULL, 0); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 248 | if (err < 0) |
| 249 | goto out_drop_write; |
| 250 | } |
| 251 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 252 | if (!upperdentry) { |
| 253 | err = ovl_copy_up(dentry); |
| 254 | if (err) |
| 255 | goto out_drop_write; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 256 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 257 | realdentry = ovl_dentry_upper(dentry); |
| 258 | } |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 259 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 260 | old_cred = ovl_override_creds(dentry->d_sb); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 261 | if (value) |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 262 | err = vfs_setxattr(realdentry, name, value, size, flags); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 263 | else { |
| 264 | WARN_ON(flags != XATTR_REPLACE); |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 265 | err = vfs_removexattr(realdentry, name); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 266 | } |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 267 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 268 | |
| 269 | out_drop_write: |
| 270 | ovl_drop_write(dentry); |
| 271 | out: |
| 272 | return err; |
| 273 | } |
| 274 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 275 | int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name, |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 276 | void *value, size_t size) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 277 | { |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 278 | ssize_t res; |
| 279 | const struct cred *old_cred; |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 280 | struct dentry *realdentry = |
| 281 | ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry); |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 282 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 283 | old_cred = ovl_override_creds(dentry->d_sb); |
| 284 | res = vfs_getxattr(realdentry, name, value, size); |
| 285 | revert_creds(old_cred); |
| 286 | return res; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 287 | } |
| 288 | |
Miklos Szeredi | a082c6f | 2017-05-29 15:15:27 +0200 | [diff] [blame] | 289 | static bool ovl_can_list(const char *s) |
| 290 | { |
| 291 | /* List all non-trusted xatts */ |
| 292 | if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0) |
| 293 | return true; |
| 294 | |
| 295 | /* Never list trusted.overlay, list other trusted for superuser only */ |
| 296 | return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN); |
| 297 | } |
| 298 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 299 | ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) |
| 300 | { |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 301 | struct dentry *realdentry = ovl_dentry_real(dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 302 | ssize_t res; |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 303 | size_t len; |
| 304 | char *s; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 305 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 306 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 307 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 308 | res = vfs_listxattr(realdentry, list, size); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 309 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 310 | if (res <= 0 || size == 0) |
| 311 | return res; |
| 312 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 313 | /* filter out private xattrs */ |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 314 | for (s = list, len = res; len;) { |
| 315 | size_t slen = strnlen(s, len) + 1; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 316 | |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 317 | /* underlying fs providing us with an broken xattr list? */ |
| 318 | if (WARN_ON(slen > len)) |
| 319 | return -EIO; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 320 | |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 321 | len -= slen; |
Miklos Szeredi | a082c6f | 2017-05-29 15:15:27 +0200 | [diff] [blame] | 322 | if (!ovl_can_list(s)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 323 | res -= slen; |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 324 | memmove(s, s + slen, len); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 325 | } else { |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 326 | s += slen; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | return res; |
| 331 | } |
| 332 | |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 333 | struct posix_acl *ovl_get_acl(struct inode *inode, int type) |
| 334 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 335 | struct inode *realinode = ovl_inode_real(inode); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 336 | const struct cred *old_cred; |
| 337 | struct posix_acl *acl; |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 338 | |
Miklos Szeredi | 5201dc4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 339 | if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode)) |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 340 | return NULL; |
| 341 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 342 | old_cred = ovl_override_creds(inode->i_sb); |
Miklos Szeredi | 5201dc4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 343 | acl = get_acl(realinode, type); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 344 | revert_creds(old_cred); |
| 345 | |
| 346 | return acl; |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 347 | } |
| 348 | |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 349 | static bool ovl_open_need_copy_up(struct dentry *dentry, int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 350 | { |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 351 | /* Copy up of disconnected dentry does not set upper alias */ |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 352 | if (ovl_dentry_upper(dentry) && |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 353 | (ovl_dentry_has_upper_alias(dentry) || |
| 354 | (dentry->d_flags & DCACHE_DISCONNECTED))) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 355 | return false; |
| 356 | |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 357 | if (special_file(d_inode(dentry)->i_mode)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 358 | return false; |
| 359 | |
| 360 | if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC)) |
| 361 | return false; |
| 362 | |
| 363 | return true; |
| 364 | } |
| 365 | |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 366 | 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] | 367 | { |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 368 | int err = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 369 | |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 370 | if (ovl_open_need_copy_up(dentry, file_flags)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 371 | err = ovl_want_write(dentry); |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 372 | if (!err) { |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 373 | err = ovl_copy_up_flags(dentry, file_flags); |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 374 | ovl_drop_write(dentry); |
| 375 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 378 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 381 | int ovl_update_time(struct inode *inode, struct timespec *ts, int flags) |
| 382 | { |
Miklos Szeredi | 8f35cf5 | 2018-04-12 12:04:50 +0200 | [diff] [blame^] | 383 | if (flags & S_ATIME) { |
| 384 | struct ovl_fs *ofs = inode->i_sb->s_fs_info; |
| 385 | struct path upperpath = { |
| 386 | .mnt = ofs->upper_mnt, |
| 387 | .dentry = ovl_upperdentry_dereference(OVL_I(inode)), |
| 388 | }; |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 389 | |
Miklos Szeredi | 8f35cf5 | 2018-04-12 12:04:50 +0200 | [diff] [blame^] | 390 | if (upperpath.dentry) { |
| 391 | touch_atime(&upperpath); |
| 392 | inode->i_atime = d_inode(upperpath.dentry)->i_atime; |
| 393 | } |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 394 | } |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 398 | static const struct inode_operations ovl_file_inode_operations = { |
| 399 | .setattr = ovl_setattr, |
| 400 | .permission = ovl_permission, |
| 401 | .getattr = ovl_getattr, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 402 | .listxattr = ovl_listxattr, |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 403 | .get_acl = ovl_get_acl, |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 404 | .update_time = ovl_update_time, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 405 | }; |
| 406 | |
| 407 | static const struct inode_operations ovl_symlink_inode_operations = { |
| 408 | .setattr = ovl_setattr, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 409 | .get_link = ovl_get_link, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 410 | .getattr = ovl_getattr, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 411 | .listxattr = ovl_listxattr, |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 412 | .update_time = ovl_update_time, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 413 | }; |
| 414 | |
Amir Goldstein | b1eaa95 | 2017-01-11 12:07:46 +0200 | [diff] [blame] | 415 | /* |
| 416 | * It is possible to stack overlayfs instance on top of another |
| 417 | * overlayfs instance as lower layer. We need to annonate the |
| 418 | * stackable i_mutex locks according to stack level of the super |
| 419 | * block instance. An overlayfs instance can never be in stack |
| 420 | * depth 0 (there is always a real fs below it). An overlayfs |
| 421 | * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth]. |
| 422 | * |
| 423 | * For example, here is a snip from /proc/lockdep_chains after |
| 424 | * dir_iterate of nested overlayfs: |
| 425 | * |
| 426 | * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2) |
| 427 | * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1) |
| 428 | * [...] &type->i_mutex_dir_key (stack_depth=0) |
| 429 | */ |
| 430 | #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH |
| 431 | |
| 432 | static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode) |
| 433 | { |
| 434 | #ifdef CONFIG_LOCKDEP |
| 435 | static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING]; |
| 436 | static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING]; |
Amir Goldstein | 4eae06d | 2017-10-27 15:44:08 +0300 | [diff] [blame] | 437 | static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING]; |
Amir Goldstein | b1eaa95 | 2017-01-11 12:07:46 +0200 | [diff] [blame] | 438 | |
| 439 | int depth = inode->i_sb->s_stack_depth - 1; |
| 440 | |
| 441 | if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING)) |
| 442 | depth = 0; |
| 443 | |
| 444 | if (S_ISDIR(inode->i_mode)) |
| 445 | lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]); |
| 446 | else |
| 447 | lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]); |
Amir Goldstein | 4eae06d | 2017-10-27 15:44:08 +0300 | [diff] [blame] | 448 | |
| 449 | lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]); |
Amir Goldstein | b1eaa95 | 2017-01-11 12:07:46 +0200 | [diff] [blame] | 450 | #endif |
| 451 | } |
| 452 | |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 453 | static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev, |
| 454 | unsigned long ino) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 455 | { |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 456 | /* |
| 457 | * When NFS export is enabled and d_ino is consistent with st_ino |
| 458 | * (samefs), set the same value to i_ino, because nfsd readdirplus |
| 459 | * compares d_ino values to i_ino values of child entries. When called |
| 460 | * from ovl_new_inode(), ino arg is 0, so i_ino will be updated to real |
| 461 | * upper inode i_ino on ovl_inode_init() or ovl_inode_update(). |
| 462 | */ |
| 463 | if (inode->i_sb->s_export_op && ovl_same_sb(inode->i_sb)) |
| 464 | inode->i_ino = ino; |
| 465 | else |
| 466 | inode->i_ino = get_next_ino(); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 467 | inode->i_mode = mode; |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 468 | inode->i_flags |= S_NOCMTIME; |
Miklos Szeredi | 2a3a2a3 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 469 | #ifdef CONFIG_FS_POSIX_ACL |
| 470 | inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE; |
| 471 | #endif |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 472 | |
Amir Goldstein | b1eaa95 | 2017-01-11 12:07:46 +0200 | [diff] [blame] | 473 | ovl_lockdep_annotate_inode_mutex_key(inode); |
| 474 | |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 475 | switch (mode & S_IFMT) { |
| 476 | case S_IFREG: |
| 477 | inode->i_op = &ovl_file_inode_operations; |
| 478 | break; |
| 479 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 480 | case S_IFDIR: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 481 | inode->i_op = &ovl_dir_inode_operations; |
| 482 | inode->i_fop = &ovl_dir_operations; |
| 483 | break; |
| 484 | |
| 485 | case S_IFLNK: |
| 486 | inode->i_op = &ovl_symlink_inode_operations; |
| 487 | break; |
| 488 | |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 489 | default: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 490 | inode->i_op = &ovl_file_inode_operations; |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 491 | init_special_inode(inode, mode, rdev); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 492 | break; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 493 | } |
| 494 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 495 | |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 496 | /* |
| 497 | * With inodes index enabled, an overlay inode nlink counts the union of upper |
| 498 | * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure |
| 499 | * upper inode, the following nlink modifying operations can happen: |
| 500 | * |
| 501 | * 1. Lower hardlink copy up |
| 502 | * 2. Upper hardlink created, unlinked or renamed over |
| 503 | * 3. Lower hardlink whiteout or renamed over |
| 504 | * |
| 505 | * For the first, copy up case, the union nlink does not change, whether the |
| 506 | * operation succeeds or fails, but the upper inode nlink may change. |
| 507 | * Therefore, before copy up, we store the union nlink value relative to the |
| 508 | * lower inode nlink in the index inode xattr trusted.overlay.nlink. |
| 509 | * |
| 510 | * For the second, upper hardlink case, the union nlink should be incremented |
| 511 | * or decremented IFF the operation succeeds, aligned with nlink change of the |
| 512 | * upper inode. Therefore, before link/unlink/rename, we store the union nlink |
| 513 | * value relative to the upper inode nlink in the index inode. |
| 514 | * |
| 515 | * For the last, lower cover up case, we simplify things by preceding the |
| 516 | * whiteout or cover up with copy up. This makes sure that there is an index |
| 517 | * upper inode where the nlink xattr can be stored before the copied up upper |
| 518 | * entry is unlink. |
| 519 | */ |
| 520 | #define OVL_NLINK_ADD_UPPER (1 << 0) |
| 521 | |
| 522 | /* |
| 523 | * On-disk format for indexed nlink: |
| 524 | * |
| 525 | * nlink relative to the upper inode - "U[+-]NUM" |
| 526 | * nlink relative to the lower inode - "L[+-]NUM" |
| 527 | */ |
| 528 | |
| 529 | static int ovl_set_nlink_common(struct dentry *dentry, |
| 530 | struct dentry *realdentry, const char *format) |
| 531 | { |
| 532 | struct inode *inode = d_inode(dentry); |
| 533 | struct inode *realinode = d_inode(realdentry); |
| 534 | char buf[13]; |
| 535 | int len; |
| 536 | |
| 537 | len = snprintf(buf, sizeof(buf), format, |
| 538 | (int) (inode->i_nlink - realinode->i_nlink)); |
| 539 | |
Miklos Szeredi | 6787341 | 2017-07-27 21:54:05 +0200 | [diff] [blame] | 540 | if (WARN_ON(len >= sizeof(buf))) |
| 541 | return -EIO; |
| 542 | |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 543 | return ovl_do_setxattr(ovl_dentry_upper(dentry), |
| 544 | OVL_XATTR_NLINK, buf, len, 0); |
| 545 | } |
| 546 | |
| 547 | int ovl_set_nlink_upper(struct dentry *dentry) |
| 548 | { |
| 549 | return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i"); |
| 550 | } |
| 551 | |
| 552 | int ovl_set_nlink_lower(struct dentry *dentry) |
| 553 | { |
| 554 | return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i"); |
| 555 | } |
| 556 | |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 557 | unsigned int ovl_get_nlink(struct dentry *lowerdentry, |
| 558 | struct dentry *upperdentry, |
| 559 | unsigned int fallback) |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 560 | { |
| 561 | int nlink_diff; |
| 562 | int nlink; |
| 563 | char buf[13]; |
| 564 | int err; |
| 565 | |
| 566 | if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1) |
| 567 | return fallback; |
| 568 | |
| 569 | err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1); |
| 570 | if (err < 0) |
| 571 | goto fail; |
| 572 | |
| 573 | buf[err] = '\0'; |
| 574 | if ((buf[0] != 'L' && buf[0] != 'U') || |
| 575 | (buf[1] != '+' && buf[1] != '-')) |
| 576 | goto fail; |
| 577 | |
| 578 | err = kstrtoint(buf + 1, 10, &nlink_diff); |
| 579 | if (err < 0) |
| 580 | goto fail; |
| 581 | |
| 582 | nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink; |
| 583 | nlink += nlink_diff; |
| 584 | |
| 585 | if (nlink <= 0) |
| 586 | goto fail; |
| 587 | |
| 588 | return nlink; |
| 589 | |
| 590 | fail: |
| 591 | pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n", |
| 592 | upperdentry, err); |
| 593 | return fallback; |
| 594 | } |
| 595 | |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 596 | 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] | 597 | { |
| 598 | struct inode *inode; |
| 599 | |
| 600 | inode = new_inode(sb); |
| 601 | if (inode) |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 602 | ovl_fill_inode(inode, mode, rdev, 0); |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 603 | |
| 604 | return inode; |
| 605 | } |
| 606 | |
| 607 | static int ovl_inode_test(struct inode *inode, void *data) |
| 608 | { |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 609 | return inode->i_private == data; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | static int ovl_inode_set(struct inode *inode, void *data) |
| 613 | { |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 614 | inode->i_private = data; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 615 | return 0; |
| 616 | } |
| 617 | |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 618 | static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry, |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 619 | struct dentry *upperdentry, bool strict) |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 620 | { |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 621 | /* |
| 622 | * For directories, @strict verify from lookup path performs consistency |
| 623 | * checks, so NULL lower/upper in dentry must match NULL lower/upper in |
| 624 | * inode. Non @strict verify from NFS handle decode path passes NULL for |
| 625 | * 'unknown' lower/upper. |
| 626 | */ |
| 627 | if (S_ISDIR(inode->i_mode) && strict) { |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 628 | /* Real lower dir moved to upper layer under us? */ |
| 629 | if (!lowerdentry && ovl_inode_lower(inode)) |
| 630 | return false; |
| 631 | |
| 632 | /* Lookup of an uncovered redirect origin? */ |
| 633 | if (!upperdentry && ovl_inode_upper(inode)) |
| 634 | return false; |
| 635 | } |
| 636 | |
Amir Goldstein | 939ae4e | 2017-09-11 16:30:15 +0300 | [diff] [blame] | 637 | /* |
| 638 | * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL. |
| 639 | * This happens when finding a copied up overlay inode for a renamed |
| 640 | * or hardlinked overlay dentry and lower dentry cannot be followed |
| 641 | * by origin because lower fs does not support file handles. |
| 642 | */ |
| 643 | if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry)) |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 644 | return false; |
| 645 | |
| 646 | /* |
| 647 | * Allow non-NULL __upperdentry in inode even if upperdentry is NULL. |
| 648 | * This happens when finding a lower alias for a copied up hard link. |
| 649 | */ |
| 650 | if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry)) |
| 651 | return false; |
| 652 | |
| 653 | return true; |
| 654 | } |
| 655 | |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 656 | struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real, |
| 657 | bool is_upper) |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame] | 658 | { |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 659 | struct inode *inode, *key = d_inode(real); |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame] | 660 | |
| 661 | inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key); |
| 662 | if (!inode) |
| 663 | return NULL; |
| 664 | |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 665 | if (!ovl_verify_inode(inode, is_upper ? NULL : real, |
| 666 | is_upper ? real : NULL, false)) { |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame] | 667 | iput(inode); |
| 668 | return ERR_PTR(-ESTALE); |
| 669 | } |
| 670 | |
| 671 | return inode; |
| 672 | } |
| 673 | |
Amir Goldstein | 764baba | 2018-02-04 15:35:09 +0200 | [diff] [blame] | 674 | /* |
| 675 | * Does overlay inode need to be hashed by lower inode? |
| 676 | */ |
| 677 | static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper, |
| 678 | struct dentry *lower, struct dentry *index) |
| 679 | { |
| 680 | struct ovl_fs *ofs = sb->s_fs_info; |
| 681 | |
| 682 | /* No, if pure upper */ |
| 683 | if (!lower) |
| 684 | return false; |
| 685 | |
| 686 | /* Yes, if already indexed */ |
| 687 | if (index) |
| 688 | return true; |
| 689 | |
| 690 | /* Yes, if won't be copied up */ |
| 691 | if (!ofs->upper_mnt) |
| 692 | return true; |
| 693 | |
| 694 | /* No, if lower hardlink is or will be broken on copy up */ |
| 695 | if ((upper || !ovl_indexdir(sb)) && |
| 696 | !d_is_dir(lower) && d_inode(lower)->i_nlink > 1) |
| 697 | return false; |
| 698 | |
| 699 | /* No, if non-indexed upper with NFS export */ |
| 700 | if (sb->s_export_op && upper) |
| 701 | return false; |
| 702 | |
| 703 | /* Otherwise, hash by lower inode for fsnotify */ |
| 704 | return true; |
| 705 | } |
| 706 | |
Amir Goldstein | 0aceb53 | 2017-12-12 23:43:16 +0200 | [diff] [blame] | 707 | struct inode *ovl_get_inode(struct super_block *sb, struct dentry *upperdentry, |
| 708 | struct dentry *lowerdentry, struct dentry *index, |
| 709 | unsigned int numlower) |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 710 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 711 | struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 712 | struct inode *inode; |
Amir Goldstein | 764baba | 2018-02-04 15:35:09 +0200 | [diff] [blame] | 713 | bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, index); |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 714 | bool is_dir; |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 715 | unsigned long ino = 0; |
Amir Goldstein | 6eaf011 | 2017-10-12 19:03:04 +0300 | [diff] [blame] | 716 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 717 | if (!realinode) |
| 718 | realinode = d_inode(lowerdentry); |
| 719 | |
Amir Goldstein | 6eaf011 | 2017-10-12 19:03:04 +0300 | [diff] [blame] | 720 | /* |
Amir Goldstein | 764baba | 2018-02-04 15:35:09 +0200 | [diff] [blame] | 721 | * Copy up origin (lower) may exist for non-indexed upper, but we must |
| 722 | * not use lower as hash key if this is a broken hardlink. |
Amir Goldstein | 6eaf011 | 2017-10-12 19:03:04 +0300 | [diff] [blame] | 723 | */ |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 724 | is_dir = S_ISDIR(realinode->i_mode); |
Amir Goldstein | 764baba | 2018-02-04 15:35:09 +0200 | [diff] [blame] | 725 | if (upperdentry || bylower) { |
| 726 | struct inode *key = d_inode(bylower ? lowerdentry : |
| 727 | upperdentry); |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 728 | unsigned int nlink = is_dir ? 1 : realinode->i_nlink; |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 729 | |
Amir Goldstein | 0aceb53 | 2017-12-12 23:43:16 +0200 | [diff] [blame] | 730 | inode = iget5_locked(sb, (unsigned long) key, |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 731 | ovl_inode_test, ovl_inode_set, key); |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 732 | if (!inode) |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 733 | goto out_nomem; |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 734 | if (!(inode->i_state & I_NEW)) { |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 735 | /* |
| 736 | * Verify that the underlying files stored in the inode |
| 737 | * match those in the dentry. |
| 738 | */ |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 739 | if (!ovl_verify_inode(inode, lowerdentry, upperdentry, |
| 740 | true)) { |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 741 | iput(inode); |
| 742 | inode = ERR_PTR(-ESTALE); |
| 743 | goto out; |
| 744 | } |
| 745 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 746 | dput(upperdentry); |
| 747 | goto out; |
| 748 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 749 | |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 750 | /* Recalculate nlink for non-dir due to indexing */ |
| 751 | if (!is_dir) |
| 752 | nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink); |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 753 | set_nlink(inode, nlink); |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 754 | ino = key->i_ino; |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 755 | } else { |
Amir Goldstein | 764baba | 2018-02-04 15:35:09 +0200 | [diff] [blame] | 756 | /* Lower hardlink that will be broken on copy up */ |
Amir Goldstein | 0aceb53 | 2017-12-12 23:43:16 +0200 | [diff] [blame] | 757 | inode = new_inode(sb); |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 758 | if (!inode) |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 759 | goto out_nomem; |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 760 | } |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 761 | ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino); |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 762 | ovl_inode_init(inode, upperdentry, lowerdentry); |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 763 | |
| 764 | if (upperdentry && ovl_is_impuredir(upperdentry)) |
| 765 | ovl_set_flag(OVL_IMPURE, inode); |
| 766 | |
Vivek Goyal | 0471a9c | 2018-03-20 16:35:40 -0400 | [diff] [blame] | 767 | if (index) |
| 768 | ovl_set_flag(OVL_INDEX, inode); |
| 769 | |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 770 | /* Check for non-merge dir that may have whiteouts */ |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 771 | if (is_dir) { |
Amir Goldstein | 0aceb53 | 2017-12-12 23:43:16 +0200 | [diff] [blame] | 772 | if (((upperdentry && lowerdentry) || numlower > 1) || |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 773 | ovl_check_origin_xattr(upperdentry ?: lowerdentry)) { |
| 774 | ovl_set_flag(OVL_WHITEOUTS, inode); |
| 775 | } |
| 776 | } |
| 777 | |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 778 | if (inode->i_state & I_NEW) |
| 779 | unlock_new_inode(inode); |
| 780 | out: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 781 | return inode; |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 782 | |
| 783 | out_nomem: |
| 784 | inode = ERR_PTR(-ENOMEM); |
| 785 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 786 | } |