blob: 7c01327b1852053c58feff725c3d5aac9e6b9717 [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 Goldstein9df085f2018-09-03 09:12:09 +030068 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
Amir Goldsteine487d882017-11-07 13:55:04 +020069 return 0;
70
71 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
Amir Goldstein02bcd152017-06-21 15:28:36 +030072}
73
74struct dentry *ovl_indexdir(struct super_block *sb)
75{
76 struct ovl_fs *ofs = sb->s_fs_info;
77
78 return ofs->indexdir;
79}
80
Amir Goldsteinf168f102018-01-19 11:26:53 +020081/* Index all files on copy up. For now only enabled for NFS export */
82bool ovl_index_all(struct super_block *sb)
83{
84 struct ovl_fs *ofs = sb->s_fs_info;
85
86 return ofs->config.nfs_export && ofs->config.index;
87}
88
89/* Verify lower origin on lookup. For now only enabled for NFS export */
90bool ovl_verify_lower(struct super_block *sb)
91{
92 struct ovl_fs *ofs = sb->s_fs_info;
93
94 return ofs->config.nfs_export && ofs->config.index;
95}
96
Miklos Szeredibbb1e542016-12-16 11:02:56 +010097struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
98{
99 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
100 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
101
102 if (oe)
103 oe->numlower = numlower;
104
105 return oe;
106}
107
108bool ovl_dentry_remote(struct dentry *dentry)
109{
110 return dentry->d_flags &
111 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
112 DCACHE_OP_REAL);
113}
114
115bool ovl_dentry_weird(struct dentry *dentry)
116{
117 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
118 DCACHE_MANAGE_TRANSIT |
119 DCACHE_OP_HASH |
120 DCACHE_OP_COMPARE);
121}
122
123enum ovl_path_type ovl_path_type(struct dentry *dentry)
124{
125 struct ovl_entry *oe = dentry->d_fsdata;
126 enum ovl_path_type type = 0;
127
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200128 if (ovl_dentry_upper(dentry)) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100129 type = __OVL_PATH_UPPER;
130
131 /*
Amir Goldstein59548502017-04-23 23:12:34 +0300132 * Non-dir dentry can hold lower dentry of its copy up origin.
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100133 */
Amir Goldstein59548502017-04-23 23:12:34 +0300134 if (oe->numlower) {
Vivek Goyal60124872018-05-11 11:49:32 -0400135 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
136 type |= __OVL_PATH_ORIGIN;
Vivek Goyal0b17c282018-05-11 11:49:32 -0400137 if (d_is_dir(dentry) ||
138 !ovl_has_upperdata(d_inode(dentry)))
Amir Goldstein59548502017-04-23 23:12:34 +0300139 type |= __OVL_PATH_MERGE;
140 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100141 } else {
142 if (oe->numlower > 1)
143 type |= __OVL_PATH_MERGE;
144 }
145 return type;
146}
147
148void ovl_path_upper(struct dentry *dentry, struct path *path)
149{
150 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100151
152 path->mnt = ofs->upper_mnt;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200153 path->dentry = ovl_dentry_upper(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100154}
155
156void ovl_path_lower(struct dentry *dentry, struct path *path)
157{
158 struct ovl_entry *oe = dentry->d_fsdata;
159
Chandan Rajendrab9343632017-07-24 01:57:54 -0500160 if (oe->numlower) {
161 path->mnt = oe->lowerstack[0].layer->mnt;
162 path->dentry = oe->lowerstack[0].dentry;
163 } else {
164 *path = (struct path) { };
165 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100166}
167
Vivek Goyal4f93b422018-05-11 11:49:30 -0400168void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
169{
170 struct ovl_entry *oe = dentry->d_fsdata;
171
172 if (oe->numlower) {
173 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
174 path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
175 } else {
176 *path = (struct path) { };
177 }
178}
179
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100180enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
181{
182 enum ovl_path_type type = ovl_path_type(dentry);
183
184 if (!OVL_TYPE_UPPER(type))
185 ovl_path_lower(dentry, path);
186 else
187 ovl_path_upper(dentry, path);
188
189 return type;
190}
191
192struct dentry *ovl_dentry_upper(struct dentry *dentry)
193{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200194 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100195}
196
197struct dentry *ovl_dentry_lower(struct dentry *dentry)
198{
199 struct ovl_entry *oe = dentry->d_fsdata;
200
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200201 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100202}
203
Amir Goldsteinda309e82017-11-08 19:39:51 +0200204struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
205{
206 struct ovl_entry *oe = dentry->d_fsdata;
207
208 return oe->numlower ? oe->lowerstack[0].layer : NULL;
209}
210
Vivek Goyal647d2532018-05-11 11:49:30 -0400211/*
212 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
213 * dependig on what is stored in lowerstack[0]. At times we need to find
214 * lower dentry which has data (and not metacopy dentry). This helper
215 * returns the lower data dentry.
216 */
217struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
218{
219 struct ovl_entry *oe = dentry->d_fsdata;
220
221 return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
222}
223
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100224struct dentry *ovl_dentry_real(struct dentry *dentry)
225{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200226 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100227}
228
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200229struct dentry *ovl_i_dentry_upper(struct inode *inode)
230{
231 return ovl_upperdentry_dereference(OVL_I(inode));
232}
233
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200234struct inode *ovl_inode_upper(struct inode *inode)
Miklos Szeredi25b77132017-07-04 22:03:16 +0200235{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200236 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200237
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200238 return upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200239}
240
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200241struct inode *ovl_inode_lower(struct inode *inode)
242{
243 return OVL_I(inode)->lower;
244}
245
246struct inode *ovl_inode_real(struct inode *inode)
247{
248 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
249}
250
Vivek Goyal2664bd02018-05-11 11:49:30 -0400251/* Return inode which contains lower data. Do not return metacopy */
252struct inode *ovl_inode_lowerdata(struct inode *inode)
253{
254 if (WARN_ON(!S_ISREG(inode->i_mode)))
255 return NULL;
256
257 return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
258}
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200259
Vivek Goyal4823d492018-05-11 11:49:31 -0400260/* Return real inode which contains data. Does not return metacopy inode */
261struct inode *ovl_inode_realdata(struct inode *inode)
262{
263 struct inode *upperinode;
264
265 upperinode = ovl_inode_upper(inode);
266 if (upperinode && ovl_has_upperdata(inode))
267 return upperinode;
268
269 return ovl_inode_lowerdata(inode);
270}
271
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200272struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100273{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200274 return OVL_I(inode)->cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100275}
276
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200277void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100278{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200279 OVL_I(inode)->cache = cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100280}
281
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200282void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
283{
284 set_bit(flag, &OVL_E(dentry)->flags);
285}
286
287void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
288{
289 clear_bit(flag, &OVL_E(dentry)->flags);
290}
291
292bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
293{
294 return test_bit(flag, &OVL_E(dentry)->flags);
295}
296
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100297bool ovl_dentry_is_opaque(struct dentry *dentry)
298{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200299 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100300}
301
302bool ovl_dentry_is_whiteout(struct dentry *dentry)
303{
304 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
305}
306
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100307void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100308{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200309 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100310}
311
Miklos Szeredi55acc662017-07-04 22:03:18 +0200312/*
Amir Goldsteinaa3ff3c2017-10-15 18:00:20 +0300313 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
314 * to return positive, while there's no actual upper alias for the inode.
315 * Copy up code needs to know about the existence of the upper alias, so it
316 * can't use ovl_dentry_upper().
Miklos Szeredi55acc662017-07-04 22:03:18 +0200317 */
318bool ovl_dentry_has_upper_alias(struct dentry *dentry)
319{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200320 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200321}
322
323void ovl_dentry_set_upper_alias(struct dentry *dentry)
324{
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200325 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200326}
327
Vivek Goyal0c288872018-05-11 11:49:28 -0400328static bool ovl_should_check_upperdata(struct inode *inode)
329{
330 if (!S_ISREG(inode->i_mode))
331 return false;
332
333 if (!ovl_inode_lower(inode))
334 return false;
335
336 return true;
337}
338
339bool ovl_has_upperdata(struct inode *inode)
340{
341 if (!ovl_should_check_upperdata(inode))
342 return true;
343
344 if (!ovl_test_flag(OVL_UPPERDATA, inode))
345 return false;
346 /*
347 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
348 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
349 * if setting of OVL_UPPERDATA is visible, then effects of writes
350 * before that are visible too.
351 */
352 smp_rmb();
353 return true;
354}
355
356void ovl_set_upperdata(struct inode *inode)
357{
358 /*
359 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
360 * if OVL_UPPERDATA flag is visible, then effects of write operations
361 * before it are visible as well.
362 */
363 smp_wmb();
364 ovl_set_flag(OVL_UPPERDATA, inode);
365}
366
367/* Caller should hold ovl_inode->lock */
368bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
369{
370 if (!ovl_open_flags_need_copy_up(flags))
371 return false;
372
373 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
374}
375
376bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
377{
378 if (!ovl_open_flags_need_copy_up(flags))
379 return false;
380
381 return !ovl_has_upperdata(d_inode(dentry));
382}
383
Miklos Szeredia6c60652016-12-16 11:02:56 +0100384bool ovl_redirect_dir(struct super_block *sb)
385{
386 struct ovl_fs *ofs = sb->s_fs_info;
387
Amir Goldstein21a22872017-05-17 00:12:41 +0300388 return ofs->config.redirect_dir && !ofs->noxattr;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100389}
390
391const char *ovl_dentry_get_redirect(struct dentry *dentry)
392{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200393 return OVL_I(d_inode(dentry))->redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100394}
395
396void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
397{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200398 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Miklos Szeredia6c60652016-12-16 11:02:56 +0100399
Miklos Szeredicf31c462017-07-04 22:03:16 +0200400 kfree(oi->redirect);
401 oi->redirect = redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100402}
403
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200404void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
Vivek Goyal2664bd02018-05-11 11:49:30 -0400405 struct dentry *lowerdentry, struct dentry *lowerdata)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100406{
Amir Goldstein695b46e2018-03-15 23:39:01 +0200407 struct inode *realinode = d_inode(upperdentry ?: lowerdentry);
408
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200409 if (upperdentry)
410 OVL_I(inode)->__upperdentry = upperdentry;
411 if (lowerdentry)
Amir Goldstein31747ed2018-01-14 18:35:40 +0200412 OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
Vivek Goyal2664bd02018-05-11 11:49:30 -0400413 if (lowerdata)
414 OVL_I(inode)->lowerdata = igrab(d_inode(lowerdata));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100415
Amir Goldstein695b46e2018-03-15 23:39:01 +0200416 ovl_copyattr(realinode, inode);
Miklos Szeredi4f357292018-07-18 15:44:41 +0200417 ovl_copyflags(realinode, inode);
Amir Goldstein695b46e2018-03-15 23:39:01 +0200418 if (!inode->i_ino)
419 inode->i_ino = realinode->i_ino;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100420}
421
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200422void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100423{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200424 struct inode *upperinode = d_inode(upperdentry);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200425
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200426 WARN_ON(OVL_I(inode)->__upperdentry);
427
Miklos Szeredi25b77132017-07-04 22:03:16 +0200428 /*
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200429 * Make sure upperdentry is consistent before making it visible
Miklos Szeredi25b77132017-07-04 22:03:16 +0200430 */
431 smp_wmb();
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200432 OVL_I(inode)->__upperdentry = upperdentry;
Amir Goldstein31747ed2018-01-14 18:35:40 +0200433 if (inode_unhashed(inode)) {
Amir Goldstein695b46e2018-03-15 23:39:01 +0200434 if (!inode->i_ino)
435 inode->i_ino = upperinode->i_ino;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200436 inode->i_private = upperinode;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100437 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200438 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100439}
440
Miklos Szeredid9854c82018-07-18 15:44:40 +0200441static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100442{
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));
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200446 /*
447 * Version is used by readdir code to keep cache consistent. For merge
448 * dirs all changes need to be noted. For non-merge dirs, cache only
449 * contains impure (ones which have been copied up and have origins)
450 * entries, so only need to note changes to impure entries.
451 */
452 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
453 OVL_I(inode)->version++;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100454}
455
Miklos Szeredid9854c82018-07-18 15:44:40 +0200456void ovl_dir_modified(struct dentry *dentry, bool impurity)
457{
458 /* Copy mtime/ctime */
459 ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
460
461 ovl_dentry_version_inc(dentry, impurity);
462}
463
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100464u64 ovl_dentry_version_get(struct dentry *dentry)
465{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200466 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100467
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200468 WARN_ON(!inode_is_locked(inode));
469 return OVL_I(inode)->version;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100470}
471
472bool ovl_is_whiteout(struct dentry *dentry)
473{
474 struct inode *inode = dentry->d_inode;
475
476 return inode && IS_WHITEOUT(inode);
477}
478
479struct file *ovl_path_open(struct path *path, int flags)
480{
481 return dentry_open(path, flags | O_NOATIME, current_cred());
482}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200483
Vivek Goyal0c288872018-05-11 11:49:28 -0400484/* Caller should hold ovl_inode->lock */
485static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
486{
487 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
488
489 if (ovl_dentry_upper(dentry) &&
490 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
491 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
492 return true;
493
494 return false;
495}
496
497bool ovl_already_copied_up(struct dentry *dentry, int flags)
Vivek Goyal2002df82018-05-11 11:49:28 -0400498{
499 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
500
501 /*
502 * Check if copy-up has happened as well as for upper alias (in
503 * case of hard links) is there.
504 *
505 * Both checks are lockless:
506 * - false negatives: will recheck under oi->lock
507 * - false positives:
508 * + ovl_dentry_upper() uses memory barriers to ensure the
509 * upper dentry is up-to-date
510 * + ovl_dentry_has_upper_alias() relies on locking of
511 * upper parent i_rwsem to prevent reordering copy-up
512 * with rename.
513 */
514 if (ovl_dentry_upper(dentry) &&
Vivek Goyal0c288872018-05-11 11:49:28 -0400515 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
516 !ovl_dentry_needs_data_copy_up(dentry, flags))
Vivek Goyal2002df82018-05-11 11:49:28 -0400517 return true;
518
519 return false;
520}
521
Vivek Goyal0c288872018-05-11 11:49:28 -0400522int ovl_copy_up_start(struct dentry *dentry, int flags)
Amir Goldstein39d3d602017-01-17 06:34:56 +0200523{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300524 struct inode *inode = d_inode(dentry);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200525 int err;
526
Amir Goldstein1e92e302018-10-18 18:37:14 +0300527 err = ovl_inode_lock(inode);
Vivek Goyal0c288872018-05-11 11:49:28 -0400528 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
Amir Goldsteina015daf2017-06-21 15:28:51 +0300529 err = 1; /* Already copied up */
Amir Goldstein1e92e302018-10-18 18:37:14 +0300530 ovl_inode_unlock(inode);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200531 }
Amir Goldstein39d3d602017-01-17 06:34:56 +0200532
533 return err;
534}
535
536void ovl_copy_up_end(struct dentry *dentry)
537{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300538 ovl_inode_unlock(d_inode(dentry));
Amir Goldstein39d3d602017-01-17 06:34:56 +0200539}
Amir Goldstein82b749b2017-05-17 00:12:40 +0300540
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300541bool ovl_check_origin_xattr(struct dentry *dentry)
542{
543 int res;
544
545 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
546
547 /* Zero size value means "copied up but origin unknown" */
548 if (res >= 0)
549 return true;
550
551 return false;
552}
553
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300554bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
555{
556 int res;
557 char val;
558
559 if (!d_is_dir(dentry))
560 return false;
561
562 res = vfs_getxattr(dentry, name, &val, 1);
563 if (res == 1 && val == 'y')
564 return true;
565
566 return false;
567}
568
Amir Goldstein82b749b2017-05-17 00:12:40 +0300569int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
570 const char *name, const void *value, size_t size,
571 int xerr)
572{
573 int err;
574 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
575
576 if (ofs->noxattr)
577 return xerr;
578
579 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
580
581 if (err == -EOPNOTSUPP) {
582 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
583 ofs->noxattr = true;
584 return xerr;
585 }
586
587 return err;
588}
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300589
590int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
591{
592 int err;
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300593
Miklos Szeredi13c72072017-07-04 22:03:16 +0200594 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300595 return 0;
596
597 /*
598 * Do not fail when upper doesn't support xattrs.
599 * Upper inodes won't have origin nor redirect xattr anyway.
600 */
601 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
602 "y", 1, 0);
603 if (!err)
Miklos Szeredi13c72072017-07-04 22:03:16 +0200604 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300605
606 return err;
607}
Miklos Szeredi13c72072017-07-04 22:03:16 +0200608
609void ovl_set_flag(unsigned long flag, struct inode *inode)
610{
611 set_bit(flag, &OVL_I(inode)->flags);
612}
613
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200614void ovl_clear_flag(unsigned long flag, struct inode *inode)
615{
616 clear_bit(flag, &OVL_I(inode)->flags);
617}
618
Miklos Szeredi13c72072017-07-04 22:03:16 +0200619bool ovl_test_flag(unsigned long flag, struct inode *inode)
620{
621 return test_bit(flag, &OVL_I(inode)->flags);
622}
Amir Goldsteinad0af712017-06-21 15:28:32 +0300623
624/**
625 * Caller must hold a reference to inode to prevent it from being freed while
626 * it is marked inuse.
627 */
628bool ovl_inuse_trylock(struct dentry *dentry)
629{
630 struct inode *inode = d_inode(dentry);
631 bool locked = false;
632
633 spin_lock(&inode->i_lock);
634 if (!(inode->i_state & I_OVL_INUSE)) {
635 inode->i_state |= I_OVL_INUSE;
636 locked = true;
637 }
638 spin_unlock(&inode->i_lock);
639
640 return locked;
641}
642
643void ovl_inuse_unlock(struct dentry *dentry)
644{
645 if (dentry) {
646 struct inode *inode = d_inode(dentry);
647
648 spin_lock(&inode->i_lock);
649 WARN_ON(!(inode->i_state & I_OVL_INUSE));
650 inode->i_state &= ~I_OVL_INUSE;
651 spin_unlock(&inode->i_lock);
652 }
653}
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300654
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300655/*
656 * Does this overlay dentry need to be indexed on copy up?
657 */
658bool ovl_need_index(struct dentry *dentry)
659{
660 struct dentry *lower = ovl_dentry_lower(dentry);
661
662 if (!lower || !ovl_indexdir(dentry->d_sb))
663 return false;
664
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200665 /* Index all files for NFS export and consistency verification */
Amir Goldstein016b7202018-01-11 14:01:08 +0200666 if (ovl_index_all(dentry->d_sb))
Amir Goldsteinfbd2d202017-11-22 00:08:21 +0200667 return true;
668
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300669 /* Index only lower hardlinks on copy up */
670 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
671 return true;
672
673 return false;
674}
675
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300676/* Caller must hold OVL_I(inode)->lock */
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300677static void ovl_cleanup_index(struct dentry *dentry)
678{
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300679 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
680 struct inode *dir = indexdir->d_inode;
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300681 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
682 struct dentry *upperdentry = ovl_dentry_upper(dentry);
683 struct dentry *index = NULL;
684 struct inode *inode;
Amir Goldstein63e13252018-09-18 16:34:31 +0300685 struct qstr name = { };
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300686 int err;
687
688 err = ovl_get_index_name(lowerdentry, &name);
689 if (err)
690 goto fail;
691
692 inode = d_inode(upperdentry);
Amir Goldstein89a17552017-09-26 07:40:37 +0300693 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300694 pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
695 upperdentry, inode->i_ino, inode->i_nlink);
696 /*
697 * We either have a bug with persistent union nlink or a lower
698 * hardlink was added while overlay is mounted. Adding a lower
699 * hardlink and then unlinking all overlay hardlinks would drop
700 * overlay nlink to zero before all upper inodes are unlinked.
701 * As a safety measure, when that situation is detected, set
702 * the overlay nlink to the index inode nlink minus one for the
703 * index entry itself.
704 */
705 set_nlink(d_inode(dentry), inode->i_nlink - 1);
706 ovl_set_nlink_upper(dentry);
707 goto out;
708 }
709
710 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300711 index = lookup_one_len(name.name, indexdir, name.len);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300712 err = PTR_ERR(index);
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300713 if (IS_ERR(index)) {
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300714 index = NULL;
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300715 } else if (ovl_index_all(dentry->d_sb)) {
716 /* Whiteout orphan index to block future open by handle */
717 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
718 } else {
719 /* Cleanup orphan index entries */
720 err = ovl_cleanup(dir, index);
721 }
Amir Goldstein9f4ec902017-09-24 17:36:26 +0300722
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300723 inode_unlock(dir);
724 if (err)
725 goto fail;
726
727out:
Amir Goldstein63e13252018-09-18 16:34:31 +0300728 kfree(name.name);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300729 dput(index);
730 return;
731
732fail:
733 pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
734 goto out;
735}
736
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300737/*
738 * Operations that change overlay inode and upper inode nlink need to be
739 * synchronized with copy up for persistent nlink accounting.
740 */
Amir Goldstein0e329922018-10-18 18:37:13 +0300741int ovl_nlink_start(struct dentry *dentry)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300742{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300743 struct inode *inode = d_inode(dentry);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300744 const struct cred *old_cred;
745 int err;
746
Amir Goldstein1e92e302018-10-18 18:37:14 +0300747 if (WARN_ON(!inode))
Amir Goldstein0e329922018-10-18 18:37:13 +0300748 return -ENOENT;
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300749
750 /*
751 * With inodes index is enabled, we store the union overlay nlink
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300752 * in an xattr on the index inode. When whiting out an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300753 * we need to decrement the overlay persistent nlink, but before the
754 * first copy up, we have no upper index inode to store the xattr.
755 *
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300756 * As a workaround, before whiteout/rename over an indexed lower,
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300757 * copy up to create the upper index. Creating the upper index will
758 * initialize the overlay nlink, so it could be dropped if unlink
759 * or rename succeeds.
760 *
761 * TODO: implement metadata only index copy up when called with
762 * ovl_copy_up_flags(dentry, O_PATH).
763 */
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300764 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300765 err = ovl_copy_up(dentry);
766 if (err)
767 return err;
768 }
769
Amir Goldstein1e92e302018-10-18 18:37:14 +0300770 err = ovl_inode_lock(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300771 if (err)
772 return err;
773
Amir Goldstein1e92e302018-10-18 18:37:14 +0300774 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300775 goto out;
776
777 old_cred = ovl_override_creds(dentry->d_sb);
778 /*
779 * The overlay inode nlink should be incremented/decremented IFF the
780 * upper operation succeeds, along with nlink change of upper inode.
781 * Therefore, before link/unlink/rename, we store the union nlink
782 * value relative to the upper inode nlink in an upper inode xattr.
783 */
784 err = ovl_set_nlink_upper(dentry);
785 revert_creds(old_cred);
786
787out:
788 if (err)
Amir Goldstein1e92e302018-10-18 18:37:14 +0300789 ovl_inode_unlock(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300790
791 return err;
792}
793
Amir Goldstein0e329922018-10-18 18:37:13 +0300794void ovl_nlink_end(struct dentry *dentry)
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300795{
Amir Goldstein1e92e302018-10-18 18:37:14 +0300796 struct inode *inode = d_inode(dentry);
797
798 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
Amir Goldstein0e329922018-10-18 18:37:13 +0300799 const struct cred *old_cred;
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300800
Amir Goldstein0e329922018-10-18 18:37:13 +0300801 old_cred = ovl_override_creds(dentry->d_sb);
802 ovl_cleanup_index(dentry);
803 revert_creds(old_cred);
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300804 }
Amir Goldstein0e329922018-10-18 18:37:13 +0300805
Amir Goldstein1e92e302018-10-18 18:37:14 +0300806 ovl_inode_unlock(inode);
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300807}
Amir Goldstein5820dc02017-09-25 16:39:55 +0300808
809int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
810{
811 /* Workdir should not be the same as upperdir */
812 if (workdir == upperdir)
813 goto err;
814
815 /* Workdir should not be subdir of upperdir and vice versa */
816 if (lock_rename(workdir, upperdir) != NULL)
817 goto err_unlock;
818
819 return 0;
820
821err_unlock:
822 unlock_rename(workdir, upperdir);
823err:
824 pr_err("overlayfs: failed to lock workdir+upperdir\n");
825 return -EIO;
826}
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400827
828/* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
829int ovl_check_metacopy_xattr(struct dentry *dentry)
830{
831 int res;
832
833 /* Only regular files can have metacopy xattr */
834 if (!S_ISREG(d_inode(dentry)->i_mode))
835 return 0;
836
837 res = vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0);
838 if (res < 0) {
839 if (res == -ENODATA || res == -EOPNOTSUPP)
840 return 0;
841 goto out;
842 }
843
844 return 1;
845out:
846 pr_warn_ratelimited("overlayfs: failed to get metacopy (%i)\n", res);
847 return res;
848}
Vivek Goyal67d756c22018-05-11 11:49:30 -0400849
850bool ovl_is_metacopy_dentry(struct dentry *dentry)
851{
852 struct ovl_entry *oe = dentry->d_fsdata;
853
854 if (!d_is_reg(dentry))
855 return false;
856
857 if (ovl_dentry_upper(dentry)) {
858 if (!ovl_has_upperdata(d_inode(dentry)))
859 return true;
860 return false;
861 }
862
863 return (oe->numlower > 1);
864}
Vivek Goyal0a2d0d32018-05-11 11:49:32 -0400865
866char *ovl_get_redirect_xattr(struct dentry *dentry, int padding)
867{
868 int res;
869 char *s, *next, *buf = NULL;
870
871 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
872 if (res < 0) {
873 if (res == -ENODATA || res == -EOPNOTSUPP)
874 return NULL;
875 goto fail;
876 }
877
878 buf = kzalloc(res + padding + 1, GFP_KERNEL);
879 if (!buf)
880 return ERR_PTR(-ENOMEM);
881
882 if (res == 0)
883 goto invalid;
884
885 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
886 if (res < 0)
887 goto fail;
888 if (res == 0)
889 goto invalid;
890
891 if (buf[0] == '/') {
892 for (s = buf; *s++ == '/'; s = next) {
893 next = strchrnul(s, '/');
894 if (s == next)
895 goto invalid;
896 }
897 } else {
898 if (strchr(buf, '/') != NULL)
899 goto invalid;
900 }
901
902 return buf;
903
904err_free:
905 kfree(buf);
906 return ERR_PTR(res);
907fail:
908 pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
909 goto err_free;
910invalid:
911 pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
912 res = -EINVAL;
913 goto err_free;
914}