1 /* 2 * sysfs.h - definitions for the device driver filesystem 3 * 4 * Copyright (c) 2001,2002 Patrick Mochel 5 * Copyright (c) 2004 Silicon Graphics, Inc. 6 * Copyright (c) 2007 SUSE Linux Products GmbH 7 * Copyright (c) 2007 Tejun Heo <[email protected]> 8 * 9 * Please see Documentation/filesystems/sysfs.txt for more information. 10 */ 11 12 #ifndef _SYSFS_H_ 13 #define _SYSFS_H_ 14 15 #include <linux/kernfs.h> 16 #include <linux/compiler.h> 17 #include <linux/errno.h> 18 #include <linux/list.h> 19 #include <linux/lockdep.h> 20 #include <linux/kobject_ns.h> 21 #include <linux/stat.h> 22 #include <linux/atomic.h> 23 24 struct kobject; 25 struct module; 26 struct bin_attribute; 27 enum kobj_ns_type; 28 29 struct attribute { 30 const char *name; 31 umode_t mode; 32 #ifdef CONFIG_DEBUG_LOCK_ALLOC 33 bool ignore_lockdep:1; 34 struct lock_class_key *key; 35 struct lock_class_key skey; 36 #endif 37 }; 38 39 /** 40 * sysfs_attr_init - initialize a dynamically allocated sysfs attribute 41 * @attr: struct attribute to initialize 42 * 43 * Initialize a dynamically allocated struct attribute so we can 44 * make lockdep happy. This is a new requirement for attributes 45 * and initially this is only needed when lockdep is enabled. 46 * Lockdep gives a nice error when your attribute is added to 47 * sysfs if you don't have this. 48 */ 49 #ifdef CONFIG_DEBUG_LOCK_ALLOC 50 #define sysfs_attr_init(attr) \ 51 do { \ 52 static struct lock_class_key __key; \ 53 \ 54 (attr)->key = &__key; \ 55 } while (0) 56 #else 57 #define sysfs_attr_init(attr) do {} while (0) 58 #endif 59 60 /** 61 * struct attribute_group - data structure used to declare an attribute group. 62 * @name: Optional: Attribute group name 63 * If specified, the attribute group will be created in 64 * a new subdirectory with this name. 65 * @is_visible: Optional: Function to return permissions associated with an 66 * attribute of the group. Will be called repeatedly for each 67 * attribute in the group. Only read/write permissions as well as 68 * SYSFS_PREALLOC are accepted. Must return 0 if an attribute is 69 * not visible. The returned value will replace static permissions 70 * defined in struct attribute or struct bin_attribute. 71 * @attrs: Pointer to NULL terminated list of attributes. 72 * @bin_attrs: Pointer to NULL terminated list of binary attributes. 73 * Either attrs or bin_attrs or both must be provided. 74 */ 75 struct attribute_group { 76 const char *name; 77 umode_t (*is_visible)(struct kobject *, 78 struct attribute *, int); 79 struct attribute **attrs; 80 struct bin_attribute **bin_attrs; 81 }; 82 83 /** 84 * Use these macros to make defining attributes easier. See include/linux/device.h 85 * for examples.. 86 */ 87 88 #define SYSFS_PREALLOC 010000 89 90 #define __ATTR(_name, _mode, _show, _store) { \ 91 .attr = {.name = __stringify(_name), \ 92 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \ 93 .show = _show, \ 94 .store = _store, \ 95 } 96 97 #define __ATTR_PREALLOC(_name, _mode, _show, _store) { \ 98 .attr = {.name = __stringify(_name), \ 99 .mode = SYSFS_PREALLOC | VERIFY_OCTAL_PERMISSIONS(_mode) },\ 100 .show = _show, \ 101 .store = _store, \ 102 } 103 104 #define __ATTR_RO(_name) { \ 105 .attr = { .name = __stringify(_name), .mode = S_IRUGO }, \ 106 .show = _name##_show, \ 107 } 108 109 #define __ATTR_WO(_name) { \ 110 .attr = { .name = __stringify(_name), .mode = S_IWUSR }, \ 111 .store = _name##_store, \ 112 } 113 114 #define __ATTR_RW(_name) __ATTR(_name, (S_IWUSR | S_IRUGO), \ 115 _name##_show, _name##_store) 116 117 #define __ATTR_NULL { .attr = { .name = NULL } } 118 119 #ifdef CONFIG_DEBUG_LOCK_ALLOC 120 #define __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) { \ 121 .attr = {.name = __stringify(_name), .mode = _mode, \ 122 .ignore_lockdep = true }, \ 123 .show = _show, \ 124 .store = _store, \ 125 } 126 #else 127 #define __ATTR_IGNORE_LOCKDEP __ATTR 128 #endif 129 130 #define __ATTRIBUTE_GROUPS(_name) \ 131 static const struct attribute_group *_name##_groups[] = { \ 132 &_name##_group, \ 133 NULL, \ 134 } 135 136 #define ATTRIBUTE_GROUPS(_name) \ 137 static const struct attribute_group _name##_group = { \ 138 .attrs = _name##_attrs, \ 139 }; \ 140 __ATTRIBUTE_GROUPS(_name) 141 142 struct file; 143 struct vm_area_struct; 144 145 struct bin_attribute { 146 struct attribute attr; 147 size_t size; 148 void *private; 149 ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *, 150 char *, loff_t, size_t); 151 ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *, 152 char *, loff_t, size_t); 153 int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr, 154 struct vm_area_struct *vma); 155 }; 156 157 /** 158 * sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute 159 * @attr: struct bin_attribute to initialize 160 * 161 * Initialize a dynamically allocated struct bin_attribute so we 162 * can make lockdep happy. This is a new requirement for 163 * attributes and initially this is only needed when lockdep is 164 * enabled. Lockdep gives a nice error when your attribute is 165 * added to sysfs if you don't have this. 166 */ 167 #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr) 168 169 /* macros to create static binary attributes easier */ 170 #define __BIN_ATTR(_name, _mode, _read, _write, _size) { \ 171 .attr = { .name = __stringify(_name), .mode = _mode }, \ 172 .read = _read, \ 173 .write = _write, \ 174 .size = _size, \ 175 } 176 177 #define __BIN_ATTR_RO(_name, _size) { \ 178 .attr = { .name = __stringify(_name), .mode = S_IRUGO }, \ 179 .read = _name##_read, \ 180 .size = _size, \ 181 } 182 183 #define __BIN_ATTR_RW(_name, _size) __BIN_ATTR(_name, \ 184 (S_IWUSR | S_IRUGO), _name##_read, \ 185 _name##_write, _size) 186 187 #define __BIN_ATTR_NULL __ATTR_NULL 188 189 #define BIN_ATTR(_name, _mode, _read, _write, _size) \ 190 struct bin_attribute bin_attr_##_name = __BIN_ATTR(_name, _mode, _read, \ 191 _write, _size) 192 193 #define BIN_ATTR_RO(_name, _size) \ 194 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO(_name, _size) 195 196 #define BIN_ATTR_RW(_name, _size) \ 197 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size) 198 199 struct sysfs_ops { 200 ssize_t (*show)(struct kobject *, struct attribute *, char *); 201 ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t); 202 }; 203 204 #ifdef CONFIG_SYSFS 205 206 int __must_check sysfs_create_dir_ns(struct kobject *kobj, const void *ns); 207 void sysfs_remove_dir(struct kobject *kobj); 208 int __must_check sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name, 209 const void *new_ns); 210 int __must_check sysfs_move_dir_ns(struct kobject *kobj, 211 struct kobject *new_parent_kobj, 212 const void *new_ns); 213 int __must_check sysfs_create_mount_point(struct kobject *parent_kobj, 214 const char *name); 215 void sysfs_remove_mount_point(struct kobject *parent_kobj, 216 const char *name); 217 218 int __must_check sysfs_create_file_ns(struct kobject *kobj, 219 const struct attribute *attr, 220 const void *ns); 221 int __must_check sysfs_create_files(struct kobject *kobj, 222 const struct attribute **attr); 223 int __must_check sysfs_chmod_file(struct kobject *kobj, 224 const struct attribute *attr, umode_t mode); 225 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr, 226 const void *ns); 227 bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr); 228 void sysfs_remove_files(struct kobject *kobj, const struct attribute **attr); 229 230 int __must_check sysfs_create_bin_file(struct kobject *kobj, 231 const struct bin_attribute *attr); 232 void sysfs_remove_bin_file(struct kobject *kobj, 233 const struct bin_attribute *attr); 234 235 int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target, 236 const char *name); 237 int __must_check sysfs_create_link_nowarn(struct kobject *kobj, 238 struct kobject *target, 239 const char *name); 240 void sysfs_remove_link(struct kobject *kobj, const char *name); 241 242 int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target, 243 const char *old_name, const char *new_name, 244 const void *new_ns); 245 246 void sysfs_delete_link(struct kobject *dir, struct kobject *targ, 247 const char *name); 248 249 int __must_check sysfs_create_group(struct kobject *kobj, 250 const struct attribute_group *grp); 251 int __must_check sysfs_create_groups(struct kobject *kobj, 252 const struct attribute_group **groups); 253 int sysfs_update_group(struct kobject *kobj, 254 const struct attribute_group *grp); 255 void sysfs_remove_group(struct kobject *kobj, 256 const struct attribute_group *grp); 257 void sysfs_remove_groups(struct kobject *kobj, 258 const struct attribute_group **groups); 259 int sysfs_add_file_to_group(struct kobject *kobj, 260 const struct attribute *attr, const char *group); 261 void sysfs_remove_file_from_group(struct kobject *kobj, 262 const struct attribute *attr, const char *group); 263 int sysfs_merge_group(struct kobject *kobj, 264 const struct attribute_group *grp); 265 void sysfs_unmerge_group(struct kobject *kobj, 266 const struct attribute_group *grp); 267 int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name, 268 struct kobject *target, const char *link_name); 269 void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name, 270 const char *link_name); 271 int __compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj, 272 struct kobject *target_kobj, 273 const char *target_name); 274 275 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr); 276 277 int __must_check sysfs_init(void); 278 279 static inline void sysfs_enable_ns(struct kernfs_node *kn) 280 { 281 return kernfs_enable_ns(kn); 282 } 283 284 #else /* CONFIG_SYSFS */ 285 286 static inline int sysfs_create_dir_ns(struct kobject *kobj, const void *ns) 287 { 288 return 0; 289 } 290 291 static inline void sysfs_remove_dir(struct kobject *kobj) 292 { 293 } 294 295 static inline int sysfs_rename_dir_ns(struct kobject *kobj, 296 const char *new_name, const void *new_ns) 297 { 298 return 0; 299 } 300 301 static inline int sysfs_move_dir_ns(struct kobject *kobj, 302 struct kobject *new_parent_kobj, 303 const void *new_ns) 304 { 305 return 0; 306 } 307 308 static inline int sysfs_create_mount_point(struct kobject *parent_kobj, 309 const char *name) 310 { 311 return 0; 312 } 313 314 static inline void sysfs_remove_mount_point(struct kobject *parent_kobj, 315 const char *name) 316 { 317 } 318 319 static inline int sysfs_create_file_ns(struct kobject *kobj, 320 const struct attribute *attr, 321 const void *ns) 322 { 323 return 0; 324 } 325 326 static inline int sysfs_create_files(struct kobject *kobj, 327 const struct attribute **attr) 328 { 329 return 0; 330 } 331 332 static inline int sysfs_chmod_file(struct kobject *kobj, 333 const struct attribute *attr, umode_t mode) 334 { 335 return 0; 336 } 337 338 static inline void sysfs_remove_file_ns(struct kobject *kobj, 339 const struct attribute *attr, 340 const void *ns) 341 { 342 } 343 344 static inline bool sysfs_remove_file_self(struct kobject *kobj, 345 const struct attribute *attr) 346 { 347 return false; 348 } 349 350 static inline void sysfs_remove_files(struct kobject *kobj, 351 const struct attribute **attr) 352 { 353 } 354 355 static inline int sysfs_create_bin_file(struct kobject *kobj, 356 const struct bin_attribute *attr) 357 { 358 return 0; 359 } 360 361 static inline void sysfs_remove_bin_file(struct kobject *kobj, 362 const struct bin_attribute *attr) 363 { 364 } 365 366 static inline int sysfs_create_link(struct kobject *kobj, 367 struct kobject *target, const char *name) 368 { 369 return 0; 370 } 371 372 static inline int sysfs_create_link_nowarn(struct kobject *kobj, 373 struct kobject *target, 374 const char *name) 375 { 376 return 0; 377 } 378 379 static inline void sysfs_remove_link(struct kobject *kobj, const char *name) 380 { 381 } 382 383 static inline int sysfs_rename_link_ns(struct kobject *k, struct kobject *t, 384 const char *old_name, 385 const char *new_name, const void *ns) 386 { 387 return 0; 388 } 389 390 static inline void sysfs_delete_link(struct kobject *k, struct kobject *t, 391 const char *name) 392 { 393 } 394 395 static inline int sysfs_create_group(struct kobject *kobj, 396 const struct attribute_group *grp) 397 { 398 return 0; 399 } 400 401 static inline int sysfs_create_groups(struct kobject *kobj, 402 const struct attribute_group **groups) 403 { 404 return 0; 405 } 406 407 static inline int sysfs_update_group(struct kobject *kobj, 408 const struct attribute_group *grp) 409 { 410 return 0; 411 } 412 413 static inline void sysfs_remove_group(struct kobject *kobj, 414 const struct attribute_group *grp) 415 { 416 } 417 418 static inline void sysfs_remove_groups(struct kobject *kobj, 419 const struct attribute_group **groups) 420 { 421 } 422 423 static inline int sysfs_add_file_to_group(struct kobject *kobj, 424 const struct attribute *attr, const char *group) 425 { 426 return 0; 427 } 428 429 static inline void sysfs_remove_file_from_group(struct kobject *kobj, 430 const struct attribute *attr, const char *group) 431 { 432 } 433 434 static inline int sysfs_merge_group(struct kobject *kobj, 435 const struct attribute_group *grp) 436 { 437 return 0; 438 } 439 440 static inline void sysfs_unmerge_group(struct kobject *kobj, 441 const struct attribute_group *grp) 442 { 443 } 444 445 static inline int sysfs_add_link_to_group(struct kobject *kobj, 446 const char *group_name, struct kobject *target, 447 const char *link_name) 448 { 449 return 0; 450 } 451 452 static inline void sysfs_remove_link_from_group(struct kobject *kobj, 453 const char *group_name, const char *link_name) 454 { 455 } 456 457 static inline int __compat_only_sysfs_link_entry_to_kobj( 458 struct kobject *kobj, 459 struct kobject *target_kobj, 460 const char *target_name) 461 { 462 return 0; 463 } 464 465 static inline void sysfs_notify(struct kobject *kobj, const char *dir, 466 const char *attr) 467 { 468 } 469 470 static inline int __must_check sysfs_init(void) 471 { 472 return 0; 473 } 474 475 static inline void sysfs_enable_ns(struct kernfs_node *kn) 476 { 477 } 478 479 #endif /* CONFIG_SYSFS */ 480 481 static inline int __must_check sysfs_create_file(struct kobject *kobj, 482 const struct attribute *attr) 483 { 484 return sysfs_create_file_ns(kobj, attr, NULL); 485 } 486 487 static inline void sysfs_remove_file(struct kobject *kobj, 488 const struct attribute *attr) 489 { 490 sysfs_remove_file_ns(kobj, attr, NULL); 491 } 492 493 static inline int sysfs_rename_link(struct kobject *kobj, struct kobject *target, 494 const char *old_name, const char *new_name) 495 { 496 return sysfs_rename_link_ns(kobj, target, old_name, new_name, NULL); 497 } 498 499 static inline void sysfs_notify_dirent(struct kernfs_node *kn) 500 { 501 kernfs_notify(kn); 502 } 503 504 static inline struct kernfs_node *sysfs_get_dirent(struct kernfs_node *parent, 505 const unsigned char *name) 506 { 507 return kernfs_find_and_get(parent, name); 508 } 509 510 static inline struct kernfs_node *sysfs_get(struct kernfs_node *kn) 511 { 512 kernfs_get(kn); 513 return kn; 514 } 515 516 static inline void sysfs_put(struct kernfs_node *kn) 517 { 518 kernfs_put(kn); 519 } 520 521 #endif /* _SYSFS_H_ */ 522