blob: 79e8994e3bc1adc1605465ea1114603fbfa38e25 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002/*
3 *
4 * Copyright (C) 2011 Novell Inc.
Miklos Szeredie9be9d52014-10-24 00:14:38 +02005 */
6
7#include <linux/fs.h>
8#include <linux/slab.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +01009#include <linux/cred.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020010#include <linux/xattr.h>
Miklos Szeredi5201dc42016-09-01 11:11:59 +020011#include <linux/posix_acl.h>
Amir Goldstein5f8415d2017-06-20 15:35:14 +030012#include <linux/ratelimit.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020013#include "overlayfs.h"
14
Chandan Rajendraba1e5632017-07-24 01:57:54 -050015
Miklos Szeredie9be9d52014-10-24 00:14:38 +020016int ovl_setattr(struct dentry *dentry, struct iattr *attr)
17{
18 int err;
Vivek Goyal997336f2018-05-11 11:49:33 -040019 bool full_copy_up = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020020 struct dentry *upperdentry;
Vivek Goyal1175b6b2016-07-01 16:34:28 -040021 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020022
Jan Kara31051c82016-05-26 16:55:18 +020023 err = setattr_prepare(dentry, attr);
Miklos Szeredicf9a6782015-12-11 16:30:49 +010024 if (err)
25 return err;
26
Miklos Szeredie9be9d52014-10-24 00:14:38 +020027 err = ovl_want_write(dentry);
28 if (err)
29 goto out;
30
Miklos Szeredi58121602018-07-18 15:44:41 +020031 if (attr->ia_valid & ATTR_SIZE) {
32 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
33
34 err = -ETXTBSY;
35 if (atomic_read(&realinode->i_writecount) < 0)
36 goto out_drop_write;
Vivek Goyal997336f2018-05-11 11:49:33 -040037
38 /* Truncate should trigger data copy up as well */
39 full_copy_up = true;
Miklos Szeredi58121602018-07-18 15:44:41 +020040 }
41
Vivek Goyal997336f2018-05-11 11:49:33 -040042 if (!full_copy_up)
43 err = ovl_copy_up(dentry);
44 else
45 err = ovl_copy_up_with_data(dentry);
Miklos Szerediacff81e2015-12-04 19:18:48 +010046 if (!err) {
Miklos Szeredi58121602018-07-18 15:44:41 +020047 struct inode *winode = NULL;
48
Miklos Szerediacff81e2015-12-04 19:18:48 +010049 upperdentry = ovl_dentry_upper(dentry);
50
Miklos Szeredi58121602018-07-18 15:44:41 +020051 if (attr->ia_valid & ATTR_SIZE) {
52 winode = d_inode(upperdentry);
53 err = get_write_access(winode);
54 if (err)
55 goto out_drop_write;
56 }
57
Miklos Szeredib99c2d92016-07-04 16:49:48 +020058 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
59 attr->ia_valid &= ~ATTR_MODE;
60
Al Viro59551022016-01-22 15:40:57 -050061 inode_lock(upperdentry->d_inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040062 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020063 err = notify_change(upperdentry, attr, NULL);
Vivek Goyal1175b6b2016-07-01 16:34:28 -040064 revert_creds(old_cred);
Konstantin Khlebnikovb81de062016-01-31 16:21:29 +030065 if (!err)
66 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
Al Viro59551022016-01-22 15:40:57 -050067 inode_unlock(upperdentry->d_inode);
Miklos Szeredi58121602018-07-18 15:44:41 +020068
69 if (winode)
70 put_write_access(winode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020071 }
Miklos Szeredi58121602018-07-18 15:44:41 +020072out_drop_write:
Miklos Szeredie9be9d52014-10-24 00:14:38 +020073 ovl_drop_write(dentry);
74out:
75 return err;
76}
77
Amir Goldstein07f1e592020-01-14 21:59:22 +020078static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, int fsid)
Amir Goldsteinda309e82017-11-08 19:39:51 +020079{
Amir Goldstein0f831ec2019-11-16 18:14:41 +020080 bool samefs = ovl_same_fs(dentry->d_sb);
Amir Goldsteine487d882017-11-07 13:55:04 +020081 unsigned int xinobits = ovl_xino_bits(dentry->d_sb);
Amir Goldsteinda309e82017-11-08 19:39:51 +020082
83 if (samefs) {
84 /*
85 * When all layers are on the same fs, all real inode
86 * number are unique, so we use the overlay st_dev,
87 * which is friendly to du -x.
88 */
89 stat->dev = dentry->d_sb->s_dev;
Amir Goldsteine487d882017-11-07 13:55:04 +020090 return 0;
91 } else if (xinobits) {
92 unsigned int shift = 64 - xinobits;
93 /*
94 * All inode numbers of underlying fs should not be using the
95 * high xinobits, so we use high xinobits to partition the
96 * overlay st_ino address space. The high bits holds the fsid
97 * (upper fsid is 0). This way overlay inode numbers are unique
98 * and all inodes use overlay st_dev. Inode numbers are also
99 * persistent for a given layer configuration.
100 */
101 if (stat->ino >> shift) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800102 pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
Amir Goldsteine487d882017-11-07 13:55:04 +0200103 dentry, stat->ino, xinobits);
104 } else {
Amir Goldstein07f1e592020-01-14 21:59:22 +0200105 stat->ino |= ((u64)fsid) << shift;
Amir Goldsteine487d882017-11-07 13:55:04 +0200106 stat->dev = dentry->d_sb->s_dev;
107 return 0;
108 }
109 }
110
111 /* The inode could not be mapped to a unified st_ino address space */
112 if (S_ISDIR(dentry->d_inode->i_mode)) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200113 /*
114 * Always use the overlay st_dev for directories, so 'find
115 * -xdev' will scan the entire overlay mount and won't cross the
116 * overlay mount boundaries.
117 *
118 * If not all layers are on the same fs the pair {real st_ino;
119 * overlay st_dev} is not unique, so use the non persistent
120 * overlay st_ino for directories.
121 */
122 stat->dev = dentry->d_sb->s_dev;
123 stat->ino = dentry->d_inode->i_ino;
Amir Goldsteinb7bf9902020-01-14 22:17:25 +0200124 } else {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200125 /*
126 * For non-samefs setup, if we cannot map all layers st_ino
127 * to a unified address space, we need to make sure that st_dev
Amir Goldsteinb7bf9902020-01-14 22:17:25 +0200128 * is unique per underlying fs, so we use the unique anonymous
129 * bdev assigned to the underlying fs.
Amir Goldsteinda309e82017-11-08 19:39:51 +0200130 */
Amir Goldstein07f1e592020-01-14 21:59:22 +0200131 stat->dev = OVL_FS(dentry->d_sb)->fs[fsid].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 Goldstein07f1e592020-01-14 21:59:22 +0200145 int fsid = 0;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400146 int err;
Vivek Goyal67d756c22018-05-11 11:49:30 -0400147 bool metacopy_blocks = false;
148
149 metacopy_blocks = ovl_is_metacopy_dentry(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200150
Amir Goldstein72b608f2017-04-24 00:25:49 +0300151 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400152 old_cred = ovl_override_creds(dentry->d_sb);
David Howellsa528d352017-01-31 16:46:22 +0000153 err = vfs_getattr(&realpath, stat, request_mask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300154 if (err)
155 goto out;
156
157 /*
Amir Goldsteinda309e82017-11-08 19:39:51 +0200158 * For non-dir or same fs, we use st_ino of the copy up origin.
159 * This guaranties constant st_dev/st_ino across copy up.
Amir Goldsteine487d882017-11-07 13:55:04 +0200160 * With xino feature and non-samefs, we use st_ino of the copy up
161 * origin masked with high bits that represent the layer id.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300162 *
Amir Goldsteinda309e82017-11-08 19:39:51 +0200163 * If lower filesystem supports NFS file handles, this also guaranties
Amir Goldstein72b608f2017-04-24 00:25:49 +0300164 * persistent st_ino across mount cycle.
165 */
Amir Goldstein0f831ec2019-11-16 18:14:41 +0200166 if (!is_dir || ovl_same_dev(dentry->d_sb)) {
Amir Goldsteinda309e82017-11-08 19:39:51 +0200167 if (!OVL_TYPE_UPPER(type)) {
Amir Goldstein07f1e592020-01-14 21:59:22 +0200168 fsid = ovl_layer_lower(dentry)->fsid;
Amir Goldsteinda309e82017-11-08 19:39:51 +0200169 } else if (OVL_TYPE_ORIGIN(type)) {
Amir Goldstein72b608f2017-04-24 00:25:49 +0300170 struct kstat lowerstat;
Vivek Goyal67d756c22018-05-11 11:49:30 -0400171 u32 lowermask = STATX_INO | STATX_BLOCKS |
172 (!is_dir ? STATX_NLINK : 0);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300173
174 ovl_path_lower(dentry, &realpath);
175 err = vfs_getattr(&realpath, &lowerstat,
Miklos Szeredi5b712092017-05-05 11:38:58 +0200176 lowermask, flags);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300177 if (err)
178 goto out;
179
Amir Goldstein72b608f2017-04-24 00:25:49 +0300180 /*
Amir Goldstein359f3922017-06-21 15:28:41 +0300181 * Lower hardlinks may be broken on copy up to different
Amir Goldstein72b608f2017-04-24 00:25:49 +0300182 * upper files, so we cannot use the lower origin st_ino
183 * for those different files, even for the same fs case.
Amir Goldstein86eaa132017-11-21 13:55:51 +0200184 *
185 * Similarly, several redirected dirs can point to the
186 * same dir on a lower layer. With the "verify_lower"
187 * feature, we do not use the lower origin st_ino, if
188 * we haven't verified that this redirect is unique.
189 *
Amir Goldstein359f3922017-06-21 15:28:41 +0300190 * With inodes index enabled, it is safe to use st_ino
Amir Goldstein86eaa132017-11-21 13:55:51 +0200191 * of an indexed origin. The index validates that the
192 * upper hardlink is not broken and that a redirected
193 * dir is the only redirect to that origin.
Amir Goldstein72b608f2017-04-24 00:25:49 +0300194 */
Amir Goldstein86eaa132017-11-21 13:55:51 +0200195 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
196 (!ovl_verify_lower(dentry->d_sb) &&
Amir Goldstein9f99e502018-04-11 20:09:29 +0300197 (is_dir || lowerstat.nlink == 1))) {
Amir Goldstein07f1e592020-01-14 21:59:22 +0200198 fsid = ovl_layer_lower(dentry)->fsid;
Amir Goldsteinb7bf9902020-01-14 22:17:25 +0200199 stat->ino = lowerstat.ino;
Amir Goldstein9f99e502018-04-11 20:09:29 +0300200 }
Vivek Goyal67d756c22018-05-11 11:49:30 -0400201
202 /*
203 * If we are querying a metacopy dentry and lower
204 * dentry is data dentry, then use the blocks we
205 * queried just now. We don't have to do additional
206 * vfs_getattr(). If lower itself is metacopy, then
207 * additional vfs_getattr() is unavoidable.
208 */
209 if (metacopy_blocks &&
210 realpath.dentry == ovl_dentry_lowerdata(dentry)) {
211 stat->blocks = lowerstat.blocks;
212 metacopy_blocks = false;
213 }
214 }
215
216 if (metacopy_blocks) {
217 /*
218 * If lower is not same as lowerdata or if there was
219 * no origin on upper, we can end up here.
220 */
221 struct kstat lowerdatastat;
222 u32 lowermask = STATX_BLOCKS;
223
224 ovl_path_lowerdata(dentry, &realpath);
225 err = vfs_getattr(&realpath, &lowerdatastat,
226 lowermask, flags);
227 if (err)
228 goto out;
229 stat->blocks = lowerdatastat.blocks;
Amir Goldstein72b608f2017-04-24 00:25:49 +0300230 }
Amir Goldstein72b608f2017-04-24 00:25:49 +0300231 }
Miklos Szeredi5b712092017-05-05 11:38:58 +0200232
Amir Goldstein07f1e592020-01-14 21:59:22 +0200233 err = ovl_map_dev_ino(dentry, stat, fsid);
Amir Goldsteinda309e82017-11-08 19:39:51 +0200234 if (err)
235 goto out;
236
Miklos Szeredi5b712092017-05-05 11:38:58 +0200237 /*
238 * It's probably not worth it to count subdirs to get the
239 * correct link count. nlink=1 seems to pacify 'find' and
240 * other utilities.
241 */
242 if (is_dir && OVL_TYPE_MERGE(type))
243 stat->nlink = 1;
244
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300245 /*
246 * Return the overlay inode nlinks for indexed upper inodes.
247 * Overlay inode nlink counts the union of the upper hardlinks
248 * and non-covered lower hardlinks. It does not include the upper
249 * index hardlink.
250 */
251 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
252 stat->nlink = dentry->d_inode->i_nlink;
253
Amir Goldstein72b608f2017-04-24 00:25:49 +0300254out:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400255 revert_creds(old_cred);
Amir Goldstein72b608f2017-04-24 00:25:49 +0300256
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400257 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200258}
259
260int ovl_permission(struct inode *inode, int mask)
261{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200262 struct inode *upperinode = ovl_inode_upper(inode);
263 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400264 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200265 int err;
266
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200267 /* Careful in RCU walk mode */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200268 if (!realinode) {
269 WARN_ON(!(mask & MAY_NOT_BLOCK));
Miklos Szeredia999d7e2016-07-29 12:05:23 +0200270 return -ECHILD;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200271 }
272
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400273 /*
274 * Check overlay inode with the creds of task and underlying inode
275 * with creds of mounter
276 */
277 err = generic_permission(inode, mask);
278 if (err)
279 return err;
280
Miklos Szerediec7ba112018-12-04 11:31:30 +0100281 old_cred = ovl_override_creds(inode->i_sb);
282 if (!upperinode &&
283 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
Vivek Goyal754f8cb2016-07-01 16:34:29 -0400284 mask &= ~(MAY_WRITE | MAY_APPEND);
Miklos Szerediec7ba112018-12-04 11:31:30 +0100285 /* Make sure mounter can read file for copy up later */
Vivek Goyal500cac32016-07-13 11:00:14 -0400286 mask |= MAY_READ;
287 }
Miklos Szeredi9c630eb2016-07-29 12:05:23 +0200288 err = inode_permission(realinode, mask);
Vivek Goyalc0ca3d72016-07-01 16:34:27 -0400289 revert_creds(old_cred);
290
291 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200292}
293
Al Viro6b255392015-11-17 10:20:54 -0500294static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500295 struct inode *inode,
296 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200297{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400298 const struct cred *old_cred;
299 const char *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200300
Al Viro6b255392015-11-17 10:20:54 -0500301 if (!dentry)
302 return ERR_PTR(-ECHILD);
303
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400304 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200305 p = vfs_get_link(ovl_dentry_real(dentry), done);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400306 revert_creds(old_cred);
307 return p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200308}
309
Miklos Szeredi09562542016-08-08 15:08:49 +0200310bool ovl_is_private_xattr(const char *name)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200311{
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +0200312 return strncmp(name, OVL_XATTR_PREFIX,
313 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200314}
315
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200316int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
317 const void *value, size_t size, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200318{
319 int err;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200320 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
321 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400322 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200323
324 err = ovl_want_write(dentry);
325 if (err)
326 goto out;
327
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200328 if (!value && !upperdentry) {
329 err = vfs_getxattr(realdentry, name, NULL, 0);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200330 if (err < 0)
331 goto out_drop_write;
332 }
333
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200334 if (!upperdentry) {
335 err = ovl_copy_up(dentry);
336 if (err)
337 goto out_drop_write;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200338
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200339 realdentry = ovl_dentry_upper(dentry);
340 }
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200341
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400342 old_cred = ovl_override_creds(dentry->d_sb);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200343 if (value)
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200344 err = vfs_setxattr(realdentry, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200345 else {
346 WARN_ON(flags != XATTR_REPLACE);
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200347 err = vfs_removexattr(realdentry, name);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200348 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400349 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200350
Miklos Szeredid9854c82018-07-18 15:44:40 +0200351 /* copy c/mtime */
352 ovl_copyattr(d_inode(realdentry), inode);
353
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200354out_drop_write:
355 ovl_drop_write(dentry);
356out:
357 return err;
358}
359
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200360int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200361 void *value, size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200362{
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400363 ssize_t res;
364 const struct cred *old_cred;
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200365 struct dentry *realdentry =
366 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
Miklos Szeredi52148462014-11-20 16:40:00 +0100367
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400368 old_cred = ovl_override_creds(dentry->d_sb);
369 res = vfs_getxattr(realdentry, name, value, size);
370 revert_creds(old_cred);
371 return res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200372}
373
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200374static bool ovl_can_list(const char *s)
375{
376 /* List all non-trusted xatts */
377 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
378 return true;
379
380 /* Never list trusted.overlay, list other trusted for superuser only */
Mark Salyzyn5c2e9f32019-08-29 11:30:14 -0700381 return !ovl_is_private_xattr(s) &&
382 ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN);
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200383}
384
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200385ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
386{
Miklos Szeredib5817552016-06-06 16:21:37 +0200387 struct dentry *realdentry = ovl_dentry_real(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200388 ssize_t res;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200389 size_t len;
390 char *s;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400391 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200392
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400393 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredib5817552016-06-06 16:21:37 +0200394 res = vfs_listxattr(realdentry, list, size);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400395 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200396 if (res <= 0 || size == 0)
397 return res;
398
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200399 /* filter out private xattrs */
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200400 for (s = list, len = res; len;) {
401 size_t slen = strnlen(s, len) + 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200402
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200403 /* underlying fs providing us with an broken xattr list? */
404 if (WARN_ON(slen > len))
405 return -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200406
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200407 len -= slen;
Miklos Szeredia082c6f2017-05-29 15:15:27 +0200408 if (!ovl_can_list(s)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200409 res -= slen;
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200410 memmove(s, s + slen, len);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200411 } else {
Miklos Szeredi7cb35112016-09-01 11:12:00 +0200412 s += slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200413 }
414 }
415
416 return res;
417}
418
Vivek Goyal39a25b22016-07-01 16:34:26 -0400419struct posix_acl *ovl_get_acl(struct inode *inode, int type)
420{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200421 struct inode *realinode = ovl_inode_real(inode);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400422 const struct cred *old_cred;
423 struct posix_acl *acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400424
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200425 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
Vivek Goyal39a25b22016-07-01 16:34:26 -0400426 return NULL;
427
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400428 old_cred = ovl_override_creds(inode->i_sb);
Miklos Szeredi5201dc42016-09-01 11:11:59 +0200429 acl = get_acl(realinode, type);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400430 revert_creds(old_cred);
431
432 return acl;
Vivek Goyal39a25b22016-07-01 16:34:26 -0400433}
434
Deepa Dinamani95582b02018-05-08 19:36:02 -0700435int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200436{
Miklos Szeredi8f35cf52018-04-12 12:04:50 +0200437 if (flags & S_ATIME) {
438 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
439 struct path upperpath = {
440 .mnt = ofs->upper_mnt,
441 .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
442 };
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200443
Miklos Szeredi8f35cf52018-04-12 12:04:50 +0200444 if (upperpath.dentry) {
445 touch_atime(&upperpath);
446 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
447 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200448 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200449 return 0;
450}
451
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200452static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
453 u64 start, u64 len)
454{
455 int err;
456 struct inode *realinode = ovl_inode_real(inode);
457 const struct cred *old_cred;
458
459 if (!realinode->i_op->fiemap)
460 return -EOPNOTSUPP;
461
462 old_cred = ovl_override_creds(inode->i_sb);
Amir Goldstein80d34812018-08-27 15:55:59 +0300463
464 if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC)
465 filemap_write_and_wait(realinode->i_mapping);
466
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200467 err = realinode->i_op->fiemap(realinode, fieinfo, start, len);
468 revert_creds(old_cred);
469
470 return err;
471}
472
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200473static const struct inode_operations ovl_file_inode_operations = {
474 .setattr = ovl_setattr,
475 .permission = ovl_permission,
476 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200477 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400478 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200479 .update_time = ovl_update_time,
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200480 .fiemap = ovl_fiemap,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200481};
482
483static const struct inode_operations ovl_symlink_inode_operations = {
484 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500485 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200486 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200487 .listxattr = ovl_listxattr,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200488 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200489};
490
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200491static const struct inode_operations ovl_special_inode_operations = {
492 .setattr = ovl_setattr,
493 .permission = ovl_permission,
494 .getattr = ovl_getattr,
495 .listxattr = ovl_listxattr,
496 .get_acl = ovl_get_acl,
497 .update_time = ovl_update_time,
498};
499
Wei Yongjun69383c52018-09-25 14:57:42 +0000500static const struct address_space_operations ovl_aops = {
Amir Goldstein5b910bd2018-08-27 15:56:00 +0300501 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
502 .direct_IO = noop_direct_IO,
503};
504
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200505/*
506 * It is possible to stack overlayfs instance on top of another
507 * overlayfs instance as lower layer. We need to annonate the
508 * stackable i_mutex locks according to stack level of the super
509 * block instance. An overlayfs instance can never be in stack
510 * depth 0 (there is always a real fs below it). An overlayfs
511 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
512 *
513 * For example, here is a snip from /proc/lockdep_chains after
514 * dir_iterate of nested overlayfs:
515 *
516 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
517 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
518 * [...] &type->i_mutex_dir_key (stack_depth=0)
Amir Goldsteinb1f9d382019-12-21 11:42:29 +0200519 *
520 * Locking order w.r.t ovl_want_write() is important for nested overlayfs.
521 *
522 * This chain is valid:
523 * - inode->i_rwsem (inode_lock[2])
524 * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
525 * - OVL_I(inode)->lock (ovl_inode_lock[2])
526 * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
527 *
528 * And this chain is valid:
529 * - inode->i_rwsem (inode_lock[2])
530 * - OVL_I(inode)->lock (ovl_inode_lock[2])
531 * - lowerinode->i_rwsem (inode_lock[1])
532 * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
533 *
534 * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is
535 * held, because it is in reverse order of the non-nested case using the same
536 * upper fs:
537 * - inode->i_rwsem (inode_lock[1])
538 * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
539 * - OVL_I(inode)->lock (ovl_inode_lock[1])
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200540 */
541#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
542
543static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
544{
545#ifdef CONFIG_LOCKDEP
546 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
547 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300548 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200549
550 int depth = inode->i_sb->s_stack_depth - 1;
551
552 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
553 depth = 0;
554
555 if (S_ISDIR(inode->i_mode))
556 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
557 else
558 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
Amir Goldstein4eae06d2017-10-27 15:44:08 +0300559
560 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200561#endif
562}
563
Amir Goldstein695b46e2018-03-15 23:39:01 +0200564static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
Amir Goldstein12574a92018-03-16 10:39:37 +0200565 unsigned long ino, int fsid)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200566{
Amir Goldstein12574a92018-03-16 10:39:37 +0200567 int xinobits = ovl_xino_bits(inode->i_sb);
568
Amir Goldstein695b46e2018-03-15 23:39:01 +0200569 /*
Amir Goldstein6dde1e42019-06-09 19:03:44 +0300570 * When d_ino is consistent with st_ino (samefs or i_ino has enough
571 * bits to encode layer), set the same value used for st_ino to i_ino,
572 * so inode number exposed via /proc/locks and a like will be
573 * consistent with d_ino and st_ino values. An i_ino value inconsistent
574 * with d_ino also causes nfsd readdirplus to fail. When called from
Amir Goldstein12574a92018-03-16 10:39:37 +0200575 * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
Amir Goldstein695b46e2018-03-15 23:39:01 +0200576 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
577 */
Amir Goldstein0f831ec2019-11-16 18:14:41 +0200578 if (ovl_same_dev(inode->i_sb)) {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200579 inode->i_ino = ino;
Amir Goldstein12574a92018-03-16 10:39:37 +0200580 if (xinobits && fsid && !(ino >> (64 - xinobits)))
581 inode->i_ino |= (unsigned long)fsid << (64 - xinobits);
582 } else {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200583 inode->i_ino = get_next_ino();
Amir Goldstein12574a92018-03-16 10:39:37 +0200584 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200585 inode->i_mode = mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200586 inode->i_flags |= S_NOCMTIME;
Miklos Szeredi2a3a2a32016-09-01 11:11:59 +0200587#ifdef CONFIG_FS_POSIX_ACL
588 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
589#endif
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200590
Amir Goldsteinb1eaa952017-01-11 12:07:46 +0200591 ovl_lockdep_annotate_inode_mutex_key(inode);
592
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100593 switch (mode & S_IFMT) {
594 case S_IFREG:
595 inode->i_op = &ovl_file_inode_operations;
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200596 inode->i_fop = &ovl_file_operations;
Amir Goldstein5b910bd2018-08-27 15:56:00 +0300597 inode->i_mapping->a_ops = &ovl_aops;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100598 break;
599
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200600 case S_IFDIR:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200601 inode->i_op = &ovl_dir_inode_operations;
602 inode->i_fop = &ovl_dir_operations;
603 break;
604
605 case S_IFLNK:
606 inode->i_op = &ovl_symlink_inode_operations;
607 break;
608
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200609 default:
Miklos Szeredi9e142c42018-07-18 15:44:42 +0200610 inode->i_op = &ovl_special_inode_operations;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100611 init_special_inode(inode, mode, rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200612 break;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200613 }
614}
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200615
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300616/*
617 * With inodes index enabled, an overlay inode nlink counts the union of upper
618 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
619 * upper inode, the following nlink modifying operations can happen:
620 *
621 * 1. Lower hardlink copy up
622 * 2. Upper hardlink created, unlinked or renamed over
623 * 3. Lower hardlink whiteout or renamed over
624 *
625 * For the first, copy up case, the union nlink does not change, whether the
626 * operation succeeds or fails, but the upper inode nlink may change.
627 * Therefore, before copy up, we store the union nlink value relative to the
628 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
629 *
630 * For the second, upper hardlink case, the union nlink should be incremented
631 * or decremented IFF the operation succeeds, aligned with nlink change of the
632 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
633 * value relative to the upper inode nlink in the index inode.
634 *
635 * For the last, lower cover up case, we simplify things by preceding the
636 * whiteout or cover up with copy up. This makes sure that there is an index
637 * upper inode where the nlink xattr can be stored before the copied up upper
638 * entry is unlink.
639 */
640#define OVL_NLINK_ADD_UPPER (1 << 0)
641
642/*
643 * On-disk format for indexed nlink:
644 *
645 * nlink relative to the upper inode - "U[+-]NUM"
646 * nlink relative to the lower inode - "L[+-]NUM"
647 */
648
649static int ovl_set_nlink_common(struct dentry *dentry,
650 struct dentry *realdentry, const char *format)
651{
652 struct inode *inode = d_inode(dentry);
653 struct inode *realinode = d_inode(realdentry);
654 char buf[13];
655 int len;
656
657 len = snprintf(buf, sizeof(buf), format,
658 (int) (inode->i_nlink - realinode->i_nlink));
659
Miklos Szeredi67873412017-07-27 21:54:05 +0200660 if (WARN_ON(len >= sizeof(buf)))
661 return -EIO;
662
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300663 return ovl_do_setxattr(ovl_dentry_upper(dentry),
664 OVL_XATTR_NLINK, buf, len, 0);
665}
666
667int ovl_set_nlink_upper(struct dentry *dentry)
668{
669 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
670}
671
672int ovl_set_nlink_lower(struct dentry *dentry)
673{
674 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
675}
676
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300677unsigned int ovl_get_nlink(struct dentry *lowerdentry,
678 struct dentry *upperdentry,
679 unsigned int fallback)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300680{
681 int nlink_diff;
682 int nlink;
683 char buf[13];
684 int err;
685
686 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
687 return fallback;
688
689 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
690 if (err < 0)
691 goto fail;
692
693 buf[err] = '\0';
694 if ((buf[0] != 'L' && buf[0] != 'U') ||
695 (buf[1] != '+' && buf[1] != '-'))
696 goto fail;
697
698 err = kstrtoint(buf + 1, 10, &nlink_diff);
699 if (err < 0)
700 goto fail;
701
702 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
703 nlink += nlink_diff;
704
705 if (nlink <= 0)
706 goto fail;
707
708 return nlink;
709
710fail:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800711 pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n",
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300712 upperdentry, err);
713 return fallback;
714}
715
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100716struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200717{
718 struct inode *inode;
719
720 inode = new_inode(sb);
721 if (inode)
Amir Goldstein12574a92018-03-16 10:39:37 +0200722 ovl_fill_inode(inode, mode, rdev, 0, 0);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200723
724 return inode;
725}
726
727static int ovl_inode_test(struct inode *inode, void *data)
728{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200729 return inode->i_private == data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200730}
731
732static int ovl_inode_set(struct inode *inode, void *data)
733{
Miklos Szeredi25b77132017-07-04 22:03:16 +0200734 inode->i_private = data;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200735 return 0;
736}
737
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200738static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
Amir Goldstein4b91c302018-01-18 16:39:13 +0200739 struct dentry *upperdentry, bool strict)
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200740{
Amir Goldstein4b91c302018-01-18 16:39:13 +0200741 /*
742 * For directories, @strict verify from lookup path performs consistency
743 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
744 * inode. Non @strict verify from NFS handle decode path passes NULL for
745 * 'unknown' lower/upper.
746 */
747 if (S_ISDIR(inode->i_mode) && strict) {
Amir Goldstein31747ed2018-01-14 18:35:40 +0200748 /* Real lower dir moved to upper layer under us? */
749 if (!lowerdentry && ovl_inode_lower(inode))
750 return false;
751
752 /* Lookup of an uncovered redirect origin? */
753 if (!upperdentry && ovl_inode_upper(inode))
754 return false;
755 }
756
Amir Goldstein939ae4e2017-09-11 16:30:15 +0300757 /*
758 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
759 * This happens when finding a copied up overlay inode for a renamed
760 * or hardlinked overlay dentry and lower dentry cannot be followed
761 * by origin because lower fs does not support file handles.
762 */
763 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200764 return false;
765
766 /*
767 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
768 * This happens when finding a lower alias for a copied up hard link.
769 */
770 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
771 return false;
772
773 return true;
774}
775
Amir Goldstein4b91c302018-01-18 16:39:13 +0200776struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
777 bool is_upper)
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200778{
Amir Goldstein4b91c302018-01-18 16:39:13 +0200779 struct inode *inode, *key = d_inode(real);
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200780
781 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
782 if (!inode)
783 return NULL;
784
Amir Goldstein4b91c302018-01-18 16:39:13 +0200785 if (!ovl_verify_inode(inode, is_upper ? NULL : real,
786 is_upper ? real : NULL, false)) {
Amir Goldstein9436a1a2017-12-24 18:28:04 +0200787 iput(inode);
788 return ERR_PTR(-ESTALE);
789 }
790
791 return inode;
792}
793
Amir Goldstein146d62e2019-04-18 17:42:08 +0300794bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir)
795{
796 struct inode *key = d_inode(dir);
797 struct inode *trap;
798 bool res;
799
800 trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
801 if (!trap)
802 return false;
803
804 res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) &&
805 !ovl_inode_lower(trap);
806
807 iput(trap);
808 return res;
809}
810
811/*
812 * Create an inode cache entry for layer root dir, that will intentionally
813 * fail ovl_verify_inode(), so any lookup that will find some layer root
814 * will fail.
815 */
816struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
817{
818 struct inode *key = d_inode(dir);
819 struct inode *trap;
820
821 if (!d_is_dir(dir))
822 return ERR_PTR(-ENOTDIR);
823
824 trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
825 ovl_inode_set, key);
826 if (!trap)
827 return ERR_PTR(-ENOMEM);
828
829 if (!(trap->i_state & I_NEW)) {
830 /* Conflicting layer roots? */
831 iput(trap);
832 return ERR_PTR(-ELOOP);
833 }
834
835 trap->i_mode = S_IFDIR;
836 trap->i_flags = S_DEAD;
837 unlock_new_inode(trap);
838
839 return trap;
840}
841
Amir Goldstein764baba2018-02-04 15:35:09 +0200842/*
843 * Does overlay inode need to be hashed by lower inode?
844 */
845static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
846 struct dentry *lower, struct dentry *index)
847{
848 struct ovl_fs *ofs = sb->s_fs_info;
849
850 /* No, if pure upper */
851 if (!lower)
852 return false;
853
854 /* Yes, if already indexed */
855 if (index)
856 return true;
857
858 /* Yes, if won't be copied up */
859 if (!ofs->upper_mnt)
860 return true;
861
862 /* No, if lower hardlink is or will be broken on copy up */
863 if ((upper || !ovl_indexdir(sb)) &&
864 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
865 return false;
866
867 /* No, if non-indexed upper with NFS export */
868 if (sb->s_export_op && upper)
869 return false;
870
871 /* Otherwise, hash by lower inode for fsnotify */
872 return true;
873}
874
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300875static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
876 struct inode *key)
877{
878 return newinode ? inode_insert5(newinode, (unsigned long) key,
879 ovl_inode_test, ovl_inode_set, key) :
880 iget5_locked(sb, (unsigned long) key,
881 ovl_inode_test, ovl_inode_set, key);
882}
883
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400884struct inode *ovl_get_inode(struct super_block *sb,
885 struct ovl_inode_params *oip)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200886{
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400887 struct dentry *upperdentry = oip->upperdentry;
888 struct ovl_path *lowerpath = oip->lowerpath;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200889 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200890 struct inode *inode;
Amir Goldstein12574a92018-03-16 10:39:37 +0200891 struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400892 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
893 oip->index);
894 int fsid = bylower ? oip->lowerpath->layer->fsid : 0;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400895 bool is_dir, metacopy = false;
Amir Goldstein695b46e2018-03-15 23:39:01 +0200896 unsigned long ino = 0;
Amir Goldsteinacf30622019-03-28 17:38:29 +0200897 int err = oip->newinode ? -EEXIST : -ENOMEM;
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300898
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200899 if (!realinode)
900 realinode = d_inode(lowerdentry);
901
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300902 /*
Amir Goldstein764baba2018-02-04 15:35:09 +0200903 * Copy up origin (lower) may exist for non-indexed upper, but we must
904 * not use lower as hash key if this is a broken hardlink.
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300905 */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200906 is_dir = S_ISDIR(realinode->i_mode);
Amir Goldstein764baba2018-02-04 15:35:09 +0200907 if (upperdentry || bylower) {
908 struct inode *key = d_inode(bylower ? lowerdentry :
909 upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200910 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200911
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300912 inode = ovl_iget5(sb, oip->newinode, key);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200913 if (!inode)
Vivek Goyal027065b2018-05-11 11:49:28 -0400914 goto out_err;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200915 if (!(inode->i_state & I_NEW)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200916 /*
917 * Verify that the underlying files stored in the inode
918 * match those in the dentry.
919 */
Amir Goldstein4b91c302018-01-18 16:39:13 +0200920 if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
921 true)) {
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200922 iput(inode);
Vivek Goyal027065b2018-05-11 11:49:28 -0400923 err = -ESTALE;
924 goto out_err;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200925 }
926
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200927 dput(upperdentry);
Vivek Goyal9cec54c2018-05-11 11:49:27 -0400928 kfree(oip->redirect);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200929 goto out;
930 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200931
Amir Goldstein31747ed2018-01-14 18:35:40 +0200932 /* Recalculate nlink for non-dir due to indexing */
933 if (!is_dir)
934 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300935 set_nlink(inode, nlink);
Amir Goldstein695b46e2018-03-15 23:39:01 +0200936 ino = key->i_ino;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200937 } else {
Amir Goldstein764baba2018-02-04 15:35:09 +0200938 /* Lower hardlink that will be broken on copy up */
Amir Goldstein0aceb532017-12-12 23:43:16 +0200939 inode = new_inode(sb);
Vivek Goyal027065b2018-05-11 11:49:28 -0400940 if (!inode) {
941 err = -ENOMEM;
942 goto out_err;
943 }
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200944 }
Amir Goldstein12574a92018-03-16 10:39:37 +0200945 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid);
Vivek Goyal2664bd02018-05-11 11:49:30 -0400946 ovl_inode_init(inode, upperdentry, lowerdentry, oip->lowerdata);
Miklos Szeredi13c72072017-07-04 22:03:16 +0200947
948 if (upperdentry && ovl_is_impuredir(upperdentry))
949 ovl_set_flag(OVL_IMPURE, inode);
950
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400951 if (oip->index)
Vivek Goyal0471a9c2018-03-20 16:35:40 -0400952 ovl_set_flag(OVL_INDEX, inode);
953
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400954 if (upperdentry) {
955 err = ovl_check_metacopy_xattr(upperdentry);
956 if (err < 0)
957 goto out_err;
958 metacopy = err;
959 if (!metacopy)
960 ovl_set_flag(OVL_UPPERDATA, inode);
961 }
962
Vivek Goyal9cec54c2018-05-11 11:49:27 -0400963 OVL_I(inode)->redirect = oip->redirect;
964
Vivek Goyala00c2d52018-05-11 11:49:32 -0400965 if (bylower)
966 ovl_set_flag(OVL_CONST_INO, inode);
967
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300968 /* Check for non-merge dir that may have whiteouts */
Amir Goldstein31747ed2018-01-14 18:35:40 +0200969 if (is_dir) {
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400970 if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300971 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
972 ovl_set_flag(OVL_WHITEOUTS, inode);
973 }
974 }
975
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200976 if (inode->i_state & I_NEW)
977 unlock_new_inode(inode);
978out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200979 return inode;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200980
Vivek Goyal027065b2018-05-11 11:49:28 -0400981out_err:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800982 pr_warn_ratelimited("failed to get inode (%i)\n", err);
Vivek Goyal027065b2018-05-11 11:49:28 -0400983 inode = ERR_PTR(err);
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200984 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200985}