blob: e0bb217c01e2a6c8389bf9ec2dfb2f56f0fab7c8 [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
2 *
3 * Copyright (C) 2011 Novell Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/fs.h>
11#include <linux/slab.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010012#include <linux/cred.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020013#include <linux/xattr.h>
Miklos Szeredi5201dc42016-09-01 11:11:59 +020014#include <linux/posix_acl.h>
Amir Goldstein5f8415d2017-06-20 15:35:14 +030015#include <linux/ratelimit.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020016#include "overlayfs.h"
17
Chandan Rajendraba1e5632017-07-24 01:57:54 -050018
Miklos Szeredie9be9d52014-10-24 00:14:38 +020019int ovl_setattr(struct dentry *dentry, struct iattr *attr)
20{
21 int err;
Vivek Goyal997336f2018-05-11 11:49:33 -040022 bool full_copy_up = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020023 struct dentry *upperdentry;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040024 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020025
Jan Kara31051c82016-05-26 16:55:18 +020026 err = setattr_prepare(dentry, attr);
Miklos Szeredicf9a6782015-12-11 16:30:49 +010027 if (err)
28 return err;
29
Miklos Szeredie9be9d52014-10-24 00:14:38 +020030 err = ovl_want_write(dentry);
31 if (err)
32 goto out;
33
Miklos Szeredi58121602018-07-18 15:44:41 +020034 if (attr->ia_valid & ATTR_SIZE) {
35 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
36
37 err = -ETXTBSY;
38 if (atomic_read(&realinode->i_writecount) < 0)
39 goto out_drop_write;
Vivek Goyal997336f2018-05-11 11:49:33 -040040
41 /* Truncate should trigger data copy up as well */
42 full_copy_up = true;
Miklos Szeredi58121602018-07-18 15:44:41 +020043 }
44
Vivek Goyal997336f2018-05-11 11:49:33 -040045 if (!full_copy_up)
46 err = ovl_copy_up(dentry);
47 else
48 err = ovl_copy_up_with_data(dentry);
Miklos Szerediacff81e2015-12-04 19:18:48 +010049 if (!err) {
Miklos Szeredi58121602018-07-18 15:44:41 +020050 struct inode *winode = NULL;
51
Miklos Szerediacff81e2015-12-04 19:18:48 +010052 upperdentry = ovl_dentry_upper(dentry);
53
Miklos Szeredi58121602018-07-18 15:44:41 +020054 if (attr->ia_valid & ATTR_SIZE) {
55 winode = d_inode(upperdentry);
56 err = get_write_access(winode);
57 if (err)
58 goto out_drop_write;
59 }
60
Miklos Szeredib99c2d92016-07-04 16:49:48 +020061 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
62 attr->ia_valid &= ~ATTR_MODE;
63
Al Viro59551022016-01-22 15:40:57 -050064 inode_lock(upperdentry->d_inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040065 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020066 err = notify_change(upperdentry, attr, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040067 revert_creds(old_cred);
Konstantin Khlebnikovb81de062016-01-31 16:21:29 +030068 if (!err)
69 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Al Viro59551022016-01-22 15:40:57 -050070 inode_unlock(upperdentry->d_inode);
Miklos Szeredi58121602018-07-18 15:44:41 +020071
72 if (winode)
73 put_write_access(winode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020074 }
Miklos Szeredi58121602018-07-18 15:44:41 +020075out_drop_write:
Miklos Szeredie9be9d52014-10-24 00:14:38 +020076 ovl_drop_write(dentry);
77out:
78 return err;
79}
80
Amir Goldsteinda309e82017-11-08 19:39:51 +020081static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat,
82 struct ovl_layer *lower_layer)
83{
84 bool samefs = ovl_same_sb(dentry->d_sb);
Amir Goldsteine487d882017-11-07 13:55:04 +020085 unsigned int xinobits = ovl_xino_bits(dentry->d_sb);
Amir Goldsteinda309e82017-11-08 19:39:51 +020086
87 if (samefs) {
88 /*
89 * When all layers are on the same fs, all real inode
90 * number are unique, so we use the overlay st_dev,
91 * which is friendly to du -x.
92 */
93 stat->dev = dentry->d_sb->s_dev;
Amir Goldsteine487d882017-11-07 13:55:04 +020094 return 0;
95 } else if (xinobits) {
96 unsigned int shift = 64 - xinobits;
97 /*
98 * All inode numbers of underlying fs should not be using the
99 * high xinobits, so we use high xinobits to partition the
100 * overlay st_ino address space. The high bits holds the fsid
101 * (upper fsid is 0). This way overlay inode numbers are unique
102 * and all inodes use overlay st_dev. Inode numbers are also
103 * persistent for a given layer configuration.
104 */
105 if (stat->ino >> shift) {
106 pr_warn_ratelimited("overlayfs: inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
107 dentry, stat->ino, xinobits);
108 } else {
109 if (lower_layer)
110 stat->ino |= ((u64)lower_layer->fsid) << shift;
111
112 stat->dev = dentry->d_sb->s_dev;
113 return 0;
114 }
115 }
116
117 /* The inode could not be mapped to a unified st_ino address space */
118 if (S_ISDIR(dentry->d_inode->i_mode)) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200119 /*
120 * Always use the overlay st_dev for directories, so 'find
121 * -xdev' will scan the entire overlay mount and won't cross the
122 * overlay mount boundaries.
123 *
124 * If not all layers are on the same fs the pair {real st_ino;
125 * overlay st_dev} is not unique, so use the non persistent
126 * overlay st_ino for directories.
127 */
128 stat->dev = dentry->d_sb->s_dev;
129 stat->ino = dentry->d_inode->i_ino;
Amir Goldstein51486262018-03-28 20:22:41 +0300130 } else if (lower_layer && lower_layer->fsid) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200131 /*
132 * For non-samefs setup, if we cannot map all layers st_ino
133 * to a unified address space, we need to make sure that st_dev
Amir Goldstein51486262018-03-28 20:22:41 +0300134 * is unique per lower fs. Upper layer uses real st_dev and
135 * lower layers use the unique anonymous bdev assigned to the
136 * lower fs.
Amir Goldsteinda309e82017-11-08 19:39:51 +0200137 */
Amir Goldstein51486262018-03-28 20:22:41 +0300138 stat->dev = lower_layer->fs->pseudo_dev;
Amir Goldsteinda309e82017-11-08 19:39:51 +0200139 }
140
141 return 0;
142}
143
Miklos Szeredi5b712092017-05-05 11:38:58 +0200144int ovl_getattr(const struct path *path, struct kstat *stat,
145 u32 request_mask, unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200146{
David Howellsa528d352017-01-31 16:46:22 +0000147 struct dentry *dentry = path->dentry;
Amir Goldstein72b608f2017-04-24 00:25:49 +0300148 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200149 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400150 const struct cred *old_cred;
Miklos Szeredi5b712092017-05-05 11:38:58 +0200151 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
Amir Goldsteina0c5ad32017-11-01 00:45:40 +0200152 bool samefs = ovl_same_sb(dentry->d_sb);
Amir Goldsteinda309e82017-11-08 19:39:51 +0200153 struct ovl_layer *lower_layer = NULL;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400154 int err;
Vivek Goyal67d756c22018-05-11 11:49:30 -0400155 bool metacopy_blocks = false;
156
157 metacopy_blocks = ovl_is_metacopy_dentry(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200158
Amir Goldstein72b608f2017-04-24 00:25:49 +0300159 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400160 old_cred = ovl_override_creds(dentry->d_sb);
David Howellsa528d352017-01-31 16:46:22 +0000161 err = vfs_getattr(&realpath, stat, request_mask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300162 if (err)
163 goto out;
164
165 /*
Amir Goldsteinda309e82017-11-08 19:39:51 +0200166 * For non-dir or same fs, we use st_ino of the copy up origin.
167 * This guaranties constant st_dev/st_ino across copy up.
Amir Goldsteine487d882017-11-07 13:55:04 +0200168 * With xino feature and non-samefs, we use st_ino of the copy up
169 * origin masked with high bits that represent the layer id.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300170 *
Amir Goldsteinda309e82017-11-08 19:39:51 +0200171 * If lower filesystem supports NFS file handles, this also guaranties
Amir Goldstein72b608f2017-04-24 00:25:49 +0300172 * persistent st_ino across mount cycle.
173 */
Amir Goldsteine487d882017-11-07 13:55:04 +0200174 if (!is_dir || samefs || ovl_xino_bits(dentry->d_sb)) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200175 if (!OVL_TYPE_UPPER(type)) {
176 lower_layer = ovl_layer_lower(dentry);
177 } else if (OVL_TYPE_ORIGIN(type)) {
Amir Goldstein72b608f2017-04-24 00:25:49 +0300178 struct kstat lowerstat;
Vivek Goyal67d756c22018-05-11 11:49:30 -0400179 u32 lowermask = STATX_INO | STATX_BLOCKS |
180 (!is_dir ? STATX_NLINK : 0);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300181
182 ovl_path_lower(dentry, &realpath);
183 err = vfs_getattr(&realpath, &lowerstat,
Miklos Szeredi5b712092017-05-05 11:38:58 +0200184 lowermask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300185 if (err)
186 goto out;
187
Amir Goldstein72b608f2017-04-24 00:25:49 +0300188 /*
Amir Goldstein359f3922017-06-21 15:28:41 +0300189 * Lower hardlinks may be broken on copy up to different
Amir Goldstein72b608f2017-04-24 00:25:49 +0300190 * upper files, so we cannot use the lower origin st_ino
191 * for those different files, even for the same fs case.
Amir Goldstein86eaa132017-11-21 13:55:51 +0200192 *
193 * Similarly, several redirected dirs can point to the
194 * same dir on a lower layer. With the "verify_lower"
195 * feature, we do not use the lower origin st_ino, if
196 * we haven't verified that this redirect is unique.
197 *
Amir Goldstein359f3922017-06-21 15:28:41 +0300198 * With inodes index enabled, it is safe to use st_ino
Amir Goldstein86eaa132017-11-21 13:55:51 +0200199 * of an indexed origin. The index validates that the
200 * upper hardlink is not broken and that a redirected
201 * dir is the only redirect to that origin.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300202 */
Amir Goldstein86eaa132017-11-21 13:55:51 +0200203 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
204 (!ovl_verify_lower(dentry->d_sb) &&
Amir Goldstein9f99e502018-04-11 20:09:29 +0300205 (is_dir || lowerstat.nlink == 1))) {
Amir Goldstein72b608f2017-04-24 00:25:49 +0300206 stat->ino = lowerstat.ino;
Amir Goldsteinda309e82017-11-08 19:39:51 +0200207 lower_layer = ovl_layer_lower(dentry);
Amir Goldstein9f99e502018-04-11 20:09:29 +0300208 }
Vivek Goyal67d756c22018-05-11 11:49:30 -0400209
210 /*
211 * If we are querying a metacopy dentry and lower
212 * dentry is data dentry, then use the blocks we
213 * queried just now. We don't have to do additional
214 * vfs_getattr(). If lower itself is metacopy, then
215 * additional vfs_getattr() is unavoidable.
216 */
217 if (metacopy_blocks &&
218 realpath.dentry == ovl_dentry_lowerdata(dentry)) {
219 stat->blocks = lowerstat.blocks;
220 metacopy_blocks = false;
221 }
222 }
223
224 if (metacopy_blocks) {
225 /*
226 * If lower is not same as lowerdata or if there was
227 * no origin on upper, we can end up here.
228 */
229 struct kstat lowerdatastat;
230 u32 lowermask = STATX_BLOCKS;
231
232 ovl_path_lowerdata(dentry, &realpath);
233 err = vfs_getattr(&realpath, &lowerdatastat,
234 lowermask, flags);
235 if (err)
236 goto out;
237 stat->blocks = lowerdatastat.blocks;
Amir Goldstein72b608f2017-04-24 00:25:49 +0300238 }
Amir Goldstein72b608f2017-04-24 00:25:49 +0300239 }
Miklos Szeredi5b712092017-05-05 11:38:58 +0200240
Amir Goldsteinda309e82017-11-08 19:39:51 +0200241 err = ovl_map_dev_ino(dentry, stat, lower_layer);
242 if (err)
243 goto out;
244
Miklos Szeredi5b712092017-05-05 11:38:58 +0200245 /*
246 * It's probably not worth it to count subdirs to get the
247 * correct link count. nlink=1 seems to pacify 'find' and
248 * other utilities.
249 */
250 if (is_dir && OVL_TYPE_MERGE(type))
251 stat->nlink = 1;
252
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300253 /*
254 * Return the overlay inode nlinks for indexed upper inodes.
255 * Overlay inode nlink counts the union of the upper hardlinks
256 * and non-covered lower hardlinks. It does not include the upper
257 * index hardlink.
258 */
259 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
260 stat->nlink = dentry->d_inode->i_nlink;
261
Amir Goldstein72b608f2017-04-24 00:25:49 +0300262out:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400263 revert_creds(old_cred);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300264
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400265 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200266}
267
268int ovl_permission(struct inode *inode, int mask)
269{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200270 struct inode *upperinode = ovl_inode_upper(inode);
271 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400272 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200273 int err;
274
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200275 /* Careful in RCU walk mode */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200276 if (!realinode) {
277 WARN_ON(!(mask & MAY_NOT_BLOCK));
Miklos Szeredia999d7e2016-07-29 12:05:23 +0200278 return -ECHILD;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200279 }
280
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400281 /*
282 * Check overlay inode with the creds of task and underlying inode
283 * with creds of mounter
284 */
285 err = generic_permission(inode, mask);
286 if (err)
287 return err;
288
289 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200290 if (!upperinode &&
291 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
Vivek Goyal754f8cb2016-07-01 16:34:29 -0400292 mask &= ~(MAY_WRITE | MAY_APPEND);
Vivek Goyal500cac32016-07-13 11:00:14 -0400293 /* Make sure mounter can read file for copy up later */
294 mask |= MAY_READ;
295 }
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200296 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400297 revert_creds(old_cred);
298
299 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200300}
301
Al Viro6b255392015-11-17 10:20:54 -0500302static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500303 struct inode *inode,
304 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200305{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400306 const struct cred *old_cred;
307 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200308
Al Viro6b255392015-11-17 10:20:54 -0500309 if (!dentry)
310 return ERR_PTR(-ECHILD);
311
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400312 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200313 p = vfs_get_link(ovl_dentry_real(dentry), done);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400314 revert_creds(old_cred);
315 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200316}
317
Miklos Szeredi09562542016-08-08 15:08:49 +0200318bool ovl_is_private_xattr(const char *name)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200319{
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +0200320 return strncmp(name, OVL_XATTR_PREFIX,
321 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200322}
323
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200324int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
325 const void *value, size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200326{
327 int err;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200328 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
329 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400330 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200331
332 err = ovl_want_write(dentry);
333 if (err)
334 goto out;
335
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200336 if (!value && !upperdentry) {
337 err = vfs_getxattr(realdentry, name, NULL, 0);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200338 if (err < 0)
339 goto out_drop_write;
340 }
341
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200342 if (!upperdentry) {
343 err = ovl_copy_up(dentry);
344 if (err)
345 goto out_drop_write;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200346
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200347 realdentry = ovl_dentry_upper(dentry);
348 }
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200349
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400350 old_cred = ovl_override_creds(dentry->d_sb);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200351 if (value)
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200352 err = vfs_setxattr(realdentry, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200353 else {
354 WARN_ON(flags != XATTR_REPLACE);
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200355 err = vfs_removexattr(realdentry, name);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200356 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400357 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200358
Miklos Szeredid9854c82018-07-18 15:44:40 +0200359 /* copy c/mtime */
360 ovl_copyattr(d_inode(realdentry), inode);
361
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200362out_drop_write:
363 ovl_drop_write(dentry);
364out:
365 return err;
366}
367
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200368int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200369 void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200370{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400371 ssize_t res;
372 const struct cred *old_cred;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200373 struct dentry *realdentry =
374 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
Miklos Szeredi52148462014-11-20 16:40:00 +0100375
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400376 old_cred = ovl_override_creds(dentry->d_sb);
377 res = vfs_getxattr(realdentry, name, value, size);
378 revert_creds(old_cred);
379 return res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200380}
381
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200382static bool ovl_can_list(const char *s)
383{
384 /* List all non-trusted xatts */
385 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
386 return true;
387
388 /* Never list trusted.overlay, list other trusted for superuser only */
389 return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
390}
391
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200392ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
393{
Miklos Szeredib5817552016-06-06 16:21:37 +0200394 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200395 ssize_t res;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200396 size_t len;
397 char *s;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400398 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200399
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400400 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200401 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400402 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200403 if (res <= 0 || size == 0)
404 return res;
405
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200406 /* filter out private xattrs */
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200407 for (s = list, len = res; len;) {
408 size_t slen = strnlen(s, len) + 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200409
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200410 /* underlying fs providing us with an broken xattr list? */
411 if (WARN_ON(slen > len))
412 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200413
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200414 len -= slen;
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200415 if (!ovl_can_list(s)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200416 res -= slen;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200417 memmove(s, s + slen, len);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200418 } else {
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200419 s += slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200420 }
421 }
422
423 return res;
424}
425
Vivek Goyal39a25b22016-07-01 16:34:26 -0400426struct posix_acl *ovl_get_acl(struct inode *inode, int type)
427{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200428 struct inode *realinode = ovl_inode_real(inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400429 const struct cred *old_cred;
430 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400431
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200432 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
Vivek Goyal39a25b22016-07-01 16:34:26 -0400433 return NULL;
434
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400435 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200436 acl = get_acl(realinode, type);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400437 revert_creds(old_cred);
438
439 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400440}
441
Deepa Dinamani95582b02018-05-08 19:36:02 -0700442int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200443{
Miklos Szeredi8f35cf52018-04-12 12:04:50 +0200444 if (flags & S_ATIME) {
445 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
446 struct path upperpath = {
447 .mnt = ofs->upper_mnt,
448 .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
449 };
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200450
Miklos Szeredi8f35cf52018-04-12 12:04:50 +0200451 if (upperpath.dentry) {
452 touch_atime(&upperpath);
453 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
454 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200455 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200456 return 0;
457}
458
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200459static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
460 u64 start, u64 len)
461{
462 int err;
463 struct inode *realinode = ovl_inode_real(inode);
464 const struct cred *old_cred;
465
466 if (!realinode->i_op->fiemap)
467 return -EOPNOTSUPP;
468
469 old_cred = ovl_override_creds(inode->i_sb);
470 err = realinode->i_op->fiemap(realinode, fieinfo, start, len);
471 revert_creds(old_cred);
472
473 return err;
474}
475
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200476static const struct inode_operations ovl_file_inode_operations = {
477 .setattr = ovl_setattr,
478 .permission = ovl_permission,
479 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200480 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400481 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200482 .update_time = ovl_update_time,
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200483 .fiemap = ovl_fiemap,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200484};
485
486static const struct inode_operations ovl_symlink_inode_operations = {
487 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500488 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200489 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200490 .listxattr = ovl_listxattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200491 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200492};
493
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200494static const struct inode_operations ovl_special_inode_operations = {
495 .setattr = ovl_setattr,
496 .permission = ovl_permission,
497 .getattr = ovl_getattr,
498 .listxattr = ovl_listxattr,
499 .get_acl = ovl_get_acl,
500 .update_time = ovl_update_time,
501};
502
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200503/*
504 * It is possible to stack overlayfs instance on top of another
505 * overlayfs instance as lower layer. We need to annonate the
506 * stackable i_mutex locks according to stack level of the super
507 * block instance. An overlayfs instance can never be in stack
508 * depth 0 (there is always a real fs below it). An overlayfs
509 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
510 *
511 * For example, here is a snip from /proc/lockdep_chains after
512 * dir_iterate of nested overlayfs:
513 *
514 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
515 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
516 * [...] &type->i_mutex_dir_key (stack_depth=0)
517 */
518#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
519
520static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
521{
522#ifdef CONFIG_LOCKDEP
523 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
524 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300525 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200526
527 int depth = inode->i_sb->s_stack_depth - 1;
528
529 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
530 depth = 0;
531
532 if (S_ISDIR(inode->i_mode))
533 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
534 else
535 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300536
537 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200538#endif
539}
540
Amir Goldstein695b46e2018-03-15 23:39:01 +0200541static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
Amir Goldstein12574a92018-03-16 10:39:37 +0200542 unsigned long ino, int fsid)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200543{
Amir Goldstein12574a92018-03-16 10:39:37 +0200544 int xinobits = ovl_xino_bits(inode->i_sb);
545
Amir Goldstein695b46e2018-03-15 23:39:01 +0200546 /*
547 * When NFS export is enabled and d_ino is consistent with st_ino
Amir Goldstein12574a92018-03-16 10:39:37 +0200548 * (samefs or i_ino has enough bits to encode layer), set the same
549 * value used for d_ino to i_ino, because nfsd readdirplus compares
550 * d_ino values to i_ino values of child entries. When called from
551 * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
Amir Goldstein695b46e2018-03-15 23:39:01 +0200552 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
553 */
Amir Goldstein12574a92018-03-16 10:39:37 +0200554 if (inode->i_sb->s_export_op &&
555 (ovl_same_sb(inode->i_sb) || xinobits)) {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200556 inode->i_ino = ino;
Amir Goldstein12574a92018-03-16 10:39:37 +0200557 if (xinobits && fsid && !(ino >> (64 - xinobits)))
558 inode->i_ino |= (unsigned long)fsid << (64 - xinobits);
559 } else {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200560 inode->i_ino = get_next_ino();
Amir Goldstein12574a92018-03-16 10:39:37 +0200561 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200562 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200563 inode->i_flags |= S_NOCMTIME;
Miklos Szeredi2a3a2a32016-09-01 11:11:59 +0200564#ifdef CONFIG_FS_POSIX_ACL
565 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
566#endif
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200567
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200568 ovl_lockdep_annotate_inode_mutex_key(inode);
569
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100570 switch (mode & S_IFMT) {
571 case S_IFREG:
572 inode->i_op = &ovl_file_inode_operations;
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200573 inode->i_fop = &ovl_file_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100574 break;
575
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200576 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200577 inode->i_op = &ovl_dir_inode_operations;
578 inode->i_fop = &ovl_dir_operations;
579 break;
580
581 case S_IFLNK:
582 inode->i_op = &ovl_symlink_inode_operations;
583 break;
584
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200585 default:
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200586 inode->i_op = &ovl_special_inode_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100587 init_special_inode(inode, mode, rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200588 break;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200589 }
590}
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200591
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300592/*
593 * With inodes index enabled, an overlay inode nlink counts the union of upper
594 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
595 * upper inode, the following nlink modifying operations can happen:
596 *
597 * 1. Lower hardlink copy up
598 * 2. Upper hardlink created, unlinked or renamed over
599 * 3. Lower hardlink whiteout or renamed over
600 *
601 * For the first, copy up case, the union nlink does not change, whether the
602 * operation succeeds or fails, but the upper inode nlink may change.
603 * Therefore, before copy up, we store the union nlink value relative to the
604 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
605 *
606 * For the second, upper hardlink case, the union nlink should be incremented
607 * or decremented IFF the operation succeeds, aligned with nlink change of the
608 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
609 * value relative to the upper inode nlink in the index inode.
610 *
611 * For the last, lower cover up case, we simplify things by preceding the
612 * whiteout or cover up with copy up. This makes sure that there is an index
613 * upper inode where the nlink xattr can be stored before the copied up upper
614 * entry is unlink.
615 */
616#define OVL_NLINK_ADD_UPPER (1 << 0)
617
618/*
619 * On-disk format for indexed nlink:
620 *
621 * nlink relative to the upper inode - "U[+-]NUM"
622 * nlink relative to the lower inode - "L[+-]NUM"
623 */
624
625static int ovl_set_nlink_common(struct dentry *dentry,
626 struct dentry *realdentry, const char *format)
627{
628 struct inode *inode = d_inode(dentry);
629 struct inode *realinode = d_inode(realdentry);
630 char buf[13];
631 int len;
632
633 len = snprintf(buf, sizeof(buf), format,
634 (int) (inode->i_nlink - realinode->i_nlink));
635
Miklos Szeredi67873412017-07-27 21:54:05 +0200636 if (WARN_ON(len >= sizeof(buf)))
637 return -EIO;
638
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300639 return ovl_do_setxattr(ovl_dentry_upper(dentry),
640 OVL_XATTR_NLINK, buf, len, 0);
641}
642
643int ovl_set_nlink_upper(struct dentry *dentry)
644{
645 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
646}
647
648int ovl_set_nlink_lower(struct dentry *dentry)
649{
650 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
651}
652
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300653unsigned int ovl_get_nlink(struct dentry *lowerdentry,
654 struct dentry *upperdentry,
655 unsigned int fallback)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300656{
657 int nlink_diff;
658 int nlink;
659 char buf[13];
660 int err;
661
662 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
663 return fallback;
664
665 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
666 if (err < 0)
667 goto fail;
668
669 buf[err] = '\0';
670 if ((buf[0] != 'L' && buf[0] != 'U') ||
671 (buf[1] != '+' && buf[1] != '-'))
672 goto fail;
673
674 err = kstrtoint(buf + 1, 10, &nlink_diff);
675 if (err < 0)
676 goto fail;
677
678 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
679 nlink += nlink_diff;
680
681 if (nlink <= 0)
682 goto fail;
683
684 return nlink;
685
686fail:
687 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
688 upperdentry, err);
689 return fallback;
690}
691
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100692struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200693{
694 struct inode *inode;
695
696 inode = new_inode(sb);
697 if (inode)
Amir Goldstein12574a92018-03-16 10:39:37 +0200698 ovl_fill_inode(inode, mode, rdev, 0, 0);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200699
700 return inode;
701}
702
703static int ovl_inode_test(struct inode *inode, void *data)
704{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200705 return inode->i_private == data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200706}
707
708static int ovl_inode_set(struct inode *inode, void *data)
709{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200710 inode->i_private = data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200711 return 0;
712}
713
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200714static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
Amir Goldstein4b91c302018-01-18 16:39:13 +0200715 struct dentry *upperdentry, bool strict)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200716{
Amir Goldstein4b91c302018-01-18 16:39:13 +0200717 /*
718 * For directories, @strict verify from lookup path performs consistency
719 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
720 * inode. Non @strict verify from NFS handle decode path passes NULL for
721 * 'unknown' lower/upper.
722 */
723 if (S_ISDIR(inode->i_mode) && strict) {
Amir Goldstein31747ed2018-01-14 18:35:40 +0200724 /* Real lower dir moved to upper layer under us? */
725 if (!lowerdentry && ovl_inode_lower(inode))
726 return false;
727
728 /* Lookup of an uncovered redirect origin? */
729 if (!upperdentry && ovl_inode_upper(inode))
730 return false;
731 }
732
Amir Goldstein939ae4e2017-09-11 16:30:15 +0300733 /*
734 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
735 * This happens when finding a copied up overlay inode for a renamed
736 * or hardlinked overlay dentry and lower dentry cannot be followed
737 * by origin because lower fs does not support file handles.
738 */
739 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200740 return false;
741
742 /*
743 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
744 * This happens when finding a lower alias for a copied up hard link.
745 */
746 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
747 return false;
748
749 return true;
750}
751
Amir Goldstein4b91c302018-01-18 16:39:13 +0200752struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
753 bool is_upper)
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200754{
Amir Goldstein4b91c302018-01-18 16:39:13 +0200755 struct inode *inode, *key = d_inode(real);
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200756
757 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
758 if (!inode)
759 return NULL;
760
Amir Goldstein4b91c302018-01-18 16:39:13 +0200761 if (!ovl_verify_inode(inode, is_upper ? NULL : real,
762 is_upper ? real : NULL, false)) {
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200763 iput(inode);
764 return ERR_PTR(-ESTALE);
765 }
766
767 return inode;
768}
769
Amir Goldstein764baba2018-02-04 15:35:09 +0200770/*
771 * Does overlay inode need to be hashed by lower inode?
772 */
773static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
774 struct dentry *lower, struct dentry *index)
775{
776 struct ovl_fs *ofs = sb->s_fs_info;
777
778 /* No, if pure upper */
779 if (!lower)
780 return false;
781
782 /* Yes, if already indexed */
783 if (index)
784 return true;
785
786 /* Yes, if won't be copied up */
787 if (!ofs->upper_mnt)
788 return true;
789
790 /* No, if lower hardlink is or will be broken on copy up */
791 if ((upper || !ovl_indexdir(sb)) &&
792 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
793 return false;
794
795 /* No, if non-indexed upper with NFS export */
796 if (sb->s_export_op && upper)
797 return false;
798
799 /* Otherwise, hash by lower inode for fsnotify */
800 return true;
801}
802
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300803static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
804 struct inode *key)
805{
806 return newinode ? inode_insert5(newinode, (unsigned long) key,
807 ovl_inode_test, ovl_inode_set, key) :
808 iget5_locked(sb, (unsigned long) key,
809 ovl_inode_test, ovl_inode_set, key);
810}
811
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400812struct inode *ovl_get_inode(struct super_block *sb,
813 struct ovl_inode_params *oip)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200814{
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400815 struct dentry *upperdentry = oip->upperdentry;
816 struct ovl_path *lowerpath = oip->lowerpath;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200817 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200818 struct inode *inode;
Amir Goldstein12574a92018-03-16 10:39:37 +0200819 struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400820 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
821 oip->index);
822 int fsid = bylower ? oip->lowerpath->layer->fsid : 0;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400823 bool is_dir, metacopy = false;
Amir Goldstein695b46e2018-03-15 23:39:01 +0200824 unsigned long ino = 0;
Vivek Goyal027065b2018-05-11 11:49:28 -0400825 int err = -ENOMEM;
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300826
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200827 if (!realinode)
828 realinode = d_inode(lowerdentry);
829
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300830 /*
Amir Goldstein764baba2018-02-04 15:35:09 +0200831 * Copy up origin (lower) may exist for non-indexed upper, but we must
832 * not use lower as hash key if this is a broken hardlink.
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300833 */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200834 is_dir = S_ISDIR(realinode->i_mode);
Amir Goldstein764baba2018-02-04 15:35:09 +0200835 if (upperdentry || bylower) {
836 struct inode *key = d_inode(bylower ? lowerdentry :
837 upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200838 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200839
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300840 inode = ovl_iget5(sb, oip->newinode, key);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200841 if (!inode)
Vivek Goyal027065b2018-05-11 11:49:28 -0400842 goto out_err;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200843 if (!(inode->i_state & I_NEW)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200844 /*
845 * Verify that the underlying files stored in the inode
846 * match those in the dentry.
847 */
Amir Goldstein4b91c302018-01-18 16:39:13 +0200848 if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
849 true)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200850 iput(inode);
Vivek Goyal027065b2018-05-11 11:49:28 -0400851 err = -ESTALE;
852 goto out_err;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200853 }
854
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200855 dput(upperdentry);
Vivek Goyal9cec54c2018-05-11 11:49:27 -0400856 kfree(oip->redirect);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200857 goto out;
858 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200859
Amir Goldstein31747ed2018-01-14 18:35:40 +0200860 /* Recalculate nlink for non-dir due to indexing */
861 if (!is_dir)
862 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300863 set_nlink(inode, nlink);
Amir Goldstein695b46e2018-03-15 23:39:01 +0200864 ino = key->i_ino;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200865 } else {
Amir Goldstein764baba2018-02-04 15:35:09 +0200866 /* Lower hardlink that will be broken on copy up */
Amir Goldstein0aceb532017-12-12 23:43:16 +0200867 inode = new_inode(sb);
Vivek Goyal027065b2018-05-11 11:49:28 -0400868 if (!inode) {
869 err = -ENOMEM;
870 goto out_err;
871 }
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200872 }
Amir Goldstein12574a92018-03-16 10:39:37 +0200873 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid);
Vivek Goyal2664bd02018-05-11 11:49:30 -0400874 ovl_inode_init(inode, upperdentry, lowerdentry, oip->lowerdata);
Miklos Szeredi13c72072017-07-04 22:03:16 +0200875
876 if (upperdentry && ovl_is_impuredir(upperdentry))
877 ovl_set_flag(OVL_IMPURE, inode);
878
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400879 if (oip->index)
Vivek Goyal0471a9c2018-03-20 16:35:40 -0400880 ovl_set_flag(OVL_INDEX, inode);
881
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400882 if (upperdentry) {
883 err = ovl_check_metacopy_xattr(upperdentry);
884 if (err < 0)
885 goto out_err;
886 metacopy = err;
887 if (!metacopy)
888 ovl_set_flag(OVL_UPPERDATA, inode);
889 }
890
Vivek Goyal9cec54c2018-05-11 11:49:27 -0400891 OVL_I(inode)->redirect = oip->redirect;
892
Vivek Goyala00c2d52018-05-11 11:49:32 -0400893 if (bylower)
894 ovl_set_flag(OVL_CONST_INO, inode);
895
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300896 /* Check for non-merge dir that may have whiteouts */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200897 if (is_dir) {
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400898 if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300899 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
900 ovl_set_flag(OVL_WHITEOUTS, inode);
901 }
902 }
903
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200904 if (inode->i_state & I_NEW)
905 unlock_new_inode(inode);
906out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200907 return inode;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200908
Vivek Goyal027065b2018-05-11 11:49:28 -0400909out_err:
910 inode = ERR_PTR(err);
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200911 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200912}