0001
0002
0003
0004
0005
0006 #ifndef __LINUX_KERNFS_H
0007 #define __LINUX_KERNFS_H
0008
0009 #include <linux/err.h>
0010 #include <linux/list.h>
0011 #include <linux/mutex.h>
0012 #include <linux/idr.h>
0013 #include <linux/lockdep.h>
0014 #include <linux/rbtree.h>
0015 #include <linux/atomic.h>
0016 #include <linux/bug.h>
0017 #include <linux/types.h>
0018 #include <linux/uidgid.h>
0019 #include <linux/wait.h>
0020 #include <linux/rwsem.h>
0021 #include <linux/cache.h>
0022
0023 struct file;
0024 struct dentry;
0025 struct iattr;
0026 struct seq_file;
0027 struct vm_area_struct;
0028 struct vm_operations_struct;
0029 struct super_block;
0030 struct file_system_type;
0031 struct poll_table_struct;
0032 struct fs_context;
0033
0034 struct kernfs_fs_context;
0035 struct kernfs_open_node;
0036 struct kernfs_iattrs;
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062 #ifdef CONFIG_SMP
0063 #define NR_KERNFS_LOCK_BITS (2 * (ilog2(NR_CPUS < 32 ? NR_CPUS : 32)))
0064 #else
0065 #define NR_KERNFS_LOCK_BITS 1
0066 #endif
0067
0068 #define NR_KERNFS_LOCKS (1 << NR_KERNFS_LOCK_BITS)
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090 struct kernfs_global_locks {
0091 struct mutex open_file_mutex[NR_KERNFS_LOCKS];
0092 };
0093
0094 enum kernfs_node_type {
0095 KERNFS_DIR = 0x0001,
0096 KERNFS_FILE = 0x0002,
0097 KERNFS_LINK = 0x0004,
0098 };
0099
0100 #define KERNFS_TYPE_MASK 0x000f
0101 #define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
0102 #define KERNFS_MAX_USER_XATTRS 128
0103 #define KERNFS_USER_XATTR_SIZE_LIMIT (128 << 10)
0104
0105 enum kernfs_node_flag {
0106 KERNFS_ACTIVATED = 0x0010,
0107 KERNFS_NS = 0x0020,
0108 KERNFS_HAS_SEQ_SHOW = 0x0040,
0109 KERNFS_HAS_MMAP = 0x0080,
0110 KERNFS_LOCKDEP = 0x0100,
0111 KERNFS_SUICIDAL = 0x0400,
0112 KERNFS_SUICIDED = 0x0800,
0113 KERNFS_EMPTY_DIR = 0x1000,
0114 KERNFS_HAS_RELEASE = 0x2000,
0115 };
0116
0117
0118 enum kernfs_root_flag {
0119
0120
0121
0122
0123
0124
0125 KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001,
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK = 0x0002,
0137
0138
0139
0140
0141
0142 KERNFS_ROOT_SUPPORT_EXPORTOP = 0x0004,
0143
0144
0145
0146
0147 KERNFS_ROOT_SUPPORT_USER_XATTR = 0x0008,
0148 };
0149
0150
0151 struct kernfs_elem_dir {
0152 unsigned long subdirs;
0153
0154 struct rb_root children;
0155
0156
0157
0158
0159
0160 struct kernfs_root *root;
0161
0162
0163
0164
0165 unsigned long rev;
0166 };
0167
0168 struct kernfs_elem_symlink {
0169 struct kernfs_node *target_kn;
0170 };
0171
0172 struct kernfs_elem_attr {
0173 const struct kernfs_ops *ops;
0174 struct kernfs_open_node __rcu *open;
0175 loff_t size;
0176 struct kernfs_node *notify_next;
0177 };
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188 struct kernfs_node {
0189 atomic_t count;
0190 atomic_t active;
0191 #ifdef CONFIG_DEBUG_LOCK_ALLOC
0192 struct lockdep_map dep_map;
0193 #endif
0194
0195
0196
0197
0198
0199
0200 struct kernfs_node *parent;
0201 const char *name;
0202
0203 struct rb_node rb;
0204
0205 const void *ns;
0206 unsigned int hash;
0207 union {
0208 struct kernfs_elem_dir dir;
0209 struct kernfs_elem_symlink symlink;
0210 struct kernfs_elem_attr attr;
0211 };
0212
0213 void *priv;
0214
0215
0216
0217
0218
0219 u64 id;
0220
0221 unsigned short flags;
0222 umode_t mode;
0223 struct kernfs_iattrs *iattr;
0224 };
0225
0226
0227
0228
0229
0230
0231
0232
0233 struct kernfs_syscall_ops {
0234 int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
0235
0236 int (*mkdir)(struct kernfs_node *parent, const char *name,
0237 umode_t mode);
0238 int (*rmdir)(struct kernfs_node *kn);
0239 int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
0240 const char *new_name);
0241 int (*show_path)(struct seq_file *sf, struct kernfs_node *kn,
0242 struct kernfs_root *root);
0243 };
0244
0245 struct kernfs_node *kernfs_root_to_node(struct kernfs_root *root);
0246
0247 struct kernfs_open_file {
0248
0249 struct kernfs_node *kn;
0250 struct file *file;
0251 struct seq_file *seq_file;
0252 void *priv;
0253
0254
0255 struct mutex mutex;
0256 struct mutex prealloc_mutex;
0257 int event;
0258 struct list_head list;
0259 char *prealloc_buf;
0260
0261 size_t atomic_write_len;
0262 bool mmapped:1;
0263 bool released:1;
0264 const struct vm_operations_struct *vm_ops;
0265 };
0266
0267 struct kernfs_ops {
0268
0269
0270
0271
0272 int (*open)(struct kernfs_open_file *of);
0273 void (*release)(struct kernfs_open_file *of);
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286 int (*seq_show)(struct seq_file *sf, void *v);
0287
0288 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
0289 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
0290 void (*seq_stop)(struct seq_file *sf, void *v);
0291
0292 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
0293 loff_t off);
0294
0295
0296
0297
0298
0299
0300
0301
0302 size_t atomic_write_len;
0303
0304
0305
0306
0307
0308
0309 bool prealloc;
0310 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
0311 loff_t off);
0312
0313 __poll_t (*poll)(struct kernfs_open_file *of,
0314 struct poll_table_struct *pt);
0315
0316 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
0317 };
0318
0319
0320
0321
0322 struct kernfs_fs_context {
0323 struct kernfs_root *root;
0324 void *ns_tag;
0325 unsigned long magic;
0326
0327
0328 bool new_sb_created;
0329 };
0330
0331 #ifdef CONFIG_KERNFS
0332
0333 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
0334 {
0335 return kn->flags & KERNFS_TYPE_MASK;
0336 }
0337
0338 static inline ino_t kernfs_id_ino(u64 id)
0339 {
0340
0341 if (sizeof(ino_t) >= sizeof(u64))
0342 return id;
0343 else
0344 return (u32)id;
0345 }
0346
0347 static inline u32 kernfs_id_gen(u64 id)
0348 {
0349
0350 if (sizeof(ino_t) >= sizeof(u64))
0351 return 1;
0352 else
0353 return id >> 32;
0354 }
0355
0356 static inline ino_t kernfs_ino(struct kernfs_node *kn)
0357 {
0358 return kernfs_id_ino(kn->id);
0359 }
0360
0361 static inline ino_t kernfs_gen(struct kernfs_node *kn)
0362 {
0363 return kernfs_id_gen(kn->id);
0364 }
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374 static inline void kernfs_enable_ns(struct kernfs_node *kn)
0375 {
0376 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
0377 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
0378 kn->flags |= KERNFS_NS;
0379 }
0380
0381
0382
0383
0384
0385
0386
0387 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
0388 {
0389 return kn->flags & KERNFS_NS;
0390 }
0391
0392 int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
0393 int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
0394 char *buf, size_t buflen);
0395 void pr_cont_kernfs_name(struct kernfs_node *kn);
0396 void pr_cont_kernfs_path(struct kernfs_node *kn);
0397 struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
0398 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
0399 const char *name, const void *ns);
0400 struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
0401 const char *path, const void *ns);
0402 void kernfs_get(struct kernfs_node *kn);
0403 void kernfs_put(struct kernfs_node *kn);
0404
0405 struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry);
0406 struct kernfs_root *kernfs_root_from_sb(struct super_block *sb);
0407 struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn);
0408
0409 struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
0410 struct super_block *sb);
0411 struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
0412 unsigned int flags, void *priv);
0413 void kernfs_destroy_root(struct kernfs_root *root);
0414
0415 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
0416 const char *name, umode_t mode,
0417 kuid_t uid, kgid_t gid,
0418 void *priv, const void *ns);
0419 struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
0420 const char *name);
0421 struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
0422 const char *name, umode_t mode,
0423 kuid_t uid, kgid_t gid,
0424 loff_t size,
0425 const struct kernfs_ops *ops,
0426 void *priv, const void *ns,
0427 struct lock_class_key *key);
0428 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
0429 const char *name,
0430 struct kernfs_node *target);
0431 void kernfs_activate(struct kernfs_node *kn);
0432 void kernfs_remove(struct kernfs_node *kn);
0433 void kernfs_break_active_protection(struct kernfs_node *kn);
0434 void kernfs_unbreak_active_protection(struct kernfs_node *kn);
0435 bool kernfs_remove_self(struct kernfs_node *kn);
0436 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
0437 const void *ns);
0438 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
0439 const char *new_name, const void *new_ns);
0440 int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
0441 __poll_t kernfs_generic_poll(struct kernfs_open_file *of,
0442 struct poll_table_struct *pt);
0443 void kernfs_notify(struct kernfs_node *kn);
0444
0445 int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
0446 void *value, size_t size);
0447 int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
0448 const void *value, size_t size, int flags);
0449
0450 const void *kernfs_super_ns(struct super_block *sb);
0451 int kernfs_get_tree(struct fs_context *fc);
0452 void kernfs_free_fs_context(struct fs_context *fc);
0453 void kernfs_kill_sb(struct super_block *sb);
0454
0455 void kernfs_init(void);
0456
0457 struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
0458 u64 id);
0459 #else
0460
0461 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
0462 { return 0; }
0463
0464 static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
0465
0466 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
0467 { return false; }
0468
0469 static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
0470 { return -ENOSYS; }
0471
0472 static inline int kernfs_path_from_node(struct kernfs_node *root_kn,
0473 struct kernfs_node *kn,
0474 char *buf, size_t buflen)
0475 { return -ENOSYS; }
0476
0477 static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
0478 static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
0479
0480 static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
0481 { return NULL; }
0482
0483 static inline struct kernfs_node *
0484 kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
0485 const void *ns)
0486 { return NULL; }
0487 static inline struct kernfs_node *
0488 kernfs_walk_and_get_ns(struct kernfs_node *parent, const char *path,
0489 const void *ns)
0490 { return NULL; }
0491
0492 static inline void kernfs_get(struct kernfs_node *kn) { }
0493 static inline void kernfs_put(struct kernfs_node *kn) { }
0494
0495 static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
0496 { return NULL; }
0497
0498 static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
0499 { return NULL; }
0500
0501 static inline struct inode *
0502 kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
0503 { return NULL; }
0504
0505 static inline struct kernfs_root *
0506 kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
0507 void *priv)
0508 { return ERR_PTR(-ENOSYS); }
0509
0510 static inline void kernfs_destroy_root(struct kernfs_root *root) { }
0511
0512 static inline struct kernfs_node *
0513 kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
0514 umode_t mode, kuid_t uid, kgid_t gid,
0515 void *priv, const void *ns)
0516 { return ERR_PTR(-ENOSYS); }
0517
0518 static inline struct kernfs_node *
0519 __kernfs_create_file(struct kernfs_node *parent, const char *name,
0520 umode_t mode, kuid_t uid, kgid_t gid,
0521 loff_t size, const struct kernfs_ops *ops,
0522 void *priv, const void *ns, struct lock_class_key *key)
0523 { return ERR_PTR(-ENOSYS); }
0524
0525 static inline struct kernfs_node *
0526 kernfs_create_link(struct kernfs_node *parent, const char *name,
0527 struct kernfs_node *target)
0528 { return ERR_PTR(-ENOSYS); }
0529
0530 static inline void kernfs_activate(struct kernfs_node *kn) { }
0531
0532 static inline void kernfs_remove(struct kernfs_node *kn) { }
0533
0534 static inline bool kernfs_remove_self(struct kernfs_node *kn)
0535 { return false; }
0536
0537 static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
0538 const char *name, const void *ns)
0539 { return -ENOSYS; }
0540
0541 static inline int kernfs_rename_ns(struct kernfs_node *kn,
0542 struct kernfs_node *new_parent,
0543 const char *new_name, const void *new_ns)
0544 { return -ENOSYS; }
0545
0546 static inline int kernfs_setattr(struct kernfs_node *kn,
0547 const struct iattr *iattr)
0548 { return -ENOSYS; }
0549
0550 static inline void kernfs_notify(struct kernfs_node *kn) { }
0551
0552 static inline int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
0553 void *value, size_t size)
0554 { return -ENOSYS; }
0555
0556 static inline int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
0557 const void *value, size_t size, int flags)
0558 { return -ENOSYS; }
0559
0560 static inline const void *kernfs_super_ns(struct super_block *sb)
0561 { return NULL; }
0562
0563 static inline int kernfs_get_tree(struct fs_context *fc)
0564 { return -ENOSYS; }
0565
0566 static inline void kernfs_free_fs_context(struct fs_context *fc) { }
0567
0568 static inline void kernfs_kill_sb(struct super_block *sb) { }
0569
0570 static inline void kernfs_init(void) { }
0571
0572 #endif
0573
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586 static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
0587 {
0588 return kernfs_path_from_node(kn, NULL, buf, buflen);
0589 }
0590
0591 static inline struct kernfs_node *
0592 kernfs_find_and_get(struct kernfs_node *kn, const char *name)
0593 {
0594 return kernfs_find_and_get_ns(kn, name, NULL);
0595 }
0596
0597 static inline struct kernfs_node *
0598 kernfs_walk_and_get(struct kernfs_node *kn, const char *path)
0599 {
0600 return kernfs_walk_and_get_ns(kn, path, NULL);
0601 }
0602
0603 static inline struct kernfs_node *
0604 kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
0605 void *priv)
0606 {
0607 return kernfs_create_dir_ns(parent, name, mode,
0608 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
0609 priv, NULL);
0610 }
0611
0612 static inline int kernfs_remove_by_name(struct kernfs_node *parent,
0613 const char *name)
0614 {
0615 return kernfs_remove_by_name_ns(parent, name, NULL);
0616 }
0617
0618 static inline int kernfs_rename(struct kernfs_node *kn,
0619 struct kernfs_node *new_parent,
0620 const char *new_name)
0621 {
0622 return kernfs_rename_ns(kn, new_parent, new_name, NULL);
0623 }
0624
0625 #endif