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