hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/fs/overlayfs/overlayfs.h
....@@ -1,16 +1,16 @@
1
+/* SPDX-License-Identifier: GPL-2.0-only */
12 /*
23 *
34 * Copyright (C) 2011 Novell Inc.
4
- *
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.
85 */
96
107 #include <linux/kernel.h>
118 #include <linux/uuid.h>
129 #include <linux/fs.h>
1310 #include "ovl_entry.h"
11
+
12
+#undef pr_fmt
13
+#define pr_fmt(fmt) "overlayfs: " fmt
1414
1515 enum ovl_path_type {
1616 __OVL_PATH_UPPER = (1 << 0),
....@@ -23,13 +23,16 @@
2323 #define OVL_TYPE_ORIGIN(type) ((type) & __OVL_PATH_ORIGIN)
2424
2525 #define OVL_XATTR_PREFIX XATTR_TRUSTED_PREFIX "overlay."
26
-#define OVL_XATTR_OPAQUE OVL_XATTR_PREFIX "opaque"
27
-#define OVL_XATTR_REDIRECT OVL_XATTR_PREFIX "redirect"
28
-#define OVL_XATTR_ORIGIN OVL_XATTR_PREFIX "origin"
29
-#define OVL_XATTR_IMPURE OVL_XATTR_PREFIX "impure"
30
-#define OVL_XATTR_NLINK OVL_XATTR_PREFIX "nlink"
31
-#define OVL_XATTR_UPPER OVL_XATTR_PREFIX "upper"
32
-#define OVL_XATTR_METACOPY OVL_XATTR_PREFIX "metacopy"
26
+
27
+enum ovl_xattr {
28
+ OVL_XATTR_OPAQUE,
29
+ OVL_XATTR_REDIRECT,
30
+ OVL_XATTR_ORIGIN,
31
+ OVL_XATTR_IMPURE,
32
+ OVL_XATTR_NLINK,
33
+ OVL_XATTR_UPPER,
34
+ OVL_XATTR_METACOPY,
35
+};
3336
3437 enum ovl_inode_flag {
3538 /* Pure upper dir that may contain non pure upper entries */
....@@ -46,6 +49,12 @@
4649 OVL_E_UPPER_ALIAS,
4750 OVL_E_OPAQUE,
4851 OVL_E_CONNECTED,
52
+};
53
+
54
+enum {
55
+ OVL_XINO_OFF,
56
+ OVL_XINO_AUTO,
57
+ OVL_XINO_ON,
4958 };
5059
5160 /*
....@@ -74,19 +83,41 @@
7483 #error Endianness not defined
7584 #endif
7685
77
-/* The type returned by overlay exportfs ops when encoding an ovl_fh handle */
78
-#define OVL_FILEID 0xfb
86
+/* The type used to be returned by overlay exportfs for misaligned fid */
87
+#define OVL_FILEID_V0 0xfb
88
+/* The type returned by overlay exportfs for 32bit aligned fid */
89
+#define OVL_FILEID_V1 0xf8
7990
80
-/* On-disk and in-memeory format for redirect by file handle */
81
-struct ovl_fh {
91
+/* On-disk format for "origin" file handle */
92
+struct ovl_fb {
8293 u8 version; /* 0 */
8394 u8 magic; /* 0xfb */
8495 u8 len; /* size of this header + size of fid */
8596 u8 flags; /* OVL_FH_FLAG_* */
8697 u8 type; /* fid_type of fid */
8798 uuid_t uuid; /* uuid of filesystem */
88
- u8 fid[0]; /* file identifier */
99
+ u32 fid[]; /* file identifier should be 32bit aligned in-memory */
89100 } __packed;
101
+
102
+/* In-memory and on-wire format for overlay file handle */
103
+struct ovl_fh {
104
+ u8 padding[3]; /* make sure fb.fid is 32bit aligned */
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
+
116
+extern const char *ovl_xattr_table[];
117
+static inline const char *ovl_xattr(struct ovl_fs *ofs, enum ovl_xattr ox)
118
+{
119
+ return ovl_xattr_table[ox];
120
+}
90121
91122 static inline int ovl_do_rmdir(struct inode *dir, struct dentry *dentry)
92123 {
....@@ -148,17 +179,31 @@
148179 return err;
149180 }
150181
151
-static inline int ovl_do_setxattr(struct dentry *dentry, const char *name,
152
- const void *value, size_t size, int flags)
182
+static inline ssize_t ovl_do_getxattr(struct ovl_fs *ofs, struct dentry *dentry,
183
+ enum ovl_xattr ox, void *value,
184
+ size_t size)
153185 {
154
- int err = vfs_setxattr(dentry, name, value, size, flags);
155
- pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, 0x%x) = %i\n",
156
- dentry, name, min((int)size, 48), value, size, flags, err);
186
+ const char *name = ovl_xattr(ofs, ox);
187
+ struct inode *ip = d_inode(dentry);
188
+
189
+ return __vfs_getxattr(dentry, ip, name, value, size, XATTR_NOSECURITY);
190
+}
191
+
192
+static inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
193
+ enum ovl_xattr ox, const void *value,
194
+ size_t size)
195
+{
196
+ const char *name = ovl_xattr(ofs, ox);
197
+ int err = vfs_setxattr(dentry, name, value, size, 0);
198
+ pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, 0) = %i\n",
199
+ dentry, name, min((int)size, 48), value, size, err);
157200 return err;
158201 }
159202
160
-static inline int ovl_do_removexattr(struct dentry *dentry, const char *name)
203
+static inline int ovl_do_removexattr(struct ovl_fs *ofs, struct dentry *dentry,
204
+ enum ovl_xattr ox)
161205 {
206
+ const char *name = ovl_xattr(ofs, ox);
162207 int err = vfs_removexattr(dentry, name);
163208 pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err);
164209 return err;
....@@ -208,16 +253,17 @@
208253 void ovl_drop_write(struct dentry *dentry);
209254 struct dentry *ovl_workdir(struct dentry *dentry);
210255 const struct cred *ovl_override_creds(struct super_block *sb);
211
-void ovl_revert_creds(const struct cred *oldcred);
212
-ssize_t ovl_vfs_getxattr(struct dentry *dentry, const char *name, void *buf,
213
- size_t size);
214
-struct super_block *ovl_same_sb(struct super_block *sb);
256
+void ovl_revert_creds(struct super_block *sb, const struct cred *oldcred);
215257 int ovl_can_decode_fh(struct super_block *sb);
216258 struct dentry *ovl_indexdir(struct super_block *sb);
217259 bool ovl_index_all(struct super_block *sb);
218260 bool ovl_verify_lower(struct super_block *sb);
219261 struct ovl_entry *ovl_alloc_entry(unsigned int numlower);
220262 bool ovl_dentry_remote(struct dentry *dentry);
263
+void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry);
264
+void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry);
265
+void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
266
+ unsigned int mask);
221267 bool ovl_dentry_weird(struct dentry *dentry);
222268 enum ovl_path_type ovl_path_type(struct dentry *dentry);
223269 void ovl_path_upper(struct dentry *dentry, struct path *path);
....@@ -227,7 +273,7 @@
227273 struct dentry *ovl_dentry_upper(struct dentry *dentry);
228274 struct dentry *ovl_dentry_lower(struct dentry *dentry);
229275 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry);
230
-struct ovl_layer *ovl_layer_lower(struct dentry *dentry);
276
+const struct ovl_layer *ovl_layer_lower(struct dentry *dentry);
231277 struct dentry *ovl_dentry_real(struct dentry *dentry);
232278 struct dentry *ovl_i_dentry_upper(struct inode *inode);
233279 struct inode *ovl_inode_upper(struct inode *inode);
....@@ -252,8 +298,6 @@
252298 bool ovl_redirect_dir(struct super_block *sb);
253299 const char *ovl_dentry_get_redirect(struct dentry *dentry);
254300 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect);
255
-void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
256
- struct dentry *lowerdentry, struct dentry *lowerdata);
257301 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry);
258302 void ovl_dir_modified(struct dentry *dentry, bool impurity);
259303 u64 ovl_dentry_version_get(struct dentry *dentry);
....@@ -262,49 +306,108 @@
262306 int ovl_copy_up_start(struct dentry *dentry, int flags);
263307 void ovl_copy_up_end(struct dentry *dentry);
264308 bool ovl_already_copied_up(struct dentry *dentry, int flags);
265
-bool ovl_check_origin_xattr(struct dentry *dentry);
266
-bool ovl_check_dir_xattr(struct dentry *dentry, const char *name);
309
+bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry);
310
+bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
311
+ enum ovl_xattr ox);
267312 int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
268
- const char *name, const void *value, size_t size,
313
+ enum ovl_xattr ox, const void *value, size_t size,
269314 int xerr);
270315 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry);
271
-void ovl_set_flag(unsigned long flag, struct inode *inode);
272
-void ovl_clear_flag(unsigned long flag, struct inode *inode);
273
-bool ovl_test_flag(unsigned long flag, struct inode *inode);
274316 bool ovl_inuse_trylock(struct dentry *dentry);
275317 void ovl_inuse_unlock(struct dentry *dentry);
276318 bool ovl_is_inuse(struct dentry *dentry);
277319 bool ovl_need_index(struct dentry *dentry);
278
-int ovl_nlink_start(struct dentry *dentry, bool *locked);
279
-void ovl_nlink_end(struct dentry *dentry, bool locked);
320
+int ovl_nlink_start(struct dentry *dentry);
321
+void ovl_nlink_end(struct dentry *dentry);
280322 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir);
281
-int ovl_check_metacopy_xattr(struct dentry *dentry);
323
+int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry);
282324 bool ovl_is_metacopy_dentry(struct dentry *dentry);
283
-char *ovl_get_redirect_xattr(struct dentry *dentry, int padding);
284
-ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value,
285
- size_t padding);
325
+char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
326
+ int padding);
327
+int ovl_sync_status(struct ovl_fs *ofs);
286328
287
-static inline bool ovl_is_impuredir(struct dentry *dentry)
329
+static inline void ovl_set_flag(unsigned long flag, struct inode *inode)
288330 {
289
- return ovl_check_dir_xattr(dentry, OVL_XATTR_IMPURE);
331
+ set_bit(flag, &OVL_I(inode)->flags);
332
+}
333
+
334
+static inline void ovl_clear_flag(unsigned long flag, struct inode *inode)
335
+{
336
+ clear_bit(flag, &OVL_I(inode)->flags);
337
+}
338
+
339
+static inline bool ovl_test_flag(unsigned long flag, struct inode *inode)
340
+{
341
+ return test_bit(flag, &OVL_I(inode)->flags);
342
+}
343
+
344
+static inline bool ovl_is_impuredir(struct super_block *sb,
345
+ struct dentry *dentry)
346
+{
347
+ return ovl_check_dir_xattr(sb, dentry, OVL_XATTR_IMPURE);
348
+}
349
+
350
+/*
351
+ * With xino=auto, we do best effort to keep all inodes on same st_dev and
352
+ * d_ino consistent with st_ino.
353
+ * With xino=on, we do the same effort but we warn if we failed.
354
+ */
355
+static inline bool ovl_xino_warn(struct super_block *sb)
356
+{
357
+ return OVL_FS(sb)->config.xino == OVL_XINO_ON;
358
+}
359
+
360
+/* All layers on same fs? */
361
+static inline bool ovl_same_fs(struct super_block *sb)
362
+{
363
+ return OVL_FS(sb)->xino_mode == 0;
364
+}
365
+
366
+/* All overlay inodes have same st_dev? */
367
+static inline bool ovl_same_dev(struct super_block *sb)
368
+{
369
+ return OVL_FS(sb)->xino_mode >= 0;
290370 }
291371
292372 static inline unsigned int ovl_xino_bits(struct super_block *sb)
293373 {
294
- struct ovl_fs *ofs = sb->s_fs_info;
374
+ return ovl_same_dev(sb) ? OVL_FS(sb)->xino_mode : 0;
375
+}
295376
296
- return ofs->xino_bits;
377
+static inline void ovl_inode_lock(struct inode *inode)
378
+{
379
+ mutex_lock(&OVL_I(inode)->lock);
380
+}
381
+
382
+static inline int ovl_inode_lock_interruptible(struct inode *inode)
383
+{
384
+ return mutex_lock_interruptible(&OVL_I(inode)->lock);
385
+}
386
+
387
+static inline void ovl_inode_unlock(struct inode *inode)
388
+{
389
+ mutex_unlock(&OVL_I(inode)->lock);
297390 }
298391
299392
300393 /* namei.c */
301
-int ovl_check_fh_len(struct ovl_fh *fh, int fh_len);
394
+int ovl_check_fb_len(struct ovl_fb *fb, int fb_len);
395
+
396
+static inline int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
397
+{
398
+ if (fh_len < sizeof(struct ovl_fh))
399
+ return -EINVAL;
400
+
401
+ return ovl_check_fb_len(&fh->fb, fh_len - OVL_FH_WIRE_OFFSET);
402
+}
403
+
302404 struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
303405 bool connected);
304406 int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
305407 struct dentry *upperdentry, struct ovl_path **stackp);
306
-int ovl_verify_set_fh(struct dentry *dentry, const char *name,
307
- struct dentry *real, bool is_upper, bool set);
408
+int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
409
+ enum ovl_xattr ox, struct dentry *real, bool is_upper,
410
+ bool set);
308411 struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index);
309412 int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index);
310413 int ovl_get_index_name(struct dentry *origin, struct qstr *name);
....@@ -316,33 +419,47 @@
316419 unsigned int flags);
317420 bool ovl_lower_positive(struct dentry *dentry);
318421
319
-static inline int ovl_verify_origin(struct dentry *upper,
422
+static inline int ovl_verify_origin(struct ovl_fs *ofs, struct dentry *upper,
320423 struct dentry *origin, bool set)
321424 {
322
- return ovl_verify_set_fh(upper, OVL_XATTR_ORIGIN, origin, false, set);
425
+ return ovl_verify_set_fh(ofs, upper, OVL_XATTR_ORIGIN, origin,
426
+ false, set);
323427 }
324428
325
-static inline int ovl_verify_upper(struct dentry *index,
326
- struct dentry *upper, bool set)
429
+static inline int ovl_verify_upper(struct ovl_fs *ofs, struct dentry *index,
430
+ struct dentry *upper, bool set)
327431 {
328
- return ovl_verify_set_fh(index, OVL_XATTR_UPPER, upper, true, set);
432
+ return ovl_verify_set_fh(ofs, index, OVL_XATTR_UPPER, upper, true, set);
329433 }
330434
331435 /* readdir.c */
332436 extern const struct file_operations ovl_dir_operations;
437
+struct file *ovl_dir_real_file(const struct file *file, bool want_upper);
333438 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
334439 void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list);
335440 void ovl_cache_free(struct list_head *list);
336441 void ovl_dir_cache_free(struct inode *inode);
337442 int ovl_check_d_type_supported(struct path *realpath);
338
-void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
339
- struct dentry *dentry, int level);
443
+int ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
444
+ struct dentry *dentry, int level);
340445 int ovl_indexdir_cleanup(struct ovl_fs *ofs);
446
+
447
+/*
448
+ * Can we iterate real dir directly?
449
+ *
450
+ * Non-merge dir may contain whiteouts from a time it was a merge upper, before
451
+ * lower dir was removed under it and possibly before it was rotated from upper
452
+ * to lower layer.
453
+ */
454
+static inline bool ovl_dir_is_real(struct dentry *dir)
455
+{
456
+ return !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir));
457
+}
341458
342459 /* inode.c */
343460 int ovl_set_nlink_upper(struct dentry *dentry);
344461 int ovl_set_nlink_lower(struct dentry *dentry);
345
-unsigned int ovl_get_nlink(struct dentry *lowerdentry,
462
+unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
346463 struct dentry *upperdentry,
347464 unsigned int fallback);
348465 int ovl_setattr(struct dentry *dentry, struct iattr *attr);
....@@ -352,23 +469,23 @@
352469 int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
353470 const void *value, size_t size, int flags);
354471 int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
355
- void *value, size_t size);
356
-int __ovl_xattr_get(struct dentry *dentry, struct inode *inode,
357
- const char *name, void *value, size_t size);
472
+ void *value, size_t size, int flags);
358473 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
359474 struct posix_acl *ovl_get_acl(struct inode *inode, int type);
360475 int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags);
361
-bool ovl_is_private_xattr(const char *name);
476
+bool ovl_is_private_xattr(struct super_block *sb, const char *name);
362477
363478 struct ovl_inode_params {
364479 struct inode *newinode;
365480 struct dentry *upperdentry;
366481 struct ovl_path *lowerpath;
367
- struct dentry *index;
482
+ bool index;
368483 unsigned int numlower;
369484 char *redirect;
370485 struct dentry *lowerdata;
371486 };
487
+void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
488
+ unsigned long ino, int fsid);
372489 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev);
373490 struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
374491 bool is_upper);
....@@ -396,7 +513,7 @@
396513
397514 /* dir.c */
398515 extern const struct inode_operations ovl_dir_inode_operations;
399
-int ovl_cleanup_and_whiteout(struct dentry *workdir, struct inode *dir,
516
+int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
400517 struct dentry *dentry);
401518 struct ovl_cattr {
402519 dev_t rdev;
....@@ -411,17 +528,22 @@
411528 struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
412529 struct ovl_cattr *attr);
413530 int ovl_cleanup(struct inode *dir, struct dentry *dentry);
531
+struct dentry *ovl_lookup_temp(struct dentry *workdir);
414532 struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr);
415533
416534 /* file.c */
417535 extern const struct file_operations ovl_file_operations;
536
+int __init ovl_aio_request_cache_init(void);
537
+void ovl_aio_request_cache_destroy(void);
538
+long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
539
+long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
418540
419541 /* copy_up.c */
420542 int ovl_copy_up(struct dentry *dentry);
421543 int ovl_copy_up_with_data(struct dentry *dentry);
422
-int ovl_copy_up_flags(struct dentry *dentry, int flags);
423544 int ovl_maybe_copy_up(struct dentry *dentry, int flags);
424
-int ovl_copy_xattr(struct dentry *old, struct dentry *new);
545
+int ovl_copy_xattr(struct super_block *sb, struct dentry *old,
546
+ struct dentry *new);
425547 int ovl_set_attr(struct dentry *upper, struct kstat *stat);
426548 struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper);
427549 int ovl_set_origin(struct dentry *dentry, struct dentry *lower,