blob: ea005085803f0c5a6eb54ae19f8c6b19d2089c38 [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
40 return override_creds(ofs->creator_cred);
41}
42
Amir Goldsteine487d882017-11-07 13:55:04 +020043/*
44 * Check if underlying fs supports file handles and try to determine encoding
45 * type, in order to deduce maximum inode number used by fs.
46 *
47 * Return 0 if file handles are not supported.
48 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
49 * Return -1 if fs uses a non default encoding with unknown inode size.
50 */
51int ovl_can_decode_fh(struct super_block *sb)
Amir Goldstein02bcd152017-06-21 15:28:36 +030052{
Amir Goldstein9df085f2018-09-03 09:12:09 +030053 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
Amir Goldsteine487d882017-11-07 13:55:04 +020054 return 0;
55
56 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
Amir Goldstein02bcd152017-06-21 15:28:36 +030057}
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) {
Vivek Goyal60124872018-05-11 11:49:32 -0400120 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
121 type |= __OVL_PATH_ORIGIN;
Vivek Goyal0b17c282018-05-11 11:49:32 -0400122 if (d_is_dir(dentry) ||
123 !ovl_has_upperdata(d_inode(dentry)))
Amir Goldstein59548502017-04-23 23:12:34 +0300124 type |= __OVL_PATH_MERGE;
125 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100126 } else {
127 if (oe->numlower > 1)
128 type |= __OVL_PATH_MERGE;
129 }
130 return type;
131}
132
133void ovl_path_upper(struct dentry *dentry, struct path *path)
134{
135 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100136
137 path->mnt = ofs->upper_mnt;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200138 path->dentry = ovl_dentry_upper(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100139}
140
141void ovl_path_lower(struct dentry *dentry, struct path *path)
142{
143 struct ovl_entry *oe = dentry->d_fsdata;
144
Chandan Rajendrab9343632017-07-24 01:57:54 -0500145 if (oe->numlower) {
146 path->mnt = oe->lowerstack[0].layer->mnt;
147 path->dentry = oe->lowerstack[0].dentry;
148 } else {
149 *path = (struct path) { };
150 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100151}
152
Vivek Goyal4f93b422018-05-11 11:49:30 -0400153void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
154{
155 struct ovl_entry *oe = dentry->d_fsdata;
156
157 if (oe->numlower) {
158 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
159 path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
160 } else {
161 *path = (struct path) { };
162 }
163}
164
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100165enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
166{
167 enum ovl_path_type type = ovl_path_type(dentry);
168
169 if (!OVL_TYPE_UPPER(type))
170 ovl_path_lower(dentry, path);
171 else
172 ovl_path_upper(dentry, path);
173
174 return type;
175}
176
177struct dentry *ovl_dentry_upper(struct dentry *dentry)
178{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200179 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100180}
181
182struct dentry *ovl_dentry_lower(struct dentry *dentry)
183{
184 struct ovl_entry *oe = dentry->d_fsdata;
185
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200186 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100187}
188
Miklos Szeredi13464162020-01-24 09:46:45 +0100189const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
Amir Goldsteinda309e82017-11-08 19:39:51 +0200190{
191 struct ovl_entry *oe = dentry->d_fsdata;
192
193 return oe->numlower ? oe->lowerstack[0].layer : NULL;
194}
195
Vivek Goyal647d2532018-05-11 11:49:30 -0400196/*
197 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
198 * dependig on what is stored in lowerstack[0]. At times we need to find
199 * lower dentry which has data (and not metacopy dentry). This helper
200 * returns the lower data dentry.
201 */
202struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
203{
204 struct ovl_entry *oe = dentry->d_fsdata;
205
206 return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
207}
208
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100209struct dentry *ovl_dentry_real(struct dentry *dentry)
210{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200211 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100212}
213
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200214struct dentry *ovl_i_dentry_upper(struct inode *inode)
215{
216 return ovl_upperdentry_dereference(OVL_I(inode));
217}
218
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200219struct inode *ovl_inode_upper(struct inode *inode)
Miklos Szeredi25b77132017-07-04 22:03:16 +0200220{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200221 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200222
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200223 return upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200224}
225
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200226struct inode *ovl_inode_lower(struct inode *inode)
227{
228 return OVL_I(inode)->lower;
229}
230
231struct inode *ovl_inode_real(struct inode *inode)
232{
233 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
234}
235
Vivek Goyal2664bd02018-05-11 11:49:30 -0400236/* Return inode which contains lower data. Do not return metacopy */
237struct inode *ovl_inode_lowerdata(struct inode *inode)
238{
239 if (WARN_ON(!S_ISREG(inode->i_mode)))
240 return NULL;
241
242 return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
243}
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200244
Vivek Goyal4823d492018-05-11 11:49:31 -0400245/* Return real inode which contains data. Does not return metacopy inode */
246struct inode *ovl_inode_realdata(struct inode *inode)
247{
248 struct inode *upperinode;
249
250 upperinode = ovl_inode_upper(inode);
251 if (upperinode && ovl_has_upperdata(inode))
252 return upperinode;
253
254 return ovl_inode_lowerdata(inode);
255}
256
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200257struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100258{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200259 return OVL_I(inode)->cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100260}
261
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200262void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100263{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200264 OVL_I(inode)->cache = cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100265}
266
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200267void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
268{
269 set_bit(flag, &OVL_E(dentry)->flags);
270}
271
272void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
273{
274 clear_bit(flag, &OVL_E(dentry)->flags);
275}
276
277bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
278{
279 return test_bit(flag, &OVL_E(dentry)->flags);
280}
281
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100282bool ovl_dentry_is_opaque(struct dentry *dentry)
283{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200284 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100285}
286
287bool ovl_dentry_is_whiteout(struct dentry *dentry)
288{
289 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
290}
291
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100292void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100293{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200294 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100295}
296
Miklos Szeredi55acc662017-07-04 22:03:18 +0200297/*
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300298 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
299 * to return positive, while there's no actual upper alias for the inode.
300 * Copy up code needs to know about the existence of the upper alias, so it
301 * can't use ovl_dentry_upper().
Miklos Szeredi55acc662017-07-04 22:03:18 +0200302 */
303bool ovl_dentry_has_upper_alias(struct dentry *dentry)
304{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200305 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200306}
307
308void ovl_dentry_set_upper_alias(struct dentry *dentry)
309{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200310 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200311}
312
Vivek Goyal0c288872018-05-11 11:49:28 -0400313static bool ovl_should_check_upperdata(struct inode *inode)
314{
315 if (!S_ISREG(inode->i_mode))
316 return false;
317
318 if (!ovl_inode_lower(inode))
319 return false;
320
321 return true;
322}
323
324bool ovl_has_upperdata(struct inode *inode)
325{
326 if (!ovl_should_check_upperdata(inode))
327 return true;
328
329 if (!ovl_test_flag(OVL_UPPERDATA, inode))
330 return false;
331 /*
332 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
333 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
334 * if setting of OVL_UPPERDATA is visible, then effects of writes
335 * before that are visible too.
336 */
337 smp_rmb();
338 return true;
339}
340
341void ovl_set_upperdata(struct inode *inode)
342{
343 /*
344 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
345 * if OVL_UPPERDATA flag is visible, then effects of write operations
346 * before it are visible as well.
347 */
348 smp_wmb();
349 ovl_set_flag(OVL_UPPERDATA, inode);
350}
351
352/* Caller should hold ovl_inode->lock */
353bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
354{
355 if (!ovl_open_flags_need_copy_up(flags))
356 return false;
357
358 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
359}
360
361bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
362{
363 if (!ovl_open_flags_need_copy_up(flags))
364 return false;
365
366 return !ovl_has_upperdata(d_inode(dentry));
367}
368
Miklos Szeredia6c60652016-12-16 11:02:56 +0100369bool ovl_redirect_dir(struct super_block *sb)
370{
371 struct ovl_fs *ofs = sb->s_fs_info;
372
Amir Goldstein21a22872017-05-17 00:12:41 +0300373 return ofs->config.redirect_dir && !ofs->noxattr;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100374}
375
376const char *ovl_dentry_get_redirect(struct dentry *dentry)
377{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200378 return OVL_I(d_inode(dentry))->redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100379}
380
381void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
382{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200383 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Miklos Szeredia6c60652016-12-16 11:02:56 +0100384
Miklos Szeredicf31c462017-07-04 22:03:16 +0200385 kfree(oi->redirect);
386 oi->redirect = redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100387}
388
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200389void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
Vivek Goyal2664bd02018-05-11 11:49:30 -0400390 struct dentry *lowerdentry, struct dentry *lowerdata)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100391{
Amir Goldstein695b46e2018-03-15 23:39:01 +0200392 struct inode *realinode = d_inode(upperdentry ?: lowerdentry);
393
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200394 if (upperdentry)
395 OVL_I(inode)->__upperdentry = upperdentry;
396 if (lowerdentry)
Amir Goldstein31747ed2018-01-14 18:35:40 +0200397 OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
Vivek Goyal2664bd02018-05-11 11:49:30 -0400398 if (lowerdata)
399 OVL_I(inode)->lowerdata = igrab(d_inode(lowerdata));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100400
Amir Goldstein695b46e2018-03-15 23:39:01 +0200401 ovl_copyattr(realinode, inode);
Miklos Szeredi4f357292018-07-18 15:44:41 +0200402 ovl_copyflags(realinode, inode);
Amir Goldstein695b46e2018-03-15 23:39:01 +0200403 if (!inode->i_ino)
404 inode->i_ino = realinode->i_ino;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100405}
406
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200407void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100408{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200409 struct inode *upperinode = d_inode(upperdentry);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200410
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200411 WARN_ON(OVL_I(inode)->__upperdentry);
412
Miklos Szeredi25b77132017-07-04 22:03:16 +0200413 /*
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200414 * Make sure upperdentry is consistent before making it visible
Miklos Szeredi25b77132017-07-04 22:03:16 +0200415 */
416 smp_wmb();
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200417 OVL_I(inode)->__upperdentry = upperdentry;
Amir Goldstein31747ed2018-01-14 18:35:40 +0200418 if (inode_unhashed(inode)) {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200419 if (!inode->i_ino)
420 inode->i_ino = upperinode->i_ino;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200421 inode->i_private = upperinode;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100422 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200423 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100424}
425
Miklos Szeredid9854c82018-07-18 15:44:40 +0200426static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100427{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200428 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100429
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200430 WARN_ON(!inode_is_locked(inode));
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200431 /*
432 * Version is used by readdir code to keep cache consistent. For merge
433 * dirs all changes need to be noted. For non-merge dirs, cache only
434 * contains impure (ones which have been copied up and have origins)
435 * entries, so only need to note changes to impure entries.
436 */
437 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
438 OVL_I(inode)->version++;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100439}
440
Miklos Szeredid9854c82018-07-18 15:44:40 +0200441void ovl_dir_modified(struct dentry *dentry, bool impurity)
442{
443 /* Copy mtime/ctime */
444 ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
445
446 ovl_dentry_version_inc(dentry, impurity);
447}
448
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100449u64 ovl_dentry_version_get(struct dentry *dentry)
450{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200451 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100452
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200453 WARN_ON(!inode_is_locked(inode));
454 return OVL_I(inode)->version;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100455}
456
457bool ovl_is_whiteout(struct dentry *dentry)
458{
459 struct inode *inode = dentry->d_inode;
460
461 return inode && IS_WHITEOUT(inode);
462}
463
464struct file *ovl_path_open(struct path *path, int flags)
465{
466 return dentry_open(path, flags | O_NOATIME, current_cred());
467}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200468
Vivek Goyal0c288872018-05-11 11:49:28 -0400469/* Caller should hold ovl_inode->lock */
470static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
471{
472 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
473
474 if (ovl_dentry_upper(dentry) &&
475 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
476 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
477 return true;
478
479 return false;
480}
481
482bool ovl_already_copied_up(struct dentry *dentry, int flags)
Vivek Goyal2002df82018-05-11 11:49:28 -0400483{
484 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
485
486 /*
487 * Check if copy-up has happened as well as for upper alias (in
488 * case of hard links) is there.
489 *
490 * Both checks are lockless:
491 * - false negatives: will recheck under oi->lock
492 * - false positives:
493 * + ovl_dentry_upper() uses memory barriers to ensure the
494 * upper dentry is up-to-date
495 * + ovl_dentry_has_upper_alias() relies on locking of
496 * upper parent i_rwsem to prevent reordering copy-up
497 * with rename.
498 */
499 if (ovl_dentry_upper(dentry) &&
Vivek Goyal0c288872018-05-11 11:49:28 -0400500 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
501 !ovl_dentry_needs_data_copy_up(dentry, flags))
Vivek Goyal2002df82018-05-11 11:49:28 -0400502 return true;
503
504 return false;
505}
506
Vivek Goyal0c288872018-05-11 11:49:28 -0400507int ovl_copy_up_start(struct dentry *dentry, int flags)
Amir Goldstein39d3d602017-01-17 06:34:56 +0200508{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300509 struct inode *inode = d_inode(dentry);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200510 int err;
511
Amir Goldstein1e92e302018-10-18 18:37:14 +0300512 err = ovl_inode_lock(inode);
Vivek Goyal0c288872018-05-11 11:49:28 -0400513 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
Amir Goldsteina015daf2017-06-21 15:28:51 +0300514 err = 1; /* Already copied up */
Amir Goldstein1e92e302018-10-18 18:37:14 +0300515 ovl_inode_unlock(inode);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200516 }
Amir Goldstein39d3d602017-01-17 06:34:56 +0200517
518 return err;
519}
520
521void ovl_copy_up_end(struct dentry *dentry)
522{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300523 ovl_inode_unlock(d_inode(dentry));
Amir Goldstein39d3d602017-01-17 06:34:56 +0200524}
Amir Goldstein82b749b2017-05-17 00:12:40 +0300525
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300526bool ovl_check_origin_xattr(struct dentry *dentry)
527{
528 int res;
529
530 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
531
532 /* Zero size value means "copied up but origin unknown" */
533 if (res >= 0)
534 return true;
535
536 return false;
537}
538
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300539bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
540{
541 int res;
542 char val;
543
544 if (!d_is_dir(dentry))
545 return false;
546
547 res = vfs_getxattr(dentry, name, &val, 1);
548 if (res == 1 && val == 'y')
549 return true;
550
551 return false;
552}
553
Amir Goldstein82b749b2017-05-17 00:12:40 +0300554int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
555 const char *name, const void *value, size_t size,
556 int xerr)
557{
558 int err;
559 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
560
561 if (ofs->noxattr)
562 return xerr;
563
564 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
565
566 if (err == -EOPNOTSUPP) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800567 pr_warn("cannot set %s xattr on upper\n", name);
Amir Goldstein82b749b2017-05-17 00:12:40 +0300568 ofs->noxattr = true;
569 return xerr;
570 }
571
572 return err;
573}
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300574
575int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
576{
577 int err;
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300578
Miklos Szeredi13c72072017-07-04 22:03:16 +0200579 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300580 return 0;
581
582 /*
583 * Do not fail when upper doesn't support xattrs.
584 * Upper inodes won't have origin nor redirect xattr anyway.
585 */
586 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
587 "y", 1, 0);
588 if (!err)
Miklos Szeredi13c72072017-07-04 22:03:16 +0200589 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300590
591 return err;
592}
Miklos Szeredi13c72072017-07-04 22:03:16 +0200593
594void ovl_set_flag(unsigned long flag, struct inode *inode)
595{
596 set_bit(flag, &OVL_I(inode)->flags);
597}
598
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200599void ovl_clear_flag(unsigned long flag, struct inode *inode)
600{
601 clear_bit(flag, &OVL_I(inode)->flags);
602}
603
Miklos Szeredi13c72072017-07-04 22:03:16 +0200604bool ovl_test_flag(unsigned long flag, struct inode *inode)
605{
606 return test_bit(flag, &OVL_I(inode)->flags);
607}
Amir Goldsteinad0af712017-06-21 15:28:32 +0300608
609/**
610 * Caller must hold a reference to inode to prevent it from being freed while
611 * it is marked inuse.
612 */
613bool ovl_inuse_trylock(struct dentry *dentry)
614{
615 struct inode *inode = d_inode(dentry);
616 bool locked = false;
617
618 spin_lock(&inode->i_lock);
619 if (!(inode->i_state & I_OVL_INUSE)) {
620 inode->i_state |= I_OVL_INUSE;
621 locked = true;
622 }
623 spin_unlock(&inode->i_lock);
624
625 return locked;
626}
627
628void ovl_inuse_unlock(struct dentry *dentry)
629{
630 if (dentry) {
631 struct inode *inode = d_inode(dentry);
632
633 spin_lock(&inode->i_lock);
634 WARN_ON(!(inode->i_state & I_OVL_INUSE));
635 inode->i_state &= ~I_OVL_INUSE;
636 spin_unlock(&inode->i_lock);
637 }
638}
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300639
Amir Goldstein146d62e2019-04-18 17:42:08 +0300640bool ovl_is_inuse(struct dentry *dentry)
641{
642 struct inode *inode = d_inode(dentry);
643 bool inuse;
644
645 spin_lock(&inode->i_lock);
646 inuse = (inode->i_state & I_OVL_INUSE);
647 spin_unlock(&inode->i_lock);
648
649 return inuse;
650}
651
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300652/*
653 * Does this overlay dentry need to be indexed on copy up?
654 */
655bool ovl_need_index(struct dentry *dentry)
656{
657 struct dentry *lower = ovl_dentry_lower(dentry);
658
659 if (!lower || !ovl_indexdir(dentry->d_sb))
660 return false;
661
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200662 /* Index all files for NFS export and consistency verification */
Amir Goldstein016b7202018-01-11 14:01:08 +0200663 if (ovl_index_all(dentry->d_sb))
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200664 return true;
665
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300666 /* Index only lower hardlinks on copy up */
667 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
668 return true;
669
670 return false;
671}
672
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300673/* Caller must hold OVL_I(inode)->lock */
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300674static void ovl_cleanup_index(struct dentry *dentry)
675{
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300676 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
677 struct inode *dir = indexdir->d_inode;
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300678 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
679 struct dentry *upperdentry = ovl_dentry_upper(dentry);
680 struct dentry *index = NULL;
681 struct inode *inode;
Amir Goldstein63e13252018-09-18 16:34:31 +0300682 struct qstr name = { };
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300683 int err;
684
685 err = ovl_get_index_name(lowerdentry, &name);
686 if (err)
687 goto fail;
688
689 inode = d_inode(upperdentry);
Amir Goldstein89a17552017-09-26 07:40:37 +0300690 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800691 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300692 upperdentry, inode->i_ino, inode->i_nlink);
693 /*
694 * We either have a bug with persistent union nlink or a lower
695 * hardlink was added while overlay is mounted. Adding a lower
696 * hardlink and then unlinking all overlay hardlinks would drop
697 * overlay nlink to zero before all upper inodes are unlinked.
698 * As a safety measure, when that situation is detected, set
699 * the overlay nlink to the index inode nlink minus one for the
700 * index entry itself.
701 */
702 set_nlink(d_inode(dentry), inode->i_nlink - 1);
703 ovl_set_nlink_upper(dentry);
704 goto out;
705 }
706
707 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300708 index = lookup_one_len(name.name, indexdir, name.len);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300709 err = PTR_ERR(index);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300710 if (IS_ERR(index)) {
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300711 index = NULL;
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300712 } else if (ovl_index_all(dentry->d_sb)) {
713 /* Whiteout orphan index to block future open by handle */
714 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
715 } else {
716 /* Cleanup orphan index entries */
717 err = ovl_cleanup(dir, index);
718 }
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300719
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300720 inode_unlock(dir);
721 if (err)
722 goto fail;
723
724out:
Amir Goldstein63e13252018-09-18 16:34:31 +0300725 kfree(name.name);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300726 dput(index);
727 return;
728
729fail:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800730 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300731 goto out;
732}
733
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300734/*
735 * Operations that change overlay inode and upper inode nlink need to be
736 * synchronized with copy up for persistent nlink accounting.
737 */
Amir Goldstein0e329922018-10-18 18:37:13 +0300738int ovl_nlink_start(struct dentry *dentry)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300739{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300740 struct inode *inode = d_inode(dentry);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300741 const struct cred *old_cred;
742 int err;
743
Amir Goldstein1e92e302018-10-18 18:37:14 +0300744 if (WARN_ON(!inode))
Amir Goldstein0e329922018-10-18 18:37:13 +0300745 return -ENOENT;
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300746
747 /*
748 * With inodes index is enabled, we store the union overlay nlink
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300749 * in an xattr on the index inode. When whiting out an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300750 * we need to decrement the overlay persistent nlink, but before the
751 * first copy up, we have no upper index inode to store the xattr.
752 *
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300753 * As a workaround, before whiteout/rename over an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300754 * copy up to create the upper index. Creating the upper index will
755 * initialize the overlay nlink, so it could be dropped if unlink
756 * or rename succeeds.
757 *
758 * TODO: implement metadata only index copy up when called with
759 * ovl_copy_up_flags(dentry, O_PATH).
760 */
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300761 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300762 err = ovl_copy_up(dentry);
763 if (err)
764 return err;
765 }
766
Amir Goldstein1e92e302018-10-18 18:37:14 +0300767 err = ovl_inode_lock(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300768 if (err)
769 return err;
770
Amir Goldstein1e92e302018-10-18 18:37:14 +0300771 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300772 goto out;
773
774 old_cred = ovl_override_creds(dentry->d_sb);
775 /*
776 * The overlay inode nlink should be incremented/decremented IFF the
777 * upper operation succeeds, along with nlink change of upper inode.
778 * Therefore, before link/unlink/rename, we store the union nlink
779 * value relative to the upper inode nlink in an upper inode xattr.
780 */
781 err = ovl_set_nlink_upper(dentry);
782 revert_creds(old_cred);
783
784out:
785 if (err)
Amir Goldstein1e92e302018-10-18 18:37:14 +0300786 ovl_inode_unlock(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300787
788 return err;
789}
790
Amir Goldstein0e329922018-10-18 18:37:13 +0300791void ovl_nlink_end(struct dentry *dentry)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300792{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300793 struct inode *inode = d_inode(dentry);
794
795 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
Amir Goldstein0e329922018-10-18 18:37:13 +0300796 const struct cred *old_cred;
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300797
Amir Goldstein0e329922018-10-18 18:37:13 +0300798 old_cred = ovl_override_creds(dentry->d_sb);
799 ovl_cleanup_index(dentry);
800 revert_creds(old_cred);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300801 }
Amir Goldstein0e329922018-10-18 18:37:13 +0300802
Amir Goldstein1e92e302018-10-18 18:37:14 +0300803 ovl_inode_unlock(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300804}
Amir Goldstein5820dc02017-09-25 16:39:55 +0300805
806int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
807{
808 /* Workdir should not be the same as upperdir */
809 if (workdir == upperdir)
810 goto err;
811
812 /* Workdir should not be subdir of upperdir and vice versa */
813 if (lock_rename(workdir, upperdir) != NULL)
814 goto err_unlock;
815
816 return 0;
817
818err_unlock:
819 unlock_rename(workdir, upperdir);
820err:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800821 pr_err("failed to lock workdir+upperdir\n");
Amir Goldstein5820dc02017-09-25 16:39:55 +0300822 return -EIO;
823}
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400824
825/* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
826int ovl_check_metacopy_xattr(struct dentry *dentry)
827{
828 int res;
829
830 /* Only regular files can have metacopy xattr */
831 if (!S_ISREG(d_inode(dentry)->i_mode))
832 return 0;
833
834 res = vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0);
835 if (res < 0) {
836 if (res == -ENODATA || res == -EOPNOTSUPP)
837 return 0;
838 goto out;
839 }
840
841 return 1;
842out:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800843 pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400844 return res;
845}
Vivek Goyal67d756c22018-05-11 11:49:30 -0400846
847bool ovl_is_metacopy_dentry(struct dentry *dentry)
848{
849 struct ovl_entry *oe = dentry->d_fsdata;
850
851 if (!d_is_reg(dentry))
852 return false;
853
854 if (ovl_dentry_upper(dentry)) {
855 if (!ovl_has_upperdata(d_inode(dentry)))
856 return true;
857 return false;
858 }
859
860 return (oe->numlower > 1);
861}
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400862
Vivek Goyal993a0b22019-01-30 14:01:57 -0500863ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value,
864 size_t padding)
865{
866 ssize_t res;
867 char *buf = NULL;
868
869 res = vfs_getxattr(dentry, name, NULL, 0);
870 if (res < 0) {
871 if (res == -ENODATA || res == -EOPNOTSUPP)
872 return -ENODATA;
873 goto fail;
874 }
875
876 if (res != 0) {
877 buf = kzalloc(res + padding, GFP_KERNEL);
878 if (!buf)
879 return -ENOMEM;
880
881 res = vfs_getxattr(dentry, name, buf, res);
882 if (res < 0)
883 goto fail;
884 }
885 *value = buf;
886
887 return res;
888
889fail:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800890 pr_warn_ratelimited("failed to get xattr %s: err=%zi)\n",
Vivek Goyal993a0b22019-01-30 14:01:57 -0500891 name, res);
892 kfree(buf);
893 return res;
894}
895
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400896char *ovl_get_redirect_xattr(struct dentry *dentry, int padding)
897{
898 int res;
899 char *s, *next, *buf = NULL;
900
Vivek Goyal993a0b22019-01-30 14:01:57 -0500901 res = ovl_getxattr(dentry, OVL_XATTR_REDIRECT, &buf, padding + 1);
902 if (res == -ENODATA)
903 return NULL;
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400904 if (res < 0)
Vivek Goyal993a0b22019-01-30 14:01:57 -0500905 return ERR_PTR(res);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400906 if (res == 0)
907 goto invalid;
908
909 if (buf[0] == '/') {
910 for (s = buf; *s++ == '/'; s = next) {
911 next = strchrnul(s, '/');
912 if (s == next)
913 goto invalid;
914 }
915 } else {
916 if (strchr(buf, '/') != NULL)
917 goto invalid;
918 }
919
920 return buf;
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400921invalid:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800922 pr_warn_ratelimited("invalid redirect (%s)\n", buf);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400923 res = -EINVAL;
Vivek Goyal993a0b22019-01-30 14:01:57 -0500924 kfree(buf);
925 return ERR_PTR(res);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400926}