2 * Copyright (C) 2011 Novell Inc.
3 * Copyright (C) 2016 Red Hat, Inc.
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.
11 #include <linux/mount.h>
12 #include <linux/slab.h>
13 #include <linux/cred.h>
14 #include <linux/xattr.h>
15 #include "overlayfs.h"
16 #include "ovl_entry.h"
18 int ovl_want_write(struct dentry
*dentry
)
20 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
21 return mnt_want_write(ofs
->upper_mnt
);
24 void ovl_drop_write(struct dentry
*dentry
)
26 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
27 mnt_drop_write(ofs
->upper_mnt
);
30 struct dentry
*ovl_workdir(struct dentry
*dentry
)
32 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
36 const struct cred
*ovl_override_creds(struct super_block
*sb
)
38 struct ovl_fs
*ofs
= sb
->s_fs_info
;
40 return override_creds(ofs
->creator_cred
);
43 struct super_block
*ovl_same_sb(struct super_block
*sb
)
45 struct ovl_fs
*ofs
= sb
->s_fs_info
;
50 struct ovl_entry
*ovl_alloc_entry(unsigned int numlower
)
52 size_t size
= offsetof(struct ovl_entry
, lowerstack
[numlower
]);
53 struct ovl_entry
*oe
= kzalloc(size
, GFP_KERNEL
);
56 oe
->numlower
= numlower
;
61 bool ovl_dentry_remote(struct dentry
*dentry
)
63 return dentry
->d_flags
&
64 (DCACHE_OP_REVALIDATE
| DCACHE_OP_WEAK_REVALIDATE
|
68 bool ovl_dentry_weird(struct dentry
*dentry
)
70 return dentry
->d_flags
& (DCACHE_NEED_AUTOMOUNT
|
71 DCACHE_MANAGE_TRANSIT
|
76 enum ovl_path_type
ovl_path_type(struct dentry
*dentry
)
78 struct ovl_entry
*oe
= dentry
->d_fsdata
;
79 enum ovl_path_type type
= 0;
81 if (oe
->__upperdentry
) {
82 type
= __OVL_PATH_UPPER
;
85 * Non-dir dentry can hold lower dentry of its copy up origin.
88 type
|= __OVL_PATH_ORIGIN
;
90 type
|= __OVL_PATH_MERGE
;
94 type
|= __OVL_PATH_MERGE
;
99 void ovl_path_upper(struct dentry
*dentry
, struct path
*path
)
101 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
102 struct ovl_entry
*oe
= dentry
->d_fsdata
;
104 path
->mnt
= ofs
->upper_mnt
;
105 path
->dentry
= ovl_upperdentry_dereference(oe
);
108 void ovl_path_lower(struct dentry
*dentry
, struct path
*path
)
110 struct ovl_entry
*oe
= dentry
->d_fsdata
;
112 *path
= oe
->numlower
? oe
->lowerstack
[0] : (struct path
) { };
115 enum ovl_path_type
ovl_path_real(struct dentry
*dentry
, struct path
*path
)
117 enum ovl_path_type type
= ovl_path_type(dentry
);
119 if (!OVL_TYPE_UPPER(type
))
120 ovl_path_lower(dentry
, path
);
122 ovl_path_upper(dentry
, path
);
127 struct dentry
*ovl_dentry_upper(struct dentry
*dentry
)
129 struct ovl_entry
*oe
= dentry
->d_fsdata
;
131 return ovl_upperdentry_dereference(oe
);
134 static struct dentry
*__ovl_dentry_lower(struct ovl_entry
*oe
)
136 return oe
->numlower
? oe
->lowerstack
[0].dentry
: NULL
;
139 struct dentry
*ovl_dentry_lower(struct dentry
*dentry
)
141 struct ovl_entry
*oe
= dentry
->d_fsdata
;
143 return __ovl_dentry_lower(oe
);
146 struct dentry
*ovl_dentry_real(struct dentry
*dentry
)
148 struct ovl_entry
*oe
= dentry
->d_fsdata
;
149 struct dentry
*realdentry
;
151 realdentry
= ovl_upperdentry_dereference(oe
);
153 realdentry
= __ovl_dentry_lower(oe
);
158 struct ovl_dir_cache
*ovl_dir_cache(struct dentry
*dentry
)
160 struct ovl_entry
*oe
= dentry
->d_fsdata
;
165 void ovl_set_dir_cache(struct dentry
*dentry
, struct ovl_dir_cache
*cache
)
167 struct ovl_entry
*oe
= dentry
->d_fsdata
;
172 bool ovl_dentry_is_opaque(struct dentry
*dentry
)
174 struct ovl_entry
*oe
= dentry
->d_fsdata
;
178 bool ovl_dentry_is_impure(struct dentry
*dentry
)
180 struct ovl_entry
*oe
= dentry
->d_fsdata
;
185 bool ovl_dentry_is_whiteout(struct dentry
*dentry
)
187 return !dentry
->d_inode
&& ovl_dentry_is_opaque(dentry
);
190 void ovl_dentry_set_opaque(struct dentry
*dentry
)
192 struct ovl_entry
*oe
= dentry
->d_fsdata
;
197 bool ovl_redirect_dir(struct super_block
*sb
)
199 struct ovl_fs
*ofs
= sb
->s_fs_info
;
201 return ofs
->config
.redirect_dir
&& !ofs
->noxattr
;
204 const char *ovl_dentry_get_redirect(struct dentry
*dentry
)
206 struct ovl_entry
*oe
= dentry
->d_fsdata
;
211 void ovl_dentry_set_redirect(struct dentry
*dentry
, const char *redirect
)
213 struct ovl_entry
*oe
= dentry
->d_fsdata
;
216 oe
->redirect
= redirect
;
219 void ovl_dentry_update(struct dentry
*dentry
, struct dentry
*upperdentry
)
221 struct ovl_entry
*oe
= dentry
->d_fsdata
;
223 WARN_ON(!inode_is_locked(upperdentry
->d_parent
->d_inode
));
224 WARN_ON(oe
->__upperdentry
);
226 * Make sure upperdentry is consistent before making it visible to
227 * ovl_upperdentry_dereference().
230 oe
->__upperdentry
= upperdentry
;
233 void ovl_inode_init(struct inode
*inode
, struct inode
*realinode
, bool is_upper
)
235 WRITE_ONCE(inode
->i_private
, (unsigned long) realinode
|
236 (is_upper
? OVL_ISUPPER_MASK
: 0));
239 void ovl_inode_update(struct inode
*inode
, struct inode
*upperinode
)
241 WARN_ON(!upperinode
);
242 WARN_ON(!inode_unhashed(inode
));
243 WRITE_ONCE(inode
->i_private
,
244 (unsigned long) upperinode
| OVL_ISUPPER_MASK
);
245 if (!S_ISDIR(upperinode
->i_mode
))
246 __insert_inode_hash(inode
, (unsigned long) upperinode
);
249 void ovl_dentry_version_inc(struct dentry
*dentry
)
251 struct ovl_entry
*oe
= dentry
->d_fsdata
;
253 WARN_ON(!inode_is_locked(dentry
->d_inode
));
257 u64
ovl_dentry_version_get(struct dentry
*dentry
)
259 struct ovl_entry
*oe
= dentry
->d_fsdata
;
261 WARN_ON(!inode_is_locked(dentry
->d_inode
));
265 bool ovl_is_whiteout(struct dentry
*dentry
)
267 struct inode
*inode
= dentry
->d_inode
;
269 return inode
&& IS_WHITEOUT(inode
);
272 struct file
*ovl_path_open(struct path
*path
, int flags
)
274 return dentry_open(path
, flags
| O_NOATIME
, current_cred());
277 int ovl_copy_up_start(struct dentry
*dentry
)
279 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
280 struct ovl_entry
*oe
= dentry
->d_fsdata
;
283 spin_lock(&ofs
->copyup_wq
.lock
);
284 err
= wait_event_interruptible_locked(ofs
->copyup_wq
, !oe
->copying
);
286 if (oe
->__upperdentry
)
287 err
= 1; /* Already copied up */
291 spin_unlock(&ofs
->copyup_wq
.lock
);
296 void ovl_copy_up_end(struct dentry
*dentry
)
298 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
299 struct ovl_entry
*oe
= dentry
->d_fsdata
;
301 spin_lock(&ofs
->copyup_wq
.lock
);
303 wake_up_locked(&ofs
->copyup_wq
);
304 spin_unlock(&ofs
->copyup_wq
.lock
);
307 bool ovl_check_dir_xattr(struct dentry
*dentry
, const char *name
)
312 if (!d_is_dir(dentry
))
315 res
= vfs_getxattr(dentry
, name
, &val
, 1);
316 if (res
== 1 && val
== 'y')
322 int ovl_check_setxattr(struct dentry
*dentry
, struct dentry
*upperdentry
,
323 const char *name
, const void *value
, size_t size
,
327 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
332 err
= ovl_do_setxattr(upperdentry
, name
, value
, size
, 0);
334 if (err
== -EOPNOTSUPP
) {
335 pr_warn("overlayfs: cannot set %s xattr on upper\n", name
);
343 int ovl_set_impure(struct dentry
*dentry
, struct dentry
*upperdentry
)
346 struct ovl_entry
*oe
= dentry
->d_fsdata
;
352 * Do not fail when upper doesn't support xattrs.
353 * Upper inodes won't have origin nor redirect xattr anyway.
355 err
= ovl_check_setxattr(dentry
, upperdentry
, OVL_XATTR_IMPURE
,