blob: e0f0da9efc1c2bb8c60c76567ab10d8c2514da75 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Miklos Szeredibbb1e542016-12-16 11:02:56 +01002/*
3 * Copyright (C) 2011 Novell Inc.
4 * Copyright (C) 2016 Red Hat, Inc.
Miklos Szeredibbb1e542016-12-16 11:02:56 +01005 */
6
7#include <linux/fs.h>
8#include <linux/mount.h>
9#include <linux/slab.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010010#include <linux/cred.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010011#include <linux/xattr.h>
Amir Goldstein02bcd152017-06-21 15:28:36 +030012#include <linux/exportfs.h>
13#include <linux/uuid.h>
Amir Goldsteincaf70cb2017-06-21 13:46:12 +030014#include <linux/namei.h>
15#include <linux/ratelimit.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010016#include "overlayfs.h"
Miklos Szeredibbb1e542016-12-16 11:02:56 +010017
18int ovl_want_write(struct dentry *dentry)
19{
20 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
21 return mnt_want_write(ofs->upper_mnt);
22}
23
24void ovl_drop_write(struct dentry *dentry)
25{
26 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
27 mnt_drop_write(ofs->upper_mnt);
28}
29
30struct dentry *ovl_workdir(struct dentry *dentry)
31{
32 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
33 return ofs->workdir;
34}
35
36const struct cred *ovl_override_creds(struct super_block *sb)
37{
38 struct ovl_fs *ofs = sb->s_fs_info;
39
Mark Salyzyn6120a4d2018-06-14 11:15:22 -070040 if (!ofs->config.override_creds)
41 return NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +010042 return override_creds(ofs->creator_cred);
43}
44
Mark Salyzyn6120a4d2018-06-14 11:15:22 -070045void ovl_revert_creds(struct super_block *sb, const struct cred *old_cred)
46{
47 if (old_cred)
48 revert_creds(old_cred);
49}
50
Amir Goldsteine487d882017-11-07 13:55:04 +020051/*
52 * Check if underlying fs supports file handles and try to determine encoding
53 * type, in order to deduce maximum inode number used by fs.
54 *
55 * Return 0 if file handles are not supported.
56 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
57 * Return -1 if fs uses a non default encoding with unknown inode size.
58 */
59int ovl_can_decode_fh(struct super_block *sb)
Amir Goldstein02bcd152017-06-21 15:28:36 +030060{
Amir Goldstein9df085f2018-09-03 09:12:09 +030061 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
Amir Goldsteine487d882017-11-07 13:55:04 +020062 return 0;
63
64 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
Amir Goldstein02bcd152017-06-21 15:28:36 +030065}
66
67struct dentry *ovl_indexdir(struct super_block *sb)
68{
69 struct ovl_fs *ofs = sb->s_fs_info;
70
71 return ofs->indexdir;
72}
73
Amir Goldsteinf168f102018-01-19 11:26:53 +020074/* Index all files on copy up. For now only enabled for NFS export */
75bool ovl_index_all(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
82/* Verify lower origin on lookup. For now only enabled for NFS export */
83bool ovl_verify_lower(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
Miklos Szeredibbb1e542016-12-16 11:02:56 +010090struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
91{
92 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
93 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
94
95 if (oe)
96 oe->numlower = numlower;
97
98 return oe;
99}
100
101bool ovl_dentry_remote(struct dentry *dentry)
102{
103 return dentry->d_flags &
104 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
105 DCACHE_OP_REAL);
106}
107
108bool ovl_dentry_weird(struct dentry *dentry)
109{
110 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
111 DCACHE_MANAGE_TRANSIT |
112 DCACHE_OP_HASH |
113 DCACHE_OP_COMPARE);
114}
115
116enum ovl_path_type ovl_path_type(struct dentry *dentry)
117{
118 struct ovl_entry *oe = dentry->d_fsdata;
119 enum ovl_path_type type = 0;
120
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200121 if (ovl_dentry_upper(dentry)) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100122 type = __OVL_PATH_UPPER;
123
124 /*
Amir Goldstein59548502017-04-23 23:12:34 +0300125 * Non-dir dentry can hold lower dentry of its copy up origin.
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100126 */
Amir Goldstein59548502017-04-23 23:12:34 +0300127 if (oe->numlower) {
Vivek Goyal60124872018-05-11 11:49:32 -0400128 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
129 type |= __OVL_PATH_ORIGIN;
Vivek Goyal0b17c282018-05-11 11:49:32 -0400130 if (d_is_dir(dentry) ||
131 !ovl_has_upperdata(d_inode(dentry)))
Amir Goldstein59548502017-04-23 23:12:34 +0300132 type |= __OVL_PATH_MERGE;
133 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100134 } else {
135 if (oe->numlower > 1)
136 type |= __OVL_PATH_MERGE;
137 }
138 return type;
139}
140
141void ovl_path_upper(struct dentry *dentry, struct path *path)
142{
143 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100144
145 path->mnt = ofs->upper_mnt;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200146 path->dentry = ovl_dentry_upper(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100147}
148
149void ovl_path_lower(struct dentry *dentry, struct path *path)
150{
151 struct ovl_entry *oe = dentry->d_fsdata;
152
Chandan Rajendrab9343632017-07-24 01:57:54 -0500153 if (oe->numlower) {
154 path->mnt = oe->lowerstack[0].layer->mnt;
155 path->dentry = oe->lowerstack[0].dentry;
156 } else {
157 *path = (struct path) { };
158 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100159}
160
Vivek Goyal4f93b422018-05-11 11:49:30 -0400161void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
162{
163 struct ovl_entry *oe = dentry->d_fsdata;
164
165 if (oe->numlower) {
166 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
167 path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
168 } else {
169 *path = (struct path) { };
170 }
171}
172
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100173enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
174{
175 enum ovl_path_type type = ovl_path_type(dentry);
176
177 if (!OVL_TYPE_UPPER(type))
178 ovl_path_lower(dentry, path);
179 else
180 ovl_path_upper(dentry, path);
181
182 return type;
183}
184
185struct dentry *ovl_dentry_upper(struct dentry *dentry)
186{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200187 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100188}
189
190struct dentry *ovl_dentry_lower(struct dentry *dentry)
191{
192 struct ovl_entry *oe = dentry->d_fsdata;
193
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200194 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100195}
196
Miklos Szeredi13464162020-01-24 09:46:45 +0100197const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
Amir Goldsteinda309e82017-11-08 19:39:51 +0200198{
199 struct ovl_entry *oe = dentry->d_fsdata;
200
201 return oe->numlower ? oe->lowerstack[0].layer : NULL;
202}
203
Vivek Goyal647d2532018-05-11 11:49:30 -0400204/*
205 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
206 * dependig on what is stored in lowerstack[0]. At times we need to find
207 * lower dentry which has data (and not metacopy dentry). This helper
208 * returns the lower data dentry.
209 */
210struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
211{
212 struct ovl_entry *oe = dentry->d_fsdata;
213
214 return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
215}
216
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100217struct dentry *ovl_dentry_real(struct dentry *dentry)
218{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200219 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100220}
221
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200222struct dentry *ovl_i_dentry_upper(struct inode *inode)
223{
224 return ovl_upperdentry_dereference(OVL_I(inode));
225}
226
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200227struct inode *ovl_inode_upper(struct inode *inode)
Miklos Szeredi25b77132017-07-04 22:03:16 +0200228{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200229 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200230
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200231 return upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200232}
233
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200234struct inode *ovl_inode_lower(struct inode *inode)
235{
236 return OVL_I(inode)->lower;
237}
238
239struct inode *ovl_inode_real(struct inode *inode)
240{
241 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
242}
243
Vivek Goyal2664bd02018-05-11 11:49:30 -0400244/* Return inode which contains lower data. Do not return metacopy */
245struct inode *ovl_inode_lowerdata(struct inode *inode)
246{
247 if (WARN_ON(!S_ISREG(inode->i_mode)))
248 return NULL;
249
250 return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
251}
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200252
Vivek Goyal4823d492018-05-11 11:49:31 -0400253/* Return real inode which contains data. Does not return metacopy inode */
254struct inode *ovl_inode_realdata(struct inode *inode)
255{
256 struct inode *upperinode;
257
258 upperinode = ovl_inode_upper(inode);
259 if (upperinode && ovl_has_upperdata(inode))
260 return upperinode;
261
262 return ovl_inode_lowerdata(inode);
263}
264
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200265struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100266{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200267 return OVL_I(inode)->cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100268}
269
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200270void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100271{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200272 OVL_I(inode)->cache = cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100273}
274
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200275void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
276{
277 set_bit(flag, &OVL_E(dentry)->flags);
278}
279
280void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
281{
282 clear_bit(flag, &OVL_E(dentry)->flags);
283}
284
285bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
286{
287 return test_bit(flag, &OVL_E(dentry)->flags);
288}
289
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100290bool ovl_dentry_is_opaque(struct dentry *dentry)
291{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200292 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100293}
294
295bool ovl_dentry_is_whiteout(struct dentry *dentry)
296{
297 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
298}
299
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100300void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100301{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200302 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100303}
304
Miklos Szeredi55acc662017-07-04 22:03:18 +0200305/*
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300306 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
307 * to return positive, while there's no actual upper alias for the inode.
308 * Copy up code needs to know about the existence of the upper alias, so it
309 * can't use ovl_dentry_upper().
Miklos Szeredi55acc662017-07-04 22:03:18 +0200310 */
311bool ovl_dentry_has_upper_alias(struct dentry *dentry)
312{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200313 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200314}
315
316void ovl_dentry_set_upper_alias(struct dentry *dentry)
317{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200318 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200319}
320
Vivek Goyal0c288872018-05-11 11:49:28 -0400321static bool ovl_should_check_upperdata(struct inode *inode)
322{
323 if (!S_ISREG(inode->i_mode))
324 return false;
325
326 if (!ovl_inode_lower(inode))
327 return false;
328
329 return true;
330}
331
332bool ovl_has_upperdata(struct inode *inode)
333{
334 if (!ovl_should_check_upperdata(inode))
335 return true;
336
337 if (!ovl_test_flag(OVL_UPPERDATA, inode))
338 return false;
339 /*
340 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
341 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
342 * if setting of OVL_UPPERDATA is visible, then effects of writes
343 * before that are visible too.
344 */
345 smp_rmb();
346 return true;
347}
348
349void ovl_set_upperdata(struct inode *inode)
350{
351 /*
352 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
353 * if OVL_UPPERDATA flag is visible, then effects of write operations
354 * before it are visible as well.
355 */
356 smp_wmb();
357 ovl_set_flag(OVL_UPPERDATA, inode);
358}
359
360/* Caller should hold ovl_inode->lock */
361bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
362{
363 if (!ovl_open_flags_need_copy_up(flags))
364 return false;
365
366 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
367}
368
369bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
370{
371 if (!ovl_open_flags_need_copy_up(flags))
372 return false;
373
374 return !ovl_has_upperdata(d_inode(dentry));
375}
376
Miklos Szeredia6c60652016-12-16 11:02:56 +0100377bool ovl_redirect_dir(struct super_block *sb)
378{
379 struct ovl_fs *ofs = sb->s_fs_info;
380
Amir Goldstein21a22872017-05-17 00:12:41 +0300381 return ofs->config.redirect_dir && !ofs->noxattr;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100382}
383
384const char *ovl_dentry_get_redirect(struct dentry *dentry)
385{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200386 return OVL_I(d_inode(dentry))->redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100387}
388
389void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
390{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200391 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Miklos Szeredia6c60652016-12-16 11:02:56 +0100392
Miklos Szeredicf31c462017-07-04 22:03:16 +0200393 kfree(oi->redirect);
394 oi->redirect = redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100395}
396
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200397void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
Vivek Goyal2664bd02018-05-11 11:49:30 -0400398 struct dentry *lowerdentry, struct dentry *lowerdata)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100399{
Amir Goldstein695b46e2018-03-15 23:39:01 +0200400 struct inode *realinode = d_inode(upperdentry ?: lowerdentry);
401
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200402 if (upperdentry)
403 OVL_I(inode)->__upperdentry = upperdentry;
404 if (lowerdentry)
Amir Goldstein31747ed2018-01-14 18:35:40 +0200405 OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
Vivek Goyal2664bd02018-05-11 11:49:30 -0400406 if (lowerdata)
407 OVL_I(inode)->lowerdata = igrab(d_inode(lowerdata));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100408
Amir Goldstein695b46e2018-03-15 23:39:01 +0200409 ovl_copyattr(realinode, inode);
Miklos Szeredi4f357292018-07-18 15:44:41 +0200410 ovl_copyflags(realinode, inode);
Amir Goldstein695b46e2018-03-15 23:39:01 +0200411 if (!inode->i_ino)
412 inode->i_ino = realinode->i_ino;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100413}
414
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200415void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100416{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200417 struct inode *upperinode = d_inode(upperdentry);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200418
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200419 WARN_ON(OVL_I(inode)->__upperdentry);
420
Miklos Szeredi25b77132017-07-04 22:03:16 +0200421 /*
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200422 * Make sure upperdentry is consistent before making it visible
Miklos Szeredi25b77132017-07-04 22:03:16 +0200423 */
424 smp_wmb();
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200425 OVL_I(inode)->__upperdentry = upperdentry;
Amir Goldstein31747ed2018-01-14 18:35:40 +0200426 if (inode_unhashed(inode)) {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200427 if (!inode->i_ino)
428 inode->i_ino = upperinode->i_ino;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200429 inode->i_private = upperinode;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100430 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200431 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100432}
433
Miklos Szeredid9854c82018-07-18 15:44:40 +0200434static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100435{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200436 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100437
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200438 WARN_ON(!inode_is_locked(inode));
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200439 /*
440 * Version is used by readdir code to keep cache consistent. For merge
441 * dirs all changes need to be noted. For non-merge dirs, cache only
442 * contains impure (ones which have been copied up and have origins)
443 * entries, so only need to note changes to impure entries.
444 */
445 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
446 OVL_I(inode)->version++;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100447}
448
Miklos Szeredid9854c82018-07-18 15:44:40 +0200449void ovl_dir_modified(struct dentry *dentry, bool impurity)
450{
451 /* Copy mtime/ctime */
452 ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
453
454 ovl_dentry_version_inc(dentry, impurity);
455}
456
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100457u64 ovl_dentry_version_get(struct dentry *dentry)
458{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200459 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100460
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200461 WARN_ON(!inode_is_locked(inode));
462 return OVL_I(inode)->version;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100463}
464
465bool ovl_is_whiteout(struct dentry *dentry)
466{
467 struct inode *inode = dentry->d_inode;
468
469 return inode && IS_WHITEOUT(inode);
470}
471
472struct file *ovl_path_open(struct path *path, int flags)
473{
474 return dentry_open(path, flags | O_NOATIME, current_cred());
475}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200476
Vivek Goyal0c288872018-05-11 11:49:28 -0400477/* Caller should hold ovl_inode->lock */
478static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
479{
480 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
481
482 if (ovl_dentry_upper(dentry) &&
483 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
484 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
485 return true;
486
487 return false;
488}
489
490bool ovl_already_copied_up(struct dentry *dentry, int flags)
Vivek Goyal2002df82018-05-11 11:49:28 -0400491{
492 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
493
494 /*
495 * Check if copy-up has happened as well as for upper alias (in
496 * case of hard links) is there.
497 *
498 * Both checks are lockless:
499 * - false negatives: will recheck under oi->lock
500 * - false positives:
501 * + ovl_dentry_upper() uses memory barriers to ensure the
502 * upper dentry is up-to-date
503 * + ovl_dentry_has_upper_alias() relies on locking of
504 * upper parent i_rwsem to prevent reordering copy-up
505 * with rename.
506 */
507 if (ovl_dentry_upper(dentry) &&
Vivek Goyal0c288872018-05-11 11:49:28 -0400508 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
509 !ovl_dentry_needs_data_copy_up(dentry, flags))
Vivek Goyal2002df82018-05-11 11:49:28 -0400510 return true;
511
512 return false;
513}
514
Vivek Goyal0c288872018-05-11 11:49:28 -0400515int ovl_copy_up_start(struct dentry *dentry, int flags)
Amir Goldstein39d3d602017-01-17 06:34:56 +0200516{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300517 struct inode *inode = d_inode(dentry);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200518 int err;
519
Amir Goldstein1e92e302018-10-18 18:37:14 +0300520 err = ovl_inode_lock(inode);
Vivek Goyal0c288872018-05-11 11:49:28 -0400521 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
Amir Goldsteina015daf2017-06-21 15:28:51 +0300522 err = 1; /* Already copied up */
Amir Goldstein1e92e302018-10-18 18:37:14 +0300523 ovl_inode_unlock(inode);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200524 }
Amir Goldstein39d3d602017-01-17 06:34:56 +0200525
526 return err;
527}
528
529void ovl_copy_up_end(struct dentry *dentry)
530{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300531 ovl_inode_unlock(d_inode(dentry));
Amir Goldstein39d3d602017-01-17 06:34:56 +0200532}
Amir Goldstein82b749b2017-05-17 00:12:40 +0300533
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300534bool ovl_check_origin_xattr(struct dentry *dentry)
535{
Greg Kroah-Hartman791d8a92020-02-08 14:23:00 +0100536 ssize_t res;
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300537
Greg Kroah-Hartman791d8a92020-02-08 14:23:00 +0100538 res = ovl_do_vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300539
540 /* Zero size value means "copied up but origin unknown" */
541 if (res >= 0)
542 return true;
543
544 return false;
545}
546
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300547bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
548{
Greg Kroah-Hartman791d8a92020-02-08 14:23:00 +0100549 ssize_t res;
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300550 char val;
551
552 if (!d_is_dir(dentry))
553 return false;
554
Greg Kroah-Hartman791d8a92020-02-08 14:23:00 +0100555 res = ovl_do_vfs_getxattr(dentry, name, &val, 1);
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300556 if (res == 1 && val == 'y')
557 return true;
558
559 return false;
560}
561
Amir Goldstein82b749b2017-05-17 00:12:40 +0300562int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
563 const char *name, const void *value, size_t size,
564 int xerr)
565{
566 int err;
567 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
568
569 if (ofs->noxattr)
570 return xerr;
571
572 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
573
574 if (err == -EOPNOTSUPP) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800575 pr_warn("cannot set %s xattr on upper\n", name);
Amir Goldstein82b749b2017-05-17 00:12:40 +0300576 ofs->noxattr = true;
577 return xerr;
578 }
579
580 return err;
581}
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300582
583int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
584{
585 int err;
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300586
Miklos Szeredi13c72072017-07-04 22:03:16 +0200587 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300588 return 0;
589
590 /*
591 * Do not fail when upper doesn't support xattrs.
592 * Upper inodes won't have origin nor redirect xattr anyway.
593 */
594 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
595 "y", 1, 0);
596 if (!err)
Miklos Szeredi13c72072017-07-04 22:03:16 +0200597 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300598
599 return err;
600}
Miklos Szeredi13c72072017-07-04 22:03:16 +0200601
602void ovl_set_flag(unsigned long flag, struct inode *inode)
603{
604 set_bit(flag, &OVL_I(inode)->flags);
605}
606
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200607void ovl_clear_flag(unsigned long flag, struct inode *inode)
608{
609 clear_bit(flag, &OVL_I(inode)->flags);
610}
611
Miklos Szeredi13c72072017-07-04 22:03:16 +0200612bool ovl_test_flag(unsigned long flag, struct inode *inode)
613{
614 return test_bit(flag, &OVL_I(inode)->flags);
615}
Amir Goldsteinad0af712017-06-21 15:28:32 +0300616
617/**
618 * Caller must hold a reference to inode to prevent it from being freed while
619 * it is marked inuse.
620 */
621bool ovl_inuse_trylock(struct dentry *dentry)
622{
623 struct inode *inode = d_inode(dentry);
624 bool locked = false;
625
626 spin_lock(&inode->i_lock);
627 if (!(inode->i_state & I_OVL_INUSE)) {
628 inode->i_state |= I_OVL_INUSE;
629 locked = true;
630 }
631 spin_unlock(&inode->i_lock);
632
633 return locked;
634}
635
636void ovl_inuse_unlock(struct dentry *dentry)
637{
638 if (dentry) {
639 struct inode *inode = d_inode(dentry);
640
641 spin_lock(&inode->i_lock);
642 WARN_ON(!(inode->i_state & I_OVL_INUSE));
643 inode->i_state &= ~I_OVL_INUSE;
644 spin_unlock(&inode->i_lock);
645 }
646}
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300647
Amir Goldstein146d62e2019-04-18 17:42:08 +0300648bool ovl_is_inuse(struct dentry *dentry)
649{
650 struct inode *inode = d_inode(dentry);
651 bool inuse;
652
653 spin_lock(&inode->i_lock);
654 inuse = (inode->i_state & I_OVL_INUSE);
655 spin_unlock(&inode->i_lock);
656
657 return inuse;
658}
659
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300660/*
661 * Does this overlay dentry need to be indexed on copy up?
662 */
663bool ovl_need_index(struct dentry *dentry)
664{
665 struct dentry *lower = ovl_dentry_lower(dentry);
666
667 if (!lower || !ovl_indexdir(dentry->d_sb))
668 return false;
669
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200670 /* Index all files for NFS export and consistency verification */
Amir Goldstein016b7202018-01-11 14:01:08 +0200671 if (ovl_index_all(dentry->d_sb))
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200672 return true;
673
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300674 /* Index only lower hardlinks on copy up */
675 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
676 return true;
677
678 return false;
679}
680
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300681/* Caller must hold OVL_I(inode)->lock */
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300682static void ovl_cleanup_index(struct dentry *dentry)
683{
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300684 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
685 struct inode *dir = indexdir->d_inode;
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300686 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
687 struct dentry *upperdentry = ovl_dentry_upper(dentry);
688 struct dentry *index = NULL;
689 struct inode *inode;
Amir Goldstein63e13252018-09-18 16:34:31 +0300690 struct qstr name = { };
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300691 int err;
692
693 err = ovl_get_index_name(lowerdentry, &name);
694 if (err)
695 goto fail;
696
697 inode = d_inode(upperdentry);
Amir Goldstein89a17552017-09-26 07:40:37 +0300698 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800699 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300700 upperdentry, inode->i_ino, inode->i_nlink);
701 /*
702 * We either have a bug with persistent union nlink or a lower
703 * hardlink was added while overlay is mounted. Adding a lower
704 * hardlink and then unlinking all overlay hardlinks would drop
705 * overlay nlink to zero before all upper inodes are unlinked.
706 * As a safety measure, when that situation is detected, set
707 * the overlay nlink to the index inode nlink minus one for the
708 * index entry itself.
709 */
710 set_nlink(d_inode(dentry), inode->i_nlink - 1);
711 ovl_set_nlink_upper(dentry);
712 goto out;
713 }
714
715 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300716 index = lookup_one_len(name.name, indexdir, name.len);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300717 err = PTR_ERR(index);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300718 if (IS_ERR(index)) {
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300719 index = NULL;
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300720 } else if (ovl_index_all(dentry->d_sb)) {
721 /* Whiteout orphan index to block future open by handle */
722 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
723 } else {
724 /* Cleanup orphan index entries */
725 err = ovl_cleanup(dir, index);
726 }
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300727
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300728 inode_unlock(dir);
729 if (err)
730 goto fail;
731
732out:
Amir Goldstein63e13252018-09-18 16:34:31 +0300733 kfree(name.name);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300734 dput(index);
735 return;
736
737fail:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800738 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300739 goto out;
740}
741
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300742/*
743 * Operations that change overlay inode and upper inode nlink need to be
744 * synchronized with copy up for persistent nlink accounting.
745 */
Amir Goldstein0e329922018-10-18 18:37:13 +0300746int ovl_nlink_start(struct dentry *dentry)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300747{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300748 struct inode *inode = d_inode(dentry);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300749 const struct cred *old_cred;
750 int err;
751
Amir Goldstein1e92e302018-10-18 18:37:14 +0300752 if (WARN_ON(!inode))
Amir Goldstein0e329922018-10-18 18:37:13 +0300753 return -ENOENT;
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300754
755 /*
756 * With inodes index is enabled, we store the union overlay nlink
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300757 * in an xattr on the index inode. When whiting out an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300758 * we need to decrement the overlay persistent nlink, but before the
759 * first copy up, we have no upper index inode to store the xattr.
760 *
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300761 * As a workaround, before whiteout/rename over an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300762 * copy up to create the upper index. Creating the upper index will
763 * initialize the overlay nlink, so it could be dropped if unlink
764 * or rename succeeds.
765 *
766 * TODO: implement metadata only index copy up when called with
767 * ovl_copy_up_flags(dentry, O_PATH).
768 */
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300769 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300770 err = ovl_copy_up(dentry);
771 if (err)
772 return err;
773 }
774
Amir Goldstein1e92e302018-10-18 18:37:14 +0300775 err = ovl_inode_lock(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300776 if (err)
777 return err;
778
Amir Goldstein1e92e302018-10-18 18:37:14 +0300779 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300780 goto out;
781
782 old_cred = ovl_override_creds(dentry->d_sb);
783 /*
784 * The overlay inode nlink should be incremented/decremented IFF the
785 * upper operation succeeds, along with nlink change of upper inode.
786 * Therefore, before link/unlink/rename, we store the union nlink
787 * value relative to the upper inode nlink in an upper inode xattr.
788 */
789 err = ovl_set_nlink_upper(dentry);
Mark Salyzyn6120a4d2018-06-14 11:15:22 -0700790 ovl_revert_creds(dentry->d_sb, old_cred);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300791
792out:
793 if (err)
Amir Goldstein1e92e302018-10-18 18:37:14 +0300794 ovl_inode_unlock(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300795
796 return err;
797}
798
Amir Goldstein0e329922018-10-18 18:37:13 +0300799void ovl_nlink_end(struct dentry *dentry)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300800{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300801 struct inode *inode = d_inode(dentry);
802
803 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
Amir Goldstein0e329922018-10-18 18:37:13 +0300804 const struct cred *old_cred;
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300805
Amir Goldstein0e329922018-10-18 18:37:13 +0300806 old_cred = ovl_override_creds(dentry->d_sb);
807 ovl_cleanup_index(dentry);
Mark Salyzyn6120a4d2018-06-14 11:15:22 -0700808 ovl_revert_creds(dentry->d_sb, old_cred);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300809 }
Amir Goldstein0e329922018-10-18 18:37:13 +0300810
Amir Goldstein1e92e302018-10-18 18:37:14 +0300811 ovl_inode_unlock(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300812}
Amir Goldstein5820dc02017-09-25 16:39:55 +0300813
814int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
815{
816 /* Workdir should not be the same as upperdir */
817 if (workdir == upperdir)
818 goto err;
819
820 /* Workdir should not be subdir of upperdir and vice versa */
821 if (lock_rename(workdir, upperdir) != NULL)
822 goto err_unlock;
823
824 return 0;
825
826err_unlock:
827 unlock_rename(workdir, upperdir);
828err:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800829 pr_err("failed to lock workdir+upperdir\n");
Amir Goldstein5820dc02017-09-25 16:39:55 +0300830 return -EIO;
831}
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400832
833/* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
834int ovl_check_metacopy_xattr(struct dentry *dentry)
835{
Greg Kroah-Hartman791d8a92020-02-08 14:23:00 +0100836 ssize_t res;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400837
838 /* Only regular files can have metacopy xattr */
839 if (!S_ISREG(d_inode(dentry)->i_mode))
840 return 0;
841
Greg Kroah-Hartman791d8a92020-02-08 14:23:00 +0100842 res = ovl_do_vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400843 if (res < 0) {
844 if (res == -ENODATA || res == -EOPNOTSUPP)
845 return 0;
846 goto out;
847 }
848
849 return 1;
850out:
Greg Kroah-Hartman791d8a92020-02-08 14:23:00 +0100851 pr_warn_ratelimited("failed to get metacopy (%zi)\n", res);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400852 return res;
853}
Vivek Goyal67d756c22018-05-11 11:49:30 -0400854
855bool ovl_is_metacopy_dentry(struct dentry *dentry)
856{
857 struct ovl_entry *oe = dentry->d_fsdata;
858
859 if (!d_is_reg(dentry))
860 return false;
861
862 if (ovl_dentry_upper(dentry)) {
863 if (!ovl_has_upperdata(d_inode(dentry)))
864 return true;
865 return false;
866 }
867
868 return (oe->numlower > 1);
869}
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400870
Vivek Goyal993a0b22019-01-30 14:01:57 -0500871ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value,
872 size_t padding)
873{
874 ssize_t res;
875 char *buf = NULL;
876
Greg Kroah-Hartman791d8a92020-02-08 14:23:00 +0100877 res = ovl_do_vfs_getxattr(dentry, name, NULL, 0);
Vivek Goyal993a0b22019-01-30 14:01:57 -0500878 if (res < 0) {
879 if (res == -ENODATA || res == -EOPNOTSUPP)
880 return -ENODATA;
881 goto fail;
882 }
883
884 if (res != 0) {
885 buf = kzalloc(res + padding, GFP_KERNEL);
886 if (!buf)
887 return -ENOMEM;
888
Greg Kroah-Hartman791d8a92020-02-08 14:23:00 +0100889 res = ovl_do_vfs_getxattr(dentry, name, buf, res);
Vivek Goyal993a0b22019-01-30 14:01:57 -0500890 if (res < 0)
891 goto fail;
892 }
893 *value = buf;
894
895 return res;
896
897fail:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800898 pr_warn_ratelimited("failed to get xattr %s: err=%zi)\n",
Vivek Goyal993a0b22019-01-30 14:01:57 -0500899 name, res);
900 kfree(buf);
901 return res;
902}
903
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400904char *ovl_get_redirect_xattr(struct dentry *dentry, int padding)
905{
906 int res;
907 char *s, *next, *buf = NULL;
908
Vivek Goyal993a0b22019-01-30 14:01:57 -0500909 res = ovl_getxattr(dentry, OVL_XATTR_REDIRECT, &buf, padding + 1);
910 if (res == -ENODATA)
911 return NULL;
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400912 if (res < 0)
Vivek Goyal993a0b22019-01-30 14:01:57 -0500913 return ERR_PTR(res);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400914 if (res == 0)
915 goto invalid;
916
917 if (buf[0] == '/') {
918 for (s = buf; *s++ == '/'; s = next) {
919 next = strchrnul(s, '/');
920 if (s == next)
921 goto invalid;
922 }
923 } else {
924 if (strchr(buf, '/') != NULL)
925 goto invalid;
926 }
927
928 return buf;
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400929invalid:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800930 pr_warn_ratelimited("invalid redirect (%s)\n", buf);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400931 res = -EINVAL;
Vivek Goyal993a0b22019-01-30 14:01:57 -0500932 kfree(buf);
933 return ERR_PTR(res);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400934}