1
2
3
4
5
6
7
8
9
10#include <linux/kernel.h>
11#include <linux/uuid.h>
12#include <linux/fs.h>
13#include "ovl_entry.h"
14
15#undef pr_fmt
16#define pr_fmt(fmt) "overlayfs: " fmt
17
18enum ovl_path_type {
19 __OVL_PATH_UPPER = (1 << 0),
20 __OVL_PATH_MERGE = (1 << 1),
21 __OVL_PATH_ORIGIN = (1 << 2),
22};
23
24#define OVL_TYPE_UPPER(type) ((type) & __OVL_PATH_UPPER)
25#define OVL_TYPE_MERGE(type) ((type) & __OVL_PATH_MERGE)
26#define OVL_TYPE_ORIGIN(type) ((type) & __OVL_PATH_ORIGIN)
27
28#define OVL_XATTR_PREFIX XATTR_TRUSTED_PREFIX "overlay."
29#define OVL_XATTR_OPAQUE OVL_XATTR_PREFIX "opaque"
30#define OVL_XATTR_REDIRECT OVL_XATTR_PREFIX "redirect"
31#define OVL_XATTR_ORIGIN OVL_XATTR_PREFIX "origin"
32#define OVL_XATTR_IMPURE OVL_XATTR_PREFIX "impure"
33#define OVL_XATTR_NLINK OVL_XATTR_PREFIX "nlink"
34#define OVL_XATTR_UPPER OVL_XATTR_PREFIX "upper"
35#define OVL_XATTR_METACOPY OVL_XATTR_PREFIX "metacopy"
36
37enum ovl_inode_flag {
38
39 OVL_IMPURE,
40
41 OVL_WHITEOUTS,
42 OVL_INDEX,
43 OVL_UPPERDATA,
44
45 OVL_CONST_INO,
46};
47
48enum ovl_entry_flag {
49 OVL_E_UPPER_ALIAS,
50 OVL_E_OPAQUE,
51 OVL_E_CONNECTED,
52};
53
54enum {
55 OVL_XINO_OFF,
56 OVL_XINO_AUTO,
57 OVL_XINO_ON,
58};
59
60
61
62
63
64
65
66#define OVL_FH_VERSION 0
67#define OVL_FH_MAGIC 0xfb
68
69
70#define OVL_FH_FLAG_BIG_ENDIAN (1 << 0)
71#define OVL_FH_FLAG_ANY_ENDIAN (1 << 1)
72
73#define OVL_FH_FLAG_PATH_UPPER (1 << 2)
74
75#define OVL_FH_FLAG_ALL (OVL_FH_FLAG_BIG_ENDIAN | OVL_FH_FLAG_ANY_ENDIAN | \
76 OVL_FH_FLAG_PATH_UPPER)
77
78#if defined(__LITTLE_ENDIAN)
79#define OVL_FH_FLAG_CPU_ENDIAN 0
80#elif defined(__BIG_ENDIAN)
81#define OVL_FH_FLAG_CPU_ENDIAN OVL_FH_FLAG_BIG_ENDIAN
82#else
83#error Endianness not defined
84#endif
85
86
87#define OVL_FILEID_V0 0xfb
88
89#define OVL_FILEID_V1 0xf8
90
91
92struct ovl_fb {
93 u8 version;
94 u8 magic;
95 u8 len;
96 u8 flags;
97 u8 type;
98 uuid_t uuid;
99 u32 fid[];
100} __packed;
101
102
103struct ovl_fh {
104 u8 padding[3];
105 union {
106 struct ovl_fb fb;
107 u8 buf[0];
108 };
109} __packed;
110
111#define OVL_FH_WIRE_OFFSET offsetof(struct ovl_fh, fb)
112#define OVL_FH_LEN(fh) (OVL_FH_WIRE_OFFSET + (fh)->fb.len)
113#define OVL_FH_FID_OFFSET (OVL_FH_WIRE_OFFSET + \
114 offsetof(struct ovl_fb, fid))
115
116static inline int ovl_do_rmdir(struct inode *dir, struct dentry *dentry)
117{
118 int err = vfs_rmdir(dir, dentry);
119
120 pr_debug("rmdir(%pd2) = %i\n", dentry, err);
121 return err;
122}
123
124static inline int ovl_do_unlink(struct inode *dir, struct dentry *dentry)
125{
126 int err = vfs_unlink(dir, dentry, NULL);
127
128 pr_debug("unlink(%pd2) = %i\n", dentry, err);
129 return err;
130}
131
132static inline int ovl_do_link(struct dentry *old_dentry, struct inode *dir,
133 struct dentry *new_dentry)
134{
135 int err = vfs_link(old_dentry, dir, new_dentry, NULL);
136
137 pr_debug("link(%pd2, %pd2) = %i\n", old_dentry, new_dentry, err);
138 return err;
139}
140
141static inline int ovl_do_create(struct inode *dir, struct dentry *dentry,
142 umode_t mode)
143{
144 int err = vfs_create(dir, dentry, mode, true);
145
146 pr_debug("create(%pd2, 0%o) = %i\n", dentry, mode, err);
147 return err;
148}
149
150static inline int ovl_do_mkdir(struct inode *dir, struct dentry *dentry,
151 umode_t mode)
152{
153 int err = vfs_mkdir(dir, dentry, mode);
154 pr_debug("mkdir(%pd2, 0%o) = %i\n", dentry, mode, err);
155 return err;
156}
157
158static inline int ovl_do_mknod(struct inode *dir, struct dentry *dentry,
159 umode_t mode, dev_t dev)
160{
161 int err = vfs_mknod(dir, dentry, mode, dev);
162
163 pr_debug("mknod(%pd2, 0%o, 0%o) = %i\n", dentry, mode, dev, err);
164 return err;
165}
166
167static inline int ovl_do_symlink(struct inode *dir, struct dentry *dentry,
168 const char *oldname)
169{
170 int err = vfs_symlink(dir, dentry, oldname);
171
172 pr_debug("symlink(\"%s\", %pd2) = %i\n", oldname, dentry, err);
173 return err;
174}
175
176static inline int ovl_do_setxattr(struct dentry *dentry, const char *name,
177 const void *value, size_t size, int flags)
178{
179 int err = vfs_setxattr(dentry, name, value, size, flags);
180 pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, 0x%x) = %i\n",
181 dentry, name, min((int)size, 48), value, size, flags, err);
182 return err;
183}
184
185static inline int ovl_do_removexattr(struct dentry *dentry, const char *name)
186{
187 int err = vfs_removexattr(dentry, name);
188 pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err);
189 return err;
190}
191
192static inline int ovl_do_rename(struct inode *olddir, struct dentry *olddentry,
193 struct inode *newdir, struct dentry *newdentry,
194 unsigned int flags)
195{
196 int err;
197
198 pr_debug("rename(%pd2, %pd2, 0x%x)\n", olddentry, newdentry, flags);
199 err = vfs_rename(olddir, olddentry, newdir, newdentry, NULL, flags);
200 if (err) {
201 pr_debug("...rename(%pd2, %pd2, ...) = %i\n",
202 olddentry, newdentry, err);
203 }
204 return err;
205}
206
207static inline int ovl_do_whiteout(struct inode *dir, struct dentry *dentry)
208{
209 int err = vfs_whiteout(dir, dentry);
210 pr_debug("whiteout(%pd2) = %i\n", dentry, err);
211 return err;
212}
213
214static inline struct dentry *ovl_do_tmpfile(struct dentry *dentry, umode_t mode)
215{
216 struct dentry *ret = vfs_tmpfile(dentry, mode, 0);
217 int err = PTR_ERR_OR_ZERO(ret);
218
219 pr_debug("tmpfile(%pd2, 0%o) = %i\n", dentry, mode, err);
220 return ret;
221}
222
223static inline bool ovl_open_flags_need_copy_up(int flags)
224{
225 if (!flags)
226 return false;
227
228 return ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC));
229}
230
231
232int ovl_want_write(struct dentry *dentry);
233void ovl_drop_write(struct dentry *dentry);
234struct dentry *ovl_workdir(struct dentry *dentry);
235const struct cred *ovl_override_creds(struct super_block *sb);
236int ovl_can_decode_fh(struct super_block *sb);
237struct dentry *ovl_indexdir(struct super_block *sb);
238bool ovl_index_all(struct super_block *sb);
239bool ovl_verify_lower(struct super_block *sb);
240struct ovl_entry *ovl_alloc_entry(unsigned int numlower);
241bool ovl_dentry_remote(struct dentry *dentry);
242void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
243 unsigned int mask);
244bool ovl_dentry_weird(struct dentry *dentry);
245enum ovl_path_type ovl_path_type(struct dentry *dentry);
246void ovl_path_upper(struct dentry *dentry, struct path *path);
247void ovl_path_lower(struct dentry *dentry, struct path *path);
248void ovl_path_lowerdata(struct dentry *dentry, struct path *path);
249enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
250struct dentry *ovl_dentry_upper(struct dentry *dentry);
251struct dentry *ovl_dentry_lower(struct dentry *dentry);
252struct dentry *ovl_dentry_lowerdata(struct dentry *dentry);
253const struct ovl_layer *ovl_layer_lower(struct dentry *dentry);
254struct dentry *ovl_dentry_real(struct dentry *dentry);
255struct dentry *ovl_i_dentry_upper(struct inode *inode);
256struct inode *ovl_inode_upper(struct inode *inode);
257struct inode *ovl_inode_lower(struct inode *inode);
258struct inode *ovl_inode_lowerdata(struct inode *inode);
259struct inode *ovl_inode_real(struct inode *inode);
260struct inode *ovl_inode_realdata(struct inode *inode);
261struct ovl_dir_cache *ovl_dir_cache(struct inode *inode);
262void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache);
263void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry);
264void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry);
265bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry);
266bool ovl_dentry_is_opaque(struct dentry *dentry);
267bool ovl_dentry_is_whiteout(struct dentry *dentry);
268void ovl_dentry_set_opaque(struct dentry *dentry);
269bool ovl_dentry_has_upper_alias(struct dentry *dentry);
270void ovl_dentry_set_upper_alias(struct dentry *dentry);
271bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags);
272bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags);
273bool ovl_has_upperdata(struct inode *inode);
274void ovl_set_upperdata(struct inode *inode);
275bool ovl_redirect_dir(struct super_block *sb);
276const char *ovl_dentry_get_redirect(struct dentry *dentry);
277void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect);
278void ovl_inode_update(struct inode *inode, struct dentry *upperdentry);
279void ovl_dir_modified(struct dentry *dentry, bool impurity);
280u64 ovl_dentry_version_get(struct dentry *dentry);
281bool ovl_is_whiteout(struct dentry *dentry);
282struct file *ovl_path_open(struct path *path, int flags);
283int ovl_copy_up_start(struct dentry *dentry, int flags);
284void ovl_copy_up_end(struct dentry *dentry);
285bool ovl_already_copied_up(struct dentry *dentry, int flags);
286bool ovl_check_origin_xattr(struct dentry *dentry);
287bool ovl_check_dir_xattr(struct dentry *dentry, const char *name);
288int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
289 const char *name, const void *value, size_t size,
290 int xerr);
291int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry);
292void ovl_set_flag(unsigned long flag, struct inode *inode);
293void ovl_clear_flag(unsigned long flag, struct inode *inode);
294bool ovl_test_flag(unsigned long flag, struct inode *inode);
295bool ovl_inuse_trylock(struct dentry *dentry);
296void ovl_inuse_unlock(struct dentry *dentry);
297bool ovl_is_inuse(struct dentry *dentry);
298bool ovl_need_index(struct dentry *dentry);
299int ovl_nlink_start(struct dentry *dentry);
300void ovl_nlink_end(struct dentry *dentry);
301int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir);
302int ovl_check_metacopy_xattr(struct dentry *dentry);
303bool ovl_is_metacopy_dentry(struct dentry *dentry);
304char *ovl_get_redirect_xattr(struct dentry *dentry, int padding);
305ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value,
306 size_t padding);
307
308static inline bool ovl_is_impuredir(struct dentry *dentry)
309{
310 return ovl_check_dir_xattr(dentry, OVL_XATTR_IMPURE);
311}
312
313
314
315
316
317
318static inline bool ovl_xino_warn(struct super_block *sb)
319{
320 return OVL_FS(sb)->config.xino == OVL_XINO_ON;
321}
322
323
324static inline bool ovl_same_fs(struct super_block *sb)
325{
326 return OVL_FS(sb)->xino_mode == 0;
327}
328
329
330static inline bool ovl_same_dev(struct super_block *sb)
331{
332 return OVL_FS(sb)->xino_mode >= 0;
333}
334
335static inline unsigned int ovl_xino_bits(struct super_block *sb)
336{
337 return ovl_same_dev(sb) ? OVL_FS(sb)->xino_mode : 0;
338}
339
340static inline void ovl_inode_lock(struct inode *inode)
341{
342 mutex_lock(&OVL_I(inode)->lock);
343}
344
345static inline int ovl_inode_lock_interruptible(struct inode *inode)
346{
347 return mutex_lock_interruptible(&OVL_I(inode)->lock);
348}
349
350static inline void ovl_inode_unlock(struct inode *inode)
351{
352 mutex_unlock(&OVL_I(inode)->lock);
353}
354
355
356
357int ovl_check_fb_len(struct ovl_fb *fb, int fb_len);
358
359static inline int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
360{
361 if (fh_len < sizeof(struct ovl_fh))
362 return -EINVAL;
363
364 return ovl_check_fb_len(&fh->fb, fh_len - OVL_FH_WIRE_OFFSET);
365}
366
367struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
368 bool connected);
369int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
370 struct dentry *upperdentry, struct ovl_path **stackp);
371int ovl_verify_set_fh(struct dentry *dentry, const char *name,
372 struct dentry *real, bool is_upper, bool set);
373struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index);
374int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index);
375int ovl_get_index_name(struct dentry *origin, struct qstr *name);
376struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh);
377struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
378 struct dentry *origin, bool verify);
379int ovl_path_next(int idx, struct dentry *dentry, struct path *path);
380struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
381 unsigned int flags);
382bool ovl_lower_positive(struct dentry *dentry);
383
384static inline int ovl_verify_origin(struct dentry *upper,
385 struct dentry *origin, bool set)
386{
387 return ovl_verify_set_fh(upper, OVL_XATTR_ORIGIN, origin, false, set);
388}
389
390static inline int ovl_verify_upper(struct dentry *index,
391 struct dentry *upper, bool set)
392{
393 return ovl_verify_set_fh(index, OVL_XATTR_UPPER, upper, true, set);
394}
395
396
397extern const struct file_operations ovl_dir_operations;
398int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
399void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list);
400void ovl_cache_free(struct list_head *list);
401void ovl_dir_cache_free(struct inode *inode);
402int ovl_check_d_type_supported(struct path *realpath);
403void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
404 struct dentry *dentry, int level);
405int ovl_indexdir_cleanup(struct ovl_fs *ofs);
406
407
408int ovl_set_nlink_upper(struct dentry *dentry);
409int ovl_set_nlink_lower(struct dentry *dentry);
410unsigned int ovl_get_nlink(struct dentry *lowerdentry,
411 struct dentry *upperdentry,
412 unsigned int fallback);
413int ovl_setattr(struct dentry *dentry, struct iattr *attr);
414int ovl_getattr(const struct path *path, struct kstat *stat,
415 u32 request_mask, unsigned int flags);
416int ovl_permission(struct inode *inode, int mask);
417int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
418 const void *value, size_t size, int flags);
419int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
420 void *value, size_t size);
421ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
422struct posix_acl *ovl_get_acl(struct inode *inode, int type);
423int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags);
424bool ovl_is_private_xattr(const char *name);
425
426struct ovl_inode_params {
427 struct inode *newinode;
428 struct dentry *upperdentry;
429 struct ovl_path *lowerpath;
430 struct dentry *index;
431 unsigned int numlower;
432 char *redirect;
433 struct dentry *lowerdata;
434};
435void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
436 unsigned long ino, int fsid);
437struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev);
438struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
439 bool is_upper);
440bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir);
441struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir);
442struct inode *ovl_get_inode(struct super_block *sb,
443 struct ovl_inode_params *oip);
444static inline void ovl_copyattr(struct inode *from, struct inode *to)
445{
446 to->i_uid = from->i_uid;
447 to->i_gid = from->i_gid;
448 to->i_mode = from->i_mode;
449 to->i_atime = from->i_atime;
450 to->i_mtime = from->i_mtime;
451 to->i_ctime = from->i_ctime;
452 i_size_write(to, i_size_read(from));
453}
454
455static inline void ovl_copyflags(struct inode *from, struct inode *to)
456{
457 unsigned int mask = S_SYNC | S_IMMUTABLE | S_APPEND | S_NOATIME;
458
459 inode_set_flags(to, from->i_flags & mask, mask);
460}
461
462
463extern const struct inode_operations ovl_dir_inode_operations;
464int ovl_cleanup_and_whiteout(struct dentry *workdir, struct inode *dir,
465 struct dentry *dentry);
466struct ovl_cattr {
467 dev_t rdev;
468 umode_t mode;
469 const char *link;
470 struct dentry *hardlink;
471};
472
473#define OVL_CATTR(m) (&(struct ovl_cattr) { .mode = (m) })
474
475struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
476 struct ovl_cattr *attr);
477int ovl_cleanup(struct inode *dir, struct dentry *dentry);
478struct dentry *ovl_lookup_temp(struct dentry *workdir);
479struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr);
480
481
482extern const struct file_operations ovl_file_operations;
483int __init ovl_aio_request_cache_init(void);
484void ovl_aio_request_cache_destroy(void);
485
486
487int ovl_copy_up(struct dentry *dentry);
488int ovl_copy_up_with_data(struct dentry *dentry);
489int ovl_copy_up_flags(struct dentry *dentry, int flags);
490int ovl_maybe_copy_up(struct dentry *dentry, int flags);
491int ovl_copy_xattr(struct dentry *old, struct dentry *new);
492int ovl_set_attr(struct dentry *upper, struct kstat *stat);
493struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper);
494int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
495 struct dentry *upper);
496
497
498extern const struct export_operations ovl_export_operations;
499