blob: 5042293d2078902dbba3f96a55f6d73f126dba4e [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
Amir Goldsteinf168f102018-01-19 11:26:53 +020066/* Index all files on copy up. For now only enabled for NFS export */
67bool 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 */
75bool 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
Miklos Szeredibbb1e542016-12-16 11:02:56 +010082struct 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
93bool 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
100bool 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
108enum 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
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200113 if (ovl_dentry_upper(dentry)) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100114 type = __OVL_PATH_UPPER;
115
116 /*
Amir Goldstein59548502017-04-23 23:12:34 +0300117 * Non-dir dentry can hold lower dentry of its copy up origin.
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100118 */
Amir Goldstein59548502017-04-23 23:12:34 +0300119 if (oe->numlower) {
120 type |= __OVL_PATH_ORIGIN;
121 if (d_is_dir(dentry))
122 type |= __OVL_PATH_MERGE;
123 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100124 } else {
125 if (oe->numlower > 1)
126 type |= __OVL_PATH_MERGE;
127 }
128 return type;
129}
130
131void ovl_path_upper(struct dentry *dentry, struct path *path)
132{
133 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100134
135 path->mnt = ofs->upper_mnt;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200136 path->dentry = ovl_dentry_upper(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100137}
138
139void ovl_path_lower(struct dentry *dentry, struct path *path)
140{
141 struct ovl_entry *oe = dentry->d_fsdata;
142
Chandan Rajendrab9343632017-07-24 01:57:54 -0500143 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 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100149}
150
151enum 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
163struct dentry *ovl_dentry_upper(struct dentry *dentry)
164{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200165 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100166}
167
168struct dentry *ovl_dentry_lower(struct dentry *dentry)
169{
170 struct ovl_entry *oe = dentry->d_fsdata;
171
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200172 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100173}
174
Amir Goldsteinda309e82017-11-08 19:39:51 +0200175struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
176{
177 struct ovl_entry *oe = dentry->d_fsdata;
178
179 return oe->numlower ? oe->lowerstack[0].layer : NULL;
180}
181
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100182struct dentry *ovl_dentry_real(struct dentry *dentry)
183{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200184 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100185}
186
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200187struct dentry *ovl_i_dentry_upper(struct inode *inode)
188{
189 return ovl_upperdentry_dereference(OVL_I(inode));
190}
191
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200192struct inode *ovl_inode_upper(struct inode *inode)
Miklos Szeredi25b77132017-07-04 22:03:16 +0200193{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200194 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200195
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200196 return upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200197}
198
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200199struct inode *ovl_inode_lower(struct inode *inode)
200{
201 return OVL_I(inode)->lower;
202}
203
204struct inode *ovl_inode_real(struct inode *inode)
205{
206 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
207}
208
209
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200210struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100211{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200212 return OVL_I(inode)->cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100213}
214
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200215void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100216{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200217 OVL_I(inode)->cache = cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100218}
219
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200220void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
221{
222 set_bit(flag, &OVL_E(dentry)->flags);
223}
224
225void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
226{
227 clear_bit(flag, &OVL_E(dentry)->flags);
228}
229
230bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
231{
232 return test_bit(flag, &OVL_E(dentry)->flags);
233}
234
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100235bool ovl_dentry_is_opaque(struct dentry *dentry)
236{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200237 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100238}
239
240bool ovl_dentry_is_whiteout(struct dentry *dentry)
241{
242 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
243}
244
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100245void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100246{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200247 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100248}
249
Miklos Szeredi55acc662017-07-04 22:03:18 +0200250/*
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300251 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
252 * to return positive, while there's no actual upper alias for the inode.
253 * Copy up code needs to know about the existence of the upper alias, so it
254 * can't use ovl_dentry_upper().
Miklos Szeredi55acc662017-07-04 22:03:18 +0200255 */
256bool ovl_dentry_has_upper_alias(struct dentry *dentry)
257{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200258 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200259}
260
261void ovl_dentry_set_upper_alias(struct dentry *dentry)
262{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200263 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200264}
265
Miklos Szeredia6c60652016-12-16 11:02:56 +0100266bool ovl_redirect_dir(struct super_block *sb)
267{
268 struct ovl_fs *ofs = sb->s_fs_info;
269
Amir Goldstein21a22872017-05-17 00:12:41 +0300270 return ofs->config.redirect_dir && !ofs->noxattr;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100271}
272
273const char *ovl_dentry_get_redirect(struct dentry *dentry)
274{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200275 return OVL_I(d_inode(dentry))->redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100276}
277
278void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
279{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200280 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Miklos Szeredia6c60652016-12-16 11:02:56 +0100281
Miklos Szeredicf31c462017-07-04 22:03:16 +0200282 kfree(oi->redirect);
283 oi->redirect = redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100284}
285
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200286void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
287 struct dentry *lowerdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100288{
Amir Goldstein695b46e2018-03-15 23:39:01 +0200289 struct inode *realinode = d_inode(upperdentry ?: lowerdentry);
290
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200291 if (upperdentry)
292 OVL_I(inode)->__upperdentry = upperdentry;
293 if (lowerdentry)
Amir Goldstein31747ed2018-01-14 18:35:40 +0200294 OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100295
Amir Goldstein695b46e2018-03-15 23:39:01 +0200296 ovl_copyattr(realinode, inode);
297 if (!inode->i_ino)
298 inode->i_ino = realinode->i_ino;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100299}
300
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200301void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100302{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200303 struct inode *upperinode = d_inode(upperdentry);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200304
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200305 WARN_ON(OVL_I(inode)->__upperdentry);
306
Miklos Szeredi25b77132017-07-04 22:03:16 +0200307 /*
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200308 * Make sure upperdentry is consistent before making it visible
Miklos Szeredi25b77132017-07-04 22:03:16 +0200309 */
310 smp_wmb();
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200311 OVL_I(inode)->__upperdentry = upperdentry;
Amir Goldstein31747ed2018-01-14 18:35:40 +0200312 if (inode_unhashed(inode)) {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200313 if (!inode->i_ino)
314 inode->i_ino = upperinode->i_ino;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200315 inode->i_private = upperinode;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100316 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200317 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100318}
319
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200320void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100321{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200322 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100323
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200324 WARN_ON(!inode_is_locked(inode));
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200325 /*
326 * Version is used by readdir code to keep cache consistent. For merge
327 * dirs all changes need to be noted. For non-merge dirs, cache only
328 * contains impure (ones which have been copied up and have origins)
329 * entries, so only need to note changes to impure entries.
330 */
331 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
332 OVL_I(inode)->version++;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100333}
334
335u64 ovl_dentry_version_get(struct dentry *dentry)
336{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200337 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100338
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200339 WARN_ON(!inode_is_locked(inode));
340 return OVL_I(inode)->version;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100341}
342
343bool ovl_is_whiteout(struct dentry *dentry)
344{
345 struct inode *inode = dentry->d_inode;
346
347 return inode && IS_WHITEOUT(inode);
348}
349
350struct file *ovl_path_open(struct path *path, int flags)
351{
352 return dentry_open(path, flags | O_NOATIME, current_cred());
353}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200354
355int ovl_copy_up_start(struct dentry *dentry)
356{
Amir Goldsteina015daf2017-06-21 15:28:51 +0300357 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Amir Goldstein39d3d602017-01-17 06:34:56 +0200358 int err;
359
Amir Goldsteina015daf2017-06-21 15:28:51 +0300360 err = mutex_lock_interruptible(&oi->lock);
Amir Goldstein59be0972017-06-20 15:25:46 +0300361 if (!err && ovl_dentry_has_upper_alias(dentry)) {
Amir Goldsteina015daf2017-06-21 15:28:51 +0300362 err = 1; /* Already copied up */
363 mutex_unlock(&oi->lock);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200364 }
Amir Goldstein39d3d602017-01-17 06:34:56 +0200365
366 return err;
367}
368
369void ovl_copy_up_end(struct dentry *dentry)
370{
Amir Goldsteina015daf2017-06-21 15:28:51 +0300371 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200372}
Amir Goldstein82b749b2017-05-17 00:12:40 +0300373
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300374bool ovl_check_origin_xattr(struct dentry *dentry)
375{
376 int res;
377
378 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
379
380 /* Zero size value means "copied up but origin unknown" */
381 if (res >= 0)
382 return true;
383
384 return false;
385}
386
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300387bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
388{
389 int res;
390 char val;
391
392 if (!d_is_dir(dentry))
393 return false;
394
395 res = vfs_getxattr(dentry, name, &val, 1);
396 if (res == 1 && val == 'y')
397 return true;
398
399 return false;
400}
401
Amir Goldstein82b749b2017-05-17 00:12:40 +0300402int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
403 const char *name, const void *value, size_t size,
404 int xerr)
405{
406 int err;
407 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
408
409 if (ofs->noxattr)
410 return xerr;
411
412 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
413
414 if (err == -EOPNOTSUPP) {
415 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
416 ofs->noxattr = true;
417 return xerr;
418 }
419
420 return err;
421}
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300422
423int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
424{
425 int err;
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300426
Miklos Szeredi13c72072017-07-04 22:03:16 +0200427 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300428 return 0;
429
430 /*
431 * Do not fail when upper doesn't support xattrs.
432 * Upper inodes won't have origin nor redirect xattr anyway.
433 */
434 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
435 "y", 1, 0);
436 if (!err)
Miklos Szeredi13c72072017-07-04 22:03:16 +0200437 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300438
439 return err;
440}
Miklos Szeredi13c72072017-07-04 22:03:16 +0200441
442void ovl_set_flag(unsigned long flag, struct inode *inode)
443{
444 set_bit(flag, &OVL_I(inode)->flags);
445}
446
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200447void ovl_clear_flag(unsigned long flag, struct inode *inode)
448{
449 clear_bit(flag, &OVL_I(inode)->flags);
450}
451
Miklos Szeredi13c72072017-07-04 22:03:16 +0200452bool ovl_test_flag(unsigned long flag, struct inode *inode)
453{
454 return test_bit(flag, &OVL_I(inode)->flags);
455}
Amir Goldsteinad0af712017-06-21 15:28:32 +0300456
457/**
458 * Caller must hold a reference to inode to prevent it from being freed while
459 * it is marked inuse.
460 */
461bool ovl_inuse_trylock(struct dentry *dentry)
462{
463 struct inode *inode = d_inode(dentry);
464 bool locked = false;
465
466 spin_lock(&inode->i_lock);
467 if (!(inode->i_state & I_OVL_INUSE)) {
468 inode->i_state |= I_OVL_INUSE;
469 locked = true;
470 }
471 spin_unlock(&inode->i_lock);
472
473 return locked;
474}
475
476void ovl_inuse_unlock(struct dentry *dentry)
477{
478 if (dentry) {
479 struct inode *inode = d_inode(dentry);
480
481 spin_lock(&inode->i_lock);
482 WARN_ON(!(inode->i_state & I_OVL_INUSE));
483 inode->i_state &= ~I_OVL_INUSE;
484 spin_unlock(&inode->i_lock);
485 }
486}
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300487
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300488/*
489 * Does this overlay dentry need to be indexed on copy up?
490 */
491bool ovl_need_index(struct dentry *dentry)
492{
493 struct dentry *lower = ovl_dentry_lower(dentry);
494
495 if (!lower || !ovl_indexdir(dentry->d_sb))
496 return false;
497
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200498 /* Index all files for NFS export and consistency verification */
Amir Goldstein016b7202018-01-11 14:01:08 +0200499 if (ovl_index_all(dentry->d_sb))
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200500 return true;
501
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300502 /* Index only lower hardlinks on copy up */
503 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
504 return true;
505
506 return false;
507}
508
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300509/* Caller must hold OVL_I(inode)->lock */
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300510static void ovl_cleanup_index(struct dentry *dentry)
511{
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300512 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
513 struct inode *dir = indexdir->d_inode;
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300514 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
515 struct dentry *upperdentry = ovl_dentry_upper(dentry);
516 struct dentry *index = NULL;
517 struct inode *inode;
518 struct qstr name;
519 int err;
520
521 err = ovl_get_index_name(lowerdentry, &name);
522 if (err)
523 goto fail;
524
525 inode = d_inode(upperdentry);
Amir Goldstein89a17552017-09-26 07:40:37 +0300526 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300527 pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
528 upperdentry, inode->i_ino, inode->i_nlink);
529 /*
530 * We either have a bug with persistent union nlink or a lower
531 * hardlink was added while overlay is mounted. Adding a lower
532 * hardlink and then unlinking all overlay hardlinks would drop
533 * overlay nlink to zero before all upper inodes are unlinked.
534 * As a safety measure, when that situation is detected, set
535 * the overlay nlink to the index inode nlink minus one for the
536 * index entry itself.
537 */
538 set_nlink(d_inode(dentry), inode->i_nlink - 1);
539 ovl_set_nlink_upper(dentry);
540 goto out;
541 }
542
543 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300544 index = lookup_one_len(name.name, indexdir, name.len);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300545 err = PTR_ERR(index);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300546 if (IS_ERR(index)) {
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300547 index = NULL;
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300548 } else if (ovl_index_all(dentry->d_sb)) {
549 /* Whiteout orphan index to block future open by handle */
550 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
551 } else {
552 /* Cleanup orphan index entries */
553 err = ovl_cleanup(dir, index);
554 }
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300555
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300556 inode_unlock(dir);
557 if (err)
558 goto fail;
559
560out:
561 dput(index);
562 return;
563
564fail:
565 pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
566 goto out;
567}
568
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300569/*
570 * Operations that change overlay inode and upper inode nlink need to be
571 * synchronized with copy up for persistent nlink accounting.
572 */
573int ovl_nlink_start(struct dentry *dentry, bool *locked)
574{
575 struct ovl_inode *oi = OVL_I(d_inode(dentry));
576 const struct cred *old_cred;
577 int err;
578
Amir Goldstein89a17552017-09-26 07:40:37 +0300579 if (!d_inode(dentry))
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300580 return 0;
581
582 /*
583 * With inodes index is enabled, we store the union overlay nlink
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300584 * in an xattr on the index inode. When whiting out an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300585 * we need to decrement the overlay persistent nlink, but before the
586 * first copy up, we have no upper index inode to store the xattr.
587 *
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300588 * As a workaround, before whiteout/rename over an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300589 * copy up to create the upper index. Creating the upper index will
590 * initialize the overlay nlink, so it could be dropped if unlink
591 * or rename succeeds.
592 *
593 * TODO: implement metadata only index copy up when called with
594 * ovl_copy_up_flags(dentry, O_PATH).
595 */
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300596 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300597 err = ovl_copy_up(dentry);
598 if (err)
599 return err;
600 }
601
602 err = mutex_lock_interruptible(&oi->lock);
603 if (err)
604 return err;
605
Amir Goldstein89a17552017-09-26 07:40:37 +0300606 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300607 goto out;
608
609 old_cred = ovl_override_creds(dentry->d_sb);
610 /*
611 * The overlay inode nlink should be incremented/decremented IFF the
612 * upper operation succeeds, along with nlink change of upper inode.
613 * Therefore, before link/unlink/rename, we store the union nlink
614 * value relative to the upper inode nlink in an upper inode xattr.
615 */
616 err = ovl_set_nlink_upper(dentry);
617 revert_creds(old_cred);
618
619out:
620 if (err)
621 mutex_unlock(&oi->lock);
622 else
623 *locked = true;
624
625 return err;
626}
627
628void ovl_nlink_end(struct dentry *dentry, bool locked)
629{
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300630 if (locked) {
631 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
632 d_inode(dentry)->i_nlink == 0) {
633 const struct cred *old_cred;
634
635 old_cred = ovl_override_creds(dentry->d_sb);
636 ovl_cleanup_index(dentry);
637 revert_creds(old_cred);
638 }
639
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300640 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300641 }
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300642}
Amir Goldstein5820dc02017-09-25 16:39:55 +0300643
644int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
645{
646 /* Workdir should not be the same as upperdir */
647 if (workdir == upperdir)
648 goto err;
649
650 /* Workdir should not be subdir of upperdir and vice versa */
651 if (lock_rename(workdir, upperdir) != NULL)
652 goto err_unlock;
653
654 return 0;
655
656err_unlock:
657 unlock_rename(workdir, upperdir);
658err:
659 pr_err("overlayfs: failed to lock workdir+upperdir\n");
660 return -EIO;
661}