blob: 36b60788ee473f3f4c25df875f81d6efa1d04f59 [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 &
Miklos Szeredi7925dad2020-03-17 15:04:22 +010096 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
Miklos Szeredibbb1e542016-12-16 11:02:56 +010097}
98
Miklos Szeredif4288842020-03-17 15:04:22 +010099void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
100 unsigned int mask)
101{
102 struct ovl_entry *oe = OVL_E(dentry);
103 unsigned int i, flags = 0;
104
Miklos Szeredibccece12020-03-17 15:04:22 +0100105 if (upperdentry)
106 flags |= upperdentry->d_flags;
Miklos Szeredif4288842020-03-17 15:04:22 +0100107 for (i = 0; i < oe->numlower; i++)
108 flags |= oe->lowerstack[i].dentry->d_flags;
109
110 spin_lock(&dentry->d_lock);
111 dentry->d_flags &= ~mask;
112 dentry->d_flags |= flags & mask;
113 spin_unlock(&dentry->d_lock);
114}
115
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100116bool 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) {
Vivek Goyal60124872018-05-11 11:49:32 -0400136 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
137 type |= __OVL_PATH_ORIGIN;
Vivek Goyal0b17c282018-05-11 11:49:32 -0400138 if (d_is_dir(dentry) ||
139 !ovl_has_upperdata(d_inode(dentry)))
Amir Goldstein59548502017-04-23 23:12:34 +0300140 type |= __OVL_PATH_MERGE;
141 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100142 } else {
143 if (oe->numlower > 1)
144 type |= __OVL_PATH_MERGE;
145 }
146 return type;
147}
148
149void ovl_path_upper(struct dentry *dentry, struct path *path)
150{
151 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100152
153 path->mnt = ofs->upper_mnt;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200154 path->dentry = ovl_dentry_upper(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100155}
156
157void ovl_path_lower(struct dentry *dentry, struct path *path)
158{
159 struct ovl_entry *oe = dentry->d_fsdata;
160
Chandan Rajendrab9343632017-07-24 01:57:54 -0500161 if (oe->numlower) {
162 path->mnt = oe->lowerstack[0].layer->mnt;
163 path->dentry = oe->lowerstack[0].dentry;
164 } else {
165 *path = (struct path) { };
166 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100167}
168
Vivek Goyal4f93b422018-05-11 11:49:30 -0400169void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
170{
171 struct ovl_entry *oe = dentry->d_fsdata;
172
173 if (oe->numlower) {
174 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
175 path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
176 } else {
177 *path = (struct path) { };
178 }
179}
180
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100181enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
182{
183 enum ovl_path_type type = ovl_path_type(dentry);
184
185 if (!OVL_TYPE_UPPER(type))
186 ovl_path_lower(dentry, path);
187 else
188 ovl_path_upper(dentry, path);
189
190 return type;
191}
192
193struct dentry *ovl_dentry_upper(struct dentry *dentry)
194{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200195 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100196}
197
198struct dentry *ovl_dentry_lower(struct dentry *dentry)
199{
200 struct ovl_entry *oe = dentry->d_fsdata;
201
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200202 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100203}
204
Miklos Szeredi13464162020-01-24 09:46:45 +0100205const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
Amir Goldsteinda309e82017-11-08 19:39:51 +0200206{
207 struct ovl_entry *oe = dentry->d_fsdata;
208
209 return oe->numlower ? oe->lowerstack[0].layer : NULL;
210}
211
Vivek Goyal647d2532018-05-11 11:49:30 -0400212/*
213 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
214 * dependig on what is stored in lowerstack[0]. At times we need to find
215 * lower dentry which has data (and not metacopy dentry). This helper
216 * returns the lower data dentry.
217 */
218struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
219{
220 struct ovl_entry *oe = dentry->d_fsdata;
221
222 return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
223}
224
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100225struct dentry *ovl_dentry_real(struct dentry *dentry)
226{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200227 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100228}
229
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200230struct dentry *ovl_i_dentry_upper(struct inode *inode)
231{
232 return ovl_upperdentry_dereference(OVL_I(inode));
233}
234
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200235struct inode *ovl_inode_upper(struct inode *inode)
Miklos Szeredi25b77132017-07-04 22:03:16 +0200236{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200237 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200238
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200239 return upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200240}
241
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200242struct inode *ovl_inode_lower(struct inode *inode)
243{
244 return OVL_I(inode)->lower;
245}
246
247struct inode *ovl_inode_real(struct inode *inode)
248{
249 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
250}
251
Vivek Goyal2664bd02018-05-11 11:49:30 -0400252/* Return inode which contains lower data. Do not return metacopy */
253struct inode *ovl_inode_lowerdata(struct inode *inode)
254{
255 if (WARN_ON(!S_ISREG(inode->i_mode)))
256 return NULL;
257
258 return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
259}
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200260
Vivek Goyal4823d492018-05-11 11:49:31 -0400261/* Return real inode which contains data. Does not return metacopy inode */
262struct inode *ovl_inode_realdata(struct inode *inode)
263{
264 struct inode *upperinode;
265
266 upperinode = ovl_inode_upper(inode);
267 if (upperinode && ovl_has_upperdata(inode))
268 return upperinode;
269
270 return ovl_inode_lowerdata(inode);
271}
272
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200273struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100274{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200275 return OVL_I(inode)->cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100276}
277
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200278void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100279{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200280 OVL_I(inode)->cache = cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100281}
282
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200283void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
284{
285 set_bit(flag, &OVL_E(dentry)->flags);
286}
287
288void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
289{
290 clear_bit(flag, &OVL_E(dentry)->flags);
291}
292
293bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
294{
295 return test_bit(flag, &OVL_E(dentry)->flags);
296}
297
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100298bool ovl_dentry_is_opaque(struct dentry *dentry)
299{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200300 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100301}
302
303bool ovl_dentry_is_whiteout(struct dentry *dentry)
304{
305 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
306}
307
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100308void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100309{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200310 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100311}
312
Miklos Szeredi55acc662017-07-04 22:03:18 +0200313/*
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300314 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
315 * to return positive, while there's no actual upper alias for the inode.
316 * Copy up code needs to know about the existence of the upper alias, so it
317 * can't use ovl_dentry_upper().
Miklos Szeredi55acc662017-07-04 22:03:18 +0200318 */
319bool ovl_dentry_has_upper_alias(struct dentry *dentry)
320{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200321 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200322}
323
324void ovl_dentry_set_upper_alias(struct dentry *dentry)
325{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200326 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200327}
328
Vivek Goyal0c288872018-05-11 11:49:28 -0400329static bool ovl_should_check_upperdata(struct inode *inode)
330{
331 if (!S_ISREG(inode->i_mode))
332 return false;
333
334 if (!ovl_inode_lower(inode))
335 return false;
336
337 return true;
338}
339
340bool ovl_has_upperdata(struct inode *inode)
341{
342 if (!ovl_should_check_upperdata(inode))
343 return true;
344
345 if (!ovl_test_flag(OVL_UPPERDATA, inode))
346 return false;
347 /*
348 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
349 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
350 * if setting of OVL_UPPERDATA is visible, then effects of writes
351 * before that are visible too.
352 */
353 smp_rmb();
354 return true;
355}
356
357void ovl_set_upperdata(struct inode *inode)
358{
359 /*
360 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
361 * if OVL_UPPERDATA flag is visible, then effects of write operations
362 * before it are visible as well.
363 */
364 smp_wmb();
365 ovl_set_flag(OVL_UPPERDATA, inode);
366}
367
368/* Caller should hold ovl_inode->lock */
369bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
370{
371 if (!ovl_open_flags_need_copy_up(flags))
372 return false;
373
374 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
375}
376
377bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
378{
379 if (!ovl_open_flags_need_copy_up(flags))
380 return false;
381
382 return !ovl_has_upperdata(d_inode(dentry));
383}
384
Miklos Szeredia6c60652016-12-16 11:02:56 +0100385bool ovl_redirect_dir(struct super_block *sb)
386{
387 struct ovl_fs *ofs = sb->s_fs_info;
388
Amir Goldstein21a22872017-05-17 00:12:41 +0300389 return ofs->config.redirect_dir && !ofs->noxattr;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100390}
391
392const char *ovl_dentry_get_redirect(struct dentry *dentry)
393{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200394 return OVL_I(d_inode(dentry))->redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100395}
396
397void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
398{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200399 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Miklos Szeredia6c60652016-12-16 11:02:56 +0100400
Miklos Szeredicf31c462017-07-04 22:03:16 +0200401 kfree(oi->redirect);
402 oi->redirect = redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100403}
404
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200405void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100406{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200407 struct inode *upperinode = d_inode(upperdentry);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200408
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200409 WARN_ON(OVL_I(inode)->__upperdentry);
410
Miklos Szeredi25b77132017-07-04 22:03:16 +0200411 /*
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200412 * Make sure upperdentry is consistent before making it visible
Miklos Szeredi25b77132017-07-04 22:03:16 +0200413 */
414 smp_wmb();
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200415 OVL_I(inode)->__upperdentry = upperdentry;
Amir Goldstein31747ed2018-01-14 18:35:40 +0200416 if (inode_unhashed(inode)) {
Miklos Szeredi25b77132017-07-04 22:03:16 +0200417 inode->i_private = upperinode;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100418 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200419 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100420}
421
Miklos Szeredid9854c82018-07-18 15:44:40 +0200422static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100423{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200424 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100425
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200426 WARN_ON(!inode_is_locked(inode));
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200427 /*
428 * Version is used by readdir code to keep cache consistent. For merge
429 * dirs all changes need to be noted. For non-merge dirs, cache only
430 * contains impure (ones which have been copied up and have origins)
431 * entries, so only need to note changes to impure entries.
432 */
433 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
434 OVL_I(inode)->version++;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100435}
436
Miklos Szeredid9854c82018-07-18 15:44:40 +0200437void ovl_dir_modified(struct dentry *dentry, bool impurity)
438{
439 /* Copy mtime/ctime */
440 ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
441
442 ovl_dentry_version_inc(dentry, impurity);
443}
444
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100445u64 ovl_dentry_version_get(struct dentry *dentry)
446{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200447 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100448
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200449 WARN_ON(!inode_is_locked(inode));
450 return OVL_I(inode)->version;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100451}
452
453bool ovl_is_whiteout(struct dentry *dentry)
454{
455 struct inode *inode = dentry->d_inode;
456
457 return inode && IS_WHITEOUT(inode);
458}
459
460struct file *ovl_path_open(struct path *path, int flags)
461{
462 return dentry_open(path, flags | O_NOATIME, current_cred());
463}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200464
Vivek Goyal0c288872018-05-11 11:49:28 -0400465/* Caller should hold ovl_inode->lock */
466static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
467{
468 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
469
470 if (ovl_dentry_upper(dentry) &&
471 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
472 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
473 return true;
474
475 return false;
476}
477
478bool ovl_already_copied_up(struct dentry *dentry, int flags)
Vivek Goyal2002df82018-05-11 11:49:28 -0400479{
480 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
481
482 /*
483 * Check if copy-up has happened as well as for upper alias (in
484 * case of hard links) is there.
485 *
486 * Both checks are lockless:
487 * - false negatives: will recheck under oi->lock
488 * - false positives:
489 * + ovl_dentry_upper() uses memory barriers to ensure the
490 * upper dentry is up-to-date
491 * + ovl_dentry_has_upper_alias() relies on locking of
492 * upper parent i_rwsem to prevent reordering copy-up
493 * with rename.
494 */
495 if (ovl_dentry_upper(dentry) &&
Vivek Goyal0c288872018-05-11 11:49:28 -0400496 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
497 !ovl_dentry_needs_data_copy_up(dentry, flags))
Vivek Goyal2002df82018-05-11 11:49:28 -0400498 return true;
499
500 return false;
501}
502
Vivek Goyal0c288872018-05-11 11:49:28 -0400503int ovl_copy_up_start(struct dentry *dentry, int flags)
Amir Goldstein39d3d602017-01-17 06:34:56 +0200504{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300505 struct inode *inode = d_inode(dentry);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200506 int err;
507
Amir Goldstein531d3042020-03-02 15:03:35 +0200508 err = ovl_inode_lock_interruptible(inode);
Vivek Goyal0c288872018-05-11 11:49:28 -0400509 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
Amir Goldsteina015daf2017-06-21 15:28:51 +0300510 err = 1; /* Already copied up */
Amir Goldstein1e92e302018-10-18 18:37:14 +0300511 ovl_inode_unlock(inode);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200512 }
Amir Goldstein39d3d602017-01-17 06:34:56 +0200513
514 return err;
515}
516
517void ovl_copy_up_end(struct dentry *dentry)
518{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300519 ovl_inode_unlock(d_inode(dentry));
Amir Goldstein39d3d602017-01-17 06:34:56 +0200520}
Amir Goldstein82b749b2017-05-17 00:12:40 +0300521
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300522bool ovl_check_origin_xattr(struct dentry *dentry)
523{
524 int res;
525
526 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
527
528 /* Zero size value means "copied up but origin unknown" */
529 if (res >= 0)
530 return true;
531
532 return false;
533}
534
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300535bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
536{
537 int res;
538 char val;
539
540 if (!d_is_dir(dentry))
541 return false;
542
543 res = vfs_getxattr(dentry, name, &val, 1);
544 if (res == 1 && val == 'y')
545 return true;
546
547 return false;
548}
549
Amir Goldstein82b749b2017-05-17 00:12:40 +0300550int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
551 const char *name, const void *value, size_t size,
552 int xerr)
553{
554 int err;
555 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
556
557 if (ofs->noxattr)
558 return xerr;
559
560 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
561
562 if (err == -EOPNOTSUPP) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800563 pr_warn("cannot set %s xattr on upper\n", name);
Amir Goldstein82b749b2017-05-17 00:12:40 +0300564 ofs->noxattr = true;
565 return xerr;
566 }
567
568 return err;
569}
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300570
571int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
572{
573 int err;
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300574
Miklos Szeredi13c72072017-07-04 22:03:16 +0200575 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300576 return 0;
577
578 /*
579 * Do not fail when upper doesn't support xattrs.
580 * Upper inodes won't have origin nor redirect xattr anyway.
581 */
582 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
583 "y", 1, 0);
584 if (!err)
Miklos Szeredi13c72072017-07-04 22:03:16 +0200585 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300586
587 return err;
588}
Miklos Szeredi13c72072017-07-04 22:03:16 +0200589
590void ovl_set_flag(unsigned long flag, struct inode *inode)
591{
592 set_bit(flag, &OVL_I(inode)->flags);
593}
594
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200595void ovl_clear_flag(unsigned long flag, struct inode *inode)
596{
597 clear_bit(flag, &OVL_I(inode)->flags);
598}
599
Miklos Szeredi13c72072017-07-04 22:03:16 +0200600bool ovl_test_flag(unsigned long flag, struct inode *inode)
601{
602 return test_bit(flag, &OVL_I(inode)->flags);
603}
Amir Goldsteinad0af712017-06-21 15:28:32 +0300604
605/**
606 * Caller must hold a reference to inode to prevent it from being freed while
607 * it is marked inuse.
608 */
609bool ovl_inuse_trylock(struct dentry *dentry)
610{
611 struct inode *inode = d_inode(dentry);
612 bool locked = false;
613
614 spin_lock(&inode->i_lock);
615 if (!(inode->i_state & I_OVL_INUSE)) {
616 inode->i_state |= I_OVL_INUSE;
617 locked = true;
618 }
619 spin_unlock(&inode->i_lock);
620
621 return locked;
622}
623
624void ovl_inuse_unlock(struct dentry *dentry)
625{
626 if (dentry) {
627 struct inode *inode = d_inode(dentry);
628
629 spin_lock(&inode->i_lock);
630 WARN_ON(!(inode->i_state & I_OVL_INUSE));
631 inode->i_state &= ~I_OVL_INUSE;
632 spin_unlock(&inode->i_lock);
633 }
634}
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300635
Amir Goldstein146d62e2019-04-18 17:42:08 +0300636bool ovl_is_inuse(struct dentry *dentry)
637{
638 struct inode *inode = d_inode(dentry);
639 bool inuse;
640
641 spin_lock(&inode->i_lock);
642 inuse = (inode->i_state & I_OVL_INUSE);
643 spin_unlock(&inode->i_lock);
644
645 return inuse;
646}
647
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300648/*
649 * Does this overlay dentry need to be indexed on copy up?
650 */
651bool ovl_need_index(struct dentry *dentry)
652{
653 struct dentry *lower = ovl_dentry_lower(dentry);
654
655 if (!lower || !ovl_indexdir(dentry->d_sb))
656 return false;
657
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200658 /* Index all files for NFS export and consistency verification */
Amir Goldstein016b7202018-01-11 14:01:08 +0200659 if (ovl_index_all(dentry->d_sb))
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200660 return true;
661
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300662 /* Index only lower hardlinks on copy up */
663 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
664 return true;
665
666 return false;
667}
668
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300669/* Caller must hold OVL_I(inode)->lock */
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300670static void ovl_cleanup_index(struct dentry *dentry)
671{
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300672 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
673 struct inode *dir = indexdir->d_inode;
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300674 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
675 struct dentry *upperdentry = ovl_dentry_upper(dentry);
676 struct dentry *index = NULL;
677 struct inode *inode;
Amir Goldstein63e13252018-09-18 16:34:31 +0300678 struct qstr name = { };
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300679 int err;
680
681 err = ovl_get_index_name(lowerdentry, &name);
682 if (err)
683 goto fail;
684
685 inode = d_inode(upperdentry);
Amir Goldstein89a17552017-09-26 07:40:37 +0300686 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800687 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300688 upperdentry, inode->i_ino, inode->i_nlink);
689 /*
690 * We either have a bug with persistent union nlink or a lower
691 * hardlink was added while overlay is mounted. Adding a lower
692 * hardlink and then unlinking all overlay hardlinks would drop
693 * overlay nlink to zero before all upper inodes are unlinked.
694 * As a safety measure, when that situation is detected, set
695 * the overlay nlink to the index inode nlink minus one for the
696 * index entry itself.
697 */
698 set_nlink(d_inode(dentry), inode->i_nlink - 1);
699 ovl_set_nlink_upper(dentry);
700 goto out;
701 }
702
703 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300704 index = lookup_one_len(name.name, indexdir, name.len);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300705 err = PTR_ERR(index);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300706 if (IS_ERR(index)) {
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300707 index = NULL;
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300708 } else if (ovl_index_all(dentry->d_sb)) {
709 /* Whiteout orphan index to block future open by handle */
710 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
711 } else {
712 /* Cleanup orphan index entries */
713 err = ovl_cleanup(dir, index);
714 }
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300715
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300716 inode_unlock(dir);
717 if (err)
718 goto fail;
719
720out:
Amir Goldstein63e13252018-09-18 16:34:31 +0300721 kfree(name.name);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300722 dput(index);
723 return;
724
725fail:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800726 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300727 goto out;
728}
729
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300730/*
731 * Operations that change overlay inode and upper inode nlink need to be
732 * synchronized with copy up for persistent nlink accounting.
733 */
Amir Goldstein0e329922018-10-18 18:37:13 +0300734int ovl_nlink_start(struct dentry *dentry)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300735{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300736 struct inode *inode = d_inode(dentry);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300737 const struct cred *old_cred;
738 int err;
739
Amir Goldstein1e92e302018-10-18 18:37:14 +0300740 if (WARN_ON(!inode))
Amir Goldstein0e329922018-10-18 18:37:13 +0300741 return -ENOENT;
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300742
743 /*
744 * With inodes index is enabled, we store the union overlay nlink
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300745 * in an xattr on the index inode. When whiting out an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300746 * we need to decrement the overlay persistent nlink, but before the
747 * first copy up, we have no upper index inode to store the xattr.
748 *
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300749 * As a workaround, before whiteout/rename over an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300750 * copy up to create the upper index. Creating the upper index will
751 * initialize the overlay nlink, so it could be dropped if unlink
752 * or rename succeeds.
753 *
754 * TODO: implement metadata only index copy up when called with
755 * ovl_copy_up_flags(dentry, O_PATH).
756 */
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300757 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300758 err = ovl_copy_up(dentry);
759 if (err)
760 return err;
761 }
762
Amir Goldstein531d3042020-03-02 15:03:35 +0200763 err = ovl_inode_lock_interruptible(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300764 if (err)
765 return err;
766
Amir Goldstein1e92e302018-10-18 18:37:14 +0300767 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300768 goto out;
769
770 old_cred = ovl_override_creds(dentry->d_sb);
771 /*
772 * The overlay inode nlink should be incremented/decremented IFF the
773 * upper operation succeeds, along with nlink change of upper inode.
774 * Therefore, before link/unlink/rename, we store the union nlink
775 * value relative to the upper inode nlink in an upper inode xattr.
776 */
777 err = ovl_set_nlink_upper(dentry);
778 revert_creds(old_cred);
779
780out:
781 if (err)
Amir Goldstein1e92e302018-10-18 18:37:14 +0300782 ovl_inode_unlock(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300783
784 return err;
785}
786
Amir Goldstein0e329922018-10-18 18:37:13 +0300787void ovl_nlink_end(struct dentry *dentry)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300788{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300789 struct inode *inode = d_inode(dentry);
790
791 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
Amir Goldstein0e329922018-10-18 18:37:13 +0300792 const struct cred *old_cred;
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300793
Amir Goldstein0e329922018-10-18 18:37:13 +0300794 old_cred = ovl_override_creds(dentry->d_sb);
795 ovl_cleanup_index(dentry);
796 revert_creds(old_cred);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300797 }
Amir Goldstein0e329922018-10-18 18:37:13 +0300798
Amir Goldstein1e92e302018-10-18 18:37:14 +0300799 ovl_inode_unlock(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300800}
Amir Goldstein5820dc02017-09-25 16:39:55 +0300801
802int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
803{
804 /* Workdir should not be the same as upperdir */
805 if (workdir == upperdir)
806 goto err;
807
808 /* Workdir should not be subdir of upperdir and vice versa */
809 if (lock_rename(workdir, upperdir) != NULL)
810 goto err_unlock;
811
812 return 0;
813
814err_unlock:
815 unlock_rename(workdir, upperdir);
816err:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800817 pr_err("failed to lock workdir+upperdir\n");
Amir Goldstein5820dc02017-09-25 16:39:55 +0300818 return -EIO;
819}
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400820
821/* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
822int ovl_check_metacopy_xattr(struct dentry *dentry)
823{
824 int res;
825
826 /* Only regular files can have metacopy xattr */
827 if (!S_ISREG(d_inode(dentry)->i_mode))
828 return 0;
829
830 res = vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0);
831 if (res < 0) {
832 if (res == -ENODATA || res == -EOPNOTSUPP)
833 return 0;
834 goto out;
835 }
836
837 return 1;
838out:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800839 pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400840 return res;
841}
Vivek Goyal67d756c22018-05-11 11:49:30 -0400842
843bool ovl_is_metacopy_dentry(struct dentry *dentry)
844{
845 struct ovl_entry *oe = dentry->d_fsdata;
846
847 if (!d_is_reg(dentry))
848 return false;
849
850 if (ovl_dentry_upper(dentry)) {
851 if (!ovl_has_upperdata(d_inode(dentry)))
852 return true;
853 return false;
854 }
855
856 return (oe->numlower > 1);
857}
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400858
Vivek Goyal993a0b22019-01-30 14:01:57 -0500859ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value,
860 size_t padding)
861{
862 ssize_t res;
863 char *buf = NULL;
864
865 res = vfs_getxattr(dentry, name, NULL, 0);
866 if (res < 0) {
867 if (res == -ENODATA || res == -EOPNOTSUPP)
868 return -ENODATA;
869 goto fail;
870 }
871
872 if (res != 0) {
873 buf = kzalloc(res + padding, GFP_KERNEL);
874 if (!buf)
875 return -ENOMEM;
876
877 res = vfs_getxattr(dentry, name, buf, res);
878 if (res < 0)
879 goto fail;
880 }
881 *value = buf;
882
883 return res;
884
885fail:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800886 pr_warn_ratelimited("failed to get xattr %s: err=%zi)\n",
Vivek Goyal993a0b22019-01-30 14:01:57 -0500887 name, res);
888 kfree(buf);
889 return res;
890}
891
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400892char *ovl_get_redirect_xattr(struct dentry *dentry, int padding)
893{
894 int res;
895 char *s, *next, *buf = NULL;
896
Vivek Goyal993a0b22019-01-30 14:01:57 -0500897 res = ovl_getxattr(dentry, OVL_XATTR_REDIRECT, &buf, padding + 1);
898 if (res == -ENODATA)
899 return NULL;
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400900 if (res < 0)
Vivek Goyal993a0b22019-01-30 14:01:57 -0500901 return ERR_PTR(res);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400902 if (res == 0)
903 goto invalid;
904
905 if (buf[0] == '/') {
906 for (s = buf; *s++ == '/'; s = next) {
907 next = strchrnul(s, '/');
908 if (s == next)
909 goto invalid;
910 }
911 } else {
912 if (strchr(buf, '/') != NULL)
913 goto invalid;
914 }
915
916 return buf;
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400917invalid:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800918 pr_warn_ratelimited("invalid redirect (%s)\n", buf);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400919 res = -EINVAL;
Vivek Goyal993a0b22019-01-30 14:01:57 -0500920 kfree(buf);
921 return ERR_PTR(res);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400922}