blob: b045cf1826fc4cb26d577d05916ab3545577061f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002/*
3 *
4 * Copyright (C) 2011 Novell Inc.
Miklos Szeredie9be9d52014-10-24 00:14:38 +02005 */
6
7#include <linux/fs.h>
8#include <linux/slab.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +01009#include <linux/cred.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020010#include <linux/xattr.h>
Miklos Szeredi5201dc42016-09-01 11:11:59 +020011#include <linux/posix_acl.h>
Amir Goldstein5f8415d2017-06-20 15:35:14 +030012#include <linux/ratelimit.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020013#include "overlayfs.h"
14
Chandan Rajendraba1e5632017-07-24 01:57:54 -050015
Miklos Szeredie9be9d52014-10-24 00:14:38 +020016int ovl_setattr(struct dentry *dentry, struct iattr *attr)
17{
18 int err;
Vivek Goyal997336f2018-05-11 11:49:33 -040019 bool full_copy_up = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020020 struct dentry *upperdentry;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040021 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020022
Jan Kara31051c82016-05-26 16:55:18 +020023 err = setattr_prepare(dentry, attr);
Miklos Szeredicf9a6782015-12-11 16:30:49 +010024 if (err)
25 return err;
26
Miklos Szeredie9be9d52014-10-24 00:14:38 +020027 err = ovl_want_write(dentry);
28 if (err)
29 goto out;
30
Miklos Szeredi58121602018-07-18 15:44:41 +020031 if (attr->ia_valid & ATTR_SIZE) {
32 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
33
34 err = -ETXTBSY;
35 if (atomic_read(&realinode->i_writecount) < 0)
36 goto out_drop_write;
Vivek Goyal997336f2018-05-11 11:49:33 -040037
38 /* Truncate should trigger data copy up as well */
39 full_copy_up = true;
Miklos Szeredi58121602018-07-18 15:44:41 +020040 }
41
Vivek Goyal997336f2018-05-11 11:49:33 -040042 if (!full_copy_up)
43 err = ovl_copy_up(dentry);
44 else
45 err = ovl_copy_up_with_data(dentry);
Miklos Szerediacff81e2015-12-04 19:18:48 +010046 if (!err) {
Miklos Szeredi58121602018-07-18 15:44:41 +020047 struct inode *winode = NULL;
48
Miklos Szerediacff81e2015-12-04 19:18:48 +010049 upperdentry = ovl_dentry_upper(dentry);
50
Miklos Szeredi58121602018-07-18 15:44:41 +020051 if (attr->ia_valid & ATTR_SIZE) {
52 winode = d_inode(upperdentry);
53 err = get_write_access(winode);
54 if (err)
55 goto out_drop_write;
56 }
57
Miklos Szeredib99c2d92016-07-04 16:49:48 +020058 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
59 attr->ia_valid &= ~ATTR_MODE;
60
Al Viro59551022016-01-22 15:40:57 -050061 inode_lock(upperdentry->d_inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040062 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020063 err = notify_change(upperdentry, attr, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040064 revert_creds(old_cred);
Konstantin Khlebnikovb81de062016-01-31 16:21:29 +030065 if (!err)
66 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Al Viro59551022016-01-22 15:40:57 -050067 inode_unlock(upperdentry->d_inode);
Miklos Szeredi58121602018-07-18 15:44:41 +020068
69 if (winode)
70 put_write_access(winode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020071 }
Miklos Szeredi58121602018-07-18 15:44:41 +020072out_drop_write:
Miklos Szeredie9be9d52014-10-24 00:14:38 +020073 ovl_drop_write(dentry);
74out:
75 return err;
76}
77
Amir Goldsteinda309e82017-11-08 19:39:51 +020078static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat,
79 struct ovl_layer *lower_layer)
80{
81 bool samefs = ovl_same_sb(dentry->d_sb);
Amir Goldsteine487d882017-11-07 13:55:04 +020082 unsigned int xinobits = ovl_xino_bits(dentry->d_sb);
Amir Goldsteinda309e82017-11-08 19:39:51 +020083
84 if (samefs) {
85 /*
86 * When all layers are on the same fs, all real inode
87 * number are unique, so we use the overlay st_dev,
88 * which is friendly to du -x.
89 */
90 stat->dev = dentry->d_sb->s_dev;
Amir Goldsteine487d882017-11-07 13:55:04 +020091 return 0;
92 } else if (xinobits) {
93 unsigned int shift = 64 - xinobits;
94 /*
95 * All inode numbers of underlying fs should not be using the
96 * high xinobits, so we use high xinobits to partition the
97 * overlay st_ino address space. The high bits holds the fsid
98 * (upper fsid is 0). This way overlay inode numbers are unique
99 * and all inodes use overlay st_dev. Inode numbers are also
100 * persistent for a given layer configuration.
101 */
102 if (stat->ino >> shift) {
103 pr_warn_ratelimited("overlayfs: inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
104 dentry, stat->ino, xinobits);
105 } else {
106 if (lower_layer)
107 stat->ino |= ((u64)lower_layer->fsid) << shift;
108
109 stat->dev = dentry->d_sb->s_dev;
110 return 0;
111 }
112 }
113
114 /* The inode could not be mapped to a unified st_ino address space */
115 if (S_ISDIR(dentry->d_inode->i_mode)) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200116 /*
117 * Always use the overlay st_dev for directories, so 'find
118 * -xdev' will scan the entire overlay mount and won't cross the
119 * overlay mount boundaries.
120 *
121 * If not all layers are on the same fs the pair {real st_ino;
122 * overlay st_dev} is not unique, so use the non persistent
123 * overlay st_ino for directories.
124 */
125 stat->dev = dentry->d_sb->s_dev;
126 stat->ino = dentry->d_inode->i_ino;
Amir Goldstein51486262018-03-28 20:22:41 +0300127 } else if (lower_layer && lower_layer->fsid) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200128 /*
129 * For non-samefs setup, if we cannot map all layers st_ino
130 * to a unified address space, we need to make sure that st_dev
Amir Goldstein51486262018-03-28 20:22:41 +0300131 * is unique per lower fs. Upper layer uses real st_dev and
132 * lower layers use the unique anonymous bdev assigned to the
133 * lower fs.
Amir Goldsteinda309e82017-11-08 19:39:51 +0200134 */
Amir Goldstein51486262018-03-28 20:22:41 +0300135 stat->dev = lower_layer->fs->pseudo_dev;
Amir Goldsteinda309e82017-11-08 19:39:51 +0200136 }
137
138 return 0;
139}
140
Miklos Szeredi5b712092017-05-05 11:38:58 +0200141int ovl_getattr(const struct path *path, struct kstat *stat,
142 u32 request_mask, unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200143{
David Howellsa528d352017-01-31 16:46:22 +0000144 struct dentry *dentry = path->dentry;
Amir Goldstein72b608f2017-04-24 00:25:49 +0300145 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200146 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400147 const struct cred *old_cred;
Miklos Szeredi5b712092017-05-05 11:38:58 +0200148 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
Amir Goldsteina0c5ad32017-11-01 00:45:40 +0200149 bool samefs = ovl_same_sb(dentry->d_sb);
Amir Goldsteinda309e82017-11-08 19:39:51 +0200150 struct ovl_layer *lower_layer = NULL;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400151 int err;
Vivek Goyal67d756c22018-05-11 11:49:30 -0400152 bool metacopy_blocks = false;
153
154 metacopy_blocks = ovl_is_metacopy_dentry(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200155
Amir Goldstein72b608f2017-04-24 00:25:49 +0300156 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400157 old_cred = ovl_override_creds(dentry->d_sb);
David Howellsa528d352017-01-31 16:46:22 +0000158 err = vfs_getattr(&realpath, stat, request_mask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300159 if (err)
160 goto out;
161
162 /*
Amir Goldsteinda309e82017-11-08 19:39:51 +0200163 * For non-dir or same fs, we use st_ino of the copy up origin.
164 * This guaranties constant st_dev/st_ino across copy up.
Amir Goldsteine487d882017-11-07 13:55:04 +0200165 * With xino feature and non-samefs, we use st_ino of the copy up
166 * origin masked with high bits that represent the layer id.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300167 *
Amir Goldsteinda309e82017-11-08 19:39:51 +0200168 * If lower filesystem supports NFS file handles, this also guaranties
Amir Goldstein72b608f2017-04-24 00:25:49 +0300169 * persistent st_ino across mount cycle.
170 */
Amir Goldsteine487d882017-11-07 13:55:04 +0200171 if (!is_dir || samefs || ovl_xino_bits(dentry->d_sb)) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200172 if (!OVL_TYPE_UPPER(type)) {
173 lower_layer = ovl_layer_lower(dentry);
174 } else if (OVL_TYPE_ORIGIN(type)) {
Amir Goldstein72b608f2017-04-24 00:25:49 +0300175 struct kstat lowerstat;
Vivek Goyal67d756c22018-05-11 11:49:30 -0400176 u32 lowermask = STATX_INO | STATX_BLOCKS |
177 (!is_dir ? STATX_NLINK : 0);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300178
179 ovl_path_lower(dentry, &realpath);
180 err = vfs_getattr(&realpath, &lowerstat,
Miklos Szeredi5b712092017-05-05 11:38:58 +0200181 lowermask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300182 if (err)
183 goto out;
184
Amir Goldstein72b608f2017-04-24 00:25:49 +0300185 /*
Amir Goldstein359f3922017-06-21 15:28:41 +0300186 * Lower hardlinks may be broken on copy up to different
Amir Goldstein72b608f2017-04-24 00:25:49 +0300187 * upper files, so we cannot use the lower origin st_ino
188 * for those different files, even for the same fs case.
Amir Goldstein86eaa132017-11-21 13:55:51 +0200189 *
190 * Similarly, several redirected dirs can point to the
191 * same dir on a lower layer. With the "verify_lower"
192 * feature, we do not use the lower origin st_ino, if
193 * we haven't verified that this redirect is unique.
194 *
Amir Goldstein359f3922017-06-21 15:28:41 +0300195 * With inodes index enabled, it is safe to use st_ino
Amir Goldstein86eaa132017-11-21 13:55:51 +0200196 * of an indexed origin. The index validates that the
197 * upper hardlink is not broken and that a redirected
198 * dir is the only redirect to that origin.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300199 */
Amir Goldstein86eaa132017-11-21 13:55:51 +0200200 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
201 (!ovl_verify_lower(dentry->d_sb) &&
Amir Goldstein9f99e502018-04-11 20:09:29 +0300202 (is_dir || lowerstat.nlink == 1))) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200203 lower_layer = ovl_layer_lower(dentry);
Amir Goldstein9c6d8f12019-11-17 17:43:44 +0200204 /*
205 * Cannot use origin st_dev;st_ino because
206 * origin inode content may differ from overlay
207 * inode content.
208 */
209 if (samefs || lower_layer->fsid)
210 stat->ino = lowerstat.ino;
Amir Goldstein9f99e502018-04-11 20:09:29 +0300211 }
Vivek Goyal67d756c22018-05-11 11:49:30 -0400212
213 /*
214 * If we are querying a metacopy dentry and lower
215 * dentry is data dentry, then use the blocks we
216 * queried just now. We don't have to do additional
217 * vfs_getattr(). If lower itself is metacopy, then
218 * additional vfs_getattr() is unavoidable.
219 */
220 if (metacopy_blocks &&
221 realpath.dentry == ovl_dentry_lowerdata(dentry)) {
222 stat->blocks = lowerstat.blocks;
223 metacopy_blocks = false;
224 }
225 }
226
227 if (metacopy_blocks) {
228 /*
229 * If lower is not same as lowerdata or if there was
230 * no origin on upper, we can end up here.
231 */
232 struct kstat lowerdatastat;
233 u32 lowermask = STATX_BLOCKS;
234
235 ovl_path_lowerdata(dentry, &realpath);
236 err = vfs_getattr(&realpath, &lowerdatastat,
237 lowermask, flags);
238 if (err)
239 goto out;
240 stat->blocks = lowerdatastat.blocks;
Amir Goldstein72b608f2017-04-24 00:25:49 +0300241 }
Amir Goldstein72b608f2017-04-24 00:25:49 +0300242 }
Miklos Szeredi5b712092017-05-05 11:38:58 +0200243
Amir Goldsteinda309e82017-11-08 19:39:51 +0200244 err = ovl_map_dev_ino(dentry, stat, lower_layer);
245 if (err)
246 goto out;
247
Miklos Szeredi5b712092017-05-05 11:38:58 +0200248 /*
249 * It's probably not worth it to count subdirs to get the
250 * correct link count. nlink=1 seems to pacify 'find' and
251 * other utilities.
252 */
253 if (is_dir && OVL_TYPE_MERGE(type))
254 stat->nlink = 1;
255
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300256 /*
257 * Return the overlay inode nlinks for indexed upper inodes.
258 * Overlay inode nlink counts the union of the upper hardlinks
259 * and non-covered lower hardlinks. It does not include the upper
260 * index hardlink.
261 */
262 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
263 stat->nlink = dentry->d_inode->i_nlink;
264
Amir Goldstein72b608f2017-04-24 00:25:49 +0300265out:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400266 revert_creds(old_cred);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300267
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400268 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200269}
270
271int ovl_permission(struct inode *inode, int mask)
272{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200273 struct inode *upperinode = ovl_inode_upper(inode);
274 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400275 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200276 int err;
277
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200278 /* Careful in RCU walk mode */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200279 if (!realinode) {
280 WARN_ON(!(mask & MAY_NOT_BLOCK));
Miklos Szeredia999d7e2016-07-29 12:05:23 +0200281 return -ECHILD;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200282 }
283
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400284 /*
285 * Check overlay inode with the creds of task and underlying inode
286 * with creds of mounter
287 */
288 err = generic_permission(inode, mask);
289 if (err)
290 return err;
291
Miklos Szerediec7ba112018-12-04 11:31:30 +0100292 old_cred = ovl_override_creds(inode->i_sb);
293 if (!upperinode &&
294 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
Vivek Goyal754f8cb2016-07-01 16:34:29 -0400295 mask &= ~(MAY_WRITE | MAY_APPEND);
Miklos Szerediec7ba112018-12-04 11:31:30 +0100296 /* Make sure mounter can read file for copy up later */
Vivek Goyal500cac32016-07-13 11:00:14 -0400297 mask |= MAY_READ;
298 }
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200299 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400300 revert_creds(old_cred);
301
302 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200303}
304
Al Viro6b255392015-11-17 10:20:54 -0500305static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500306 struct inode *inode,
307 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200308{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400309 const struct cred *old_cred;
310 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200311
Al Viro6b255392015-11-17 10:20:54 -0500312 if (!dentry)
313 return ERR_PTR(-ECHILD);
314
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400315 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200316 p = vfs_get_link(ovl_dentry_real(dentry), done);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400317 revert_creds(old_cred);
318 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200319}
320
Miklos Szeredi09562542016-08-08 15:08:49 +0200321bool ovl_is_private_xattr(const char *name)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200322{
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +0200323 return strncmp(name, OVL_XATTR_PREFIX,
324 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200325}
326
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200327int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
328 const void *value, size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200329{
330 int err;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200331 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
332 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400333 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200334
335 err = ovl_want_write(dentry);
336 if (err)
337 goto out;
338
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200339 if (!value && !upperdentry) {
340 err = vfs_getxattr(realdentry, name, NULL, 0);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200341 if (err < 0)
342 goto out_drop_write;
343 }
344
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200345 if (!upperdentry) {
346 err = ovl_copy_up(dentry);
347 if (err)
348 goto out_drop_write;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200349
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200350 realdentry = ovl_dentry_upper(dentry);
351 }
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200352
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400353 old_cred = ovl_override_creds(dentry->d_sb);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200354 if (value)
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200355 err = vfs_setxattr(realdentry, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200356 else {
357 WARN_ON(flags != XATTR_REPLACE);
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200358 err = vfs_removexattr(realdentry, name);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200359 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400360 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200361
Miklos Szeredid9854c82018-07-18 15:44:40 +0200362 /* copy c/mtime */
363 ovl_copyattr(d_inode(realdentry), inode);
364
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200365out_drop_write:
366 ovl_drop_write(dentry);
367out:
368 return err;
369}
370
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200371int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200372 void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200373{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400374 ssize_t res;
375 const struct cred *old_cred;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200376 struct dentry *realdentry =
377 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
Miklos Szeredi52148462014-11-20 16:40:00 +0100378
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400379 old_cred = ovl_override_creds(dentry->d_sb);
380 res = vfs_getxattr(realdentry, name, value, size);
381 revert_creds(old_cred);
382 return res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200383}
384
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200385static bool ovl_can_list(const char *s)
386{
387 /* List all non-trusted xatts */
388 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
389 return true;
390
391 /* Never list trusted.overlay, list other trusted for superuser only */
Mark Salyzyn5c2e9f32019-08-29 11:30:14 -0700392 return !ovl_is_private_xattr(s) &&
393 ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN);
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200394}
395
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200396ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
397{
Miklos Szeredib5817552016-06-06 16:21:37 +0200398 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200399 ssize_t res;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200400 size_t len;
401 char *s;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400402 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200403
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400404 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200405 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400406 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200407 if (res <= 0 || size == 0)
408 return res;
409
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200410 /* filter out private xattrs */
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200411 for (s = list, len = res; len;) {
412 size_t slen = strnlen(s, len) + 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200413
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200414 /* underlying fs providing us with an broken xattr list? */
415 if (WARN_ON(slen > len))
416 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200417
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200418 len -= slen;
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200419 if (!ovl_can_list(s)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200420 res -= slen;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200421 memmove(s, s + slen, len);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200422 } else {
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200423 s += slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200424 }
425 }
426
427 return res;
428}
429
Vivek Goyal39a25b22016-07-01 16:34:26 -0400430struct posix_acl *ovl_get_acl(struct inode *inode, int type)
431{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200432 struct inode *realinode = ovl_inode_real(inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400433 const struct cred *old_cred;
434 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400435
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200436 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
Vivek Goyal39a25b22016-07-01 16:34:26 -0400437 return NULL;
438
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400439 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200440 acl = get_acl(realinode, type);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400441 revert_creds(old_cred);
442
443 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400444}
445
Deepa Dinamani95582b02018-05-08 19:36:02 -0700446int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200447{
Miklos Szeredi8f35cf52018-04-12 12:04:50 +0200448 if (flags & S_ATIME) {
449 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
450 struct path upperpath = {
451 .mnt = ofs->upper_mnt,
452 .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
453 };
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200454
Miklos Szeredi8f35cf52018-04-12 12:04:50 +0200455 if (upperpath.dentry) {
456 touch_atime(&upperpath);
457 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
458 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200459 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200460 return 0;
461}
462
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200463static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
464 u64 start, u64 len)
465{
466 int err;
467 struct inode *realinode = ovl_inode_real(inode);
468 const struct cred *old_cred;
469
470 if (!realinode->i_op->fiemap)
471 return -EOPNOTSUPP;
472
473 old_cred = ovl_override_creds(inode->i_sb);
Amir Goldstein80d34812018-08-27 15:55:59 +0300474
475 if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC)
476 filemap_write_and_wait(realinode->i_mapping);
477
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200478 err = realinode->i_op->fiemap(realinode, fieinfo, start, len);
479 revert_creds(old_cred);
480
481 return err;
482}
483
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200484static const struct inode_operations ovl_file_inode_operations = {
485 .setattr = ovl_setattr,
486 .permission = ovl_permission,
487 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200488 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400489 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200490 .update_time = ovl_update_time,
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200491 .fiemap = ovl_fiemap,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200492};
493
494static const struct inode_operations ovl_symlink_inode_operations = {
495 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500496 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200497 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200498 .listxattr = ovl_listxattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200499 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200500};
501
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200502static const struct inode_operations ovl_special_inode_operations = {
503 .setattr = ovl_setattr,
504 .permission = ovl_permission,
505 .getattr = ovl_getattr,
506 .listxattr = ovl_listxattr,
507 .get_acl = ovl_get_acl,
508 .update_time = ovl_update_time,
509};
510
Wei Yongjun69383c52018-09-25 14:57:42 +0000511static const struct address_space_operations ovl_aops = {
Amir Goldstein5b910bd2018-08-27 15:56:00 +0300512 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
513 .direct_IO = noop_direct_IO,
514};
515
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200516/*
517 * It is possible to stack overlayfs instance on top of another
518 * overlayfs instance as lower layer. We need to annonate the
519 * stackable i_mutex locks according to stack level of the super
520 * block instance. An overlayfs instance can never be in stack
521 * depth 0 (there is always a real fs below it). An overlayfs
522 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
523 *
524 * For example, here is a snip from /proc/lockdep_chains after
525 * dir_iterate of nested overlayfs:
526 *
527 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
528 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
529 * [...] &type->i_mutex_dir_key (stack_depth=0)
530 */
531#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
532
533static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
534{
535#ifdef CONFIG_LOCKDEP
536 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
537 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300538 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200539
540 int depth = inode->i_sb->s_stack_depth - 1;
541
542 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
543 depth = 0;
544
545 if (S_ISDIR(inode->i_mode))
546 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
547 else
548 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300549
550 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200551#endif
552}
553
Amir Goldstein695b46e2018-03-15 23:39:01 +0200554static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
Amir Goldstein12574a92018-03-16 10:39:37 +0200555 unsigned long ino, int fsid)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200556{
Amir Goldstein12574a92018-03-16 10:39:37 +0200557 int xinobits = ovl_xino_bits(inode->i_sb);
558
Amir Goldstein695b46e2018-03-15 23:39:01 +0200559 /*
Amir Goldstein6dde1e42019-06-09 19:03:44 +0300560 * When d_ino is consistent with st_ino (samefs or i_ino has enough
561 * bits to encode layer), set the same value used for st_ino to i_ino,
562 * so inode number exposed via /proc/locks and a like will be
563 * consistent with d_ino and st_ino values. An i_ino value inconsistent
564 * with d_ino also causes nfsd readdirplus to fail. When called from
Amir Goldstein12574a92018-03-16 10:39:37 +0200565 * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
Amir Goldstein695b46e2018-03-15 23:39:01 +0200566 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
567 */
Amir Goldstein6dde1e42019-06-09 19:03:44 +0300568 if (ovl_same_sb(inode->i_sb) || xinobits) {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200569 inode->i_ino = ino;
Amir Goldstein12574a92018-03-16 10:39:37 +0200570 if (xinobits && fsid && !(ino >> (64 - xinobits)))
571 inode->i_ino |= (unsigned long)fsid << (64 - xinobits);
572 } else {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200573 inode->i_ino = get_next_ino();
Amir Goldstein12574a92018-03-16 10:39:37 +0200574 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200575 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200576 inode->i_flags |= S_NOCMTIME;
Miklos Szeredi2a3a2a32016-09-01 11:11:59 +0200577#ifdef CONFIG_FS_POSIX_ACL
578 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
579#endif
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200580
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200581 ovl_lockdep_annotate_inode_mutex_key(inode);
582
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100583 switch (mode & S_IFMT) {
584 case S_IFREG:
585 inode->i_op = &ovl_file_inode_operations;
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200586 inode->i_fop = &ovl_file_operations;
Amir Goldstein5b910bd2018-08-27 15:56:00 +0300587 inode->i_mapping->a_ops = &ovl_aops;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100588 break;
589
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200590 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200591 inode->i_op = &ovl_dir_inode_operations;
592 inode->i_fop = &ovl_dir_operations;
593 break;
594
595 case S_IFLNK:
596 inode->i_op = &ovl_symlink_inode_operations;
597 break;
598
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200599 default:
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200600 inode->i_op = &ovl_special_inode_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100601 init_special_inode(inode, mode, rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200602 break;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200603 }
604}
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200605
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300606/*
607 * With inodes index enabled, an overlay inode nlink counts the union of upper
608 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
609 * upper inode, the following nlink modifying operations can happen:
610 *
611 * 1. Lower hardlink copy up
612 * 2. Upper hardlink created, unlinked or renamed over
613 * 3. Lower hardlink whiteout or renamed over
614 *
615 * For the first, copy up case, the union nlink does not change, whether the
616 * operation succeeds or fails, but the upper inode nlink may change.
617 * Therefore, before copy up, we store the union nlink value relative to the
618 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
619 *
620 * For the second, upper hardlink case, the union nlink should be incremented
621 * or decremented IFF the operation succeeds, aligned with nlink change of the
622 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
623 * value relative to the upper inode nlink in the index inode.
624 *
625 * For the last, lower cover up case, we simplify things by preceding the
626 * whiteout or cover up with copy up. This makes sure that there is an index
627 * upper inode where the nlink xattr can be stored before the copied up upper
628 * entry is unlink.
629 */
630#define OVL_NLINK_ADD_UPPER (1 << 0)
631
632/*
633 * On-disk format for indexed nlink:
634 *
635 * nlink relative to the upper inode - "U[+-]NUM"
636 * nlink relative to the lower inode - "L[+-]NUM"
637 */
638
639static int ovl_set_nlink_common(struct dentry *dentry,
640 struct dentry *realdentry, const char *format)
641{
642 struct inode *inode = d_inode(dentry);
643 struct inode *realinode = d_inode(realdentry);
644 char buf[13];
645 int len;
646
647 len = snprintf(buf, sizeof(buf), format,
648 (int) (inode->i_nlink - realinode->i_nlink));
649
Miklos Szeredi67873412017-07-27 21:54:05 +0200650 if (WARN_ON(len >= sizeof(buf)))
651 return -EIO;
652
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300653 return ovl_do_setxattr(ovl_dentry_upper(dentry),
654 OVL_XATTR_NLINK, buf, len, 0);
655}
656
657int ovl_set_nlink_upper(struct dentry *dentry)
658{
659 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
660}
661
662int ovl_set_nlink_lower(struct dentry *dentry)
663{
664 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
665}
666
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300667unsigned int ovl_get_nlink(struct dentry *lowerdentry,
668 struct dentry *upperdentry,
669 unsigned int fallback)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300670{
671 int nlink_diff;
672 int nlink;
673 char buf[13];
674 int err;
675
676 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
677 return fallback;
678
679 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
680 if (err < 0)
681 goto fail;
682
683 buf[err] = '\0';
684 if ((buf[0] != 'L' && buf[0] != 'U') ||
685 (buf[1] != '+' && buf[1] != '-'))
686 goto fail;
687
688 err = kstrtoint(buf + 1, 10, &nlink_diff);
689 if (err < 0)
690 goto fail;
691
692 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
693 nlink += nlink_diff;
694
695 if (nlink <= 0)
696 goto fail;
697
698 return nlink;
699
700fail:
701 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
702 upperdentry, err);
703 return fallback;
704}
705
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100706struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200707{
708 struct inode *inode;
709
710 inode = new_inode(sb);
711 if (inode)
Amir Goldstein12574a92018-03-16 10:39:37 +0200712 ovl_fill_inode(inode, mode, rdev, 0, 0);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200713
714 return inode;
715}
716
717static int ovl_inode_test(struct inode *inode, void *data)
718{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200719 return inode->i_private == data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200720}
721
722static int ovl_inode_set(struct inode *inode, void *data)
723{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200724 inode->i_private = data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200725 return 0;
726}
727
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200728static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
Amir Goldstein4b91c302018-01-18 16:39:13 +0200729 struct dentry *upperdentry, bool strict)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200730{
Amir Goldstein4b91c302018-01-18 16:39:13 +0200731 /*
732 * For directories, @strict verify from lookup path performs consistency
733 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
734 * inode. Non @strict verify from NFS handle decode path passes NULL for
735 * 'unknown' lower/upper.
736 */
737 if (S_ISDIR(inode->i_mode) && strict) {
Amir Goldstein31747ed2018-01-14 18:35:40 +0200738 /* Real lower dir moved to upper layer under us? */
739 if (!lowerdentry && ovl_inode_lower(inode))
740 return false;
741
742 /* Lookup of an uncovered redirect origin? */
743 if (!upperdentry && ovl_inode_upper(inode))
744 return false;
745 }
746
Amir Goldstein939ae4e2017-09-11 16:30:15 +0300747 /*
748 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
749 * This happens when finding a copied up overlay inode for a renamed
750 * or hardlinked overlay dentry and lower dentry cannot be followed
751 * by origin because lower fs does not support file handles.
752 */
753 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200754 return false;
755
756 /*
757 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
758 * This happens when finding a lower alias for a copied up hard link.
759 */
760 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
761 return false;
762
763 return true;
764}
765
Amir Goldstein4b91c302018-01-18 16:39:13 +0200766struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
767 bool is_upper)
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200768{
Amir Goldstein4b91c302018-01-18 16:39:13 +0200769 struct inode *inode, *key = d_inode(real);
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200770
771 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
772 if (!inode)
773 return NULL;
774
Amir Goldstein4b91c302018-01-18 16:39:13 +0200775 if (!ovl_verify_inode(inode, is_upper ? NULL : real,
776 is_upper ? real : NULL, false)) {
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200777 iput(inode);
778 return ERR_PTR(-ESTALE);
779 }
780
781 return inode;
782}
783
Amir Goldstein146d62e2019-04-18 17:42:08 +0300784bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir)
785{
786 struct inode *key = d_inode(dir);
787 struct inode *trap;
788 bool res;
789
790 trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
791 if (!trap)
792 return false;
793
794 res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) &&
795 !ovl_inode_lower(trap);
796
797 iput(trap);
798 return res;
799}
800
801/*
802 * Create an inode cache entry for layer root dir, that will intentionally
803 * fail ovl_verify_inode(), so any lookup that will find some layer root
804 * will fail.
805 */
806struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
807{
808 struct inode *key = d_inode(dir);
809 struct inode *trap;
810
811 if (!d_is_dir(dir))
812 return ERR_PTR(-ENOTDIR);
813
814 trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
815 ovl_inode_set, key);
816 if (!trap)
817 return ERR_PTR(-ENOMEM);
818
819 if (!(trap->i_state & I_NEW)) {
820 /* Conflicting layer roots? */
821 iput(trap);
822 return ERR_PTR(-ELOOP);
823 }
824
825 trap->i_mode = S_IFDIR;
826 trap->i_flags = S_DEAD;
827 unlock_new_inode(trap);
828
829 return trap;
830}
831
Amir Goldstein764baba2018-02-04 15:35:09 +0200832/*
833 * Does overlay inode need to be hashed by lower inode?
834 */
835static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
836 struct dentry *lower, struct dentry *index)
837{
838 struct ovl_fs *ofs = sb->s_fs_info;
839
840 /* No, if pure upper */
841 if (!lower)
842 return false;
843
844 /* Yes, if already indexed */
845 if (index)
846 return true;
847
848 /* Yes, if won't be copied up */
849 if (!ofs->upper_mnt)
850 return true;
851
852 /* No, if lower hardlink is or will be broken on copy up */
853 if ((upper || !ovl_indexdir(sb)) &&
854 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
855 return false;
856
857 /* No, if non-indexed upper with NFS export */
858 if (sb->s_export_op && upper)
859 return false;
860
861 /* Otherwise, hash by lower inode for fsnotify */
862 return true;
863}
864
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300865static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
866 struct inode *key)
867{
868 return newinode ? inode_insert5(newinode, (unsigned long) key,
869 ovl_inode_test, ovl_inode_set, key) :
870 iget5_locked(sb, (unsigned long) key,
871 ovl_inode_test, ovl_inode_set, key);
872}
873
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400874struct inode *ovl_get_inode(struct super_block *sb,
875 struct ovl_inode_params *oip)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200876{
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400877 struct dentry *upperdentry = oip->upperdentry;
878 struct ovl_path *lowerpath = oip->lowerpath;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200879 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200880 struct inode *inode;
Amir Goldstein12574a92018-03-16 10:39:37 +0200881 struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400882 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
883 oip->index);
884 int fsid = bylower ? oip->lowerpath->layer->fsid : 0;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400885 bool is_dir, metacopy = false;
Amir Goldstein695b46e2018-03-15 23:39:01 +0200886 unsigned long ino = 0;
Amir Goldsteinacf30622019-03-28 17:38:29 +0200887 int err = oip->newinode ? -EEXIST : -ENOMEM;
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300888
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200889 if (!realinode)
890 realinode = d_inode(lowerdentry);
891
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300892 /*
Amir Goldstein764baba2018-02-04 15:35:09 +0200893 * Copy up origin (lower) may exist for non-indexed upper, but we must
894 * not use lower as hash key if this is a broken hardlink.
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300895 */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200896 is_dir = S_ISDIR(realinode->i_mode);
Amir Goldstein764baba2018-02-04 15:35:09 +0200897 if (upperdentry || bylower) {
898 struct inode *key = d_inode(bylower ? lowerdentry :
899 upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200900 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200901
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300902 inode = ovl_iget5(sb, oip->newinode, key);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200903 if (!inode)
Vivek Goyal027065b2018-05-11 11:49:28 -0400904 goto out_err;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200905 if (!(inode->i_state & I_NEW)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200906 /*
907 * Verify that the underlying files stored in the inode
908 * match those in the dentry.
909 */
Amir Goldstein4b91c302018-01-18 16:39:13 +0200910 if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
911 true)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200912 iput(inode);
Vivek Goyal027065b2018-05-11 11:49:28 -0400913 err = -ESTALE;
914 goto out_err;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200915 }
916
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200917 dput(upperdentry);
Vivek Goyal9cec54c2018-05-11 11:49:27 -0400918 kfree(oip->redirect);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200919 goto out;
920 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200921
Amir Goldstein31747ed2018-01-14 18:35:40 +0200922 /* Recalculate nlink for non-dir due to indexing */
923 if (!is_dir)
924 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300925 set_nlink(inode, nlink);
Amir Goldstein695b46e2018-03-15 23:39:01 +0200926 ino = key->i_ino;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200927 } else {
Amir Goldstein764baba2018-02-04 15:35:09 +0200928 /* Lower hardlink that will be broken on copy up */
Amir Goldstein0aceb532017-12-12 23:43:16 +0200929 inode = new_inode(sb);
Vivek Goyal027065b2018-05-11 11:49:28 -0400930 if (!inode) {
931 err = -ENOMEM;
932 goto out_err;
933 }
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200934 }
Amir Goldstein12574a92018-03-16 10:39:37 +0200935 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid);
Vivek Goyal2664bd02018-05-11 11:49:30 -0400936 ovl_inode_init(inode, upperdentry, lowerdentry, oip->lowerdata);
Miklos Szeredi13c72072017-07-04 22:03:16 +0200937
938 if (upperdentry && ovl_is_impuredir(upperdentry))
939 ovl_set_flag(OVL_IMPURE, inode);
940
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400941 if (oip->index)
Vivek Goyal0471a9c2018-03-20 16:35:40 -0400942 ovl_set_flag(OVL_INDEX, inode);
943
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400944 if (upperdentry) {
945 err = ovl_check_metacopy_xattr(upperdentry);
946 if (err < 0)
947 goto out_err;
948 metacopy = err;
949 if (!metacopy)
950 ovl_set_flag(OVL_UPPERDATA, inode);
951 }
952
Vivek Goyal9cec54c2018-05-11 11:49:27 -0400953 OVL_I(inode)->redirect = oip->redirect;
954
Vivek Goyala00c2d52018-05-11 11:49:32 -0400955 if (bylower)
956 ovl_set_flag(OVL_CONST_INO, inode);
957
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300958 /* Check for non-merge dir that may have whiteouts */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200959 if (is_dir) {
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400960 if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300961 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
962 ovl_set_flag(OVL_WHITEOUTS, inode);
963 }
964 }
965
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200966 if (inode->i_state & I_NEW)
967 unlock_new_inode(inode);
968out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200969 return inode;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200970
Vivek Goyal027065b2018-05-11 11:49:28 -0400971out_err:
Amir Goldsteinacf30622019-03-28 17:38:29 +0200972 pr_warn_ratelimited("overlayfs: failed to get inode (%i)\n", err);
Vivek Goyal027065b2018-05-11 11:49:28 -0400973 inode = ERR_PTR(err);
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200974 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200975}