Commit | Line | Data |
---|---|---|
bbb1e54d MS |
1 | /* |
2 | * Copyright (C) 2011 Novell Inc. | |
3 | * Copyright (C) 2016 Red Hat, Inc. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License version 2 as published by | |
7 | * the Free Software Foundation. | |
8 | */ | |
9 | ||
10 | #include <linux/fs.h> | |
11 | #include <linux/mount.h> | |
12 | #include <linux/slab.h> | |
5b825c3a | 13 | #include <linux/cred.h> |
bbb1e54d | 14 | #include <linux/xattr.h> |
02bcd157 AG |
15 | #include <linux/exportfs.h> |
16 | #include <linux/uuid.h> | |
caf70cb2 AG |
17 | #include <linux/namei.h> |
18 | #include <linux/ratelimit.h> | |
bbb1e54d | 19 | #include "overlayfs.h" |
bbb1e54d MS |
20 | |
21 | int ovl_want_write(struct dentry *dentry) | |
22 | { | |
23 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
24 | return mnt_want_write(ofs->upper_mnt); | |
25 | } | |
26 | ||
27 | void ovl_drop_write(struct dentry *dentry) | |
28 | { | |
29 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
30 | mnt_drop_write(ofs->upper_mnt); | |
31 | } | |
32 | ||
33 | struct dentry *ovl_workdir(struct dentry *dentry) | |
34 | { | |
35 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
36 | return ofs->workdir; | |
37 | } | |
38 | ||
39 | const struct cred *ovl_override_creds(struct super_block *sb) | |
40 | { | |
41 | struct ovl_fs *ofs = sb->s_fs_info; | |
42 | ||
43 | return override_creds(ofs->creator_cred); | |
44 | } | |
45 | ||
7bcd74b9 AG |
46 | struct super_block *ovl_same_sb(struct super_block *sb) |
47 | { | |
48 | struct ovl_fs *ofs = sb->s_fs_info; | |
49 | ||
5148626b AG |
50 | if (!ofs->numlowerfs) |
51 | return ofs->upper_mnt->mnt_sb; | |
52 | else if (ofs->numlowerfs == 1 && !ofs->upper_mnt) | |
53 | return ofs->lower_fs[0].sb; | |
54 | else | |
55 | return NULL; | |
7bcd74b9 AG |
56 | } |
57 | ||
e487d889 AG |
58 | /* |
59 | * Check if underlying fs supports file handles and try to determine encoding | |
60 | * type, in order to deduce maximum inode number used by fs. | |
61 | * | |
62 | * Return 0 if file handles are not supported. | |
63 | * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding. | |
64 | * Return -1 if fs uses a non default encoding with unknown inode size. | |
65 | */ | |
66 | int ovl_can_decode_fh(struct super_block *sb) | |
02bcd157 | 67 | { |
e487d889 AG |
68 | if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry || |
69 | uuid_is_null(&sb->s_uuid)) | |
70 | return 0; | |
71 | ||
72 | return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN; | |
02bcd157 AG |
73 | } |
74 | ||
75 | struct dentry *ovl_indexdir(struct super_block *sb) | |
76 | { | |
77 | struct ovl_fs *ofs = sb->s_fs_info; | |
78 | ||
79 | return ofs->indexdir; | |
80 | } | |
81 | ||
f168f109 AG |
82 | /* Index all files on copy up. For now only enabled for NFS export */ |
83 | bool ovl_index_all(struct super_block *sb) | |
84 | { | |
85 | struct ovl_fs *ofs = sb->s_fs_info; | |
86 | ||
87 | return ofs->config.nfs_export && ofs->config.index; | |
88 | } | |
89 | ||
90 | /* Verify lower origin on lookup. For now only enabled for NFS export */ | |
91 | bool ovl_verify_lower(struct super_block *sb) | |
92 | { | |
93 | struct ovl_fs *ofs = sb->s_fs_info; | |
94 | ||
95 | return ofs->config.nfs_export && ofs->config.index; | |
96 | } | |
97 | ||
bbb1e54d MS |
98 | struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
99 | { | |
100 | size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); | |
101 | struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); | |
102 | ||
103 | if (oe) | |
104 | oe->numlower = numlower; | |
105 | ||
106 | return oe; | |
107 | } | |
108 | ||
109 | bool ovl_dentry_remote(struct dentry *dentry) | |
110 | { | |
111 | return dentry->d_flags & | |
112 | (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE | | |
113 | DCACHE_OP_REAL); | |
114 | } | |
115 | ||
116 | bool ovl_dentry_weird(struct dentry *dentry) | |
117 | { | |
118 | return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | | |
119 | DCACHE_MANAGE_TRANSIT | | |
120 | DCACHE_OP_HASH | | |
121 | DCACHE_OP_COMPARE); | |
122 | } | |
123 | ||
124 | enum ovl_path_type ovl_path_type(struct dentry *dentry) | |
125 | { | |
126 | struct ovl_entry *oe = dentry->d_fsdata; | |
127 | enum ovl_path_type type = 0; | |
128 | ||
09d8b586 | 129 | if (ovl_dentry_upper(dentry)) { |
bbb1e54d MS |
130 | type = __OVL_PATH_UPPER; |
131 | ||
132 | /* | |
59548503 | 133 | * Non-dir dentry can hold lower dentry of its copy up origin. |
bbb1e54d | 134 | */ |
59548503 AG |
135 | if (oe->numlower) { |
136 | type |= __OVL_PATH_ORIGIN; | |
137 | if (d_is_dir(dentry)) | |
138 | type |= __OVL_PATH_MERGE; | |
139 | } | |
bbb1e54d MS |
140 | } else { |
141 | if (oe->numlower > 1) | |
142 | type |= __OVL_PATH_MERGE; | |
143 | } | |
144 | return type; | |
145 | } | |
146 | ||
147 | void ovl_path_upper(struct dentry *dentry, struct path *path) | |
148 | { | |
149 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
bbb1e54d MS |
150 | |
151 | path->mnt = ofs->upper_mnt; | |
09d8b586 | 152 | path->dentry = ovl_dentry_upper(dentry); |
bbb1e54d MS |
153 | } |
154 | ||
155 | void ovl_path_lower(struct dentry *dentry, struct path *path) | |
156 | { | |
157 | struct ovl_entry *oe = dentry->d_fsdata; | |
158 | ||
b9343632 CR |
159 | if (oe->numlower) { |
160 | path->mnt = oe->lowerstack[0].layer->mnt; | |
161 | path->dentry = oe->lowerstack[0].dentry; | |
162 | } else { | |
163 | *path = (struct path) { }; | |
164 | } | |
bbb1e54d MS |
165 | } |
166 | ||
167 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) | |
168 | { | |
169 | enum ovl_path_type type = ovl_path_type(dentry); | |
170 | ||
171 | if (!OVL_TYPE_UPPER(type)) | |
172 | ovl_path_lower(dentry, path); | |
173 | else | |
174 | ovl_path_upper(dentry, path); | |
175 | ||
176 | return type; | |
177 | } | |
178 | ||
179 | struct dentry *ovl_dentry_upper(struct dentry *dentry) | |
180 | { | |
09d8b586 | 181 | return ovl_upperdentry_dereference(OVL_I(d_inode(dentry))); |
bbb1e54d MS |
182 | } |
183 | ||
184 | struct dentry *ovl_dentry_lower(struct dentry *dentry) | |
185 | { | |
186 | struct ovl_entry *oe = dentry->d_fsdata; | |
187 | ||
09d8b586 | 188 | return oe->numlower ? oe->lowerstack[0].dentry : NULL; |
bbb1e54d MS |
189 | } |
190 | ||
da309e8c AG |
191 | struct ovl_layer *ovl_layer_lower(struct dentry *dentry) |
192 | { | |
193 | struct ovl_entry *oe = dentry->d_fsdata; | |
194 | ||
195 | return oe->numlower ? oe->lowerstack[0].layer : NULL; | |
196 | } | |
197 | ||
bbb1e54d MS |
198 | struct dentry *ovl_dentry_real(struct dentry *dentry) |
199 | { | |
09d8b586 | 200 | return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); |
bbb1e54d MS |
201 | } |
202 | ||
1d88f183 MS |
203 | struct dentry *ovl_i_dentry_upper(struct inode *inode) |
204 | { | |
205 | return ovl_upperdentry_dereference(OVL_I(inode)); | |
206 | } | |
207 | ||
09d8b586 | 208 | struct inode *ovl_inode_upper(struct inode *inode) |
25b7713a | 209 | { |
1d88f183 | 210 | struct dentry *upperdentry = ovl_i_dentry_upper(inode); |
25b7713a | 211 | |
09d8b586 MS |
212 | return upperdentry ? d_inode(upperdentry) : NULL; |
213 | } | |
25b7713a | 214 | |
09d8b586 MS |
215 | struct inode *ovl_inode_lower(struct inode *inode) |
216 | { | |
217 | return OVL_I(inode)->lower; | |
218 | } | |
25b7713a | 219 | |
09d8b586 MS |
220 | struct inode *ovl_inode_real(struct inode *inode) |
221 | { | |
222 | return ovl_inode_upper(inode) ?: ovl_inode_lower(inode); | |
25b7713a MS |
223 | } |
224 | ||
09d8b586 | 225 | |
4edb83bb | 226 | struct ovl_dir_cache *ovl_dir_cache(struct inode *inode) |
bbb1e54d | 227 | { |
4edb83bb | 228 | return OVL_I(inode)->cache; |
bbb1e54d MS |
229 | } |
230 | ||
4edb83bb | 231 | void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache) |
bbb1e54d | 232 | { |
4edb83bb | 233 | OVL_I(inode)->cache = cache; |
bbb1e54d MS |
234 | } |
235 | ||
c62520a8 AG |
236 | void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry) |
237 | { | |
238 | set_bit(flag, &OVL_E(dentry)->flags); | |
239 | } | |
240 | ||
241 | void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry) | |
242 | { | |
243 | clear_bit(flag, &OVL_E(dentry)->flags); | |
244 | } | |
245 | ||
246 | bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry) | |
247 | { | |
248 | return test_bit(flag, &OVL_E(dentry)->flags); | |
249 | } | |
250 | ||
bbb1e54d MS |
251 | bool ovl_dentry_is_opaque(struct dentry *dentry) |
252 | { | |
c62520a8 | 253 | return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry); |
bbb1e54d MS |
254 | } |
255 | ||
256 | bool ovl_dentry_is_whiteout(struct dentry *dentry) | |
257 | { | |
258 | return !dentry->d_inode && ovl_dentry_is_opaque(dentry); | |
259 | } | |
260 | ||
5cf5b477 | 261 | void ovl_dentry_set_opaque(struct dentry *dentry) |
bbb1e54d | 262 | { |
c62520a8 | 263 | ovl_dentry_set_flag(OVL_E_OPAQUE, dentry); |
bbb1e54d MS |
264 | } |
265 | ||
55acc661 | 266 | /* |
aa3ff3c1 AG |
267 | * For hard links and decoded file handles, it's possible for ovl_dentry_upper() |
268 | * to return positive, while there's no actual upper alias for the inode. | |
269 | * Copy up code needs to know about the existence of the upper alias, so it | |
270 | * can't use ovl_dentry_upper(). | |
55acc661 MS |
271 | */ |
272 | bool ovl_dentry_has_upper_alias(struct dentry *dentry) | |
273 | { | |
c62520a8 | 274 | return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry); |
55acc661 MS |
275 | } |
276 | ||
277 | void ovl_dentry_set_upper_alias(struct dentry *dentry) | |
278 | { | |
c62520a8 | 279 | ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry); |
55acc661 MS |
280 | } |
281 | ||
a6c60655 MS |
282 | bool ovl_redirect_dir(struct super_block *sb) |
283 | { | |
284 | struct ovl_fs *ofs = sb->s_fs_info; | |
285 | ||
21a22878 | 286 | return ofs->config.redirect_dir && !ofs->noxattr; |
a6c60655 MS |
287 | } |
288 | ||
289 | const char *ovl_dentry_get_redirect(struct dentry *dentry) | |
290 | { | |
cf31c463 | 291 | return OVL_I(d_inode(dentry))->redirect; |
a6c60655 MS |
292 | } |
293 | ||
294 | void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) | |
295 | { | |
cf31c463 | 296 | struct ovl_inode *oi = OVL_I(d_inode(dentry)); |
a6c60655 | 297 | |
cf31c463 MS |
298 | kfree(oi->redirect); |
299 | oi->redirect = redirect; | |
a6c60655 MS |
300 | } |
301 | ||
09d8b586 MS |
302 | void ovl_inode_init(struct inode *inode, struct dentry *upperdentry, |
303 | struct dentry *lowerdentry) | |
bbb1e54d | 304 | { |
695b46e7 AG |
305 | struct inode *realinode = d_inode(upperdentry ?: lowerdentry); |
306 | ||
09d8b586 MS |
307 | if (upperdentry) |
308 | OVL_I(inode)->__upperdentry = upperdentry; | |
309 | if (lowerdentry) | |
31747eda | 310 | OVL_I(inode)->lower = igrab(d_inode(lowerdentry)); |
bbb1e54d | 311 | |
695b46e7 AG |
312 | ovl_copyattr(realinode, inode); |
313 | if (!inode->i_ino) | |
314 | inode->i_ino = realinode->i_ino; | |
bbb1e54d MS |
315 | } |
316 | ||
09d8b586 | 317 | void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) |
bbb1e54d | 318 | { |
09d8b586 | 319 | struct inode *upperinode = d_inode(upperdentry); |
e6d2ebdd | 320 | |
09d8b586 MS |
321 | WARN_ON(OVL_I(inode)->__upperdentry); |
322 | ||
25b7713a | 323 | /* |
09d8b586 | 324 | * Make sure upperdentry is consistent before making it visible |
25b7713a MS |
325 | */ |
326 | smp_wmb(); | |
09d8b586 | 327 | OVL_I(inode)->__upperdentry = upperdentry; |
31747eda | 328 | if (inode_unhashed(inode)) { |
695b46e7 AG |
329 | if (!inode->i_ino) |
330 | inode->i_ino = upperinode->i_ino; | |
25b7713a | 331 | inode->i_private = upperinode; |
bbb1e54d | 332 | __insert_inode_hash(inode, (unsigned long) upperinode); |
25b7713a | 333 | } |
bbb1e54d MS |
334 | } |
335 | ||
4edb83bb | 336 | void ovl_dentry_version_inc(struct dentry *dentry, bool impurity) |
bbb1e54d | 337 | { |
04a01ac7 | 338 | struct inode *inode = d_inode(dentry); |
bbb1e54d | 339 | |
04a01ac7 | 340 | WARN_ON(!inode_is_locked(inode)); |
4edb83bb MS |
341 | /* |
342 | * Version is used by readdir code to keep cache consistent. For merge | |
343 | * dirs all changes need to be noted. For non-merge dirs, cache only | |
344 | * contains impure (ones which have been copied up and have origins) | |
345 | * entries, so only need to note changes to impure entries. | |
346 | */ | |
347 | if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity) | |
348 | OVL_I(inode)->version++; | |
bbb1e54d MS |
349 | } |
350 | ||
351 | u64 ovl_dentry_version_get(struct dentry *dentry) | |
352 | { | |
04a01ac7 | 353 | struct inode *inode = d_inode(dentry); |
bbb1e54d | 354 | |
04a01ac7 MS |
355 | WARN_ON(!inode_is_locked(inode)); |
356 | return OVL_I(inode)->version; | |
bbb1e54d MS |
357 | } |
358 | ||
359 | bool ovl_is_whiteout(struct dentry *dentry) | |
360 | { | |
361 | struct inode *inode = dentry->d_inode; | |
362 | ||
363 | return inode && IS_WHITEOUT(inode); | |
364 | } | |
365 | ||
366 | struct file *ovl_path_open(struct path *path, int flags) | |
367 | { | |
368 | return dentry_open(path, flags | O_NOATIME, current_cred()); | |
369 | } | |
39d3d60a AG |
370 | |
371 | int ovl_copy_up_start(struct dentry *dentry) | |
372 | { | |
a015dafc | 373 | struct ovl_inode *oi = OVL_I(d_inode(dentry)); |
39d3d60a AG |
374 | int err; |
375 | ||
a015dafc | 376 | err = mutex_lock_interruptible(&oi->lock); |
59be0971 | 377 | if (!err && ovl_dentry_has_upper_alias(dentry)) { |
a015dafc AG |
378 | err = 1; /* Already copied up */ |
379 | mutex_unlock(&oi->lock); | |
39d3d60a | 380 | } |
39d3d60a AG |
381 | |
382 | return err; | |
383 | } | |
384 | ||
385 | void ovl_copy_up_end(struct dentry *dentry) | |
386 | { | |
a015dafc | 387 | mutex_unlock(&OVL_I(d_inode(dentry))->lock); |
39d3d60a | 388 | } |
82b749b2 | 389 | |
b79e05aa AG |
390 | bool ovl_check_origin_xattr(struct dentry *dentry) |
391 | { | |
392 | int res; | |
393 | ||
394 | res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0); | |
395 | ||
396 | /* Zero size value means "copied up but origin unknown" */ | |
397 | if (res >= 0) | |
398 | return true; | |
399 | ||
400 | return false; | |
401 | } | |
402 | ||
f3a15685 AG |
403 | bool ovl_check_dir_xattr(struct dentry *dentry, const char *name) |
404 | { | |
405 | int res; | |
406 | char val; | |
407 | ||
408 | if (!d_is_dir(dentry)) | |
409 | return false; | |
410 | ||
411 | res = vfs_getxattr(dentry, name, &val, 1); | |
412 | if (res == 1 && val == 'y') | |
413 | return true; | |
414 | ||
415 | return false; | |
416 | } | |
417 | ||
82b749b2 AG |
418 | int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry, |
419 | const char *name, const void *value, size_t size, | |
420 | int xerr) | |
421 | { | |
422 | int err; | |
423 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
424 | ||
425 | if (ofs->noxattr) | |
426 | return xerr; | |
427 | ||
428 | err = ovl_do_setxattr(upperdentry, name, value, size, 0); | |
429 | ||
430 | if (err == -EOPNOTSUPP) { | |
431 | pr_warn("overlayfs: cannot set %s xattr on upper\n", name); | |
432 | ofs->noxattr = true; | |
433 | return xerr; | |
434 | } | |
435 | ||
436 | return err; | |
437 | } | |
f3a15685 AG |
438 | |
439 | int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry) | |
440 | { | |
441 | int err; | |
f3a15685 | 442 | |
13c72075 | 443 | if (ovl_test_flag(OVL_IMPURE, d_inode(dentry))) |
f3a15685 AG |
444 | return 0; |
445 | ||
446 | /* | |
447 | * Do not fail when upper doesn't support xattrs. | |
448 | * Upper inodes won't have origin nor redirect xattr anyway. | |
449 | */ | |
450 | err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE, | |
451 | "y", 1, 0); | |
452 | if (!err) | |
13c72075 | 453 | ovl_set_flag(OVL_IMPURE, d_inode(dentry)); |
f3a15685 AG |
454 | |
455 | return err; | |
456 | } | |
13c72075 MS |
457 | |
458 | void ovl_set_flag(unsigned long flag, struct inode *inode) | |
459 | { | |
460 | set_bit(flag, &OVL_I(inode)->flags); | |
461 | } | |
462 | ||
4edb83bb MS |
463 | void ovl_clear_flag(unsigned long flag, struct inode *inode) |
464 | { | |
465 | clear_bit(flag, &OVL_I(inode)->flags); | |
466 | } | |
467 | ||
13c72075 MS |
468 | bool ovl_test_flag(unsigned long flag, struct inode *inode) |
469 | { | |
470 | return test_bit(flag, &OVL_I(inode)->flags); | |
471 | } | |
ad0af710 AG |
472 | |
473 | /** | |
474 | * Caller must hold a reference to inode to prevent it from being freed while | |
475 | * it is marked inuse. | |
476 | */ | |
477 | bool ovl_inuse_trylock(struct dentry *dentry) | |
478 | { | |
479 | struct inode *inode = d_inode(dentry); | |
480 | bool locked = false; | |
481 | ||
482 | spin_lock(&inode->i_lock); | |
483 | if (!(inode->i_state & I_OVL_INUSE)) { | |
484 | inode->i_state |= I_OVL_INUSE; | |
485 | locked = true; | |
486 | } | |
487 | spin_unlock(&inode->i_lock); | |
488 | ||
489 | return locked; | |
490 | } | |
491 | ||
492 | void ovl_inuse_unlock(struct dentry *dentry) | |
493 | { | |
494 | if (dentry) { | |
495 | struct inode *inode = d_inode(dentry); | |
496 | ||
497 | spin_lock(&inode->i_lock); | |
498 | WARN_ON(!(inode->i_state & I_OVL_INUSE)); | |
499 | inode->i_state &= ~I_OVL_INUSE; | |
500 | spin_unlock(&inode->i_lock); | |
501 | } | |
502 | } | |
5f8415d6 | 503 | |
24b33ee1 AG |
504 | /* |
505 | * Does this overlay dentry need to be indexed on copy up? | |
506 | */ | |
507 | bool ovl_need_index(struct dentry *dentry) | |
508 | { | |
509 | struct dentry *lower = ovl_dentry_lower(dentry); | |
510 | ||
511 | if (!lower || !ovl_indexdir(dentry->d_sb)) | |
512 | return false; | |
513 | ||
fbd2d207 | 514 | /* Index all files for NFS export and consistency verification */ |
016b720f | 515 | if (ovl_index_all(dentry->d_sb)) |
fbd2d207 AG |
516 | return true; |
517 | ||
24b33ee1 AG |
518 | /* Index only lower hardlinks on copy up */ |
519 | if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1) | |
520 | return true; | |
521 | ||
522 | return false; | |
523 | } | |
524 | ||
9f4ec904 | 525 | /* Caller must hold OVL_I(inode)->lock */ |
caf70cb2 AG |
526 | static void ovl_cleanup_index(struct dentry *dentry) |
527 | { | |
e7dd0e71 AG |
528 | struct dentry *indexdir = ovl_indexdir(dentry->d_sb); |
529 | struct inode *dir = indexdir->d_inode; | |
caf70cb2 AG |
530 | struct dentry *lowerdentry = ovl_dentry_lower(dentry); |
531 | struct dentry *upperdentry = ovl_dentry_upper(dentry); | |
532 | struct dentry *index = NULL; | |
533 | struct inode *inode; | |
534 | struct qstr name; | |
535 | int err; | |
536 | ||
537 | err = ovl_get_index_name(lowerdentry, &name); | |
538 | if (err) | |
539 | goto fail; | |
540 | ||
541 | inode = d_inode(upperdentry); | |
89a17556 | 542 | if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) { |
caf70cb2 AG |
543 | pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n", |
544 | upperdentry, inode->i_ino, inode->i_nlink); | |
545 | /* | |
546 | * We either have a bug with persistent union nlink or a lower | |
547 | * hardlink was added while overlay is mounted. Adding a lower | |
548 | * hardlink and then unlinking all overlay hardlinks would drop | |
549 | * overlay nlink to zero before all upper inodes are unlinked. | |
550 | * As a safety measure, when that situation is detected, set | |
551 | * the overlay nlink to the index inode nlink minus one for the | |
552 | * index entry itself. | |
553 | */ | |
554 | set_nlink(d_inode(dentry), inode->i_nlink - 1); | |
555 | ovl_set_nlink_upper(dentry); | |
556 | goto out; | |
557 | } | |
558 | ||
559 | inode_lock_nested(dir, I_MUTEX_PARENT); | |
e7dd0e71 | 560 | index = lookup_one_len(name.name, indexdir, name.len); |
caf70cb2 | 561 | err = PTR_ERR(index); |
e7dd0e71 | 562 | if (IS_ERR(index)) { |
9f4ec904 | 563 | index = NULL; |
e7dd0e71 AG |
564 | } else if (ovl_index_all(dentry->d_sb)) { |
565 | /* Whiteout orphan index to block future open by handle */ | |
566 | err = ovl_cleanup_and_whiteout(indexdir, dir, index); | |
567 | } else { | |
568 | /* Cleanup orphan index entries */ | |
569 | err = ovl_cleanup(dir, index); | |
570 | } | |
9f4ec904 | 571 | |
caf70cb2 AG |
572 | inode_unlock(dir); |
573 | if (err) | |
574 | goto fail; | |
575 | ||
576 | out: | |
577 | dput(index); | |
578 | return; | |
579 | ||
580 | fail: | |
581 | pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err); | |
582 | goto out; | |
583 | } | |
584 | ||
5f8415d6 AG |
585 | /* |
586 | * Operations that change overlay inode and upper inode nlink need to be | |
587 | * synchronized with copy up for persistent nlink accounting. | |
588 | */ | |
589 | int ovl_nlink_start(struct dentry *dentry, bool *locked) | |
590 | { | |
591 | struct ovl_inode *oi = OVL_I(d_inode(dentry)); | |
592 | const struct cred *old_cred; | |
593 | int err; | |
594 | ||
89a17556 | 595 | if (!d_inode(dentry)) |
5f8415d6 AG |
596 | return 0; |
597 | ||
598 | /* | |
599 | * With inodes index is enabled, we store the union overlay nlink | |
24b33ee1 | 600 | * in an xattr on the index inode. When whiting out an indexed lower, |
5f8415d6 AG |
601 | * we need to decrement the overlay persistent nlink, but before the |
602 | * first copy up, we have no upper index inode to store the xattr. | |
603 | * | |
24b33ee1 | 604 | * As a workaround, before whiteout/rename over an indexed lower, |
5f8415d6 AG |
605 | * copy up to create the upper index. Creating the upper index will |
606 | * initialize the overlay nlink, so it could be dropped if unlink | |
607 | * or rename succeeds. | |
608 | * | |
609 | * TODO: implement metadata only index copy up when called with | |
610 | * ovl_copy_up_flags(dentry, O_PATH). | |
611 | */ | |
24b33ee1 | 612 | if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) { |
5f8415d6 AG |
613 | err = ovl_copy_up(dentry); |
614 | if (err) | |
615 | return err; | |
616 | } | |
617 | ||
618 | err = mutex_lock_interruptible(&oi->lock); | |
619 | if (err) | |
620 | return err; | |
621 | ||
89a17556 | 622 | if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, d_inode(dentry))) |
5f8415d6 AG |
623 | goto out; |
624 | ||
625 | old_cred = ovl_override_creds(dentry->d_sb); | |
626 | /* | |
627 | * The overlay inode nlink should be incremented/decremented IFF the | |
628 | * upper operation succeeds, along with nlink change of upper inode. | |
629 | * Therefore, before link/unlink/rename, we store the union nlink | |
630 | * value relative to the upper inode nlink in an upper inode xattr. | |
631 | */ | |
632 | err = ovl_set_nlink_upper(dentry); | |
633 | revert_creds(old_cred); | |
634 | ||
635 | out: | |
636 | if (err) | |
637 | mutex_unlock(&oi->lock); | |
638 | else | |
639 | *locked = true; | |
640 | ||
641 | return err; | |
642 | } | |
643 | ||
644 | void ovl_nlink_end(struct dentry *dentry, bool locked) | |
645 | { | |
caf70cb2 AG |
646 | if (locked) { |
647 | if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) && | |
648 | d_inode(dentry)->i_nlink == 0) { | |
649 | const struct cred *old_cred; | |
650 | ||
651 | old_cred = ovl_override_creds(dentry->d_sb); | |
652 | ovl_cleanup_index(dentry); | |
653 | revert_creds(old_cred); | |
654 | } | |
655 | ||
5f8415d6 | 656 | mutex_unlock(&OVL_I(d_inode(dentry))->lock); |
caf70cb2 | 657 | } |
5f8415d6 | 658 | } |
5820dc08 AG |
659 | |
660 | int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir) | |
661 | { | |
662 | /* Workdir should not be the same as upperdir */ | |
663 | if (workdir == upperdir) | |
664 | goto err; | |
665 | ||
666 | /* Workdir should not be subdir of upperdir and vice versa */ | |
667 | if (lock_rename(workdir, upperdir) != NULL) | |
668 | goto err_unlock; | |
669 | ||
670 | return 0; | |
671 | ||
672 | err_unlock: | |
673 | unlock_rename(workdir, upperdir); | |
674 | err: | |
675 | pr_err("overlayfs: failed to lock workdir+upperdir\n"); | |
676 | return -EIO; | |
677 | } |