1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2011 Novell Inc.
4 * Copyright (C) 2016 Red Hat, Inc.
5 */
6
7 #include <linux/fs.h>
8 #include <linux/mount.h>
9 #include <linux/slab.h>
10 #include <linux/cred.h>
11 #include <linux/xattr.h>
12 #include <linux/exportfs.h>
13 #include <linux/uuid.h>
14 #include <linux/namei.h>
15 #include <linux/ratelimit.h>
16 #include "overlayfs.h"
17
ovl_want_write(struct dentry * dentry)18 int ovl_want_write(struct dentry *dentry)
19 {
20 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
21 return mnt_want_write(ovl_upper_mnt(ofs));
22 }
23
ovl_drop_write(struct dentry * dentry)24 void ovl_drop_write(struct dentry *dentry)
25 {
26 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
27 mnt_drop_write(ovl_upper_mnt(ofs));
28 }
29
ovl_workdir(struct dentry * dentry)30 struct dentry *ovl_workdir(struct dentry *dentry)
31 {
32 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
33 return ofs->workdir;
34 }
35
ovl_override_creds(struct super_block * sb)36 const struct cred *ovl_override_creds(struct super_block *sb)
37 {
38 struct ovl_fs *ofs = sb->s_fs_info;
39
40 if (!ofs->config.override_creds)
41 return NULL;
42 return override_creds(ofs->creator_cred);
43 }
44
ovl_revert_creds(struct super_block * sb,const struct cred * old_cred)45 void ovl_revert_creds(struct super_block *sb, const struct cred *old_cred)
46 {
47 if (old_cred)
48 revert_creds(old_cred);
49 }
50
51 /*
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 */
ovl_can_decode_fh(struct super_block * sb)59 int ovl_can_decode_fh(struct super_block *sb)
60 {
61 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
62 return 0;
63
64 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
65 }
66
ovl_indexdir(struct super_block * sb)67 struct dentry *ovl_indexdir(struct super_block *sb)
68 {
69 struct ovl_fs *ofs = sb->s_fs_info;
70
71 return ofs->indexdir;
72 }
73
74 /* Index all files on copy up. For now only enabled for NFS export */
ovl_index_all(struct super_block * sb)75 bool 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 */
ovl_verify_lower(struct super_block * sb)83 bool 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
ovl_alloc_entry(unsigned int numlower)90 struct 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
101 #define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
102
ovl_dentry_remote(struct dentry * dentry)103 bool ovl_dentry_remote(struct dentry *dentry)
104 {
105 return dentry->d_flags & OVL_D_REVALIDATE;
106 }
107
ovl_dentry_update_reval(struct dentry * dentry,struct dentry * realdentry)108 void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
109 {
110 if (!ovl_dentry_remote(realdentry))
111 return;
112
113 spin_lock(&dentry->d_lock);
114 dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
115 spin_unlock(&dentry->d_lock);
116 }
117
ovl_dentry_init_reval(struct dentry * dentry,struct dentry * upperdentry)118 void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry)
119 {
120 return ovl_dentry_init_flags(dentry, upperdentry, OVL_D_REVALIDATE);
121 }
122
ovl_dentry_init_flags(struct dentry * dentry,struct dentry * upperdentry,unsigned int mask)123 void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
124 unsigned int mask)
125 {
126 struct ovl_entry *oe = OVL_E(dentry);
127 unsigned int i, flags = 0;
128
129 if (upperdentry)
130 flags |= upperdentry->d_flags;
131 for (i = 0; i < oe->numlower; i++)
132 flags |= oe->lowerstack[i].dentry->d_flags;
133
134 spin_lock(&dentry->d_lock);
135 dentry->d_flags &= ~mask;
136 dentry->d_flags |= flags & mask;
137 spin_unlock(&dentry->d_lock);
138 }
139
ovl_dentry_weird(struct dentry * dentry)140 bool ovl_dentry_weird(struct dentry *dentry)
141 {
142 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
143 DCACHE_MANAGE_TRANSIT |
144 DCACHE_OP_HASH |
145 DCACHE_OP_COMPARE);
146 }
147
ovl_path_type(struct dentry * dentry)148 enum ovl_path_type ovl_path_type(struct dentry *dentry)
149 {
150 struct ovl_entry *oe = dentry->d_fsdata;
151 enum ovl_path_type type = 0;
152
153 if (ovl_dentry_upper(dentry)) {
154 type = __OVL_PATH_UPPER;
155
156 /*
157 * Non-dir dentry can hold lower dentry of its copy up origin.
158 */
159 if (oe->numlower) {
160 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
161 type |= __OVL_PATH_ORIGIN;
162 if (d_is_dir(dentry) ||
163 !ovl_has_upperdata(d_inode(dentry)))
164 type |= __OVL_PATH_MERGE;
165 }
166 } else {
167 if (oe->numlower > 1)
168 type |= __OVL_PATH_MERGE;
169 }
170 return type;
171 }
172
ovl_path_upper(struct dentry * dentry,struct path * path)173 void ovl_path_upper(struct dentry *dentry, struct path *path)
174 {
175 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
176
177 path->mnt = ovl_upper_mnt(ofs);
178 path->dentry = ovl_dentry_upper(dentry);
179 }
180
ovl_path_lower(struct dentry * dentry,struct path * path)181 void ovl_path_lower(struct dentry *dentry, struct path *path)
182 {
183 struct ovl_entry *oe = dentry->d_fsdata;
184
185 if (oe->numlower) {
186 path->mnt = oe->lowerstack[0].layer->mnt;
187 path->dentry = oe->lowerstack[0].dentry;
188 } else {
189 *path = (struct path) { };
190 }
191 }
192
ovl_path_lowerdata(struct dentry * dentry,struct path * path)193 void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
194 {
195 struct ovl_entry *oe = dentry->d_fsdata;
196
197 if (oe->numlower) {
198 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
199 path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
200 } else {
201 *path = (struct path) { };
202 }
203 }
204
ovl_path_real(struct dentry * dentry,struct path * path)205 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
206 {
207 enum ovl_path_type type = ovl_path_type(dentry);
208
209 if (!OVL_TYPE_UPPER(type))
210 ovl_path_lower(dentry, path);
211 else
212 ovl_path_upper(dentry, path);
213
214 return type;
215 }
216
ovl_dentry_upper(struct dentry * dentry)217 struct dentry *ovl_dentry_upper(struct dentry *dentry)
218 {
219 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
220 }
221
ovl_dentry_lower(struct dentry * dentry)222 struct dentry *ovl_dentry_lower(struct dentry *dentry)
223 {
224 struct ovl_entry *oe = dentry->d_fsdata;
225
226 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
227 }
228
ovl_layer_lower(struct dentry * dentry)229 const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
230 {
231 struct ovl_entry *oe = dentry->d_fsdata;
232
233 return oe->numlower ? oe->lowerstack[0].layer : NULL;
234 }
235
236 /*
237 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
238 * dependig on what is stored in lowerstack[0]. At times we need to find
239 * lower dentry which has data (and not metacopy dentry). This helper
240 * returns the lower data dentry.
241 */
ovl_dentry_lowerdata(struct dentry * dentry)242 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
243 {
244 struct ovl_entry *oe = dentry->d_fsdata;
245
246 return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
247 }
248
ovl_dentry_real(struct dentry * dentry)249 struct dentry *ovl_dentry_real(struct dentry *dentry)
250 {
251 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
252 }
253
ovl_i_dentry_upper(struct inode * inode)254 struct dentry *ovl_i_dentry_upper(struct inode *inode)
255 {
256 return ovl_upperdentry_dereference(OVL_I(inode));
257 }
258
ovl_inode_upper(struct inode * inode)259 struct inode *ovl_inode_upper(struct inode *inode)
260 {
261 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
262
263 return upperdentry ? d_inode(upperdentry) : NULL;
264 }
265
ovl_inode_lower(struct inode * inode)266 struct inode *ovl_inode_lower(struct inode *inode)
267 {
268 return OVL_I(inode)->lower;
269 }
270
ovl_inode_real(struct inode * inode)271 struct inode *ovl_inode_real(struct inode *inode)
272 {
273 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
274 }
275
276 /* Return inode which contains lower data. Do not return metacopy */
ovl_inode_lowerdata(struct inode * inode)277 struct inode *ovl_inode_lowerdata(struct inode *inode)
278 {
279 if (WARN_ON(!S_ISREG(inode->i_mode)))
280 return NULL;
281
282 return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
283 }
284
285 /* Return real inode which contains data. Does not return metacopy inode */
ovl_inode_realdata(struct inode * inode)286 struct inode *ovl_inode_realdata(struct inode *inode)
287 {
288 struct inode *upperinode;
289
290 upperinode = ovl_inode_upper(inode);
291 if (upperinode && ovl_has_upperdata(inode))
292 return upperinode;
293
294 return ovl_inode_lowerdata(inode);
295 }
296
ovl_dir_cache(struct inode * inode)297 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
298 {
299 return OVL_I(inode)->cache;
300 }
301
ovl_set_dir_cache(struct inode * inode,struct ovl_dir_cache * cache)302 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
303 {
304 OVL_I(inode)->cache = cache;
305 }
306
ovl_dentry_set_flag(unsigned long flag,struct dentry * dentry)307 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
308 {
309 set_bit(flag, &OVL_E(dentry)->flags);
310 }
311
ovl_dentry_clear_flag(unsigned long flag,struct dentry * dentry)312 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
313 {
314 clear_bit(flag, &OVL_E(dentry)->flags);
315 }
316
ovl_dentry_test_flag(unsigned long flag,struct dentry * dentry)317 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
318 {
319 return test_bit(flag, &OVL_E(dentry)->flags);
320 }
321
ovl_dentry_is_opaque(struct dentry * dentry)322 bool ovl_dentry_is_opaque(struct dentry *dentry)
323 {
324 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
325 }
326
ovl_dentry_is_whiteout(struct dentry * dentry)327 bool ovl_dentry_is_whiteout(struct dentry *dentry)
328 {
329 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
330 }
331
ovl_dentry_set_opaque(struct dentry * dentry)332 void ovl_dentry_set_opaque(struct dentry *dentry)
333 {
334 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
335 }
336
337 /*
338 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
339 * to return positive, while there's no actual upper alias for the inode.
340 * Copy up code needs to know about the existence of the upper alias, so it
341 * can't use ovl_dentry_upper().
342 */
ovl_dentry_has_upper_alias(struct dentry * dentry)343 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
344 {
345 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
346 }
347
ovl_dentry_set_upper_alias(struct dentry * dentry)348 void ovl_dentry_set_upper_alias(struct dentry *dentry)
349 {
350 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
351 }
352
ovl_should_check_upperdata(struct inode * inode)353 static bool ovl_should_check_upperdata(struct inode *inode)
354 {
355 if (!S_ISREG(inode->i_mode))
356 return false;
357
358 if (!ovl_inode_lower(inode))
359 return false;
360
361 return true;
362 }
363
ovl_has_upperdata(struct inode * inode)364 bool ovl_has_upperdata(struct inode *inode)
365 {
366 if (!ovl_should_check_upperdata(inode))
367 return true;
368
369 if (!ovl_test_flag(OVL_UPPERDATA, inode))
370 return false;
371 /*
372 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
373 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
374 * if setting of OVL_UPPERDATA is visible, then effects of writes
375 * before that are visible too.
376 */
377 smp_rmb();
378 return true;
379 }
380
ovl_set_upperdata(struct inode * inode)381 void ovl_set_upperdata(struct inode *inode)
382 {
383 /*
384 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
385 * if OVL_UPPERDATA flag is visible, then effects of write operations
386 * before it are visible as well.
387 */
388 smp_wmb();
389 ovl_set_flag(OVL_UPPERDATA, inode);
390 }
391
392 /* Caller should hold ovl_inode->lock */
ovl_dentry_needs_data_copy_up_locked(struct dentry * dentry,int flags)393 bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
394 {
395 if (!ovl_open_flags_need_copy_up(flags))
396 return false;
397
398 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
399 }
400
ovl_dentry_needs_data_copy_up(struct dentry * dentry,int flags)401 bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
402 {
403 if (!ovl_open_flags_need_copy_up(flags))
404 return false;
405
406 return !ovl_has_upperdata(d_inode(dentry));
407 }
408
ovl_redirect_dir(struct super_block * sb)409 bool ovl_redirect_dir(struct super_block *sb)
410 {
411 struct ovl_fs *ofs = sb->s_fs_info;
412
413 return ofs->config.redirect_dir && !ofs->noxattr;
414 }
415
ovl_dentry_get_redirect(struct dentry * dentry)416 const char *ovl_dentry_get_redirect(struct dentry *dentry)
417 {
418 return OVL_I(d_inode(dentry))->redirect;
419 }
420
ovl_dentry_set_redirect(struct dentry * dentry,const char * redirect)421 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
422 {
423 struct ovl_inode *oi = OVL_I(d_inode(dentry));
424
425 kfree(oi->redirect);
426 oi->redirect = redirect;
427 }
428
ovl_inode_update(struct inode * inode,struct dentry * upperdentry)429 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
430 {
431 struct inode *upperinode = d_inode(upperdentry);
432
433 WARN_ON(OVL_I(inode)->__upperdentry);
434
435 /*
436 * Make sure upperdentry is consistent before making it visible
437 */
438 smp_wmb();
439 OVL_I(inode)->__upperdentry = upperdentry;
440 if (inode_unhashed(inode)) {
441 inode->i_private = upperinode;
442 __insert_inode_hash(inode, (unsigned long) upperinode);
443 }
444 }
445
ovl_dir_version_inc(struct dentry * dentry,bool impurity)446 static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
447 {
448 struct inode *inode = d_inode(dentry);
449
450 WARN_ON(!inode_is_locked(inode));
451 WARN_ON(!d_is_dir(dentry));
452 /*
453 * Version is used by readdir code to keep cache consistent.
454 * For merge dirs (or dirs with origin) all changes need to be noted.
455 * For non-merge dirs, cache contains only impure entries (i.e. ones
456 * which have been copied up and have origins), so only need to note
457 * changes to impure entries.
458 */
459 if (!ovl_dir_is_real(dentry) || impurity)
460 OVL_I(inode)->version++;
461 }
462
ovl_dir_modified(struct dentry * dentry,bool impurity)463 void ovl_dir_modified(struct dentry *dentry, bool impurity)
464 {
465 /* Copy mtime/ctime */
466 ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
467
468 ovl_dir_version_inc(dentry, impurity);
469 }
470
ovl_dentry_version_get(struct dentry * dentry)471 u64 ovl_dentry_version_get(struct dentry *dentry)
472 {
473 struct inode *inode = d_inode(dentry);
474
475 WARN_ON(!inode_is_locked(inode));
476 return OVL_I(inode)->version;
477 }
478
ovl_is_whiteout(struct dentry * dentry)479 bool ovl_is_whiteout(struct dentry *dentry)
480 {
481 struct inode *inode = dentry->d_inode;
482
483 return inode && IS_WHITEOUT(inode);
484 }
485
ovl_path_open(struct path * path,int flags)486 struct file *ovl_path_open(struct path *path, int flags)
487 {
488 struct inode *inode = d_inode(path->dentry);
489 int err, acc_mode;
490
491 if (flags & ~(O_ACCMODE | O_LARGEFILE))
492 BUG();
493
494 switch (flags & O_ACCMODE) {
495 case O_RDONLY:
496 acc_mode = MAY_READ;
497 break;
498 case O_WRONLY:
499 acc_mode = MAY_WRITE;
500 break;
501 default:
502 BUG();
503 }
504
505 err = inode_permission(inode, acc_mode | MAY_OPEN);
506 if (err)
507 return ERR_PTR(err);
508
509 /* O_NOATIME is an optimization, don't fail if not permitted */
510 if (inode_owner_or_capable(inode))
511 flags |= O_NOATIME;
512
513 return dentry_open(path, flags, current_cred());
514 }
515
516 /* Caller should hold ovl_inode->lock */
ovl_already_copied_up_locked(struct dentry * dentry,int flags)517 static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
518 {
519 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
520
521 if (ovl_dentry_upper(dentry) &&
522 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
523 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
524 return true;
525
526 return false;
527 }
528
ovl_already_copied_up(struct dentry * dentry,int flags)529 bool ovl_already_copied_up(struct dentry *dentry, int flags)
530 {
531 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
532
533 /*
534 * Check if copy-up has happened as well as for upper alias (in
535 * case of hard links) is there.
536 *
537 * Both checks are lockless:
538 * - false negatives: will recheck under oi->lock
539 * - false positives:
540 * + ovl_dentry_upper() uses memory barriers to ensure the
541 * upper dentry is up-to-date
542 * + ovl_dentry_has_upper_alias() relies on locking of
543 * upper parent i_rwsem to prevent reordering copy-up
544 * with rename.
545 */
546 if (ovl_dentry_upper(dentry) &&
547 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
548 !ovl_dentry_needs_data_copy_up(dentry, flags))
549 return true;
550
551 return false;
552 }
553
ovl_copy_up_start(struct dentry * dentry,int flags)554 int ovl_copy_up_start(struct dentry *dentry, int flags)
555 {
556 struct inode *inode = d_inode(dentry);
557 int err;
558
559 err = ovl_inode_lock_interruptible(inode);
560 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
561 err = 1; /* Already copied up */
562 ovl_inode_unlock(inode);
563 }
564
565 return err;
566 }
567
ovl_copy_up_end(struct dentry * dentry)568 void ovl_copy_up_end(struct dentry *dentry)
569 {
570 ovl_inode_unlock(d_inode(dentry));
571 }
572
ovl_check_origin_xattr(struct ovl_fs * ofs,struct dentry * dentry)573 bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry)
574 {
575 ssize_t res;
576
577 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_ORIGIN, NULL, 0);
578
579 /* Zero size value means "copied up but origin unknown" */
580 if (res >= 0)
581 return true;
582
583 return false;
584 }
585
ovl_check_dir_xattr(struct super_block * sb,struct dentry * dentry,enum ovl_xattr ox)586 bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
587 enum ovl_xattr ox)
588 {
589 ssize_t res;
590 char val;
591
592 if (!d_is_dir(dentry))
593 return false;
594
595 res = ovl_do_getxattr(OVL_FS(sb), dentry, ox, &val, 1);
596 if (res == 1 && val == 'y')
597 return true;
598
599 return false;
600 }
601
602 #define OVL_XATTR_OPAQUE_POSTFIX "opaque"
603 #define OVL_XATTR_REDIRECT_POSTFIX "redirect"
604 #define OVL_XATTR_ORIGIN_POSTFIX "origin"
605 #define OVL_XATTR_IMPURE_POSTFIX "impure"
606 #define OVL_XATTR_NLINK_POSTFIX "nlink"
607 #define OVL_XATTR_UPPER_POSTFIX "upper"
608 #define OVL_XATTR_METACOPY_POSTFIX "metacopy"
609
610 #define OVL_XATTR_TAB_ENTRY(x) \
611 [x] = OVL_XATTR_PREFIX x ## _POSTFIX
612
613 const char *ovl_xattr_table[] = {
614 OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
615 OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
616 OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
617 OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
618 OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
619 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
620 OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
621 };
622
ovl_check_setxattr(struct dentry * dentry,struct dentry * upperdentry,enum ovl_xattr ox,const void * value,size_t size,int xerr)623 int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
624 enum ovl_xattr ox, const void *value, size_t size,
625 int xerr)
626 {
627 int err;
628 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
629
630 if (ofs->noxattr)
631 return xerr;
632
633 err = ovl_do_setxattr(ofs, upperdentry, ox, value, size);
634
635 if (err == -EOPNOTSUPP) {
636 pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
637 ofs->noxattr = true;
638 return xerr;
639 }
640
641 return err;
642 }
643
ovl_set_impure(struct dentry * dentry,struct dentry * upperdentry)644 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
645 {
646 int err;
647
648 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
649 return 0;
650
651 /*
652 * Do not fail when upper doesn't support xattrs.
653 * Upper inodes won't have origin nor redirect xattr anyway.
654 */
655 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
656 "y", 1, 0);
657 if (!err)
658 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
659
660 return err;
661 }
662
663 /**
664 * Caller must hold a reference to inode to prevent it from being freed while
665 * it is marked inuse.
666 */
ovl_inuse_trylock(struct dentry * dentry)667 bool ovl_inuse_trylock(struct dentry *dentry)
668 {
669 struct inode *inode = d_inode(dentry);
670 bool locked = false;
671
672 spin_lock(&inode->i_lock);
673 if (!(inode->i_state & I_OVL_INUSE)) {
674 inode->i_state |= I_OVL_INUSE;
675 locked = true;
676 }
677 spin_unlock(&inode->i_lock);
678
679 return locked;
680 }
681
ovl_inuse_unlock(struct dentry * dentry)682 void ovl_inuse_unlock(struct dentry *dentry)
683 {
684 if (dentry) {
685 struct inode *inode = d_inode(dentry);
686
687 spin_lock(&inode->i_lock);
688 WARN_ON(!(inode->i_state & I_OVL_INUSE));
689 inode->i_state &= ~I_OVL_INUSE;
690 spin_unlock(&inode->i_lock);
691 }
692 }
693
ovl_is_inuse(struct dentry * dentry)694 bool ovl_is_inuse(struct dentry *dentry)
695 {
696 struct inode *inode = d_inode(dentry);
697 bool inuse;
698
699 spin_lock(&inode->i_lock);
700 inuse = (inode->i_state & I_OVL_INUSE);
701 spin_unlock(&inode->i_lock);
702
703 return inuse;
704 }
705
706 /*
707 * Does this overlay dentry need to be indexed on copy up?
708 */
ovl_need_index(struct dentry * dentry)709 bool ovl_need_index(struct dentry *dentry)
710 {
711 struct dentry *lower = ovl_dentry_lower(dentry);
712
713 if (!lower || !ovl_indexdir(dentry->d_sb))
714 return false;
715
716 /* Index all files for NFS export and consistency verification */
717 if (ovl_index_all(dentry->d_sb))
718 return true;
719
720 /* Index only lower hardlinks on copy up */
721 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
722 return true;
723
724 return false;
725 }
726
727 /* Caller must hold OVL_I(inode)->lock */
ovl_cleanup_index(struct dentry * dentry)728 static void ovl_cleanup_index(struct dentry *dentry)
729 {
730 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
731 struct inode *dir = indexdir->d_inode;
732 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
733 struct dentry *upperdentry = ovl_dentry_upper(dentry);
734 struct dentry *index = NULL;
735 struct inode *inode;
736 struct qstr name = { };
737 int err;
738
739 err = ovl_get_index_name(lowerdentry, &name);
740 if (err)
741 goto fail;
742
743 inode = d_inode(upperdentry);
744 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
745 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
746 upperdentry, inode->i_ino, inode->i_nlink);
747 /*
748 * We either have a bug with persistent union nlink or a lower
749 * hardlink was added while overlay is mounted. Adding a lower
750 * hardlink and then unlinking all overlay hardlinks would drop
751 * overlay nlink to zero before all upper inodes are unlinked.
752 * As a safety measure, when that situation is detected, set
753 * the overlay nlink to the index inode nlink minus one for the
754 * index entry itself.
755 */
756 set_nlink(d_inode(dentry), inode->i_nlink - 1);
757 ovl_set_nlink_upper(dentry);
758 goto out;
759 }
760
761 inode_lock_nested(dir, I_MUTEX_PARENT);
762 index = lookup_one_len(name.name, indexdir, name.len);
763 err = PTR_ERR(index);
764 if (IS_ERR(index)) {
765 index = NULL;
766 } else if (ovl_index_all(dentry->d_sb)) {
767 /* Whiteout orphan index to block future open by handle */
768 err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
769 dir, index);
770 } else {
771 /* Cleanup orphan index entries */
772 err = ovl_cleanup(dir, index);
773 }
774
775 inode_unlock(dir);
776 if (err)
777 goto fail;
778
779 out:
780 kfree(name.name);
781 dput(index);
782 return;
783
784 fail:
785 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
786 goto out;
787 }
788
789 /*
790 * Operations that change overlay inode and upper inode nlink need to be
791 * synchronized with copy up for persistent nlink accounting.
792 */
ovl_nlink_start(struct dentry * dentry)793 int ovl_nlink_start(struct dentry *dentry)
794 {
795 struct inode *inode = d_inode(dentry);
796 const struct cred *old_cred;
797 int err;
798
799 if (WARN_ON(!inode))
800 return -ENOENT;
801
802 /*
803 * With inodes index is enabled, we store the union overlay nlink
804 * in an xattr on the index inode. When whiting out an indexed lower,
805 * we need to decrement the overlay persistent nlink, but before the
806 * first copy up, we have no upper index inode to store the xattr.
807 *
808 * As a workaround, before whiteout/rename over an indexed lower,
809 * copy up to create the upper index. Creating the upper index will
810 * initialize the overlay nlink, so it could be dropped if unlink
811 * or rename succeeds.
812 *
813 * TODO: implement metadata only index copy up when called with
814 * ovl_copy_up_flags(dentry, O_PATH).
815 */
816 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
817 err = ovl_copy_up(dentry);
818 if (err)
819 return err;
820 }
821
822 err = ovl_inode_lock_interruptible(inode);
823 if (err)
824 return err;
825
826 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
827 goto out;
828
829 old_cred = ovl_override_creds(dentry->d_sb);
830 /*
831 * The overlay inode nlink should be incremented/decremented IFF the
832 * upper operation succeeds, along with nlink change of upper inode.
833 * Therefore, before link/unlink/rename, we store the union nlink
834 * value relative to the upper inode nlink in an upper inode xattr.
835 */
836 err = ovl_set_nlink_upper(dentry);
837 ovl_revert_creds(dentry->d_sb, old_cred);
838
839 out:
840 if (err)
841 ovl_inode_unlock(inode);
842
843 return err;
844 }
845
ovl_nlink_end(struct dentry * dentry)846 void ovl_nlink_end(struct dentry *dentry)
847 {
848 struct inode *inode = d_inode(dentry);
849
850 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
851 const struct cred *old_cred;
852
853 old_cred = ovl_override_creds(dentry->d_sb);
854 ovl_cleanup_index(dentry);
855 ovl_revert_creds(dentry->d_sb, old_cred);
856 }
857
858 ovl_inode_unlock(inode);
859 }
860
ovl_lock_rename_workdir(struct dentry * workdir,struct dentry * upperdir)861 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
862 {
863 /* Workdir should not be the same as upperdir */
864 if (workdir == upperdir)
865 goto err;
866
867 /* Workdir should not be subdir of upperdir and vice versa */
868 if (lock_rename(workdir, upperdir) != NULL)
869 goto err_unlock;
870
871 return 0;
872
873 err_unlock:
874 unlock_rename(workdir, upperdir);
875 err:
876 pr_err("failed to lock workdir+upperdir\n");
877 return -EIO;
878 }
879
880 /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
ovl_check_metacopy_xattr(struct ovl_fs * ofs,struct dentry * dentry)881 int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry)
882 {
883 ssize_t res;
884
885 /* Only regular files can have metacopy xattr */
886 if (!S_ISREG(d_inode(dentry)->i_mode))
887 return 0;
888
889 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_METACOPY, NULL, 0);
890 if (res < 0) {
891 if (res == -ENODATA || res == -EOPNOTSUPP)
892 return 0;
893 goto out;
894 }
895
896 return 1;
897 out:
898 pr_warn_ratelimited("failed to get metacopy (%zi)\n", res);
899 return res;
900 }
901
ovl_is_metacopy_dentry(struct dentry * dentry)902 bool ovl_is_metacopy_dentry(struct dentry *dentry)
903 {
904 struct ovl_entry *oe = dentry->d_fsdata;
905
906 if (!d_is_reg(dentry))
907 return false;
908
909 if (ovl_dentry_upper(dentry)) {
910 if (!ovl_has_upperdata(d_inode(dentry)))
911 return true;
912 return false;
913 }
914
915 return (oe->numlower > 1);
916 }
917
ovl_get_redirect_xattr(struct ovl_fs * ofs,struct dentry * dentry,int padding)918 char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
919 int padding)
920 {
921 int res;
922 char *s, *next, *buf = NULL;
923
924 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, NULL, 0);
925 if (res == -ENODATA || res == -EOPNOTSUPP)
926 return NULL;
927 if (res < 0)
928 goto fail;
929 if (res == 0)
930 goto invalid;
931
932 buf = kzalloc(res + padding + 1, GFP_KERNEL);
933 if (!buf)
934 return ERR_PTR(-ENOMEM);
935
936 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, buf, res);
937 if (res < 0)
938 goto fail;
939 if (res == 0)
940 goto invalid;
941
942 if (buf[0] == '/') {
943 for (s = buf; *s++ == '/'; s = next) {
944 next = strchrnul(s, '/');
945 if (s == next)
946 goto invalid;
947 }
948 } else {
949 if (strchr(buf, '/') != NULL)
950 goto invalid;
951 }
952
953 return buf;
954 invalid:
955 pr_warn_ratelimited("invalid redirect (%s)\n", buf);
956 res = -EINVAL;
957 goto err_free;
958 fail:
959 pr_warn_ratelimited("failed to get redirect (%i)\n", res);
960 err_free:
961 kfree(buf);
962 return ERR_PTR(res);
963 }
964
965 /*
966 * ovl_sync_status() - Check fs sync status for volatile mounts
967 *
968 * Returns 1 if this is not a volatile mount and a real sync is required.
969 *
970 * Returns 0 if syncing can be skipped because mount is volatile, and no errors
971 * have occurred on the upperdir since the mount.
972 *
973 * Returns -errno if it is a volatile mount, and the error that occurred since
974 * the last mount. If the error code changes, it'll return the latest error
975 * code.
976 */
977
ovl_sync_status(struct ovl_fs * ofs)978 int ovl_sync_status(struct ovl_fs *ofs)
979 {
980 struct vfsmount *mnt;
981
982 if (ovl_should_sync(ofs))
983 return 1;
984
985 mnt = ovl_upper_mnt(ofs);
986 if (!mnt)
987 return 0;
988
989 return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
990 }
991