Linux 6.7-rc1
[linux-block.git] / fs / overlayfs / util.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
bbb1e54d
MS
2/*
3 * Copyright (C) 2011 Novell Inc.
4 * Copyright (C) 2016 Red Hat, Inc.
bbb1e54d
MS
5 */
6
7#include <linux/fs.h>
8#include <linux/mount.h>
9#include <linux/slab.h>
5b825c3a 10#include <linux/cred.h>
bbb1e54d 11#include <linux/xattr.h>
02bcd157 12#include <linux/exportfs.h>
184996e9 13#include <linux/file.h>
096a218a 14#include <linux/fileattr.h>
02bcd157 15#include <linux/uuid.h>
caf70cb2
AG
16#include <linux/namei.h>
17#include <linux/ratelimit.h>
bbb1e54d 18#include "overlayfs.h"
bbb1e54d 19
d08d3b3c
AG
20/* Get write access to upper mnt - may fail if upper sb was remounted ro */
21int ovl_get_write_access(struct dentry *dentry)
22{
23 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
24 return mnt_get_write_access(ovl_upper_mnt(ofs));
25}
26
27/* Get write access to upper sb - may block if upper sb is frozen */
28void ovl_start_write(struct dentry *dentry)
29{
30 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
31 sb_start_write(ovl_upper_mnt(ofs)->mnt_sb);
32}
33
bbb1e54d
MS
34int ovl_want_write(struct dentry *dentry)
35{
f01d0889 36 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
08f4c7c8 37 return mnt_want_write(ovl_upper_mnt(ofs));
bbb1e54d
MS
38}
39
d08d3b3c
AG
40void ovl_put_write_access(struct dentry *dentry)
41{
42 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
43 mnt_put_write_access(ovl_upper_mnt(ofs));
44}
45
46void ovl_end_write(struct dentry *dentry)
47{
48 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
49 sb_end_write(ovl_upper_mnt(ofs)->mnt_sb);
50}
51
bbb1e54d
MS
52void ovl_drop_write(struct dentry *dentry)
53{
f01d0889 54 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
08f4c7c8 55 mnt_drop_write(ovl_upper_mnt(ofs));
bbb1e54d
MS
56}
57
58struct dentry *ovl_workdir(struct dentry *dentry)
59{
f01d0889 60 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
bbb1e54d
MS
61 return ofs->workdir;
62}
63
64const struct cred *ovl_override_creds(struct super_block *sb)
65{
f01d0889 66 struct ovl_fs *ofs = OVL_FS(sb);
bbb1e54d
MS
67
68 return override_creds(ofs->creator_cred);
69}
70
e487d889
AG
71/*
72 * Check if underlying fs supports file handles and try to determine encoding
73 * type, in order to deduce maximum inode number used by fs.
74 *
75 * Return 0 if file handles are not supported.
76 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
77 * Return -1 if fs uses a non default encoding with unknown inode size.
78 */
79int ovl_can_decode_fh(struct super_block *sb)
02bcd157 80{
c846af05
MS
81 if (!capable(CAP_DAC_READ_SEARCH))
82 return 0;
83
66c62769 84 if (!exportfs_can_decode_fh(sb->s_export_op))
e487d889
AG
85 return 0;
86
87 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
02bcd157
AG
88}
89
90struct dentry *ovl_indexdir(struct super_block *sb)
91{
f01d0889 92 struct ovl_fs *ofs = OVL_FS(sb);
02bcd157
AG
93
94 return ofs->indexdir;
95}
96
f168f109
AG
97/* Index all files on copy up. For now only enabled for NFS export */
98bool ovl_index_all(struct super_block *sb)
99{
f01d0889 100 struct ovl_fs *ofs = OVL_FS(sb);
f168f109
AG
101
102 return ofs->config.nfs_export && ofs->config.index;
103}
104
105/* Verify lower origin on lookup. For now only enabled for NFS export */
106bool ovl_verify_lower(struct super_block *sb)
107{
f01d0889 108 struct ovl_fs *ofs = OVL_FS(sb);
f168f109
AG
109
110 return ofs->config.nfs_export && ofs->config.index;
111}
112
163db0da
AG
113struct ovl_path *ovl_stack_alloc(unsigned int n)
114{
115 return kcalloc(n, sizeof(struct ovl_path), GFP_KERNEL);
116}
117
118void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n)
119{
120 unsigned int i;
121
122 memcpy(dst, src, sizeof(struct ovl_path) * n);
123 for (i = 0; i < n; i++)
124 dget(src[i].dentry);
125}
126
127void ovl_stack_put(struct ovl_path *stack, unsigned int n)
128{
129 unsigned int i;
130
131 for (i = 0; stack && i < n; i++)
132 dput(stack[i].dentry);
133}
134
135void ovl_stack_free(struct ovl_path *stack, unsigned int n)
136{
137 ovl_stack_put(stack, n);
138 kfree(stack);
139}
140
bbb1e54d
MS
141struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
142{
5522c9c7 143 size_t size = offsetof(struct ovl_entry, __lowerstack[numlower]);
bbb1e54d
MS
144 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
145
146 if (oe)
5522c9c7 147 oe->__numlower = numlower;
bbb1e54d
MS
148
149 return oe;
150}
151
163db0da
AG
152void ovl_free_entry(struct ovl_entry *oe)
153{
154 ovl_stack_put(ovl_lowerstack(oe), ovl_numlower(oe));
155 kfree(oe);
156}
157
b07d5cc9
AG
158#define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
159
bbb1e54d
MS
160bool ovl_dentry_remote(struct dentry *dentry)
161{
b07d5cc9
AG
162 return dentry->d_flags & OVL_D_REVALIDATE;
163}
164
165void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
166{
167 if (!ovl_dentry_remote(realdentry))
168 return;
169
170 spin_lock(&dentry->d_lock);
171 dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
172 spin_unlock(&dentry->d_lock);
173}
174
0af950f5
AG
175void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry,
176 struct ovl_entry *oe)
b07d5cc9 177{
0af950f5 178 return ovl_dentry_init_flags(dentry, upperdentry, oe, OVL_D_REVALIDATE);
bbb1e54d
MS
179}
180
b07d5cc9 181void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
0af950f5 182 struct ovl_entry *oe, unsigned int mask)
f4288844 183{
5522c9c7 184 struct ovl_path *lowerstack = ovl_lowerstack(oe);
f4288844
MS
185 unsigned int i, flags = 0;
186
bccece1e
MS
187 if (upperdentry)
188 flags |= upperdentry->d_flags;
41665644 189 for (i = 0; i < ovl_numlower(oe) && lowerstack[i].dentry; i++)
5522c9c7 190 flags |= lowerstack[i].dentry->d_flags;
f4288844
MS
191
192 spin_lock(&dentry->d_lock);
193 dentry->d_flags &= ~mask;
194 dentry->d_flags |= flags & mask;
195 spin_unlock(&dentry->d_lock);
196}
197
bbb1e54d
MS
198bool ovl_dentry_weird(struct dentry *dentry)
199{
200 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
201 DCACHE_MANAGE_TRANSIT |
202 DCACHE_OP_HASH |
203 DCACHE_OP_COMPARE);
204}
205
206enum ovl_path_type ovl_path_type(struct dentry *dentry)
207{
a6ff2bc0 208 struct ovl_entry *oe = OVL_E(dentry);
bbb1e54d
MS
209 enum ovl_path_type type = 0;
210
09d8b586 211 if (ovl_dentry_upper(dentry)) {
bbb1e54d
MS
212 type = __OVL_PATH_UPPER;
213
214 /*
59548503 215 * Non-dir dentry can hold lower dentry of its copy up origin.
bbb1e54d 216 */
5522c9c7 217 if (ovl_numlower(oe)) {
60124877
VG
218 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
219 type |= __OVL_PATH_ORIGIN;
0b17c28a
VG
220 if (d_is_dir(dentry) ||
221 !ovl_has_upperdata(d_inode(dentry)))
59548503
AG
222 type |= __OVL_PATH_MERGE;
223 }
bbb1e54d 224 } else {
5522c9c7 225 if (ovl_numlower(oe) > 1)
bbb1e54d
MS
226 type |= __OVL_PATH_MERGE;
227 }
228 return type;
229}
230
231void ovl_path_upper(struct dentry *dentry, struct path *path)
232{
f01d0889 233 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
bbb1e54d 234
08f4c7c8 235 path->mnt = ovl_upper_mnt(ofs);
09d8b586 236 path->dentry = ovl_dentry_upper(dentry);
bbb1e54d
MS
237}
238
239void ovl_path_lower(struct dentry *dentry, struct path *path)
240{
a6ff2bc0 241 struct ovl_entry *oe = OVL_E(dentry);
5522c9c7 242 struct ovl_path *lowerpath = ovl_lowerstack(oe);
bbb1e54d 243
5522c9c7
AG
244 if (ovl_numlower(oe)) {
245 path->mnt = lowerpath->layer->mnt;
246 path->dentry = lowerpath->dentry;
b9343632
CR
247 } else {
248 *path = (struct path) { };
249 }
bbb1e54d
MS
250}
251
4f93b426
VG
252void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
253{
a6ff2bc0 254 struct ovl_entry *oe = OVL_E(dentry);
ab1eb5ff 255 struct ovl_path *lowerdata = ovl_lowerdata(oe);
2b21da92 256 struct dentry *lowerdata_dentry = ovl_lowerdata_dentry(oe);
4f93b426 257
2b21da92 258 if (lowerdata_dentry) {
2b21da92 259 path->dentry = lowerdata_dentry;
42dd69ae
AG
260 /*
261 * Pairs with smp_wmb() in ovl_dentry_set_lowerdata().
262 * Make sure that if lowerdata->dentry is visible, then
263 * datapath->layer is visible as well.
264 */
265 smp_rmb();
266 path->mnt = READ_ONCE(lowerdata->layer)->mnt;
4f93b426
VG
267 } else {
268 *path = (struct path) { };
269 }
270}
271
bbb1e54d
MS
272enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
273{
274 enum ovl_path_type type = ovl_path_type(dentry);
275
276 if (!OVL_TYPE_UPPER(type))
277 ovl_path_lower(dentry, path);
278 else
279 ovl_path_upper(dentry, path);
280
281 return type;
282}
283
1248ea4b
AG
284enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
285{
286 enum ovl_path_type type = ovl_path_type(dentry);
287
288 WARN_ON_ONCE(d_is_dir(dentry));
289
290 if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
291 ovl_path_lowerdata(dentry, path);
292 else
293 ovl_path_upper(dentry, path);
294
295 return type;
296}
297
bbb1e54d
MS
298struct dentry *ovl_dentry_upper(struct dentry *dentry)
299{
09d8b586 300 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
bbb1e54d
MS
301}
302
303struct dentry *ovl_dentry_lower(struct dentry *dentry)
304{
a6ff2bc0 305 struct ovl_entry *oe = OVL_E(dentry);
bbb1e54d 306
5522c9c7 307 return ovl_numlower(oe) ? ovl_lowerstack(oe)->dentry : NULL;
bbb1e54d
MS
308}
309
13464165 310const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
da309e8c 311{
a6ff2bc0 312 struct ovl_entry *oe = OVL_E(dentry);
da309e8c 313
5522c9c7 314 return ovl_numlower(oe) ? ovl_lowerstack(oe)->layer : NULL;
da309e8c
AG
315}
316
647d253f
VG
317/*
318 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
597534e7 319 * depending on what is stored in lowerstack[0]. At times we need to find
647d253f
VG
320 * lower dentry which has data (and not metacopy dentry). This helper
321 * returns the lower data dentry.
322 */
323struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
324{
ab1eb5ff 325 return ovl_lowerdata_dentry(OVL_E(dentry));
647d253f
VG
326}
327
42dd69ae
AG
328int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath)
329{
330 struct ovl_entry *oe = OVL_E(dentry);
331 struct ovl_path *lowerdata = ovl_lowerdata(oe);
332 struct dentry *datadentry = datapath->dentry;
333
334 if (WARN_ON_ONCE(ovl_numlower(oe) <= 1))
335 return -EIO;
336
337 WRITE_ONCE(lowerdata->layer, datapath->layer);
338 /*
339 * Pairs with smp_rmb() in ovl_path_lowerdata().
340 * Make sure that if lowerdata->dentry is visible, then
341 * lowerdata->layer is visible as well.
342 */
343 smp_wmb();
344 WRITE_ONCE(lowerdata->dentry, dget(datadentry));
345
346 ovl_dentry_update_reval(dentry, datadentry);
347
348 return 0;
349}
350
bbb1e54d
MS
351struct dentry *ovl_dentry_real(struct dentry *dentry)
352{
09d8b586 353 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
bbb1e54d
MS
354}
355
1d88f183
MS
356struct dentry *ovl_i_dentry_upper(struct inode *inode)
357{
358 return ovl_upperdentry_dereference(OVL_I(inode));
359}
360
b2dd05f1 361struct inode *ovl_i_path_real(struct inode *inode, struct path *path)
ffa5723c 362{
ac900ed4
AG
363 struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
364
ffa5723c
AG
365 path->dentry = ovl_i_dentry_upper(inode);
366 if (!path->dentry) {
ac900ed4
AG
367 path->dentry = lowerpath->dentry;
368 path->mnt = lowerpath->layer->mnt;
ffa5723c
AG
369 } else {
370 path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
371 }
b2dd05f1
ZC
372
373 return path->dentry ? d_inode_rcu(path->dentry) : NULL;
ffa5723c
AG
374}
375
09d8b586 376struct inode *ovl_inode_upper(struct inode *inode)
25b7713a 377{
1d88f183 378 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
25b7713a 379
09d8b586
MS
380 return upperdentry ? d_inode(upperdentry) : NULL;
381}
25b7713a 382
09d8b586
MS
383struct inode *ovl_inode_lower(struct inode *inode)
384{
ac900ed4 385 struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
ffa5723c 386
ac900ed4 387 return lowerpath ? d_inode(lowerpath->dentry) : NULL;
09d8b586 388}
25b7713a 389
09d8b586
MS
390struct inode *ovl_inode_real(struct inode *inode)
391{
392 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
25b7713a
MS
393}
394
2664bd08
VG
395/* Return inode which contains lower data. Do not return metacopy */
396struct inode *ovl_inode_lowerdata(struct inode *inode)
397{
ab1eb5ff
AG
398 struct dentry *lowerdata = ovl_lowerdata_dentry(OVL_I_E(inode));
399
2664bd08
VG
400 if (WARN_ON(!S_ISREG(inode->i_mode)))
401 return NULL;
402
ab1eb5ff 403 return lowerdata ? d_inode(lowerdata) : NULL;
2664bd08 404}
09d8b586 405
4823d49c
VG
406/* Return real inode which contains data. Does not return metacopy inode */
407struct inode *ovl_inode_realdata(struct inode *inode)
408{
409 struct inode *upperinode;
410
411 upperinode = ovl_inode_upper(inode);
412 if (upperinode && ovl_has_upperdata(inode))
413 return upperinode;
414
415 return ovl_inode_lowerdata(inode);
416}
417
2b21da92
AG
418const char *ovl_lowerdata_redirect(struct inode *inode)
419{
420 return inode && S_ISREG(inode->i_mode) ?
421 OVL_I(inode)->lowerdata_redirect : NULL;
422}
423
4edb83bb 424struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
bbb1e54d 425{
2b21da92 426 return inode && S_ISDIR(inode->i_mode) ? OVL_I(inode)->cache : NULL;
bbb1e54d
MS
427}
428
4edb83bb 429void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
bbb1e54d 430{
4edb83bb 431 OVL_I(inode)->cache = cache;
bbb1e54d
MS
432}
433
c62520a8
AG
434void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
435{
a6ff2bc0 436 set_bit(flag, OVL_E_FLAGS(dentry));
c62520a8
AG
437}
438
439void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
440{
a6ff2bc0 441 clear_bit(flag, OVL_E_FLAGS(dentry));
c62520a8
AG
442}
443
444bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
445{
a6ff2bc0 446 return test_bit(flag, OVL_E_FLAGS(dentry));
c62520a8
AG
447}
448
bbb1e54d
MS
449bool ovl_dentry_is_opaque(struct dentry *dentry)
450{
c62520a8 451 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
bbb1e54d
MS
452}
453
454bool ovl_dentry_is_whiteout(struct dentry *dentry)
455{
456 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
457}
458
5cf5b477 459void ovl_dentry_set_opaque(struct dentry *dentry)
bbb1e54d 460{
c62520a8 461 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
bbb1e54d
MS
462}
463
55acc661 464/*
aa3ff3c1
AG
465 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
466 * to return positive, while there's no actual upper alias for the inode.
467 * Copy up code needs to know about the existence of the upper alias, so it
468 * can't use ovl_dentry_upper().
55acc661
MS
469 */
470bool ovl_dentry_has_upper_alias(struct dentry *dentry)
471{
c62520a8 472 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
55acc661
MS
473}
474
475void ovl_dentry_set_upper_alias(struct dentry *dentry)
476{
c62520a8 477 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
55acc661
MS
478}
479
0c288874
VG
480static bool ovl_should_check_upperdata(struct inode *inode)
481{
482 if (!S_ISREG(inode->i_mode))
483 return false;
484
485 if (!ovl_inode_lower(inode))
486 return false;
487
488 return true;
489}
490
491bool ovl_has_upperdata(struct inode *inode)
492{
493 if (!ovl_should_check_upperdata(inode))
494 return true;
495
496 if (!ovl_test_flag(OVL_UPPERDATA, inode))
497 return false;
498 /*
499 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
500 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
501 * if setting of OVL_UPPERDATA is visible, then effects of writes
502 * before that are visible too.
503 */
504 smp_rmb();
505 return true;
506}
507
508void ovl_set_upperdata(struct inode *inode)
509{
510 /*
511 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
512 * if OVL_UPPERDATA flag is visible, then effects of write operations
513 * before it are visible as well.
514 */
515 smp_wmb();
516 ovl_set_flag(OVL_UPPERDATA, inode);
517}
518
519/* Caller should hold ovl_inode->lock */
520bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
521{
522 if (!ovl_open_flags_need_copy_up(flags))
523 return false;
524
525 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
526}
527
528bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
529{
530 if (!ovl_open_flags_need_copy_up(flags))
531 return false;
532
533 return !ovl_has_upperdata(d_inode(dentry));
534}
535
a6c60655
MS
536const char *ovl_dentry_get_redirect(struct dentry *dentry)
537{
cf31c463 538 return OVL_I(d_inode(dentry))->redirect;
a6c60655
MS
539}
540
541void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
542{
cf31c463 543 struct ovl_inode *oi = OVL_I(d_inode(dentry));
a6c60655 544
cf31c463
MS
545 kfree(oi->redirect);
546 oi->redirect = redirect;
a6c60655
MS
547}
548
09d8b586 549void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
bbb1e54d 550{
09d8b586 551 struct inode *upperinode = d_inode(upperdentry);
e6d2ebdd 552
09d8b586
MS
553 WARN_ON(OVL_I(inode)->__upperdentry);
554
25b7713a 555 /*
09d8b586 556 * Make sure upperdentry is consistent before making it visible
25b7713a
MS
557 */
558 smp_wmb();
09d8b586 559 OVL_I(inode)->__upperdentry = upperdentry;
31747eda 560 if (inode_unhashed(inode)) {
25b7713a 561 inode->i_private = upperinode;
bbb1e54d 562 __insert_inode_hash(inode, (unsigned long) upperinode);
25b7713a 563 }
bbb1e54d
MS
564}
565
65cd913e 566static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
bbb1e54d 567{
04a01ac7 568 struct inode *inode = d_inode(dentry);
bbb1e54d 569
04a01ac7 570 WARN_ON(!inode_is_locked(inode));
65cd913e 571 WARN_ON(!d_is_dir(dentry));
4edb83bb 572 /*
65cd913e
AG
573 * Version is used by readdir code to keep cache consistent.
574 * For merge dirs (or dirs with origin) all changes need to be noted.
575 * For non-merge dirs, cache contains only impure entries (i.e. ones
576 * which have been copied up and have origins), so only need to note
577 * changes to impure entries.
4edb83bb 578 */
1fa9c5c5 579 if (!ovl_dir_is_real(inode) || impurity)
4edb83bb 580 OVL_I(inode)->version++;
bbb1e54d
MS
581}
582
d9854c87
MS
583void ovl_dir_modified(struct dentry *dentry, bool impurity)
584{
585 /* Copy mtime/ctime */
2878dffc 586 ovl_copyattr(d_inode(dentry));
d9854c87 587
65cd913e 588 ovl_dir_version_inc(dentry, impurity);
d9854c87
MS
589}
590
1fa9c5c5 591u64 ovl_inode_version_get(struct inode *inode)
bbb1e54d 592{
04a01ac7
MS
593 WARN_ON(!inode_is_locked(inode));
594 return OVL_I(inode)->version;
bbb1e54d
MS
595}
596
597bool ovl_is_whiteout(struct dentry *dentry)
598{
599 struct inode *inode = dentry->d_inode;
600
601 return inode && IS_WHITEOUT(inode);
602}
603
bc8df7a3
AL
604/*
605 * Use this over ovl_is_whiteout for upper and lower files, as it also
606 * handles overlay.whiteout xattr whiteout files.
607 */
608bool ovl_path_is_whiteout(struct ovl_fs *ofs, const struct path *path)
609{
610 return ovl_is_whiteout(path->dentry) ||
611 ovl_path_check_xwhiteout_xattr(ofs, path);
612}
613
2d343087 614struct file *ovl_path_open(const struct path *path, int flags)
bbb1e54d 615{
56230d95 616 struct inode *inode = d_inode(path->dentry);
4609e1f1 617 struct mnt_idmap *real_idmap = mnt_idmap(path->mnt);
56230d95
MS
618 int err, acc_mode;
619
620 if (flags & ~(O_ACCMODE | O_LARGEFILE))
621 BUG();
622
623 switch (flags & O_ACCMODE) {
624 case O_RDONLY:
625 acc_mode = MAY_READ;
626 break;
627 case O_WRONLY:
628 acc_mode = MAY_WRITE;
629 break;
630 default:
631 BUG();
632 }
633
4609e1f1 634 err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN);
56230d95
MS
635 if (err)
636 return ERR_PTR(err);
637
638 /* O_NOATIME is an optimization, don't fail if not permitted */
01beba79 639 if (inode_owner_or_capable(real_idmap, inode))
56230d95
MS
640 flags |= O_NOATIME;
641
642 return dentry_open(path, flags, current_cred());
bbb1e54d 643}
39d3d60a 644
0c288874
VG
645/* Caller should hold ovl_inode->lock */
646static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
647{
648 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
649
650 if (ovl_dentry_upper(dentry) &&
651 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
652 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
653 return true;
654
655 return false;
656}
657
658bool ovl_already_copied_up(struct dentry *dentry, int flags)
2002df85
VG
659{
660 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
661
662 /*
663 * Check if copy-up has happened as well as for upper alias (in
664 * case of hard links) is there.
665 *
666 * Both checks are lockless:
667 * - false negatives: will recheck under oi->lock
668 * - false positives:
669 * + ovl_dentry_upper() uses memory barriers to ensure the
670 * upper dentry is up-to-date
671 * + ovl_dentry_has_upper_alias() relies on locking of
672 * upper parent i_rwsem to prevent reordering copy-up
673 * with rename.
674 */
675 if (ovl_dentry_upper(dentry) &&
0c288874
VG
676 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
677 !ovl_dentry_needs_data_copy_up(dentry, flags))
2002df85
VG
678 return true;
679
680 return false;
681}
682
c63e56a4
AG
683/*
684 * The copy up "transaction" keeps an elevated mnt write count on upper mnt,
685 * but leaves taking freeze protection on upper sb to lower level helpers.
686 */
0c288874 687int ovl_copy_up_start(struct dentry *dentry, int flags)
39d3d60a 688{
1e92e307 689 struct inode *inode = d_inode(dentry);
39d3d60a
AG
690 int err;
691
531d3040 692 err = ovl_inode_lock_interruptible(inode);
162d0644
AG
693 if (err)
694 return err;
695
696 if (ovl_already_copied_up_locked(dentry, flags))
a015dafc 697 err = 1; /* Already copied up */
162d0644 698 else
c63e56a4 699 err = ovl_get_write_access(dentry);
162d0644
AG
700 if (err)
701 goto out_unlock;
702
703 return 0;
39d3d60a 704
162d0644
AG
705out_unlock:
706 ovl_inode_unlock(inode);
39d3d60a
AG
707 return err;
708}
709
710void ovl_copy_up_end(struct dentry *dentry)
711{
c63e56a4 712 ovl_put_write_access(dentry);
1e92e307 713 ovl_inode_unlock(d_inode(dentry));
39d3d60a 714}
82b749b2 715
2d343087 716bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)
b79e05aa
AG
717{
718 int res;
719
dad7017a 720 res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
b79e05aa
AG
721
722 /* Zero size value means "copied up but origin unknown" */
723 if (res >= 0)
724 return true;
725
726 return false;
727}
728
bc8df7a3
AL
729bool ovl_path_check_xwhiteout_xattr(struct ovl_fs *ofs, const struct path *path)
730{
731 struct dentry *dentry = path->dentry;
732 int res;
733
734 /* xattr.whiteout must be a zero size regular file */
735 if (!d_is_reg(dentry) || i_size_read(d_inode(dentry)) != 0)
736 return false;
737
738 res = ovl_path_getxattr(ofs, path, OVL_XATTR_XWHITEOUT, NULL, 0);
739 return res >= 0;
740}
741
742bool ovl_path_check_xwhiteouts_xattr(struct ovl_fs *ofs, const struct path *path)
743{
744 struct dentry *dentry = path->dentry;
745 int res;
746
747 /* xattr.whiteouts must be a directory */
748 if (!d_is_dir(dentry))
749 return false;
750
751 res = ovl_path_getxattr(ofs, path, OVL_XATTR_XWHITEOUTS, NULL, 0);
752 return res >= 0;
753}
754
d9544c1b
AG
755/*
756 * Load persistent uuid from xattr into s_uuid if found, or store a new
757 * random generated value in s_uuid and in xattr.
758 */
759bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs,
760 const struct path *upperpath)
761{
762 bool set = false;
763 int res;
764
765 /* Try to load existing persistent uuid */
766 res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_UUID, sb->s_uuid.b,
767 UUID_SIZE);
768 if (res == UUID_SIZE)
769 return true;
770
771 if (res != -ENODATA)
772 goto fail;
773
cbb44f09
AG
774 /*
775 * With uuid=auto, if uuid xattr is found, it will be used.
776 * If uuid xattrs is not found, generate a persistent uuid only on mount
777 * of new overlays where upper root dir is not yet marked as impure.
778 * An upper dir is marked as impure on copy up or lookup of its subdirs.
779 */
780 if (ofs->config.uuid == OVL_UUID_AUTO) {
781 res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_IMPURE, NULL,
782 0);
783 if (res > 0) {
784 /* Any mount of old overlay - downgrade to uuid=null */
785 ofs->config.uuid = OVL_UUID_NULL;
786 return true;
787 } else if (res == -ENODATA) {
788 /* First mount of new overlay - upgrade to uuid=on */
789 ofs->config.uuid = OVL_UUID_ON;
790 } else if (res < 0) {
791 goto fail;
792 }
793
794 }
795
d9544c1b
AG
796 /* Generate overlay instance uuid */
797 uuid_gen(&sb->s_uuid);
798
799 /* Try to store persistent uuid */
800 set = true;
801 res = ovl_setxattr(ofs, upperpath->dentry, OVL_XATTR_UUID, sb->s_uuid.b,
802 UUID_SIZE);
803 if (res == 0)
804 return true;
805
806fail:
807 memset(sb->s_uuid.b, 0, UUID_SIZE);
808 ofs->config.uuid = OVL_UUID_NULL;
809 pr_warn("failed to %s uuid (%pd2, err=%i); falling back to uuid=null.\n",
810 set ? "set" : "get", upperpath->dentry, res);
811 return false;
812}
813
2d343087 814bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
dad7017a 815 enum ovl_xattr ox)
f3a15685
AG
816{
817 int res;
818 char val;
819
dad7017a 820 if (!d_is_dir(path->dentry))
f3a15685
AG
821 return false;
822
dad7017a 823 res = ovl_path_getxattr(ofs, path, ox, &val, 1);
f3a15685
AG
824 if (res == 1 && val == 'y')
825 return true;
826
827 return false;
828}
829
43d193f8
MS
830#define OVL_XATTR_OPAQUE_POSTFIX "opaque"
831#define OVL_XATTR_REDIRECT_POSTFIX "redirect"
832#define OVL_XATTR_ORIGIN_POSTFIX "origin"
833#define OVL_XATTR_IMPURE_POSTFIX "impure"
834#define OVL_XATTR_NLINK_POSTFIX "nlink"
835#define OVL_XATTR_UPPER_POSTFIX "upper"
d9544c1b 836#define OVL_XATTR_UUID_POSTFIX "uuid"
43d193f8 837#define OVL_XATTR_METACOPY_POSTFIX "metacopy"
096a218a 838#define OVL_XATTR_PROTATTR_POSTFIX "protattr"
bc8df7a3
AL
839#define OVL_XATTR_XWHITEOUT_POSTFIX "whiteout"
840#define OVL_XATTR_XWHITEOUTS_POSTFIX "whiteouts"
43d193f8
MS
841
842#define OVL_XATTR_TAB_ENTRY(x) \
2d2f2d73
MS
843 [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
844 [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
43d193f8 845
2d2f2d73 846const char *const ovl_xattr_table[][2] = {
43d193f8
MS
847 OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
848 OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
849 OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
850 OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
851 OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
852 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
d9544c1b 853 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UUID),
43d193f8 854 OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
096a218a 855 OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
bc8df7a3
AL
856 OVL_XATTR_TAB_ENTRY(OVL_XATTR_XWHITEOUT),
857 OVL_XATTR_TAB_ENTRY(OVL_XATTR_XWHITEOUTS),
43d193f8
MS
858};
859
a0c236b1 860int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
43d193f8 861 enum ovl_xattr ox, const void *value, size_t size,
82b749b2
AG
862 int xerr)
863{
864 int err;
82b749b2
AG
865
866 if (ofs->noxattr)
867 return xerr;
868
c914c0e2 869 err = ovl_setxattr(ofs, upperdentry, ox, value, size);
82b749b2
AG
870
871 if (err == -EOPNOTSUPP) {
43d193f8 872 pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
82b749b2
AG
873 ofs->noxattr = true;
874 return xerr;
875 }
876
877 return err;
878}
f3a15685
AG
879
880int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
881{
a0c236b1 882 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
f3a15685 883 int err;
f3a15685 884
13c72075 885 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
f3a15685
AG
886 return 0;
887
888 /*
889 * Do not fail when upper doesn't support xattrs.
890 * Upper inodes won't have origin nor redirect xattr anyway.
891 */
a0c236b1 892 err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
f3a15685 893 if (!err)
13c72075 894 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
f3a15685
AG
895
896 return err;
897}
13c72075 898
096a218a
AG
899
900#define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
901
902void ovl_check_protattr(struct inode *inode, struct dentry *upper)
903{
904 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
905 u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
906 char buf[OVL_PROTATTR_MAX+1];
907 int res, n;
908
dad7017a
CB
909 res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
910 OVL_PROTATTR_MAX);
096a218a
AG
911 if (res < 0)
912 return;
913
914 /*
915 * Initialize inode flags from overlay.protattr xattr and upper inode
916 * flags. If upper inode has those fileattr flags set (i.e. from old
917 * kernel), we do not clear them on ovl_get_inode(), but we will clear
918 * them on next fileattr_set().
919 */
920 for (n = 0; n < res; n++) {
921 if (buf[n] == 'a')
922 iflags |= S_APPEND;
923 else if (buf[n] == 'i')
924 iflags |= S_IMMUTABLE;
925 else
926 break;
927 }
928
929 if (!res || n < res) {
930 pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
931 upper, res);
932 } else {
933 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
934 }
935}
936
937int ovl_set_protattr(struct inode *inode, struct dentry *upper,
938 struct fileattr *fa)
939{
940 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
941 char buf[OVL_PROTATTR_MAX];
942 int len = 0, err = 0;
943 u32 iflags = 0;
944
945 BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
946
947 if (fa->flags & FS_APPEND_FL) {
948 buf[len++] = 'a';
949 iflags |= S_APPEND;
950 }
951 if (fa->flags & FS_IMMUTABLE_FL) {
952 buf[len++] = 'i';
953 iflags |= S_IMMUTABLE;
954 }
955
956 /*
957 * Do not allow to set protection flags when upper doesn't support
958 * xattrs, because we do not set those fileattr flags on upper inode.
959 * Remove xattr if it exist and all protection flags are cleared.
960 */
961 if (len) {
962 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
963 buf, len, -EPERM);
964 } else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
c914c0e2 965 err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
096a218a
AG
966 if (err == -EOPNOTSUPP || err == -ENODATA)
967 err = 0;
968 }
969 if (err)
970 return err;
971
972 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
973
974 /* Mask out the fileattr flags that should not be set in upper inode */
975 fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
976 fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
977
978 return 0;
979}
980
ad0af710
AG
981/**
982 * Caller must hold a reference to inode to prevent it from being freed while
983 * it is marked inuse.
984 */
985bool ovl_inuse_trylock(struct dentry *dentry)
986{
987 struct inode *inode = d_inode(dentry);
988 bool locked = false;
989
990 spin_lock(&inode->i_lock);
991 if (!(inode->i_state & I_OVL_INUSE)) {
992 inode->i_state |= I_OVL_INUSE;
993 locked = true;
994 }
995 spin_unlock(&inode->i_lock);
996
997 return locked;
998}
999
1000void ovl_inuse_unlock(struct dentry *dentry)
1001{
1002 if (dentry) {
1003 struct inode *inode = d_inode(dentry);
1004
1005 spin_lock(&inode->i_lock);
1006 WARN_ON(!(inode->i_state & I_OVL_INUSE));
1007 inode->i_state &= ~I_OVL_INUSE;
1008 spin_unlock(&inode->i_lock);
1009 }
1010}
5f8415d6 1011
146d62e5
AG
1012bool ovl_is_inuse(struct dentry *dentry)
1013{
1014 struct inode *inode = d_inode(dentry);
1015 bool inuse;
1016
1017 spin_lock(&inode->i_lock);
1018 inuse = (inode->i_state & I_OVL_INUSE);
1019 spin_unlock(&inode->i_lock);
1020
1021 return inuse;
1022}
1023
24b33ee1
AG
1024/*
1025 * Does this overlay dentry need to be indexed on copy up?
1026 */
1027bool ovl_need_index(struct dentry *dentry)
1028{
1029 struct dentry *lower = ovl_dentry_lower(dentry);
1030
1031 if (!lower || !ovl_indexdir(dentry->d_sb))
1032 return false;
1033
fbd2d207 1034 /* Index all files for NFS export and consistency verification */
016b720f 1035 if (ovl_index_all(dentry->d_sb))
fbd2d207
AG
1036 return true;
1037
24b33ee1
AG
1038 /* Index only lower hardlinks on copy up */
1039 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
1040 return true;
1041
1042 return false;
1043}
1044
9f4ec904 1045/* Caller must hold OVL_I(inode)->lock */
caf70cb2
AG
1046static void ovl_cleanup_index(struct dentry *dentry)
1047{
1cdb0cb6 1048 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
e7dd0e71
AG
1049 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
1050 struct inode *dir = indexdir->d_inode;
caf70cb2
AG
1051 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
1052 struct dentry *upperdentry = ovl_dentry_upper(dentry);
1053 struct dentry *index = NULL;
1054 struct inode *inode;
63e13252 1055 struct qstr name = { };
5b02bfc1 1056 bool got_write = false;
caf70cb2
AG
1057 int err;
1058
1cdb0cb6 1059 err = ovl_get_index_name(ofs, lowerdentry, &name);
caf70cb2
AG
1060 if (err)
1061 goto fail;
1062
5b02bfc1
AG
1063 err = ovl_want_write(dentry);
1064 if (err)
1065 goto fail;
1066
1067 got_write = true;
caf70cb2 1068 inode = d_inode(upperdentry);
89a17556 1069 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
1bd0a3ae 1070 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
caf70cb2
AG
1071 upperdentry, inode->i_ino, inode->i_nlink);
1072 /*
1073 * We either have a bug with persistent union nlink or a lower
1074 * hardlink was added while overlay is mounted. Adding a lower
1075 * hardlink and then unlinking all overlay hardlinks would drop
1076 * overlay nlink to zero before all upper inodes are unlinked.
1077 * As a safety measure, when that situation is detected, set
1078 * the overlay nlink to the index inode nlink minus one for the
1079 * index entry itself.
1080 */
1081 set_nlink(d_inode(dentry), inode->i_nlink - 1);
1082 ovl_set_nlink_upper(dentry);
1083 goto out;
1084 }
1085
1086 inode_lock_nested(dir, I_MUTEX_PARENT);
22f289ce 1087 index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
caf70cb2 1088 err = PTR_ERR(index);
e7dd0e71 1089 if (IS_ERR(index)) {
9f4ec904 1090 index = NULL;
e7dd0e71
AG
1091 } else if (ovl_index_all(dentry->d_sb)) {
1092 /* Whiteout orphan index to block future open by handle */
c21c839b
CX
1093 err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
1094 dir, index);
e7dd0e71
AG
1095 } else {
1096 /* Cleanup orphan index entries */
576bb263 1097 err = ovl_cleanup(ofs, dir, index);
e7dd0e71 1098 }
9f4ec904 1099
caf70cb2
AG
1100 inode_unlock(dir);
1101 if (err)
1102 goto fail;
1103
1104out:
5b02bfc1
AG
1105 if (got_write)
1106 ovl_drop_write(dentry);
63e13252 1107 kfree(name.name);
caf70cb2
AG
1108 dput(index);
1109 return;
1110
1111fail:
1bd0a3ae 1112 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
caf70cb2
AG
1113 goto out;
1114}
1115
5f8415d6
AG
1116/*
1117 * Operations that change overlay inode and upper inode nlink need to be
1118 * synchronized with copy up for persistent nlink accounting.
1119 */
0e32992f 1120int ovl_nlink_start(struct dentry *dentry)
5f8415d6 1121{
1e92e307 1122 struct inode *inode = d_inode(dentry);
5f8415d6
AG
1123 const struct cred *old_cred;
1124 int err;
1125
1e92e307 1126 if (WARN_ON(!inode))
0e32992f 1127 return -ENOENT;
5f8415d6
AG
1128
1129 /*
1130 * With inodes index is enabled, we store the union overlay nlink
24b33ee1 1131 * in an xattr on the index inode. When whiting out an indexed lower,
5f8415d6
AG
1132 * we need to decrement the overlay persistent nlink, but before the
1133 * first copy up, we have no upper index inode to store the xattr.
1134 *
24b33ee1 1135 * As a workaround, before whiteout/rename over an indexed lower,
5f8415d6
AG
1136 * copy up to create the upper index. Creating the upper index will
1137 * initialize the overlay nlink, so it could be dropped if unlink
1138 * or rename succeeds.
1139 *
1140 * TODO: implement metadata only index copy up when called with
1141 * ovl_copy_up_flags(dentry, O_PATH).
1142 */
24b33ee1 1143 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
5f8415d6
AG
1144 err = ovl_copy_up(dentry);
1145 if (err)
1146 return err;
1147 }
1148
531d3040 1149 err = ovl_inode_lock_interruptible(inode);
5f8415d6
AG
1150 if (err)
1151 return err;
1152
162d0644
AG
1153 err = ovl_want_write(dentry);
1154 if (err)
1155 goto out_unlock;
1156
1e92e307 1157 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
162d0644 1158 return 0;
5f8415d6
AG
1159
1160 old_cred = ovl_override_creds(dentry->d_sb);
1161 /*
1162 * The overlay inode nlink should be incremented/decremented IFF the
1163 * upper operation succeeds, along with nlink change of upper inode.
1164 * Therefore, before link/unlink/rename, we store the union nlink
1165 * value relative to the upper inode nlink in an upper inode xattr.
1166 */
1167 err = ovl_set_nlink_upper(dentry);
1168 revert_creds(old_cred);
5f8415d6 1169 if (err)
162d0644
AG
1170 goto out_drop_write;
1171
1172 return 0;
1173
1174out_drop_write:
1175 ovl_drop_write(dentry);
1176out_unlock:
1177 ovl_inode_unlock(inode);
5f8415d6
AG
1178
1179 return err;
1180}
1181
0e32992f 1182void ovl_nlink_end(struct dentry *dentry)
5f8415d6 1183{
1e92e307
AG
1184 struct inode *inode = d_inode(dentry);
1185
5b02bfc1
AG
1186 ovl_drop_write(dentry);
1187
1e92e307 1188 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
0e32992f 1189 const struct cred *old_cred;
caf70cb2 1190
0e32992f
AG
1191 old_cred = ovl_override_creds(dentry->d_sb);
1192 ovl_cleanup_index(dentry);
1193 revert_creds(old_cred);
caf70cb2 1194 }
0e32992f 1195
1e92e307 1196 ovl_inode_unlock(inode);
5f8415d6 1197}
5820dc08
AG
1198
1199int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
1200{
1201 /* Workdir should not be the same as upperdir */
1202 if (workdir == upperdir)
1203 goto err;
1204
1205 /* Workdir should not be subdir of upperdir and vice versa */
1206 if (lock_rename(workdir, upperdir) != NULL)
1207 goto err_unlock;
1208
1209 return 0;
1210
1211err_unlock:
1212 unlock_rename(workdir, upperdir);
1213err:
1bd0a3ae 1214 pr_err("failed to lock workdir+upperdir\n");
5820dc08
AG
1215 return -EIO;
1216}
9d3dfea3 1217
bf070890
AL
1218/*
1219 * err < 0, 0 if no metacopy xattr, metacopy data size if xattr found.
1220 * an empty xattr returns OVL_METACOPY_MIN_SIZE to distinguish from no xattr value.
1221 */
1222int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path,
1223 struct ovl_metacopy *data)
9d3dfea3
VG
1224{
1225 int res;
1226
1227 /* Only regular files can have metacopy xattr */
dad7017a 1228 if (!S_ISREG(d_inode(path->dentry)->i_mode))
9d3dfea3
VG
1229 return 0;
1230
bf070890
AL
1231 res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY,
1232 data, data ? OVL_METACOPY_MAX_SIZE : 0);
9d3dfea3
VG
1233 if (res < 0) {
1234 if (res == -ENODATA || res == -EOPNOTSUPP)
1235 return 0;
87b2c60c
MS
1236 /*
1237 * getxattr on user.* may fail with EACCES in case there's no
1238 * read permission on the inode. Not much we can do, other than
1239 * tell the caller that this is not a metacopy inode.
1240 */
1241 if (ofs->config.userxattr && res == -EACCES)
1242 return 0;
9d3dfea3
VG
1243 goto out;
1244 }
1245
bf070890
AL
1246 if (res == 0) {
1247 /* Emulate empty data for zero size metacopy xattr */
1248 res = OVL_METACOPY_MIN_SIZE;
1249 if (data) {
1250 memset(data, 0, res);
1251 data->len = res;
1252 }
1253 } else if (res < OVL_METACOPY_MIN_SIZE) {
1254 pr_warn_ratelimited("metacopy file '%pd' has too small xattr\n",
1255 path->dentry);
1256 return -EIO;
1257 } else if (data) {
1258 if (data->version != 0) {
1259 pr_warn_ratelimited("metacopy file '%pd' has unsupported version\n",
1260 path->dentry);
1261 return -EIO;
1262 }
1263 if (res != data->len) {
1264 pr_warn_ratelimited("metacopy file '%pd' has invalid xattr size\n",
1265 path->dentry);
1266 return -EIO;
1267 }
1268 }
1269
1270 return res;
9d3dfea3 1271out:
1bd0a3ae 1272 pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
9d3dfea3
VG
1273 return res;
1274}
67d756c2 1275
184996e9
AL
1276int ovl_set_metacopy_xattr(struct ovl_fs *ofs, struct dentry *d, struct ovl_metacopy *metacopy)
1277{
1278 size_t len = metacopy->len;
1279
1280 /* If no flags or digest fall back to empty metacopy file */
1281 if (metacopy->version == 0 && metacopy->flags == 0 && metacopy->digest_algo == 0)
1282 len = 0;
1283
1284 return ovl_check_setxattr(ofs, d, OVL_XATTR_METACOPY,
1285 metacopy, len, -EOPNOTSUPP);
1286}
1287
67d756c2
VG
1288bool ovl_is_metacopy_dentry(struct dentry *dentry)
1289{
a6ff2bc0 1290 struct ovl_entry *oe = OVL_E(dentry);
67d756c2
VG
1291
1292 if (!d_is_reg(dentry))
1293 return false;
1294
1295 if (ovl_dentry_upper(dentry)) {
1296 if (!ovl_has_upperdata(d_inode(dentry)))
1297 return true;
1298 return false;
1299 }
1300
5522c9c7 1301 return (ovl_numlower(oe) > 1);
67d756c2 1302}
0a2d0d3f 1303
2d343087 1304char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding)
993a0b2a
VG
1305{
1306 int res;
1307 char *s, *next, *buf = NULL;
1308
dad7017a 1309 res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
92f0d6c9 1310 if (res == -ENODATA || res == -EOPNOTSUPP)
993a0b2a 1311 return NULL;
0a2d0d3f 1312 if (res < 0)
92f0d6c9
MS
1313 goto fail;
1314 if (res == 0)
1315 goto invalid;
1316
1317 buf = kzalloc(res + padding + 1, GFP_KERNEL);
1318 if (!buf)
1319 return ERR_PTR(-ENOMEM);
1320
dad7017a 1321 res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
92f0d6c9
MS
1322 if (res < 0)
1323 goto fail;
0a2d0d3f
VG
1324 if (res == 0)
1325 goto invalid;
1326
1327 if (buf[0] == '/') {
1328 for (s = buf; *s++ == '/'; s = next) {
1329 next = strchrnul(s, '/');
1330 if (s == next)
1331 goto invalid;
1332 }
1333 } else {
1334 if (strchr(buf, '/') != NULL)
1335 goto invalid;
1336 }
1337
1338 return buf;
0a2d0d3f 1339invalid:
1bd0a3ae 1340 pr_warn_ratelimited("invalid redirect (%s)\n", buf);
0a2d0d3f 1341 res = -EINVAL;
92f0d6c9
MS
1342 goto err_free;
1343fail:
1344 pr_warn_ratelimited("failed to get redirect (%i)\n", res);
1345err_free:
993a0b2a
VG
1346 kfree(buf);
1347 return ERR_PTR(res);
0a2d0d3f 1348}
335d3fc5 1349
184996e9 1350/* Call with mounter creds as it may open the file */
0c71faf5 1351int ovl_ensure_verity_loaded(struct path *datapath)
184996e9
AL
1352{
1353 struct inode *inode = d_inode(datapath->dentry);
1354 struct file *filp;
1355
1356 if (!fsverity_active(inode) && IS_VERITY(inode)) {
1357 /*
1358 * If this inode was not yet opened, the verity info hasn't been
1359 * loaded yet, so we need to do that here to force it into memory.
1360 */
1361 filp = kernel_file_open(datapath, O_RDONLY, inode, current_cred());
1362 if (IS_ERR(filp))
1363 return PTR_ERR(filp);
1364 fput(filp);
1365 }
1366
1367 return 0;
1368}
1369
1370int ovl_validate_verity(struct ovl_fs *ofs,
1371 struct path *metapath,
1372 struct path *datapath)
1373{
1374 struct ovl_metacopy metacopy_data;
1375 u8 actual_digest[FS_VERITY_MAX_DIGEST_SIZE];
1376 int xattr_digest_size, digest_size;
1377 int xattr_size, err;
1378 u8 verity_algo;
1379
1380 if (!ofs->config.verity_mode ||
1381 /* Verity only works on regular files */
1382 !S_ISREG(d_inode(metapath->dentry)->i_mode))
1383 return 0;
1384
1385 xattr_size = ovl_check_metacopy_xattr(ofs, metapath, &metacopy_data);
1386 if (xattr_size < 0)
1387 return xattr_size;
1388
1389 if (!xattr_size || !metacopy_data.digest_algo) {
1390 if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
1391 pr_warn_ratelimited("metacopy file '%pd' has no digest specified\n",
1392 metapath->dentry);
1393 return -EIO;
1394 }
1395 return 0;
1396 }
1397
1398 xattr_digest_size = ovl_metadata_digest_size(&metacopy_data);
1399
1400 err = ovl_ensure_verity_loaded(datapath);
1401 if (err < 0) {
1402 pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n",
1403 datapath->dentry);
1404 return -EIO;
1405 }
1406
1407 digest_size = fsverity_get_digest(d_inode(datapath->dentry), actual_digest,
1408 &verity_algo, NULL);
1409 if (digest_size == 0) {
1410 pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n", datapath->dentry);
1411 return -EIO;
1412 }
1413
1414 if (xattr_digest_size != digest_size ||
1415 metacopy_data.digest_algo != verity_algo ||
1416 memcmp(metacopy_data.digest, actual_digest, xattr_digest_size) != 0) {
1417 pr_warn_ratelimited("lower file '%pd' has the wrong fs-verity digest\n",
1418 datapath->dentry);
1419 return -EIO;
1420 }
1421
1422 return 0;
1423}
1424
0c71faf5
AL
1425int ovl_get_verity_digest(struct ovl_fs *ofs, struct path *src,
1426 struct ovl_metacopy *metacopy)
1427{
1428 int err, digest_size;
1429
1430 if (!ofs->config.verity_mode || !S_ISREG(d_inode(src->dentry)->i_mode))
1431 return 0;
1432
1433 err = ovl_ensure_verity_loaded(src);
1434 if (err < 0) {
1435 pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n",
1436 src->dentry);
1437 return -EIO;
1438 }
1439
1440 digest_size = fsverity_get_digest(d_inode(src->dentry),
1441 metacopy->digest, &metacopy->digest_algo, NULL);
1442 if (digest_size == 0 ||
1443 WARN_ON_ONCE(digest_size > FS_VERITY_MAX_DIGEST_SIZE)) {
1444 if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
1445 pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n",
1446 src->dentry);
1447 return -EIO;
1448 }
1449 return 0;
1450 }
1451
1452 metacopy->len += digest_size;
1453 return 0;
1454}
1455
335d3fc5
SD
1456/*
1457 * ovl_sync_status() - Check fs sync status for volatile mounts
1458 *
1459 * Returns 1 if this is not a volatile mount and a real sync is required.
1460 *
1461 * Returns 0 if syncing can be skipped because mount is volatile, and no errors
1462 * have occurred on the upperdir since the mount.
1463 *
1464 * Returns -errno if it is a volatile mount, and the error that occurred since
1465 * the last mount. If the error code changes, it'll return the latest error
1466 * code.
1467 */
1468
1469int ovl_sync_status(struct ovl_fs *ofs)
1470{
1471 struct vfsmount *mnt;
1472
1473 if (ovl_should_sync(ofs))
1474 return 1;
1475
1476 mnt = ovl_upper_mnt(ofs);
1477 if (!mnt)
1478 return 0;
1479
1480 return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
1481}
2878dffc
CB
1482
1483/*
1484 * ovl_copyattr() - copy inode attributes from layer to ovl inode
1485 *
1486 * When overlay copies inode information from an upper or lower layer to the
1487 * relevant overlay inode it will apply the idmapping of the upper or lower
1488 * layer when doing so ensuring that the ovl inode ownership will correctly
1489 * reflect the ownership of the idmapped upper or lower layer. For example, an
1490 * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
1491 * map any lower or upper inode owned by id 1001 to id 1000. These mapping
1492 * helpers are nops when the relevant layer isn't idmapped.
1493 */
1494void ovl_copyattr(struct inode *inode)
1495{
1496 struct path realpath;
1497 struct inode *realinode;
e67fe633 1498 struct mnt_idmap *real_idmap;
73db6a06
CB
1499 vfsuid_t vfsuid;
1500 vfsgid_t vfsgid;
2878dffc 1501
b2dd05f1 1502 realinode = ovl_i_path_real(inode, &realpath);
e67fe633 1503 real_idmap = mnt_idmap(realpath.mnt);
2878dffc 1504
f7621b11 1505 spin_lock(&inode->i_lock);
e67fe633
CB
1506 vfsuid = i_uid_into_vfsuid(real_idmap, realinode);
1507 vfsgid = i_gid_into_vfsgid(real_idmap, realinode);
73db6a06
CB
1508
1509 inode->i_uid = vfsuid_into_kuid(vfsuid);
1510 inode->i_gid = vfsgid_into_kgid(vfsgid);
2878dffc 1511 inode->i_mode = realinode->i_mode;
4ddbd0f1
JL
1512 inode_set_atime_to_ts(inode, inode_get_atime(realinode));
1513 inode_set_mtime_to_ts(inode, inode_get_mtime(realinode));
9aa71115 1514 inode_set_ctime_to_ts(inode, inode_get_ctime(realinode));
2878dffc 1515 i_size_write(inode, i_size_read(realinode));
f7621b11 1516 spin_unlock(&inode->i_lock);
2878dffc 1517}