blob: 5edb177b63dc3387accc1b1287ce0540efd6a19e [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Miklos Szeredibbb1e542016-12-16 11:02:56 +01002/*
3 * Copyright (C) 2011 Novell Inc.
4 * Copyright (C) 2016 Red Hat, Inc.
Miklos Szeredibbb1e542016-12-16 11:02:56 +01005 */
6
7#include <linux/fs.h>
8#include <linux/mount.h>
9#include <linux/slab.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010010#include <linux/cred.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010011#include <linux/xattr.h>
Amir Goldstein02bcd152017-06-21 15:28:36 +030012#include <linux/exportfs.h>
13#include <linux/uuid.h>
Amir Goldsteincaf70cb2017-06-21 13:46:12 +030014#include <linux/namei.h>
15#include <linux/ratelimit.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010016#include "overlayfs.h"
Miklos Szeredibbb1e542016-12-16 11:02:56 +010017
18int ovl_want_write(struct dentry *dentry)
19{
20 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +020021 return mnt_want_write(ovl_upper_mnt(ofs));
Miklos Szeredibbb1e542016-12-16 11:02:56 +010022}
23
24void ovl_drop_write(struct dentry *dentry)
25{
26 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +020027 mnt_drop_write(ovl_upper_mnt(ofs));
Miklos Szeredibbb1e542016-12-16 11:02:56 +010028}
29
30struct dentry *ovl_workdir(struct dentry *dentry)
31{
32 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
33 return ofs->workdir;
34}
35
36const struct cred *ovl_override_creds(struct super_block *sb)
37{
38 struct ovl_fs *ofs = sb->s_fs_info;
39
Mark Salyzyn6120a4d2018-06-14 11:15:22 -070040 if (!ofs->config.override_creds)
41 return NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +010042 return override_creds(ofs->creator_cred);
43}
44
Mark Salyzyn6120a4d2018-06-14 11:15:22 -070045void ovl_revert_creds(struct super_block *sb, const struct cred *old_cred)
46{
47 if (old_cred)
48 revert_creds(old_cred);
49}
50
Amir Goldsteine487d882017-11-07 13:55:04 +020051/*
52 * Check if underlying fs supports file handles and try to determine encoding
53 * type, in order to deduce maximum inode number used by fs.
54 *
55 * Return 0 if file handles are not supported.
56 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
57 * Return -1 if fs uses a non default encoding with unknown inode size.
58 */
59int ovl_can_decode_fh(struct super_block *sb)
Amir Goldstein02bcd152017-06-21 15:28:36 +030060{
Amir Goldstein9df085f2018-09-03 09:12:09 +030061 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
Amir Goldsteine487d882017-11-07 13:55:04 +020062 return 0;
63
64 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
Amir Goldstein02bcd152017-06-21 15:28:36 +030065}
66
67struct dentry *ovl_indexdir(struct super_block *sb)
68{
69 struct ovl_fs *ofs = sb->s_fs_info;
70
71 return ofs->indexdir;
72}
73
Amir Goldsteinf168f102018-01-19 11:26:53 +020074/* Index all files on copy up. For now only enabled for NFS export */
75bool ovl_index_all(struct super_block *sb)
76{
77 struct ovl_fs *ofs = sb->s_fs_info;
78
79 return ofs->config.nfs_export && ofs->config.index;
80}
81
82/* Verify lower origin on lookup. For now only enabled for NFS export */
83bool ovl_verify_lower(struct super_block *sb)
84{
85 struct ovl_fs *ofs = sb->s_fs_info;
86
87 return ofs->config.nfs_export && ofs->config.index;
88}
89
Miklos Szeredibbb1e542016-12-16 11:02:56 +010090struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
91{
92 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
93 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
94
95 if (oe)
96 oe->numlower = numlower;
97
98 return oe;
99}
100
101bool ovl_dentry_remote(struct dentry *dentry)
102{
103 return dentry->d_flags &
Miklos Szeredi7925dad2020-03-17 15:04:22 +0100104 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100105}
106
Miklos Szeredif4288842020-03-17 15:04:22 +0100107void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
108 unsigned int mask)
109{
110 struct ovl_entry *oe = OVL_E(dentry);
111 unsigned int i, flags = 0;
112
Miklos Szeredibccece12020-03-17 15:04:22 +0100113 if (upperdentry)
114 flags |= upperdentry->d_flags;
Miklos Szeredif4288842020-03-17 15:04:22 +0100115 for (i = 0; i < oe->numlower; i++)
116 flags |= oe->lowerstack[i].dentry->d_flags;
117
118 spin_lock(&dentry->d_lock);
119 dentry->d_flags &= ~mask;
120 dentry->d_flags |= flags & mask;
121 spin_unlock(&dentry->d_lock);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100122}
123
124bool ovl_dentry_weird(struct dentry *dentry)
125{
126 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
127 DCACHE_MANAGE_TRANSIT |
128 DCACHE_OP_HASH |
129 DCACHE_OP_COMPARE);
130}
131
132enum ovl_path_type ovl_path_type(struct dentry *dentry)
133{
134 struct ovl_entry *oe = dentry->d_fsdata;
135 enum ovl_path_type type = 0;
136
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200137 if (ovl_dentry_upper(dentry)) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100138 type = __OVL_PATH_UPPER;
139
140 /*
Amir Goldstein59548502017-04-23 23:12:34 +0300141 * Non-dir dentry can hold lower dentry of its copy up origin.
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100142 */
Amir Goldstein59548502017-04-23 23:12:34 +0300143 if (oe->numlower) {
Vivek Goyal60124872018-05-11 11:49:32 -0400144 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
145 type |= __OVL_PATH_ORIGIN;
Vivek Goyal0b17c282018-05-11 11:49:32 -0400146 if (d_is_dir(dentry) ||
147 !ovl_has_upperdata(d_inode(dentry)))
Amir Goldstein59548502017-04-23 23:12:34 +0300148 type |= __OVL_PATH_MERGE;
149 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100150 } else {
151 if (oe->numlower > 1)
152 type |= __OVL_PATH_MERGE;
153 }
154 return type;
155}
156
157void ovl_path_upper(struct dentry *dentry, struct path *path)
158{
159 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100160
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200161 path->mnt = ovl_upper_mnt(ofs);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200162 path->dentry = ovl_dentry_upper(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100163}
164
165void ovl_path_lower(struct dentry *dentry, struct path *path)
166{
167 struct ovl_entry *oe = dentry->d_fsdata;
168
Chandan Rajendrab9343632017-07-24 01:57:54 -0500169 if (oe->numlower) {
170 path->mnt = oe->lowerstack[0].layer->mnt;
171 path->dentry = oe->lowerstack[0].dentry;
172 } else {
173 *path = (struct path) { };
174 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100175}
176
Vivek Goyal4f93b422018-05-11 11:49:30 -0400177void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
178{
179 struct ovl_entry *oe = dentry->d_fsdata;
180
181 if (oe->numlower) {
182 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
183 path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
184 } else {
185 *path = (struct path) { };
186 }
187}
188
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100189enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
190{
191 enum ovl_path_type type = ovl_path_type(dentry);
192
193 if (!OVL_TYPE_UPPER(type))
194 ovl_path_lower(dentry, path);
195 else
196 ovl_path_upper(dentry, path);
197
198 return type;
199}
200
201struct dentry *ovl_dentry_upper(struct dentry *dentry)
202{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200203 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100204}
205
206struct dentry *ovl_dentry_lower(struct dentry *dentry)
207{
208 struct ovl_entry *oe = dentry->d_fsdata;
209
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200210 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100211}
212
Miklos Szeredi13464162020-01-24 09:46:45 +0100213const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
Amir Goldsteinda309e82017-11-08 19:39:51 +0200214{
215 struct ovl_entry *oe = dentry->d_fsdata;
216
217 return oe->numlower ? oe->lowerstack[0].layer : NULL;
218}
219
Vivek Goyal647d2532018-05-11 11:49:30 -0400220/*
221 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
222 * dependig on what is stored in lowerstack[0]. At times we need to find
223 * lower dentry which has data (and not metacopy dentry). This helper
224 * returns the lower data dentry.
225 */
226struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
227{
228 struct ovl_entry *oe = dentry->d_fsdata;
229
230 return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
231}
232
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100233struct dentry *ovl_dentry_real(struct dentry *dentry)
234{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200235 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100236}
237
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200238struct dentry *ovl_i_dentry_upper(struct inode *inode)
239{
240 return ovl_upperdentry_dereference(OVL_I(inode));
241}
242
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200243struct inode *ovl_inode_upper(struct inode *inode)
Miklos Szeredi25b77132017-07-04 22:03:16 +0200244{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200245 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200246
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200247 return upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200248}
249
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200250struct inode *ovl_inode_lower(struct inode *inode)
251{
252 return OVL_I(inode)->lower;
253}
254
255struct inode *ovl_inode_real(struct inode *inode)
256{
257 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
258}
259
Vivek Goyal2664bd02018-05-11 11:49:30 -0400260/* Return inode which contains lower data. Do not return metacopy */
261struct inode *ovl_inode_lowerdata(struct inode *inode)
262{
263 if (WARN_ON(!S_ISREG(inode->i_mode)))
264 return NULL;
265
266 return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
267}
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200268
Vivek Goyal4823d492018-05-11 11:49:31 -0400269/* Return real inode which contains data. Does not return metacopy inode */
270struct inode *ovl_inode_realdata(struct inode *inode)
271{
272 struct inode *upperinode;
273
274 upperinode = ovl_inode_upper(inode);
275 if (upperinode && ovl_has_upperdata(inode))
276 return upperinode;
277
278 return ovl_inode_lowerdata(inode);
279}
280
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200281struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100282{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200283 return OVL_I(inode)->cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100284}
285
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200286void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100287{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200288 OVL_I(inode)->cache = cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100289}
290
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200291void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
292{
293 set_bit(flag, &OVL_E(dentry)->flags);
294}
295
296void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
297{
298 clear_bit(flag, &OVL_E(dentry)->flags);
299}
300
301bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
302{
303 return test_bit(flag, &OVL_E(dentry)->flags);
304}
305
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100306bool ovl_dentry_is_opaque(struct dentry *dentry)
307{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200308 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100309}
310
311bool ovl_dentry_is_whiteout(struct dentry *dentry)
312{
313 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
314}
315
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100316void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100317{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200318 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100319}
320
Miklos Szeredi55acc662017-07-04 22:03:18 +0200321/*
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300322 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
323 * to return positive, while there's no actual upper alias for the inode.
324 * Copy up code needs to know about the existence of the upper alias, so it
325 * can't use ovl_dentry_upper().
Miklos Szeredi55acc662017-07-04 22:03:18 +0200326 */
327bool ovl_dentry_has_upper_alias(struct dentry *dentry)
328{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200329 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200330}
331
332void ovl_dentry_set_upper_alias(struct dentry *dentry)
333{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200334 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200335}
336
Vivek Goyal0c288872018-05-11 11:49:28 -0400337static bool ovl_should_check_upperdata(struct inode *inode)
338{
339 if (!S_ISREG(inode->i_mode))
340 return false;
341
342 if (!ovl_inode_lower(inode))
343 return false;
344
345 return true;
346}
347
348bool ovl_has_upperdata(struct inode *inode)
349{
350 if (!ovl_should_check_upperdata(inode))
351 return true;
352
353 if (!ovl_test_flag(OVL_UPPERDATA, inode))
354 return false;
355 /*
356 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
357 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
358 * if setting of OVL_UPPERDATA is visible, then effects of writes
359 * before that are visible too.
360 */
361 smp_rmb();
362 return true;
363}
364
365void ovl_set_upperdata(struct inode *inode)
366{
367 /*
368 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
369 * if OVL_UPPERDATA flag is visible, then effects of write operations
370 * before it are visible as well.
371 */
372 smp_wmb();
373 ovl_set_flag(OVL_UPPERDATA, inode);
374}
375
376/* Caller should hold ovl_inode->lock */
377bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
378{
379 if (!ovl_open_flags_need_copy_up(flags))
380 return false;
381
382 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
383}
384
385bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
386{
387 if (!ovl_open_flags_need_copy_up(flags))
388 return false;
389
390 return !ovl_has_upperdata(d_inode(dentry));
391}
392
Miklos Szeredia6c60652016-12-16 11:02:56 +0100393bool ovl_redirect_dir(struct super_block *sb)
394{
395 struct ovl_fs *ofs = sb->s_fs_info;
396
Amir Goldstein21a22872017-05-17 00:12:41 +0300397 return ofs->config.redirect_dir && !ofs->noxattr;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100398}
399
400const char *ovl_dentry_get_redirect(struct dentry *dentry)
401{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200402 return OVL_I(d_inode(dentry))->redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100403}
404
405void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
406{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200407 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Miklos Szeredia6c60652016-12-16 11:02:56 +0100408
Miklos Szeredicf31c462017-07-04 22:03:16 +0200409 kfree(oi->redirect);
410 oi->redirect = redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100411}
412
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200413void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100414{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200415 struct inode *upperinode = d_inode(upperdentry);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200416
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200417 WARN_ON(OVL_I(inode)->__upperdentry);
418
Miklos Szeredi25b77132017-07-04 22:03:16 +0200419 /*
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200420 * Make sure upperdentry is consistent before making it visible
Miklos Szeredi25b77132017-07-04 22:03:16 +0200421 */
422 smp_wmb();
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200423 OVL_I(inode)->__upperdentry = upperdentry;
Amir Goldstein31747ed2018-01-14 18:35:40 +0200424 if (inode_unhashed(inode)) {
Miklos Szeredi25b77132017-07-04 22:03:16 +0200425 inode->i_private = upperinode;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100426 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200427 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100428}
429
Amir Goldstein0f8528c72021-04-11 12:22:23 +0300430static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100431{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200432 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100433
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200434 WARN_ON(!inode_is_locked(inode));
Amir Goldstein0f8528c72021-04-11 12:22:23 +0300435 WARN_ON(!d_is_dir(dentry));
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200436 /*
Amir Goldstein0f8528c72021-04-11 12:22:23 +0300437 * Version is used by readdir code to keep cache consistent.
438 * For merge dirs (or dirs with origin) all changes need to be noted.
439 * For non-merge dirs, cache contains only impure entries (i.e. ones
440 * which have been copied up and have origins), so only need to note
441 * changes to impure entries.
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200442 */
Amir Goldstein0f8528c72021-04-11 12:22:23 +0300443 if (!ovl_dir_is_real(dentry) || impurity)
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200444 OVL_I(inode)->version++;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100445}
446
Miklos Szeredid9854c82018-07-18 15:44:40 +0200447void ovl_dir_modified(struct dentry *dentry, bool impurity)
448{
449 /* Copy mtime/ctime */
450 ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
451
Amir Goldstein0f8528c72021-04-11 12:22:23 +0300452 ovl_dir_version_inc(dentry, impurity);
Miklos Szeredid9854c82018-07-18 15:44:40 +0200453}
454
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100455u64 ovl_dentry_version_get(struct dentry *dentry)
456{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200457 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100458
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200459 WARN_ON(!inode_is_locked(inode));
460 return OVL_I(inode)->version;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100461}
462
463bool ovl_is_whiteout(struct dentry *dentry)
464{
465 struct inode *inode = dentry->d_inode;
466
467 return inode && IS_WHITEOUT(inode);
468}
469
470struct file *ovl_path_open(struct path *path, int flags)
471{
Miklos Szeredi56230d92020-06-02 22:20:26 +0200472 struct inode *inode = d_inode(path->dentry);
473 int err, acc_mode;
474
475 if (flags & ~(O_ACCMODE | O_LARGEFILE))
476 BUG();
477
478 switch (flags & O_ACCMODE) {
479 case O_RDONLY:
480 acc_mode = MAY_READ;
481 break;
482 case O_WRONLY:
483 acc_mode = MAY_WRITE;
484 break;
485 default:
486 BUG();
487 }
488
489 err = inode_permission(inode, acc_mode | MAY_OPEN);
490 if (err)
491 return ERR_PTR(err);
492
493 /* O_NOATIME is an optimization, don't fail if not permitted */
494 if (inode_owner_or_capable(inode))
495 flags |= O_NOATIME;
496
497 return dentry_open(path, flags, current_cred());
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100498}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200499
Vivek Goyal0c288872018-05-11 11:49:28 -0400500/* Caller should hold ovl_inode->lock */
501static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
502{
503 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
504
505 if (ovl_dentry_upper(dentry) &&
506 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
507 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
508 return true;
509
510 return false;
511}
512
513bool ovl_already_copied_up(struct dentry *dentry, int flags)
Vivek Goyal2002df82018-05-11 11:49:28 -0400514{
515 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
516
517 /*
518 * Check if copy-up has happened as well as for upper alias (in
519 * case of hard links) is there.
520 *
521 * Both checks are lockless:
522 * - false negatives: will recheck under oi->lock
523 * - false positives:
524 * + ovl_dentry_upper() uses memory barriers to ensure the
525 * upper dentry is up-to-date
526 * + ovl_dentry_has_upper_alias() relies on locking of
527 * upper parent i_rwsem to prevent reordering copy-up
528 * with rename.
529 */
530 if (ovl_dentry_upper(dentry) &&
Vivek Goyal0c288872018-05-11 11:49:28 -0400531 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
532 !ovl_dentry_needs_data_copy_up(dentry, flags))
Vivek Goyal2002df82018-05-11 11:49:28 -0400533 return true;
534
535 return false;
536}
537
Vivek Goyal0c288872018-05-11 11:49:28 -0400538int ovl_copy_up_start(struct dentry *dentry, int flags)
Amir Goldstein39d3d602017-01-17 06:34:56 +0200539{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300540 struct inode *inode = d_inode(dentry);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200541 int err;
542
Amir Goldstein531d3042020-03-02 15:03:35 +0200543 err = ovl_inode_lock_interruptible(inode);
Vivek Goyal0c288872018-05-11 11:49:28 -0400544 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
Amir Goldsteina015daf2017-06-21 15:28:51 +0300545 err = 1; /* Already copied up */
Amir Goldstein1e92e302018-10-18 18:37:14 +0300546 ovl_inode_unlock(inode);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200547 }
Amir Goldstein39d3d602017-01-17 06:34:56 +0200548
549 return err;
550}
551
552void ovl_copy_up_end(struct dentry *dentry)
553{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300554 ovl_inode_unlock(d_inode(dentry));
Amir Goldstein39d3d602017-01-17 06:34:56 +0200555}
Amir Goldstein82b749b2017-05-17 00:12:40 +0300556
Miklos Szeredi610afc02020-09-02 10:58:49 +0200557bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry)
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300558{
Greg Kroah-Hartman791d8a92020-02-08 14:23:00 +0100559 ssize_t res;
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300560
Miklos Szeredi610afc02020-09-02 10:58:49 +0200561 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_ORIGIN, NULL, 0);
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300562
563 /* Zero size value means "copied up but origin unknown" */
564 if (res >= 0)
565 return true;
566
567 return false;
568}
569
Miklos Szeredi610afc02020-09-02 10:58:49 +0200570bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200571 enum ovl_xattr ox)
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300572{
Greg Kroah-Hartman791d8a92020-02-08 14:23:00 +0100573 ssize_t res;
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300574 char val;
575
576 if (!d_is_dir(dentry))
577 return false;
578
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200579 res = ovl_do_getxattr(OVL_FS(sb), dentry, ox, &val, 1);
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300580 if (res == 1 && val == 'y')
581 return true;
582
583 return false;
584}
585
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200586#define OVL_XATTR_OPAQUE_POSTFIX "opaque"
587#define OVL_XATTR_REDIRECT_POSTFIX "redirect"
588#define OVL_XATTR_ORIGIN_POSTFIX "origin"
589#define OVL_XATTR_IMPURE_POSTFIX "impure"
590#define OVL_XATTR_NLINK_POSTFIX "nlink"
591#define OVL_XATTR_UPPER_POSTFIX "upper"
592#define OVL_XATTR_METACOPY_POSTFIX "metacopy"
593
594#define OVL_XATTR_TAB_ENTRY(x) \
595 [x] = OVL_XATTR_PREFIX x ## _POSTFIX
596
597const char *ovl_xattr_table[] = {
598 OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
599 OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
600 OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
601 OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
602 OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
603 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
604 OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
605};
606
Amir Goldstein82b749b2017-05-17 00:12:40 +0300607int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200608 enum ovl_xattr ox, const void *value, size_t size,
Amir Goldstein82b749b2017-05-17 00:12:40 +0300609 int xerr)
610{
611 int err;
612 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
613
614 if (ofs->noxattr)
615 return xerr;
616
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200617 err = ovl_do_setxattr(ofs, upperdentry, ox, value, size);
Amir Goldstein82b749b2017-05-17 00:12:40 +0300618
619 if (err == -EOPNOTSUPP) {
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200620 pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
Amir Goldstein82b749b2017-05-17 00:12:40 +0300621 ofs->noxattr = true;
622 return xerr;
623 }
624
625 return err;
626}
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300627
628int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
629{
630 int err;
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300631
Miklos Szeredi13c72072017-07-04 22:03:16 +0200632 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300633 return 0;
634
635 /*
636 * Do not fail when upper doesn't support xattrs.
637 * Upper inodes won't have origin nor redirect xattr anyway.
638 */
639 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
640 "y", 1, 0);
641 if (!err)
Miklos Szeredi13c72072017-07-04 22:03:16 +0200642 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300643
644 return err;
645}
Miklos Szeredi13c72072017-07-04 22:03:16 +0200646
Amir Goldsteinad0af712017-06-21 15:28:32 +0300647/**
648 * Caller must hold a reference to inode to prevent it from being freed while
649 * it is marked inuse.
650 */
651bool ovl_inuse_trylock(struct dentry *dentry)
652{
653 struct inode *inode = d_inode(dentry);
654 bool locked = false;
655
656 spin_lock(&inode->i_lock);
657 if (!(inode->i_state & I_OVL_INUSE)) {
658 inode->i_state |= I_OVL_INUSE;
659 locked = true;
660 }
661 spin_unlock(&inode->i_lock);
662
663 return locked;
664}
665
666void ovl_inuse_unlock(struct dentry *dentry)
667{
668 if (dentry) {
669 struct inode *inode = d_inode(dentry);
670
671 spin_lock(&inode->i_lock);
672 WARN_ON(!(inode->i_state & I_OVL_INUSE));
673 inode->i_state &= ~I_OVL_INUSE;
674 spin_unlock(&inode->i_lock);
675 }
676}
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300677
Amir Goldstein146d62e2019-04-18 17:42:08 +0300678bool ovl_is_inuse(struct dentry *dentry)
679{
680 struct inode *inode = d_inode(dentry);
681 bool inuse;
682
683 spin_lock(&inode->i_lock);
684 inuse = (inode->i_state & I_OVL_INUSE);
685 spin_unlock(&inode->i_lock);
686
687 return inuse;
688}
689
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300690/*
691 * Does this overlay dentry need to be indexed on copy up?
692 */
693bool ovl_need_index(struct dentry *dentry)
694{
695 struct dentry *lower = ovl_dentry_lower(dentry);
696
697 if (!lower || !ovl_indexdir(dentry->d_sb))
698 return false;
699
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200700 /* Index all files for NFS export and consistency verification */
Amir Goldstein016b7202018-01-11 14:01:08 +0200701 if (ovl_index_all(dentry->d_sb))
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200702 return true;
703
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300704 /* Index only lower hardlinks on copy up */
705 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
706 return true;
707
708 return false;
709}
710
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300711/* Caller must hold OVL_I(inode)->lock */
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300712static void ovl_cleanup_index(struct dentry *dentry)
713{
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300714 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
715 struct inode *dir = indexdir->d_inode;
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300716 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
717 struct dentry *upperdentry = ovl_dentry_upper(dentry);
718 struct dentry *index = NULL;
719 struct inode *inode;
Amir Goldstein63e13252018-09-18 16:34:31 +0300720 struct qstr name = { };
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300721 int err;
722
723 err = ovl_get_index_name(lowerdentry, &name);
724 if (err)
725 goto fail;
726
727 inode = d_inode(upperdentry);
Amir Goldstein89a17552017-09-26 07:40:37 +0300728 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800729 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300730 upperdentry, inode->i_ino, inode->i_nlink);
731 /*
732 * We either have a bug with persistent union nlink or a lower
733 * hardlink was added while overlay is mounted. Adding a lower
734 * hardlink and then unlinking all overlay hardlinks would drop
735 * overlay nlink to zero before all upper inodes are unlinked.
736 * As a safety measure, when that situation is detected, set
737 * the overlay nlink to the index inode nlink minus one for the
738 * index entry itself.
739 */
740 set_nlink(d_inode(dentry), inode->i_nlink - 1);
741 ovl_set_nlink_upper(dentry);
742 goto out;
743 }
744
745 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300746 index = lookup_one_len(name.name, indexdir, name.len);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300747 err = PTR_ERR(index);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300748 if (IS_ERR(index)) {
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300749 index = NULL;
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300750 } else if (ovl_index_all(dentry->d_sb)) {
751 /* Whiteout orphan index to block future open by handle */
Chengguang Xuc21c8392020-04-24 10:55:17 +0800752 err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
753 dir, index);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300754 } else {
755 /* Cleanup orphan index entries */
756 err = ovl_cleanup(dir, index);
757 }
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300758
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300759 inode_unlock(dir);
760 if (err)
761 goto fail;
762
763out:
Amir Goldstein63e13252018-09-18 16:34:31 +0300764 kfree(name.name);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300765 dput(index);
766 return;
767
768fail:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800769 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300770 goto out;
771}
772
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300773/*
774 * Operations that change overlay inode and upper inode nlink need to be
775 * synchronized with copy up for persistent nlink accounting.
776 */
Amir Goldstein0e329922018-10-18 18:37:13 +0300777int ovl_nlink_start(struct dentry *dentry)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300778{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300779 struct inode *inode = d_inode(dentry);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300780 const struct cred *old_cred;
781 int err;
782
Amir Goldstein1e92e302018-10-18 18:37:14 +0300783 if (WARN_ON(!inode))
Amir Goldstein0e329922018-10-18 18:37:13 +0300784 return -ENOENT;
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300785
786 /*
787 * With inodes index is enabled, we store the union overlay nlink
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300788 * in an xattr on the index inode. When whiting out an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300789 * we need to decrement the overlay persistent nlink, but before the
790 * first copy up, we have no upper index inode to store the xattr.
791 *
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300792 * As a workaround, before whiteout/rename over an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300793 * copy up to create the upper index. Creating the upper index will
794 * initialize the overlay nlink, so it could be dropped if unlink
795 * or rename succeeds.
796 *
797 * TODO: implement metadata only index copy up when called with
798 * ovl_copy_up_flags(dentry, O_PATH).
799 */
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300800 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300801 err = ovl_copy_up(dentry);
802 if (err)
803 return err;
804 }
805
Amir Goldstein531d3042020-03-02 15:03:35 +0200806 err = ovl_inode_lock_interruptible(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300807 if (err)
808 return err;
809
Amir Goldstein1e92e302018-10-18 18:37:14 +0300810 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300811 goto out;
812
813 old_cred = ovl_override_creds(dentry->d_sb);
814 /*
815 * The overlay inode nlink should be incremented/decremented IFF the
816 * upper operation succeeds, along with nlink change of upper inode.
817 * Therefore, before link/unlink/rename, we store the union nlink
818 * value relative to the upper inode nlink in an upper inode xattr.
819 */
820 err = ovl_set_nlink_upper(dentry);
Mark Salyzyn6120a4d2018-06-14 11:15:22 -0700821 ovl_revert_creds(dentry->d_sb, old_cred);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300822
823out:
824 if (err)
Amir Goldstein1e92e302018-10-18 18:37:14 +0300825 ovl_inode_unlock(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300826
827 return err;
828}
829
Amir Goldstein0e329922018-10-18 18:37:13 +0300830void ovl_nlink_end(struct dentry *dentry)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300831{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300832 struct inode *inode = d_inode(dentry);
833
834 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
Amir Goldstein0e329922018-10-18 18:37:13 +0300835 const struct cred *old_cred;
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300836
Amir Goldstein0e329922018-10-18 18:37:13 +0300837 old_cred = ovl_override_creds(dentry->d_sb);
838 ovl_cleanup_index(dentry);
Mark Salyzyn6120a4d2018-06-14 11:15:22 -0700839 ovl_revert_creds(dentry->d_sb, old_cred);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300840 }
Amir Goldstein0e329922018-10-18 18:37:13 +0300841
Amir Goldstein1e92e302018-10-18 18:37:14 +0300842 ovl_inode_unlock(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300843}
Amir Goldstein5820dc02017-09-25 16:39:55 +0300844
845int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
846{
847 /* Workdir should not be the same as upperdir */
848 if (workdir == upperdir)
849 goto err;
850
851 /* Workdir should not be subdir of upperdir and vice versa */
852 if (lock_rename(workdir, upperdir) != NULL)
853 goto err_unlock;
854
855 return 0;
856
857err_unlock:
858 unlock_rename(workdir, upperdir);
859err:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800860 pr_err("failed to lock workdir+upperdir\n");
Amir Goldstein5820dc02017-09-25 16:39:55 +0300861 return -EIO;
862}
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400863
864/* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
Miklos Szeredi610afc02020-09-02 10:58:49 +0200865int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry)
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400866{
Greg Kroah-Hartman791d8a92020-02-08 14:23:00 +0100867 ssize_t res;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400868
869 /* Only regular files can have metacopy xattr */
870 if (!S_ISREG(d_inode(dentry)->i_mode))
871 return 0;
872
Miklos Szeredi610afc02020-09-02 10:58:49 +0200873 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_METACOPY, NULL, 0);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400874 if (res < 0) {
875 if (res == -ENODATA || res == -EOPNOTSUPP)
876 return 0;
877 goto out;
878 }
879
880 return 1;
881out:
Greg Kroah-Hartman791d8a92020-02-08 14:23:00 +0100882 pr_warn_ratelimited("failed to get metacopy (%zi)\n", res);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400883 return res;
884}
Vivek Goyal67d756c22018-05-11 11:49:30 -0400885
886bool ovl_is_metacopy_dentry(struct dentry *dentry)
887{
888 struct ovl_entry *oe = dentry->d_fsdata;
889
890 if (!d_is_reg(dentry))
891 return false;
892
893 if (ovl_dentry_upper(dentry)) {
894 if (!ovl_has_upperdata(d_inode(dentry)))
895 return true;
896 return false;
897 }
898
899 return (oe->numlower > 1);
900}
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400901
Miklos Szeredi610afc02020-09-02 10:58:49 +0200902char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
903 int padding)
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400904{
905 int res;
906 char *s, *next, *buf = NULL;
907
Miklos Szeredi610afc02020-09-02 10:58:49 +0200908 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, NULL, 0);
Miklos Szeredi92f0d6c2020-09-02 10:58:48 +0200909 if (res == -ENODATA || res == -EOPNOTSUPP)
Vivek Goyal993a0b22019-01-30 14:01:57 -0500910 return NULL;
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400911 if (res < 0)
Miklos Szeredi92f0d6c2020-09-02 10:58:48 +0200912 goto fail;
913 if (res == 0)
914 goto invalid;
915
916 buf = kzalloc(res + padding + 1, GFP_KERNEL);
917 if (!buf)
918 return ERR_PTR(-ENOMEM);
919
Miklos Szeredi610afc02020-09-02 10:58:49 +0200920 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, buf, res);
Miklos Szeredi92f0d6c2020-09-02 10:58:48 +0200921 if (res < 0)
922 goto fail;
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400923 if (res == 0)
924 goto invalid;
925
926 if (buf[0] == '/') {
927 for (s = buf; *s++ == '/'; s = next) {
928 next = strchrnul(s, '/');
929 if (s == next)
930 goto invalid;
931 }
932 } else {
933 if (strchr(buf, '/') != NULL)
934 goto invalid;
935 }
936
937 return buf;
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400938invalid:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800939 pr_warn_ratelimited("invalid redirect (%s)\n", buf);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400940 res = -EINVAL;
Miklos Szeredi92f0d6c2020-09-02 10:58:48 +0200941 goto err_free;
942fail:
943 pr_warn_ratelimited("failed to get redirect (%i)\n", res);
944err_free:
Vivek Goyal993a0b22019-01-30 14:01:57 -0500945 kfree(buf);
946 return ERR_PTR(res);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400947}
Sargun Dhillon8ccf963c2021-01-07 16:10:43 -0800948
949/*
950 * ovl_sync_status() - Check fs sync status for volatile mounts
951 *
952 * Returns 1 if this is not a volatile mount and a real sync is required.
953 *
954 * Returns 0 if syncing can be skipped because mount is volatile, and no errors
955 * have occurred on the upperdir since the mount.
956 *
957 * Returns -errno if it is a volatile mount, and the error that occurred since
958 * the last mount. If the error code changes, it'll return the latest error
959 * code.
960 */
961
962int ovl_sync_status(struct ovl_fs *ofs)
963{
964 struct vfsmount *mnt;
965
966 if (ovl_should_sync(ofs))
967 return 1;
968
969 mnt = ovl_upper_mnt(ofs);
970 if (!mnt)
971 return 0;
972
973 return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
974}