blob: 8a283ec21e34dca78026475f4e784d99f3ecacb1 [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
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 Molnar5b825c32017-02-02 17:54:15 +010012#include <linux/cred.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020013#include <linux/xattr.h>
Miklos Szeredi5201dc42016-09-01 11:11:59 +020014#include <linux/posix_acl.h>
Amir Goldstein5f8415d2017-06-20 15:35:14 +030015#include <linux/ratelimit.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020016#include "overlayfs.h"
17
Chandan Rajendraba1e5632017-07-24 01:57:54 -050018
Miklos Szeredie9be9d52014-10-24 00:14:38 +020019int ovl_setattr(struct dentry *dentry, struct iattr *attr)
20{
21 int err;
22 struct dentry *upperdentry;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040023 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020024
Miklos Szeredicf9a6782015-12-11 16:30:49 +010025 /*
26 * Check for permissions before trying to copy-up. This is redundant
27 * since it will be rechecked later by ->setattr() on upper dentry. But
28 * without this, copy-up can be triggered by just about anybody.
29 *
30 * We don't initialize inode->size, which just means that
31 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
32 * check for a swapfile (which this won't be anyway).
33 */
Jan Kara31051c82016-05-26 16:55:18 +020034 err = setattr_prepare(dentry, attr);
Miklos Szeredicf9a6782015-12-11 16:30:49 +010035 if (err)
36 return err;
37
Miklos Szeredie9be9d52014-10-24 00:14:38 +020038 err = ovl_want_write(dentry);
39 if (err)
40 goto out;
41
Miklos Szerediacff81e2015-12-04 19:18:48 +010042 err = ovl_copy_up(dentry);
43 if (!err) {
44 upperdentry = ovl_dentry_upper(dentry);
45
Miklos Szeredib99c2d92016-07-04 16:49:48 +020046 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
47 attr->ia_valid &= ~ATTR_MODE;
48
Al Viro59551022016-01-22 15:40:57 -050049 inode_lock(upperdentry->d_inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040050 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020051 err = notify_change(upperdentry, attr, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040052 revert_creds(old_cred);
Konstantin Khlebnikovb81de062016-01-31 16:21:29 +030053 if (!err)
54 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Al Viro59551022016-01-22 15:40:57 -050055 inode_unlock(upperdentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020056 }
57 ovl_drop_write(dentry);
58out:
59 return err;
60}
61
Amir Goldsteinda309e82017-11-08 19:39:51 +020062static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat,
63 struct ovl_layer *lower_layer)
64{
65 bool samefs = ovl_same_sb(dentry->d_sb);
66
67 if (samefs) {
68 /*
69 * When all layers are on the same fs, all real inode
70 * number are unique, so we use the overlay st_dev,
71 * which is friendly to du -x.
72 */
73 stat->dev = dentry->d_sb->s_dev;
74 } else if (S_ISDIR(dentry->d_inode->i_mode)) {
75 /*
76 * Always use the overlay st_dev for directories, so 'find
77 * -xdev' will scan the entire overlay mount and won't cross the
78 * overlay mount boundaries.
79 *
80 * If not all layers are on the same fs the pair {real st_ino;
81 * overlay st_dev} is not unique, so use the non persistent
82 * overlay st_ino for directories.
83 */
84 stat->dev = dentry->d_sb->s_dev;
85 stat->ino = dentry->d_inode->i_ino;
86 } else if (lower_layer) {
87 /*
88 * For non-samefs setup, if we cannot map all layers st_ino
89 * to a unified address space, we need to make sure that st_dev
90 * is unique per layer. Upper layer uses real st_dev and lower
91 * layers use the unique anonymous bdev.
92 */
93 stat->dev = lower_layer->pseudo_dev;
94 }
95
96 return 0;
97}
98
Miklos Szeredi5b712092017-05-05 11:38:58 +020099int ovl_getattr(const struct path *path, struct kstat *stat,
100 u32 request_mask, unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200101{
David Howellsa528d352017-01-31 16:46:22 +0000102 struct dentry *dentry = path->dentry;
Amir Goldstein72b608f2017-04-24 00:25:49 +0300103 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200104 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400105 const struct cred *old_cred;
Miklos Szeredi5b712092017-05-05 11:38:58 +0200106 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
Amir Goldsteina0c5ad32017-11-01 00:45:40 +0200107 bool samefs = ovl_same_sb(dentry->d_sb);
Amir Goldsteinda309e82017-11-08 19:39:51 +0200108 struct ovl_layer *lower_layer = NULL;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400109 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200110
Amir Goldstein72b608f2017-04-24 00:25:49 +0300111 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400112 old_cred = ovl_override_creds(dentry->d_sb);
David Howellsa528d352017-01-31 16:46:22 +0000113 err = vfs_getattr(&realpath, stat, request_mask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300114 if (err)
115 goto out;
116
117 /*
Amir Goldsteinda309e82017-11-08 19:39:51 +0200118 * For non-dir or same fs, we use st_ino of the copy up origin.
119 * This guaranties constant st_dev/st_ino across copy up.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300120 *
Amir Goldsteinda309e82017-11-08 19:39:51 +0200121 * If lower filesystem supports NFS file handles, this also guaranties
Amir Goldstein72b608f2017-04-24 00:25:49 +0300122 * persistent st_ino across mount cycle.
123 */
Amir Goldsteina0c5ad32017-11-01 00:45:40 +0200124 if (!is_dir || samefs) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200125 if (!OVL_TYPE_UPPER(type)) {
126 lower_layer = ovl_layer_lower(dentry);
127 } else if (OVL_TYPE_ORIGIN(type)) {
Amir Goldstein72b608f2017-04-24 00:25:49 +0300128 struct kstat lowerstat;
Miklos Szeredi5b712092017-05-05 11:38:58 +0200129 u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300130
131 ovl_path_lower(dentry, &realpath);
132 err = vfs_getattr(&realpath, &lowerstat,
Miklos Szeredi5b712092017-05-05 11:38:58 +0200133 lowermask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300134 if (err)
135 goto out;
136
Amir Goldstein72b608f2017-04-24 00:25:49 +0300137 /*
Amir Goldstein359f3922017-06-21 15:28:41 +0300138 * Lower hardlinks may be broken on copy up to different
Amir Goldstein72b608f2017-04-24 00:25:49 +0300139 * upper files, so we cannot use the lower origin st_ino
140 * for those different files, even for the same fs case.
Amir Goldstein86eaa132017-11-21 13:55:51 +0200141 *
142 * Similarly, several redirected dirs can point to the
143 * same dir on a lower layer. With the "verify_lower"
144 * feature, we do not use the lower origin st_ino, if
145 * we haven't verified that this redirect is unique.
146 *
Amir Goldstein359f3922017-06-21 15:28:41 +0300147 * With inodes index enabled, it is safe to use st_ino
Amir Goldstein86eaa132017-11-21 13:55:51 +0200148 * of an indexed origin. The index validates that the
149 * upper hardlink is not broken and that a redirected
150 * dir is the only redirect to that origin.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300151 */
Amir Goldstein86eaa132017-11-21 13:55:51 +0200152 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
153 (!ovl_verify_lower(dentry->d_sb) &&
Amir Goldstein9f99e502018-04-11 20:09:29 +0300154 (is_dir || lowerstat.nlink == 1))) {
Amir Goldstein72b608f2017-04-24 00:25:49 +0300155 stat->ino = lowerstat.ino;
Amir Goldsteinda309e82017-11-08 19:39:51 +0200156 lower_layer = ovl_layer_lower(dentry);
Amir Goldstein9f99e502018-04-11 20:09:29 +0300157 }
Amir Goldstein72b608f2017-04-24 00:25:49 +0300158 }
Amir Goldstein72b608f2017-04-24 00:25:49 +0300159 }
Miklos Szeredi5b712092017-05-05 11:38:58 +0200160
Amir Goldsteinda309e82017-11-08 19:39:51 +0200161 err = ovl_map_dev_ino(dentry, stat, lower_layer);
162 if (err)
163 goto out;
164
Miklos Szeredi5b712092017-05-05 11:38:58 +0200165 /*
166 * It's probably not worth it to count subdirs to get the
167 * correct link count. nlink=1 seems to pacify 'find' and
168 * other utilities.
169 */
170 if (is_dir && OVL_TYPE_MERGE(type))
171 stat->nlink = 1;
172
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300173 /*
174 * Return the overlay inode nlinks for indexed upper inodes.
175 * Overlay inode nlink counts the union of the upper hardlinks
176 * and non-covered lower hardlinks. It does not include the upper
177 * index hardlink.
178 */
179 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
180 stat->nlink = dentry->d_inode->i_nlink;
181
Amir Goldstein72b608f2017-04-24 00:25:49 +0300182out:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400183 revert_creds(old_cred);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300184
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400185 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200186}
187
188int ovl_permission(struct inode *inode, int mask)
189{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200190 struct inode *upperinode = ovl_inode_upper(inode);
191 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400192 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200193 int err;
194
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200195 /* Careful in RCU walk mode */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200196 if (!realinode) {
197 WARN_ON(!(mask & MAY_NOT_BLOCK));
Miklos Szeredia999d7e2016-07-29 12:05:23 +0200198 return -ECHILD;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200199 }
200
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400201 /*
202 * Check overlay inode with the creds of task and underlying inode
203 * with creds of mounter
204 */
205 err = generic_permission(inode, mask);
206 if (err)
207 return err;
208
209 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200210 if (!upperinode &&
211 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
Vivek Goyal754f8cb2016-07-01 16:34:29 -0400212 mask &= ~(MAY_WRITE | MAY_APPEND);
Vivek Goyal500cac32016-07-13 11:00:14 -0400213 /* Make sure mounter can read file for copy up later */
214 mask |= MAY_READ;
215 }
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200216 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400217 revert_creds(old_cred);
218
219 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200220}
221
Al Viro6b255392015-11-17 10:20:54 -0500222static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500223 struct inode *inode,
224 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200225{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400226 const struct cred *old_cred;
227 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200228
Al Viro6b255392015-11-17 10:20:54 -0500229 if (!dentry)
230 return ERR_PTR(-ECHILD);
231
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400232 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200233 p = vfs_get_link(ovl_dentry_real(dentry), done);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400234 revert_creds(old_cred);
235 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200236}
237
Miklos Szeredi09562542016-08-08 15:08:49 +0200238bool ovl_is_private_xattr(const char *name)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200239{
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +0200240 return strncmp(name, OVL_XATTR_PREFIX,
241 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200242}
243
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200244int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
245 const void *value, size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200246{
247 int err;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200248 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
249 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400250 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200251
252 err = ovl_want_write(dentry);
253 if (err)
254 goto out;
255
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200256 if (!value && !upperdentry) {
257 err = vfs_getxattr(realdentry, name, NULL, 0);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200258 if (err < 0)
259 goto out_drop_write;
260 }
261
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200262 if (!upperdentry) {
263 err = ovl_copy_up(dentry);
264 if (err)
265 goto out_drop_write;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200266
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200267 realdentry = ovl_dentry_upper(dentry);
268 }
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200269
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400270 old_cred = ovl_override_creds(dentry->d_sb);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200271 if (value)
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200272 err = vfs_setxattr(realdentry, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200273 else {
274 WARN_ON(flags != XATTR_REPLACE);
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200275 err = vfs_removexattr(realdentry, name);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200276 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400277 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200278
279out_drop_write:
280 ovl_drop_write(dentry);
281out:
282 return err;
283}
284
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200285int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200286 void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200287{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400288 ssize_t res;
289 const struct cred *old_cred;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200290 struct dentry *realdentry =
291 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
Miklos Szeredi52148462014-11-20 16:40:00 +0100292
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400293 old_cred = ovl_override_creds(dentry->d_sb);
294 res = vfs_getxattr(realdentry, name, value, size);
295 revert_creds(old_cred);
296 return res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200297}
298
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200299static bool ovl_can_list(const char *s)
300{
301 /* List all non-trusted xatts */
302 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
303 return true;
304
305 /* Never list trusted.overlay, list other trusted for superuser only */
306 return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
307}
308
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200309ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
310{
Miklos Szeredib5817552016-06-06 16:21:37 +0200311 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200312 ssize_t res;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200313 size_t len;
314 char *s;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400315 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200316
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400317 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200318 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400319 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200320 if (res <= 0 || size == 0)
321 return res;
322
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200323 /* filter out private xattrs */
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200324 for (s = list, len = res; len;) {
325 size_t slen = strnlen(s, len) + 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200326
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200327 /* underlying fs providing us with an broken xattr list? */
328 if (WARN_ON(slen > len))
329 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200330
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200331 len -= slen;
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200332 if (!ovl_can_list(s)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200333 res -= slen;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200334 memmove(s, s + slen, len);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200335 } else {
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200336 s += slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200337 }
338 }
339
340 return res;
341}
342
Vivek Goyal39a25b22016-07-01 16:34:26 -0400343struct posix_acl *ovl_get_acl(struct inode *inode, int type)
344{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200345 struct inode *realinode = ovl_inode_real(inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400346 const struct cred *old_cred;
347 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400348
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200349 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
Vivek Goyal39a25b22016-07-01 16:34:26 -0400350 return NULL;
351
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400352 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200353 acl = get_acl(realinode, type);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400354 revert_creds(old_cred);
355
356 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400357}
358
Amir Goldstein59be0972017-06-20 15:25:46 +0300359static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200360{
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300361 /* Copy up of disconnected dentry does not set upper alias */
Amir Goldstein59be0972017-06-20 15:25:46 +0300362 if (ovl_dentry_upper(dentry) &&
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300363 (ovl_dentry_has_upper_alias(dentry) ||
364 (dentry->d_flags & DCACHE_DISCONNECTED)))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200365 return false;
366
Amir Goldstein59be0972017-06-20 15:25:46 +0300367 if (special_file(d_inode(dentry)->i_mode))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200368 return false;
369
370 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
371 return false;
372
373 return true;
374}
375
Miklos Szeredi2d902672016-06-30 08:53:27 +0200376int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200377{
Miklos Szeredi2d902672016-06-30 08:53:27 +0200378 int err = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200379
Amir Goldstein59be0972017-06-20 15:25:46 +0300380 if (ovl_open_need_copy_up(dentry, file_flags)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200381 err = ovl_want_write(dentry);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200382 if (!err) {
Amir Goldstein9aba6522016-11-12 21:36:03 +0200383 err = ovl_copy_up_flags(dentry, file_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200384 ovl_drop_write(dentry);
385 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200386 }
387
Miklos Szeredi2d902672016-06-30 08:53:27 +0200388 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200389}
390
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200391int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
392{
Miklos Szeredi8f35cf52018-04-12 12:04:50 +0200393 if (flags & S_ATIME) {
394 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
395 struct path upperpath = {
396 .mnt = ofs->upper_mnt,
397 .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
398 };
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200399
Miklos Szeredi8f35cf52018-04-12 12:04:50 +0200400 if (upperpath.dentry) {
401 touch_atime(&upperpath);
402 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
403 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200404 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200405 return 0;
406}
407
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200408static const struct inode_operations ovl_file_inode_operations = {
409 .setattr = ovl_setattr,
410 .permission = ovl_permission,
411 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200412 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400413 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200414 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200415};
416
417static const struct inode_operations ovl_symlink_inode_operations = {
418 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500419 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200420 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200421 .listxattr = ovl_listxattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200422 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200423};
424
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200425/*
426 * It is possible to stack overlayfs instance on top of another
427 * overlayfs instance as lower layer. We need to annonate the
428 * stackable i_mutex locks according to stack level of the super
429 * block instance. An overlayfs instance can never be in stack
430 * depth 0 (there is always a real fs below it). An overlayfs
431 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
432 *
433 * For example, here is a snip from /proc/lockdep_chains after
434 * dir_iterate of nested overlayfs:
435 *
436 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
437 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
438 * [...] &type->i_mutex_dir_key (stack_depth=0)
439 */
440#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
441
442static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
443{
444#ifdef CONFIG_LOCKDEP
445 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
446 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300447 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200448
449 int depth = inode->i_sb->s_stack_depth - 1;
450
451 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
452 depth = 0;
453
454 if (S_ISDIR(inode->i_mode))
455 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
456 else
457 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300458
459 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200460#endif
461}
462
Amir Goldstein695b46e2018-03-15 23:39:01 +0200463static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
464 unsigned long ino)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200465{
Amir Goldstein695b46e2018-03-15 23:39:01 +0200466 /*
467 * When NFS export is enabled and d_ino is consistent with st_ino
468 * (samefs), set the same value to i_ino, because nfsd readdirplus
469 * compares d_ino values to i_ino values of child entries. When called
470 * from ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
471 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
472 */
473 if (inode->i_sb->s_export_op && ovl_same_sb(inode->i_sb))
474 inode->i_ino = ino;
475 else
476 inode->i_ino = get_next_ino();
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200477 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200478 inode->i_flags |= S_NOCMTIME;
Miklos Szeredi2a3a2a32016-09-01 11:11:59 +0200479#ifdef CONFIG_FS_POSIX_ACL
480 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
481#endif
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200482
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200483 ovl_lockdep_annotate_inode_mutex_key(inode);
484
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100485 switch (mode & S_IFMT) {
486 case S_IFREG:
487 inode->i_op = &ovl_file_inode_operations;
488 break;
489
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200490 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200491 inode->i_op = &ovl_dir_inode_operations;
492 inode->i_fop = &ovl_dir_operations;
493 break;
494
495 case S_IFLNK:
496 inode->i_op = &ovl_symlink_inode_operations;
497 break;
498
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200499 default:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200500 inode->i_op = &ovl_file_inode_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100501 init_special_inode(inode, mode, rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200502 break;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200503 }
504}
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200505
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300506/*
507 * With inodes index enabled, an overlay inode nlink counts the union of upper
508 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
509 * upper inode, the following nlink modifying operations can happen:
510 *
511 * 1. Lower hardlink copy up
512 * 2. Upper hardlink created, unlinked or renamed over
513 * 3. Lower hardlink whiteout or renamed over
514 *
515 * For the first, copy up case, the union nlink does not change, whether the
516 * operation succeeds or fails, but the upper inode nlink may change.
517 * Therefore, before copy up, we store the union nlink value relative to the
518 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
519 *
520 * For the second, upper hardlink case, the union nlink should be incremented
521 * or decremented IFF the operation succeeds, aligned with nlink change of the
522 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
523 * value relative to the upper inode nlink in the index inode.
524 *
525 * For the last, lower cover up case, we simplify things by preceding the
526 * whiteout or cover up with copy up. This makes sure that there is an index
527 * upper inode where the nlink xattr can be stored before the copied up upper
528 * entry is unlink.
529 */
530#define OVL_NLINK_ADD_UPPER (1 << 0)
531
532/*
533 * On-disk format for indexed nlink:
534 *
535 * nlink relative to the upper inode - "U[+-]NUM"
536 * nlink relative to the lower inode - "L[+-]NUM"
537 */
538
539static int ovl_set_nlink_common(struct dentry *dentry,
540 struct dentry *realdentry, const char *format)
541{
542 struct inode *inode = d_inode(dentry);
543 struct inode *realinode = d_inode(realdentry);
544 char buf[13];
545 int len;
546
547 len = snprintf(buf, sizeof(buf), format,
548 (int) (inode->i_nlink - realinode->i_nlink));
549
Miklos Szeredi67873412017-07-27 21:54:05 +0200550 if (WARN_ON(len >= sizeof(buf)))
551 return -EIO;
552
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300553 return ovl_do_setxattr(ovl_dentry_upper(dentry),
554 OVL_XATTR_NLINK, buf, len, 0);
555}
556
557int ovl_set_nlink_upper(struct dentry *dentry)
558{
559 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
560}
561
562int ovl_set_nlink_lower(struct dentry *dentry)
563{
564 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
565}
566
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300567unsigned int ovl_get_nlink(struct dentry *lowerdentry,
568 struct dentry *upperdentry,
569 unsigned int fallback)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300570{
571 int nlink_diff;
572 int nlink;
573 char buf[13];
574 int err;
575
576 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
577 return fallback;
578
579 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
580 if (err < 0)
581 goto fail;
582
583 buf[err] = '\0';
584 if ((buf[0] != 'L' && buf[0] != 'U') ||
585 (buf[1] != '+' && buf[1] != '-'))
586 goto fail;
587
588 err = kstrtoint(buf + 1, 10, &nlink_diff);
589 if (err < 0)
590 goto fail;
591
592 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
593 nlink += nlink_diff;
594
595 if (nlink <= 0)
596 goto fail;
597
598 return nlink;
599
600fail:
601 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
602 upperdentry, err);
603 return fallback;
604}
605
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100606struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200607{
608 struct inode *inode;
609
610 inode = new_inode(sb);
611 if (inode)
Amir Goldstein695b46e2018-03-15 23:39:01 +0200612 ovl_fill_inode(inode, mode, rdev, 0);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200613
614 return inode;
615}
616
617static int ovl_inode_test(struct inode *inode, void *data)
618{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200619 return inode->i_private == data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200620}
621
622static int ovl_inode_set(struct inode *inode, void *data)
623{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200624 inode->i_private = data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200625 return 0;
626}
627
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200628static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
Amir Goldstein4b91c302018-01-18 16:39:13 +0200629 struct dentry *upperdentry, bool strict)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200630{
Amir Goldstein4b91c302018-01-18 16:39:13 +0200631 /*
632 * For directories, @strict verify from lookup path performs consistency
633 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
634 * inode. Non @strict verify from NFS handle decode path passes NULL for
635 * 'unknown' lower/upper.
636 */
637 if (S_ISDIR(inode->i_mode) && strict) {
Amir Goldstein31747ed2018-01-14 18:35:40 +0200638 /* Real lower dir moved to upper layer under us? */
639 if (!lowerdentry && ovl_inode_lower(inode))
640 return false;
641
642 /* Lookup of an uncovered redirect origin? */
643 if (!upperdentry && ovl_inode_upper(inode))
644 return false;
645 }
646
Amir Goldstein939ae4e2017-09-11 16:30:15 +0300647 /*
648 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
649 * This happens when finding a copied up overlay inode for a renamed
650 * or hardlinked overlay dentry and lower dentry cannot be followed
651 * by origin because lower fs does not support file handles.
652 */
653 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200654 return false;
655
656 /*
657 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
658 * This happens when finding a lower alias for a copied up hard link.
659 */
660 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
661 return false;
662
663 return true;
664}
665
Amir Goldstein4b91c302018-01-18 16:39:13 +0200666struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
667 bool is_upper)
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200668{
Amir Goldstein4b91c302018-01-18 16:39:13 +0200669 struct inode *inode, *key = d_inode(real);
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200670
671 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
672 if (!inode)
673 return NULL;
674
Amir Goldstein4b91c302018-01-18 16:39:13 +0200675 if (!ovl_verify_inode(inode, is_upper ? NULL : real,
676 is_upper ? real : NULL, false)) {
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200677 iput(inode);
678 return ERR_PTR(-ESTALE);
679 }
680
681 return inode;
682}
683
Amir Goldstein764baba2018-02-04 15:35:09 +0200684/*
685 * Does overlay inode need to be hashed by lower inode?
686 */
687static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
688 struct dentry *lower, struct dentry *index)
689{
690 struct ovl_fs *ofs = sb->s_fs_info;
691
692 /* No, if pure upper */
693 if (!lower)
694 return false;
695
696 /* Yes, if already indexed */
697 if (index)
698 return true;
699
700 /* Yes, if won't be copied up */
701 if (!ofs->upper_mnt)
702 return true;
703
704 /* No, if lower hardlink is or will be broken on copy up */
705 if ((upper || !ovl_indexdir(sb)) &&
706 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
707 return false;
708
709 /* No, if non-indexed upper with NFS export */
710 if (sb->s_export_op && upper)
711 return false;
712
713 /* Otherwise, hash by lower inode for fsnotify */
714 return true;
715}
716
Amir Goldstein0aceb532017-12-12 23:43:16 +0200717struct inode *ovl_get_inode(struct super_block *sb, struct dentry *upperdentry,
718 struct dentry *lowerdentry, struct dentry *index,
719 unsigned int numlower)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200720{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200721 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200722 struct inode *inode;
Amir Goldstein764baba2018-02-04 15:35:09 +0200723 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, index);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200724 bool is_dir;
Amir Goldstein695b46e2018-03-15 23:39:01 +0200725 unsigned long ino = 0;
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300726
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200727 if (!realinode)
728 realinode = d_inode(lowerdentry);
729
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300730 /*
Amir Goldstein764baba2018-02-04 15:35:09 +0200731 * Copy up origin (lower) may exist for non-indexed upper, but we must
732 * not use lower as hash key if this is a broken hardlink.
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300733 */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200734 is_dir = S_ISDIR(realinode->i_mode);
Amir Goldstein764baba2018-02-04 15:35:09 +0200735 if (upperdentry || bylower) {
736 struct inode *key = d_inode(bylower ? lowerdentry :
737 upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200738 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200739
Amir Goldstein0aceb532017-12-12 23:43:16 +0200740 inode = iget5_locked(sb, (unsigned long) key,
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200741 ovl_inode_test, ovl_inode_set, key);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200742 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200743 goto out_nomem;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200744 if (!(inode->i_state & I_NEW)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200745 /*
746 * Verify that the underlying files stored in the inode
747 * match those in the dentry.
748 */
Amir Goldstein4b91c302018-01-18 16:39:13 +0200749 if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
750 true)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200751 iput(inode);
752 inode = ERR_PTR(-ESTALE);
753 goto out;
754 }
755
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200756 dput(upperdentry);
757 goto out;
758 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200759
Amir Goldstein31747ed2018-01-14 18:35:40 +0200760 /* Recalculate nlink for non-dir due to indexing */
761 if (!is_dir)
762 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300763 set_nlink(inode, nlink);
Amir Goldstein695b46e2018-03-15 23:39:01 +0200764 ino = key->i_ino;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200765 } else {
Amir Goldstein764baba2018-02-04 15:35:09 +0200766 /* Lower hardlink that will be broken on copy up */
Amir Goldstein0aceb532017-12-12 23:43:16 +0200767 inode = new_inode(sb);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200768 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200769 goto out_nomem;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200770 }
Amir Goldstein695b46e2018-03-15 23:39:01 +0200771 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200772 ovl_inode_init(inode, upperdentry, lowerdentry);
Miklos Szeredi13c72072017-07-04 22:03:16 +0200773
774 if (upperdentry && ovl_is_impuredir(upperdentry))
775 ovl_set_flag(OVL_IMPURE, inode);
776
Vivek Goyal0471a9c2018-03-20 16:35:40 -0400777 if (index)
778 ovl_set_flag(OVL_INDEX, inode);
779
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300780 /* Check for non-merge dir that may have whiteouts */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200781 if (is_dir) {
Amir Goldstein0aceb532017-12-12 23:43:16 +0200782 if (((upperdentry && lowerdentry) || numlower > 1) ||
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300783 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
784 ovl_set_flag(OVL_WHITEOUTS, inode);
785 }
786 }
787
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200788 if (inode->i_state & I_NEW)
789 unlock_new_inode(inode);
790out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200791 return inode;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200792
793out_nomem:
794 inode = ERR_PTR(-ENOMEM);
795 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200796}