dm-crypt: use __bio_add_page to add single page to clone bio
[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>
096a218a 13#include <linux/fileattr.h>
02bcd157 14#include <linux/uuid.h>
caf70cb2
AG
15#include <linux/namei.h>
16#include <linux/ratelimit.h>
bbb1e54d 17#include "overlayfs.h"
bbb1e54d
MS
18
19int ovl_want_write(struct dentry *dentry)
20{
21 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
08f4c7c8 22 return mnt_want_write(ovl_upper_mnt(ofs));
bbb1e54d
MS
23}
24
25void ovl_drop_write(struct dentry *dentry)
26{
27 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
08f4c7c8 28 mnt_drop_write(ovl_upper_mnt(ofs));
bbb1e54d
MS
29}
30
31struct dentry *ovl_workdir(struct dentry *dentry)
32{
33 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
34 return ofs->workdir;
35}
36
37const struct cred *ovl_override_creds(struct super_block *sb)
38{
39 struct ovl_fs *ofs = sb->s_fs_info;
40
41 return override_creds(ofs->creator_cred);
42}
43
e487d889
AG
44/*
45 * Check if underlying fs supports file handles and try to determine encoding
46 * type, in order to deduce maximum inode number used by fs.
47 *
48 * Return 0 if file handles are not supported.
49 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
50 * Return -1 if fs uses a non default encoding with unknown inode size.
51 */
52int ovl_can_decode_fh(struct super_block *sb)
02bcd157 53{
c846af05
MS
54 if (!capable(CAP_DAC_READ_SEARCH))
55 return 0;
56
9df085f3 57 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
e487d889
AG
58 return 0;
59
60 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
02bcd157
AG
61}
62
63struct dentry *ovl_indexdir(struct super_block *sb)
64{
65 struct ovl_fs *ofs = sb->s_fs_info;
66
67 return ofs->indexdir;
68}
69
f168f109
AG
70/* Index all files on copy up. For now only enabled for NFS export */
71bool ovl_index_all(struct super_block *sb)
72{
73 struct ovl_fs *ofs = sb->s_fs_info;
74
75 return ofs->config.nfs_export && ofs->config.index;
76}
77
78/* Verify lower origin on lookup. For now only enabled for NFS export */
79bool ovl_verify_lower(struct super_block *sb)
80{
81 struct ovl_fs *ofs = sb->s_fs_info;
82
83 return ofs->config.nfs_export && ofs->config.index;
84}
85
bbb1e54d
MS
86struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
87{
88 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
89 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
90
91 if (oe)
92 oe->numlower = numlower;
93
94 return oe;
95}
96
97bool ovl_dentry_remote(struct dentry *dentry)
98{
99 return dentry->d_flags &
7925dad8 100 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
bbb1e54d
MS
101}
102
f4288844
MS
103void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
104 unsigned int mask)
105{
106 struct ovl_entry *oe = OVL_E(dentry);
107 unsigned int i, flags = 0;
108
bccece1e
MS
109 if (upperdentry)
110 flags |= upperdentry->d_flags;
f4288844
MS
111 for (i = 0; i < oe->numlower; i++)
112 flags |= oe->lowerstack[i].dentry->d_flags;
113
114 spin_lock(&dentry->d_lock);
115 dentry->d_flags &= ~mask;
116 dentry->d_flags |= flags & mask;
117 spin_unlock(&dentry->d_lock);
118}
119
bbb1e54d
MS
120bool ovl_dentry_weird(struct dentry *dentry)
121{
122 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
123 DCACHE_MANAGE_TRANSIT |
124 DCACHE_OP_HASH |
125 DCACHE_OP_COMPARE);
126}
127
128enum ovl_path_type ovl_path_type(struct dentry *dentry)
129{
130 struct ovl_entry *oe = dentry->d_fsdata;
131 enum ovl_path_type type = 0;
132
09d8b586 133 if (ovl_dentry_upper(dentry)) {
bbb1e54d
MS
134 type = __OVL_PATH_UPPER;
135
136 /*
59548503 137 * Non-dir dentry can hold lower dentry of its copy up origin.
bbb1e54d 138 */
59548503 139 if (oe->numlower) {
60124877
VG
140 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
141 type |= __OVL_PATH_ORIGIN;
0b17c28a
VG
142 if (d_is_dir(dentry) ||
143 !ovl_has_upperdata(d_inode(dentry)))
59548503
AG
144 type |= __OVL_PATH_MERGE;
145 }
bbb1e54d
MS
146 } else {
147 if (oe->numlower > 1)
148 type |= __OVL_PATH_MERGE;
149 }
150 return type;
151}
152
153void ovl_path_upper(struct dentry *dentry, struct path *path)
154{
155 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
bbb1e54d 156
08f4c7c8 157 path->mnt = ovl_upper_mnt(ofs);
09d8b586 158 path->dentry = ovl_dentry_upper(dentry);
bbb1e54d
MS
159}
160
161void ovl_path_lower(struct dentry *dentry, struct path *path)
162{
163 struct ovl_entry *oe = dentry->d_fsdata;
164
b9343632
CR
165 if (oe->numlower) {
166 path->mnt = oe->lowerstack[0].layer->mnt;
167 path->dentry = oe->lowerstack[0].dentry;
168 } else {
169 *path = (struct path) { };
170 }
bbb1e54d
MS
171}
172
4f93b426
VG
173void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
174{
175 struct ovl_entry *oe = dentry->d_fsdata;
176
177 if (oe->numlower) {
178 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
179 path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
180 } else {
181 *path = (struct path) { };
182 }
183}
184
bbb1e54d
MS
185enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
186{
187 enum ovl_path_type type = ovl_path_type(dentry);
188
189 if (!OVL_TYPE_UPPER(type))
190 ovl_path_lower(dentry, path);
191 else
192 ovl_path_upper(dentry, path);
193
194 return type;
195}
196
1248ea4b
AG
197enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
198{
199 enum ovl_path_type type = ovl_path_type(dentry);
200
201 WARN_ON_ONCE(d_is_dir(dentry));
202
203 if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
204 ovl_path_lowerdata(dentry, path);
205 else
206 ovl_path_upper(dentry, path);
207
208 return type;
209}
210
bbb1e54d
MS
211struct dentry *ovl_dentry_upper(struct dentry *dentry)
212{
09d8b586 213 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
bbb1e54d
MS
214}
215
216struct dentry *ovl_dentry_lower(struct dentry *dentry)
217{
218 struct ovl_entry *oe = dentry->d_fsdata;
219
09d8b586 220 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
bbb1e54d
MS
221}
222
13464165 223const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
da309e8c
AG
224{
225 struct ovl_entry *oe = dentry->d_fsdata;
226
227 return oe->numlower ? oe->lowerstack[0].layer : NULL;
228}
229
647d253f
VG
230/*
231 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
597534e7 232 * depending on what is stored in lowerstack[0]. At times we need to find
647d253f
VG
233 * lower dentry which has data (and not metacopy dentry). This helper
234 * returns the lower data dentry.
235 */
236struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
237{
238 struct ovl_entry *oe = dentry->d_fsdata;
239
240 return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
241}
242
bbb1e54d
MS
243struct dentry *ovl_dentry_real(struct dentry *dentry)
244{
09d8b586 245 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
bbb1e54d
MS
246}
247
1d88f183
MS
248struct dentry *ovl_i_dentry_upper(struct inode *inode)
249{
250 return ovl_upperdentry_dereference(OVL_I(inode));
251}
252
ffa5723c
AG
253void ovl_i_path_real(struct inode *inode, struct path *path)
254{
255 path->dentry = ovl_i_dentry_upper(inode);
256 if (!path->dentry) {
257 path->dentry = OVL_I(inode)->lowerpath.dentry;
258 path->mnt = OVL_I(inode)->lowerpath.layer->mnt;
259 } else {
260 path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
261 }
262}
263
09d8b586 264struct inode *ovl_inode_upper(struct inode *inode)
25b7713a 265{
1d88f183 266 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
25b7713a 267
09d8b586
MS
268 return upperdentry ? d_inode(upperdentry) : NULL;
269}
25b7713a 270
09d8b586
MS
271struct inode *ovl_inode_lower(struct inode *inode)
272{
ffa5723c
AG
273 struct dentry *lowerdentry = OVL_I(inode)->lowerpath.dentry;
274
275 return lowerdentry ? d_inode(lowerdentry) : NULL;
09d8b586 276}
25b7713a 277
09d8b586
MS
278struct inode *ovl_inode_real(struct inode *inode)
279{
280 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
25b7713a
MS
281}
282
2664bd08
VG
283/* Return inode which contains lower data. Do not return metacopy */
284struct inode *ovl_inode_lowerdata(struct inode *inode)
285{
286 if (WARN_ON(!S_ISREG(inode->i_mode)))
287 return NULL;
288
289 return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
290}
09d8b586 291
4823d49c
VG
292/* Return real inode which contains data. Does not return metacopy inode */
293struct inode *ovl_inode_realdata(struct inode *inode)
294{
295 struct inode *upperinode;
296
297 upperinode = ovl_inode_upper(inode);
298 if (upperinode && ovl_has_upperdata(inode))
299 return upperinode;
300
301 return ovl_inode_lowerdata(inode);
302}
303
4edb83bb 304struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
bbb1e54d 305{
4edb83bb 306 return OVL_I(inode)->cache;
bbb1e54d
MS
307}
308
4edb83bb 309void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
bbb1e54d 310{
4edb83bb 311 OVL_I(inode)->cache = cache;
bbb1e54d
MS
312}
313
c62520a8
AG
314void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
315{
316 set_bit(flag, &OVL_E(dentry)->flags);
317}
318
319void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
320{
321 clear_bit(flag, &OVL_E(dentry)->flags);
322}
323
324bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
325{
326 return test_bit(flag, &OVL_E(dentry)->flags);
327}
328
bbb1e54d
MS
329bool ovl_dentry_is_opaque(struct dentry *dentry)
330{
c62520a8 331 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
bbb1e54d
MS
332}
333
334bool ovl_dentry_is_whiteout(struct dentry *dentry)
335{
336 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
337}
338
5cf5b477 339void ovl_dentry_set_opaque(struct dentry *dentry)
bbb1e54d 340{
c62520a8 341 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
bbb1e54d
MS
342}
343
55acc661 344/*
aa3ff3c1
AG
345 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
346 * to return positive, while there's no actual upper alias for the inode.
347 * Copy up code needs to know about the existence of the upper alias, so it
348 * can't use ovl_dentry_upper().
55acc661
MS
349 */
350bool ovl_dentry_has_upper_alias(struct dentry *dentry)
351{
c62520a8 352 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
55acc661
MS
353}
354
355void ovl_dentry_set_upper_alias(struct dentry *dentry)
356{
c62520a8 357 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
55acc661
MS
358}
359
0c288874
VG
360static bool ovl_should_check_upperdata(struct inode *inode)
361{
362 if (!S_ISREG(inode->i_mode))
363 return false;
364
365 if (!ovl_inode_lower(inode))
366 return false;
367
368 return true;
369}
370
371bool ovl_has_upperdata(struct inode *inode)
372{
373 if (!ovl_should_check_upperdata(inode))
374 return true;
375
376 if (!ovl_test_flag(OVL_UPPERDATA, inode))
377 return false;
378 /*
379 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
380 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
381 * if setting of OVL_UPPERDATA is visible, then effects of writes
382 * before that are visible too.
383 */
384 smp_rmb();
385 return true;
386}
387
388void ovl_set_upperdata(struct inode *inode)
389{
390 /*
391 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
392 * if OVL_UPPERDATA flag is visible, then effects of write operations
393 * before it are visible as well.
394 */
395 smp_wmb();
396 ovl_set_flag(OVL_UPPERDATA, inode);
397}
398
399/* Caller should hold ovl_inode->lock */
400bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
401{
402 if (!ovl_open_flags_need_copy_up(flags))
403 return false;
404
405 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
406}
407
408bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
409{
410 if (!ovl_open_flags_need_copy_up(flags))
411 return false;
412
413 return !ovl_has_upperdata(d_inode(dentry));
414}
415
a6c60655
MS
416bool ovl_redirect_dir(struct super_block *sb)
417{
418 struct ovl_fs *ofs = sb->s_fs_info;
419
21a22878 420 return ofs->config.redirect_dir && !ofs->noxattr;
a6c60655
MS
421}
422
423const char *ovl_dentry_get_redirect(struct dentry *dentry)
424{
cf31c463 425 return OVL_I(d_inode(dentry))->redirect;
a6c60655
MS
426}
427
428void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
429{
cf31c463 430 struct ovl_inode *oi = OVL_I(d_inode(dentry));
a6c60655 431
cf31c463
MS
432 kfree(oi->redirect);
433 oi->redirect = redirect;
a6c60655
MS
434}
435
09d8b586 436void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
bbb1e54d 437{
09d8b586 438 struct inode *upperinode = d_inode(upperdentry);
e6d2ebdd 439
09d8b586
MS
440 WARN_ON(OVL_I(inode)->__upperdentry);
441
25b7713a 442 /*
09d8b586 443 * Make sure upperdentry is consistent before making it visible
25b7713a
MS
444 */
445 smp_wmb();
09d8b586 446 OVL_I(inode)->__upperdentry = upperdentry;
31747eda 447 if (inode_unhashed(inode)) {
25b7713a 448 inode->i_private = upperinode;
bbb1e54d 449 __insert_inode_hash(inode, (unsigned long) upperinode);
25b7713a 450 }
bbb1e54d
MS
451}
452
65cd913e 453static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
bbb1e54d 454{
04a01ac7 455 struct inode *inode = d_inode(dentry);
bbb1e54d 456
04a01ac7 457 WARN_ON(!inode_is_locked(inode));
65cd913e 458 WARN_ON(!d_is_dir(dentry));
4edb83bb 459 /*
65cd913e
AG
460 * Version is used by readdir code to keep cache consistent.
461 * For merge dirs (or dirs with origin) all changes need to be noted.
462 * For non-merge dirs, cache contains only impure entries (i.e. ones
463 * which have been copied up and have origins), so only need to note
464 * changes to impure entries.
4edb83bb 465 */
1fa9c5c5 466 if (!ovl_dir_is_real(inode) || impurity)
4edb83bb 467 OVL_I(inode)->version++;
bbb1e54d
MS
468}
469
d9854c87
MS
470void ovl_dir_modified(struct dentry *dentry, bool impurity)
471{
472 /* Copy mtime/ctime */
2878dffc 473 ovl_copyattr(d_inode(dentry));
d9854c87 474
65cd913e 475 ovl_dir_version_inc(dentry, impurity);
d9854c87
MS
476}
477
1fa9c5c5 478u64 ovl_inode_version_get(struct inode *inode)
bbb1e54d 479{
04a01ac7
MS
480 WARN_ON(!inode_is_locked(inode));
481 return OVL_I(inode)->version;
bbb1e54d
MS
482}
483
484bool ovl_is_whiteout(struct dentry *dentry)
485{
486 struct inode *inode = dentry->d_inode;
487
488 return inode && IS_WHITEOUT(inode);
489}
490
2d343087 491struct file *ovl_path_open(const struct path *path, int flags)
bbb1e54d 492{
56230d95 493 struct inode *inode = d_inode(path->dentry);
4609e1f1 494 struct mnt_idmap *real_idmap = mnt_idmap(path->mnt);
56230d95
MS
495 int err, acc_mode;
496
497 if (flags & ~(O_ACCMODE | O_LARGEFILE))
498 BUG();
499
500 switch (flags & O_ACCMODE) {
501 case O_RDONLY:
502 acc_mode = MAY_READ;
503 break;
504 case O_WRONLY:
505 acc_mode = MAY_WRITE;
506 break;
507 default:
508 BUG();
509 }
510
4609e1f1 511 err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN);
56230d95
MS
512 if (err)
513 return ERR_PTR(err);
514
515 /* O_NOATIME is an optimization, don't fail if not permitted */
01beba79 516 if (inode_owner_or_capable(real_idmap, inode))
56230d95
MS
517 flags |= O_NOATIME;
518
519 return dentry_open(path, flags, current_cred());
bbb1e54d 520}
39d3d60a 521
0c288874
VG
522/* Caller should hold ovl_inode->lock */
523static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
524{
525 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
526
527 if (ovl_dentry_upper(dentry) &&
528 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
529 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
530 return true;
531
532 return false;
533}
534
535bool ovl_already_copied_up(struct dentry *dentry, int flags)
2002df85
VG
536{
537 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
538
539 /*
540 * Check if copy-up has happened as well as for upper alias (in
541 * case of hard links) is there.
542 *
543 * Both checks are lockless:
544 * - false negatives: will recheck under oi->lock
545 * - false positives:
546 * + ovl_dentry_upper() uses memory barriers to ensure the
547 * upper dentry is up-to-date
548 * + ovl_dentry_has_upper_alias() relies on locking of
549 * upper parent i_rwsem to prevent reordering copy-up
550 * with rename.
551 */
552 if (ovl_dentry_upper(dentry) &&
0c288874
VG
553 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
554 !ovl_dentry_needs_data_copy_up(dentry, flags))
2002df85
VG
555 return true;
556
557 return false;
558}
559
0c288874 560int ovl_copy_up_start(struct dentry *dentry, int flags)
39d3d60a 561{
1e92e307 562 struct inode *inode = d_inode(dentry);
39d3d60a
AG
563 int err;
564
531d3040 565 err = ovl_inode_lock_interruptible(inode);
0c288874 566 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
a015dafc 567 err = 1; /* Already copied up */
1e92e307 568 ovl_inode_unlock(inode);
39d3d60a 569 }
39d3d60a
AG
570
571 return err;
572}
573
574void ovl_copy_up_end(struct dentry *dentry)
575{
1e92e307 576 ovl_inode_unlock(d_inode(dentry));
39d3d60a 577}
82b749b2 578
2d343087 579bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)
b79e05aa
AG
580{
581 int res;
582
dad7017a 583 res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
b79e05aa
AG
584
585 /* Zero size value means "copied up but origin unknown" */
586 if (res >= 0)
587 return true;
588
589 return false;
590}
591
2d343087 592bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
dad7017a 593 enum ovl_xattr ox)
f3a15685
AG
594{
595 int res;
596 char val;
597
dad7017a 598 if (!d_is_dir(path->dentry))
f3a15685
AG
599 return false;
600
dad7017a 601 res = ovl_path_getxattr(ofs, path, ox, &val, 1);
f3a15685
AG
602 if (res == 1 && val == 'y')
603 return true;
604
605 return false;
606}
607
43d193f8
MS
608#define OVL_XATTR_OPAQUE_POSTFIX "opaque"
609#define OVL_XATTR_REDIRECT_POSTFIX "redirect"
610#define OVL_XATTR_ORIGIN_POSTFIX "origin"
611#define OVL_XATTR_IMPURE_POSTFIX "impure"
612#define OVL_XATTR_NLINK_POSTFIX "nlink"
613#define OVL_XATTR_UPPER_POSTFIX "upper"
614#define OVL_XATTR_METACOPY_POSTFIX "metacopy"
096a218a 615#define OVL_XATTR_PROTATTR_POSTFIX "protattr"
43d193f8
MS
616
617#define OVL_XATTR_TAB_ENTRY(x) \
2d2f2d73
MS
618 [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
619 [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
43d193f8 620
2d2f2d73 621const char *const ovl_xattr_table[][2] = {
43d193f8
MS
622 OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
623 OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
624 OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
625 OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
626 OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
627 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
628 OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
096a218a 629 OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
43d193f8
MS
630};
631
a0c236b1 632int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
43d193f8 633 enum ovl_xattr ox, const void *value, size_t size,
82b749b2
AG
634 int xerr)
635{
636 int err;
82b749b2
AG
637
638 if (ofs->noxattr)
639 return xerr;
640
c914c0e2 641 err = ovl_setxattr(ofs, upperdentry, ox, value, size);
82b749b2
AG
642
643 if (err == -EOPNOTSUPP) {
43d193f8 644 pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
82b749b2
AG
645 ofs->noxattr = true;
646 return xerr;
647 }
648
649 return err;
650}
f3a15685
AG
651
652int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
653{
a0c236b1 654 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
f3a15685 655 int err;
f3a15685 656
13c72075 657 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
f3a15685
AG
658 return 0;
659
660 /*
661 * Do not fail when upper doesn't support xattrs.
662 * Upper inodes won't have origin nor redirect xattr anyway.
663 */
a0c236b1 664 err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
f3a15685 665 if (!err)
13c72075 666 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
f3a15685
AG
667
668 return err;
669}
13c72075 670
096a218a
AG
671
672#define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
673
674void ovl_check_protattr(struct inode *inode, struct dentry *upper)
675{
676 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
677 u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
678 char buf[OVL_PROTATTR_MAX+1];
679 int res, n;
680
dad7017a
CB
681 res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
682 OVL_PROTATTR_MAX);
096a218a
AG
683 if (res < 0)
684 return;
685
686 /*
687 * Initialize inode flags from overlay.protattr xattr and upper inode
688 * flags. If upper inode has those fileattr flags set (i.e. from old
689 * kernel), we do not clear them on ovl_get_inode(), but we will clear
690 * them on next fileattr_set().
691 */
692 for (n = 0; n < res; n++) {
693 if (buf[n] == 'a')
694 iflags |= S_APPEND;
695 else if (buf[n] == 'i')
696 iflags |= S_IMMUTABLE;
697 else
698 break;
699 }
700
701 if (!res || n < res) {
702 pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
703 upper, res);
704 } else {
705 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
706 }
707}
708
709int ovl_set_protattr(struct inode *inode, struct dentry *upper,
710 struct fileattr *fa)
711{
712 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
713 char buf[OVL_PROTATTR_MAX];
714 int len = 0, err = 0;
715 u32 iflags = 0;
716
717 BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
718
719 if (fa->flags & FS_APPEND_FL) {
720 buf[len++] = 'a';
721 iflags |= S_APPEND;
722 }
723 if (fa->flags & FS_IMMUTABLE_FL) {
724 buf[len++] = 'i';
725 iflags |= S_IMMUTABLE;
726 }
727
728 /*
729 * Do not allow to set protection flags when upper doesn't support
730 * xattrs, because we do not set those fileattr flags on upper inode.
731 * Remove xattr if it exist and all protection flags are cleared.
732 */
733 if (len) {
734 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
735 buf, len, -EPERM);
736 } else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
c914c0e2 737 err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
096a218a
AG
738 if (err == -EOPNOTSUPP || err == -ENODATA)
739 err = 0;
740 }
741 if (err)
742 return err;
743
744 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
745
746 /* Mask out the fileattr flags that should not be set in upper inode */
747 fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
748 fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
749
750 return 0;
751}
752
ad0af710
AG
753/**
754 * Caller must hold a reference to inode to prevent it from being freed while
755 * it is marked inuse.
756 */
757bool ovl_inuse_trylock(struct dentry *dentry)
758{
759 struct inode *inode = d_inode(dentry);
760 bool locked = false;
761
762 spin_lock(&inode->i_lock);
763 if (!(inode->i_state & I_OVL_INUSE)) {
764 inode->i_state |= I_OVL_INUSE;
765 locked = true;
766 }
767 spin_unlock(&inode->i_lock);
768
769 return locked;
770}
771
772void ovl_inuse_unlock(struct dentry *dentry)
773{
774 if (dentry) {
775 struct inode *inode = d_inode(dentry);
776
777 spin_lock(&inode->i_lock);
778 WARN_ON(!(inode->i_state & I_OVL_INUSE));
779 inode->i_state &= ~I_OVL_INUSE;
780 spin_unlock(&inode->i_lock);
781 }
782}
5f8415d6 783
146d62e5
AG
784bool ovl_is_inuse(struct dentry *dentry)
785{
786 struct inode *inode = d_inode(dentry);
787 bool inuse;
788
789 spin_lock(&inode->i_lock);
790 inuse = (inode->i_state & I_OVL_INUSE);
791 spin_unlock(&inode->i_lock);
792
793 return inuse;
794}
795
24b33ee1
AG
796/*
797 * Does this overlay dentry need to be indexed on copy up?
798 */
799bool ovl_need_index(struct dentry *dentry)
800{
801 struct dentry *lower = ovl_dentry_lower(dentry);
802
803 if (!lower || !ovl_indexdir(dentry->d_sb))
804 return false;
805
fbd2d207 806 /* Index all files for NFS export and consistency verification */
016b720f 807 if (ovl_index_all(dentry->d_sb))
fbd2d207
AG
808 return true;
809
24b33ee1
AG
810 /* Index only lower hardlinks on copy up */
811 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
812 return true;
813
814 return false;
815}
816
9f4ec904 817/* Caller must hold OVL_I(inode)->lock */
caf70cb2
AG
818static void ovl_cleanup_index(struct dentry *dentry)
819{
1cdb0cb6 820 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
e7dd0e71
AG
821 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
822 struct inode *dir = indexdir->d_inode;
caf70cb2
AG
823 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
824 struct dentry *upperdentry = ovl_dentry_upper(dentry);
825 struct dentry *index = NULL;
826 struct inode *inode;
63e13252 827 struct qstr name = { };
caf70cb2
AG
828 int err;
829
1cdb0cb6 830 err = ovl_get_index_name(ofs, lowerdentry, &name);
caf70cb2
AG
831 if (err)
832 goto fail;
833
834 inode = d_inode(upperdentry);
89a17556 835 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
1bd0a3ae 836 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
caf70cb2
AG
837 upperdentry, inode->i_ino, inode->i_nlink);
838 /*
839 * We either have a bug with persistent union nlink or a lower
840 * hardlink was added while overlay is mounted. Adding a lower
841 * hardlink and then unlinking all overlay hardlinks would drop
842 * overlay nlink to zero before all upper inodes are unlinked.
843 * As a safety measure, when that situation is detected, set
844 * the overlay nlink to the index inode nlink minus one for the
845 * index entry itself.
846 */
847 set_nlink(d_inode(dentry), inode->i_nlink - 1);
848 ovl_set_nlink_upper(dentry);
849 goto out;
850 }
851
852 inode_lock_nested(dir, I_MUTEX_PARENT);
22f289ce 853 index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
caf70cb2 854 err = PTR_ERR(index);
e7dd0e71 855 if (IS_ERR(index)) {
9f4ec904 856 index = NULL;
e7dd0e71
AG
857 } else if (ovl_index_all(dentry->d_sb)) {
858 /* Whiteout orphan index to block future open by handle */
c21c839b
CX
859 err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
860 dir, index);
e7dd0e71
AG
861 } else {
862 /* Cleanup orphan index entries */
576bb263 863 err = ovl_cleanup(ofs, dir, index);
e7dd0e71 864 }
9f4ec904 865
caf70cb2
AG
866 inode_unlock(dir);
867 if (err)
868 goto fail;
869
870out:
63e13252 871 kfree(name.name);
caf70cb2
AG
872 dput(index);
873 return;
874
875fail:
1bd0a3ae 876 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
caf70cb2
AG
877 goto out;
878}
879
5f8415d6
AG
880/*
881 * Operations that change overlay inode and upper inode nlink need to be
882 * synchronized with copy up for persistent nlink accounting.
883 */
0e32992f 884int ovl_nlink_start(struct dentry *dentry)
5f8415d6 885{
1e92e307 886 struct inode *inode = d_inode(dentry);
5f8415d6
AG
887 const struct cred *old_cred;
888 int err;
889
1e92e307 890 if (WARN_ON(!inode))
0e32992f 891 return -ENOENT;
5f8415d6
AG
892
893 /*
894 * With inodes index is enabled, we store the union overlay nlink
24b33ee1 895 * in an xattr on the index inode. When whiting out an indexed lower,
5f8415d6
AG
896 * we need to decrement the overlay persistent nlink, but before the
897 * first copy up, we have no upper index inode to store the xattr.
898 *
24b33ee1 899 * As a workaround, before whiteout/rename over an indexed lower,
5f8415d6
AG
900 * copy up to create the upper index. Creating the upper index will
901 * initialize the overlay nlink, so it could be dropped if unlink
902 * or rename succeeds.
903 *
904 * TODO: implement metadata only index copy up when called with
905 * ovl_copy_up_flags(dentry, O_PATH).
906 */
24b33ee1 907 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
5f8415d6
AG
908 err = ovl_copy_up(dentry);
909 if (err)
910 return err;
911 }
912
531d3040 913 err = ovl_inode_lock_interruptible(inode);
5f8415d6
AG
914 if (err)
915 return err;
916
1e92e307 917 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
5f8415d6
AG
918 goto out;
919
920 old_cred = ovl_override_creds(dentry->d_sb);
921 /*
922 * The overlay inode nlink should be incremented/decremented IFF the
923 * upper operation succeeds, along with nlink change of upper inode.
924 * Therefore, before link/unlink/rename, we store the union nlink
925 * value relative to the upper inode nlink in an upper inode xattr.
926 */
927 err = ovl_set_nlink_upper(dentry);
928 revert_creds(old_cred);
929
930out:
931 if (err)
1e92e307 932 ovl_inode_unlock(inode);
5f8415d6
AG
933
934 return err;
935}
936
0e32992f 937void ovl_nlink_end(struct dentry *dentry)
5f8415d6 938{
1e92e307
AG
939 struct inode *inode = d_inode(dentry);
940
941 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
0e32992f 942 const struct cred *old_cred;
caf70cb2 943
0e32992f
AG
944 old_cred = ovl_override_creds(dentry->d_sb);
945 ovl_cleanup_index(dentry);
946 revert_creds(old_cred);
caf70cb2 947 }
0e32992f 948
1e92e307 949 ovl_inode_unlock(inode);
5f8415d6 950}
5820dc08
AG
951
952int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
953{
954 /* Workdir should not be the same as upperdir */
955 if (workdir == upperdir)
956 goto err;
957
958 /* Workdir should not be subdir of upperdir and vice versa */
959 if (lock_rename(workdir, upperdir) != NULL)
960 goto err_unlock;
961
962 return 0;
963
964err_unlock:
965 unlock_rename(workdir, upperdir);
966err:
1bd0a3ae 967 pr_err("failed to lock workdir+upperdir\n");
5820dc08
AG
968 return -EIO;
969}
9d3dfea3
VG
970
971/* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
2d343087 972int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path)
9d3dfea3
VG
973{
974 int res;
975
976 /* Only regular files can have metacopy xattr */
dad7017a 977 if (!S_ISREG(d_inode(path->dentry)->i_mode))
9d3dfea3
VG
978 return 0;
979
dad7017a 980 res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY, NULL, 0);
9d3dfea3
VG
981 if (res < 0) {
982 if (res == -ENODATA || res == -EOPNOTSUPP)
983 return 0;
87b2c60c
MS
984 /*
985 * getxattr on user.* may fail with EACCES in case there's no
986 * read permission on the inode. Not much we can do, other than
987 * tell the caller that this is not a metacopy inode.
988 */
989 if (ofs->config.userxattr && res == -EACCES)
990 return 0;
9d3dfea3
VG
991 goto out;
992 }
993
994 return 1;
995out:
1bd0a3ae 996 pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
9d3dfea3
VG
997 return res;
998}
67d756c2
VG
999
1000bool ovl_is_metacopy_dentry(struct dentry *dentry)
1001{
1002 struct ovl_entry *oe = dentry->d_fsdata;
1003
1004 if (!d_is_reg(dentry))
1005 return false;
1006
1007 if (ovl_dentry_upper(dentry)) {
1008 if (!ovl_has_upperdata(d_inode(dentry)))
1009 return true;
1010 return false;
1011 }
1012
1013 return (oe->numlower > 1);
1014}
0a2d0d3f 1015
2d343087 1016char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding)
993a0b2a
VG
1017{
1018 int res;
1019 char *s, *next, *buf = NULL;
1020
dad7017a 1021 res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
92f0d6c9 1022 if (res == -ENODATA || res == -EOPNOTSUPP)
993a0b2a 1023 return NULL;
0a2d0d3f 1024 if (res < 0)
92f0d6c9
MS
1025 goto fail;
1026 if (res == 0)
1027 goto invalid;
1028
1029 buf = kzalloc(res + padding + 1, GFP_KERNEL);
1030 if (!buf)
1031 return ERR_PTR(-ENOMEM);
1032
dad7017a 1033 res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
92f0d6c9
MS
1034 if (res < 0)
1035 goto fail;
0a2d0d3f
VG
1036 if (res == 0)
1037 goto invalid;
1038
1039 if (buf[0] == '/') {
1040 for (s = buf; *s++ == '/'; s = next) {
1041 next = strchrnul(s, '/');
1042 if (s == next)
1043 goto invalid;
1044 }
1045 } else {
1046 if (strchr(buf, '/') != NULL)
1047 goto invalid;
1048 }
1049
1050 return buf;
0a2d0d3f 1051invalid:
1bd0a3ae 1052 pr_warn_ratelimited("invalid redirect (%s)\n", buf);
0a2d0d3f 1053 res = -EINVAL;
92f0d6c9
MS
1054 goto err_free;
1055fail:
1056 pr_warn_ratelimited("failed to get redirect (%i)\n", res);
1057err_free:
993a0b2a
VG
1058 kfree(buf);
1059 return ERR_PTR(res);
0a2d0d3f 1060}
335d3fc5
SD
1061
1062/*
1063 * ovl_sync_status() - Check fs sync status for volatile mounts
1064 *
1065 * Returns 1 if this is not a volatile mount and a real sync is required.
1066 *
1067 * Returns 0 if syncing can be skipped because mount is volatile, and no errors
1068 * have occurred on the upperdir since the mount.
1069 *
1070 * Returns -errno if it is a volatile mount, and the error that occurred since
1071 * the last mount. If the error code changes, it'll return the latest error
1072 * code.
1073 */
1074
1075int ovl_sync_status(struct ovl_fs *ofs)
1076{
1077 struct vfsmount *mnt;
1078
1079 if (ovl_should_sync(ofs))
1080 return 1;
1081
1082 mnt = ovl_upper_mnt(ofs);
1083 if (!mnt)
1084 return 0;
1085
1086 return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
1087}
2878dffc
CB
1088
1089/*
1090 * ovl_copyattr() - copy inode attributes from layer to ovl inode
1091 *
1092 * When overlay copies inode information from an upper or lower layer to the
1093 * relevant overlay inode it will apply the idmapping of the upper or lower
1094 * layer when doing so ensuring that the ovl inode ownership will correctly
1095 * reflect the ownership of the idmapped upper or lower layer. For example, an
1096 * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
1097 * map any lower or upper inode owned by id 1001 to id 1000. These mapping
1098 * helpers are nops when the relevant layer isn't idmapped.
1099 */
1100void ovl_copyattr(struct inode *inode)
1101{
1102 struct path realpath;
1103 struct inode *realinode;
e67fe633 1104 struct mnt_idmap *real_idmap;
73db6a06
CB
1105 vfsuid_t vfsuid;
1106 vfsgid_t vfsgid;
2878dffc
CB
1107
1108 ovl_i_path_real(inode, &realpath);
1109 realinode = d_inode(realpath.dentry);
e67fe633 1110 real_idmap = mnt_idmap(realpath.mnt);
2878dffc 1111
e67fe633
CB
1112 vfsuid = i_uid_into_vfsuid(real_idmap, realinode);
1113 vfsgid = i_gid_into_vfsgid(real_idmap, realinode);
73db6a06
CB
1114
1115 inode->i_uid = vfsuid_into_kuid(vfsuid);
1116 inode->i_gid = vfsgid_into_kgid(vfsgid);
2878dffc
CB
1117 inode->i_mode = realinode->i_mode;
1118 inode->i_atime = realinode->i_atime;
1119 inode->i_mtime = realinode->i_mtime;
1120 inode->i_ctime = realinode->i_ctime;
1121 i_size_write(inode, i_size_read(realinode));
1122}