blob: 9158d17bb3200134de2c04d34b2fed566f1e93bc [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"
Miklos Szeredibbb1e542016-12-16 11:02:56 +010020
21int 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
27void 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
33struct dentry *ovl_workdir(struct dentry *dentry)
34{
35 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
36 return ofs->workdir;
37}
38
39const 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
Amir Goldstein7bcd74b2017-03-22 08:42:21 -040046struct 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
Amir Goldstein02bcd152017-06-21 15:28:36 +030053bool 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
59struct dentry *ovl_indexdir(struct super_block *sb)
60{
61 struct ovl_fs *ofs = sb->s_fs_info;
62
63 return ofs->indexdir;
64}
65
Miklos Szeredibbb1e542016-12-16 11:02:56 +010066struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
67{
68 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
69 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
70
71 if (oe)
72 oe->numlower = numlower;
73
74 return oe;
75}
76
77bool ovl_dentry_remote(struct dentry *dentry)
78{
79 return dentry->d_flags &
80 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
81 DCACHE_OP_REAL);
82}
83
84bool ovl_dentry_weird(struct dentry *dentry)
85{
86 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
87 DCACHE_MANAGE_TRANSIT |
88 DCACHE_OP_HASH |
89 DCACHE_OP_COMPARE);
90}
91
92enum ovl_path_type ovl_path_type(struct dentry *dentry)
93{
94 struct ovl_entry *oe = dentry->d_fsdata;
95 enum ovl_path_type type = 0;
96
Miklos Szeredi09d8b582017-07-04 22:03:16 +020097 if (ovl_dentry_upper(dentry)) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +010098 type = __OVL_PATH_UPPER;
99
100 /*
Amir Goldstein59548502017-04-23 23:12:34 +0300101 * Non-dir dentry can hold lower dentry of its copy up origin.
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100102 */
Amir Goldstein59548502017-04-23 23:12:34 +0300103 if (oe->numlower) {
104 type |= __OVL_PATH_ORIGIN;
105 if (d_is_dir(dentry))
106 type |= __OVL_PATH_MERGE;
107 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100108 } else {
109 if (oe->numlower > 1)
110 type |= __OVL_PATH_MERGE;
111 }
112 return type;
113}
114
115void ovl_path_upper(struct dentry *dentry, struct path *path)
116{
117 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100118
119 path->mnt = ofs->upper_mnt;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200120 path->dentry = ovl_dentry_upper(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100121}
122
123void ovl_path_lower(struct dentry *dentry, struct path *path)
124{
125 struct ovl_entry *oe = dentry->d_fsdata;
126
Kees Cook33006cd2017-03-29 14:02:19 -0700127 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100128}
129
130enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
131{
132 enum ovl_path_type type = ovl_path_type(dentry);
133
134 if (!OVL_TYPE_UPPER(type))
135 ovl_path_lower(dentry, path);
136 else
137 ovl_path_upper(dentry, path);
138
139 return type;
140}
141
142struct dentry *ovl_dentry_upper(struct dentry *dentry)
143{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200144 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100145}
146
147struct dentry *ovl_dentry_lower(struct dentry *dentry)
148{
149 struct ovl_entry *oe = dentry->d_fsdata;
150
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200151 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100152}
153
154struct dentry *ovl_dentry_real(struct dentry *dentry)
155{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200156 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100157}
158
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200159struct dentry *ovl_i_dentry_upper(struct inode *inode)
160{
161 return ovl_upperdentry_dereference(OVL_I(inode));
162}
163
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200164struct inode *ovl_inode_upper(struct inode *inode)
Miklos Szeredi25b77132017-07-04 22:03:16 +0200165{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200166 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200167
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200168 return upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200169}
170
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200171struct inode *ovl_inode_lower(struct inode *inode)
172{
173 return OVL_I(inode)->lower;
174}
175
176struct inode *ovl_inode_real(struct inode *inode)
177{
178 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
179}
180
181
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200182struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100183{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200184 return OVL_I(inode)->cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100185}
186
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200187void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100188{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200189 OVL_I(inode)->cache = cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100190}
191
192bool ovl_dentry_is_opaque(struct dentry *dentry)
193{
194 struct ovl_entry *oe = dentry->d_fsdata;
195 return oe->opaque;
196}
197
198bool ovl_dentry_is_whiteout(struct dentry *dentry)
199{
200 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
201}
202
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100203void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100204{
205 struct ovl_entry *oe = dentry->d_fsdata;
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100206
207 oe->opaque = true;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100208}
209
Miklos Szeredi55acc662017-07-04 22:03:18 +0200210/*
211 * For hard links it's possible for ovl_dentry_upper() to return positive, while
212 * there's no actual upper alias for the inode. Copy up code needs to know
213 * about the existence of the upper alias, so it can't use ovl_dentry_upper().
214 */
215bool ovl_dentry_has_upper_alias(struct dentry *dentry)
216{
217 struct ovl_entry *oe = dentry->d_fsdata;
218
219 return oe->has_upper;
220}
221
222void ovl_dentry_set_upper_alias(struct dentry *dentry)
223{
224 struct ovl_entry *oe = dentry->d_fsdata;
225
226 oe->has_upper = true;
227}
228
Miklos Szeredia6c60652016-12-16 11:02:56 +0100229bool ovl_redirect_dir(struct super_block *sb)
230{
231 struct ovl_fs *ofs = sb->s_fs_info;
232
Amir Goldstein21a22872017-05-17 00:12:41 +0300233 return ofs->config.redirect_dir && !ofs->noxattr;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100234}
235
236const char *ovl_dentry_get_redirect(struct dentry *dentry)
237{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200238 return OVL_I(d_inode(dentry))->redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100239}
240
241void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
242{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200243 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Miklos Szeredia6c60652016-12-16 11:02:56 +0100244
Miklos Szeredicf31c462017-07-04 22:03:16 +0200245 kfree(oi->redirect);
246 oi->redirect = redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100247}
248
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200249void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
250 struct dentry *lowerdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100251{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200252 if (upperdentry)
253 OVL_I(inode)->__upperdentry = upperdentry;
254 if (lowerdentry)
255 OVL_I(inode)->lower = d_inode(lowerdentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100256
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200257 ovl_copyattr(d_inode(upperdentry ?: lowerdentry), inode);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100258}
259
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200260void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100261{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200262 struct inode *upperinode = d_inode(upperdentry);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200263
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200264 WARN_ON(OVL_I(inode)->__upperdentry);
265
Miklos Szeredi25b77132017-07-04 22:03:16 +0200266 /*
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200267 * Make sure upperdentry is consistent before making it visible
Miklos Szeredi25b77132017-07-04 22:03:16 +0200268 */
269 smp_wmb();
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200270 OVL_I(inode)->__upperdentry = upperdentry;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200271 if (!S_ISDIR(upperinode->i_mode) && inode_unhashed(inode)) {
Miklos Szeredi25b77132017-07-04 22:03:16 +0200272 inode->i_private = upperinode;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100273 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200274 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100275}
276
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200277void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100278{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200279 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100280
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200281 WARN_ON(!inode_is_locked(inode));
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200282 /*
283 * Version is used by readdir code to keep cache consistent. For merge
284 * dirs all changes need to be noted. For non-merge dirs, cache only
285 * contains impure (ones which have been copied up and have origins)
286 * entries, so only need to note changes to impure entries.
287 */
288 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
289 OVL_I(inode)->version++;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100290}
291
292u64 ovl_dentry_version_get(struct dentry *dentry)
293{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200294 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100295
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200296 WARN_ON(!inode_is_locked(inode));
297 return OVL_I(inode)->version;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100298}
299
300bool ovl_is_whiteout(struct dentry *dentry)
301{
302 struct inode *inode = dentry->d_inode;
303
304 return inode && IS_WHITEOUT(inode);
305}
306
307struct file *ovl_path_open(struct path *path, int flags)
308{
309 return dentry_open(path, flags | O_NOATIME, current_cred());
310}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200311
312int ovl_copy_up_start(struct dentry *dentry)
313{
Amir Goldsteina015daf2017-06-21 15:28:51 +0300314 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Amir Goldstein39d3d602017-01-17 06:34:56 +0200315 int err;
316
Amir Goldsteina015daf2017-06-21 15:28:51 +0300317 err = mutex_lock_interruptible(&oi->lock);
Amir Goldstein59be0972017-06-20 15:25:46 +0300318 if (!err && ovl_dentry_has_upper_alias(dentry)) {
Amir Goldsteina015daf2017-06-21 15:28:51 +0300319 err = 1; /* Already copied up */
320 mutex_unlock(&oi->lock);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200321 }
Amir Goldstein39d3d602017-01-17 06:34:56 +0200322
323 return err;
324}
325
326void ovl_copy_up_end(struct dentry *dentry)
327{
Amir Goldsteina015daf2017-06-21 15:28:51 +0300328 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200329}
Amir Goldstein82b749b2017-05-17 00:12:40 +0300330
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300331bool ovl_check_origin_xattr(struct dentry *dentry)
332{
333 int res;
334
335 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
336
337 /* Zero size value means "copied up but origin unknown" */
338 if (res >= 0)
339 return true;
340
341 return false;
342}
343
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300344bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
345{
346 int res;
347 char val;
348
349 if (!d_is_dir(dentry))
350 return false;
351
352 res = vfs_getxattr(dentry, name, &val, 1);
353 if (res == 1 && val == 'y')
354 return true;
355
356 return false;
357}
358
Amir Goldstein82b749b2017-05-17 00:12:40 +0300359int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
360 const char *name, const void *value, size_t size,
361 int xerr)
362{
363 int err;
364 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
365
366 if (ofs->noxattr)
367 return xerr;
368
369 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
370
371 if (err == -EOPNOTSUPP) {
372 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
373 ofs->noxattr = true;
374 return xerr;
375 }
376
377 return err;
378}
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300379
380int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
381{
382 int err;
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300383
Miklos Szeredi13c72072017-07-04 22:03:16 +0200384 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300385 return 0;
386
387 /*
388 * Do not fail when upper doesn't support xattrs.
389 * Upper inodes won't have origin nor redirect xattr anyway.
390 */
391 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
392 "y", 1, 0);
393 if (!err)
Miklos Szeredi13c72072017-07-04 22:03:16 +0200394 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300395
396 return err;
397}
Miklos Szeredi13c72072017-07-04 22:03:16 +0200398
399void ovl_set_flag(unsigned long flag, struct inode *inode)
400{
401 set_bit(flag, &OVL_I(inode)->flags);
402}
403
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200404void ovl_clear_flag(unsigned long flag, struct inode *inode)
405{
406 clear_bit(flag, &OVL_I(inode)->flags);
407}
408
Miklos Szeredi13c72072017-07-04 22:03:16 +0200409bool ovl_test_flag(unsigned long flag, struct inode *inode)
410{
411 return test_bit(flag, &OVL_I(inode)->flags);
412}
Amir Goldsteinad0af712017-06-21 15:28:32 +0300413
414/**
415 * Caller must hold a reference to inode to prevent it from being freed while
416 * it is marked inuse.
417 */
418bool ovl_inuse_trylock(struct dentry *dentry)
419{
420 struct inode *inode = d_inode(dentry);
421 bool locked = false;
422
423 spin_lock(&inode->i_lock);
424 if (!(inode->i_state & I_OVL_INUSE)) {
425 inode->i_state |= I_OVL_INUSE;
426 locked = true;
427 }
428 spin_unlock(&inode->i_lock);
429
430 return locked;
431}
432
433void ovl_inuse_unlock(struct dentry *dentry)
434{
435 if (dentry) {
436 struct inode *inode = d_inode(dentry);
437
438 spin_lock(&inode->i_lock);
439 WARN_ON(!(inode->i_state & I_OVL_INUSE));
440 inode->i_state &= ~I_OVL_INUSE;
441 spin_unlock(&inode->i_lock);
442 }
443}
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300444
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300445/* Caller must hold OVL_I(inode)->lock */
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300446static void ovl_cleanup_index(struct dentry *dentry)
447{
448 struct inode *dir = ovl_indexdir(dentry->d_sb)->d_inode;
449 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
450 struct dentry *upperdentry = ovl_dentry_upper(dentry);
451 struct dentry *index = NULL;
452 struct inode *inode;
453 struct qstr name;
454 int err;
455
456 err = ovl_get_index_name(lowerdentry, &name);
457 if (err)
458 goto fail;
459
460 inode = d_inode(upperdentry);
461 if (inode->i_nlink != 1) {
462 pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
463 upperdentry, inode->i_ino, inode->i_nlink);
464 /*
465 * We either have a bug with persistent union nlink or a lower
466 * hardlink was added while overlay is mounted. Adding a lower
467 * hardlink and then unlinking all overlay hardlinks would drop
468 * overlay nlink to zero before all upper inodes are unlinked.
469 * As a safety measure, when that situation is detected, set
470 * the overlay nlink to the index inode nlink minus one for the
471 * index entry itself.
472 */
473 set_nlink(d_inode(dentry), inode->i_nlink - 1);
474 ovl_set_nlink_upper(dentry);
475 goto out;
476 }
477
478 inode_lock_nested(dir, I_MUTEX_PARENT);
479 /* TODO: whiteout instead of cleanup to block future open by handle */
480 index = lookup_one_len(name.name, ovl_indexdir(dentry->d_sb), name.len);
481 err = PTR_ERR(index);
482 if (!IS_ERR(index))
483 err = ovl_cleanup(dir, index);
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300484 else
485 index = NULL;
486
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300487 inode_unlock(dir);
488 if (err)
489 goto fail;
490
491out:
492 dput(index);
493 return;
494
495fail:
496 pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
497 goto out;
498}
499
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300500/*
501 * Operations that change overlay inode and upper inode nlink need to be
502 * synchronized with copy up for persistent nlink accounting.
503 */
504int ovl_nlink_start(struct dentry *dentry, bool *locked)
505{
506 struct ovl_inode *oi = OVL_I(d_inode(dentry));
507 const struct cred *old_cred;
508 int err;
509
510 if (!d_inode(dentry) || d_is_dir(dentry))
511 return 0;
512
513 /*
514 * With inodes index is enabled, we store the union overlay nlink
515 * in an xattr on the index inode. When whiting out lower hardlinks
516 * we need to decrement the overlay persistent nlink, but before the
517 * first copy up, we have no upper index inode to store the xattr.
518 *
519 * As a workaround, before whiteout/rename over of a lower hardlink,
520 * copy up to create the upper index. Creating the upper index will
521 * initialize the overlay nlink, so it could be dropped if unlink
522 * or rename succeeds.
523 *
524 * TODO: implement metadata only index copy up when called with
525 * ovl_copy_up_flags(dentry, O_PATH).
526 */
527 if (ovl_indexdir(dentry->d_sb) && !ovl_dentry_has_upper_alias(dentry) &&
528 d_inode(ovl_dentry_lower(dentry))->i_nlink > 1) {
529 err = ovl_copy_up(dentry);
530 if (err)
531 return err;
532 }
533
534 err = mutex_lock_interruptible(&oi->lock);
535 if (err)
536 return err;
537
538 if (!ovl_test_flag(OVL_INDEX, d_inode(dentry)))
539 goto out;
540
541 old_cred = ovl_override_creds(dentry->d_sb);
542 /*
543 * The overlay inode nlink should be incremented/decremented IFF the
544 * upper operation succeeds, along with nlink change of upper inode.
545 * Therefore, before link/unlink/rename, we store the union nlink
546 * value relative to the upper inode nlink in an upper inode xattr.
547 */
548 err = ovl_set_nlink_upper(dentry);
549 revert_creds(old_cred);
550
551out:
552 if (err)
553 mutex_unlock(&oi->lock);
554 else
555 *locked = true;
556
557 return err;
558}
559
560void ovl_nlink_end(struct dentry *dentry, bool locked)
561{
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300562 if (locked) {
563 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
564 d_inode(dentry)->i_nlink == 0) {
565 const struct cred *old_cred;
566
567 old_cred = ovl_override_creds(dentry->d_sb);
568 ovl_cleanup_index(dentry);
569 revert_creds(old_cred);
570 }
571
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300572 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300573 }
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300574}
Amir Goldstein5820dc02017-09-25 16:39:55 +0300575
576int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
577{
578 /* Workdir should not be the same as upperdir */
579 if (workdir == upperdir)
580 goto err;
581
582 /* Workdir should not be subdir of upperdir and vice versa */
583 if (lock_rename(workdir, upperdir) != NULL)
584 goto err_unlock;
585
586 return 0;
587
588err_unlock:
589 unlock_rename(workdir, upperdir);
590err:
591 pr_err("overlayfs: failed to lock workdir+upperdir\n");
592 return -EIO;
593}