blob: 73939e08d8bf8dc9dd1127d49d697d9a310ddd29 [file] [log] [blame]
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001/*
2 * Copyright (C) 2011 Novell Inc.
3 * Copyright (C) 2016 Red Hat, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/fs.h>
11#include <linux/mount.h>
12#include <linux/slab.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010013#include <linux/cred.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010014#include <linux/xattr.h>
Amir Goldstein02bcd152017-06-21 15:28:36 +030015#include <linux/exportfs.h>
16#include <linux/uuid.h>
Amir Goldsteincaf70cb2017-06-21 13:46:12 +030017#include <linux/namei.h>
18#include <linux/ratelimit.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010019#include "overlayfs.h"
Miklos Szeredibbb1e542016-12-16 11:02:56 +010020
21int ovl_want_write(struct dentry *dentry)
22{
23 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
24 return mnt_want_write(ofs->upper_mnt);
25}
26
27void ovl_drop_write(struct dentry *dentry)
28{
29 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
30 mnt_drop_write(ofs->upper_mnt);
31}
32
33struct dentry *ovl_workdir(struct dentry *dentry)
34{
35 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
36 return ofs->workdir;
37}
38
39const struct cred *ovl_override_creds(struct super_block *sb)
40{
41 struct ovl_fs *ofs = sb->s_fs_info;
42
43 return override_creds(ofs->creator_cred);
44}
45
Amir Goldstein7bcd74b2017-03-22 08:42:21 -040046struct super_block *ovl_same_sb(struct super_block *sb)
47{
48 struct ovl_fs *ofs = sb->s_fs_info;
49
Amir Goldstein51486262018-03-28 20:22:41 +030050 if (!ofs->numlowerfs)
51 return ofs->upper_mnt->mnt_sb;
52 else if (ofs->numlowerfs == 1 && !ofs->upper_mnt)
53 return ofs->lower_fs[0].sb;
54 else
55 return NULL;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -040056}
57
Amir Goldsteine487d882017-11-07 13:55:04 +020058/*
59 * Check if underlying fs supports file handles and try to determine encoding
60 * type, in order to deduce maximum inode number used by fs.
61 *
62 * Return 0 if file handles are not supported.
63 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
64 * Return -1 if fs uses a non default encoding with unknown inode size.
65 */
66int ovl_can_decode_fh(struct super_block *sb)
Amir Goldstein02bcd152017-06-21 15:28:36 +030067{
Amir Goldsteine487d882017-11-07 13:55:04 +020068 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry ||
69 uuid_is_null(&sb->s_uuid))
70 return 0;
71
72 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
Amir Goldstein02bcd152017-06-21 15:28:36 +030073}
74
75struct dentry *ovl_indexdir(struct super_block *sb)
76{
77 struct ovl_fs *ofs = sb->s_fs_info;
78
79 return ofs->indexdir;
80}
81
Amir Goldsteinf168f102018-01-19 11:26:53 +020082/* Index all files on copy up. For now only enabled for NFS export */
83bool ovl_index_all(struct super_block *sb)
84{
85 struct ovl_fs *ofs = sb->s_fs_info;
86
87 return ofs->config.nfs_export && ofs->config.index;
88}
89
90/* Verify lower origin on lookup. For now only enabled for NFS export */
91bool ovl_verify_lower(struct super_block *sb)
92{
93 struct ovl_fs *ofs = sb->s_fs_info;
94
95 return ofs->config.nfs_export && ofs->config.index;
96}
97
Miklos Szeredibbb1e542016-12-16 11:02:56 +010098struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
99{
100 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
101 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
102
103 if (oe)
104 oe->numlower = numlower;
105
106 return oe;
107}
108
109bool ovl_dentry_remote(struct dentry *dentry)
110{
111 return dentry->d_flags &
112 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
113 DCACHE_OP_REAL);
114}
115
116bool ovl_dentry_weird(struct dentry *dentry)
117{
118 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
119 DCACHE_MANAGE_TRANSIT |
120 DCACHE_OP_HASH |
121 DCACHE_OP_COMPARE);
122}
123
124enum ovl_path_type ovl_path_type(struct dentry *dentry)
125{
126 struct ovl_entry *oe = dentry->d_fsdata;
127 enum ovl_path_type type = 0;
128
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200129 if (ovl_dentry_upper(dentry)) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100130 type = __OVL_PATH_UPPER;
131
132 /*
Amir Goldstein59548502017-04-23 23:12:34 +0300133 * Non-dir dentry can hold lower dentry of its copy up origin.
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100134 */
Amir Goldstein59548502017-04-23 23:12:34 +0300135 if (oe->numlower) {
136 type |= __OVL_PATH_ORIGIN;
137 if (d_is_dir(dentry))
138 type |= __OVL_PATH_MERGE;
139 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100140 } else {
141 if (oe->numlower > 1)
142 type |= __OVL_PATH_MERGE;
143 }
144 return type;
145}
146
147void ovl_path_upper(struct dentry *dentry, struct path *path)
148{
149 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100150
151 path->mnt = ofs->upper_mnt;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200152 path->dentry = ovl_dentry_upper(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100153}
154
155void ovl_path_lower(struct dentry *dentry, struct path *path)
156{
157 struct ovl_entry *oe = dentry->d_fsdata;
158
Chandan Rajendrab9343632017-07-24 01:57:54 -0500159 if (oe->numlower) {
160 path->mnt = oe->lowerstack[0].layer->mnt;
161 path->dentry = oe->lowerstack[0].dentry;
162 } else {
163 *path = (struct path) { };
164 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100165}
166
Vivek Goyal4f93b422018-05-11 11:49:30 -0400167void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
168{
169 struct ovl_entry *oe = dentry->d_fsdata;
170
171 if (oe->numlower) {
172 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
173 path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
174 } else {
175 *path = (struct path) { };
176 }
177}
178
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100179enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
180{
181 enum ovl_path_type type = ovl_path_type(dentry);
182
183 if (!OVL_TYPE_UPPER(type))
184 ovl_path_lower(dentry, path);
185 else
186 ovl_path_upper(dentry, path);
187
188 return type;
189}
190
191struct dentry *ovl_dentry_upper(struct dentry *dentry)
192{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200193 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100194}
195
196struct dentry *ovl_dentry_lower(struct dentry *dentry)
197{
198 struct ovl_entry *oe = dentry->d_fsdata;
199
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200200 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100201}
202
Amir Goldsteinda309e82017-11-08 19:39:51 +0200203struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
204{
205 struct ovl_entry *oe = dentry->d_fsdata;
206
207 return oe->numlower ? oe->lowerstack[0].layer : NULL;
208}
209
Vivek Goyal647d2532018-05-11 11:49:30 -0400210/*
211 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
212 * dependig on what is stored in lowerstack[0]. At times we need to find
213 * lower dentry which has data (and not metacopy dentry). This helper
214 * returns the lower data dentry.
215 */
216struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
217{
218 struct ovl_entry *oe = dentry->d_fsdata;
219
220 return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
221}
222
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100223struct dentry *ovl_dentry_real(struct dentry *dentry)
224{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200225 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100226}
227
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200228struct dentry *ovl_i_dentry_upper(struct inode *inode)
229{
230 return ovl_upperdentry_dereference(OVL_I(inode));
231}
232
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200233struct inode *ovl_inode_upper(struct inode *inode)
Miklos Szeredi25b77132017-07-04 22:03:16 +0200234{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200235 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200236
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200237 return upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200238}
239
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200240struct inode *ovl_inode_lower(struct inode *inode)
241{
242 return OVL_I(inode)->lower;
243}
244
245struct inode *ovl_inode_real(struct inode *inode)
246{
247 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
248}
249
Vivek Goyal2664bd02018-05-11 11:49:30 -0400250/* Return inode which contains lower data. Do not return metacopy */
251struct inode *ovl_inode_lowerdata(struct inode *inode)
252{
253 if (WARN_ON(!S_ISREG(inode->i_mode)))
254 return NULL;
255
256 return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
257}
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200258
Vivek Goyal4823d492018-05-11 11:49:31 -0400259/* Return real inode which contains data. Does not return metacopy inode */
260struct inode *ovl_inode_realdata(struct inode *inode)
261{
262 struct inode *upperinode;
263
264 upperinode = ovl_inode_upper(inode);
265 if (upperinode && ovl_has_upperdata(inode))
266 return upperinode;
267
268 return ovl_inode_lowerdata(inode);
269}
270
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200271struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100272{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200273 return OVL_I(inode)->cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100274}
275
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200276void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100277{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200278 OVL_I(inode)->cache = cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100279}
280
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200281void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
282{
283 set_bit(flag, &OVL_E(dentry)->flags);
284}
285
286void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
287{
288 clear_bit(flag, &OVL_E(dentry)->flags);
289}
290
291bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
292{
293 return test_bit(flag, &OVL_E(dentry)->flags);
294}
295
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100296bool ovl_dentry_is_opaque(struct dentry *dentry)
297{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200298 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100299}
300
301bool ovl_dentry_is_whiteout(struct dentry *dentry)
302{
303 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
304}
305
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100306void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100307{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200308 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100309}
310
Miklos Szeredi55acc662017-07-04 22:03:18 +0200311/*
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300312 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
313 * to return positive, while there's no actual upper alias for the inode.
314 * Copy up code needs to know about the existence of the upper alias, so it
315 * can't use ovl_dentry_upper().
Miklos Szeredi55acc662017-07-04 22:03:18 +0200316 */
317bool ovl_dentry_has_upper_alias(struct dentry *dentry)
318{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200319 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200320}
321
322void ovl_dentry_set_upper_alias(struct dentry *dentry)
323{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200324 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200325}
326
Vivek Goyal0c288872018-05-11 11:49:28 -0400327static bool ovl_should_check_upperdata(struct inode *inode)
328{
329 if (!S_ISREG(inode->i_mode))
330 return false;
331
332 if (!ovl_inode_lower(inode))
333 return false;
334
335 return true;
336}
337
338bool ovl_has_upperdata(struct inode *inode)
339{
340 if (!ovl_should_check_upperdata(inode))
341 return true;
342
343 if (!ovl_test_flag(OVL_UPPERDATA, inode))
344 return false;
345 /*
346 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
347 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
348 * if setting of OVL_UPPERDATA is visible, then effects of writes
349 * before that are visible too.
350 */
351 smp_rmb();
352 return true;
353}
354
355void ovl_set_upperdata(struct inode *inode)
356{
357 /*
358 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
359 * if OVL_UPPERDATA flag is visible, then effects of write operations
360 * before it are visible as well.
361 */
362 smp_wmb();
363 ovl_set_flag(OVL_UPPERDATA, inode);
364}
365
366/* Caller should hold ovl_inode->lock */
367bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
368{
369 if (!ovl_open_flags_need_copy_up(flags))
370 return false;
371
372 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
373}
374
375bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
376{
377 if (!ovl_open_flags_need_copy_up(flags))
378 return false;
379
380 return !ovl_has_upperdata(d_inode(dentry));
381}
382
Miklos Szeredia6c60652016-12-16 11:02:56 +0100383bool ovl_redirect_dir(struct super_block *sb)
384{
385 struct ovl_fs *ofs = sb->s_fs_info;
386
Amir Goldstein21a22872017-05-17 00:12:41 +0300387 return ofs->config.redirect_dir && !ofs->noxattr;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100388}
389
390const char *ovl_dentry_get_redirect(struct dentry *dentry)
391{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200392 return OVL_I(d_inode(dentry))->redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100393}
394
395void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
396{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200397 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Miklos Szeredia6c60652016-12-16 11:02:56 +0100398
Miklos Szeredicf31c462017-07-04 22:03:16 +0200399 kfree(oi->redirect);
400 oi->redirect = redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100401}
402
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200403void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
Vivek Goyal2664bd02018-05-11 11:49:30 -0400404 struct dentry *lowerdentry, struct dentry *lowerdata)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100405{
Amir Goldstein695b46e2018-03-15 23:39:01 +0200406 struct inode *realinode = d_inode(upperdentry ?: lowerdentry);
407
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200408 if (upperdentry)
409 OVL_I(inode)->__upperdentry = upperdentry;
410 if (lowerdentry)
Amir Goldstein31747ed2018-01-14 18:35:40 +0200411 OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
Vivek Goyal2664bd02018-05-11 11:49:30 -0400412 if (lowerdata)
413 OVL_I(inode)->lowerdata = igrab(d_inode(lowerdata));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100414
Amir Goldstein695b46e2018-03-15 23:39:01 +0200415 ovl_copyattr(realinode, inode);
Miklos Szeredi4f357292018-07-18 15:44:41 +0200416 ovl_copyflags(realinode, inode);
Amir Goldstein695b46e2018-03-15 23:39:01 +0200417 if (!inode->i_ino)
418 inode->i_ino = realinode->i_ino;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100419}
420
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200421void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100422{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200423 struct inode *upperinode = d_inode(upperdentry);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200424
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200425 WARN_ON(OVL_I(inode)->__upperdentry);
426
Miklos Szeredi25b77132017-07-04 22:03:16 +0200427 /*
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200428 * Make sure upperdentry is consistent before making it visible
Miklos Szeredi25b77132017-07-04 22:03:16 +0200429 */
430 smp_wmb();
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200431 OVL_I(inode)->__upperdentry = upperdentry;
Amir Goldstein31747ed2018-01-14 18:35:40 +0200432 if (inode_unhashed(inode)) {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200433 if (!inode->i_ino)
434 inode->i_ino = upperinode->i_ino;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200435 inode->i_private = upperinode;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100436 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200437 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100438}
439
Miklos Szeredid9854c82018-07-18 15:44:40 +0200440static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100441{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200442 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100443
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200444 WARN_ON(!inode_is_locked(inode));
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200445 /*
446 * Version is used by readdir code to keep cache consistent. For merge
447 * dirs all changes need to be noted. For non-merge dirs, cache only
448 * contains impure (ones which have been copied up and have origins)
449 * entries, so only need to note changes to impure entries.
450 */
451 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
452 OVL_I(inode)->version++;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100453}
454
Miklos Szeredid9854c82018-07-18 15:44:40 +0200455void ovl_dir_modified(struct dentry *dentry, bool impurity)
456{
457 /* Copy mtime/ctime */
458 ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
459
460 ovl_dentry_version_inc(dentry, impurity);
461}
462
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100463u64 ovl_dentry_version_get(struct dentry *dentry)
464{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200465 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100466
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200467 WARN_ON(!inode_is_locked(inode));
468 return OVL_I(inode)->version;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100469}
470
471bool ovl_is_whiteout(struct dentry *dentry)
472{
473 struct inode *inode = dentry->d_inode;
474
475 return inode && IS_WHITEOUT(inode);
476}
477
478struct file *ovl_path_open(struct path *path, int flags)
479{
480 return dentry_open(path, flags | O_NOATIME, current_cred());
481}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200482
Vivek Goyal0c288872018-05-11 11:49:28 -0400483/* Caller should hold ovl_inode->lock */
484static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
485{
486 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
487
488 if (ovl_dentry_upper(dentry) &&
489 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
490 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
491 return true;
492
493 return false;
494}
495
496bool ovl_already_copied_up(struct dentry *dentry, int flags)
Vivek Goyal2002df82018-05-11 11:49:28 -0400497{
498 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
499
500 /*
501 * Check if copy-up has happened as well as for upper alias (in
502 * case of hard links) is there.
503 *
504 * Both checks are lockless:
505 * - false negatives: will recheck under oi->lock
506 * - false positives:
507 * + ovl_dentry_upper() uses memory barriers to ensure the
508 * upper dentry is up-to-date
509 * + ovl_dentry_has_upper_alias() relies on locking of
510 * upper parent i_rwsem to prevent reordering copy-up
511 * with rename.
512 */
513 if (ovl_dentry_upper(dentry) &&
Vivek Goyal0c288872018-05-11 11:49:28 -0400514 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
515 !ovl_dentry_needs_data_copy_up(dentry, flags))
Vivek Goyal2002df82018-05-11 11:49:28 -0400516 return true;
517
518 return false;
519}
520
Vivek Goyal0c288872018-05-11 11:49:28 -0400521int ovl_copy_up_start(struct dentry *dentry, int flags)
Amir Goldstein39d3d602017-01-17 06:34:56 +0200522{
Amir Goldsteina015daf2017-06-21 15:28:51 +0300523 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Amir Goldstein39d3d602017-01-17 06:34:56 +0200524 int err;
525
Amir Goldsteina015daf2017-06-21 15:28:51 +0300526 err = mutex_lock_interruptible(&oi->lock);
Vivek Goyal0c288872018-05-11 11:49:28 -0400527 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
Amir Goldsteina015daf2017-06-21 15:28:51 +0300528 err = 1; /* Already copied up */
529 mutex_unlock(&oi->lock);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200530 }
Amir Goldstein39d3d602017-01-17 06:34:56 +0200531
532 return err;
533}
534
535void ovl_copy_up_end(struct dentry *dentry)
536{
Amir Goldsteina015daf2017-06-21 15:28:51 +0300537 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200538}
Amir Goldstein82b749b2017-05-17 00:12:40 +0300539
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300540bool ovl_check_origin_xattr(struct dentry *dentry)
541{
542 int res;
543
544 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
545
546 /* Zero size value means "copied up but origin unknown" */
547 if (res >= 0)
548 return true;
549
550 return false;
551}
552
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300553bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
554{
555 int res;
556 char val;
557
558 if (!d_is_dir(dentry))
559 return false;
560
561 res = vfs_getxattr(dentry, name, &val, 1);
562 if (res == 1 && val == 'y')
563 return true;
564
565 return false;
566}
567
Amir Goldstein82b749b2017-05-17 00:12:40 +0300568int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
569 const char *name, const void *value, size_t size,
570 int xerr)
571{
572 int err;
573 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
574
575 if (ofs->noxattr)
576 return xerr;
577
578 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
579
580 if (err == -EOPNOTSUPP) {
581 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
582 ofs->noxattr = true;
583 return xerr;
584 }
585
586 return err;
587}
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300588
589int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
590{
591 int err;
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300592
Miklos Szeredi13c72072017-07-04 22:03:16 +0200593 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300594 return 0;
595
596 /*
597 * Do not fail when upper doesn't support xattrs.
598 * Upper inodes won't have origin nor redirect xattr anyway.
599 */
600 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
601 "y", 1, 0);
602 if (!err)
Miklos Szeredi13c72072017-07-04 22:03:16 +0200603 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300604
605 return err;
606}
Miklos Szeredi13c72072017-07-04 22:03:16 +0200607
608void ovl_set_flag(unsigned long flag, struct inode *inode)
609{
610 set_bit(flag, &OVL_I(inode)->flags);
611}
612
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200613void ovl_clear_flag(unsigned long flag, struct inode *inode)
614{
615 clear_bit(flag, &OVL_I(inode)->flags);
616}
617
Miklos Szeredi13c72072017-07-04 22:03:16 +0200618bool ovl_test_flag(unsigned long flag, struct inode *inode)
619{
620 return test_bit(flag, &OVL_I(inode)->flags);
621}
Amir Goldsteinad0af712017-06-21 15:28:32 +0300622
623/**
624 * Caller must hold a reference to inode to prevent it from being freed while
625 * it is marked inuse.
626 */
627bool ovl_inuse_trylock(struct dentry *dentry)
628{
629 struct inode *inode = d_inode(dentry);
630 bool locked = false;
631
632 spin_lock(&inode->i_lock);
633 if (!(inode->i_state & I_OVL_INUSE)) {
634 inode->i_state |= I_OVL_INUSE;
635 locked = true;
636 }
637 spin_unlock(&inode->i_lock);
638
639 return locked;
640}
641
642void ovl_inuse_unlock(struct dentry *dentry)
643{
644 if (dentry) {
645 struct inode *inode = d_inode(dentry);
646
647 spin_lock(&inode->i_lock);
648 WARN_ON(!(inode->i_state & I_OVL_INUSE));
649 inode->i_state &= ~I_OVL_INUSE;
650 spin_unlock(&inode->i_lock);
651 }
652}
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300653
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300654/*
655 * Does this overlay dentry need to be indexed on copy up?
656 */
657bool ovl_need_index(struct dentry *dentry)
658{
659 struct dentry *lower = ovl_dentry_lower(dentry);
660
661 if (!lower || !ovl_indexdir(dentry->d_sb))
662 return false;
663
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200664 /* Index all files for NFS export and consistency verification */
Amir Goldstein016b7202018-01-11 14:01:08 +0200665 if (ovl_index_all(dentry->d_sb))
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200666 return true;
667
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300668 /* Index only lower hardlinks on copy up */
669 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
670 return true;
671
672 return false;
673}
674
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300675/* Caller must hold OVL_I(inode)->lock */
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300676static void ovl_cleanup_index(struct dentry *dentry)
677{
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300678 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
679 struct inode *dir = indexdir->d_inode;
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300680 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
681 struct dentry *upperdentry = ovl_dentry_upper(dentry);
682 struct dentry *index = NULL;
683 struct inode *inode;
684 struct qstr name;
685 int err;
686
687 err = ovl_get_index_name(lowerdentry, &name);
688 if (err)
689 goto fail;
690
691 inode = d_inode(upperdentry);
Amir Goldstein89a17552017-09-26 07:40:37 +0300692 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300693 pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
694 upperdentry, inode->i_ino, inode->i_nlink);
695 /*
696 * We either have a bug with persistent union nlink or a lower
697 * hardlink was added while overlay is mounted. Adding a lower
698 * hardlink and then unlinking all overlay hardlinks would drop
699 * overlay nlink to zero before all upper inodes are unlinked.
700 * As a safety measure, when that situation is detected, set
701 * the overlay nlink to the index inode nlink minus one for the
702 * index entry itself.
703 */
704 set_nlink(d_inode(dentry), inode->i_nlink - 1);
705 ovl_set_nlink_upper(dentry);
706 goto out;
707 }
708
709 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300710 index = lookup_one_len(name.name, indexdir, name.len);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300711 err = PTR_ERR(index);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300712 if (IS_ERR(index)) {
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300713 index = NULL;
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300714 } else if (ovl_index_all(dentry->d_sb)) {
715 /* Whiteout orphan index to block future open by handle */
716 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
717 } else {
718 /* Cleanup orphan index entries */
719 err = ovl_cleanup(dir, index);
720 }
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300721
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300722 inode_unlock(dir);
723 if (err)
724 goto fail;
725
726out:
727 dput(index);
728 return;
729
730fail:
731 pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
732 goto out;
733}
734
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300735/*
736 * Operations that change overlay inode and upper inode nlink need to be
737 * synchronized with copy up for persistent nlink accounting.
738 */
739int ovl_nlink_start(struct dentry *dentry, bool *locked)
740{
741 struct ovl_inode *oi = OVL_I(d_inode(dentry));
742 const struct cred *old_cred;
743 int err;
744
Amir Goldstein89a17552017-09-26 07:40:37 +0300745 if (!d_inode(dentry))
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300746 return 0;
747
748 /*
749 * With inodes index is enabled, we store the union overlay nlink
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300750 * in an xattr on the index inode. When whiting out an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300751 * we need to decrement the overlay persistent nlink, but before the
752 * first copy up, we have no upper index inode to store the xattr.
753 *
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300754 * As a workaround, before whiteout/rename over an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300755 * copy up to create the upper index. Creating the upper index will
756 * initialize the overlay nlink, so it could be dropped if unlink
757 * or rename succeeds.
758 *
759 * TODO: implement metadata only index copy up when called with
760 * ovl_copy_up_flags(dentry, O_PATH).
761 */
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300762 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300763 err = ovl_copy_up(dentry);
764 if (err)
765 return err;
766 }
767
768 err = mutex_lock_interruptible(&oi->lock);
769 if (err)
770 return err;
771
Amir Goldstein89a17552017-09-26 07:40:37 +0300772 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300773 goto out;
774
775 old_cred = ovl_override_creds(dentry->d_sb);
776 /*
777 * The overlay inode nlink should be incremented/decremented IFF the
778 * upper operation succeeds, along with nlink change of upper inode.
779 * Therefore, before link/unlink/rename, we store the union nlink
780 * value relative to the upper inode nlink in an upper inode xattr.
781 */
782 err = ovl_set_nlink_upper(dentry);
783 revert_creds(old_cred);
784
785out:
786 if (err)
787 mutex_unlock(&oi->lock);
788 else
789 *locked = true;
790
791 return err;
792}
793
794void ovl_nlink_end(struct dentry *dentry, bool locked)
795{
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300796 if (locked) {
797 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
798 d_inode(dentry)->i_nlink == 0) {
799 const struct cred *old_cred;
800
801 old_cred = ovl_override_creds(dentry->d_sb);
802 ovl_cleanup_index(dentry);
803 revert_creds(old_cred);
804 }
805
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300806 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300807 }
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300808}
Amir Goldstein5820dc02017-09-25 16:39:55 +0300809
810int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
811{
812 /* Workdir should not be the same as upperdir */
813 if (workdir == upperdir)
814 goto err;
815
816 /* Workdir should not be subdir of upperdir and vice versa */
817 if (lock_rename(workdir, upperdir) != NULL)
818 goto err_unlock;
819
820 return 0;
821
822err_unlock:
823 unlock_rename(workdir, upperdir);
824err:
825 pr_err("overlayfs: failed to lock workdir+upperdir\n");
826 return -EIO;
827}
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400828
829/* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
830int ovl_check_metacopy_xattr(struct dentry *dentry)
831{
832 int res;
833
834 /* Only regular files can have metacopy xattr */
835 if (!S_ISREG(d_inode(dentry)->i_mode))
836 return 0;
837
838 res = vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0);
839 if (res < 0) {
840 if (res == -ENODATA || res == -EOPNOTSUPP)
841 return 0;
842 goto out;
843 }
844
845 return 1;
846out:
847 pr_warn_ratelimited("overlayfs: failed to get metacopy (%i)\n", res);
848 return res;
849}
Vivek Goyal67d756c22018-05-11 11:49:30 -0400850
851bool ovl_is_metacopy_dentry(struct dentry *dentry)
852{
853 struct ovl_entry *oe = dentry->d_fsdata;
854
855 if (!d_is_reg(dentry))
856 return false;
857
858 if (ovl_dentry_upper(dentry)) {
859 if (!ovl_has_upperdata(d_inode(dentry)))
860 return true;
861 return false;
862 }
863
864 return (oe->numlower > 1);
865}