blob: b599059d6f782e65d5a58c684a3097002959564a [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);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040077 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020078
Amir Goldstein72b608f2017-04-24 00:25:49 +030079 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040080 old_cred = ovl_override_creds(dentry->d_sb);
David Howellsa528d352017-01-31 16:46:22 +000081 err = vfs_getattr(&realpath, stat, request_mask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +030082 if (err)
83 goto out;
84
85 /*
86 * When all layers are on the same fs, all real inode number are
87 * unique, so we use the overlay st_dev, which is friendly to du -x.
88 *
89 * We also use st_ino of the copy up origin, if we know it.
90 * This guaranties constant st_dev/st_ino across copy up.
91 *
92 * If filesystem supports NFS export ops, this also guaranties
93 * persistent st_ino across mount cycle.
94 */
95 if (ovl_same_sb(dentry->d_sb)) {
96 if (OVL_TYPE_ORIGIN(type)) {
97 struct kstat lowerstat;
Miklos Szeredi5b712092017-05-05 11:38:58 +020098 u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
Amir Goldstein72b608f2017-04-24 00:25:49 +030099
100 ovl_path_lower(dentry, &realpath);
101 err = vfs_getattr(&realpath, &lowerstat,
Miklos Szeredi5b712092017-05-05 11:38:58 +0200102 lowermask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300103 if (err)
104 goto out;
105
106 WARN_ON_ONCE(stat->dev != lowerstat.dev);
107 /*
Amir Goldstein359f3922017-06-21 15:28:41 +0300108 * Lower hardlinks may be broken on copy up to different
Amir Goldstein72b608f2017-04-24 00:25:49 +0300109 * upper files, so we cannot use the lower origin st_ino
110 * for those different files, even for the same fs case.
Amir Goldstein359f3922017-06-21 15:28:41 +0300111 * With inodes index enabled, it is safe to use st_ino
112 * of an indexed hardlinked origin. The index validates
113 * that the upper hardlink is not broken.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300114 */
Amir Goldstein359f3922017-06-21 15:28:41 +0300115 if (is_dir || lowerstat.nlink == 1 ||
116 ovl_test_flag(OVL_INDEX, d_inode(dentry)))
Amir Goldstein72b608f2017-04-24 00:25:49 +0300117 stat->ino = lowerstat.ino;
118 }
119 stat->dev = dentry->d_sb->s_dev;
Miklos Szeredi5b712092017-05-05 11:38:58 +0200120 } else if (is_dir) {
121 /*
122 * If not all layers are on the same fs the pair {real st_ino;
123 * overlay st_dev} is not unique, so use the non persistent
124 * overlay st_ino.
125 *
126 * Always use the overlay st_dev for directories, so 'find
127 * -xdev' will scan the entire overlay mount and won't cross the
128 * overlay mount boundaries.
129 */
130 stat->dev = dentry->d_sb->s_dev;
131 stat->ino = dentry->d_inode->i_ino;
Chandan Rajendraba1e5632017-07-24 01:57:54 -0500132 } else if (!OVL_TYPE_UPPER(type)) {
133 /*
134 * For non-samefs setup, to make sure that st_dev/st_ino pair
135 * is unique across the system, we use a unique anonymous
136 * st_dev for lower layer inode.
137 */
138 stat->dev = ovl_get_pseudo_dev(dentry);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300139 }
Miklos Szeredi5b712092017-05-05 11:38:58 +0200140
141 /*
142 * It's probably not worth it to count subdirs to get the
143 * correct link count. nlink=1 seems to pacify 'find' and
144 * other utilities.
145 */
146 if (is_dir && OVL_TYPE_MERGE(type))
147 stat->nlink = 1;
148
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300149 /*
150 * Return the overlay inode nlinks for indexed upper inodes.
151 * Overlay inode nlink counts the union of the upper hardlinks
152 * and non-covered lower hardlinks. It does not include the upper
153 * index hardlink.
154 */
155 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
156 stat->nlink = dentry->d_inode->i_nlink;
157
Amir Goldstein72b608f2017-04-24 00:25:49 +0300158out:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400159 revert_creds(old_cred);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300160
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400161 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200162}
163
164int ovl_permission(struct inode *inode, int mask)
165{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200166 struct inode *upperinode = ovl_inode_upper(inode);
167 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400168 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200169 int err;
170
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200171 /* Careful in RCU walk mode */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200172 if (!realinode) {
173 WARN_ON(!(mask & MAY_NOT_BLOCK));
Miklos Szeredia999d7e2016-07-29 12:05:23 +0200174 return -ECHILD;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200175 }
176
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400177 /*
178 * Check overlay inode with the creds of task and underlying inode
179 * with creds of mounter
180 */
181 err = generic_permission(inode, mask);
182 if (err)
183 return err;
184
185 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200186 if (!upperinode &&
187 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
Vivek Goyal754f8cb2016-07-01 16:34:29 -0400188 mask &= ~(MAY_WRITE | MAY_APPEND);
Vivek Goyal500cac32016-07-13 11:00:14 -0400189 /* Make sure mounter can read file for copy up later */
190 mask |= MAY_READ;
191 }
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200192 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400193 revert_creds(old_cred);
194
195 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200196}
197
Al Viro6b255392015-11-17 10:20:54 -0500198static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500199 struct inode *inode,
200 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200201{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400202 const struct cred *old_cred;
203 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200204
Al Viro6b255392015-11-17 10:20:54 -0500205 if (!dentry)
206 return ERR_PTR(-ECHILD);
207
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400208 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200209 p = vfs_get_link(ovl_dentry_real(dentry), done);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400210 revert_creds(old_cred);
211 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200212}
213
Miklos Szeredi09562542016-08-08 15:08:49 +0200214bool ovl_is_private_xattr(const char *name)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200215{
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +0200216 return strncmp(name, OVL_XATTR_PREFIX,
217 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200218}
219
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200220int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
221 const void *value, size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200222{
223 int err;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200224 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
225 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400226 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200227
228 err = ovl_want_write(dentry);
229 if (err)
230 goto out;
231
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200232 if (!value && !upperdentry) {
233 err = vfs_getxattr(realdentry, name, NULL, 0);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200234 if (err < 0)
235 goto out_drop_write;
236 }
237
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200238 if (!upperdentry) {
239 err = ovl_copy_up(dentry);
240 if (err)
241 goto out_drop_write;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200242
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200243 realdentry = ovl_dentry_upper(dentry);
244 }
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200245
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400246 old_cred = ovl_override_creds(dentry->d_sb);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200247 if (value)
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200248 err = vfs_setxattr(realdentry, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200249 else {
250 WARN_ON(flags != XATTR_REPLACE);
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200251 err = vfs_removexattr(realdentry, name);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200252 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400253 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200254
255out_drop_write:
256 ovl_drop_write(dentry);
257out:
258 return err;
259}
260
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200261int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200262 void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200263{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400264 ssize_t res;
265 const struct cred *old_cred;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200266 struct dentry *realdentry =
267 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
Miklos Szeredi52148462014-11-20 16:40:00 +0100268
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400269 old_cred = ovl_override_creds(dentry->d_sb);
270 res = vfs_getxattr(realdentry, name, value, size);
271 revert_creds(old_cred);
272 return res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200273}
274
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200275static bool ovl_can_list(const char *s)
276{
277 /* List all non-trusted xatts */
278 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
279 return true;
280
281 /* Never list trusted.overlay, list other trusted for superuser only */
282 return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
283}
284
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200285ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
286{
Miklos Szeredib5817552016-06-06 16:21:37 +0200287 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200288 ssize_t res;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200289 size_t len;
290 char *s;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400291 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200292
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400293 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200294 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400295 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200296 if (res <= 0 || size == 0)
297 return res;
298
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200299 /* filter out private xattrs */
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200300 for (s = list, len = res; len;) {
301 size_t slen = strnlen(s, len) + 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200302
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200303 /* underlying fs providing us with an broken xattr list? */
304 if (WARN_ON(slen > len))
305 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200306
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200307 len -= slen;
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200308 if (!ovl_can_list(s)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200309 res -= slen;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200310 memmove(s, s + slen, len);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200311 } else {
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200312 s += slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200313 }
314 }
315
316 return res;
317}
318
Vivek Goyal39a25b22016-07-01 16:34:26 -0400319struct posix_acl *ovl_get_acl(struct inode *inode, int type)
320{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200321 struct inode *realinode = ovl_inode_real(inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400322 const struct cred *old_cred;
323 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400324
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200325 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
Vivek Goyal39a25b22016-07-01 16:34:26 -0400326 return NULL;
327
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400328 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200329 acl = get_acl(realinode, type);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400330 revert_creds(old_cred);
331
332 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400333}
334
Amir Goldstein59be0972017-06-20 15:25:46 +0300335static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200336{
Amir Goldstein59be0972017-06-20 15:25:46 +0300337 if (ovl_dentry_upper(dentry) &&
338 ovl_dentry_has_upper_alias(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200339 return false;
340
Amir Goldstein59be0972017-06-20 15:25:46 +0300341 if (special_file(d_inode(dentry)->i_mode))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200342 return false;
343
344 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
345 return false;
346
347 return true;
348}
349
Miklos Szeredi2d902672016-06-30 08:53:27 +0200350int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200351{
Miklos Szeredi2d902672016-06-30 08:53:27 +0200352 int err = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200353
Amir Goldstein59be0972017-06-20 15:25:46 +0300354 if (ovl_open_need_copy_up(dentry, file_flags)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200355 err = ovl_want_write(dentry);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200356 if (!err) {
Amir Goldstein9aba6522016-11-12 21:36:03 +0200357 err = ovl_copy_up_flags(dentry, file_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200358 ovl_drop_write(dentry);
359 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200360 }
361
Miklos Szeredi2d902672016-06-30 08:53:27 +0200362 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200363}
364
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200365int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
366{
367 struct dentry *alias;
368 struct path upperpath;
369
370 if (!(flags & S_ATIME))
371 return 0;
372
373 alias = d_find_any_alias(inode);
374 if (!alias)
375 return 0;
376
377 ovl_path_upper(alias, &upperpath);
378 if (upperpath.dentry) {
379 touch_atime(&upperpath);
380 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
381 }
382
383 dput(alias);
384
385 return 0;
386}
387
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200388static const struct inode_operations ovl_file_inode_operations = {
389 .setattr = ovl_setattr,
390 .permission = ovl_permission,
391 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200392 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400393 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200394 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200395};
396
397static const struct inode_operations ovl_symlink_inode_operations = {
398 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500399 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200400 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200401 .listxattr = ovl_listxattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200402 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200403};
404
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200405/*
406 * It is possible to stack overlayfs instance on top of another
407 * overlayfs instance as lower layer. We need to annonate the
408 * stackable i_mutex locks according to stack level of the super
409 * block instance. An overlayfs instance can never be in stack
410 * depth 0 (there is always a real fs below it). An overlayfs
411 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
412 *
413 * For example, here is a snip from /proc/lockdep_chains after
414 * dir_iterate of nested overlayfs:
415 *
416 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
417 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
418 * [...] &type->i_mutex_dir_key (stack_depth=0)
419 */
420#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
421
422static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
423{
424#ifdef CONFIG_LOCKDEP
425 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
426 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300427 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200428
429 int depth = inode->i_sb->s_stack_depth - 1;
430
431 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
432 depth = 0;
433
434 if (S_ISDIR(inode->i_mode))
435 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
436 else
437 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300438
439 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200440#endif
441}
442
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100443static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200444{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200445 inode->i_ino = get_next_ino();
446 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200447 inode->i_flags |= S_NOCMTIME;
Miklos Szeredi2a3a2a32016-09-01 11:11:59 +0200448#ifdef CONFIG_FS_POSIX_ACL
449 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
450#endif
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200451
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200452 ovl_lockdep_annotate_inode_mutex_key(inode);
453
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100454 switch (mode & S_IFMT) {
455 case S_IFREG:
456 inode->i_op = &ovl_file_inode_operations;
457 break;
458
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200459 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200460 inode->i_op = &ovl_dir_inode_operations;
461 inode->i_fop = &ovl_dir_operations;
462 break;
463
464 case S_IFLNK:
465 inode->i_op = &ovl_symlink_inode_operations;
466 break;
467
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200468 default:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200469 inode->i_op = &ovl_file_inode_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100470 init_special_inode(inode, mode, rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200471 break;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200472 }
473}
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200474
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300475/*
476 * With inodes index enabled, an overlay inode nlink counts the union of upper
477 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
478 * upper inode, the following nlink modifying operations can happen:
479 *
480 * 1. Lower hardlink copy up
481 * 2. Upper hardlink created, unlinked or renamed over
482 * 3. Lower hardlink whiteout or renamed over
483 *
484 * For the first, copy up case, the union nlink does not change, whether the
485 * operation succeeds or fails, but the upper inode nlink may change.
486 * Therefore, before copy up, we store the union nlink value relative to the
487 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
488 *
489 * For the second, upper hardlink case, the union nlink should be incremented
490 * or decremented IFF the operation succeeds, aligned with nlink change of the
491 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
492 * value relative to the upper inode nlink in the index inode.
493 *
494 * For the last, lower cover up case, we simplify things by preceding the
495 * whiteout or cover up with copy up. This makes sure that there is an index
496 * upper inode where the nlink xattr can be stored before the copied up upper
497 * entry is unlink.
498 */
499#define OVL_NLINK_ADD_UPPER (1 << 0)
500
501/*
502 * On-disk format for indexed nlink:
503 *
504 * nlink relative to the upper inode - "U[+-]NUM"
505 * nlink relative to the lower inode - "L[+-]NUM"
506 */
507
508static int ovl_set_nlink_common(struct dentry *dentry,
509 struct dentry *realdentry, const char *format)
510{
511 struct inode *inode = d_inode(dentry);
512 struct inode *realinode = d_inode(realdentry);
513 char buf[13];
514 int len;
515
516 len = snprintf(buf, sizeof(buf), format,
517 (int) (inode->i_nlink - realinode->i_nlink));
518
Miklos Szeredi67873412017-07-27 21:54:05 +0200519 if (WARN_ON(len >= sizeof(buf)))
520 return -EIO;
521
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300522 return ovl_do_setxattr(ovl_dentry_upper(dentry),
523 OVL_XATTR_NLINK, buf, len, 0);
524}
525
526int ovl_set_nlink_upper(struct dentry *dentry)
527{
528 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
529}
530
531int ovl_set_nlink_lower(struct dentry *dentry)
532{
533 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
534}
535
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300536unsigned int ovl_get_nlink(struct dentry *lowerdentry,
537 struct dentry *upperdentry,
538 unsigned int fallback)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300539{
540 int nlink_diff;
541 int nlink;
542 char buf[13];
543 int err;
544
545 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
546 return fallback;
547
548 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
549 if (err < 0)
550 goto fail;
551
552 buf[err] = '\0';
553 if ((buf[0] != 'L' && buf[0] != 'U') ||
554 (buf[1] != '+' && buf[1] != '-'))
555 goto fail;
556
557 err = kstrtoint(buf + 1, 10, &nlink_diff);
558 if (err < 0)
559 goto fail;
560
561 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
562 nlink += nlink_diff;
563
564 if (nlink <= 0)
565 goto fail;
566
567 return nlink;
568
569fail:
570 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
571 upperdentry, err);
572 return fallback;
573}
574
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100575struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200576{
577 struct inode *inode;
578
579 inode = new_inode(sb);
580 if (inode)
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100581 ovl_fill_inode(inode, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200582
583 return inode;
584}
585
586static int ovl_inode_test(struct inode *inode, void *data)
587{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200588 return inode->i_private == data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200589}
590
591static int ovl_inode_set(struct inode *inode, void *data)
592{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200593 inode->i_private = data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200594 return 0;
595}
596
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200597static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
598 struct dentry *upperdentry)
599{
Amir Goldstein939ae4e2017-09-11 16:30:15 +0300600 /*
601 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
602 * This happens when finding a copied up overlay inode for a renamed
603 * or hardlinked overlay dentry and lower dentry cannot be followed
604 * by origin because lower fs does not support file handles.
605 */
606 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200607 return false;
608
609 /*
610 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
611 * This happens when finding a lower alias for a copied up hard link.
612 */
613 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
614 return false;
615
616 return true;
617}
618
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300619struct inode *ovl_get_inode(struct dentry *dentry, struct dentry *upperdentry,
620 struct dentry *index)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200621{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200622 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
623 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200624 struct inode *inode;
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300625 /* Already indexed or could be indexed on copy up? */
626 bool indexed = (index || (ovl_indexdir(dentry->d_sb) && !upperdentry));
627
628 if (WARN_ON(upperdentry && indexed && !lowerdentry))
629 return ERR_PTR(-EIO);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200630
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200631 if (!realinode)
632 realinode = d_inode(lowerdentry);
633
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300634 /*
635 * Copy up origin (lower) may exist for non-indexed upper, but we must
636 * not use lower as hash key in that case.
637 * Hash inodes that are or could be indexed by origin inode and
638 * non-indexed upper inodes that could be hard linked by upper inode.
639 */
640 if (!S_ISDIR(realinode->i_mode) && (upperdentry || indexed)) {
641 struct inode *key = d_inode(indexed ? lowerdentry :
642 upperdentry);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300643 unsigned int nlink;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200644
645 inode = iget5_locked(dentry->d_sb, (unsigned long) key,
646 ovl_inode_test, ovl_inode_set, key);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200647 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200648 goto out_nomem;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200649 if (!(inode->i_state & I_NEW)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200650 /*
651 * Verify that the underlying files stored in the inode
652 * match those in the dentry.
653 */
654 if (!ovl_verify_inode(inode, lowerdentry, upperdentry)) {
655 iput(inode);
656 inode = ERR_PTR(-ESTALE);
657 goto out;
658 }
659
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200660 dput(upperdentry);
661 goto out;
662 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200663
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300664 nlink = ovl_get_nlink(lowerdentry, upperdentry,
665 realinode->i_nlink);
666 set_nlink(inode, nlink);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200667 } else {
668 inode = new_inode(dentry->d_sb);
669 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200670 goto out_nomem;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200671 }
672 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200673 ovl_inode_init(inode, upperdentry, lowerdentry);
Miklos Szeredi13c72072017-07-04 22:03:16 +0200674
675 if (upperdentry && ovl_is_impuredir(upperdentry))
676 ovl_set_flag(OVL_IMPURE, inode);
677
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300678 /* Check for non-merge dir that may have whiteouts */
679 if (S_ISDIR(realinode->i_mode)) {
680 struct ovl_entry *oe = dentry->d_fsdata;
681
682 if (((upperdentry && lowerdentry) || oe->numlower > 1) ||
683 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
684 ovl_set_flag(OVL_WHITEOUTS, inode);
685 }
686 }
687
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200688 if (inode->i_state & I_NEW)
689 unlock_new_inode(inode);
690out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200691 return inode;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200692
693out_nomem:
694 inode = ERR_PTR(-ENOMEM);
695 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200696}