blob: 94d2f8a8b779b0a8ddbfa11e782d1ac77575be1c [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 Goldstein359f3922017-06-21 15:28:41 +0300108 * With inodes index enabled, it is safe to use st_ino
109 * of an indexed hardlinked origin. The index validates
110 * that the upper hardlink is not broken.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300111 */
Amir Goldstein359f3922017-06-21 15:28:41 +0300112 if (is_dir || lowerstat.nlink == 1 ||
113 ovl_test_flag(OVL_INDEX, d_inode(dentry)))
Amir Goldstein72b608f2017-04-24 00:25:49 +0300114 stat->ino = lowerstat.ino;
Amir Goldsteina0c5ad32017-11-01 00:45:40 +0200115
116 if (samefs)
117 WARN_ON_ONCE(stat->dev != lowerstat.dev);
118 else
119 stat->dev = ovl_get_pseudo_dev(dentry);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300120 }
Amir Goldsteina0c5ad32017-11-01 00:45:40 +0200121 if (samefs) {
122 /*
123 * When all layers are on the same fs, all real inode
124 * number are unique, so we use the overlay st_dev,
125 * which is friendly to du -x.
126 */
127 stat->dev = dentry->d_sb->s_dev;
128 } else if (!OVL_TYPE_UPPER(type)) {
129 /*
130 * For non-samefs setup, to make sure that st_dev/st_ino
131 * pair is unique across the system, we use a unique
132 * anonymous st_dev for lower layer inode.
133 */
134 stat->dev = ovl_get_pseudo_dev(dentry);
135 }
136 } else {
Miklos Szeredi5b712092017-05-05 11:38:58 +0200137 /*
Miklos Szeredi5b712092017-05-05 11:38:58 +0200138 * Always use the overlay st_dev for directories, so 'find
139 * -xdev' will scan the entire overlay mount and won't cross the
140 * overlay mount boundaries.
Amir Goldsteina0c5ad32017-11-01 00:45:40 +0200141 *
142 * If not all layers are on the same fs the pair {real st_ino;
143 * overlay st_dev} is not unique, so use the non persistent
144 * overlay st_ino for directories.
Miklos Szeredi5b712092017-05-05 11:38:58 +0200145 */
146 stat->dev = dentry->d_sb->s_dev;
147 stat->ino = dentry->d_inode->i_ino;
Amir Goldstein72b608f2017-04-24 00:25:49 +0300148 }
Miklos Szeredi5b712092017-05-05 11:38:58 +0200149
150 /*
151 * It's probably not worth it to count subdirs to get the
152 * correct link count. nlink=1 seems to pacify 'find' and
153 * other utilities.
154 */
155 if (is_dir && OVL_TYPE_MERGE(type))
156 stat->nlink = 1;
157
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300158 /*
159 * Return the overlay inode nlinks for indexed upper inodes.
160 * Overlay inode nlink counts the union of the upper hardlinks
161 * and non-covered lower hardlinks. It does not include the upper
162 * index hardlink.
163 */
164 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
165 stat->nlink = dentry->d_inode->i_nlink;
166
Amir Goldstein72b608f2017-04-24 00:25:49 +0300167out:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400168 revert_creds(old_cred);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300169
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400170 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200171}
172
173int ovl_permission(struct inode *inode, int mask)
174{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200175 struct inode *upperinode = ovl_inode_upper(inode);
176 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400177 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200178 int err;
179
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200180 /* Careful in RCU walk mode */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200181 if (!realinode) {
182 WARN_ON(!(mask & MAY_NOT_BLOCK));
Miklos Szeredia999d7e2016-07-29 12:05:23 +0200183 return -ECHILD;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200184 }
185
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400186 /*
187 * Check overlay inode with the creds of task and underlying inode
188 * with creds of mounter
189 */
190 err = generic_permission(inode, mask);
191 if (err)
192 return err;
193
194 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200195 if (!upperinode &&
196 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
Vivek Goyal754f8cb2016-07-01 16:34:29 -0400197 mask &= ~(MAY_WRITE | MAY_APPEND);
Vivek Goyal500cac32016-07-13 11:00:14 -0400198 /* Make sure mounter can read file for copy up later */
199 mask |= MAY_READ;
200 }
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200201 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400202 revert_creds(old_cred);
203
204 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200205}
206
Al Viro6b255392015-11-17 10:20:54 -0500207static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500208 struct inode *inode,
209 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200210{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400211 const struct cred *old_cred;
212 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200213
Al Viro6b255392015-11-17 10:20:54 -0500214 if (!dentry)
215 return ERR_PTR(-ECHILD);
216
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400217 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200218 p = vfs_get_link(ovl_dentry_real(dentry), done);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400219 revert_creds(old_cred);
220 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200221}
222
Miklos Szeredi09562542016-08-08 15:08:49 +0200223bool ovl_is_private_xattr(const char *name)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200224{
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +0200225 return strncmp(name, OVL_XATTR_PREFIX,
226 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200227}
228
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200229int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
230 const void *value, size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200231{
232 int err;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200233 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
234 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400235 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200236
237 err = ovl_want_write(dentry);
238 if (err)
239 goto out;
240
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200241 if (!value && !upperdentry) {
242 err = vfs_getxattr(realdentry, name, NULL, 0);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200243 if (err < 0)
244 goto out_drop_write;
245 }
246
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200247 if (!upperdentry) {
248 err = ovl_copy_up(dentry);
249 if (err)
250 goto out_drop_write;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200251
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200252 realdentry = ovl_dentry_upper(dentry);
253 }
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200254
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400255 old_cred = ovl_override_creds(dentry->d_sb);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200256 if (value)
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200257 err = vfs_setxattr(realdentry, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200258 else {
259 WARN_ON(flags != XATTR_REPLACE);
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200260 err = vfs_removexattr(realdentry, name);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200261 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400262 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200263
264out_drop_write:
265 ovl_drop_write(dentry);
266out:
267 return err;
268}
269
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200270int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200271 void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200272{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400273 ssize_t res;
274 const struct cred *old_cred;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200275 struct dentry *realdentry =
276 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
Miklos Szeredi52148462014-11-20 16:40:00 +0100277
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400278 old_cred = ovl_override_creds(dentry->d_sb);
279 res = vfs_getxattr(realdentry, name, value, size);
280 revert_creds(old_cred);
281 return res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200282}
283
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200284static bool ovl_can_list(const char *s)
285{
286 /* List all non-trusted xatts */
287 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
288 return true;
289
290 /* Never list trusted.overlay, list other trusted for superuser only */
291 return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
292}
293
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200294ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
295{
Miklos Szeredib5817552016-06-06 16:21:37 +0200296 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200297 ssize_t res;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200298 size_t len;
299 char *s;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400300 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200301
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400302 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200303 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400304 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200305 if (res <= 0 || size == 0)
306 return res;
307
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200308 /* filter out private xattrs */
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200309 for (s = list, len = res; len;) {
310 size_t slen = strnlen(s, len) + 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200311
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200312 /* underlying fs providing us with an broken xattr list? */
313 if (WARN_ON(slen > len))
314 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200315
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200316 len -= slen;
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200317 if (!ovl_can_list(s)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200318 res -= slen;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200319 memmove(s, s + slen, len);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200320 } else {
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200321 s += slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200322 }
323 }
324
325 return res;
326}
327
Vivek Goyal39a25b22016-07-01 16:34:26 -0400328struct posix_acl *ovl_get_acl(struct inode *inode, int type)
329{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200330 struct inode *realinode = ovl_inode_real(inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400331 const struct cred *old_cred;
332 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400333
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200334 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
Vivek Goyal39a25b22016-07-01 16:34:26 -0400335 return NULL;
336
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400337 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200338 acl = get_acl(realinode, type);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400339 revert_creds(old_cred);
340
341 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400342}
343
Amir Goldstein59be0972017-06-20 15:25:46 +0300344static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200345{
Amir Goldstein59be0972017-06-20 15:25:46 +0300346 if (ovl_dentry_upper(dentry) &&
347 ovl_dentry_has_upper_alias(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200348 return false;
349
Amir Goldstein59be0972017-06-20 15:25:46 +0300350 if (special_file(d_inode(dentry)->i_mode))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200351 return false;
352
353 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
354 return false;
355
356 return true;
357}
358
Miklos Szeredi2d902672016-06-30 08:53:27 +0200359int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200360{
Miklos Szeredi2d902672016-06-30 08:53:27 +0200361 int err = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200362
Amir Goldstein59be0972017-06-20 15:25:46 +0300363 if (ovl_open_need_copy_up(dentry, file_flags)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200364 err = ovl_want_write(dentry);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200365 if (!err) {
Amir Goldstein9aba6522016-11-12 21:36:03 +0200366 err = ovl_copy_up_flags(dentry, file_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200367 ovl_drop_write(dentry);
368 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200369 }
370
Miklos Szeredi2d902672016-06-30 08:53:27 +0200371 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200372}
373
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200374int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
375{
376 struct dentry *alias;
377 struct path upperpath;
378
379 if (!(flags & S_ATIME))
380 return 0;
381
382 alias = d_find_any_alias(inode);
383 if (!alias)
384 return 0;
385
386 ovl_path_upper(alias, &upperpath);
387 if (upperpath.dentry) {
388 touch_atime(&upperpath);
389 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
390 }
391
392 dput(alias);
393
394 return 0;
395}
396
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200397static const struct inode_operations ovl_file_inode_operations = {
398 .setattr = ovl_setattr,
399 .permission = ovl_permission,
400 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200401 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400402 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200403 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200404};
405
406static const struct inode_operations ovl_symlink_inode_operations = {
407 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500408 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200409 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200410 .listxattr = ovl_listxattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200411 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200412};
413
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200414/*
415 * It is possible to stack overlayfs instance on top of another
416 * overlayfs instance as lower layer. We need to annonate the
417 * stackable i_mutex locks according to stack level of the super
418 * block instance. An overlayfs instance can never be in stack
419 * depth 0 (there is always a real fs below it). An overlayfs
420 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
421 *
422 * For example, here is a snip from /proc/lockdep_chains after
423 * dir_iterate of nested overlayfs:
424 *
425 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
426 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
427 * [...] &type->i_mutex_dir_key (stack_depth=0)
428 */
429#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
430
431static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
432{
433#ifdef CONFIG_LOCKDEP
434 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
435 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300436 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200437
438 int depth = inode->i_sb->s_stack_depth - 1;
439
440 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
441 depth = 0;
442
443 if (S_ISDIR(inode->i_mode))
444 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
445 else
446 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300447
448 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200449#endif
450}
451
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100452static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200453{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200454 inode->i_ino = get_next_ino();
455 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200456 inode->i_flags |= S_NOCMTIME;
Miklos Szeredi2a3a2a32016-09-01 11:11:59 +0200457#ifdef CONFIG_FS_POSIX_ACL
458 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
459#endif
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200460
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200461 ovl_lockdep_annotate_inode_mutex_key(inode);
462
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100463 switch (mode & S_IFMT) {
464 case S_IFREG:
465 inode->i_op = &ovl_file_inode_operations;
466 break;
467
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200468 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200469 inode->i_op = &ovl_dir_inode_operations;
470 inode->i_fop = &ovl_dir_operations;
471 break;
472
473 case S_IFLNK:
474 inode->i_op = &ovl_symlink_inode_operations;
475 break;
476
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200477 default:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200478 inode->i_op = &ovl_file_inode_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100479 init_special_inode(inode, mode, rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200480 break;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200481 }
482}
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200483
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300484/*
485 * With inodes index enabled, an overlay inode nlink counts the union of upper
486 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
487 * upper inode, the following nlink modifying operations can happen:
488 *
489 * 1. Lower hardlink copy up
490 * 2. Upper hardlink created, unlinked or renamed over
491 * 3. Lower hardlink whiteout or renamed over
492 *
493 * For the first, copy up case, the union nlink does not change, whether the
494 * operation succeeds or fails, but the upper inode nlink may change.
495 * Therefore, before copy up, we store the union nlink value relative to the
496 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
497 *
498 * For the second, upper hardlink case, the union nlink should be incremented
499 * or decremented IFF the operation succeeds, aligned with nlink change of the
500 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
501 * value relative to the upper inode nlink in the index inode.
502 *
503 * For the last, lower cover up case, we simplify things by preceding the
504 * whiteout or cover up with copy up. This makes sure that there is an index
505 * upper inode where the nlink xattr can be stored before the copied up upper
506 * entry is unlink.
507 */
508#define OVL_NLINK_ADD_UPPER (1 << 0)
509
510/*
511 * On-disk format for indexed nlink:
512 *
513 * nlink relative to the upper inode - "U[+-]NUM"
514 * nlink relative to the lower inode - "L[+-]NUM"
515 */
516
517static int ovl_set_nlink_common(struct dentry *dentry,
518 struct dentry *realdentry, const char *format)
519{
520 struct inode *inode = d_inode(dentry);
521 struct inode *realinode = d_inode(realdentry);
522 char buf[13];
523 int len;
524
525 len = snprintf(buf, sizeof(buf), format,
526 (int) (inode->i_nlink - realinode->i_nlink));
527
Miklos Szeredi67873412017-07-27 21:54:05 +0200528 if (WARN_ON(len >= sizeof(buf)))
529 return -EIO;
530
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300531 return ovl_do_setxattr(ovl_dentry_upper(dentry),
532 OVL_XATTR_NLINK, buf, len, 0);
533}
534
535int ovl_set_nlink_upper(struct dentry *dentry)
536{
537 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
538}
539
540int ovl_set_nlink_lower(struct dentry *dentry)
541{
542 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
543}
544
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300545unsigned int ovl_get_nlink(struct dentry *lowerdentry,
546 struct dentry *upperdentry,
547 unsigned int fallback)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300548{
549 int nlink_diff;
550 int nlink;
551 char buf[13];
552 int err;
553
554 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
555 return fallback;
556
557 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
558 if (err < 0)
559 goto fail;
560
561 buf[err] = '\0';
562 if ((buf[0] != 'L' && buf[0] != 'U') ||
563 (buf[1] != '+' && buf[1] != '-'))
564 goto fail;
565
566 err = kstrtoint(buf + 1, 10, &nlink_diff);
567 if (err < 0)
568 goto fail;
569
570 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
571 nlink += nlink_diff;
572
573 if (nlink <= 0)
574 goto fail;
575
576 return nlink;
577
578fail:
579 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
580 upperdentry, err);
581 return fallback;
582}
583
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100584struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200585{
586 struct inode *inode;
587
588 inode = new_inode(sb);
589 if (inode)
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100590 ovl_fill_inode(inode, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200591
592 return inode;
593}
594
595static int ovl_inode_test(struct inode *inode, void *data)
596{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200597 return inode->i_private == data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200598}
599
600static int ovl_inode_set(struct inode *inode, void *data)
601{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200602 inode->i_private = data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200603 return 0;
604}
605
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200606static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
607 struct dentry *upperdentry)
608{
Amir Goldstein31747ed2018-01-14 18:35:40 +0200609 if (S_ISDIR(inode->i_mode)) {
610 /* Real lower dir moved to upper layer under us? */
611 if (!lowerdentry && ovl_inode_lower(inode))
612 return false;
613
614 /* Lookup of an uncovered redirect origin? */
615 if (!upperdentry && ovl_inode_upper(inode))
616 return false;
617 }
618
Amir Goldstein939ae4e2017-09-11 16:30:15 +0300619 /*
620 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
621 * This happens when finding a copied up overlay inode for a renamed
622 * or hardlinked overlay dentry and lower dentry cannot be followed
623 * by origin because lower fs does not support file handles.
624 */
625 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200626 return false;
627
628 /*
629 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
630 * This happens when finding a lower alias for a copied up hard link.
631 */
632 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
633 return false;
634
635 return true;
636}
637
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300638struct inode *ovl_get_inode(struct dentry *dentry, struct dentry *upperdentry,
639 struct dentry *index)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200640{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200641 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
642 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200643 struct inode *inode;
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300644 /* Already indexed or could be indexed on copy up? */
645 bool indexed = (index || (ovl_indexdir(dentry->d_sb) && !upperdentry));
Amir Goldstein31747ed2018-01-14 18:35:40 +0200646 struct dentry *origin = indexed ? lowerdentry : NULL;
647 bool is_dir;
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300648
649 if (WARN_ON(upperdentry && indexed && !lowerdentry))
650 return ERR_PTR(-EIO);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200651
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200652 if (!realinode)
653 realinode = d_inode(lowerdentry);
654
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300655 /*
Amir Goldstein31747ed2018-01-14 18:35:40 +0200656 * Copy up origin (lower) may exist for non-indexed non-dir upper, but
657 * we must not use lower as hash key in that case.
658 * Hash non-dir that is or could be indexed by origin inode.
659 * Hash dir that is or could be merged by origin inode.
660 * Hash pure upper and non-indexed non-dir by upper inode.
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300661 */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200662 is_dir = S_ISDIR(realinode->i_mode);
663 if (is_dir)
664 origin = lowerdentry;
665
666 if (upperdentry || origin) {
667 struct inode *key = d_inode(origin ?: upperdentry);
668 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200669
670 inode = iget5_locked(dentry->d_sb, (unsigned long) key,
671 ovl_inode_test, ovl_inode_set, key);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200672 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200673 goto out_nomem;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200674 if (!(inode->i_state & I_NEW)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200675 /*
676 * Verify that the underlying files stored in the inode
677 * match those in the dentry.
678 */
679 if (!ovl_verify_inode(inode, lowerdentry, upperdentry)) {
680 iput(inode);
681 inode = ERR_PTR(-ESTALE);
682 goto out;
683 }
684
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200685 dput(upperdentry);
686 goto out;
687 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200688
Amir Goldstein31747ed2018-01-14 18:35:40 +0200689 /* Recalculate nlink for non-dir due to indexing */
690 if (!is_dir)
691 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300692 set_nlink(inode, nlink);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200693 } else {
694 inode = new_inode(dentry->d_sb);
695 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200696 goto out_nomem;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200697 }
698 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200699 ovl_inode_init(inode, upperdentry, lowerdentry);
Miklos Szeredi13c72072017-07-04 22:03:16 +0200700
701 if (upperdentry && ovl_is_impuredir(upperdentry))
702 ovl_set_flag(OVL_IMPURE, inode);
703
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300704 /* Check for non-merge dir that may have whiteouts */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200705 if (is_dir) {
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300706 struct ovl_entry *oe = dentry->d_fsdata;
707
708 if (((upperdentry && lowerdentry) || oe->numlower > 1) ||
709 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
710 ovl_set_flag(OVL_WHITEOUTS, inode);
711 }
712 }
713
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200714 if (inode->i_state & I_NEW)
715 unlock_new_inode(inode);
716out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200717 return inode;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200718
719out_nomem:
720 inode = ERR_PTR(-ENOMEM);
721 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200722}