blob: 22ed51f80e58937d955f326d1c278a89c080ce1f [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>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010017#include "overlayfs.h"
18#include "ovl_entry.h"
19
20int ovl_want_write(struct dentry *dentry)
21{
22 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
23 return mnt_want_write(ofs->upper_mnt);
24}
25
26void ovl_drop_write(struct dentry *dentry)
27{
28 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
29 mnt_drop_write(ofs->upper_mnt);
30}
31
32struct dentry *ovl_workdir(struct dentry *dentry)
33{
34 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
35 return ofs->workdir;
36}
37
38const struct cred *ovl_override_creds(struct super_block *sb)
39{
40 struct ovl_fs *ofs = sb->s_fs_info;
41
42 return override_creds(ofs->creator_cred);
43}
44
Amir Goldstein7bcd74b2017-03-22 08:42:21 -040045struct super_block *ovl_same_sb(struct super_block *sb)
46{
47 struct ovl_fs *ofs = sb->s_fs_info;
48
49 return ofs->same_sb;
50}
51
Amir Goldstein02bcd152017-06-21 15:28:36 +030052bool ovl_can_decode_fh(struct super_block *sb)
53{
54 return (sb->s_export_op && sb->s_export_op->fh_to_dentry &&
55 !uuid_is_null(&sb->s_uuid));
56}
57
58struct dentry *ovl_indexdir(struct super_block *sb)
59{
60 struct ovl_fs *ofs = sb->s_fs_info;
61
62 return ofs->indexdir;
63}
64
Miklos Szeredibbb1e542016-12-16 11:02:56 +010065struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
66{
67 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
68 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
69
70 if (oe)
71 oe->numlower = numlower;
72
73 return oe;
74}
75
76bool ovl_dentry_remote(struct dentry *dentry)
77{
78 return dentry->d_flags &
79 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
80 DCACHE_OP_REAL);
81}
82
83bool ovl_dentry_weird(struct dentry *dentry)
84{
85 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
86 DCACHE_MANAGE_TRANSIT |
87 DCACHE_OP_HASH |
88 DCACHE_OP_COMPARE);
89}
90
91enum ovl_path_type ovl_path_type(struct dentry *dentry)
92{
93 struct ovl_entry *oe = dentry->d_fsdata;
94 enum ovl_path_type type = 0;
95
Miklos Szeredi09d8b582017-07-04 22:03:16 +020096 if (ovl_dentry_upper(dentry)) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +010097 type = __OVL_PATH_UPPER;
98
99 /*
Amir Goldstein59548502017-04-23 23:12:34 +0300100 * Non-dir dentry can hold lower dentry of its copy up origin.
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100101 */
Amir Goldstein59548502017-04-23 23:12:34 +0300102 if (oe->numlower) {
103 type |= __OVL_PATH_ORIGIN;
104 if (d_is_dir(dentry))
105 type |= __OVL_PATH_MERGE;
106 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100107 } else {
108 if (oe->numlower > 1)
109 type |= __OVL_PATH_MERGE;
110 }
111 return type;
112}
113
114void ovl_path_upper(struct dentry *dentry, struct path *path)
115{
116 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100117
118 path->mnt = ofs->upper_mnt;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200119 path->dentry = ovl_dentry_upper(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100120}
121
122void ovl_path_lower(struct dentry *dentry, struct path *path)
123{
124 struct ovl_entry *oe = dentry->d_fsdata;
125
Kees Cook33006cd2017-03-29 14:02:19 -0700126 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100127}
128
129enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
130{
131 enum ovl_path_type type = ovl_path_type(dentry);
132
133 if (!OVL_TYPE_UPPER(type))
134 ovl_path_lower(dentry, path);
135 else
136 ovl_path_upper(dentry, path);
137
138 return type;
139}
140
141struct dentry *ovl_dentry_upper(struct dentry *dentry)
142{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200143 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100144}
145
146struct dentry *ovl_dentry_lower(struct dentry *dentry)
147{
148 struct ovl_entry *oe = dentry->d_fsdata;
149
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200150 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100151}
152
153struct dentry *ovl_dentry_real(struct dentry *dentry)
154{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200155 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100156}
157
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200158struct inode *ovl_inode_upper(struct inode *inode)
Miklos Szeredi25b77132017-07-04 22:03:16 +0200159{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200160 struct dentry *upperdentry = ovl_upperdentry_dereference(OVL_I(inode));
Miklos Szeredi25b77132017-07-04 22:03:16 +0200161
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200162 return upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200163}
164
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200165struct inode *ovl_inode_lower(struct inode *inode)
166{
167 return OVL_I(inode)->lower;
168}
169
170struct inode *ovl_inode_real(struct inode *inode)
171{
172 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
173}
174
175
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100176struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
177{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200178 return OVL_I(d_inode(dentry))->cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100179}
180
181void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
182{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200183 OVL_I(d_inode(dentry))->cache = cache;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100184}
185
186bool ovl_dentry_is_opaque(struct dentry *dentry)
187{
188 struct ovl_entry *oe = dentry->d_fsdata;
189 return oe->opaque;
190}
191
192bool ovl_dentry_is_whiteout(struct dentry *dentry)
193{
194 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
195}
196
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100197void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100198{
199 struct ovl_entry *oe = dentry->d_fsdata;
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100200
201 oe->opaque = true;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100202}
203
Miklos Szeredia6c60652016-12-16 11:02:56 +0100204bool ovl_redirect_dir(struct super_block *sb)
205{
206 struct ovl_fs *ofs = sb->s_fs_info;
207
Amir Goldstein21a22872017-05-17 00:12:41 +0300208 return ofs->config.redirect_dir && !ofs->noxattr;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100209}
210
211const char *ovl_dentry_get_redirect(struct dentry *dentry)
212{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200213 return OVL_I(d_inode(dentry))->redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100214}
215
216void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
217{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200218 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Miklos Szeredia6c60652016-12-16 11:02:56 +0100219
Miklos Szeredicf31c462017-07-04 22:03:16 +0200220 kfree(oi->redirect);
221 oi->redirect = redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100222}
223
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200224void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
225 struct dentry *lowerdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100226{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200227 if (upperdentry)
228 OVL_I(inode)->__upperdentry = upperdentry;
229 if (lowerdentry)
230 OVL_I(inode)->lower = d_inode(lowerdentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100231
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200232 ovl_copyattr(d_inode(upperdentry ?: lowerdentry), inode);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100233}
234
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200235void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100236{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200237 struct inode *upperinode = d_inode(upperdentry);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200238
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200239 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
240 WARN_ON(OVL_I(inode)->__upperdentry);
241
Miklos Szeredi25b77132017-07-04 22:03:16 +0200242 /*
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200243 * Make sure upperdentry is consistent before making it visible
Miklos Szeredi25b77132017-07-04 22:03:16 +0200244 */
245 smp_wmb();
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200246 OVL_I(inode)->__upperdentry = upperdentry;
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200247 if (!S_ISDIR(upperinode->i_mode) && inode_unhashed(inode)) {
Miklos Szeredi25b77132017-07-04 22:03:16 +0200248 inode->i_private = upperinode;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100249 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200250 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100251}
252
253void ovl_dentry_version_inc(struct dentry *dentry)
254{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200255 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100256
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200257 WARN_ON(!inode_is_locked(inode));
258 OVL_I(inode)->version++;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100259}
260
261u64 ovl_dentry_version_get(struct dentry *dentry)
262{
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200263 struct inode *inode = d_inode(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100264
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200265 WARN_ON(!inode_is_locked(inode));
266 return OVL_I(inode)->version;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100267}
268
269bool ovl_is_whiteout(struct dentry *dentry)
270{
271 struct inode *inode = dentry->d_inode;
272
273 return inode && IS_WHITEOUT(inode);
274}
275
276struct file *ovl_path_open(struct path *path, int flags)
277{
278 return dentry_open(path, flags | O_NOATIME, current_cred());
279}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200280
281int ovl_copy_up_start(struct dentry *dentry)
282{
Amir Goldsteina015daf2017-06-21 15:28:51 +0300283 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Amir Goldstein39d3d602017-01-17 06:34:56 +0200284 int err;
285
Amir Goldsteina015daf2017-06-21 15:28:51 +0300286 err = mutex_lock_interruptible(&oi->lock);
287 if (!err && ovl_dentry_upper(dentry)) {
288 err = 1; /* Already copied up */
289 mutex_unlock(&oi->lock);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200290 }
Amir Goldstein39d3d602017-01-17 06:34:56 +0200291
292 return err;
293}
294
295void ovl_copy_up_end(struct dentry *dentry)
296{
Amir Goldsteina015daf2017-06-21 15:28:51 +0300297 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200298}
Amir Goldstein82b749b2017-05-17 00:12:40 +0300299
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300300bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
301{
302 int res;
303 char val;
304
305 if (!d_is_dir(dentry))
306 return false;
307
308 res = vfs_getxattr(dentry, name, &val, 1);
309 if (res == 1 && val == 'y')
310 return true;
311
312 return false;
313}
314
Amir Goldstein82b749b2017-05-17 00:12:40 +0300315int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
316 const char *name, const void *value, size_t size,
317 int xerr)
318{
319 int err;
320 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
321
322 if (ofs->noxattr)
323 return xerr;
324
325 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
326
327 if (err == -EOPNOTSUPP) {
328 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
329 ofs->noxattr = true;
330 return xerr;
331 }
332
333 return err;
334}
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300335
336int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
337{
338 int err;
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300339
Miklos Szeredi13c72072017-07-04 22:03:16 +0200340 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300341 return 0;
342
343 /*
344 * Do not fail when upper doesn't support xattrs.
345 * Upper inodes won't have origin nor redirect xattr anyway.
346 */
347 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
348 "y", 1, 0);
349 if (!err)
Miklos Szeredi13c72072017-07-04 22:03:16 +0200350 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300351
352 return err;
353}
Miklos Szeredi13c72072017-07-04 22:03:16 +0200354
355void ovl_set_flag(unsigned long flag, struct inode *inode)
356{
357 set_bit(flag, &OVL_I(inode)->flags);
358}
359
360bool ovl_test_flag(unsigned long flag, struct inode *inode)
361{
362 return test_bit(flag, &OVL_I(inode)->flags);
363}
Amir Goldsteinad0af712017-06-21 15:28:32 +0300364
365/**
366 * Caller must hold a reference to inode to prevent it from being freed while
367 * it is marked inuse.
368 */
369bool ovl_inuse_trylock(struct dentry *dentry)
370{
371 struct inode *inode = d_inode(dentry);
372 bool locked = false;
373
374 spin_lock(&inode->i_lock);
375 if (!(inode->i_state & I_OVL_INUSE)) {
376 inode->i_state |= I_OVL_INUSE;
377 locked = true;
378 }
379 spin_unlock(&inode->i_lock);
380
381 return locked;
382}
383
384void ovl_inuse_unlock(struct dentry *dentry)
385{
386 if (dentry) {
387 struct inode *inode = d_inode(dentry);
388
389 spin_lock(&inode->i_lock);
390 WARN_ON(!(inode->i_state & I_OVL_INUSE));
391 inode->i_state &= ~I_OVL_INUSE;
392 spin_unlock(&inode->i_lock);
393 }
394}