blob: 25d202b473262274468906dd9d547599ed594152 [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
Amir Goldstein51486262018-03-28 20:22:41 +030050 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;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -040056}
57
Amir Goldsteine487d882017-11-07 13:55:04 +020058/*
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 */
66int ovl_can_decode_fh(struct super_block *sb)
Amir Goldstein02bcd152017-06-21 15:28:36 +030067{
Amir Goldsteine487d882017-11-07 13:55:04 +020068 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;
Amir Goldstein02bcd152017-06-21 15:28:36 +030073}
74
75struct dentry *ovl_indexdir(struct super_block *sb)
76{
77 struct ovl_fs *ofs = sb->s_fs_info;
78
79 return ofs->indexdir;
80}
81
Amir Goldsteinf168f102018-01-19 11:26:53 +020082/* Index all files on copy up. For now only enabled for NFS export */
83bool 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 */
91bool 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
Miklos Szeredibbb1e542016-12-16 11:02:56 +010098struct 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
109bool 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
116bool 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
124enum 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
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200129 if (ovl_dentry_upper(dentry)) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100130 type = __OVL_PATH_UPPER;
131
132 /*
Amir Goldstein59548502017-04-23 23:12:34 +0300133 * Non-dir dentry can hold lower dentry of its copy up origin.
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100134 */
Amir Goldstein59548502017-04-23 23:12:34 +0300135 if (oe->numlower) {
136 type |= __OVL_PATH_ORIGIN;
137 if (d_is_dir(dentry))
138 type |= __OVL_PATH_MERGE;
139 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100140 } else {
141 if (oe->numlower > 1)
142 type |= __OVL_PATH_MERGE;
143 }
144 return type;
145}
146
147void ovl_path_upper(struct dentry *dentry, struct path *path)
148{
149 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100150
151 path->mnt = ofs->upper_mnt;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200152 path->dentry = ovl_dentry_upper(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100153}
154
155void ovl_path_lower(struct dentry *dentry, struct path *path)
156{
157 struct ovl_entry *oe = dentry->d_fsdata;
158
Chandan Rajendrab9343632017-07-24 01:57:54 -0500159 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 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100165}
166
167enum 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
179struct dentry *ovl_dentry_upper(struct dentry *dentry)
180{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200181 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100182}
183
184struct dentry *ovl_dentry_lower(struct dentry *dentry)
185{
186 struct ovl_entry *oe = dentry->d_fsdata;
187
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200188 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100189}
190
Amir Goldsteinda309e82017-11-08 19:39:51 +0200191struct 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
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100198struct dentry *ovl_dentry_real(struct dentry *dentry)
199{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200200 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100201}
202
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200203struct dentry *ovl_i_dentry_upper(struct inode *inode)
204{
205 return ovl_upperdentry_dereference(OVL_I(inode));
206}
207
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200208struct inode *ovl_inode_upper(struct inode *inode)
Miklos Szeredi25b77132017-07-04 22:03:16 +0200209{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200210 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200211
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200212 return upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200213}
214
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200215struct inode *ovl_inode_lower(struct inode *inode)
216{
217 return OVL_I(inode)->lower;
218}
219
220struct inode *ovl_inode_real(struct inode *inode)
221{
222 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
223}
224
225
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200226struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100227{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200228 return OVL_I(inode)->cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100229}
230
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200231void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100232{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200233 OVL_I(inode)->cache = cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100234}
235
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200236void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
237{
238 set_bit(flag, &OVL_E(dentry)->flags);
239}
240
241void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
242{
243 clear_bit(flag, &OVL_E(dentry)->flags);
244}
245
246bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
247{
248 return test_bit(flag, &OVL_E(dentry)->flags);
249}
250
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100251bool ovl_dentry_is_opaque(struct dentry *dentry)
252{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200253 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100254}
255
256bool ovl_dentry_is_whiteout(struct dentry *dentry)
257{
258 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
259}
260
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100261void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100262{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200263 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100264}
265
Miklos Szeredi55acc662017-07-04 22:03:18 +0200266/*
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300267 * 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().
Miklos Szeredi55acc662017-07-04 22:03:18 +0200271 */
272bool ovl_dentry_has_upper_alias(struct dentry *dentry)
273{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200274 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200275}
276
277void ovl_dentry_set_upper_alias(struct dentry *dentry)
278{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200279 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200280}
281
Miklos Szeredia6c60652016-12-16 11:02:56 +0100282bool ovl_redirect_dir(struct super_block *sb)
283{
284 struct ovl_fs *ofs = sb->s_fs_info;
285
Amir Goldstein21a22872017-05-17 00:12:41 +0300286 return ofs->config.redirect_dir && !ofs->noxattr;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100287}
288
289const char *ovl_dentry_get_redirect(struct dentry *dentry)
290{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200291 return OVL_I(d_inode(dentry))->redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100292}
293
294void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
295{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200296 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Miklos Szeredia6c60652016-12-16 11:02:56 +0100297
Miklos Szeredicf31c462017-07-04 22:03:16 +0200298 kfree(oi->redirect);
299 oi->redirect = redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100300}
301
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200302void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
303 struct dentry *lowerdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100304{
Amir Goldstein695b46e2018-03-15 23:39:01 +0200305 struct inode *realinode = d_inode(upperdentry ?: lowerdentry);
306
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200307 if (upperdentry)
308 OVL_I(inode)->__upperdentry = upperdentry;
309 if (lowerdentry)
Amir Goldstein31747ed2018-01-14 18:35:40 +0200310 OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100311
Amir Goldstein695b46e2018-03-15 23:39:01 +0200312 ovl_copyattr(realinode, inode);
Miklos Szeredi4f357292018-07-18 15:44:41 +0200313 ovl_copyflags(realinode, inode);
Amir Goldstein695b46e2018-03-15 23:39:01 +0200314 if (!inode->i_ino)
315 inode->i_ino = realinode->i_ino;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100316}
317
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200318void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100319{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200320 struct inode *upperinode = d_inode(upperdentry);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200321
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200322 WARN_ON(OVL_I(inode)->__upperdentry);
323
Miklos Szeredi25b77132017-07-04 22:03:16 +0200324 /*
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200325 * Make sure upperdentry is consistent before making it visible
Miklos Szeredi25b77132017-07-04 22:03:16 +0200326 */
327 smp_wmb();
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200328 OVL_I(inode)->__upperdentry = upperdentry;
Amir Goldstein31747ed2018-01-14 18:35:40 +0200329 if (inode_unhashed(inode)) {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200330 if (!inode->i_ino)
331 inode->i_ino = upperinode->i_ino;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200332 inode->i_private = upperinode;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100333 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200334 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100335}
336
Miklos Szeredid9854c82018-07-18 15:44:40 +0200337static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100338{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200339 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100340
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200341 WARN_ON(!inode_is_locked(inode));
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200342 /*
343 * Version is used by readdir code to keep cache consistent. For merge
344 * dirs all changes need to be noted. For non-merge dirs, cache only
345 * contains impure (ones which have been copied up and have origins)
346 * entries, so only need to note changes to impure entries.
347 */
348 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
349 OVL_I(inode)->version++;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100350}
351
Miklos Szeredid9854c82018-07-18 15:44:40 +0200352void ovl_dir_modified(struct dentry *dentry, bool impurity)
353{
354 /* Copy mtime/ctime */
355 ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
356
357 ovl_dentry_version_inc(dentry, impurity);
358}
359
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100360u64 ovl_dentry_version_get(struct dentry *dentry)
361{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200362 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100363
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200364 WARN_ON(!inode_is_locked(inode));
365 return OVL_I(inode)->version;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100366}
367
368bool ovl_is_whiteout(struct dentry *dentry)
369{
370 struct inode *inode = dentry->d_inode;
371
372 return inode && IS_WHITEOUT(inode);
373}
374
375struct file *ovl_path_open(struct path *path, int flags)
376{
377 return dentry_open(path, flags | O_NOATIME, current_cred());
378}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200379
380int ovl_copy_up_start(struct dentry *dentry)
381{
Amir Goldsteina015daf2017-06-21 15:28:51 +0300382 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Amir Goldstein39d3d602017-01-17 06:34:56 +0200383 int err;
384
Amir Goldsteina015daf2017-06-21 15:28:51 +0300385 err = mutex_lock_interruptible(&oi->lock);
Amir Goldstein59be0972017-06-20 15:25:46 +0300386 if (!err && ovl_dentry_has_upper_alias(dentry)) {
Amir Goldsteina015daf2017-06-21 15:28:51 +0300387 err = 1; /* Already copied up */
388 mutex_unlock(&oi->lock);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200389 }
Amir Goldstein39d3d602017-01-17 06:34:56 +0200390
391 return err;
392}
393
394void ovl_copy_up_end(struct dentry *dentry)
395{
Amir Goldsteina015daf2017-06-21 15:28:51 +0300396 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200397}
Amir Goldstein82b749b2017-05-17 00:12:40 +0300398
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300399bool ovl_check_origin_xattr(struct dentry *dentry)
400{
401 int res;
402
403 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
404
405 /* Zero size value means "copied up but origin unknown" */
406 if (res >= 0)
407 return true;
408
409 return false;
410}
411
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300412bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
413{
414 int res;
415 char val;
416
417 if (!d_is_dir(dentry))
418 return false;
419
420 res = vfs_getxattr(dentry, name, &val, 1);
421 if (res == 1 && val == 'y')
422 return true;
423
424 return false;
425}
426
Amir Goldstein82b749b2017-05-17 00:12:40 +0300427int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
428 const char *name, const void *value, size_t size,
429 int xerr)
430{
431 int err;
432 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
433
434 if (ofs->noxattr)
435 return xerr;
436
437 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
438
439 if (err == -EOPNOTSUPP) {
440 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
441 ofs->noxattr = true;
442 return xerr;
443 }
444
445 return err;
446}
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300447
448int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
449{
450 int err;
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300451
Miklos Szeredi13c72072017-07-04 22:03:16 +0200452 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300453 return 0;
454
455 /*
456 * Do not fail when upper doesn't support xattrs.
457 * Upper inodes won't have origin nor redirect xattr anyway.
458 */
459 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
460 "y", 1, 0);
461 if (!err)
Miklos Szeredi13c72072017-07-04 22:03:16 +0200462 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300463
464 return err;
465}
Miklos Szeredi13c72072017-07-04 22:03:16 +0200466
467void ovl_set_flag(unsigned long flag, struct inode *inode)
468{
469 set_bit(flag, &OVL_I(inode)->flags);
470}
471
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200472void ovl_clear_flag(unsigned long flag, struct inode *inode)
473{
474 clear_bit(flag, &OVL_I(inode)->flags);
475}
476
Miklos Szeredi13c72072017-07-04 22:03:16 +0200477bool ovl_test_flag(unsigned long flag, struct inode *inode)
478{
479 return test_bit(flag, &OVL_I(inode)->flags);
480}
Amir Goldsteinad0af712017-06-21 15:28:32 +0300481
482/**
483 * Caller must hold a reference to inode to prevent it from being freed while
484 * it is marked inuse.
485 */
486bool ovl_inuse_trylock(struct dentry *dentry)
487{
488 struct inode *inode = d_inode(dentry);
489 bool locked = false;
490
491 spin_lock(&inode->i_lock);
492 if (!(inode->i_state & I_OVL_INUSE)) {
493 inode->i_state |= I_OVL_INUSE;
494 locked = true;
495 }
496 spin_unlock(&inode->i_lock);
497
498 return locked;
499}
500
501void ovl_inuse_unlock(struct dentry *dentry)
502{
503 if (dentry) {
504 struct inode *inode = d_inode(dentry);
505
506 spin_lock(&inode->i_lock);
507 WARN_ON(!(inode->i_state & I_OVL_INUSE));
508 inode->i_state &= ~I_OVL_INUSE;
509 spin_unlock(&inode->i_lock);
510 }
511}
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300512
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300513/*
514 * Does this overlay dentry need to be indexed on copy up?
515 */
516bool ovl_need_index(struct dentry *dentry)
517{
518 struct dentry *lower = ovl_dentry_lower(dentry);
519
520 if (!lower || !ovl_indexdir(dentry->d_sb))
521 return false;
522
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200523 /* Index all files for NFS export and consistency verification */
Amir Goldstein016b7202018-01-11 14:01:08 +0200524 if (ovl_index_all(dentry->d_sb))
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200525 return true;
526
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300527 /* Index only lower hardlinks on copy up */
528 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
529 return true;
530
531 return false;
532}
533
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300534/* Caller must hold OVL_I(inode)->lock */
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300535static void ovl_cleanup_index(struct dentry *dentry)
536{
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300537 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
538 struct inode *dir = indexdir->d_inode;
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300539 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
540 struct dentry *upperdentry = ovl_dentry_upper(dentry);
541 struct dentry *index = NULL;
542 struct inode *inode;
543 struct qstr name;
544 int err;
545
546 err = ovl_get_index_name(lowerdentry, &name);
547 if (err)
548 goto fail;
549
550 inode = d_inode(upperdentry);
Amir Goldstein89a17552017-09-26 07:40:37 +0300551 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300552 pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
553 upperdentry, inode->i_ino, inode->i_nlink);
554 /*
555 * We either have a bug with persistent union nlink or a lower
556 * hardlink was added while overlay is mounted. Adding a lower
557 * hardlink and then unlinking all overlay hardlinks would drop
558 * overlay nlink to zero before all upper inodes are unlinked.
559 * As a safety measure, when that situation is detected, set
560 * the overlay nlink to the index inode nlink minus one for the
561 * index entry itself.
562 */
563 set_nlink(d_inode(dentry), inode->i_nlink - 1);
564 ovl_set_nlink_upper(dentry);
565 goto out;
566 }
567
568 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300569 index = lookup_one_len(name.name, indexdir, name.len);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300570 err = PTR_ERR(index);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300571 if (IS_ERR(index)) {
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300572 index = NULL;
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300573 } else if (ovl_index_all(dentry->d_sb)) {
574 /* Whiteout orphan index to block future open by handle */
575 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
576 } else {
577 /* Cleanup orphan index entries */
578 err = ovl_cleanup(dir, index);
579 }
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300580
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300581 inode_unlock(dir);
582 if (err)
583 goto fail;
584
585out:
586 dput(index);
587 return;
588
589fail:
590 pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
591 goto out;
592}
593
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300594/*
595 * Operations that change overlay inode and upper inode nlink need to be
596 * synchronized with copy up for persistent nlink accounting.
597 */
598int ovl_nlink_start(struct dentry *dentry, bool *locked)
599{
600 struct ovl_inode *oi = OVL_I(d_inode(dentry));
601 const struct cred *old_cred;
602 int err;
603
Amir Goldstein89a17552017-09-26 07:40:37 +0300604 if (!d_inode(dentry))
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300605 return 0;
606
607 /*
608 * With inodes index is enabled, we store the union overlay nlink
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300609 * in an xattr on the index inode. When whiting out an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300610 * we need to decrement the overlay persistent nlink, but before the
611 * first copy up, we have no upper index inode to store the xattr.
612 *
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300613 * As a workaround, before whiteout/rename over an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300614 * copy up to create the upper index. Creating the upper index will
615 * initialize the overlay nlink, so it could be dropped if unlink
616 * or rename succeeds.
617 *
618 * TODO: implement metadata only index copy up when called with
619 * ovl_copy_up_flags(dentry, O_PATH).
620 */
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300621 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300622 err = ovl_copy_up(dentry);
623 if (err)
624 return err;
625 }
626
627 err = mutex_lock_interruptible(&oi->lock);
628 if (err)
629 return err;
630
Amir Goldstein89a17552017-09-26 07:40:37 +0300631 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300632 goto out;
633
634 old_cred = ovl_override_creds(dentry->d_sb);
635 /*
636 * The overlay inode nlink should be incremented/decremented IFF the
637 * upper operation succeeds, along with nlink change of upper inode.
638 * Therefore, before link/unlink/rename, we store the union nlink
639 * value relative to the upper inode nlink in an upper inode xattr.
640 */
641 err = ovl_set_nlink_upper(dentry);
642 revert_creds(old_cred);
643
644out:
645 if (err)
646 mutex_unlock(&oi->lock);
647 else
648 *locked = true;
649
650 return err;
651}
652
653void ovl_nlink_end(struct dentry *dentry, bool locked)
654{
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300655 if (locked) {
656 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
657 d_inode(dentry)->i_nlink == 0) {
658 const struct cred *old_cred;
659
660 old_cred = ovl_override_creds(dentry->d_sb);
661 ovl_cleanup_index(dentry);
662 revert_creds(old_cred);
663 }
664
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300665 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300666 }
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300667}
Amir Goldstein5820dc02017-09-25 16:39:55 +0300668
669int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
670{
671 /* Workdir should not be the same as upperdir */
672 if (workdir == upperdir)
673 goto err;
674
675 /* Workdir should not be subdir of upperdir and vice versa */
676 if (lock_rename(workdir, upperdir) != NULL)
677 goto err_unlock;
678
679 return 0;
680
681err_unlock:
682 unlock_rename(workdir, upperdir);
683err:
684 pr_err("overlayfs: failed to lock workdir+upperdir\n");
685 return -EIO;
686}