blob: 3f1e5bd0ba8739b8ae8527e589d9d4bdf5747121 [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
Miklos Szeredicf9a6782015-12-11 16:30:49 +010025 /*
26 * Check for permissions before trying to copy-up. This is redundant
27 * since it will be rechecked later by ->setattr() on upper dentry. But
28 * without this, copy-up can be triggered by just about anybody.
29 *
30 * We don't initialize inode->size, which just means that
31 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
32 * check for a swapfile (which this won't be anyway).
33 */
Jan Kara31051c82016-05-26 16:55:18 +020034 err = setattr_prepare(dentry, attr);
Miklos Szeredicf9a6782015-12-11 16:30:49 +010035 if (err)
36 return err;
37
Miklos Szeredie9be9d52014-10-24 00:14:38 +020038 err = ovl_want_write(dentry);
39 if (err)
40 goto out;
41
Miklos Szeredi58121602018-07-18 15:44:41 +020042 if (attr->ia_valid & ATTR_SIZE) {
43 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
44
45 err = -ETXTBSY;
46 if (atomic_read(&realinode->i_writecount) < 0)
47 goto out_drop_write;
48 }
49
Miklos Szerediacff81e2015-12-04 19:18:48 +010050 err = ovl_copy_up(dentry);
51 if (!err) {
Miklos Szeredi58121602018-07-18 15:44:41 +020052 struct inode *winode = NULL;
53
Miklos Szerediacff81e2015-12-04 19:18:48 +010054 upperdentry = ovl_dentry_upper(dentry);
55
Miklos Szeredi58121602018-07-18 15:44:41 +020056 if (attr->ia_valid & ATTR_SIZE) {
57 winode = d_inode(upperdentry);
58 err = get_write_access(winode);
59 if (err)
60 goto out_drop_write;
61 }
62
Miklos Szeredib99c2d92016-07-04 16:49:48 +020063 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
64 attr->ia_valid &= ~ATTR_MODE;
65
Al Viro59551022016-01-22 15:40:57 -050066 inode_lock(upperdentry->d_inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040067 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020068 err = notify_change(upperdentry, attr, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040069 revert_creds(old_cred);
Konstantin Khlebnikovb81de062016-01-31 16:21:29 +030070 if (!err)
71 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Al Viro59551022016-01-22 15:40:57 -050072 inode_unlock(upperdentry->d_inode);
Miklos Szeredi58121602018-07-18 15:44:41 +020073
74 if (winode)
75 put_write_access(winode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020076 }
Miklos Szeredi58121602018-07-18 15:44:41 +020077out_drop_write:
Miklos Szeredie9be9d52014-10-24 00:14:38 +020078 ovl_drop_write(dentry);
79out:
80 return err;
81}
82
Amir Goldsteinda309e82017-11-08 19:39:51 +020083static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat,
84 struct ovl_layer *lower_layer)
85{
86 bool samefs = ovl_same_sb(dentry->d_sb);
Amir Goldsteine487d882017-11-07 13:55:04 +020087 unsigned int xinobits = ovl_xino_bits(dentry->d_sb);
Amir Goldsteinda309e82017-11-08 19:39:51 +020088
89 if (samefs) {
90 /*
91 * When all layers are on the same fs, all real inode
92 * number are unique, so we use the overlay st_dev,
93 * which is friendly to du -x.
94 */
95 stat->dev = dentry->d_sb->s_dev;
Amir Goldsteine487d882017-11-07 13:55:04 +020096 return 0;
97 } else if (xinobits) {
98 unsigned int shift = 64 - xinobits;
99 /*
100 * All inode numbers of underlying fs should not be using the
101 * high xinobits, so we use high xinobits to partition the
102 * overlay st_ino address space. The high bits holds the fsid
103 * (upper fsid is 0). This way overlay inode numbers are unique
104 * and all inodes use overlay st_dev. Inode numbers are also
105 * persistent for a given layer configuration.
106 */
107 if (stat->ino >> shift) {
108 pr_warn_ratelimited("overlayfs: inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
109 dentry, stat->ino, xinobits);
110 } else {
111 if (lower_layer)
112 stat->ino |= ((u64)lower_layer->fsid) << shift;
113
114 stat->dev = dentry->d_sb->s_dev;
115 return 0;
116 }
117 }
118
119 /* The inode could not be mapped to a unified st_ino address space */
120 if (S_ISDIR(dentry->d_inode->i_mode)) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200121 /*
122 * Always use the overlay st_dev for directories, so 'find
123 * -xdev' will scan the entire overlay mount and won't cross the
124 * overlay mount boundaries.
125 *
126 * If not all layers are on the same fs the pair {real st_ino;
127 * overlay st_dev} is not unique, so use the non persistent
128 * overlay st_ino for directories.
129 */
130 stat->dev = dentry->d_sb->s_dev;
131 stat->ino = dentry->d_inode->i_ino;
Amir Goldstein51486262018-03-28 20:22:41 +0300132 } else if (lower_layer && lower_layer->fsid) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200133 /*
134 * For non-samefs setup, if we cannot map all layers st_ino
135 * to a unified address space, we need to make sure that st_dev
Amir Goldstein51486262018-03-28 20:22:41 +0300136 * is unique per lower fs. Upper layer uses real st_dev and
137 * lower layers use the unique anonymous bdev assigned to the
138 * lower fs.
Amir Goldsteinda309e82017-11-08 19:39:51 +0200139 */
Amir Goldstein51486262018-03-28 20:22:41 +0300140 stat->dev = lower_layer->fs->pseudo_dev;
Amir Goldsteinda309e82017-11-08 19:39:51 +0200141 }
142
143 return 0;
144}
145
Miklos Szeredi5b712092017-05-05 11:38:58 +0200146int ovl_getattr(const struct path *path, struct kstat *stat,
147 u32 request_mask, unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200148{
David Howellsa528d352017-01-31 16:46:22 +0000149 struct dentry *dentry = path->dentry;
Amir Goldstein72b608f2017-04-24 00:25:49 +0300150 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200151 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400152 const struct cred *old_cred;
Miklos Szeredi5b712092017-05-05 11:38:58 +0200153 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
Amir Goldsteina0c5ad32017-11-01 00:45:40 +0200154 bool samefs = ovl_same_sb(dentry->d_sb);
Amir Goldsteinda309e82017-11-08 19:39:51 +0200155 struct ovl_layer *lower_layer = NULL;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400156 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200157
Amir Goldstein72b608f2017-04-24 00:25:49 +0300158 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400159 old_cred = ovl_override_creds(dentry->d_sb);
David Howellsa528d352017-01-31 16:46:22 +0000160 err = vfs_getattr(&realpath, stat, request_mask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300161 if (err)
162 goto out;
163
164 /*
Amir Goldsteinda309e82017-11-08 19:39:51 +0200165 * For non-dir or same fs, we use st_ino of the copy up origin.
166 * This guaranties constant st_dev/st_ino across copy up.
Amir Goldsteine487d882017-11-07 13:55:04 +0200167 * With xino feature and non-samefs, we use st_ino of the copy up
168 * origin masked with high bits that represent the layer id.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300169 *
Amir Goldsteinda309e82017-11-08 19:39:51 +0200170 * If lower filesystem supports NFS file handles, this also guaranties
Amir Goldstein72b608f2017-04-24 00:25:49 +0300171 * persistent st_ino across mount cycle.
172 */
Amir Goldsteine487d882017-11-07 13:55:04 +0200173 if (!is_dir || samefs || ovl_xino_bits(dentry->d_sb)) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200174 if (!OVL_TYPE_UPPER(type)) {
175 lower_layer = ovl_layer_lower(dentry);
176 } else if (OVL_TYPE_ORIGIN(type)) {
Amir Goldstein72b608f2017-04-24 00:25:49 +0300177 struct kstat lowerstat;
Miklos Szeredi5b712092017-05-05 11:38:58 +0200178 u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300179
180 ovl_path_lower(dentry, &realpath);
181 err = vfs_getattr(&realpath, &lowerstat,
Miklos Szeredi5b712092017-05-05 11:38:58 +0200182 lowermask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300183 if (err)
184 goto out;
185
Amir Goldstein72b608f2017-04-24 00:25:49 +0300186 /*
Amir Goldstein359f3922017-06-21 15:28:41 +0300187 * Lower hardlinks may be broken on copy up to different
Amir Goldstein72b608f2017-04-24 00:25:49 +0300188 * upper files, so we cannot use the lower origin st_ino
189 * for those different files, even for the same fs case.
Amir Goldstein86eaa132017-11-21 13:55:51 +0200190 *
191 * Similarly, several redirected dirs can point to the
192 * same dir on a lower layer. With the "verify_lower"
193 * feature, we do not use the lower origin st_ino, if
194 * we haven't verified that this redirect is unique.
195 *
Amir Goldstein359f3922017-06-21 15:28:41 +0300196 * With inodes index enabled, it is safe to use st_ino
Amir Goldstein86eaa132017-11-21 13:55:51 +0200197 * of an indexed origin. The index validates that the
198 * upper hardlink is not broken and that a redirected
199 * dir is the only redirect to that origin.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300200 */
Amir Goldstein86eaa132017-11-21 13:55:51 +0200201 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
202 (!ovl_verify_lower(dentry->d_sb) &&
Amir Goldstein9f99e502018-04-11 20:09:29 +0300203 (is_dir || lowerstat.nlink == 1))) {
Amir Goldstein72b608f2017-04-24 00:25:49 +0300204 stat->ino = lowerstat.ino;
Amir Goldsteinda309e82017-11-08 19:39:51 +0200205 lower_layer = ovl_layer_lower(dentry);
Amir Goldstein9f99e502018-04-11 20:09:29 +0300206 }
Amir Goldstein72b608f2017-04-24 00:25:49 +0300207 }
Amir Goldstein72b608f2017-04-24 00:25:49 +0300208 }
Miklos Szeredi5b712092017-05-05 11:38:58 +0200209
Amir Goldsteinda309e82017-11-08 19:39:51 +0200210 err = ovl_map_dev_ino(dentry, stat, lower_layer);
211 if (err)
212 goto out;
213
Miklos Szeredi5b712092017-05-05 11:38:58 +0200214 /*
215 * It's probably not worth it to count subdirs to get the
216 * correct link count. nlink=1 seems to pacify 'find' and
217 * other utilities.
218 */
219 if (is_dir && OVL_TYPE_MERGE(type))
220 stat->nlink = 1;
221
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300222 /*
223 * Return the overlay inode nlinks for indexed upper inodes.
224 * Overlay inode nlink counts the union of the upper hardlinks
225 * and non-covered lower hardlinks. It does not include the upper
226 * index hardlink.
227 */
228 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
229 stat->nlink = dentry->d_inode->i_nlink;
230
Amir Goldstein72b608f2017-04-24 00:25:49 +0300231out:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400232 revert_creds(old_cred);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300233
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400234 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200235}
236
237int ovl_permission(struct inode *inode, int mask)
238{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200239 struct inode *upperinode = ovl_inode_upper(inode);
240 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400241 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200242 int err;
243
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200244 /* Careful in RCU walk mode */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200245 if (!realinode) {
246 WARN_ON(!(mask & MAY_NOT_BLOCK));
Miklos Szeredia999d7e2016-07-29 12:05:23 +0200247 return -ECHILD;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200248 }
249
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400250 /*
251 * Check overlay inode with the creds of task and underlying inode
252 * with creds of mounter
253 */
254 err = generic_permission(inode, mask);
255 if (err)
256 return err;
257
258 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200259 if (!upperinode &&
260 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
Vivek Goyal754f8cb2016-07-01 16:34:29 -0400261 mask &= ~(MAY_WRITE | MAY_APPEND);
Vivek Goyal500cac32016-07-13 11:00:14 -0400262 /* Make sure mounter can read file for copy up later */
263 mask |= MAY_READ;
264 }
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200265 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400266 revert_creds(old_cred);
267
268 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200269}
270
Al Viro6b255392015-11-17 10:20:54 -0500271static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500272 struct inode *inode,
273 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200274{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400275 const struct cred *old_cred;
276 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200277
Al Viro6b255392015-11-17 10:20:54 -0500278 if (!dentry)
279 return ERR_PTR(-ECHILD);
280
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400281 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200282 p = vfs_get_link(ovl_dentry_real(dentry), done);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400283 revert_creds(old_cred);
284 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200285}
286
Miklos Szeredi09562542016-08-08 15:08:49 +0200287bool ovl_is_private_xattr(const char *name)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200288{
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +0200289 return strncmp(name, OVL_XATTR_PREFIX,
290 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200291}
292
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200293int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
294 const void *value, size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200295{
296 int err;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200297 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
298 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400299 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200300
301 err = ovl_want_write(dentry);
302 if (err)
303 goto out;
304
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200305 if (!value && !upperdentry) {
306 err = vfs_getxattr(realdentry, name, NULL, 0);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200307 if (err < 0)
308 goto out_drop_write;
309 }
310
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200311 if (!upperdentry) {
312 err = ovl_copy_up(dentry);
313 if (err)
314 goto out_drop_write;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200315
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200316 realdentry = ovl_dentry_upper(dentry);
317 }
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200318
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400319 old_cred = ovl_override_creds(dentry->d_sb);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200320 if (value)
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200321 err = vfs_setxattr(realdentry, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200322 else {
323 WARN_ON(flags != XATTR_REPLACE);
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200324 err = vfs_removexattr(realdentry, name);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200325 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400326 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200327
Miklos Szeredid9854c82018-07-18 15:44:40 +0200328 /* copy c/mtime */
329 ovl_copyattr(d_inode(realdentry), inode);
330
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200331out_drop_write:
332 ovl_drop_write(dentry);
333out:
334 return err;
335}
336
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200337int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200338 void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200339{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400340 ssize_t res;
341 const struct cred *old_cred;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200342 struct dentry *realdentry =
343 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
Miklos Szeredi52148462014-11-20 16:40:00 +0100344
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400345 old_cred = ovl_override_creds(dentry->d_sb);
346 res = vfs_getxattr(realdentry, name, value, size);
347 revert_creds(old_cred);
348 return res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200349}
350
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200351static bool ovl_can_list(const char *s)
352{
353 /* List all non-trusted xatts */
354 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
355 return true;
356
357 /* Never list trusted.overlay, list other trusted for superuser only */
358 return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
359}
360
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200361ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
362{
Miklos Szeredib5817552016-06-06 16:21:37 +0200363 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200364 ssize_t res;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200365 size_t len;
366 char *s;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400367 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200368
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400369 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200370 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400371 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200372 if (res <= 0 || size == 0)
373 return res;
374
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200375 /* filter out private xattrs */
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200376 for (s = list, len = res; len;) {
377 size_t slen = strnlen(s, len) + 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200378
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200379 /* underlying fs providing us with an broken xattr list? */
380 if (WARN_ON(slen > len))
381 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200382
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200383 len -= slen;
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200384 if (!ovl_can_list(s)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200385 res -= slen;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200386 memmove(s, s + slen, len);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200387 } else {
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200388 s += slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200389 }
390 }
391
392 return res;
393}
394
Vivek Goyal39a25b22016-07-01 16:34:26 -0400395struct posix_acl *ovl_get_acl(struct inode *inode, int type)
396{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200397 struct inode *realinode = ovl_inode_real(inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400398 const struct cred *old_cred;
399 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400400
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200401 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
Vivek Goyal39a25b22016-07-01 16:34:26 -0400402 return NULL;
403
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400404 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200405 acl = get_acl(realinode, type);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400406 revert_creds(old_cred);
407
408 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400409}
410
Amir Goldstein59be0972017-06-20 15:25:46 +0300411static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200412{
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300413 /* Copy up of disconnected dentry does not set upper alias */
Amir Goldstein59be0972017-06-20 15:25:46 +0300414 if (ovl_dentry_upper(dentry) &&
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300415 (ovl_dentry_has_upper_alias(dentry) ||
416 (dentry->d_flags & DCACHE_DISCONNECTED)))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200417 return false;
418
Amir Goldstein59be0972017-06-20 15:25:46 +0300419 if (special_file(d_inode(dentry)->i_mode))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200420 return false;
421
422 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
423 return false;
424
425 return true;
426}
427
Miklos Szeredi2d902672016-06-30 08:53:27 +0200428int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200429{
Miklos Szeredi2d902672016-06-30 08:53:27 +0200430 int err = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200431
Amir Goldstein59be0972017-06-20 15:25:46 +0300432 if (ovl_open_need_copy_up(dentry, file_flags)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200433 err = ovl_want_write(dentry);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200434 if (!err) {
Amir Goldstein9aba6522016-11-12 21:36:03 +0200435 err = ovl_copy_up_flags(dentry, file_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200436 ovl_drop_write(dentry);
437 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200438 }
439
Miklos Szeredi2d902672016-06-30 08:53:27 +0200440 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200441}
442
Deepa Dinamani95582b02018-05-08 19:36:02 -0700443int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200444{
Miklos Szeredi8f35cf52018-04-12 12:04:50 +0200445 if (flags & S_ATIME) {
446 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
447 struct path upperpath = {
448 .mnt = ofs->upper_mnt,
449 .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
450 };
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200451
Miklos Szeredi8f35cf52018-04-12 12:04:50 +0200452 if (upperpath.dentry) {
453 touch_atime(&upperpath);
454 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
455 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200456 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200457 return 0;
458}
459
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200460static const struct inode_operations ovl_file_inode_operations = {
461 .setattr = ovl_setattr,
462 .permission = ovl_permission,
463 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200464 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400465 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200466 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200467};
468
469static const struct inode_operations ovl_symlink_inode_operations = {
470 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500471 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200472 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200473 .listxattr = ovl_listxattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200474 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200475};
476
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200477/*
478 * It is possible to stack overlayfs instance on top of another
479 * overlayfs instance as lower layer. We need to annonate the
480 * stackable i_mutex locks according to stack level of the super
481 * block instance. An overlayfs instance can never be in stack
482 * depth 0 (there is always a real fs below it). An overlayfs
483 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
484 *
485 * For example, here is a snip from /proc/lockdep_chains after
486 * dir_iterate of nested overlayfs:
487 *
488 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
489 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
490 * [...] &type->i_mutex_dir_key (stack_depth=0)
491 */
492#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
493
494static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
495{
496#ifdef CONFIG_LOCKDEP
497 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
498 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300499 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200500
501 int depth = inode->i_sb->s_stack_depth - 1;
502
503 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
504 depth = 0;
505
506 if (S_ISDIR(inode->i_mode))
507 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
508 else
509 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300510
511 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200512#endif
513}
514
Amir Goldstein695b46e2018-03-15 23:39:01 +0200515static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
Amir Goldstein12574a92018-03-16 10:39:37 +0200516 unsigned long ino, int fsid)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200517{
Amir Goldstein12574a92018-03-16 10:39:37 +0200518 int xinobits = ovl_xino_bits(inode->i_sb);
519
Amir Goldstein695b46e2018-03-15 23:39:01 +0200520 /*
521 * When NFS export is enabled and d_ino is consistent with st_ino
Amir Goldstein12574a92018-03-16 10:39:37 +0200522 * (samefs or i_ino has enough bits to encode layer), set the same
523 * value used for d_ino to i_ino, because nfsd readdirplus compares
524 * d_ino values to i_ino values of child entries. When called from
525 * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
Amir Goldstein695b46e2018-03-15 23:39:01 +0200526 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
527 */
Amir Goldstein12574a92018-03-16 10:39:37 +0200528 if (inode->i_sb->s_export_op &&
529 (ovl_same_sb(inode->i_sb) || xinobits)) {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200530 inode->i_ino = ino;
Amir Goldstein12574a92018-03-16 10:39:37 +0200531 if (xinobits && fsid && !(ino >> (64 - xinobits)))
532 inode->i_ino |= (unsigned long)fsid << (64 - xinobits);
533 } else {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200534 inode->i_ino = get_next_ino();
Amir Goldstein12574a92018-03-16 10:39:37 +0200535 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200536 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200537 inode->i_flags |= S_NOCMTIME;
Miklos Szeredi2a3a2a32016-09-01 11:11:59 +0200538#ifdef CONFIG_FS_POSIX_ACL
539 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
540#endif
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200541
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200542 ovl_lockdep_annotate_inode_mutex_key(inode);
543
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100544 switch (mode & S_IFMT) {
545 case S_IFREG:
546 inode->i_op = &ovl_file_inode_operations;
547 break;
548
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200549 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200550 inode->i_op = &ovl_dir_inode_operations;
551 inode->i_fop = &ovl_dir_operations;
552 break;
553
554 case S_IFLNK:
555 inode->i_op = &ovl_symlink_inode_operations;
556 break;
557
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200558 default:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200559 inode->i_op = &ovl_file_inode_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100560 init_special_inode(inode, mode, rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200561 break;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200562 }
563}
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200564
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300565/*
566 * With inodes index enabled, an overlay inode nlink counts the union of upper
567 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
568 * upper inode, the following nlink modifying operations can happen:
569 *
570 * 1. Lower hardlink copy up
571 * 2. Upper hardlink created, unlinked or renamed over
572 * 3. Lower hardlink whiteout or renamed over
573 *
574 * For the first, copy up case, the union nlink does not change, whether the
575 * operation succeeds or fails, but the upper inode nlink may change.
576 * Therefore, before copy up, we store the union nlink value relative to the
577 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
578 *
579 * For the second, upper hardlink case, the union nlink should be incremented
580 * or decremented IFF the operation succeeds, aligned with nlink change of the
581 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
582 * value relative to the upper inode nlink in the index inode.
583 *
584 * For the last, lower cover up case, we simplify things by preceding the
585 * whiteout or cover up with copy up. This makes sure that there is an index
586 * upper inode where the nlink xattr can be stored before the copied up upper
587 * entry is unlink.
588 */
589#define OVL_NLINK_ADD_UPPER (1 << 0)
590
591/*
592 * On-disk format for indexed nlink:
593 *
594 * nlink relative to the upper inode - "U[+-]NUM"
595 * nlink relative to the lower inode - "L[+-]NUM"
596 */
597
598static int ovl_set_nlink_common(struct dentry *dentry,
599 struct dentry *realdentry, const char *format)
600{
601 struct inode *inode = d_inode(dentry);
602 struct inode *realinode = d_inode(realdentry);
603 char buf[13];
604 int len;
605
606 len = snprintf(buf, sizeof(buf), format,
607 (int) (inode->i_nlink - realinode->i_nlink));
608
Miklos Szeredi67873412017-07-27 21:54:05 +0200609 if (WARN_ON(len >= sizeof(buf)))
610 return -EIO;
611
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300612 return ovl_do_setxattr(ovl_dentry_upper(dentry),
613 OVL_XATTR_NLINK, buf, len, 0);
614}
615
616int ovl_set_nlink_upper(struct dentry *dentry)
617{
618 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
619}
620
621int ovl_set_nlink_lower(struct dentry *dentry)
622{
623 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
624}
625
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300626unsigned int ovl_get_nlink(struct dentry *lowerdentry,
627 struct dentry *upperdentry,
628 unsigned int fallback)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300629{
630 int nlink_diff;
631 int nlink;
632 char buf[13];
633 int err;
634
635 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
636 return fallback;
637
638 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
639 if (err < 0)
640 goto fail;
641
642 buf[err] = '\0';
643 if ((buf[0] != 'L' && buf[0] != 'U') ||
644 (buf[1] != '+' && buf[1] != '-'))
645 goto fail;
646
647 err = kstrtoint(buf + 1, 10, &nlink_diff);
648 if (err < 0)
649 goto fail;
650
651 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
652 nlink += nlink_diff;
653
654 if (nlink <= 0)
655 goto fail;
656
657 return nlink;
658
659fail:
660 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
661 upperdentry, err);
662 return fallback;
663}
664
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100665struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200666{
667 struct inode *inode;
668
669 inode = new_inode(sb);
670 if (inode)
Amir Goldstein12574a92018-03-16 10:39:37 +0200671 ovl_fill_inode(inode, mode, rdev, 0, 0);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200672
673 return inode;
674}
675
676static int ovl_inode_test(struct inode *inode, void *data)
677{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200678 return inode->i_private == data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200679}
680
681static int ovl_inode_set(struct inode *inode, void *data)
682{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200683 inode->i_private = data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200684 return 0;
685}
686
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200687static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
Amir Goldstein4b91c302018-01-18 16:39:13 +0200688 struct dentry *upperdentry, bool strict)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200689{
Amir Goldstein4b91c302018-01-18 16:39:13 +0200690 /*
691 * For directories, @strict verify from lookup path performs consistency
692 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
693 * inode. Non @strict verify from NFS handle decode path passes NULL for
694 * 'unknown' lower/upper.
695 */
696 if (S_ISDIR(inode->i_mode) && strict) {
Amir Goldstein31747ed2018-01-14 18:35:40 +0200697 /* Real lower dir moved to upper layer under us? */
698 if (!lowerdentry && ovl_inode_lower(inode))
699 return false;
700
701 /* Lookup of an uncovered redirect origin? */
702 if (!upperdentry && ovl_inode_upper(inode))
703 return false;
704 }
705
Amir Goldstein939ae4e2017-09-11 16:30:15 +0300706 /*
707 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
708 * This happens when finding a copied up overlay inode for a renamed
709 * or hardlinked overlay dentry and lower dentry cannot be followed
710 * by origin because lower fs does not support file handles.
711 */
712 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200713 return false;
714
715 /*
716 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
717 * This happens when finding a lower alias for a copied up hard link.
718 */
719 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
720 return false;
721
722 return true;
723}
724
Amir Goldstein4b91c302018-01-18 16:39:13 +0200725struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
726 bool is_upper)
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200727{
Amir Goldstein4b91c302018-01-18 16:39:13 +0200728 struct inode *inode, *key = d_inode(real);
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200729
730 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
731 if (!inode)
732 return NULL;
733
Amir Goldstein4b91c302018-01-18 16:39:13 +0200734 if (!ovl_verify_inode(inode, is_upper ? NULL : real,
735 is_upper ? real : NULL, false)) {
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200736 iput(inode);
737 return ERR_PTR(-ESTALE);
738 }
739
740 return inode;
741}
742
Amir Goldstein764baba2018-02-04 15:35:09 +0200743/*
744 * Does overlay inode need to be hashed by lower inode?
745 */
746static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
747 struct dentry *lower, struct dentry *index)
748{
749 struct ovl_fs *ofs = sb->s_fs_info;
750
751 /* No, if pure upper */
752 if (!lower)
753 return false;
754
755 /* Yes, if already indexed */
756 if (index)
757 return true;
758
759 /* Yes, if won't be copied up */
760 if (!ofs->upper_mnt)
761 return true;
762
763 /* No, if lower hardlink is or will be broken on copy up */
764 if ((upper || !ovl_indexdir(sb)) &&
765 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
766 return false;
767
768 /* No, if non-indexed upper with NFS export */
769 if (sb->s_export_op && upper)
770 return false;
771
772 /* Otherwise, hash by lower inode for fsnotify */
773 return true;
774}
775
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300776static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
777 struct inode *key)
778{
779 return newinode ? inode_insert5(newinode, (unsigned long) key,
780 ovl_inode_test, ovl_inode_set, key) :
781 iget5_locked(sb, (unsigned long) key,
782 ovl_inode_test, ovl_inode_set, key);
783}
784
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400785struct inode *ovl_get_inode(struct super_block *sb,
786 struct ovl_inode_params *oip)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200787{
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400788 struct dentry *upperdentry = oip->upperdentry;
789 struct ovl_path *lowerpath = oip->lowerpath;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200790 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200791 struct inode *inode;
Amir Goldstein12574a92018-03-16 10:39:37 +0200792 struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400793 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
794 oip->index);
795 int fsid = bylower ? oip->lowerpath->layer->fsid : 0;
Amir Goldstein31747ed2018-01-14 18:35:40 +0200796 bool is_dir;
Amir Goldstein695b46e2018-03-15 23:39:01 +0200797 unsigned long ino = 0;
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300798
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200799 if (!realinode)
800 realinode = d_inode(lowerdentry);
801
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300802 /*
Amir Goldstein764baba2018-02-04 15:35:09 +0200803 * Copy up origin (lower) may exist for non-indexed upper, but we must
804 * not use lower as hash key if this is a broken hardlink.
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300805 */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200806 is_dir = S_ISDIR(realinode->i_mode);
Amir Goldstein764baba2018-02-04 15:35:09 +0200807 if (upperdentry || bylower) {
808 struct inode *key = d_inode(bylower ? lowerdentry :
809 upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200810 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200811
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300812 inode = ovl_iget5(sb, oip->newinode, key);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200813 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200814 goto out_nomem;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200815 if (!(inode->i_state & I_NEW)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200816 /*
817 * Verify that the underlying files stored in the inode
818 * match those in the dentry.
819 */
Amir Goldstein4b91c302018-01-18 16:39:13 +0200820 if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
821 true)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200822 iput(inode);
823 inode = ERR_PTR(-ESTALE);
824 goto out;
825 }
826
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200827 dput(upperdentry);
828 goto out;
829 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200830
Amir Goldstein31747ed2018-01-14 18:35:40 +0200831 /* Recalculate nlink for non-dir due to indexing */
832 if (!is_dir)
833 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300834 set_nlink(inode, nlink);
Amir Goldstein695b46e2018-03-15 23:39:01 +0200835 ino = key->i_ino;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200836 } else {
Amir Goldstein764baba2018-02-04 15:35:09 +0200837 /* Lower hardlink that will be broken on copy up */
Amir Goldstein0aceb532017-12-12 23:43:16 +0200838 inode = new_inode(sb);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200839 if (!inode)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200840 goto out_nomem;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200841 }
Amir Goldstein12574a92018-03-16 10:39:37 +0200842 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200843 ovl_inode_init(inode, upperdentry, lowerdentry);
Miklos Szeredi13c72072017-07-04 22:03:16 +0200844
845 if (upperdentry && ovl_is_impuredir(upperdentry))
846 ovl_set_flag(OVL_IMPURE, inode);
847
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400848 if (oip->index)
Vivek Goyal0471a9c2018-03-20 16:35:40 -0400849 ovl_set_flag(OVL_INDEX, inode);
850
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300851 /* Check for non-merge dir that may have whiteouts */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200852 if (is_dir) {
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400853 if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300854 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
855 ovl_set_flag(OVL_WHITEOUTS, inode);
856 }
857 }
858
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200859 if (inode->i_state & I_NEW)
860 unlock_new_inode(inode);
861out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200862 return inode;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200863
864out_nomem:
865 inode = ERR_PTR(-ENOMEM);
866 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200867}