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