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 (ovl_dentry_upper(dentry
)) {
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
;
103 path
->mnt
= ofs
->upper_mnt
;
104 path
->dentry
= ovl_dentry_upper(dentry
);
107 void ovl_path_lower(struct dentry
*dentry
, struct path
*path
)
109 struct ovl_entry
*oe
= dentry
->d_fsdata
;
111 *path
= oe
->numlower
? oe
->lowerstack
[0] : (struct path
) { };
114 enum ovl_path_type
ovl_path_real(struct dentry
*dentry
, struct path
*path
)
116 enum ovl_path_type type
= ovl_path_type(dentry
);
118 if (!OVL_TYPE_UPPER(type
))
119 ovl_path_lower(dentry
, path
);
121 ovl_path_upper(dentry
, path
);
126 struct dentry
*ovl_dentry_upper(struct dentry
*dentry
)
128 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry
)));
131 struct dentry
*ovl_dentry_lower(struct dentry
*dentry
)
133 struct ovl_entry
*oe
= dentry
->d_fsdata
;
135 return oe
->numlower
? oe
->lowerstack
[0].dentry
: NULL
;
138 struct dentry
*ovl_dentry_real(struct dentry
*dentry
)
140 return ovl_dentry_upper(dentry
) ?: ovl_dentry_lower(dentry
);
143 struct inode
*ovl_inode_upper(struct inode
*inode
)
145 struct dentry
*upperdentry
= ovl_upperdentry_dereference(OVL_I(inode
));
147 return upperdentry
? d_inode(upperdentry
) : NULL
;
150 struct inode
*ovl_inode_lower(struct inode
*inode
)
152 return OVL_I(inode
)->lower
;
155 struct inode
*ovl_inode_real(struct inode
*inode
)
157 return ovl_inode_upper(inode
) ?: ovl_inode_lower(inode
);
161 struct ovl_dir_cache
*ovl_dir_cache(struct dentry
*dentry
)
163 return OVL_I(d_inode(dentry
))->cache
;
166 void ovl_set_dir_cache(struct dentry
*dentry
, struct ovl_dir_cache
*cache
)
168 OVL_I(d_inode(dentry
))->cache
= cache
;
171 bool ovl_dentry_is_opaque(struct dentry
*dentry
)
173 struct ovl_entry
*oe
= dentry
->d_fsdata
;
177 bool ovl_dentry_is_whiteout(struct dentry
*dentry
)
179 return !dentry
->d_inode
&& ovl_dentry_is_opaque(dentry
);
182 void ovl_dentry_set_opaque(struct dentry
*dentry
)
184 struct ovl_entry
*oe
= dentry
->d_fsdata
;
189 bool ovl_redirect_dir(struct super_block
*sb
)
191 struct ovl_fs
*ofs
= sb
->s_fs_info
;
193 return ofs
->config
.redirect_dir
&& !ofs
->noxattr
;
196 const char *ovl_dentry_get_redirect(struct dentry
*dentry
)
198 return OVL_I(d_inode(dentry
))->redirect
;
201 void ovl_dentry_set_redirect(struct dentry
*dentry
, const char *redirect
)
203 struct ovl_inode
*oi
= OVL_I(d_inode(dentry
));
206 oi
->redirect
= redirect
;
209 void ovl_inode_init(struct inode
*inode
, struct dentry
*upperdentry
,
210 struct dentry
*lowerdentry
)
213 OVL_I(inode
)->__upperdentry
= upperdentry
;
215 OVL_I(inode
)->lower
= d_inode(lowerdentry
);
217 ovl_copyattr(d_inode(upperdentry
?: lowerdentry
), inode
);
220 void ovl_inode_update(struct inode
*inode
, struct dentry
*upperdentry
)
222 struct inode
*upperinode
= d_inode(upperdentry
);
224 WARN_ON(!inode_unhashed(inode
));
225 WARN_ON(!inode_is_locked(upperdentry
->d_parent
->d_inode
));
226 WARN_ON(OVL_I(inode
)->__upperdentry
);
229 * Make sure upperdentry is consistent before making it visible
232 OVL_I(inode
)->__upperdentry
= upperdentry
;
233 if (!S_ISDIR(upperinode
->i_mode
)) {
234 inode
->i_private
= upperinode
;
235 __insert_inode_hash(inode
, (unsigned long) upperinode
);
239 void ovl_dentry_version_inc(struct dentry
*dentry
)
241 struct inode
*inode
= d_inode(dentry
);
243 WARN_ON(!inode_is_locked(inode
));
244 OVL_I(inode
)->version
++;
247 u64
ovl_dentry_version_get(struct dentry
*dentry
)
249 struct inode
*inode
= d_inode(dentry
);
251 WARN_ON(!inode_is_locked(inode
));
252 return OVL_I(inode
)->version
;
255 bool ovl_is_whiteout(struct dentry
*dentry
)
257 struct inode
*inode
= dentry
->d_inode
;
259 return inode
&& IS_WHITEOUT(inode
);
262 struct file
*ovl_path_open(struct path
*path
, int flags
)
264 return dentry_open(path
, flags
| O_NOATIME
, current_cred());
267 int ovl_copy_up_start(struct dentry
*dentry
)
269 struct ovl_inode
*oi
= OVL_I(d_inode(dentry
));
272 err
= mutex_lock_interruptible(&oi
->lock
);
273 if (!err
&& ovl_dentry_upper(dentry
)) {
274 err
= 1; /* Already copied up */
275 mutex_unlock(&oi
->lock
);
281 void ovl_copy_up_end(struct dentry
*dentry
)
283 mutex_unlock(&OVL_I(d_inode(dentry
))->lock
);
286 bool ovl_check_dir_xattr(struct dentry
*dentry
, const char *name
)
291 if (!d_is_dir(dentry
))
294 res
= vfs_getxattr(dentry
, name
, &val
, 1);
295 if (res
== 1 && val
== 'y')
301 int ovl_check_setxattr(struct dentry
*dentry
, struct dentry
*upperdentry
,
302 const char *name
, const void *value
, size_t size
,
306 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
311 err
= ovl_do_setxattr(upperdentry
, name
, value
, size
, 0);
313 if (err
== -EOPNOTSUPP
) {
314 pr_warn("overlayfs: cannot set %s xattr on upper\n", name
);
322 int ovl_set_impure(struct dentry
*dentry
, struct dentry
*upperdentry
)
326 if (ovl_test_flag(OVL_IMPURE
, d_inode(dentry
)))
330 * Do not fail when upper doesn't support xattrs.
331 * Upper inodes won't have origin nor redirect xattr anyway.
333 err
= ovl_check_setxattr(dentry
, upperdentry
, OVL_XATTR_IMPURE
,
336 ovl_set_flag(OVL_IMPURE
, d_inode(dentry
));
341 void ovl_set_flag(unsigned long flag
, struct inode
*inode
)
343 set_bit(flag
, &OVL_I(inode
)->flags
);
346 bool ovl_test_flag(unsigned long flag
, struct inode
*inode
)
348 return test_bit(flag
, &OVL_I(inode
)->flags
);