blob: 28ee802e6eaf5929b48bc767703f350fda4e3f2e [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;
22 struct dentry *upperdentry;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040023 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020024
Jan Kara31051c82016-05-26 16:55:18 +020025 err = setattr_prepare(dentry, attr);
Miklos Szeredicf9a6782015-12-11 16:30:49 +010026 if (err)
27 return err;
28
Miklos Szeredie9be9d52014-10-24 00:14:38 +020029 err = ovl_want_write(dentry);
30 if (err)
31 goto out;
32
Miklos Szeredi58121602018-07-18 15:44:41 +020033 if (attr->ia_valid & ATTR_SIZE) {
34 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
35
36 err = -ETXTBSY;
37 if (atomic_read(&realinode->i_writecount) < 0)
38 goto out_drop_write;
39 }
40
Miklos Szerediacff81e2015-12-04 19:18:48 +010041 err = ovl_copy_up(dentry);
42 if (!err) {
Miklos Szeredi58121602018-07-18 15:44:41 +020043 struct inode *winode = NULL;
44
Miklos Szerediacff81e2015-12-04 19:18:48 +010045 upperdentry = ovl_dentry_upper(dentry);
46
Miklos Szeredi58121602018-07-18 15:44:41 +020047 if (attr->ia_valid & ATTR_SIZE) {
48 winode = d_inode(upperdentry);
49 err = get_write_access(winode);
50 if (err)
51 goto out_drop_write;
52 }
53
Miklos Szeredib99c2d92016-07-04 16:49:48 +020054 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
55 attr->ia_valid &= ~ATTR_MODE;
56
Al Viro59551022016-01-22 15:40:57 -050057 inode_lock(upperdentry->d_inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040058 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020059 err = notify_change(upperdentry, attr, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040060 revert_creds(old_cred);
Konstantin Khlebnikovb81de062016-01-31 16:21:29 +030061 if (!err)
62 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Al Viro59551022016-01-22 15:40:57 -050063 inode_unlock(upperdentry->d_inode);
Miklos Szeredi58121602018-07-18 15:44:41 +020064
65 if (winode)
66 put_write_access(winode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020067 }
Miklos Szeredi58121602018-07-18 15:44:41 +020068out_drop_write:
Miklos Szeredie9be9d52014-10-24 00:14:38 +020069 ovl_drop_write(dentry);
70out:
71 return err;
72}
73
Amir Goldsteinda309e82017-11-08 19:39:51 +020074static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat,
75 struct ovl_layer *lower_layer)
76{
77 bool samefs = ovl_same_sb(dentry->d_sb);
Amir Goldsteine487d882017-11-07 13:55:04 +020078 unsigned int xinobits = ovl_xino_bits(dentry->d_sb);
Amir Goldsteinda309e82017-11-08 19:39:51 +020079
80 if (samefs) {
81 /*
82 * When all layers are on the same fs, all real inode
83 * number are unique, so we use the overlay st_dev,
84 * which is friendly to du -x.
85 */
86 stat->dev = dentry->d_sb->s_dev;
Amir Goldsteine487d882017-11-07 13:55:04 +020087 return 0;
88 } else if (xinobits) {
89 unsigned int shift = 64 - xinobits;
90 /*
91 * All inode numbers of underlying fs should not be using the
92 * high xinobits, so we use high xinobits to partition the
93 * overlay st_ino address space. The high bits holds the fsid
94 * (upper fsid is 0). This way overlay inode numbers are unique
95 * and all inodes use overlay st_dev. Inode numbers are also
96 * persistent for a given layer configuration.
97 */
98 if (stat->ino >> shift) {
99 pr_warn_ratelimited("overlayfs: inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
100 dentry, stat->ino, xinobits);
101 } else {
102 if (lower_layer)
103 stat->ino |= ((u64)lower_layer->fsid) << shift;
104
105 stat->dev = dentry->d_sb->s_dev;
106 return 0;
107 }
108 }
109
110 /* The inode could not be mapped to a unified st_ino address space */
111 if (S_ISDIR(dentry->d_inode->i_mode)) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200112 /*
113 * Always use the overlay st_dev for directories, so 'find
114 * -xdev' will scan the entire overlay mount and won't cross the
115 * overlay mount boundaries.
116 *
117 * If not all layers are on the same fs the pair {real st_ino;
118 * overlay st_dev} is not unique, so use the non persistent
119 * overlay st_ino for directories.
120 */
121 stat->dev = dentry->d_sb->s_dev;
122 stat->ino = dentry->d_inode->i_ino;
Amir Goldstein51486262018-03-28 20:22:41 +0300123 } else if (lower_layer && lower_layer->fsid) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200124 /*
125 * For non-samefs setup, if we cannot map all layers st_ino
126 * to a unified address space, we need to make sure that st_dev
Amir Goldstein51486262018-03-28 20:22:41 +0300127 * is unique per lower fs. Upper layer uses real st_dev and
128 * lower layers use the unique anonymous bdev assigned to the
129 * lower fs.
Amir Goldsteinda309e82017-11-08 19:39:51 +0200130 */
Amir Goldstein51486262018-03-28 20:22:41 +0300131 stat->dev = lower_layer->fs->pseudo_dev;
Amir Goldsteinda309e82017-11-08 19:39:51 +0200132 }
133
134 return 0;
135}
136
Miklos Szeredi5b712092017-05-05 11:38:58 +0200137int ovl_getattr(const struct path *path, struct kstat *stat,
138 u32 request_mask, unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200139{
David Howellsa528d352017-01-31 16:46:22 +0000140 struct dentry *dentry = path->dentry;
Amir Goldstein72b608f2017-04-24 00:25:49 +0300141 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200142 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400143 const struct cred *old_cred;
Miklos Szeredi5b712092017-05-05 11:38:58 +0200144 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
Amir Goldsteina0c5ad32017-11-01 00:45:40 +0200145 bool samefs = ovl_same_sb(dentry->d_sb);
Amir Goldsteinda309e82017-11-08 19:39:51 +0200146 struct ovl_layer *lower_layer = NULL;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400147 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200148
Amir Goldstein72b608f2017-04-24 00:25:49 +0300149 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400150 old_cred = ovl_override_creds(dentry->d_sb);
David Howellsa528d352017-01-31 16:46:22 +0000151 err = vfs_getattr(&realpath, stat, request_mask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300152 if (err)
153 goto out;
154
155 /*
Amir Goldsteinda309e82017-11-08 19:39:51 +0200156 * For non-dir or same fs, we use st_ino of the copy up origin.
157 * This guaranties constant st_dev/st_ino across copy up.
Amir Goldsteine487d882017-11-07 13:55:04 +0200158 * With xino feature and non-samefs, we use st_ino of the copy up
159 * origin masked with high bits that represent the layer id.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300160 *
Amir Goldsteinda309e82017-11-08 19:39:51 +0200161 * If lower filesystem supports NFS file handles, this also guaranties
Amir Goldstein72b608f2017-04-24 00:25:49 +0300162 * persistent st_ino across mount cycle.
163 */
Amir Goldsteine487d882017-11-07 13:55:04 +0200164 if (!is_dir || samefs || ovl_xino_bits(dentry->d_sb)) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200165 if (!OVL_TYPE_UPPER(type)) {
166 lower_layer = ovl_layer_lower(dentry);
167 } else if (OVL_TYPE_ORIGIN(type)) {
Amir Goldstein72b608f2017-04-24 00:25:49 +0300168 struct kstat lowerstat;
Miklos Szeredi5b712092017-05-05 11:38:58 +0200169 u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300170
171 ovl_path_lower(dentry, &realpath);
172 err = vfs_getattr(&realpath, &lowerstat,
Miklos Szeredi5b712092017-05-05 11:38:58 +0200173 lowermask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300174 if (err)
175 goto out;
176
Amir Goldstein72b608f2017-04-24 00:25:49 +0300177 /*
Amir Goldstein359f3922017-06-21 15:28:41 +0300178 * Lower hardlinks may be broken on copy up to different
Amir Goldstein72b608f2017-04-24 00:25:49 +0300179 * upper files, so we cannot use the lower origin st_ino
180 * for those different files, even for the same fs case.
Amir Goldstein86eaa132017-11-21 13:55:51 +0200181 *
182 * Similarly, several redirected dirs can point to the
183 * same dir on a lower layer. With the "verify_lower"
184 * feature, we do not use the lower origin st_ino, if
185 * we haven't verified that this redirect is unique.
186 *
Amir Goldstein359f3922017-06-21 15:28:41 +0300187 * With inodes index enabled, it is safe to use st_ino
Amir Goldstein86eaa132017-11-21 13:55:51 +0200188 * of an indexed origin. The index validates that the
189 * upper hardlink is not broken and that a redirected
190 * dir is the only redirect to that origin.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300191 */
Amir Goldstein86eaa132017-11-21 13:55:51 +0200192 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
193 (!ovl_verify_lower(dentry->d_sb) &&
Amir Goldstein9f99e502018-04-11 20:09:29 +0300194 (is_dir || lowerstat.nlink == 1))) {
Amir Goldstein72b608f2017-04-24 00:25:49 +0300195 stat->ino = lowerstat.ino;
Amir Goldsteinda309e82017-11-08 19:39:51 +0200196 lower_layer = ovl_layer_lower(dentry);
Amir Goldstein9f99e502018-04-11 20:09:29 +0300197 }
Amir Goldstein72b608f2017-04-24 00:25:49 +0300198 }
Amir Goldstein72b608f2017-04-24 00:25:49 +0300199 }
Miklos Szeredi5b712092017-05-05 11:38:58 +0200200
Amir Goldsteinda309e82017-11-08 19:39:51 +0200201 err = ovl_map_dev_ino(dentry, stat, lower_layer);
202 if (err)
203 goto out;
204
Miklos Szeredi5b712092017-05-05 11:38:58 +0200205 /*
206 * It's probably not worth it to count subdirs to get the
207 * correct link count. nlink=1 seems to pacify 'find' and
208 * other utilities.
209 */
210 if (is_dir && OVL_TYPE_MERGE(type))
211 stat->nlink = 1;
212
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300213 /*
214 * Return the overlay inode nlinks for indexed upper inodes.
215 * Overlay inode nlink counts the union of the upper hardlinks
216 * and non-covered lower hardlinks. It does not include the upper
217 * index hardlink.
218 */
219 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
220 stat->nlink = dentry->d_inode->i_nlink;
221
Amir Goldstein72b608f2017-04-24 00:25:49 +0300222out:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400223 revert_creds(old_cred);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300224
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400225 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200226}
227
228int ovl_permission(struct inode *inode, int mask)
229{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200230 struct inode *upperinode = ovl_inode_upper(inode);
231 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400232 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200233 int err;
234
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200235 /* Careful in RCU walk mode */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200236 if (!realinode) {
237 WARN_ON(!(mask & MAY_NOT_BLOCK));
Miklos Szeredia999d7e2016-07-29 12:05:23 +0200238 return -ECHILD;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200239 }
240
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400241 /*
242 * Check overlay inode with the creds of task and underlying inode
243 * with creds of mounter
244 */
245 err = generic_permission(inode, mask);
246 if (err)
247 return err;
248
249 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200250 if (!upperinode &&
251 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
Vivek Goyal754f8cb2016-07-01 16:34:29 -0400252 mask &= ~(MAY_WRITE | MAY_APPEND);
Vivek Goyal500cac32016-07-13 11:00:14 -0400253 /* Make sure mounter can read file for copy up later */
254 mask |= MAY_READ;
255 }
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200256 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400257 revert_creds(old_cred);
258
259 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200260}
261
Al Viro6b255392015-11-17 10:20:54 -0500262static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500263 struct inode *inode,
264 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200265{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400266 const struct cred *old_cred;
267 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200268
Al Viro6b255392015-11-17 10:20:54 -0500269 if (!dentry)
270 return ERR_PTR(-ECHILD);
271
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400272 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200273 p = vfs_get_link(ovl_dentry_real(dentry), done);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400274 revert_creds(old_cred);
275 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200276}
277
Miklos Szeredi09562542016-08-08 15:08:49 +0200278bool ovl_is_private_xattr(const char *name)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200279{
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +0200280 return strncmp(name, OVL_XATTR_PREFIX,
281 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200282}
283
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200284int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
285 const void *value, size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200286{
287 int err;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200288 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
289 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400290 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200291
292 err = ovl_want_write(dentry);
293 if (err)
294 goto out;
295
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200296 if (!value && !upperdentry) {
297 err = vfs_getxattr(realdentry, name, NULL, 0);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200298 if (err < 0)
299 goto out_drop_write;
300 }
301
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200302 if (!upperdentry) {
303 err = ovl_copy_up(dentry);
304 if (err)
305 goto out_drop_write;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200306
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200307 realdentry = ovl_dentry_upper(dentry);
308 }
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200309
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400310 old_cred = ovl_override_creds(dentry->d_sb);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200311 if (value)
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200312 err = vfs_setxattr(realdentry, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200313 else {
314 WARN_ON(flags != XATTR_REPLACE);
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200315 err = vfs_removexattr(realdentry, name);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200316 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400317 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200318
Miklos Szeredid9854c82018-07-18 15:44:40 +0200319 /* copy c/mtime */
320 ovl_copyattr(d_inode(realdentry), inode);
321
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200322out_drop_write:
323 ovl_drop_write(dentry);
324out:
325 return err;
326}
327
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200328int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200329 void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200330{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400331 ssize_t res;
332 const struct cred *old_cred;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200333 struct dentry *realdentry =
334 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
Miklos Szeredi52148462014-11-20 16:40:00 +0100335
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400336 old_cred = ovl_override_creds(dentry->d_sb);
337 res = vfs_getxattr(realdentry, name, value, size);
338 revert_creds(old_cred);
339 return res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200340}
341
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200342static bool ovl_can_list(const char *s)
343{
344 /* List all non-trusted xatts */
345 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
346 return true;
347
348 /* Never list trusted.overlay, list other trusted for superuser only */
349 return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
350}
351
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200352ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
353{
Miklos Szeredib5817552016-06-06 16:21:37 +0200354 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200355 ssize_t res;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200356 size_t len;
357 char *s;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400358 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200359
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400360 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200361 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400362 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200363 if (res <= 0 || size == 0)
364 return res;
365
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200366 /* filter out private xattrs */
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200367 for (s = list, len = res; len;) {
368 size_t slen = strnlen(s, len) + 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200369
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200370 /* underlying fs providing us with an broken xattr list? */
371 if (WARN_ON(slen > len))
372 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200373
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200374 len -= slen;
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200375 if (!ovl_can_list(s)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200376 res -= slen;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200377 memmove(s, s + slen, len);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200378 } else {
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200379 s += slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200380 }
381 }
382
383 return res;
384}
385
Vivek Goyal39a25b22016-07-01 16:34:26 -0400386struct posix_acl *ovl_get_acl(struct inode *inode, int type)
387{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200388 struct inode *realinode = ovl_inode_real(inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400389 const struct cred *old_cred;
390 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400391
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200392 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
Vivek Goyal39a25b22016-07-01 16:34:26 -0400393 return NULL;
394
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400395 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200396 acl = get_acl(realinode, type);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400397 revert_creds(old_cred);
398
399 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400400}
401
Amir Goldstein59be0972017-06-20 15:25:46 +0300402static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200403{
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300404 /* Copy up of disconnected dentry does not set upper alias */
Amir Goldstein59be0972017-06-20 15:25:46 +0300405 if (ovl_dentry_upper(dentry) &&
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300406 (ovl_dentry_has_upper_alias(dentry) ||
407 (dentry->d_flags & DCACHE_DISCONNECTED)))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200408 return false;
409
Amir Goldstein59be0972017-06-20 15:25:46 +0300410 if (special_file(d_inode(dentry)->i_mode))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200411 return false;
412
413 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
414 return false;
415
416 return true;
417}
418
Miklos Szeredi2d902672016-06-30 08:53:27 +0200419int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200420{
Miklos Szeredi2d902672016-06-30 08:53:27 +0200421 int err = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200422
Amir Goldstein59be0972017-06-20 15:25:46 +0300423 if (ovl_open_need_copy_up(dentry, file_flags)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200424 err = ovl_want_write(dentry);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200425 if (!err) {
Amir Goldstein9aba6522016-11-12 21:36:03 +0200426 err = ovl_copy_up_flags(dentry, file_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200427 ovl_drop_write(dentry);
428 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200429 }
430
Miklos Szeredi2d902672016-06-30 08:53:27 +0200431 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200432}
433
Deepa Dinamani95582b02018-05-08 19:36:02 -0700434int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200435{
Miklos Szeredi8f35cf52018-04-12 12:04:50 +0200436 if (flags & S_ATIME) {
437 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
438 struct path upperpath = {
439 .mnt = ofs->upper_mnt,
440 .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
441 };
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200442
Miklos Szeredi8f35cf52018-04-12 12:04:50 +0200443 if (upperpath.dentry) {
444 touch_atime(&upperpath);
445 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
446 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200447 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200448 return 0;
449}
450
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200451static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
452 u64 start, u64 len)
453{
454 int err;
455 struct inode *realinode = ovl_inode_real(inode);
456 const struct cred *old_cred;
457
458 if (!realinode->i_op->fiemap)
459 return -EOPNOTSUPP;
460
461 old_cred = ovl_override_creds(inode->i_sb);
462 err = realinode->i_op->fiemap(realinode, fieinfo, start, len);
463 revert_creds(old_cred);
464
465 return err;
466}
467
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200468static const struct inode_operations ovl_file_inode_operations = {
469 .setattr = ovl_setattr,
470 .permission = ovl_permission,
471 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200472 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400473 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200474 .update_time = ovl_update_time,
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200475 .fiemap = ovl_fiemap,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200476};
477
478static const struct inode_operations ovl_symlink_inode_operations = {
479 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500480 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200481 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200482 .listxattr = ovl_listxattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200483 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200484};
485
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200486static const struct inode_operations ovl_special_inode_operations = {
487 .setattr = ovl_setattr,
488 .permission = ovl_permission,
489 .getattr = ovl_getattr,
490 .listxattr = ovl_listxattr,
491 .get_acl = ovl_get_acl,
492 .update_time = ovl_update_time,
493};
494
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200495/*
496 * It is possible to stack overlayfs instance on top of another
497 * overlayfs instance as lower layer. We need to annonate the
498 * stackable i_mutex locks according to stack level of the super
499 * block instance. An overlayfs instance can never be in stack
500 * depth 0 (there is always a real fs below it). An overlayfs
501 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
502 *
503 * For example, here is a snip from /proc/lockdep_chains after
504 * dir_iterate of nested overlayfs:
505 *
506 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
507 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
508 * [...] &type->i_mutex_dir_key (stack_depth=0)
509 */
510#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
511
512static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
513{
514#ifdef CONFIG_LOCKDEP
515 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
516 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300517 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200518
519 int depth = inode->i_sb->s_stack_depth - 1;
520
521 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
522 depth = 0;
523
524 if (S_ISDIR(inode->i_mode))
525 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
526 else
527 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300528
529 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200530#endif
531}
532
Amir Goldstein695b46e2018-03-15 23:39:01 +0200533static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
Amir Goldstein12574a92018-03-16 10:39:37 +0200534 unsigned long ino, int fsid)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200535{
Amir Goldstein12574a92018-03-16 10:39:37 +0200536 int xinobits = ovl_xino_bits(inode->i_sb);
537
Amir Goldstein695b46e2018-03-15 23:39:01 +0200538 /*
539 * When NFS export is enabled and d_ino is consistent with st_ino
Amir Goldstein12574a92018-03-16 10:39:37 +0200540 * (samefs or i_ino has enough bits to encode layer), set the same
541 * value used for d_ino to i_ino, because nfsd readdirplus compares
542 * d_ino values to i_ino values of child entries. When called from
543 * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
Amir Goldstein695b46e2018-03-15 23:39:01 +0200544 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
545 */
Amir Goldstein12574a92018-03-16 10:39:37 +0200546 if (inode->i_sb->s_export_op &&
547 (ovl_same_sb(inode->i_sb) || xinobits)) {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200548 inode->i_ino = ino;
Amir Goldstein12574a92018-03-16 10:39:37 +0200549 if (xinobits && fsid && !(ino >> (64 - xinobits)))
550 inode->i_ino |= (unsigned long)fsid << (64 - xinobits);
551 } else {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200552 inode->i_ino = get_next_ino();
Amir Goldstein12574a92018-03-16 10:39:37 +0200553 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200554 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200555 inode->i_flags |= S_NOCMTIME;
Miklos Szeredi2a3a2a32016-09-01 11:11:59 +0200556#ifdef CONFIG_FS_POSIX_ACL
557 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
558#endif
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200559
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200560 ovl_lockdep_annotate_inode_mutex_key(inode);
561
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100562 switch (mode & S_IFMT) {
563 case S_IFREG:
564 inode->i_op = &ovl_file_inode_operations;
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200565 inode->i_fop = &ovl_file_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100566 break;
567
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200568 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200569 inode->i_op = &ovl_dir_inode_operations;
570 inode->i_fop = &ovl_dir_operations;
571 break;
572
573 case S_IFLNK:
574 inode->i_op = &ovl_symlink_inode_operations;
575 break;
576
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200577 default:
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200578 inode->i_op = &ovl_special_inode_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100579 init_special_inode(inode, mode, rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200580 break;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200581 }
582}
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200583
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300584/*
585 * With inodes index enabled, an overlay inode nlink counts the union of upper
586 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
587 * upper inode, the following nlink modifying operations can happen:
588 *
589 * 1. Lower hardlink copy up
590 * 2. Upper hardlink created, unlinked or renamed over
591 * 3. Lower hardlink whiteout or renamed over
592 *
593 * For the first, copy up case, the union nlink does not change, whether the
594 * operation succeeds or fails, but the upper inode nlink may change.
595 * Therefore, before copy up, we store the union nlink value relative to the
596 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
597 *
598 * For the second, upper hardlink case, the union nlink should be incremented
599 * or decremented IFF the operation succeeds, aligned with nlink change of the
600 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
601 * value relative to the upper inode nlink in the index inode.
602 *
603 * For the last, lower cover up case, we simplify things by preceding the
604 * whiteout or cover up with copy up. This makes sure that there is an index
605 * upper inode where the nlink xattr can be stored before the copied up upper
606 * entry is unlink.
607 */
608#define OVL_NLINK_ADD_UPPER (1 << 0)
609
610/*
611 * On-disk format for indexed nlink:
612 *
613 * nlink relative to the upper inode - "U[+-]NUM"
614 * nlink relative to the lower inode - "L[+-]NUM"
615 */
616
617static int ovl_set_nlink_common(struct dentry *dentry,
618 struct dentry *realdentry, const char *format)
619{
620 struct inode *inode = d_inode(dentry);
621 struct inode *realinode = d_inode(realdentry);
622 char buf[13];
623 int len;
624
625 len = snprintf(buf, sizeof(buf), format,
626 (int) (inode->i_nlink - realinode->i_nlink));
627
Miklos Szeredi67873412017-07-27 21:54:05 +0200628 if (WARN_ON(len >= sizeof(buf)))
629 return -EIO;
630
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300631 return ovl_do_setxattr(ovl_dentry_upper(dentry),
632 OVL_XATTR_NLINK, buf, len, 0);
633}
634
635int ovl_set_nlink_upper(struct dentry *dentry)
636{
637 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
638}
639
640int ovl_set_nlink_lower(struct dentry *dentry)
641{
642 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
643}
644
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300645unsigned int ovl_get_nlink(struct dentry *lowerdentry,
646 struct dentry *upperdentry,
647 unsigned int fallback)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300648{
649 int nlink_diff;
650 int nlink;
651 char buf[13];
652 int err;
653
654 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
655 return fallback;
656
657 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
658 if (err < 0)
659 goto fail;
660
661 buf[err] = '\0';
662 if ((buf[0] != 'L' && buf[0] != 'U') ||
663 (buf[1] != '+' && buf[1] != '-'))
664 goto fail;
665
666 err = kstrtoint(buf + 1, 10, &nlink_diff);
667 if (err < 0)
668 goto fail;
669
670 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
671 nlink += nlink_diff;
672
673 if (nlink <= 0)
674 goto fail;
675
676 return nlink;
677
678fail:
679 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
680 upperdentry, err);
681 return fallback;
682}
683
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100684struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200685{
686 struct inode *inode;
687
688 inode = new_inode(sb);
689 if (inode)
Amir Goldstein12574a92018-03-16 10:39:37 +0200690 ovl_fill_inode(inode, mode, rdev, 0, 0);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200691
692 return inode;
693}
694
695static int ovl_inode_test(struct inode *inode, void *data)
696{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200697 return inode->i_private == data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200698}
699
700static int ovl_inode_set(struct inode *inode, void *data)
701{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200702 inode->i_private = data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200703 return 0;
704}
705
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200706static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
Amir Goldstein4b91c302018-01-18 16:39:13 +0200707 struct dentry *upperdentry, bool strict)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200708{
Amir Goldstein4b91c302018-01-18 16:39:13 +0200709 /*
710 * For directories, @strict verify from lookup path performs consistency
711 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
712 * inode. Non @strict verify from NFS handle decode path passes NULL for
713 * 'unknown' lower/upper.
714 */
715 if (S_ISDIR(inode->i_mode) && strict) {
Amir Goldstein31747ed2018-01-14 18:35:40 +0200716 /* Real lower dir moved to upper layer under us? */
717 if (!lowerdentry && ovl_inode_lower(inode))
718 return false;
719
720 /* Lookup of an uncovered redirect origin? */
721 if (!upperdentry && ovl_inode_upper(inode))
722 return false;
723 }
724
Amir Goldstein939ae4e2017-09-11 16:30:15 +0300725 /*
726 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
727 * This happens when finding a copied up overlay inode for a renamed
728 * or hardlinked overlay dentry and lower dentry cannot be followed
729 * by origin because lower fs does not support file handles.
730 */
731 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200732 return false;
733
734 /*
735 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
736 * This happens when finding a lower alias for a copied up hard link.
737 */
738 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
739 return false;
740
741 return true;
742}
743
Amir Goldstein4b91c302018-01-18 16:39:13 +0200744struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
745 bool is_upper)
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200746{
Amir Goldstein4b91c302018-01-18 16:39:13 +0200747 struct inode *inode, *key = d_inode(real);
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200748
749 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
750 if (!inode)
751 return NULL;
752
Amir Goldstein4b91c302018-01-18 16:39:13 +0200753 if (!ovl_verify_inode(inode, is_upper ? NULL : real,
754 is_upper ? real : NULL, false)) {
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200755 iput(inode);
756 return ERR_PTR(-ESTALE);
757 }
758
759 return inode;
760}
761
Amir Goldstein764baba2018-02-04 15:35:09 +0200762/*
763 * Does overlay inode need to be hashed by lower inode?
764 */
765static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
766 struct dentry *lower, struct dentry *index)
767{
768 struct ovl_fs *ofs = sb->s_fs_info;
769
770 /* No, if pure upper */
771 if (!lower)
772 return false;
773
774 /* Yes, if already indexed */
775 if (index)
776 return true;
777
778 /* Yes, if won't be copied up */
779 if (!ofs->upper_mnt)
780 return true;
781
782 /* No, if lower hardlink is or will be broken on copy up */
783 if ((upper || !ovl_indexdir(sb)) &&
784 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
785 return false;
786
787 /* No, if non-indexed upper with NFS export */
788 if (sb->s_export_op && upper)
789 return false;
790
791 /* Otherwise, hash by lower inode for fsnotify */
792 return true;
793}
794
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300795static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
796 struct inode *key)
797{
798 return newinode ? inode_insert5(newinode, (unsigned long) key,
799 ovl_inode_test, ovl_inode_set, key) :
800 iget5_locked(sb, (unsigned long) key,
801 ovl_inode_test, ovl_inode_set, key);
802}
803
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400804struct inode *ovl_get_inode(struct super_block *sb,
805 struct ovl_inode_params *oip)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200806{
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400807 struct dentry *upperdentry = oip->upperdentry;
808 struct ovl_path *lowerpath = oip->lowerpath;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200809 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200810 struct inode *inode;
Amir Goldstein12574a92018-03-16 10:39:37 +0200811 struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400812 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
813 oip->index);
814 int fsid = bylower ? oip->lowerpath->layer->fsid : 0;
Amir Goldstein31747ed2018-01-14 18:35:40 +0200815 bool is_dir;
Amir Goldstein695b46e2018-03-15 23:39:01 +0200816 unsigned long ino = 0;
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300817
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200818 if (!realinode)
819 realinode = d_inode(lowerdentry);
820
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300821 /*
Amir Goldstein764baba2018-02-04 15:35:09 +0200822 * Copy up origin (lower) may exist for non-indexed upper, but we must
823 * not use lower as hash key if this is a broken hardlink.
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300824 */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200825 is_dir = S_ISDIR(realinode->i_mode);
Amir Goldstein764baba2018-02-04 15:35:09 +0200826 if (upperdentry || bylower) {
827 struct inode *key = d_inode(bylower ? lowerdentry :
828 upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200829 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200830
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300831 inode = ovl_iget5(sb, oip->newinode, key);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200832 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200833 goto out_nomem;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200834 if (!(inode->i_state & I_NEW)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200835 /*
836 * Verify that the underlying files stored in the inode
837 * match those in the dentry.
838 */
Amir Goldstein4b91c302018-01-18 16:39:13 +0200839 if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
840 true)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200841 iput(inode);
842 inode = ERR_PTR(-ESTALE);
843 goto out;
844 }
845
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200846 dput(upperdentry);
847 goto out;
848 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200849
Amir Goldstein31747ed2018-01-14 18:35:40 +0200850 /* Recalculate nlink for non-dir due to indexing */
851 if (!is_dir)
852 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300853 set_nlink(inode, nlink);
Amir Goldstein695b46e2018-03-15 23:39:01 +0200854 ino = key->i_ino;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200855 } else {
Amir Goldstein764baba2018-02-04 15:35:09 +0200856 /* Lower hardlink that will be broken on copy up */
Amir Goldstein0aceb532017-12-12 23:43:16 +0200857 inode = new_inode(sb);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200858 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200859 goto out_nomem;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200860 }
Amir Goldstein12574a92018-03-16 10:39:37 +0200861 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200862 ovl_inode_init(inode, upperdentry, lowerdentry);
Miklos Szeredi13c72072017-07-04 22:03:16 +0200863
864 if (upperdentry && ovl_is_impuredir(upperdentry))
865 ovl_set_flag(OVL_IMPURE, inode);
866
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400867 if (oip->index)
Vivek Goyal0471a9c2018-03-20 16:35:40 -0400868 ovl_set_flag(OVL_INDEX, inode);
869
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300870 /* Check for non-merge dir that may have whiteouts */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200871 if (is_dir) {
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400872 if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300873 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
874 ovl_set_flag(OVL_WHITEOUTS, inode);
875 }
876 }
877
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200878 if (inode->i_state & I_NEW)
879 unlock_new_inode(inode);
880out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200881 return inode;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200882
883out_nomem:
884 inode = ERR_PTR(-ENOMEM);
885 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200886}