blob: 51ca8bd160095684dc33eed27c6d99ae3a2eba0f [file] [log] [blame]
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001/*
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>
Ingo Molnar5b825c32017-02-02 17:54:15 +010013#include <linux/cred.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010014#include <linux/xattr.h>
Amir Goldstein02bcd152017-06-21 15:28:36 +030015#include <linux/exportfs.h>
16#include <linux/uuid.h>
Amir Goldsteincaf70cb2017-06-21 13:46:12 +030017#include <linux/namei.h>
18#include <linux/ratelimit.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010019#include "overlayfs.h"
20#include "ovl_entry.h"
21
22int ovl_want_write(struct dentry *dentry)
23{
24 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
25 return mnt_want_write(ofs->upper_mnt);
26}
27
28void ovl_drop_write(struct dentry *dentry)
29{
30 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
31 mnt_drop_write(ofs->upper_mnt);
32}
33
34struct dentry *ovl_workdir(struct dentry *dentry)
35{
36 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
37 return ofs->workdir;
38}
39
40const struct cred *ovl_override_creds(struct super_block *sb)
41{
42 struct ovl_fs *ofs = sb->s_fs_info;
43
44 return override_creds(ofs->creator_cred);
45}
46
Amir Goldstein7bcd74b2017-03-22 08:42:21 -040047struct super_block *ovl_same_sb(struct super_block *sb)
48{
49 struct ovl_fs *ofs = sb->s_fs_info;
50
51 return ofs->same_sb;
52}
53
Amir Goldstein02bcd152017-06-21 15:28:36 +030054bool ovl_can_decode_fh(struct super_block *sb)
55{
56 return (sb->s_export_op && sb->s_export_op->fh_to_dentry &&
57 !uuid_is_null(&sb->s_uuid));
58}
59
60struct dentry *ovl_indexdir(struct super_block *sb)
61{
62 struct ovl_fs *ofs = sb->s_fs_info;
63
64 return ofs->indexdir;
65}
66
Miklos Szeredibbb1e542016-12-16 11:02:56 +010067struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
68{
69 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
70 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
71
72 if (oe)
73 oe->numlower = numlower;
74
75 return oe;
76}
77
78bool ovl_dentry_remote(struct dentry *dentry)
79{
80 return dentry->d_flags &
81 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
82 DCACHE_OP_REAL);
83}
84
85bool ovl_dentry_weird(struct dentry *dentry)
86{
87 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
88 DCACHE_MANAGE_TRANSIT |
89 DCACHE_OP_HASH |
90 DCACHE_OP_COMPARE);
91}
92
93enum ovl_path_type ovl_path_type(struct dentry *dentry)
94{
95 struct ovl_entry *oe = dentry->d_fsdata;
96 enum ovl_path_type type = 0;
97
Miklos Szeredi09d8b582017-07-04 22:03:16 +020098 if (ovl_dentry_upper(dentry)) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +010099 type = __OVL_PATH_UPPER;
100
101 /*
Amir Goldstein59548502017-04-23 23:12:34 +0300102 * Non-dir dentry can hold lower dentry of its copy up origin.
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100103 */
Amir Goldstein59548502017-04-23 23:12:34 +0300104 if (oe->numlower) {
105 type |= __OVL_PATH_ORIGIN;
106 if (d_is_dir(dentry))
107 type |= __OVL_PATH_MERGE;
108 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100109 } else {
110 if (oe->numlower > 1)
111 type |= __OVL_PATH_MERGE;
112 }
113 return type;
114}
115
116void ovl_path_upper(struct dentry *dentry, struct path *path)
117{
118 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100119
120 path->mnt = ofs->upper_mnt;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200121 path->dentry = ovl_dentry_upper(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100122}
123
124void ovl_path_lower(struct dentry *dentry, struct path *path)
125{
126 struct ovl_entry *oe = dentry->d_fsdata;
127
Kees Cook33006cd2017-03-29 14:02:19 -0700128 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100129}
130
131enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
132{
133 enum ovl_path_type type = ovl_path_type(dentry);
134
135 if (!OVL_TYPE_UPPER(type))
136 ovl_path_lower(dentry, path);
137 else
138 ovl_path_upper(dentry, path);
139
140 return type;
141}
142
143struct dentry *ovl_dentry_upper(struct dentry *dentry)
144{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200145 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100146}
147
148struct dentry *ovl_dentry_lower(struct dentry *dentry)
149{
150 struct ovl_entry *oe = dentry->d_fsdata;
151
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200152 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100153}
154
155struct dentry *ovl_dentry_real(struct dentry *dentry)
156{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200157 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100158}
159
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200160struct dentry *ovl_i_dentry_upper(struct inode *inode)
161{
162 return ovl_upperdentry_dereference(OVL_I(inode));
163}
164
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200165struct inode *ovl_inode_upper(struct inode *inode)
Miklos Szeredi25b77132017-07-04 22:03:16 +0200166{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200167 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200168
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200169 return upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200170}
171
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200172struct inode *ovl_inode_lower(struct inode *inode)
173{
174 return OVL_I(inode)->lower;
175}
176
177struct inode *ovl_inode_real(struct inode *inode)
178{
179 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
180}
181
182
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200183struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100184{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200185 return OVL_I(inode)->cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100186}
187
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200188void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100189{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200190 OVL_I(inode)->cache = cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100191}
192
193bool ovl_dentry_is_opaque(struct dentry *dentry)
194{
195 struct ovl_entry *oe = dentry->d_fsdata;
196 return oe->opaque;
197}
198
199bool ovl_dentry_is_whiteout(struct dentry *dentry)
200{
201 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
202}
203
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100204void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100205{
206 struct ovl_entry *oe = dentry->d_fsdata;
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100207
208 oe->opaque = true;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100209}
210
Miklos Szeredi55acc662017-07-04 22:03:18 +0200211/*
212 * For hard links it's possible for ovl_dentry_upper() to return positive, while
213 * there's no actual upper alias for the inode. Copy up code needs to know
214 * about the existence of the upper alias, so it can't use ovl_dentry_upper().
215 */
216bool ovl_dentry_has_upper_alias(struct dentry *dentry)
217{
218 struct ovl_entry *oe = dentry->d_fsdata;
219
220 return oe->has_upper;
221}
222
223void ovl_dentry_set_upper_alias(struct dentry *dentry)
224{
225 struct ovl_entry *oe = dentry->d_fsdata;
226
227 oe->has_upper = true;
228}
229
Miklos Szeredia6c60652016-12-16 11:02:56 +0100230bool ovl_redirect_dir(struct super_block *sb)
231{
232 struct ovl_fs *ofs = sb->s_fs_info;
233
Amir Goldstein21a22872017-05-17 00:12:41 +0300234 return ofs->config.redirect_dir && !ofs->noxattr;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100235}
236
237const char *ovl_dentry_get_redirect(struct dentry *dentry)
238{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200239 return OVL_I(d_inode(dentry))->redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100240}
241
242void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
243{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200244 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Miklos Szeredia6c60652016-12-16 11:02:56 +0100245
Miklos Szeredicf31c462017-07-04 22:03:16 +0200246 kfree(oi->redirect);
247 oi->redirect = redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100248}
249
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200250void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
251 struct dentry *lowerdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100252{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200253 if (upperdentry)
254 OVL_I(inode)->__upperdentry = upperdentry;
255 if (lowerdentry)
256 OVL_I(inode)->lower = d_inode(lowerdentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100257
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200258 ovl_copyattr(d_inode(upperdentry ?: lowerdentry), inode);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100259}
260
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200261void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100262{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200263 struct inode *upperinode = d_inode(upperdentry);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200264
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200265 WARN_ON(OVL_I(inode)->__upperdentry);
266
Miklos Szeredi25b77132017-07-04 22:03:16 +0200267 /*
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200268 * Make sure upperdentry is consistent before making it visible
Miklos Szeredi25b77132017-07-04 22:03:16 +0200269 */
270 smp_wmb();
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200271 OVL_I(inode)->__upperdentry = upperdentry;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200272 if (!S_ISDIR(upperinode->i_mode) && inode_unhashed(inode)) {
Miklos Szeredi25b77132017-07-04 22:03:16 +0200273 inode->i_private = upperinode;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100274 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200275 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100276}
277
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200278void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100279{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200280 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100281
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200282 WARN_ON(!inode_is_locked(inode));
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200283 /*
284 * Version is used by readdir code to keep cache consistent. For merge
285 * dirs all changes need to be noted. For non-merge dirs, cache only
286 * contains impure (ones which have been copied up and have origins)
287 * entries, so only need to note changes to impure entries.
288 */
289 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
290 OVL_I(inode)->version++;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100291}
292
293u64 ovl_dentry_version_get(struct dentry *dentry)
294{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200295 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100296
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200297 WARN_ON(!inode_is_locked(inode));
298 return OVL_I(inode)->version;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100299}
300
301bool ovl_is_whiteout(struct dentry *dentry)
302{
303 struct inode *inode = dentry->d_inode;
304
305 return inode && IS_WHITEOUT(inode);
306}
307
308struct file *ovl_path_open(struct path *path, int flags)
309{
310 return dentry_open(path, flags | O_NOATIME, current_cred());
311}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200312
313int ovl_copy_up_start(struct dentry *dentry)
314{
Amir Goldsteina015daf2017-06-21 15:28:51 +0300315 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Amir Goldstein39d3d602017-01-17 06:34:56 +0200316 int err;
317
Amir Goldsteina015daf2017-06-21 15:28:51 +0300318 err = mutex_lock_interruptible(&oi->lock);
Amir Goldstein59be0972017-06-20 15:25:46 +0300319 if (!err && ovl_dentry_has_upper_alias(dentry)) {
Amir Goldsteina015daf2017-06-21 15:28:51 +0300320 err = 1; /* Already copied up */
321 mutex_unlock(&oi->lock);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200322 }
Amir Goldstein39d3d602017-01-17 06:34:56 +0200323
324 return err;
325}
326
327void ovl_copy_up_end(struct dentry *dentry)
328{
Amir Goldsteina015daf2017-06-21 15:28:51 +0300329 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200330}
Amir Goldstein82b749b2017-05-17 00:12:40 +0300331
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300332bool ovl_check_origin_xattr(struct dentry *dentry)
333{
334 int res;
335
336 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
337
338 /* Zero size value means "copied up but origin unknown" */
339 if (res >= 0)
340 return true;
341
342 return false;
343}
344
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300345bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
346{
347 int res;
348 char val;
349
350 if (!d_is_dir(dentry))
351 return false;
352
353 res = vfs_getxattr(dentry, name, &val, 1);
354 if (res == 1 && val == 'y')
355 return true;
356
357 return false;
358}
359
Amir Goldstein82b749b2017-05-17 00:12:40 +0300360int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
361 const char *name, const void *value, size_t size,
362 int xerr)
363{
364 int err;
365 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
366
367 if (ofs->noxattr)
368 return xerr;
369
370 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
371
372 if (err == -EOPNOTSUPP) {
373 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
374 ofs->noxattr = true;
375 return xerr;
376 }
377
378 return err;
379}
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300380
381int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
382{
383 int err;
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300384
Miklos Szeredi13c72072017-07-04 22:03:16 +0200385 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300386 return 0;
387
388 /*
389 * Do not fail when upper doesn't support xattrs.
390 * Upper inodes won't have origin nor redirect xattr anyway.
391 */
392 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
393 "y", 1, 0);
394 if (!err)
Miklos Szeredi13c72072017-07-04 22:03:16 +0200395 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300396
397 return err;
398}
Miklos Szeredi13c72072017-07-04 22:03:16 +0200399
400void ovl_set_flag(unsigned long flag, struct inode *inode)
401{
402 set_bit(flag, &OVL_I(inode)->flags);
403}
404
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200405void ovl_clear_flag(unsigned long flag, struct inode *inode)
406{
407 clear_bit(flag, &OVL_I(inode)->flags);
408}
409
Miklos Szeredi13c72072017-07-04 22:03:16 +0200410bool ovl_test_flag(unsigned long flag, struct inode *inode)
411{
412 return test_bit(flag, &OVL_I(inode)->flags);
413}
Amir Goldsteinad0af712017-06-21 15:28:32 +0300414
415/**
416 * Caller must hold a reference to inode to prevent it from being freed while
417 * it is marked inuse.
418 */
419bool ovl_inuse_trylock(struct dentry *dentry)
420{
421 struct inode *inode = d_inode(dentry);
422 bool locked = false;
423
424 spin_lock(&inode->i_lock);
425 if (!(inode->i_state & I_OVL_INUSE)) {
426 inode->i_state |= I_OVL_INUSE;
427 locked = true;
428 }
429 spin_unlock(&inode->i_lock);
430
431 return locked;
432}
433
434void ovl_inuse_unlock(struct dentry *dentry)
435{
436 if (dentry) {
437 struct inode *inode = d_inode(dentry);
438
439 spin_lock(&inode->i_lock);
440 WARN_ON(!(inode->i_state & I_OVL_INUSE));
441 inode->i_state &= ~I_OVL_INUSE;
442 spin_unlock(&inode->i_lock);
443 }
444}
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300445
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300446/* Caller must hold OVL_I(inode)->lock */
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300447static void ovl_cleanup_index(struct dentry *dentry)
448{
449 struct inode *dir = ovl_indexdir(dentry->d_sb)->d_inode;
450 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
451 struct dentry *upperdentry = ovl_dentry_upper(dentry);
452 struct dentry *index = NULL;
453 struct inode *inode;
454 struct qstr name;
455 int err;
456
457 err = ovl_get_index_name(lowerdentry, &name);
458 if (err)
459 goto fail;
460
461 inode = d_inode(upperdentry);
462 if (inode->i_nlink != 1) {
463 pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
464 upperdentry, inode->i_ino, inode->i_nlink);
465 /*
466 * We either have a bug with persistent union nlink or a lower
467 * hardlink was added while overlay is mounted. Adding a lower
468 * hardlink and then unlinking all overlay hardlinks would drop
469 * overlay nlink to zero before all upper inodes are unlinked.
470 * As a safety measure, when that situation is detected, set
471 * the overlay nlink to the index inode nlink minus one for the
472 * index entry itself.
473 */
474 set_nlink(d_inode(dentry), inode->i_nlink - 1);
475 ovl_set_nlink_upper(dentry);
476 goto out;
477 }
478
479 inode_lock_nested(dir, I_MUTEX_PARENT);
480 /* TODO: whiteout instead of cleanup to block future open by handle */
481 index = lookup_one_len(name.name, ovl_indexdir(dentry->d_sb), name.len);
482 err = PTR_ERR(index);
483 if (!IS_ERR(index))
484 err = ovl_cleanup(dir, index);
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300485 else
486 index = NULL;
487
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300488 inode_unlock(dir);
489 if (err)
490 goto fail;
491
492out:
493 dput(index);
494 return;
495
496fail:
497 pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
498 goto out;
499}
500
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300501/*
502 * Operations that change overlay inode and upper inode nlink need to be
503 * synchronized with copy up for persistent nlink accounting.
504 */
505int ovl_nlink_start(struct dentry *dentry, bool *locked)
506{
507 struct ovl_inode *oi = OVL_I(d_inode(dentry));
508 const struct cred *old_cred;
509 int err;
510
511 if (!d_inode(dentry) || d_is_dir(dentry))
512 return 0;
513
514 /*
515 * With inodes index is enabled, we store the union overlay nlink
516 * in an xattr on the index inode. When whiting out lower hardlinks
517 * we need to decrement the overlay persistent nlink, but before the
518 * first copy up, we have no upper index inode to store the xattr.
519 *
520 * As a workaround, before whiteout/rename over of a lower hardlink,
521 * copy up to create the upper index. Creating the upper index will
522 * initialize the overlay nlink, so it could be dropped if unlink
523 * or rename succeeds.
524 *
525 * TODO: implement metadata only index copy up when called with
526 * ovl_copy_up_flags(dentry, O_PATH).
527 */
528 if (ovl_indexdir(dentry->d_sb) && !ovl_dentry_has_upper_alias(dentry) &&
529 d_inode(ovl_dentry_lower(dentry))->i_nlink > 1) {
530 err = ovl_copy_up(dentry);
531 if (err)
532 return err;
533 }
534
535 err = mutex_lock_interruptible(&oi->lock);
536 if (err)
537 return err;
538
539 if (!ovl_test_flag(OVL_INDEX, d_inode(dentry)))
540 goto out;
541
542 old_cred = ovl_override_creds(dentry->d_sb);
543 /*
544 * The overlay inode nlink should be incremented/decremented IFF the
545 * upper operation succeeds, along with nlink change of upper inode.
546 * Therefore, before link/unlink/rename, we store the union nlink
547 * value relative to the upper inode nlink in an upper inode xattr.
548 */
549 err = ovl_set_nlink_upper(dentry);
550 revert_creds(old_cred);
551
552out:
553 if (err)
554 mutex_unlock(&oi->lock);
555 else
556 *locked = true;
557
558 return err;
559}
560
561void ovl_nlink_end(struct dentry *dentry, bool locked)
562{
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300563 if (locked) {
564 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
565 d_inode(dentry)->i_nlink == 0) {
566 const struct cred *old_cred;
567
568 old_cred = ovl_override_creds(dentry->d_sb);
569 ovl_cleanup_index(dentry);
570 revert_creds(old_cred);
571 }
572
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300573 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300574 }
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300575}
Amir Goldstein5820dc02017-09-25 16:39:55 +0300576
577int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
578{
579 /* Workdir should not be the same as upperdir */
580 if (workdir == upperdir)
581 goto err;
582
583 /* Workdir should not be subdir of upperdir and vice versa */
584 if (lock_rename(workdir, upperdir) != NULL)
585 goto err_unlock;
586
587 return 0;
588
589err_unlock:
590 unlock_rename(workdir, upperdir);
591err:
592 pr_err("overlayfs: failed to lock workdir+upperdir\n");
593 return -EIO;
594}