blob: 03f0ec2b73ebcaa036d832ff82522bc3669c502a [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"
Amir Goldstein4eae06d2017-10-27 15:44:08 +030017#include "ovl_entry.h"
Miklos Szeredie9be9d52014-10-24 00:14:38 +020018
Miklos Szeredie9be9d52014-10-24 00:14:38 +020019int ovl_setattr(struct dentry *dentry, struct iattr *attr)
20{
21 int err;
22 struct dentry *upperdentry;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040023 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020024
Miklos Szeredicf9a6782015-12-11 16:30:49 +010025 /*
26 * Check for permissions before trying to copy-up. This is redundant
27 * since it will be rechecked later by ->setattr() on upper dentry. But
28 * without this, copy-up can be triggered by just about anybody.
29 *
30 * We don't initialize inode->size, which just means that
31 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
32 * check for a swapfile (which this won't be anyway).
33 */
Jan Kara31051c82016-05-26 16:55:18 +020034 err = setattr_prepare(dentry, attr);
Miklos Szeredicf9a6782015-12-11 16:30:49 +010035 if (err)
36 return err;
37
Miklos Szeredie9be9d52014-10-24 00:14:38 +020038 err = ovl_want_write(dentry);
39 if (err)
40 goto out;
41
Miklos Szerediacff81e2015-12-04 19:18:48 +010042 err = ovl_copy_up(dentry);
43 if (!err) {
44 upperdentry = ovl_dentry_upper(dentry);
45
Miklos Szeredib99c2d92016-07-04 16:49:48 +020046 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
47 attr->ia_valid &= ~ATTR_MODE;
48
Al Viro59551022016-01-22 15:40:57 -050049 inode_lock(upperdentry->d_inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040050 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020051 err = notify_change(upperdentry, attr, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040052 revert_creds(old_cred);
Konstantin Khlebnikovb81de062016-01-31 16:21:29 +030053 if (!err)
54 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Al Viro59551022016-01-22 15:40:57 -050055 inode_unlock(upperdentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020056 }
57 ovl_drop_write(dentry);
58out:
59 return err;
60}
61
Miklos Szeredi5b712092017-05-05 11:38:58 +020062int ovl_getattr(const struct path *path, struct kstat *stat,
63 u32 request_mask, unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020064{
David Howellsa528d352017-01-31 16:46:22 +000065 struct dentry *dentry = path->dentry;
Amir Goldstein72b608f2017-04-24 00:25:49 +030066 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020067 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040068 const struct cred *old_cred;
Miklos Szeredi5b712092017-05-05 11:38:58 +020069 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040070 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020071
Amir Goldstein72b608f2017-04-24 00:25:49 +030072 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040073 old_cred = ovl_override_creds(dentry->d_sb);
David Howellsa528d352017-01-31 16:46:22 +000074 err = vfs_getattr(&realpath, stat, request_mask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +030075 if (err)
76 goto out;
77
78 /*
79 * When all layers are on the same fs, all real inode number are
80 * unique, so we use the overlay st_dev, which is friendly to du -x.
81 *
82 * We also use st_ino of the copy up origin, if we know it.
83 * This guaranties constant st_dev/st_ino across copy up.
84 *
85 * If filesystem supports NFS export ops, this also guaranties
86 * persistent st_ino across mount cycle.
87 */
88 if (ovl_same_sb(dentry->d_sb)) {
89 if (OVL_TYPE_ORIGIN(type)) {
90 struct kstat lowerstat;
Miklos Szeredi5b712092017-05-05 11:38:58 +020091 u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
Amir Goldstein72b608f2017-04-24 00:25:49 +030092
93 ovl_path_lower(dentry, &realpath);
94 err = vfs_getattr(&realpath, &lowerstat,
Miklos Szeredi5b712092017-05-05 11:38:58 +020095 lowermask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +030096 if (err)
97 goto out;
98
99 WARN_ON_ONCE(stat->dev != lowerstat.dev);
100 /*
Amir Goldstein359f3922017-06-21 15:28:41 +0300101 * Lower hardlinks may be broken on copy up to different
Amir Goldstein72b608f2017-04-24 00:25:49 +0300102 * upper files, so we cannot use the lower origin st_ino
103 * for those different files, even for the same fs case.
Amir Goldstein359f3922017-06-21 15:28:41 +0300104 * With inodes index enabled, it is safe to use st_ino
105 * of an indexed hardlinked origin. The index validates
106 * that the upper hardlink is not broken.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300107 */
Amir Goldstein359f3922017-06-21 15:28:41 +0300108 if (is_dir || lowerstat.nlink == 1 ||
109 ovl_test_flag(OVL_INDEX, d_inode(dentry)))
Amir Goldstein72b608f2017-04-24 00:25:49 +0300110 stat->ino = lowerstat.ino;
111 }
112 stat->dev = dentry->d_sb->s_dev;
Miklos Szeredi5b712092017-05-05 11:38:58 +0200113 } else if (is_dir) {
114 /*
115 * If not all layers are on the same fs the pair {real st_ino;
116 * overlay st_dev} is not unique, so use the non persistent
117 * overlay st_ino.
118 *
119 * Always use the overlay st_dev for directories, so 'find
120 * -xdev' will scan the entire overlay mount and won't cross the
121 * overlay mount boundaries.
122 */
123 stat->dev = dentry->d_sb->s_dev;
124 stat->ino = dentry->d_inode->i_ino;
Amir Goldstein72b608f2017-04-24 00:25:49 +0300125 }
Miklos Szeredi5b712092017-05-05 11:38:58 +0200126
127 /*
128 * It's probably not worth it to count subdirs to get the
129 * correct link count. nlink=1 seems to pacify 'find' and
130 * other utilities.
131 */
132 if (is_dir && OVL_TYPE_MERGE(type))
133 stat->nlink = 1;
134
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300135 /*
136 * Return the overlay inode nlinks for indexed upper inodes.
137 * Overlay inode nlink counts the union of the upper hardlinks
138 * and non-covered lower hardlinks. It does not include the upper
139 * index hardlink.
140 */
141 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
142 stat->nlink = dentry->d_inode->i_nlink;
143
Amir Goldstein72b608f2017-04-24 00:25:49 +0300144out:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400145 revert_creds(old_cred);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300146
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400147 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200148}
149
150int ovl_permission(struct inode *inode, int mask)
151{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200152 struct inode *upperinode = ovl_inode_upper(inode);
153 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400154 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200155 int err;
156
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200157 /* Careful in RCU walk mode */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200158 if (!realinode) {
159 WARN_ON(!(mask & MAY_NOT_BLOCK));
Miklos Szeredia999d7e2016-07-29 12:05:23 +0200160 return -ECHILD;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200161 }
162
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400163 /*
164 * Check overlay inode with the creds of task and underlying inode
165 * with creds of mounter
166 */
167 err = generic_permission(inode, mask);
168 if (err)
169 return err;
170
171 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200172 if (!upperinode &&
173 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
Vivek Goyal754f8cb2016-07-01 16:34:29 -0400174 mask &= ~(MAY_WRITE | MAY_APPEND);
Vivek Goyal500cac32016-07-13 11:00:14 -0400175 /* Make sure mounter can read file for copy up later */
176 mask |= MAY_READ;
177 }
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200178 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400179 revert_creds(old_cred);
180
181 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200182}
183
Al Viro6b255392015-11-17 10:20:54 -0500184static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500185 struct inode *inode,
186 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200187{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400188 const struct cred *old_cred;
189 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200190
Al Viro6b255392015-11-17 10:20:54 -0500191 if (!dentry)
192 return ERR_PTR(-ECHILD);
193
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400194 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200195 p = vfs_get_link(ovl_dentry_real(dentry), done);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400196 revert_creds(old_cred);
197 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200198}
199
Miklos Szeredi09562542016-08-08 15:08:49 +0200200bool ovl_is_private_xattr(const char *name)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200201{
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +0200202 return strncmp(name, OVL_XATTR_PREFIX,
203 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200204}
205
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200206int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
207 const void *value, size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200208{
209 int err;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200210 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
211 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400212 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200213
214 err = ovl_want_write(dentry);
215 if (err)
216 goto out;
217
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200218 if (!value && !upperdentry) {
219 err = vfs_getxattr(realdentry, name, NULL, 0);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200220 if (err < 0)
221 goto out_drop_write;
222 }
223
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200224 if (!upperdentry) {
225 err = ovl_copy_up(dentry);
226 if (err)
227 goto out_drop_write;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200228
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200229 realdentry = ovl_dentry_upper(dentry);
230 }
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200231
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400232 old_cred = ovl_override_creds(dentry->d_sb);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200233 if (value)
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200234 err = vfs_setxattr(realdentry, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200235 else {
236 WARN_ON(flags != XATTR_REPLACE);
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200237 err = vfs_removexattr(realdentry, name);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200238 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400239 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200240
241out_drop_write:
242 ovl_drop_write(dentry);
243out:
244 return err;
245}
246
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200247int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200248 void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200249{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400250 ssize_t res;
251 const struct cred *old_cred;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200252 struct dentry *realdentry =
253 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
Miklos Szeredi52148462014-11-20 16:40:00 +0100254
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400255 old_cred = ovl_override_creds(dentry->d_sb);
256 res = vfs_getxattr(realdentry, name, value, size);
257 revert_creds(old_cred);
258 return res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200259}
260
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200261static bool ovl_can_list(const char *s)
262{
263 /* List all non-trusted xatts */
264 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
265 return true;
266
267 /* Never list trusted.overlay, list other trusted for superuser only */
268 return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
269}
270
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200271ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
272{
Miklos Szeredib5817552016-06-06 16:21:37 +0200273 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200274 ssize_t res;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200275 size_t len;
276 char *s;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400277 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200278
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400279 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200280 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400281 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200282 if (res <= 0 || size == 0)
283 return res;
284
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200285 /* filter out private xattrs */
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200286 for (s = list, len = res; len;) {
287 size_t slen = strnlen(s, len) + 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200288
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200289 /* underlying fs providing us with an broken xattr list? */
290 if (WARN_ON(slen > len))
291 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200292
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200293 len -= slen;
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200294 if (!ovl_can_list(s)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200295 res -= slen;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200296 memmove(s, s + slen, len);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200297 } else {
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200298 s += slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200299 }
300 }
301
302 return res;
303}
304
Vivek Goyal39a25b22016-07-01 16:34:26 -0400305struct posix_acl *ovl_get_acl(struct inode *inode, int type)
306{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200307 struct inode *realinode = ovl_inode_real(inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400308 const struct cred *old_cred;
309 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400310
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200311 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
Vivek Goyal39a25b22016-07-01 16:34:26 -0400312 return NULL;
313
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400314 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200315 acl = get_acl(realinode, type);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400316 revert_creds(old_cred);
317
318 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400319}
320
Amir Goldstein59be0972017-06-20 15:25:46 +0300321static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200322{
Amir Goldstein59be0972017-06-20 15:25:46 +0300323 if (ovl_dentry_upper(dentry) &&
324 ovl_dentry_has_upper_alias(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200325 return false;
326
Amir Goldstein59be0972017-06-20 15:25:46 +0300327 if (special_file(d_inode(dentry)->i_mode))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200328 return false;
329
330 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
331 return false;
332
333 return true;
334}
335
Miklos Szeredi2d902672016-06-30 08:53:27 +0200336int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200337{
Miklos Szeredi2d902672016-06-30 08:53:27 +0200338 int err = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200339
Amir Goldstein59be0972017-06-20 15:25:46 +0300340 if (ovl_open_need_copy_up(dentry, file_flags)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200341 err = ovl_want_write(dentry);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200342 if (!err) {
Amir Goldstein9aba6522016-11-12 21:36:03 +0200343 err = ovl_copy_up_flags(dentry, file_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200344 ovl_drop_write(dentry);
345 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200346 }
347
Miklos Szeredi2d902672016-06-30 08:53:27 +0200348 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200349}
350
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200351int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
352{
353 struct dentry *alias;
354 struct path upperpath;
355
356 if (!(flags & S_ATIME))
357 return 0;
358
359 alias = d_find_any_alias(inode);
360 if (!alias)
361 return 0;
362
363 ovl_path_upper(alias, &upperpath);
364 if (upperpath.dentry) {
365 touch_atime(&upperpath);
366 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
367 }
368
369 dput(alias);
370
371 return 0;
372}
373
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200374static const struct inode_operations ovl_file_inode_operations = {
375 .setattr = ovl_setattr,
376 .permission = ovl_permission,
377 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200378 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400379 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200380 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200381};
382
383static const struct inode_operations ovl_symlink_inode_operations = {
384 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500385 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200386 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200387 .listxattr = ovl_listxattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200388 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200389};
390
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200391/*
392 * It is possible to stack overlayfs instance on top of another
393 * overlayfs instance as lower layer. We need to annonate the
394 * stackable i_mutex locks according to stack level of the super
395 * block instance. An overlayfs instance can never be in stack
396 * depth 0 (there is always a real fs below it). An overlayfs
397 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
398 *
399 * For example, here is a snip from /proc/lockdep_chains after
400 * dir_iterate of nested overlayfs:
401 *
402 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
403 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
404 * [...] &type->i_mutex_dir_key (stack_depth=0)
405 */
406#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
407
408static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
409{
410#ifdef CONFIG_LOCKDEP
411 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
412 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300413 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200414
415 int depth = inode->i_sb->s_stack_depth - 1;
416
417 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
418 depth = 0;
419
420 if (S_ISDIR(inode->i_mode))
421 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
422 else
423 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300424
425 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200426#endif
427}
428
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100429static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200430{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200431 inode->i_ino = get_next_ino();
432 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200433 inode->i_flags |= S_NOCMTIME;
Miklos Szeredi2a3a2a32016-09-01 11:11:59 +0200434#ifdef CONFIG_FS_POSIX_ACL
435 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
436#endif
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200437
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200438 ovl_lockdep_annotate_inode_mutex_key(inode);
439
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100440 switch (mode & S_IFMT) {
441 case S_IFREG:
442 inode->i_op = &ovl_file_inode_operations;
443 break;
444
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200445 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200446 inode->i_op = &ovl_dir_inode_operations;
447 inode->i_fop = &ovl_dir_operations;
448 break;
449
450 case S_IFLNK:
451 inode->i_op = &ovl_symlink_inode_operations;
452 break;
453
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200454 default:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200455 inode->i_op = &ovl_file_inode_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100456 init_special_inode(inode, mode, rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200457 break;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200458 }
459}
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200460
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300461/*
462 * With inodes index enabled, an overlay inode nlink counts the union of upper
463 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
464 * upper inode, the following nlink modifying operations can happen:
465 *
466 * 1. Lower hardlink copy up
467 * 2. Upper hardlink created, unlinked or renamed over
468 * 3. Lower hardlink whiteout or renamed over
469 *
470 * For the first, copy up case, the union nlink does not change, whether the
471 * operation succeeds or fails, but the upper inode nlink may change.
472 * Therefore, before copy up, we store the union nlink value relative to the
473 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
474 *
475 * For the second, upper hardlink case, the union nlink should be incremented
476 * or decremented IFF the operation succeeds, aligned with nlink change of the
477 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
478 * value relative to the upper inode nlink in the index inode.
479 *
480 * For the last, lower cover up case, we simplify things by preceding the
481 * whiteout or cover up with copy up. This makes sure that there is an index
482 * upper inode where the nlink xattr can be stored before the copied up upper
483 * entry is unlink.
484 */
485#define OVL_NLINK_ADD_UPPER (1 << 0)
486
487/*
488 * On-disk format for indexed nlink:
489 *
490 * nlink relative to the upper inode - "U[+-]NUM"
491 * nlink relative to the lower inode - "L[+-]NUM"
492 */
493
494static int ovl_set_nlink_common(struct dentry *dentry,
495 struct dentry *realdentry, const char *format)
496{
497 struct inode *inode = d_inode(dentry);
498 struct inode *realinode = d_inode(realdentry);
499 char buf[13];
500 int len;
501
502 len = snprintf(buf, sizeof(buf), format,
503 (int) (inode->i_nlink - realinode->i_nlink));
504
Miklos Szeredi67873412017-07-27 21:54:05 +0200505 if (WARN_ON(len >= sizeof(buf)))
506 return -EIO;
507
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300508 return ovl_do_setxattr(ovl_dentry_upper(dentry),
509 OVL_XATTR_NLINK, buf, len, 0);
510}
511
512int ovl_set_nlink_upper(struct dentry *dentry)
513{
514 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
515}
516
517int ovl_set_nlink_lower(struct dentry *dentry)
518{
519 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
520}
521
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300522unsigned int ovl_get_nlink(struct dentry *lowerdentry,
523 struct dentry *upperdentry,
524 unsigned int fallback)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300525{
526 int nlink_diff;
527 int nlink;
528 char buf[13];
529 int err;
530
531 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
532 return fallback;
533
534 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
535 if (err < 0)
536 goto fail;
537
538 buf[err] = '\0';
539 if ((buf[0] != 'L' && buf[0] != 'U') ||
540 (buf[1] != '+' && buf[1] != '-'))
541 goto fail;
542
543 err = kstrtoint(buf + 1, 10, &nlink_diff);
544 if (err < 0)
545 goto fail;
546
547 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
548 nlink += nlink_diff;
549
550 if (nlink <= 0)
551 goto fail;
552
553 return nlink;
554
555fail:
556 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
557 upperdentry, err);
558 return fallback;
559}
560
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100561struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200562{
563 struct inode *inode;
564
565 inode = new_inode(sb);
566 if (inode)
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100567 ovl_fill_inode(inode, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200568
569 return inode;
570}
571
572static int ovl_inode_test(struct inode *inode, void *data)
573{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200574 return inode->i_private == data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200575}
576
577static int ovl_inode_set(struct inode *inode, void *data)
578{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200579 inode->i_private = data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200580 return 0;
581}
582
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200583static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
584 struct dentry *upperdentry)
585{
Amir Goldstein939ae4e2017-09-11 16:30:15 +0300586 /*
587 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
588 * This happens when finding a copied up overlay inode for a renamed
589 * or hardlinked overlay dentry and lower dentry cannot be followed
590 * by origin because lower fs does not support file handles.
591 */
592 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200593 return false;
594
595 /*
596 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
597 * This happens when finding a lower alias for a copied up hard link.
598 */
599 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
600 return false;
601
602 return true;
603}
604
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300605struct inode *ovl_get_inode(struct dentry *dentry, struct dentry *upperdentry,
606 struct dentry *index)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200607{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200608 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
609 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200610 struct inode *inode;
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300611 /* Already indexed or could be indexed on copy up? */
612 bool indexed = (index || (ovl_indexdir(dentry->d_sb) && !upperdentry));
613
614 if (WARN_ON(upperdentry && indexed && !lowerdentry))
615 return ERR_PTR(-EIO);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200616
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200617 if (!realinode)
618 realinode = d_inode(lowerdentry);
619
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300620 /*
621 * Copy up origin (lower) may exist for non-indexed upper, but we must
622 * not use lower as hash key in that case.
623 * Hash inodes that are or could be indexed by origin inode and
624 * non-indexed upper inodes that could be hard linked by upper inode.
625 */
626 if (!S_ISDIR(realinode->i_mode) && (upperdentry || indexed)) {
627 struct inode *key = d_inode(indexed ? lowerdentry :
628 upperdentry);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300629 unsigned int nlink;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200630
631 inode = iget5_locked(dentry->d_sb, (unsigned long) key,
632 ovl_inode_test, ovl_inode_set, key);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200633 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200634 goto out_nomem;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200635 if (!(inode->i_state & I_NEW)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200636 /*
637 * Verify that the underlying files stored in the inode
638 * match those in the dentry.
639 */
640 if (!ovl_verify_inode(inode, lowerdentry, upperdentry)) {
641 iput(inode);
642 inode = ERR_PTR(-ESTALE);
643 goto out;
644 }
645
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200646 dput(upperdentry);
647 goto out;
648 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200649
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300650 nlink = ovl_get_nlink(lowerdentry, upperdentry,
651 realinode->i_nlink);
652 set_nlink(inode, nlink);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200653 } else {
654 inode = new_inode(dentry->d_sb);
655 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200656 goto out_nomem;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200657 }
658 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200659 ovl_inode_init(inode, upperdentry, lowerdentry);
Miklos Szeredi13c72072017-07-04 22:03:16 +0200660
661 if (upperdentry && ovl_is_impuredir(upperdentry))
662 ovl_set_flag(OVL_IMPURE, inode);
663
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200664 if (inode->i_state & I_NEW)
665 unlock_new_inode(inode);
666out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200667 return inode;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200668
669out_nomem:
670 inode = ERR_PTR(-ENOMEM);
671 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200672}