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