blob: 69f4fc26ee398002879b7383b48f14a27e26c015 [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
Miklos Szeredie9be9d52014-10-24 00:14:38 +020018int ovl_setattr(struct dentry *dentry, struct iattr *attr)
19{
20 int err;
21 struct dentry *upperdentry;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040022 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020023
Miklos Szeredicf9a6782015-12-11 16:30:49 +010024 /*
25 * Check for permissions before trying to copy-up. This is redundant
26 * since it will be rechecked later by ->setattr() on upper dentry. But
27 * without this, copy-up can be triggered by just about anybody.
28 *
29 * We don't initialize inode->size, which just means that
30 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
31 * check for a swapfile (which this won't be anyway).
32 */
Jan Kara31051c82016-05-26 16:55:18 +020033 err = setattr_prepare(dentry, attr);
Miklos Szeredicf9a6782015-12-11 16:30:49 +010034 if (err)
35 return err;
36
Miklos Szeredie9be9d52014-10-24 00:14:38 +020037 err = ovl_want_write(dentry);
38 if (err)
39 goto out;
40
Miklos Szerediacff81e2015-12-04 19:18:48 +010041 err = ovl_copy_up(dentry);
42 if (!err) {
43 upperdentry = ovl_dentry_upper(dentry);
44
Miklos Szeredib99c2d92016-07-04 16:49:48 +020045 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
46 attr->ia_valid &= ~ATTR_MODE;
47
Al Viro59551022016-01-22 15:40:57 -050048 inode_lock(upperdentry->d_inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040049 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020050 err = notify_change(upperdentry, attr, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040051 revert_creds(old_cred);
Konstantin Khlebnikovb81de062016-01-31 16:21:29 +030052 if (!err)
53 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Al Viro59551022016-01-22 15:40:57 -050054 inode_unlock(upperdentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020055 }
56 ovl_drop_write(dentry);
57out:
58 return err;
59}
60
Miklos Szeredi5b712092017-05-05 11:38:58 +020061int ovl_getattr(const struct path *path, struct kstat *stat,
62 u32 request_mask, unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020063{
David Howellsa528d352017-01-31 16:46:22 +000064 struct dentry *dentry = path->dentry;
Amir Goldstein72b608f2017-04-24 00:25:49 +030065 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020066 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040067 const struct cred *old_cred;
Miklos Szeredi5b712092017-05-05 11:38:58 +020068 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040069 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020070
Amir Goldstein72b608f2017-04-24 00:25:49 +030071 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040072 old_cred = ovl_override_creds(dentry->d_sb);
David Howellsa528d352017-01-31 16:46:22 +000073 err = vfs_getattr(&realpath, stat, request_mask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +030074 if (err)
75 goto out;
76
77 /*
78 * When all layers are on the same fs, all real inode number are
79 * unique, so we use the overlay st_dev, which is friendly to du -x.
80 *
81 * We also use st_ino of the copy up origin, if we know it.
82 * This guaranties constant st_dev/st_ino across copy up.
83 *
84 * If filesystem supports NFS export ops, this also guaranties
85 * persistent st_ino across mount cycle.
86 */
87 if (ovl_same_sb(dentry->d_sb)) {
88 if (OVL_TYPE_ORIGIN(type)) {
89 struct kstat lowerstat;
Miklos Szeredi5b712092017-05-05 11:38:58 +020090 u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
Amir Goldstein72b608f2017-04-24 00:25:49 +030091
92 ovl_path_lower(dentry, &realpath);
93 err = vfs_getattr(&realpath, &lowerstat,
Miklos Szeredi5b712092017-05-05 11:38:58 +020094 lowermask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +030095 if (err)
96 goto out;
97
98 WARN_ON_ONCE(stat->dev != lowerstat.dev);
99 /*
Amir Goldstein359f3922017-06-21 15:28:41 +0300100 * Lower hardlinks may be broken on copy up to different
Amir Goldstein72b608f2017-04-24 00:25:49 +0300101 * upper files, so we cannot use the lower origin st_ino
102 * for those different files, even for the same fs case.
Amir Goldstein359f3922017-06-21 15:28:41 +0300103 * With inodes index enabled, it is safe to use st_ino
104 * of an indexed hardlinked origin. The index validates
105 * that the upper hardlink is not broken.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300106 */
Amir Goldstein359f3922017-06-21 15:28:41 +0300107 if (is_dir || lowerstat.nlink == 1 ||
108 ovl_test_flag(OVL_INDEX, d_inode(dentry)))
Amir Goldstein72b608f2017-04-24 00:25:49 +0300109 stat->ino = lowerstat.ino;
110 }
111 stat->dev = dentry->d_sb->s_dev;
Miklos Szeredi5b712092017-05-05 11:38:58 +0200112 } else if (is_dir) {
113 /*
114 * If not all layers are on the same fs the pair {real st_ino;
115 * overlay st_dev} is not unique, so use the non persistent
116 * overlay st_ino.
117 *
118 * Always use the overlay st_dev for directories, so 'find
119 * -xdev' will scan the entire overlay mount and won't cross the
120 * overlay mount boundaries.
121 */
122 stat->dev = dentry->d_sb->s_dev;
123 stat->ino = dentry->d_inode->i_ino;
Amir Goldstein72b608f2017-04-24 00:25:49 +0300124 }
Miklos Szeredi5b712092017-05-05 11:38:58 +0200125
126 /*
127 * It's probably not worth it to count subdirs to get the
128 * correct link count. nlink=1 seems to pacify 'find' and
129 * other utilities.
130 */
131 if (is_dir && OVL_TYPE_MERGE(type))
132 stat->nlink = 1;
133
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300134 /*
135 * Return the overlay inode nlinks for indexed upper inodes.
136 * Overlay inode nlink counts the union of the upper hardlinks
137 * and non-covered lower hardlinks. It does not include the upper
138 * index hardlink.
139 */
140 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
141 stat->nlink = dentry->d_inode->i_nlink;
142
Amir Goldstein72b608f2017-04-24 00:25:49 +0300143out:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400144 revert_creds(old_cred);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300145
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400146 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200147}
148
149int ovl_permission(struct inode *inode, int mask)
150{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200151 struct inode *upperinode = ovl_inode_upper(inode);
152 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400153 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200154 int err;
155
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200156 /* Careful in RCU walk mode */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200157 if (!realinode) {
158 WARN_ON(!(mask & MAY_NOT_BLOCK));
Miklos Szeredia999d7e2016-07-29 12:05:23 +0200159 return -ECHILD;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200160 }
161
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400162 /*
163 * Check overlay inode with the creds of task and underlying inode
164 * with creds of mounter
165 */
166 err = generic_permission(inode, mask);
167 if (err)
168 return err;
169
170 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200171 if (!upperinode &&
172 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
Vivek Goyal754f8cb2016-07-01 16:34:29 -0400173 mask &= ~(MAY_WRITE | MAY_APPEND);
Vivek Goyal500cac32016-07-13 11:00:14 -0400174 /* Make sure mounter can read file for copy up later */
175 mask |= MAY_READ;
176 }
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200177 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400178 revert_creds(old_cred);
179
180 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200181}
182
Al Viro6b255392015-11-17 10:20:54 -0500183static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500184 struct inode *inode,
185 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200186{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400187 const struct cred *old_cred;
188 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200189
Al Viro6b255392015-11-17 10:20:54 -0500190 if (!dentry)
191 return ERR_PTR(-ECHILD);
192
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400193 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200194 p = vfs_get_link(ovl_dentry_real(dentry), done);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400195 revert_creds(old_cred);
196 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200197}
198
Miklos Szeredi09562542016-08-08 15:08:49 +0200199bool ovl_is_private_xattr(const char *name)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200200{
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +0200201 return strncmp(name, OVL_XATTR_PREFIX,
202 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200203}
204
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200205int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value,
206 size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200207{
208 int err;
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200209 struct path realpath;
210 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400211 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200212
213 err = ovl_want_write(dentry);
214 if (err)
215 goto out;
216
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200217 if (!value && !OVL_TYPE_UPPER(type)) {
218 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
219 if (err < 0)
220 goto out_drop_write;
221 }
222
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200223 err = ovl_copy_up(dentry);
224 if (err)
225 goto out_drop_write;
226
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200227 if (!OVL_TYPE_UPPER(type))
228 ovl_path_upper(dentry, &realpath);
229
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400230 old_cred = ovl_override_creds(dentry->d_sb);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200231 if (value)
232 err = vfs_setxattr(realpath.dentry, name, value, size, flags);
233 else {
234 WARN_ON(flags != XATTR_REPLACE);
235 err = vfs_removexattr(realpath.dentry, name);
236 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400237 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200238
239out_drop_write:
240 ovl_drop_write(dentry);
241out:
242 return err;
243}
244
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200245int ovl_xattr_get(struct dentry *dentry, const char *name,
246 void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200247{
Miklos Szeredib5817552016-06-06 16:21:37 +0200248 struct dentry *realdentry = ovl_dentry_real(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400249 ssize_t res;
250 const struct cred *old_cred;
Miklos Szeredi52148462014-11-20 16:40:00 +0100251
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400252 old_cred = ovl_override_creds(dentry->d_sb);
253 res = vfs_getxattr(realdentry, name, value, size);
254 revert_creds(old_cred);
255 return res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200256}
257
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200258static bool ovl_can_list(const char *s)
259{
260 /* List all non-trusted xatts */
261 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
262 return true;
263
264 /* Never list trusted.overlay, list other trusted for superuser only */
265 return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
266}
267
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200268ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
269{
Miklos Szeredib5817552016-06-06 16:21:37 +0200270 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200271 ssize_t res;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200272 size_t len;
273 char *s;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400274 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200275
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400276 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200277 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400278 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200279 if (res <= 0 || size == 0)
280 return res;
281
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200282 /* filter out private xattrs */
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200283 for (s = list, len = res; len;) {
284 size_t slen = strnlen(s, len) + 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200285
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200286 /* underlying fs providing us with an broken xattr list? */
287 if (WARN_ON(slen > len))
288 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200289
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200290 len -= slen;
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200291 if (!ovl_can_list(s)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200292 res -= slen;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200293 memmove(s, s + slen, len);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200294 } else {
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200295 s += slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200296 }
297 }
298
299 return res;
300}
301
Vivek Goyal39a25b22016-07-01 16:34:26 -0400302struct posix_acl *ovl_get_acl(struct inode *inode, int type)
303{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200304 struct inode *realinode = ovl_inode_real(inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400305 const struct cred *old_cred;
306 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400307
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200308 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
Vivek Goyal39a25b22016-07-01 16:34:26 -0400309 return NULL;
310
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400311 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200312 acl = get_acl(realinode, type);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400313 revert_creds(old_cred);
314
315 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400316}
317
Amir Goldstein59be0972017-06-20 15:25:46 +0300318static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200319{
Amir Goldstein59be0972017-06-20 15:25:46 +0300320 if (ovl_dentry_upper(dentry) &&
321 ovl_dentry_has_upper_alias(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200322 return false;
323
Amir Goldstein59be0972017-06-20 15:25:46 +0300324 if (special_file(d_inode(dentry)->i_mode))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200325 return false;
326
327 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
328 return false;
329
330 return true;
331}
332
Miklos Szeredi2d902672016-06-30 08:53:27 +0200333int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200334{
Miklos Szeredi2d902672016-06-30 08:53:27 +0200335 int err = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200336
Amir Goldstein59be0972017-06-20 15:25:46 +0300337 if (ovl_open_need_copy_up(dentry, file_flags)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200338 err = ovl_want_write(dentry);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200339 if (!err) {
Amir Goldstein9aba6522016-11-12 21:36:03 +0200340 err = ovl_copy_up_flags(dentry, file_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200341 ovl_drop_write(dentry);
342 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200343 }
344
Miklos Szeredi2d902672016-06-30 08:53:27 +0200345 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200346}
347
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200348int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
349{
350 struct dentry *alias;
351 struct path upperpath;
352
353 if (!(flags & S_ATIME))
354 return 0;
355
356 alias = d_find_any_alias(inode);
357 if (!alias)
358 return 0;
359
360 ovl_path_upper(alias, &upperpath);
361 if (upperpath.dentry) {
362 touch_atime(&upperpath);
363 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
364 }
365
366 dput(alias);
367
368 return 0;
369}
370
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200371static const struct inode_operations ovl_file_inode_operations = {
372 .setattr = ovl_setattr,
373 .permission = ovl_permission,
374 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200375 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400376 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200377 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200378};
379
380static const struct inode_operations ovl_symlink_inode_operations = {
381 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500382 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200383 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200384 .listxattr = ovl_listxattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200385 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200386};
387
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200388/*
389 * It is possible to stack overlayfs instance on top of another
390 * overlayfs instance as lower layer. We need to annonate the
391 * stackable i_mutex locks according to stack level of the super
392 * block instance. An overlayfs instance can never be in stack
393 * depth 0 (there is always a real fs below it). An overlayfs
394 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
395 *
396 * For example, here is a snip from /proc/lockdep_chains after
397 * dir_iterate of nested overlayfs:
398 *
399 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
400 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
401 * [...] &type->i_mutex_dir_key (stack_depth=0)
402 */
403#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
404
405static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
406{
407#ifdef CONFIG_LOCKDEP
408 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
409 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
410
411 int depth = inode->i_sb->s_stack_depth - 1;
412
413 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
414 depth = 0;
415
416 if (S_ISDIR(inode->i_mode))
417 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
418 else
419 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
420#endif
421}
422
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100423static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200424{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200425 inode->i_ino = get_next_ino();
426 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200427 inode->i_flags |= S_NOCMTIME;
Miklos Szeredi2a3a2a32016-09-01 11:11:59 +0200428#ifdef CONFIG_FS_POSIX_ACL
429 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
430#endif
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200431
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200432 ovl_lockdep_annotate_inode_mutex_key(inode);
433
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100434 switch (mode & S_IFMT) {
435 case S_IFREG:
436 inode->i_op = &ovl_file_inode_operations;
437 break;
438
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200439 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200440 inode->i_op = &ovl_dir_inode_operations;
441 inode->i_fop = &ovl_dir_operations;
442 break;
443
444 case S_IFLNK:
445 inode->i_op = &ovl_symlink_inode_operations;
446 break;
447
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200448 default:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200449 inode->i_op = &ovl_file_inode_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100450 init_special_inode(inode, mode, rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200451 break;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200452 }
453}
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200454
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300455/*
456 * With inodes index enabled, an overlay inode nlink counts the union of upper
457 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
458 * upper inode, the following nlink modifying operations can happen:
459 *
460 * 1. Lower hardlink copy up
461 * 2. Upper hardlink created, unlinked or renamed over
462 * 3. Lower hardlink whiteout or renamed over
463 *
464 * For the first, copy up case, the union nlink does not change, whether the
465 * operation succeeds or fails, but the upper inode nlink may change.
466 * Therefore, before copy up, we store the union nlink value relative to the
467 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
468 *
469 * For the second, upper hardlink case, the union nlink should be incremented
470 * or decremented IFF the operation succeeds, aligned with nlink change of the
471 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
472 * value relative to the upper inode nlink in the index inode.
473 *
474 * For the last, lower cover up case, we simplify things by preceding the
475 * whiteout or cover up with copy up. This makes sure that there is an index
476 * upper inode where the nlink xattr can be stored before the copied up upper
477 * entry is unlink.
478 */
479#define OVL_NLINK_ADD_UPPER (1 << 0)
480
481/*
482 * On-disk format for indexed nlink:
483 *
484 * nlink relative to the upper inode - "U[+-]NUM"
485 * nlink relative to the lower inode - "L[+-]NUM"
486 */
487
488static int ovl_set_nlink_common(struct dentry *dentry,
489 struct dentry *realdentry, const char *format)
490{
491 struct inode *inode = d_inode(dentry);
492 struct inode *realinode = d_inode(realdentry);
493 char buf[13];
494 int len;
495
496 len = snprintf(buf, sizeof(buf), format,
497 (int) (inode->i_nlink - realinode->i_nlink));
498
499 return ovl_do_setxattr(ovl_dentry_upper(dentry),
500 OVL_XATTR_NLINK, buf, len, 0);
501}
502
503int ovl_set_nlink_upper(struct dentry *dentry)
504{
505 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
506}
507
508int ovl_set_nlink_lower(struct dentry *dentry)
509{
510 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
511}
512
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300513unsigned int ovl_get_nlink(struct dentry *lowerdentry,
514 struct dentry *upperdentry,
515 unsigned int fallback)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300516{
517 int nlink_diff;
518 int nlink;
519 char buf[13];
520 int err;
521
522 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
523 return fallback;
524
525 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
526 if (err < 0)
527 goto fail;
528
529 buf[err] = '\0';
530 if ((buf[0] != 'L' && buf[0] != 'U') ||
531 (buf[1] != '+' && buf[1] != '-'))
532 goto fail;
533
534 err = kstrtoint(buf + 1, 10, &nlink_diff);
535 if (err < 0)
536 goto fail;
537
538 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
539 nlink += nlink_diff;
540
541 if (nlink <= 0)
542 goto fail;
543
544 return nlink;
545
546fail:
547 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
548 upperdentry, err);
549 return fallback;
550}
551
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100552struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200553{
554 struct inode *inode;
555
556 inode = new_inode(sb);
557 if (inode)
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100558 ovl_fill_inode(inode, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200559
560 return inode;
561}
562
563static int ovl_inode_test(struct inode *inode, void *data)
564{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200565 return inode->i_private == data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200566}
567
568static int ovl_inode_set(struct inode *inode, void *data)
569{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200570 inode->i_private = data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200571 return 0;
572}
573
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200574static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
575 struct dentry *upperdentry)
576{
577 struct inode *lowerinode = lowerdentry ? d_inode(lowerdentry) : NULL;
578
579 /* Lower (origin) inode must match, even if NULL */
580 if (ovl_inode_lower(inode) != lowerinode)
581 return false;
582
583 /*
584 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
585 * This happens when finding a lower alias for a copied up hard link.
586 */
587 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
588 return false;
589
590 return true;
591}
592
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200593struct inode *ovl_get_inode(struct dentry *dentry, struct dentry *upperdentry)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200594{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200595 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
596 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200597 struct inode *inode;
598
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200599 if (!realinode)
600 realinode = d_inode(lowerdentry);
601
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200602 if (!S_ISDIR(realinode->i_mode) &&
603 (upperdentry || (lowerdentry && ovl_indexdir(dentry->d_sb)))) {
604 struct inode *key = d_inode(lowerdentry ?: upperdentry);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300605 unsigned int nlink;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200606
607 inode = iget5_locked(dentry->d_sb, (unsigned long) key,
608 ovl_inode_test, ovl_inode_set, key);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200609 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200610 goto out_nomem;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200611 if (!(inode->i_state & I_NEW)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200612 /*
613 * Verify that the underlying files stored in the inode
614 * match those in the dentry.
615 */
616 if (!ovl_verify_inode(inode, lowerdentry, upperdentry)) {
617 iput(inode);
618 inode = ERR_PTR(-ESTALE);
619 goto out;
620 }
621
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200622 dput(upperdentry);
623 goto out;
624 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200625
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300626 nlink = ovl_get_nlink(lowerdentry, upperdentry,
627 realinode->i_nlink);
628 set_nlink(inode, nlink);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200629 } else {
630 inode = new_inode(dentry->d_sb);
631 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200632 goto out_nomem;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200633 }
634 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200635 ovl_inode_init(inode, upperdentry, lowerdentry);
Miklos Szeredi13c72072017-07-04 22:03:16 +0200636
637 if (upperdentry && ovl_is_impuredir(upperdentry))
638 ovl_set_flag(OVL_IMPURE, inode);
639
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200640 if (inode->i_state & I_NEW)
641 unlock_new_inode(inode);
642out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200643 return inode;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200644
645out_nomem:
646 inode = ERR_PTR(-ENOMEM);
647 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200648}