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/sched/signal.h>
16 #include "overlayfs.h"
17 #include "ovl_entry.h"
19 int ovl_want_write(struct dentry
*dentry
)
21 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
22 return mnt_want_write(ofs
->upper_mnt
);
25 void ovl_drop_write(struct dentry
*dentry
)
27 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
28 mnt_drop_write(ofs
->upper_mnt
);
31 struct dentry
*ovl_workdir(struct dentry
*dentry
)
33 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
37 const struct cred
*ovl_override_creds(struct super_block
*sb
)
39 struct ovl_fs
*ofs
= sb
->s_fs_info
;
41 return override_creds(ofs
->creator_cred
);
44 struct ovl_entry
*ovl_alloc_entry(unsigned int numlower
)
46 size_t size
= offsetof(struct ovl_entry
, lowerstack
[numlower
]);
47 struct ovl_entry
*oe
= kzalloc(size
, GFP_KERNEL
);
50 oe
->numlower
= numlower
;
55 bool ovl_dentry_remote(struct dentry
*dentry
)
57 return dentry
->d_flags
&
58 (DCACHE_OP_REVALIDATE
| DCACHE_OP_WEAK_REVALIDATE
|
62 bool ovl_dentry_weird(struct dentry
*dentry
)
64 return dentry
->d_flags
& (DCACHE_NEED_AUTOMOUNT
|
65 DCACHE_MANAGE_TRANSIT
|
70 enum ovl_path_type
ovl_path_type(struct dentry
*dentry
)
72 struct ovl_entry
*oe
= dentry
->d_fsdata
;
73 enum ovl_path_type type
= 0;
75 if (oe
->__upperdentry
) {
76 type
= __OVL_PATH_UPPER
;
79 * Non-dir dentry can hold lower dentry from previous
82 if (oe
->numlower
&& d_is_dir(dentry
))
83 type
|= __OVL_PATH_MERGE
;
86 type
|= __OVL_PATH_MERGE
;
91 void ovl_path_upper(struct dentry
*dentry
, struct path
*path
)
93 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
94 struct ovl_entry
*oe
= dentry
->d_fsdata
;
96 path
->mnt
= ofs
->upper_mnt
;
97 path
->dentry
= ovl_upperdentry_dereference(oe
);
100 void ovl_path_lower(struct dentry
*dentry
, struct path
*path
)
102 struct ovl_entry
*oe
= dentry
->d_fsdata
;
104 *path
= oe
->numlower
? oe
->lowerstack
[0] : (struct path
) { NULL
, NULL
};
107 enum ovl_path_type
ovl_path_real(struct dentry
*dentry
, struct path
*path
)
109 enum ovl_path_type type
= ovl_path_type(dentry
);
111 if (!OVL_TYPE_UPPER(type
))
112 ovl_path_lower(dentry
, path
);
114 ovl_path_upper(dentry
, path
);
119 struct dentry
*ovl_dentry_upper(struct dentry
*dentry
)
121 struct ovl_entry
*oe
= dentry
->d_fsdata
;
123 return ovl_upperdentry_dereference(oe
);
126 static struct dentry
*__ovl_dentry_lower(struct ovl_entry
*oe
)
128 return oe
->numlower
? oe
->lowerstack
[0].dentry
: NULL
;
131 struct dentry
*ovl_dentry_lower(struct dentry
*dentry
)
133 struct ovl_entry
*oe
= dentry
->d_fsdata
;
135 return __ovl_dentry_lower(oe
);
138 struct dentry
*ovl_dentry_real(struct dentry
*dentry
)
140 struct ovl_entry
*oe
= dentry
->d_fsdata
;
141 struct dentry
*realdentry
;
143 realdentry
= ovl_upperdentry_dereference(oe
);
145 realdentry
= __ovl_dentry_lower(oe
);
150 struct ovl_dir_cache
*ovl_dir_cache(struct dentry
*dentry
)
152 struct ovl_entry
*oe
= dentry
->d_fsdata
;
157 void ovl_set_dir_cache(struct dentry
*dentry
, struct ovl_dir_cache
*cache
)
159 struct ovl_entry
*oe
= dentry
->d_fsdata
;
164 bool ovl_dentry_is_opaque(struct dentry
*dentry
)
166 struct ovl_entry
*oe
= dentry
->d_fsdata
;
170 bool ovl_dentry_is_whiteout(struct dentry
*dentry
)
172 return !dentry
->d_inode
&& ovl_dentry_is_opaque(dentry
);
175 void ovl_dentry_set_opaque(struct dentry
*dentry
)
177 struct ovl_entry
*oe
= dentry
->d_fsdata
;
182 bool ovl_redirect_dir(struct super_block
*sb
)
184 struct ovl_fs
*ofs
= sb
->s_fs_info
;
186 return ofs
->config
.redirect_dir
;
189 void ovl_clear_redirect_dir(struct super_block
*sb
)
191 struct ovl_fs
*ofs
= sb
->s_fs_info
;
193 ofs
->config
.redirect_dir
= false;
196 const char *ovl_dentry_get_redirect(struct dentry
*dentry
)
198 struct ovl_entry
*oe
= dentry
->d_fsdata
;
203 void ovl_dentry_set_redirect(struct dentry
*dentry
, const char *redirect
)
205 struct ovl_entry
*oe
= dentry
->d_fsdata
;
208 oe
->redirect
= redirect
;
211 void ovl_dentry_update(struct dentry
*dentry
, struct dentry
*upperdentry
)
213 struct ovl_entry
*oe
= dentry
->d_fsdata
;
215 WARN_ON(!inode_is_locked(upperdentry
->d_parent
->d_inode
));
216 WARN_ON(oe
->__upperdentry
);
218 * Make sure upperdentry is consistent before making it visible to
219 * ovl_upperdentry_dereference().
222 oe
->__upperdentry
= upperdentry
;
225 void ovl_inode_init(struct inode
*inode
, struct inode
*realinode
, bool is_upper
)
227 WRITE_ONCE(inode
->i_private
, (unsigned long) realinode
|
228 (is_upper
? OVL_ISUPPER_MASK
: 0));
231 void ovl_inode_update(struct inode
*inode
, struct inode
*upperinode
)
233 WARN_ON(!upperinode
);
234 WARN_ON(!inode_unhashed(inode
));
235 WRITE_ONCE(inode
->i_private
,
236 (unsigned long) upperinode
| OVL_ISUPPER_MASK
);
237 if (!S_ISDIR(upperinode
->i_mode
))
238 __insert_inode_hash(inode
, (unsigned long) upperinode
);
241 void ovl_dentry_version_inc(struct dentry
*dentry
)
243 struct ovl_entry
*oe
= dentry
->d_fsdata
;
245 WARN_ON(!inode_is_locked(dentry
->d_inode
));
249 u64
ovl_dentry_version_get(struct dentry
*dentry
)
251 struct ovl_entry
*oe
= dentry
->d_fsdata
;
253 WARN_ON(!inode_is_locked(dentry
->d_inode
));
257 bool ovl_is_whiteout(struct dentry
*dentry
)
259 struct inode
*inode
= dentry
->d_inode
;
261 return inode
&& IS_WHITEOUT(inode
);
264 struct file
*ovl_path_open(struct path
*path
, int flags
)
266 return dentry_open(path
, flags
| O_NOATIME
, current_cred());
269 int ovl_copy_up_start(struct dentry
*dentry
)
271 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
272 struct ovl_entry
*oe
= dentry
->d_fsdata
;
275 spin_lock(&ofs
->copyup_wq
.lock
);
276 err
= wait_event_interruptible_locked(ofs
->copyup_wq
, !oe
->copying
);
278 if (oe
->__upperdentry
)
279 err
= 1; /* Already copied up */
283 spin_unlock(&ofs
->copyup_wq
.lock
);
288 void ovl_copy_up_end(struct dentry
*dentry
)
290 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
291 struct ovl_entry
*oe
= dentry
->d_fsdata
;
293 spin_lock(&ofs
->copyup_wq
.lock
);
295 wake_up_locked(&ofs
->copyup_wq
);
296 spin_unlock(&ofs
->copyup_wq
.lock
);