blob: 4f9c2ecee74c69c7f7701471940eda6f2048f884 [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
250
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200251struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100252{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200253 return OVL_I(inode)->cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100254}
255
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200256void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100257{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200258 OVL_I(inode)->cache = cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100259}
260
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200261void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
262{
263 set_bit(flag, &OVL_E(dentry)->flags);
264}
265
266void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
267{
268 clear_bit(flag, &OVL_E(dentry)->flags);
269}
270
271bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
272{
273 return test_bit(flag, &OVL_E(dentry)->flags);
274}
275
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100276bool ovl_dentry_is_opaque(struct dentry *dentry)
277{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200278 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100279}
280
281bool ovl_dentry_is_whiteout(struct dentry *dentry)
282{
283 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
284}
285
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100286void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100287{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200288 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100289}
290
Miklos Szeredi55acc662017-07-04 22:03:18 +0200291/*
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300292 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
293 * to return positive, while there's no actual upper alias for the inode.
294 * Copy up code needs to know about the existence of the upper alias, so it
295 * can't use ovl_dentry_upper().
Miklos Szeredi55acc662017-07-04 22:03:18 +0200296 */
297bool ovl_dentry_has_upper_alias(struct dentry *dentry)
298{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200299 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200300}
301
302void ovl_dentry_set_upper_alias(struct dentry *dentry)
303{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200304 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200305}
306
Vivek Goyal0c288872018-05-11 11:49:28 -0400307static bool ovl_should_check_upperdata(struct inode *inode)
308{
309 if (!S_ISREG(inode->i_mode))
310 return false;
311
312 if (!ovl_inode_lower(inode))
313 return false;
314
315 return true;
316}
317
318bool ovl_has_upperdata(struct inode *inode)
319{
320 if (!ovl_should_check_upperdata(inode))
321 return true;
322
323 if (!ovl_test_flag(OVL_UPPERDATA, inode))
324 return false;
325 /*
326 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
327 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
328 * if setting of OVL_UPPERDATA is visible, then effects of writes
329 * before that are visible too.
330 */
331 smp_rmb();
332 return true;
333}
334
335void ovl_set_upperdata(struct inode *inode)
336{
337 /*
338 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
339 * if OVL_UPPERDATA flag is visible, then effects of write operations
340 * before it are visible as well.
341 */
342 smp_wmb();
343 ovl_set_flag(OVL_UPPERDATA, inode);
344}
345
346/* Caller should hold ovl_inode->lock */
347bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
348{
349 if (!ovl_open_flags_need_copy_up(flags))
350 return false;
351
352 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
353}
354
355bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
356{
357 if (!ovl_open_flags_need_copy_up(flags))
358 return false;
359
360 return !ovl_has_upperdata(d_inode(dentry));
361}
362
Miklos Szeredia6c60652016-12-16 11:02:56 +0100363bool ovl_redirect_dir(struct super_block *sb)
364{
365 struct ovl_fs *ofs = sb->s_fs_info;
366
Amir Goldstein21a22872017-05-17 00:12:41 +0300367 return ofs->config.redirect_dir && !ofs->noxattr;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100368}
369
370const char *ovl_dentry_get_redirect(struct dentry *dentry)
371{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200372 return OVL_I(d_inode(dentry))->redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100373}
374
375void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
376{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200377 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Miklos Szeredia6c60652016-12-16 11:02:56 +0100378
Miklos Szeredicf31c462017-07-04 22:03:16 +0200379 kfree(oi->redirect);
380 oi->redirect = redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100381}
382
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200383void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
384 struct dentry *lowerdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100385{
Amir Goldstein695b46e2018-03-15 23:39:01 +0200386 struct inode *realinode = d_inode(upperdentry ?: lowerdentry);
387
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200388 if (upperdentry)
389 OVL_I(inode)->__upperdentry = upperdentry;
390 if (lowerdentry)
Amir Goldstein31747ed2018-01-14 18:35:40 +0200391 OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100392
Amir Goldstein695b46e2018-03-15 23:39:01 +0200393 ovl_copyattr(realinode, inode);
Miklos Szeredi4f357292018-07-18 15:44:41 +0200394 ovl_copyflags(realinode, inode);
Amir Goldstein695b46e2018-03-15 23:39:01 +0200395 if (!inode->i_ino)
396 inode->i_ino = realinode->i_ino;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100397}
398
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200399void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100400{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200401 struct inode *upperinode = d_inode(upperdentry);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200402
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200403 WARN_ON(OVL_I(inode)->__upperdentry);
404
Miklos Szeredi25b77132017-07-04 22:03:16 +0200405 /*
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200406 * Make sure upperdentry is consistent before making it visible
Miklos Szeredi25b77132017-07-04 22:03:16 +0200407 */
408 smp_wmb();
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200409 OVL_I(inode)->__upperdentry = upperdentry;
Amir Goldstein31747ed2018-01-14 18:35:40 +0200410 if (inode_unhashed(inode)) {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200411 if (!inode->i_ino)
412 inode->i_ino = upperinode->i_ino;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200413 inode->i_private = upperinode;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100414 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200415 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100416}
417
Miklos Szeredid9854c82018-07-18 15:44:40 +0200418static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100419{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200420 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100421
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200422 WARN_ON(!inode_is_locked(inode));
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200423 /*
424 * Version is used by readdir code to keep cache consistent. For merge
425 * dirs all changes need to be noted. For non-merge dirs, cache only
426 * contains impure (ones which have been copied up and have origins)
427 * entries, so only need to note changes to impure entries.
428 */
429 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
430 OVL_I(inode)->version++;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100431}
432
Miklos Szeredid9854c82018-07-18 15:44:40 +0200433void ovl_dir_modified(struct dentry *dentry, bool impurity)
434{
435 /* Copy mtime/ctime */
436 ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
437
438 ovl_dentry_version_inc(dentry, impurity);
439}
440
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100441u64 ovl_dentry_version_get(struct dentry *dentry)
442{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200443 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100444
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200445 WARN_ON(!inode_is_locked(inode));
446 return OVL_I(inode)->version;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100447}
448
449bool ovl_is_whiteout(struct dentry *dentry)
450{
451 struct inode *inode = dentry->d_inode;
452
453 return inode && IS_WHITEOUT(inode);
454}
455
456struct file *ovl_path_open(struct path *path, int flags)
457{
458 return dentry_open(path, flags | O_NOATIME, current_cred());
459}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200460
Vivek Goyal0c288872018-05-11 11:49:28 -0400461/* Caller should hold ovl_inode->lock */
462static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
463{
464 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
465
466 if (ovl_dentry_upper(dentry) &&
467 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
468 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
469 return true;
470
471 return false;
472}
473
474bool ovl_already_copied_up(struct dentry *dentry, int flags)
Vivek Goyal2002df82018-05-11 11:49:28 -0400475{
476 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
477
478 /*
479 * Check if copy-up has happened as well as for upper alias (in
480 * case of hard links) is there.
481 *
482 * Both checks are lockless:
483 * - false negatives: will recheck under oi->lock
484 * - false positives:
485 * + ovl_dentry_upper() uses memory barriers to ensure the
486 * upper dentry is up-to-date
487 * + ovl_dentry_has_upper_alias() relies on locking of
488 * upper parent i_rwsem to prevent reordering copy-up
489 * with rename.
490 */
491 if (ovl_dentry_upper(dentry) &&
Vivek Goyal0c288872018-05-11 11:49:28 -0400492 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
493 !ovl_dentry_needs_data_copy_up(dentry, flags))
Vivek Goyal2002df82018-05-11 11:49:28 -0400494 return true;
495
496 return false;
497}
498
Vivek Goyal0c288872018-05-11 11:49:28 -0400499int ovl_copy_up_start(struct dentry *dentry, int flags)
Amir Goldstein39d3d602017-01-17 06:34:56 +0200500{
Amir Goldsteina015daf2017-06-21 15:28:51 +0300501 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Amir Goldstein39d3d602017-01-17 06:34:56 +0200502 int err;
503
Amir Goldsteina015daf2017-06-21 15:28:51 +0300504 err = mutex_lock_interruptible(&oi->lock);
Vivek Goyal0c288872018-05-11 11:49:28 -0400505 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
Amir Goldsteina015daf2017-06-21 15:28:51 +0300506 err = 1; /* Already copied up */
507 mutex_unlock(&oi->lock);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200508 }
Amir Goldstein39d3d602017-01-17 06:34:56 +0200509
510 return err;
511}
512
513void ovl_copy_up_end(struct dentry *dentry)
514{
Amir Goldsteina015daf2017-06-21 15:28:51 +0300515 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200516}
Amir Goldstein82b749b2017-05-17 00:12:40 +0300517
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300518bool ovl_check_origin_xattr(struct dentry *dentry)
519{
520 int res;
521
522 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
523
524 /* Zero size value means "copied up but origin unknown" */
525 if (res >= 0)
526 return true;
527
528 return false;
529}
530
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300531bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
532{
533 int res;
534 char val;
535
536 if (!d_is_dir(dentry))
537 return false;
538
539 res = vfs_getxattr(dentry, name, &val, 1);
540 if (res == 1 && val == 'y')
541 return true;
542
543 return false;
544}
545
Amir Goldstein82b749b2017-05-17 00:12:40 +0300546int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
547 const char *name, const void *value, size_t size,
548 int xerr)
549{
550 int err;
551 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
552
553 if (ofs->noxattr)
554 return xerr;
555
556 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
557
558 if (err == -EOPNOTSUPP) {
559 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
560 ofs->noxattr = true;
561 return xerr;
562 }
563
564 return err;
565}
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300566
567int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
568{
569 int err;
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300570
Miklos Szeredi13c72072017-07-04 22:03:16 +0200571 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300572 return 0;
573
574 /*
575 * Do not fail when upper doesn't support xattrs.
576 * Upper inodes won't have origin nor redirect xattr anyway.
577 */
578 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
579 "y", 1, 0);
580 if (!err)
Miklos Szeredi13c72072017-07-04 22:03:16 +0200581 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300582
583 return err;
584}
Miklos Szeredi13c72072017-07-04 22:03:16 +0200585
586void ovl_set_flag(unsigned long flag, struct inode *inode)
587{
588 set_bit(flag, &OVL_I(inode)->flags);
589}
590
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200591void ovl_clear_flag(unsigned long flag, struct inode *inode)
592{
593 clear_bit(flag, &OVL_I(inode)->flags);
594}
595
Miklos Szeredi13c72072017-07-04 22:03:16 +0200596bool ovl_test_flag(unsigned long flag, struct inode *inode)
597{
598 return test_bit(flag, &OVL_I(inode)->flags);
599}
Amir Goldsteinad0af712017-06-21 15:28:32 +0300600
601/**
602 * Caller must hold a reference to inode to prevent it from being freed while
603 * it is marked inuse.
604 */
605bool ovl_inuse_trylock(struct dentry *dentry)
606{
607 struct inode *inode = d_inode(dentry);
608 bool locked = false;
609
610 spin_lock(&inode->i_lock);
611 if (!(inode->i_state & I_OVL_INUSE)) {
612 inode->i_state |= I_OVL_INUSE;
613 locked = true;
614 }
615 spin_unlock(&inode->i_lock);
616
617 return locked;
618}
619
620void ovl_inuse_unlock(struct dentry *dentry)
621{
622 if (dentry) {
623 struct inode *inode = d_inode(dentry);
624
625 spin_lock(&inode->i_lock);
626 WARN_ON(!(inode->i_state & I_OVL_INUSE));
627 inode->i_state &= ~I_OVL_INUSE;
628 spin_unlock(&inode->i_lock);
629 }
630}
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300631
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300632/*
633 * Does this overlay dentry need to be indexed on copy up?
634 */
635bool ovl_need_index(struct dentry *dentry)
636{
637 struct dentry *lower = ovl_dentry_lower(dentry);
638
639 if (!lower || !ovl_indexdir(dentry->d_sb))
640 return false;
641
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200642 /* Index all files for NFS export and consistency verification */
Amir Goldstein016b7202018-01-11 14:01:08 +0200643 if (ovl_index_all(dentry->d_sb))
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200644 return true;
645
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300646 /* Index only lower hardlinks on copy up */
647 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
648 return true;
649
650 return false;
651}
652
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300653/* Caller must hold OVL_I(inode)->lock */
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300654static void ovl_cleanup_index(struct dentry *dentry)
655{
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300656 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
657 struct inode *dir = indexdir->d_inode;
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300658 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
659 struct dentry *upperdentry = ovl_dentry_upper(dentry);
660 struct dentry *index = NULL;
661 struct inode *inode;
662 struct qstr name;
663 int err;
664
665 err = ovl_get_index_name(lowerdentry, &name);
666 if (err)
667 goto fail;
668
669 inode = d_inode(upperdentry);
Amir Goldstein89a17552017-09-26 07:40:37 +0300670 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300671 pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
672 upperdentry, inode->i_ino, inode->i_nlink);
673 /*
674 * We either have a bug with persistent union nlink or a lower
675 * hardlink was added while overlay is mounted. Adding a lower
676 * hardlink and then unlinking all overlay hardlinks would drop
677 * overlay nlink to zero before all upper inodes are unlinked.
678 * As a safety measure, when that situation is detected, set
679 * the overlay nlink to the index inode nlink minus one for the
680 * index entry itself.
681 */
682 set_nlink(d_inode(dentry), inode->i_nlink - 1);
683 ovl_set_nlink_upper(dentry);
684 goto out;
685 }
686
687 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300688 index = lookup_one_len(name.name, indexdir, name.len);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300689 err = PTR_ERR(index);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300690 if (IS_ERR(index)) {
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300691 index = NULL;
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300692 } else if (ovl_index_all(dentry->d_sb)) {
693 /* Whiteout orphan index to block future open by handle */
694 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
695 } else {
696 /* Cleanup orphan index entries */
697 err = ovl_cleanup(dir, index);
698 }
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300699
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300700 inode_unlock(dir);
701 if (err)
702 goto fail;
703
704out:
705 dput(index);
706 return;
707
708fail:
709 pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
710 goto out;
711}
712
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300713/*
714 * Operations that change overlay inode and upper inode nlink need to be
715 * synchronized with copy up for persistent nlink accounting.
716 */
717int ovl_nlink_start(struct dentry *dentry, bool *locked)
718{
719 struct ovl_inode *oi = OVL_I(d_inode(dentry));
720 const struct cred *old_cred;
721 int err;
722
Amir Goldstein89a17552017-09-26 07:40:37 +0300723 if (!d_inode(dentry))
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300724 return 0;
725
726 /*
727 * With inodes index is enabled, we store the union overlay nlink
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300728 * in an xattr on the index inode. When whiting out an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300729 * we need to decrement the overlay persistent nlink, but before the
730 * first copy up, we have no upper index inode to store the xattr.
731 *
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300732 * As a workaround, before whiteout/rename over an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300733 * copy up to create the upper index. Creating the upper index will
734 * initialize the overlay nlink, so it could be dropped if unlink
735 * or rename succeeds.
736 *
737 * TODO: implement metadata only index copy up when called with
738 * ovl_copy_up_flags(dentry, O_PATH).
739 */
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300740 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300741 err = ovl_copy_up(dentry);
742 if (err)
743 return err;
744 }
745
746 err = mutex_lock_interruptible(&oi->lock);
747 if (err)
748 return err;
749
Amir Goldstein89a17552017-09-26 07:40:37 +0300750 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300751 goto out;
752
753 old_cred = ovl_override_creds(dentry->d_sb);
754 /*
755 * The overlay inode nlink should be incremented/decremented IFF the
756 * upper operation succeeds, along with nlink change of upper inode.
757 * Therefore, before link/unlink/rename, we store the union nlink
758 * value relative to the upper inode nlink in an upper inode xattr.
759 */
760 err = ovl_set_nlink_upper(dentry);
761 revert_creds(old_cred);
762
763out:
764 if (err)
765 mutex_unlock(&oi->lock);
766 else
767 *locked = true;
768
769 return err;
770}
771
772void ovl_nlink_end(struct dentry *dentry, bool locked)
773{
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300774 if (locked) {
775 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
776 d_inode(dentry)->i_nlink == 0) {
777 const struct cred *old_cred;
778
779 old_cred = ovl_override_creds(dentry->d_sb);
780 ovl_cleanup_index(dentry);
781 revert_creds(old_cred);
782 }
783
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300784 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300785 }
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300786}
Amir Goldstein5820dc02017-09-25 16:39:55 +0300787
788int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
789{
790 /* Workdir should not be the same as upperdir */
791 if (workdir == upperdir)
792 goto err;
793
794 /* Workdir should not be subdir of upperdir and vice versa */
795 if (lock_rename(workdir, upperdir) != NULL)
796 goto err_unlock;
797
798 return 0;
799
800err_unlock:
801 unlock_rename(workdir, upperdir);
802err:
803 pr_err("overlayfs: failed to lock workdir+upperdir\n");
804 return -EIO;
805}
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400806
807/* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
808int ovl_check_metacopy_xattr(struct dentry *dentry)
809{
810 int res;
811
812 /* Only regular files can have metacopy xattr */
813 if (!S_ISREG(d_inode(dentry)->i_mode))
814 return 0;
815
816 res = vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0);
817 if (res < 0) {
818 if (res == -ENODATA || res == -EOPNOTSUPP)
819 return 0;
820 goto out;
821 }
822
823 return 1;
824out:
825 pr_warn_ratelimited("overlayfs: failed to get metacopy (%i)\n", res);
826 return res;
827}
Vivek Goyal67d756c22018-05-11 11:49:30 -0400828
829bool ovl_is_metacopy_dentry(struct dentry *dentry)
830{
831 struct ovl_entry *oe = dentry->d_fsdata;
832
833 if (!d_is_reg(dentry))
834 return false;
835
836 if (ovl_dentry_upper(dentry)) {
837 if (!ovl_has_upperdata(d_inode(dentry)))
838 return true;
839 return false;
840 }
841
842 return (oe->numlower > 1);
843}