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