blob: b3be0cfd4c92d93c61a42f101eeae6d05fc76e62 [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
19static 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 Szeredie9be9d52014-10-24 00:14:38 +020026int ovl_setattr(struct dentry *dentry, struct iattr *attr)
27{
28 int err;
29 struct dentry *upperdentry;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040030 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020031
Miklos Szeredicf9a6782015-12-11 16:30:49 +010032 /*
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 Kara31051c82016-05-26 16:55:18 +020041 err = setattr_prepare(dentry, attr);
Miklos Szeredicf9a6782015-12-11 16:30:49 +010042 if (err)
43 return err;
44
Miklos Szeredie9be9d52014-10-24 00:14:38 +020045 err = ovl_want_write(dentry);
46 if (err)
47 goto out;
48
Miklos Szerediacff81e2015-12-04 19:18:48 +010049 err = ovl_copy_up(dentry);
50 if (!err) {
51 upperdentry = ovl_dentry_upper(dentry);
52
Miklos Szeredib99c2d92016-07-04 16:49:48 +020053 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
54 attr->ia_valid &= ~ATTR_MODE;
55
Al Viro59551022016-01-22 15:40:57 -050056 inode_lock(upperdentry->d_inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040057 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020058 err = notify_change(upperdentry, attr, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040059 revert_creds(old_cred);
Konstantin Khlebnikovb81de062016-01-31 16:21:29 +030060 if (!err)
61 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Al Viro59551022016-01-22 15:40:57 -050062 inode_unlock(upperdentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020063 }
64 ovl_drop_write(dentry);
65out:
66 return err;
67}
68
Miklos Szeredi5b712092017-05-05 11:38:58 +020069int ovl_getattr(const struct path *path, struct kstat *stat,
70 u32 request_mask, unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020071{
David Howellsa528d352017-01-31 16:46:22 +000072 struct dentry *dentry = path->dentry;
Amir Goldstein72b608f2017-04-24 00:25:49 +030073 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020074 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040075 const struct cred *old_cred;
Miklos Szeredi5b712092017-05-05 11:38:58 +020076 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
Amir Goldsteina0c5ad32017-11-01 00:45:40 +020077 bool samefs = ovl_same_sb(dentry->d_sb);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040078 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020079
Amir Goldstein72b608f2017-04-24 00:25:49 +030080 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040081 old_cred = ovl_override_creds(dentry->d_sb);
David Howellsa528d352017-01-31 16:46:22 +000082 err = vfs_getattr(&realpath, stat, request_mask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +030083 if (err)
84 goto out;
85
86 /*
Amir Goldsteina0c5ad32017-11-01 00:45:40 +020087 * 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 Goldstein72b608f2017-04-24 00:25:49 +030089 *
90 * If filesystem supports NFS export ops, this also guaranties
91 * persistent st_ino across mount cycle.
92 */
Amir Goldsteina0c5ad32017-11-01 00:45:40 +020093 if (!is_dir || samefs) {
Amir Goldstein72b608f2017-04-24 00:25:49 +030094 if (OVL_TYPE_ORIGIN(type)) {
95 struct kstat lowerstat;
Miklos Szeredi5b712092017-05-05 11:38:58 +020096 u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
Amir Goldstein72b608f2017-04-24 00:25:49 +030097
98 ovl_path_lower(dentry, &realpath);
99 err = vfs_getattr(&realpath, &lowerstat,
Miklos Szeredi5b712092017-05-05 11:38:58 +0200100 lowermask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300101 if (err)
102 goto out;
103
Amir Goldstein72b608f2017-04-24 00:25:49 +0300104 /*
Amir Goldstein359f3922017-06-21 15:28:41 +0300105 * Lower hardlinks may be broken on copy up to different
Amir Goldstein72b608f2017-04-24 00:25:49 +0300106 * upper files, so we cannot use the lower origin st_ino
107 * for those different files, even for the same fs case.
Amir Goldstein86eaa132017-11-21 13:55:51 +0200108 *
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 Goldstein359f3922017-06-21 15:28:41 +0300114 * With inodes index enabled, it is safe to use st_ino
Amir Goldstein86eaa132017-11-21 13:55:51 +0200115 * 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 Goldstein72b608f2017-04-24 00:25:49 +0300118 */
Amir Goldstein86eaa132017-11-21 13:55:51 +0200119 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
120 (!ovl_verify_lower(dentry->d_sb) &&
Amir Goldstein9f99e502018-04-11 20:09:29 +0300121 (is_dir || lowerstat.nlink == 1))) {
Amir Goldstein72b608f2017-04-24 00:25:49 +0300122 stat->ino = lowerstat.ino;
Amir Goldsteina0c5ad32017-11-01 00:45:40 +0200123 stat->dev = ovl_get_pseudo_dev(dentry);
Amir Goldstein9f99e502018-04-11 20:09:29 +0300124 }
Amir Goldstein72b608f2017-04-24 00:25:49 +0300125 }
Amir Goldsteina0c5ad32017-11-01 00:45:40 +0200126 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 Szeredi5b712092017-05-05 11:38:58 +0200142 /*
Miklos Szeredi5b712092017-05-05 11:38:58 +0200143 * 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 Goldsteina0c5ad32017-11-01 00:45:40 +0200146 *
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 Szeredi5b712092017-05-05 11:38:58 +0200150 */
151 stat->dev = dentry->d_sb->s_dev;
152 stat->ino = dentry->d_inode->i_ino;
Amir Goldstein72b608f2017-04-24 00:25:49 +0300153 }
Miklos Szeredi5b712092017-05-05 11:38:58 +0200154
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 Goldstein5f8415d2017-06-20 15:35:14 +0300163 /*
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 Goldstein72b608f2017-04-24 00:25:49 +0300172out:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400173 revert_creds(old_cred);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300174
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400175 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200176}
177
178int ovl_permission(struct inode *inode, int mask)
179{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200180 struct inode *upperinode = ovl_inode_upper(inode);
181 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400182 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200183 int err;
184
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200185 /* Careful in RCU walk mode */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200186 if (!realinode) {
187 WARN_ON(!(mask & MAY_NOT_BLOCK));
Miklos Szeredia999d7e2016-07-29 12:05:23 +0200188 return -ECHILD;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200189 }
190
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400191 /*
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 Szeredi09d8b582017-07-04 22:03:16 +0200200 if (!upperinode &&
201 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
Vivek Goyal754f8cb2016-07-01 16:34:29 -0400202 mask &= ~(MAY_WRITE | MAY_APPEND);
Vivek Goyal500cac32016-07-13 11:00:14 -0400203 /* Make sure mounter can read file for copy up later */
204 mask |= MAY_READ;
205 }
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200206 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400207 revert_creds(old_cred);
208
209 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200210}
211
Al Viro6b255392015-11-17 10:20:54 -0500212static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500213 struct inode *inode,
214 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200215{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400216 const struct cred *old_cred;
217 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200218
Al Viro6b255392015-11-17 10:20:54 -0500219 if (!dentry)
220 return ERR_PTR(-ECHILD);
221
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400222 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200223 p = vfs_get_link(ovl_dentry_real(dentry), done);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400224 revert_creds(old_cred);
225 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200226}
227
Miklos Szeredi09562542016-08-08 15:08:49 +0200228bool ovl_is_private_xattr(const char *name)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200229{
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +0200230 return strncmp(name, OVL_XATTR_PREFIX,
231 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200232}
233
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200234int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
235 const void *value, size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200236{
237 int err;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200238 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
239 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400240 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200241
242 err = ovl_want_write(dentry);
243 if (err)
244 goto out;
245
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200246 if (!value && !upperdentry) {
247 err = vfs_getxattr(realdentry, name, NULL, 0);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200248 if (err < 0)
249 goto out_drop_write;
250 }
251
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200252 if (!upperdentry) {
253 err = ovl_copy_up(dentry);
254 if (err)
255 goto out_drop_write;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200256
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200257 realdentry = ovl_dentry_upper(dentry);
258 }
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200259
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400260 old_cred = ovl_override_creds(dentry->d_sb);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200261 if (value)
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200262 err = vfs_setxattr(realdentry, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200263 else {
264 WARN_ON(flags != XATTR_REPLACE);
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200265 err = vfs_removexattr(realdentry, name);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200266 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400267 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200268
269out_drop_write:
270 ovl_drop_write(dentry);
271out:
272 return err;
273}
274
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200275int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200276 void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200277{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400278 ssize_t res;
279 const struct cred *old_cred;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200280 struct dentry *realdentry =
281 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
Miklos Szeredi52148462014-11-20 16:40:00 +0100282
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400283 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 Szeredie9be9d52014-10-24 00:14:38 +0200287}
288
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200289static 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 Szeredie9be9d52014-10-24 00:14:38 +0200299ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
300{
Miklos Szeredib5817552016-06-06 16:21:37 +0200301 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200302 ssize_t res;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200303 size_t len;
304 char *s;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400305 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200306
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400307 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200308 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400309 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200310 if (res <= 0 || size == 0)
311 return res;
312
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200313 /* filter out private xattrs */
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200314 for (s = list, len = res; len;) {
315 size_t slen = strnlen(s, len) + 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200316
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200317 /* underlying fs providing us with an broken xattr list? */
318 if (WARN_ON(slen > len))
319 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200320
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200321 len -= slen;
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200322 if (!ovl_can_list(s)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200323 res -= slen;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200324 memmove(s, s + slen, len);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200325 } else {
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200326 s += slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200327 }
328 }
329
330 return res;
331}
332
Vivek Goyal39a25b22016-07-01 16:34:26 -0400333struct posix_acl *ovl_get_acl(struct inode *inode, int type)
334{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200335 struct inode *realinode = ovl_inode_real(inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400336 const struct cred *old_cred;
337 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400338
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200339 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
Vivek Goyal39a25b22016-07-01 16:34:26 -0400340 return NULL;
341
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400342 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200343 acl = get_acl(realinode, type);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400344 revert_creds(old_cred);
345
346 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400347}
348
Amir Goldstein59be0972017-06-20 15:25:46 +0300349static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200350{
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300351 /* Copy up of disconnected dentry does not set upper alias */
Amir Goldstein59be0972017-06-20 15:25:46 +0300352 if (ovl_dentry_upper(dentry) &&
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300353 (ovl_dentry_has_upper_alias(dentry) ||
354 (dentry->d_flags & DCACHE_DISCONNECTED)))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200355 return false;
356
Amir Goldstein59be0972017-06-20 15:25:46 +0300357 if (special_file(d_inode(dentry)->i_mode))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200358 return false;
359
360 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
361 return false;
362
363 return true;
364}
365
Miklos Szeredi2d902672016-06-30 08:53:27 +0200366int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200367{
Miklos Szeredi2d902672016-06-30 08:53:27 +0200368 int err = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200369
Amir Goldstein59be0972017-06-20 15:25:46 +0300370 if (ovl_open_need_copy_up(dentry, file_flags)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200371 err = ovl_want_write(dentry);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200372 if (!err) {
Amir Goldstein9aba6522016-11-12 21:36:03 +0200373 err = ovl_copy_up_flags(dentry, file_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200374 ovl_drop_write(dentry);
375 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200376 }
377
Miklos Szeredi2d902672016-06-30 08:53:27 +0200378 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200379}
380
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200381int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
382{
383 struct dentry *alias;
384 struct path upperpath;
385
386 if (!(flags & S_ATIME))
387 return 0;
388
389 alias = d_find_any_alias(inode);
390 if (!alias)
391 return 0;
392
393 ovl_path_upper(alias, &upperpath);
394 if (upperpath.dentry) {
395 touch_atime(&upperpath);
396 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
397 }
398
399 dput(alias);
400
401 return 0;
402}
403
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200404static const struct inode_operations ovl_file_inode_operations = {
405 .setattr = ovl_setattr,
406 .permission = ovl_permission,
407 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200408 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400409 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200410 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200411};
412
413static const struct inode_operations ovl_symlink_inode_operations = {
414 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500415 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200416 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200417 .listxattr = ovl_listxattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200418 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200419};
420
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200421/*
422 * It is possible to stack overlayfs instance on top of another
423 * overlayfs instance as lower layer. We need to annonate the
424 * stackable i_mutex locks according to stack level of the super
425 * block instance. An overlayfs instance can never be in stack
426 * depth 0 (there is always a real fs below it). An overlayfs
427 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
428 *
429 * For example, here is a snip from /proc/lockdep_chains after
430 * dir_iterate of nested overlayfs:
431 *
432 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
433 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
434 * [...] &type->i_mutex_dir_key (stack_depth=0)
435 */
436#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
437
438static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
439{
440#ifdef CONFIG_LOCKDEP
441 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
442 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300443 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200444
445 int depth = inode->i_sb->s_stack_depth - 1;
446
447 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
448 depth = 0;
449
450 if (S_ISDIR(inode->i_mode))
451 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
452 else
453 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300454
455 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200456#endif
457}
458
Amir Goldstein695b46e2018-03-15 23:39:01 +0200459static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
460 unsigned long ino)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200461{
Amir Goldstein695b46e2018-03-15 23:39:01 +0200462 /*
463 * When NFS export is enabled and d_ino is consistent with st_ino
464 * (samefs), set the same value to i_ino, because nfsd readdirplus
465 * compares d_ino values to i_ino values of child entries. When called
466 * from ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
467 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
468 */
469 if (inode->i_sb->s_export_op && ovl_same_sb(inode->i_sb))
470 inode->i_ino = ino;
471 else
472 inode->i_ino = get_next_ino();
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200473 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200474 inode->i_flags |= S_NOCMTIME;
Miklos Szeredi2a3a2a32016-09-01 11:11:59 +0200475#ifdef CONFIG_FS_POSIX_ACL
476 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
477#endif
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200478
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200479 ovl_lockdep_annotate_inode_mutex_key(inode);
480
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100481 switch (mode & S_IFMT) {
482 case S_IFREG:
483 inode->i_op = &ovl_file_inode_operations;
484 break;
485
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200486 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200487 inode->i_op = &ovl_dir_inode_operations;
488 inode->i_fop = &ovl_dir_operations;
489 break;
490
491 case S_IFLNK:
492 inode->i_op = &ovl_symlink_inode_operations;
493 break;
494
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200495 default:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200496 inode->i_op = &ovl_file_inode_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100497 init_special_inode(inode, mode, rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200498 break;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200499 }
500}
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200501
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300502/*
503 * With inodes index enabled, an overlay inode nlink counts the union of upper
504 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
505 * upper inode, the following nlink modifying operations can happen:
506 *
507 * 1. Lower hardlink copy up
508 * 2. Upper hardlink created, unlinked or renamed over
509 * 3. Lower hardlink whiteout or renamed over
510 *
511 * For the first, copy up case, the union nlink does not change, whether the
512 * operation succeeds or fails, but the upper inode nlink may change.
513 * Therefore, before copy up, we store the union nlink value relative to the
514 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
515 *
516 * For the second, upper hardlink case, the union nlink should be incremented
517 * or decremented IFF the operation succeeds, aligned with nlink change of the
518 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
519 * value relative to the upper inode nlink in the index inode.
520 *
521 * For the last, lower cover up case, we simplify things by preceding the
522 * whiteout or cover up with copy up. This makes sure that there is an index
523 * upper inode where the nlink xattr can be stored before the copied up upper
524 * entry is unlink.
525 */
526#define OVL_NLINK_ADD_UPPER (1 << 0)
527
528/*
529 * On-disk format for indexed nlink:
530 *
531 * nlink relative to the upper inode - "U[+-]NUM"
532 * nlink relative to the lower inode - "L[+-]NUM"
533 */
534
535static int ovl_set_nlink_common(struct dentry *dentry,
536 struct dentry *realdentry, const char *format)
537{
538 struct inode *inode = d_inode(dentry);
539 struct inode *realinode = d_inode(realdentry);
540 char buf[13];
541 int len;
542
543 len = snprintf(buf, sizeof(buf), format,
544 (int) (inode->i_nlink - realinode->i_nlink));
545
Miklos Szeredi67873412017-07-27 21:54:05 +0200546 if (WARN_ON(len >= sizeof(buf)))
547 return -EIO;
548
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300549 return ovl_do_setxattr(ovl_dentry_upper(dentry),
550 OVL_XATTR_NLINK, buf, len, 0);
551}
552
553int ovl_set_nlink_upper(struct dentry *dentry)
554{
555 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
556}
557
558int ovl_set_nlink_lower(struct dentry *dentry)
559{
560 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
561}
562
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300563unsigned int ovl_get_nlink(struct dentry *lowerdentry,
564 struct dentry *upperdentry,
565 unsigned int fallback)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300566{
567 int nlink_diff;
568 int nlink;
569 char buf[13];
570 int err;
571
572 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
573 return fallback;
574
575 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
576 if (err < 0)
577 goto fail;
578
579 buf[err] = '\0';
580 if ((buf[0] != 'L' && buf[0] != 'U') ||
581 (buf[1] != '+' && buf[1] != '-'))
582 goto fail;
583
584 err = kstrtoint(buf + 1, 10, &nlink_diff);
585 if (err < 0)
586 goto fail;
587
588 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
589 nlink += nlink_diff;
590
591 if (nlink <= 0)
592 goto fail;
593
594 return nlink;
595
596fail:
597 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
598 upperdentry, err);
599 return fallback;
600}
601
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100602struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200603{
604 struct inode *inode;
605
606 inode = new_inode(sb);
607 if (inode)
Amir Goldstein695b46e2018-03-15 23:39:01 +0200608 ovl_fill_inode(inode, mode, rdev, 0);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200609
610 return inode;
611}
612
613static int ovl_inode_test(struct inode *inode, void *data)
614{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200615 return inode->i_private == data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200616}
617
618static int ovl_inode_set(struct inode *inode, void *data)
619{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200620 inode->i_private = data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200621 return 0;
622}
623
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200624static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
Amir Goldstein4b91c302018-01-18 16:39:13 +0200625 struct dentry *upperdentry, bool strict)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200626{
Amir Goldstein4b91c302018-01-18 16:39:13 +0200627 /*
628 * For directories, @strict verify from lookup path performs consistency
629 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
630 * inode. Non @strict verify from NFS handle decode path passes NULL for
631 * 'unknown' lower/upper.
632 */
633 if (S_ISDIR(inode->i_mode) && strict) {
Amir Goldstein31747ed2018-01-14 18:35:40 +0200634 /* Real lower dir moved to upper layer under us? */
635 if (!lowerdentry && ovl_inode_lower(inode))
636 return false;
637
638 /* Lookup of an uncovered redirect origin? */
639 if (!upperdentry && ovl_inode_upper(inode))
640 return false;
641 }
642
Amir Goldstein939ae4e2017-09-11 16:30:15 +0300643 /*
644 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
645 * This happens when finding a copied up overlay inode for a renamed
646 * or hardlinked overlay dentry and lower dentry cannot be followed
647 * by origin because lower fs does not support file handles.
648 */
649 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200650 return false;
651
652 /*
653 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
654 * This happens when finding a lower alias for a copied up hard link.
655 */
656 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
657 return false;
658
659 return true;
660}
661
Amir Goldstein4b91c302018-01-18 16:39:13 +0200662struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
663 bool is_upper)
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200664{
Amir Goldstein4b91c302018-01-18 16:39:13 +0200665 struct inode *inode, *key = d_inode(real);
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200666
667 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
668 if (!inode)
669 return NULL;
670
Amir Goldstein4b91c302018-01-18 16:39:13 +0200671 if (!ovl_verify_inode(inode, is_upper ? NULL : real,
672 is_upper ? real : NULL, false)) {
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200673 iput(inode);
674 return ERR_PTR(-ESTALE);
675 }
676
677 return inode;
678}
679
Amir Goldstein764baba2018-02-04 15:35:09 +0200680/*
681 * Does overlay inode need to be hashed by lower inode?
682 */
683static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
684 struct dentry *lower, struct dentry *index)
685{
686 struct ovl_fs *ofs = sb->s_fs_info;
687
688 /* No, if pure upper */
689 if (!lower)
690 return false;
691
692 /* Yes, if already indexed */
693 if (index)
694 return true;
695
696 /* Yes, if won't be copied up */
697 if (!ofs->upper_mnt)
698 return true;
699
700 /* No, if lower hardlink is or will be broken on copy up */
701 if ((upper || !ovl_indexdir(sb)) &&
702 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
703 return false;
704
705 /* No, if non-indexed upper with NFS export */
706 if (sb->s_export_op && upper)
707 return false;
708
709 /* Otherwise, hash by lower inode for fsnotify */
710 return true;
711}
712
Amir Goldstein0aceb532017-12-12 23:43:16 +0200713struct inode *ovl_get_inode(struct super_block *sb, struct dentry *upperdentry,
714 struct dentry *lowerdentry, struct dentry *index,
715 unsigned int numlower)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200716{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200717 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200718 struct inode *inode;
Amir Goldstein764baba2018-02-04 15:35:09 +0200719 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, index);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200720 bool is_dir;
Amir Goldstein695b46e2018-03-15 23:39:01 +0200721 unsigned long ino = 0;
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300722
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200723 if (!realinode)
724 realinode = d_inode(lowerdentry);
725
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300726 /*
Amir Goldstein764baba2018-02-04 15:35:09 +0200727 * Copy up origin (lower) may exist for non-indexed upper, but we must
728 * not use lower as hash key if this is a broken hardlink.
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300729 */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200730 is_dir = S_ISDIR(realinode->i_mode);
Amir Goldstein764baba2018-02-04 15:35:09 +0200731 if (upperdentry || bylower) {
732 struct inode *key = d_inode(bylower ? lowerdentry :
733 upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200734 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200735
Amir Goldstein0aceb532017-12-12 23:43:16 +0200736 inode = iget5_locked(sb, (unsigned long) key,
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200737 ovl_inode_test, ovl_inode_set, key);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200738 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200739 goto out_nomem;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200740 if (!(inode->i_state & I_NEW)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200741 /*
742 * Verify that the underlying files stored in the inode
743 * match those in the dentry.
744 */
Amir Goldstein4b91c302018-01-18 16:39:13 +0200745 if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
746 true)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200747 iput(inode);
748 inode = ERR_PTR(-ESTALE);
749 goto out;
750 }
751
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200752 dput(upperdentry);
753 goto out;
754 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200755
Amir Goldstein31747ed2018-01-14 18:35:40 +0200756 /* Recalculate nlink for non-dir due to indexing */
757 if (!is_dir)
758 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300759 set_nlink(inode, nlink);
Amir Goldstein695b46e2018-03-15 23:39:01 +0200760 ino = key->i_ino;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200761 } else {
Amir Goldstein764baba2018-02-04 15:35:09 +0200762 /* Lower hardlink that will be broken on copy up */
Amir Goldstein0aceb532017-12-12 23:43:16 +0200763 inode = new_inode(sb);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200764 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200765 goto out_nomem;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200766 }
Amir Goldstein695b46e2018-03-15 23:39:01 +0200767 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200768 ovl_inode_init(inode, upperdentry, lowerdentry);
Miklos Szeredi13c72072017-07-04 22:03:16 +0200769
770 if (upperdentry && ovl_is_impuredir(upperdentry))
771 ovl_set_flag(OVL_IMPURE, inode);
772
Vivek Goyal0471a9c2018-03-20 16:35:40 -0400773 if (index)
774 ovl_set_flag(OVL_INDEX, inode);
775
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300776 /* Check for non-merge dir that may have whiteouts */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200777 if (is_dir) {
Amir Goldstein0aceb532017-12-12 23:43:16 +0200778 if (((upperdentry && lowerdentry) || numlower > 1) ||
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300779 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
780 ovl_set_flag(OVL_WHITEOUTS, inode);
781 }
782 }
783
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200784 if (inode->i_state & I_NEW)
785 unlock_new_inode(inode);
786out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200787 return inode;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200788
789out_nomem:
790 inode = ERR_PTR(-ENOMEM);
791 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200792}