forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
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,15 @@
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 *upperdentry,
264
+ unsigned int mask);
221265 bool ovl_dentry_weird(struct dentry *dentry);
222266 enum ovl_path_type ovl_path_type(struct dentry *dentry);
223267 void ovl_path_upper(struct dentry *dentry, struct path *path);
....@@ -227,7 +271,7 @@
227271 struct dentry *ovl_dentry_upper(struct dentry *dentry);
228272 struct dentry *ovl_dentry_lower(struct dentry *dentry);
229273 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry);
230
-struct ovl_layer *ovl_layer_lower(struct dentry *dentry);
274
+const struct ovl_layer *ovl_layer_lower(struct dentry *dentry);
231275 struct dentry *ovl_dentry_real(struct dentry *dentry);
232276 struct dentry *ovl_i_dentry_upper(struct inode *inode);
233277 struct inode *ovl_inode_upper(struct inode *inode);
....@@ -252,8 +296,6 @@
252296 bool ovl_redirect_dir(struct super_block *sb);
253297 const char *ovl_dentry_get_redirect(struct dentry *dentry);
254298 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);
257299 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry);
258300 void ovl_dir_modified(struct dentry *dentry, bool impurity);
259301 u64 ovl_dentry_version_get(struct dentry *dentry);
....@@ -262,49 +304,108 @@
262304 int ovl_copy_up_start(struct dentry *dentry, int flags);
263305 void ovl_copy_up_end(struct dentry *dentry);
264306 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);
307
+bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry);
308
+bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
309
+ enum ovl_xattr ox);
267310 int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
268
- const char *name, const void *value, size_t size,
311
+ enum ovl_xattr ox, const void *value, size_t size,
269312 int xerr);
270313 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);
274314 bool ovl_inuse_trylock(struct dentry *dentry);
275315 void ovl_inuse_unlock(struct dentry *dentry);
276316 bool ovl_is_inuse(struct dentry *dentry);
277317 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);
318
+int ovl_nlink_start(struct dentry *dentry);
319
+void ovl_nlink_end(struct dentry *dentry);
280320 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir);
281
-int ovl_check_metacopy_xattr(struct dentry *dentry);
321
+int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry);
282322 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);
323
+char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
324
+ int padding);
325
+int ovl_sync_status(struct ovl_fs *ofs);
286326
287
-static inline bool ovl_is_impuredir(struct dentry *dentry)
327
+static inline void ovl_set_flag(unsigned long flag, struct inode *inode)
288328 {
289
- return ovl_check_dir_xattr(dentry, OVL_XATTR_IMPURE);
329
+ set_bit(flag, &OVL_I(inode)->flags);
330
+}
331
+
332
+static inline void ovl_clear_flag(unsigned long flag, struct inode *inode)
333
+{
334
+ clear_bit(flag, &OVL_I(inode)->flags);
335
+}
336
+
337
+static inline bool ovl_test_flag(unsigned long flag, struct inode *inode)
338
+{
339
+ return test_bit(flag, &OVL_I(inode)->flags);
340
+}
341
+
342
+static inline bool ovl_is_impuredir(struct super_block *sb,
343
+ struct dentry *dentry)
344
+{
345
+ return ovl_check_dir_xattr(sb, dentry, OVL_XATTR_IMPURE);
346
+}
347
+
348
+/*
349
+ * With xino=auto, we do best effort to keep all inodes on same st_dev and
350
+ * d_ino consistent with st_ino.
351
+ * With xino=on, we do the same effort but we warn if we failed.
352
+ */
353
+static inline bool ovl_xino_warn(struct super_block *sb)
354
+{
355
+ return OVL_FS(sb)->config.xino == OVL_XINO_ON;
356
+}
357
+
358
+/* All layers on same fs? */
359
+static inline bool ovl_same_fs(struct super_block *sb)
360
+{
361
+ return OVL_FS(sb)->xino_mode == 0;
362
+}
363
+
364
+/* All overlay inodes have same st_dev? */
365
+static inline bool ovl_same_dev(struct super_block *sb)
366
+{
367
+ return OVL_FS(sb)->xino_mode >= 0;
290368 }
291369
292370 static inline unsigned int ovl_xino_bits(struct super_block *sb)
293371 {
294
- struct ovl_fs *ofs = sb->s_fs_info;
372
+ return ovl_same_dev(sb) ? OVL_FS(sb)->xino_mode : 0;
373
+}
295374
296
- return ofs->xino_bits;
375
+static inline void ovl_inode_lock(struct inode *inode)
376
+{
377
+ mutex_lock(&OVL_I(inode)->lock);
378
+}
379
+
380
+static inline int ovl_inode_lock_interruptible(struct inode *inode)
381
+{
382
+ return mutex_lock_interruptible(&OVL_I(inode)->lock);
383
+}
384
+
385
+static inline void ovl_inode_unlock(struct inode *inode)
386
+{
387
+ mutex_unlock(&OVL_I(inode)->lock);
297388 }
298389
299390
300391 /* namei.c */
301
-int ovl_check_fh_len(struct ovl_fh *fh, int fh_len);
392
+int ovl_check_fb_len(struct ovl_fb *fb, int fb_len);
393
+
394
+static inline int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
395
+{
396
+ if (fh_len < sizeof(struct ovl_fh))
397
+ return -EINVAL;
398
+
399
+ return ovl_check_fb_len(&fh->fb, fh_len - OVL_FH_WIRE_OFFSET);
400
+}
401
+
302402 struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
303403 bool connected);
304404 int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
305405 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);
406
+int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
407
+ enum ovl_xattr ox, struct dentry *real, bool is_upper,
408
+ bool set);
308409 struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index);
309410 int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index);
310411 int ovl_get_index_name(struct dentry *origin, struct qstr *name);
....@@ -316,33 +417,47 @@
316417 unsigned int flags);
317418 bool ovl_lower_positive(struct dentry *dentry);
318419
319
-static inline int ovl_verify_origin(struct dentry *upper,
420
+static inline int ovl_verify_origin(struct ovl_fs *ofs, struct dentry *upper,
320421 struct dentry *origin, bool set)
321422 {
322
- return ovl_verify_set_fh(upper, OVL_XATTR_ORIGIN, origin, false, set);
423
+ return ovl_verify_set_fh(ofs, upper, OVL_XATTR_ORIGIN, origin,
424
+ false, set);
323425 }
324426
325
-static inline int ovl_verify_upper(struct dentry *index,
326
- struct dentry *upper, bool set)
427
+static inline int ovl_verify_upper(struct ovl_fs *ofs, struct dentry *index,
428
+ struct dentry *upper, bool set)
327429 {
328
- return ovl_verify_set_fh(index, OVL_XATTR_UPPER, upper, true, set);
430
+ return ovl_verify_set_fh(ofs, index, OVL_XATTR_UPPER, upper, true, set);
329431 }
330432
331433 /* readdir.c */
332434 extern const struct file_operations ovl_dir_operations;
435
+struct file *ovl_dir_real_file(const struct file *file, bool want_upper);
333436 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
334437 void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list);
335438 void ovl_cache_free(struct list_head *list);
336439 void ovl_dir_cache_free(struct inode *inode);
337440 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);
441
+int ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
442
+ struct dentry *dentry, int level);
340443 int ovl_indexdir_cleanup(struct ovl_fs *ofs);
444
+
445
+/*
446
+ * Can we iterate real dir directly?
447
+ *
448
+ * Non-merge dir may contain whiteouts from a time it was a merge upper, before
449
+ * lower dir was removed under it and possibly before it was rotated from upper
450
+ * to lower layer.
451
+ */
452
+static inline bool ovl_dir_is_real(struct dentry *dir)
453
+{
454
+ return !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir));
455
+}
341456
342457 /* inode.c */
343458 int ovl_set_nlink_upper(struct dentry *dentry);
344459 int ovl_set_nlink_lower(struct dentry *dentry);
345
-unsigned int ovl_get_nlink(struct dentry *lowerdentry,
460
+unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
346461 struct dentry *upperdentry,
347462 unsigned int fallback);
348463 int ovl_setattr(struct dentry *dentry, struct iattr *attr);
....@@ -352,23 +467,23 @@
352467 int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
353468 const void *value, size_t size, int flags);
354469 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);
470
+ void *value, size_t size, int flags);
358471 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
359472 struct posix_acl *ovl_get_acl(struct inode *inode, int type);
360473 int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags);
361
-bool ovl_is_private_xattr(const char *name);
474
+bool ovl_is_private_xattr(struct super_block *sb, const char *name);
362475
363476 struct ovl_inode_params {
364477 struct inode *newinode;
365478 struct dentry *upperdentry;
366479 struct ovl_path *lowerpath;
367
- struct dentry *index;
480
+ bool index;
368481 unsigned int numlower;
369482 char *redirect;
370483 struct dentry *lowerdata;
371484 };
485
+void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
486
+ unsigned long ino, int fsid);
372487 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev);
373488 struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
374489 bool is_upper);
....@@ -396,7 +511,7 @@
396511
397512 /* dir.c */
398513 extern const struct inode_operations ovl_dir_inode_operations;
399
-int ovl_cleanup_and_whiteout(struct dentry *workdir, struct inode *dir,
514
+int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
400515 struct dentry *dentry);
401516 struct ovl_cattr {
402517 dev_t rdev;
....@@ -411,17 +526,22 @@
411526 struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
412527 struct ovl_cattr *attr);
413528 int ovl_cleanup(struct inode *dir, struct dentry *dentry);
529
+struct dentry *ovl_lookup_temp(struct dentry *workdir);
414530 struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr);
415531
416532 /* file.c */
417533 extern const struct file_operations ovl_file_operations;
534
+int __init ovl_aio_request_cache_init(void);
535
+void ovl_aio_request_cache_destroy(void);
536
+long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
537
+long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
418538
419539 /* copy_up.c */
420540 int ovl_copy_up(struct dentry *dentry);
421541 int ovl_copy_up_with_data(struct dentry *dentry);
422
-int ovl_copy_up_flags(struct dentry *dentry, int flags);
423542 int ovl_maybe_copy_up(struct dentry *dentry, int flags);
424
-int ovl_copy_xattr(struct dentry *old, struct dentry *new);
543
+int ovl_copy_xattr(struct super_block *sb, struct dentry *old,
544
+ struct dentry *new);
425545 int ovl_set_attr(struct dentry *upper, struct kstat *stat);
426546 struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper);
427547 int ovl_set_origin(struct dentry *dentry, struct dentry *lower,