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 <linux/exportfs.h>
16 #include <linux/uuid.h>
17 #include "overlayfs.h"
18 #include "ovl_entry.h"
20 int ovl_want_write(struct dentry
*dentry
)
22 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
23 return mnt_want_write(ofs
->upper_mnt
);
26 void ovl_drop_write(struct dentry
*dentry
)
28 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
29 mnt_drop_write(ofs
->upper_mnt
);
32 struct dentry
*ovl_workdir(struct dentry
*dentry
)
34 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
38 const struct cred
*ovl_override_creds(struct super_block
*sb
)
40 struct ovl_fs
*ofs
= sb
->s_fs_info
;
42 return override_creds(ofs
->creator_cred
);
45 struct super_block
*ovl_same_sb(struct super_block
*sb
)
47 struct ovl_fs
*ofs
= sb
->s_fs_info
;
52 bool ovl_can_decode_fh(struct super_block
*sb
)
54 return (sb
->s_export_op
&& sb
->s_export_op
->fh_to_dentry
&&
55 !uuid_is_null(&sb
->s_uuid
));
58 struct dentry
*ovl_indexdir(struct super_block
*sb
)
60 struct ovl_fs
*ofs
= sb
->s_fs_info
;
65 struct ovl_entry
*ovl_alloc_entry(unsigned int numlower
)
67 size_t size
= offsetof(struct ovl_entry
, lowerstack
[numlower
]);
68 struct ovl_entry
*oe
= kzalloc(size
, GFP_KERNEL
);
71 oe
->numlower
= numlower
;
76 bool ovl_dentry_remote(struct dentry
*dentry
)
78 return dentry
->d_flags
&
79 (DCACHE_OP_REVALIDATE
| DCACHE_OP_WEAK_REVALIDATE
|
83 bool ovl_dentry_weird(struct dentry
*dentry
)
85 return dentry
->d_flags
& (DCACHE_NEED_AUTOMOUNT
|
86 DCACHE_MANAGE_TRANSIT
|
91 enum ovl_path_type
ovl_path_type(struct dentry
*dentry
)
93 struct ovl_entry
*oe
= dentry
->d_fsdata
;
94 enum ovl_path_type type
= 0;
96 if (ovl_dentry_upper(dentry
)) {
97 type
= __OVL_PATH_UPPER
;
100 * Non-dir dentry can hold lower dentry of its copy up origin.
103 type
|= __OVL_PATH_ORIGIN
;
104 if (d_is_dir(dentry
))
105 type
|= __OVL_PATH_MERGE
;
108 if (oe
->numlower
> 1)
109 type
|= __OVL_PATH_MERGE
;
114 void ovl_path_upper(struct dentry
*dentry
, struct path
*path
)
116 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
118 path
->mnt
= ofs
->upper_mnt
;
119 path
->dentry
= ovl_dentry_upper(dentry
);
122 void ovl_path_lower(struct dentry
*dentry
, struct path
*path
)
124 struct ovl_entry
*oe
= dentry
->d_fsdata
;
126 *path
= oe
->numlower
? oe
->lowerstack
[0] : (struct path
) { };
129 enum ovl_path_type
ovl_path_real(struct dentry
*dentry
, struct path
*path
)
131 enum ovl_path_type type
= ovl_path_type(dentry
);
133 if (!OVL_TYPE_UPPER(type
))
134 ovl_path_lower(dentry
, path
);
136 ovl_path_upper(dentry
, path
);
141 struct dentry
*ovl_dentry_upper(struct dentry
*dentry
)
143 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry
)));
146 struct dentry
*ovl_dentry_lower(struct dentry
*dentry
)
148 struct ovl_entry
*oe
= dentry
->d_fsdata
;
150 return oe
->numlower
? oe
->lowerstack
[0].dentry
: NULL
;
153 struct dentry
*ovl_dentry_real(struct dentry
*dentry
)
155 return ovl_dentry_upper(dentry
) ?: ovl_dentry_lower(dentry
);
158 struct inode
*ovl_inode_upper(struct inode
*inode
)
160 struct dentry
*upperdentry
= ovl_upperdentry_dereference(OVL_I(inode
));
162 return upperdentry
? d_inode(upperdentry
) : NULL
;
165 struct inode
*ovl_inode_lower(struct inode
*inode
)
167 return OVL_I(inode
)->lower
;
170 struct inode
*ovl_inode_real(struct inode
*inode
)
172 return ovl_inode_upper(inode
) ?: ovl_inode_lower(inode
);
176 struct ovl_dir_cache
*ovl_dir_cache(struct dentry
*dentry
)
178 return OVL_I(d_inode(dentry
))->cache
;
181 void ovl_set_dir_cache(struct dentry
*dentry
, struct ovl_dir_cache
*cache
)
183 OVL_I(d_inode(dentry
))->cache
= cache
;
186 bool ovl_dentry_is_opaque(struct dentry
*dentry
)
188 struct ovl_entry
*oe
= dentry
->d_fsdata
;
192 bool ovl_dentry_is_whiteout(struct dentry
*dentry
)
194 return !dentry
->d_inode
&& ovl_dentry_is_opaque(dentry
);
197 void ovl_dentry_set_opaque(struct dentry
*dentry
)
199 struct ovl_entry
*oe
= dentry
->d_fsdata
;
204 bool ovl_redirect_dir(struct super_block
*sb
)
206 struct ovl_fs
*ofs
= sb
->s_fs_info
;
208 return ofs
->config
.redirect_dir
&& !ofs
->noxattr
;
211 const char *ovl_dentry_get_redirect(struct dentry
*dentry
)
213 return OVL_I(d_inode(dentry
))->redirect
;
216 void ovl_dentry_set_redirect(struct dentry
*dentry
, const char *redirect
)
218 struct ovl_inode
*oi
= OVL_I(d_inode(dentry
));
221 oi
->redirect
= redirect
;
224 void ovl_inode_init(struct inode
*inode
, struct dentry
*upperdentry
,
225 struct dentry
*lowerdentry
)
228 OVL_I(inode
)->__upperdentry
= upperdentry
;
230 OVL_I(inode
)->lower
= d_inode(lowerdentry
);
232 ovl_copyattr(d_inode(upperdentry
?: lowerdentry
), inode
);
235 void ovl_inode_update(struct inode
*inode
, struct dentry
*upperdentry
)
237 struct inode
*upperinode
= d_inode(upperdentry
);
239 WARN_ON(OVL_I(inode
)->__upperdentry
);
242 * Make sure upperdentry is consistent before making it visible
245 OVL_I(inode
)->__upperdentry
= upperdentry
;
246 if (!S_ISDIR(upperinode
->i_mode
) && inode_unhashed(inode
)) {
247 inode
->i_private
= upperinode
;
248 __insert_inode_hash(inode
, (unsigned long) upperinode
);
252 void ovl_dentry_version_inc(struct dentry
*dentry
)
254 struct inode
*inode
= d_inode(dentry
);
256 WARN_ON(!inode_is_locked(inode
));
257 OVL_I(inode
)->version
++;
260 u64
ovl_dentry_version_get(struct dentry
*dentry
)
262 struct inode
*inode
= d_inode(dentry
);
264 WARN_ON(!inode_is_locked(inode
));
265 return OVL_I(inode
)->version
;
268 bool ovl_is_whiteout(struct dentry
*dentry
)
270 struct inode
*inode
= dentry
->d_inode
;
272 return inode
&& IS_WHITEOUT(inode
);
275 struct file
*ovl_path_open(struct path
*path
, int flags
)
277 return dentry_open(path
, flags
| O_NOATIME
, current_cred());
280 int ovl_copy_up_start(struct dentry
*dentry
)
282 struct ovl_inode
*oi
= OVL_I(d_inode(dentry
));
285 err
= mutex_lock_interruptible(&oi
->lock
);
286 if (!err
&& ovl_dentry_upper(dentry
)) {
287 err
= 1; /* Already copied up */
288 mutex_unlock(&oi
->lock
);
294 void ovl_copy_up_end(struct dentry
*dentry
)
296 mutex_unlock(&OVL_I(d_inode(dentry
))->lock
);
299 bool ovl_check_dir_xattr(struct dentry
*dentry
, const char *name
)
304 if (!d_is_dir(dentry
))
307 res
= vfs_getxattr(dentry
, name
, &val
, 1);
308 if (res
== 1 && val
== 'y')
314 int ovl_check_setxattr(struct dentry
*dentry
, struct dentry
*upperdentry
,
315 const char *name
, const void *value
, size_t size
,
319 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
324 err
= ovl_do_setxattr(upperdentry
, name
, value
, size
, 0);
326 if (err
== -EOPNOTSUPP
) {
327 pr_warn("overlayfs: cannot set %s xattr on upper\n", name
);
335 int ovl_set_impure(struct dentry
*dentry
, struct dentry
*upperdentry
)
339 if (ovl_test_flag(OVL_IMPURE
, d_inode(dentry
)))
343 * Do not fail when upper doesn't support xattrs.
344 * Upper inodes won't have origin nor redirect xattr anyway.
346 err
= ovl_check_setxattr(dentry
, upperdentry
, OVL_XATTR_IMPURE
,
349 ovl_set_flag(OVL_IMPURE
, d_inode(dentry
));
354 void ovl_set_flag(unsigned long flag
, struct inode
*inode
)
356 set_bit(flag
, &OVL_I(inode
)->flags
);
359 bool ovl_test_flag(unsigned long flag
, struct inode
*inode
)
361 return test_bit(flag
, &OVL_I(inode
)->flags
);
365 * Caller must hold a reference to inode to prevent it from being freed while
366 * it is marked inuse.
368 bool ovl_inuse_trylock(struct dentry
*dentry
)
370 struct inode
*inode
= d_inode(dentry
);
373 spin_lock(&inode
->i_lock
);
374 if (!(inode
->i_state
& I_OVL_INUSE
)) {
375 inode
->i_state
|= I_OVL_INUSE
;
378 spin_unlock(&inode
->i_lock
);
383 void ovl_inuse_unlock(struct dentry
*dentry
)
386 struct inode
*inode
= d_inode(dentry
);
388 spin_lock(&inode
->i_lock
);
389 WARN_ON(!(inode
->i_state
& I_OVL_INUSE
));
390 inode
->i_state
&= ~I_OVL_INUSE
;
391 spin_unlock(&inode
->i_lock
);