Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'driver-core-3.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core

Pull driver core / sysfs patches from Greg KH:
"Here's the big driver core and sysfs patch set for 3.14-rc1.

There's a lot of work here moving sysfs logic out into a "kernfs" to
allow other subsystems to also have a virtual filesystem with the same
attributes of sysfs (handle device disconnect, dynamic creation /
removal as needed / unneeded, etc)

This is primarily being done for the cgroups filesystem, but the goal
is to also move debugfs to it when it is ready, solving all of the
known issues in that filesystem as well. The code isn't completed
yet, but all should be stable now (there is a big section that was
reverted due to problems found when testing)

There's also some other smaller fixes, and a driver core addition that
allows for a "collection" of objects, that the DRM people will be
using soon (it's in this tree to make merges after -rc1 easier)

All of this has been in linux-next with no reported issues"

* tag 'driver-core-3.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core: (113 commits)
kernfs: associate a new kernfs_node with its parent on creation
kernfs: add struct dentry declaration in kernfs.h
kernfs: fix get_active failure handling in kernfs_seq_*()
Revert "kernfs: fix get_active failure handling in kernfs_seq_*()"
Revert "kernfs: replace kernfs_node->u.completion with kernfs_root->deactivate_waitq"
Revert "kernfs: remove KERNFS_ACTIVE_REF and add kernfs_lockdep()"
Revert "kernfs: remove KERNFS_REMOVED"
Revert "kernfs: restructure removal path to fix possible premature return"
Revert "kernfs: invoke kernfs_unmap_bin_file() directly from __kernfs_remove()"
Revert "kernfs: remove kernfs_addrm_cxt"
Revert "kernfs: make kernfs_get_active() block if the node is deactivated but not removed"
Revert "kernfs: implement kernfs_{de|re}activate[_self]()"
Revert "kernfs, sysfs, driver-core: implement kernfs_remove_self() and its wrappers"
Revert "pci: use device_remove_file_self() instead of device_schedule_callback()"
Revert "scsi: use device_remove_file_self() instead of device_schedule_callback()"
Revert "s390: use device_remove_file_self() instead of device_schedule_callback()"
Revert "sysfs, driver-core: remove unused {sysfs|device}_schedule_callback_owner()"
Revert "kernfs: remove unnecessary NULL check in __kernfs_remove()"
kernfs: remove unnecessary NULL check in __kernfs_remove()
drivers/base: provide an infrastructure for componentised subsystems
...

+4191 -2919
+116
Documentation/driver-model/design-patterns.txt
··· 1 + 2 + Device Driver Design Patterns 3 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 + 5 + This document describes a few common design patterns found in device drivers. 6 + It is likely that subsystem maintainers will ask driver developers to 7 + conform to these design patterns. 8 + 9 + 1. State Container 10 + 2. container_of() 11 + 12 + 13 + 1. State Container 14 + ~~~~~~~~~~~~~~~~~~ 15 + 16 + While the kernel contains a few device drivers that assume that they will 17 + only be probed() once on a certain system (singletons), it is custom to assume 18 + that the device the driver binds to will appear in several instances. This 19 + means that the probe() function and all callbacks need to be reentrant. 20 + 21 + The most common way to achieve this is to use the state container design 22 + pattern. It usually has this form: 23 + 24 + struct foo { 25 + spinlock_t lock; /* Example member */ 26 + (...) 27 + }; 28 + 29 + static int foo_probe(...) 30 + { 31 + struct foo *foo; 32 + 33 + foo = devm_kzalloc(dev, sizeof(*foo), GFP_KERNEL); 34 + if (!foo) 35 + return -ENOMEM; 36 + spin_lock_init(&foo->lock); 37 + (...) 38 + } 39 + 40 + This will create an instance of struct foo in memory every time probe() is 41 + called. This is our state container for this instance of the device driver. 42 + Of course it is then necessary to always pass this instance of the 43 + state around to all functions that need access to the state and its members. 44 + 45 + For example, if the driver is registering an interrupt handler, you would 46 + pass around a pointer to struct foo like this: 47 + 48 + static irqreturn_t foo_handler(int irq, void *arg) 49 + { 50 + struct foo *foo = arg; 51 + (...) 52 + } 53 + 54 + static int foo_probe(...) 55 + { 56 + struct foo *foo; 57 + 58 + (...) 59 + ret = request_irq(irq, foo_handler, 0, "foo", foo); 60 + } 61 + 62 + This way you always get a pointer back to the correct instance of foo in 63 + your interrupt handler. 64 + 65 + 66 + 2. container_of() 67 + ~~~~~~~~~~~~~~~~~ 68 + 69 + Continuing on the above example we add an offloaded work: 70 + 71 + struct foo { 72 + spinlock_t lock; 73 + struct workqueue_struct *wq; 74 + struct work_struct offload; 75 + (...) 76 + }; 77 + 78 + static void foo_work(struct work_struct *work) 79 + { 80 + struct foo *foo = container_of(work, struct foo, offload); 81 + 82 + (...) 83 + } 84 + 85 + static irqreturn_t foo_handler(int irq, void *arg) 86 + { 87 + struct foo *foo = arg; 88 + 89 + queue_work(foo->wq, &foo->offload); 90 + (...) 91 + } 92 + 93 + static int foo_probe(...) 94 + { 95 + struct foo *foo; 96 + 97 + foo->wq = create_singlethread_workqueue("foo-wq"); 98 + INIT_WORK(&foo->offload, foo_work); 99 + (...) 100 + } 101 + 102 + The design pattern is the same for an hrtimer or something similar that will 103 + return a single argument which is a pointer to a struct member in the 104 + callback. 105 + 106 + container_of() is a macro defined in <linux/kernel.h> 107 + 108 + What container_of() does is to obtain a pointer to the containing struct from 109 + a pointer to a member by a simple subtraction using the offsetof() macro from 110 + standard C, which allows something similar to object oriented behaviours. 111 + Notice that the contained member must not be a pointer, but an actual member 112 + for this to work. 113 + 114 + We can see here that we avoid having global pointers to our struct foo * 115 + instance this way, while still keeping the number of parameters passed to the 116 + work function to a single pointer.
+4 -1
Documentation/kobject.txt
··· 342 342 343 343 When you are finished with the kset, call: 344 344 void kset_unregister(struct kset *kset); 345 - to destroy it. 345 + to destroy it. This removes the kset from sysfs and decrements its reference 346 + count. When the reference count goes to zero, the kset will be released. 347 + Because other references to the kset may still exist, the release may happen 348 + after kset_unregister() returns. 346 349 347 350 An example of using a kset can be seen in the 348 351 samples/kobject/kset-example.c file in the kernel tree.
+1 -1
arch/x86/kernel/cpu/microcode/amd.c
··· 433 433 if (c->x86 >= 0x15) 434 434 snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86); 435 435 436 - if (request_firmware(&fw, (const char *)fw_name, device)) { 436 + if (request_firmware_direct(&fw, (const char *)fw_name, device)) { 437 437 pr_debug("failed to load file %s\n", fw_name); 438 438 goto out; 439 439 }
+1 -1
arch/x86/kernel/cpu/microcode/intel.c
··· 278 278 sprintf(name, "intel-ucode/%02x-%02x-%02x", 279 279 c->x86, c->x86_model, c->x86_mask); 280 280 281 - if (request_firmware(&firmware, name, device)) { 281 + if (request_firmware_direct(&firmware, name, device)) { 282 282 pr_debug("data file %s load failed\n", name); 283 283 return UCODE_NFOUND; 284 284 }
+1 -1
drivers/base/Makefile
··· 1 1 # Makefile for the Linux device tree 2 2 3 - obj-y := core.o bus.o dd.o syscore.o \ 3 + obj-y := component.o core.o bus.o dd.o syscore.o \ 4 4 driver.o class.o platform.o \ 5 5 cpu.o firmware.o init.o map.o devres.o \ 6 6 attribute_container.o transport_class.o \
+11 -2
drivers/base/bus.c
··· 146 146 } 147 147 EXPORT_SYMBOL_GPL(bus_remove_file); 148 148 149 + static void bus_release(struct kobject *kobj) 150 + { 151 + struct subsys_private *priv = 152 + container_of(kobj, typeof(*priv), subsys.kobj); 153 + struct bus_type *bus = priv->bus; 154 + 155 + kfree(priv); 156 + bus->p = NULL; 157 + } 158 + 149 159 static struct kobj_type bus_ktype = { 150 160 .sysfs_ops = &bus_sysfs_ops, 161 + .release = bus_release, 151 162 }; 152 163 153 164 static int bus_uevent_filter(struct kset *kset, struct kobject *kobj) ··· 964 953 kset_unregister(bus->p->devices_kset); 965 954 bus_remove_file(bus, &bus_attr_uevent); 966 955 kset_unregister(&bus->p->subsys); 967 - kfree(bus->p); 968 - bus->p = NULL; 969 956 } 970 957 EXPORT_SYMBOL_GPL(bus_unregister); 971 958
+382
drivers/base/component.c
··· 1 + /* 2 + * Componentized device handling. 3 + * 4 + * This program is free software; you can redistribute it and/or modify 5 + * it under the terms of the GNU General Public License version 2 as 6 + * published by the Free Software Foundation. 7 + * 8 + * This is work in progress. We gather up the component devices into a list, 9 + * and bind them when instructed. At the moment, we're specific to the DRM 10 + * subsystem, and only handles one master device, but this doesn't have to be 11 + * the case. 12 + */ 13 + #include <linux/component.h> 14 + #include <linux/device.h> 15 + #include <linux/kref.h> 16 + #include <linux/list.h> 17 + #include <linux/module.h> 18 + #include <linux/mutex.h> 19 + #include <linux/slab.h> 20 + 21 + struct master { 22 + struct list_head node; 23 + struct list_head components; 24 + bool bound; 25 + 26 + const struct component_master_ops *ops; 27 + struct device *dev; 28 + }; 29 + 30 + struct component { 31 + struct list_head node; 32 + struct list_head master_node; 33 + struct master *master; 34 + bool bound; 35 + 36 + const struct component_ops *ops; 37 + struct device *dev; 38 + }; 39 + 40 + static DEFINE_MUTEX(component_mutex); 41 + static LIST_HEAD(component_list); 42 + static LIST_HEAD(masters); 43 + 44 + static struct master *__master_find(struct device *dev, 45 + const struct component_master_ops *ops) 46 + { 47 + struct master *m; 48 + 49 + list_for_each_entry(m, &masters, node) 50 + if (m->dev == dev && (!ops || m->ops == ops)) 51 + return m; 52 + 53 + return NULL; 54 + } 55 + 56 + /* Attach an unattached component to a master. */ 57 + static void component_attach_master(struct master *master, struct component *c) 58 + { 59 + c->master = master; 60 + 61 + list_add_tail(&c->master_node, &master->components); 62 + } 63 + 64 + /* Detach a component from a master. */ 65 + static void component_detach_master(struct master *master, struct component *c) 66 + { 67 + list_del(&c->master_node); 68 + 69 + c->master = NULL; 70 + } 71 + 72 + int component_master_add_child(struct master *master, 73 + int (*compare)(struct device *, void *), void *compare_data) 74 + { 75 + struct component *c; 76 + int ret = -ENXIO; 77 + 78 + list_for_each_entry(c, &component_list, node) { 79 + if (c->master) 80 + continue; 81 + 82 + if (compare(c->dev, compare_data)) { 83 + component_attach_master(master, c); 84 + ret = 0; 85 + break; 86 + } 87 + } 88 + 89 + return ret; 90 + } 91 + EXPORT_SYMBOL_GPL(component_master_add_child); 92 + 93 + /* Detach all attached components from this master */ 94 + static void master_remove_components(struct master *master) 95 + { 96 + while (!list_empty(&master->components)) { 97 + struct component *c = list_first_entry(&master->components, 98 + struct component, master_node); 99 + 100 + WARN_ON(c->master != master); 101 + 102 + component_detach_master(master, c); 103 + } 104 + } 105 + 106 + /* 107 + * Try to bring up a master. If component is NULL, we're interested in 108 + * this master, otherwise it's a component which must be present to try 109 + * and bring up the master. 110 + * 111 + * Returns 1 for successful bringup, 0 if not ready, or -ve errno. 112 + */ 113 + static int try_to_bring_up_master(struct master *master, 114 + struct component *component) 115 + { 116 + int ret = 0; 117 + 118 + if (!master->bound) { 119 + /* 120 + * Search the list of components, looking for components that 121 + * belong to this master, and attach them to the master. 122 + */ 123 + if (master->ops->add_components(master->dev, master)) { 124 + /* Failed to find all components */ 125 + master_remove_components(master); 126 + ret = 0; 127 + goto out; 128 + } 129 + 130 + if (component && component->master != master) { 131 + master_remove_components(master); 132 + ret = 0; 133 + goto out; 134 + } 135 + 136 + /* Found all components */ 137 + ret = master->ops->bind(master->dev); 138 + if (ret < 0) { 139 + master_remove_components(master); 140 + goto out; 141 + } 142 + 143 + master->bound = true; 144 + ret = 1; 145 + } 146 + out: 147 + 148 + return ret; 149 + } 150 + 151 + static int try_to_bring_up_masters(struct component *component) 152 + { 153 + struct master *m; 154 + int ret = 0; 155 + 156 + list_for_each_entry(m, &masters, node) { 157 + ret = try_to_bring_up_master(m, component); 158 + if (ret != 0) 159 + break; 160 + } 161 + 162 + return ret; 163 + } 164 + 165 + static void take_down_master(struct master *master) 166 + { 167 + if (master->bound) { 168 + master->ops->unbind(master->dev); 169 + master->bound = false; 170 + } 171 + 172 + master_remove_components(master); 173 + } 174 + 175 + int component_master_add(struct device *dev, 176 + const struct component_master_ops *ops) 177 + { 178 + struct master *master; 179 + int ret; 180 + 181 + master = kzalloc(sizeof(*master), GFP_KERNEL); 182 + if (!master) 183 + return -ENOMEM; 184 + 185 + master->dev = dev; 186 + master->ops = ops; 187 + INIT_LIST_HEAD(&master->components); 188 + 189 + /* Add to the list of available masters. */ 190 + mutex_lock(&component_mutex); 191 + list_add(&master->node, &masters); 192 + 193 + ret = try_to_bring_up_master(master, NULL); 194 + 195 + if (ret < 0) { 196 + /* Delete off the list if we weren't successful */ 197 + list_del(&master->node); 198 + kfree(master); 199 + } 200 + mutex_unlock(&component_mutex); 201 + 202 + return ret < 0 ? ret : 0; 203 + } 204 + EXPORT_SYMBOL_GPL(component_master_add); 205 + 206 + void component_master_del(struct device *dev, 207 + const struct component_master_ops *ops) 208 + { 209 + struct master *master; 210 + 211 + mutex_lock(&component_mutex); 212 + master = __master_find(dev, ops); 213 + if (master) { 214 + take_down_master(master); 215 + 216 + list_del(&master->node); 217 + kfree(master); 218 + } 219 + mutex_unlock(&component_mutex); 220 + } 221 + EXPORT_SYMBOL_GPL(component_master_del); 222 + 223 + static void component_unbind(struct component *component, 224 + struct master *master, void *data) 225 + { 226 + WARN_ON(!component->bound); 227 + 228 + component->ops->unbind(component->dev, master->dev, data); 229 + component->bound = false; 230 + 231 + /* Release all resources claimed in the binding of this component */ 232 + devres_release_group(component->dev, component); 233 + } 234 + 235 + void component_unbind_all(struct device *master_dev, void *data) 236 + { 237 + struct master *master; 238 + struct component *c; 239 + 240 + WARN_ON(!mutex_is_locked(&component_mutex)); 241 + 242 + master = __master_find(master_dev, NULL); 243 + if (!master) 244 + return; 245 + 246 + list_for_each_entry_reverse(c, &master->components, master_node) 247 + component_unbind(c, master, data); 248 + } 249 + EXPORT_SYMBOL_GPL(component_unbind_all); 250 + 251 + static int component_bind(struct component *component, struct master *master, 252 + void *data) 253 + { 254 + int ret; 255 + 256 + /* 257 + * Each component initialises inside its own devres group. 258 + * This allows us to roll-back a failed component without 259 + * affecting anything else. 260 + */ 261 + if (!devres_open_group(master->dev, NULL, GFP_KERNEL)) 262 + return -ENOMEM; 263 + 264 + /* 265 + * Also open a group for the device itself: this allows us 266 + * to release the resources claimed against the sub-device 267 + * at the appropriate moment. 268 + */ 269 + if (!devres_open_group(component->dev, component, GFP_KERNEL)) { 270 + devres_release_group(master->dev, NULL); 271 + return -ENOMEM; 272 + } 273 + 274 + dev_dbg(master->dev, "binding %s (ops %ps)\n", 275 + dev_name(component->dev), component->ops); 276 + 277 + ret = component->ops->bind(component->dev, master->dev, data); 278 + if (!ret) { 279 + component->bound = true; 280 + 281 + /* 282 + * Close the component device's group so that resources 283 + * allocated in the binding are encapsulated for removal 284 + * at unbind. Remove the group on the DRM device as we 285 + * can clean those resources up independently. 286 + */ 287 + devres_close_group(component->dev, NULL); 288 + devres_remove_group(master->dev, NULL); 289 + 290 + dev_info(master->dev, "bound %s (ops %ps)\n", 291 + dev_name(component->dev), component->ops); 292 + } else { 293 + devres_release_group(component->dev, NULL); 294 + devres_release_group(master->dev, NULL); 295 + 296 + dev_err(master->dev, "failed to bind %s (ops %ps): %d\n", 297 + dev_name(component->dev), component->ops, ret); 298 + } 299 + 300 + return ret; 301 + } 302 + 303 + int component_bind_all(struct device *master_dev, void *data) 304 + { 305 + struct master *master; 306 + struct component *c; 307 + int ret = 0; 308 + 309 + WARN_ON(!mutex_is_locked(&component_mutex)); 310 + 311 + master = __master_find(master_dev, NULL); 312 + if (!master) 313 + return -EINVAL; 314 + 315 + list_for_each_entry(c, &master->components, master_node) { 316 + ret = component_bind(c, master, data); 317 + if (ret) 318 + break; 319 + } 320 + 321 + if (ret != 0) { 322 + list_for_each_entry_continue_reverse(c, &master->components, 323 + master_node) 324 + component_unbind(c, master, data); 325 + } 326 + 327 + return ret; 328 + } 329 + EXPORT_SYMBOL_GPL(component_bind_all); 330 + 331 + int component_add(struct device *dev, const struct component_ops *ops) 332 + { 333 + struct component *component; 334 + int ret; 335 + 336 + component = kzalloc(sizeof(*component), GFP_KERNEL); 337 + if (!component) 338 + return -ENOMEM; 339 + 340 + component->ops = ops; 341 + component->dev = dev; 342 + 343 + dev_dbg(dev, "adding component (ops %ps)\n", ops); 344 + 345 + mutex_lock(&component_mutex); 346 + list_add_tail(&component->node, &component_list); 347 + 348 + ret = try_to_bring_up_masters(component); 349 + if (ret < 0) { 350 + list_del(&component->node); 351 + 352 + kfree(component); 353 + } 354 + mutex_unlock(&component_mutex); 355 + 356 + return ret < 0 ? ret : 0; 357 + } 358 + EXPORT_SYMBOL_GPL(component_add); 359 + 360 + void component_del(struct device *dev, const struct component_ops *ops) 361 + { 362 + struct component *c, *component = NULL; 363 + 364 + mutex_lock(&component_mutex); 365 + list_for_each_entry(c, &component_list, node) 366 + if (c->dev == dev && c->ops == ops) { 367 + list_del(&c->node); 368 + component = c; 369 + break; 370 + } 371 + 372 + if (component && component->master) 373 + take_down_master(component->master); 374 + 375 + mutex_unlock(&component_mutex); 376 + 377 + WARN_ON(!component); 378 + kfree(component); 379 + } 380 + EXPORT_SYMBOL_GPL(component_del); 381 + 382 + MODULE_LICENSE("GPL v2");
+5 -2
drivers/base/core.c
··· 491 491 if (device_supports_offline(dev) && !dev->offline_disabled) { 492 492 error = device_create_file(dev, &dev_attr_online); 493 493 if (error) 494 - goto err_remove_type_groups; 494 + goto err_remove_dev_groups; 495 495 } 496 496 497 497 return 0; 498 498 499 + err_remove_dev_groups: 500 + device_remove_groups(dev, dev->groups); 499 501 err_remove_type_groups: 500 502 if (type) 501 503 device_remove_groups(dev, type->groups); ··· 1605 1603 goto error; 1606 1604 } 1607 1605 1606 + device_initialize(dev); 1608 1607 dev->devt = devt; 1609 1608 dev->class = class; 1610 1609 dev->parent = parent; ··· 1617 1614 if (retval) 1618 1615 goto error; 1619 1616 1620 - retval = device_register(dev); 1617 + retval = device_add(dev); 1621 1618 if (retval) 1622 1619 goto error; 1623 1620
+1 -1
drivers/base/devtmpfs.c
··· 299 299 { 300 300 struct path parent; 301 301 struct dentry *dentry; 302 - int deleted = 1; 302 + int deleted = 0; 303 303 int err; 304 304 305 305 dentry = kern_path_locked(nodename, &parent);
+66 -27
drivers/base/firmware_class.c
··· 96 96 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT; 97 97 } 98 98 99 + /* firmware behavior options */ 100 + #define FW_OPT_UEVENT (1U << 0) 101 + #define FW_OPT_NOWAIT (1U << 1) 102 + #ifdef CONFIG_FW_LOADER_USER_HELPER 103 + #define FW_OPT_FALLBACK (1U << 2) 104 + #else 105 + #define FW_OPT_FALLBACK 0 106 + #endif 107 + 99 108 struct firmware_cache { 100 109 /* firmware_buf instance will be added into the below list */ 101 110 spinlock_t lock; ··· 228 219 } 229 220 230 221 static void __fw_free_buf(struct kref *ref) 222 + __releases(&fwc->lock) 231 223 { 232 224 struct firmware_buf *buf = to_fwbuf(ref); 233 225 struct firmware_cache *fwc = buf->fwc; ··· 280 270 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path"); 281 271 282 272 /* Don't inline this: 'struct kstat' is biggish */ 283 - static noinline_for_stack long fw_file_size(struct file *file) 273 + static noinline_for_stack int fw_file_size(struct file *file) 284 274 { 285 275 struct kstat st; 286 276 if (vfs_getattr(&file->f_path, &st)) 287 277 return -1; 288 278 if (!S_ISREG(st.mode)) 289 279 return -1; 290 - if (st.size != (long)st.size) 280 + if (st.size != (int)st.size) 291 281 return -1; 292 282 return st.size; 293 283 } 294 284 295 285 static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf) 296 286 { 297 - long size; 287 + int size; 298 288 char *buf; 299 289 int rc; 300 290 ··· 830 820 831 821 static struct firmware_priv * 832 822 fw_create_instance(struct firmware *firmware, const char *fw_name, 833 - struct device *device, bool uevent, bool nowait) 823 + struct device *device, unsigned int opt_flags) 834 824 { 835 825 struct firmware_priv *fw_priv; 836 826 struct device *f_dev; ··· 842 832 goto exit; 843 833 } 844 834 845 - fw_priv->nowait = nowait; 835 + fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT); 846 836 fw_priv->fw = firmware; 847 837 INIT_DELAYED_WORK(&fw_priv->timeout_work, 848 838 firmware_class_timeout_work); ··· 858 848 } 859 849 860 850 /* load a firmware via user helper */ 861 - static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent, 862 - long timeout) 851 + static int _request_firmware_load(struct firmware_priv *fw_priv, 852 + unsigned int opt_flags, long timeout) 863 853 { 864 854 int retval = 0; 865 855 struct device *f_dev = &fw_priv->dev; ··· 895 885 goto err_del_bin_attr; 896 886 } 897 887 898 - if (uevent) { 888 + if (opt_flags & FW_OPT_UEVENT) { 899 889 buf->need_uevent = true; 900 890 dev_set_uevent_suppress(f_dev, false); 901 891 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id); ··· 921 911 922 912 static int fw_load_from_user_helper(struct firmware *firmware, 923 913 const char *name, struct device *device, 924 - bool uevent, bool nowait, long timeout) 914 + unsigned int opt_flags, long timeout) 925 915 { 926 916 struct firmware_priv *fw_priv; 927 917 928 - fw_priv = fw_create_instance(firmware, name, device, uevent, nowait); 918 + fw_priv = fw_create_instance(firmware, name, device, opt_flags); 929 919 if (IS_ERR(fw_priv)) 930 920 return PTR_ERR(fw_priv); 931 921 932 922 fw_priv->buf = firmware->priv; 933 - return _request_firmware_load(fw_priv, uevent, timeout); 923 + return _request_firmware_load(fw_priv, opt_flags, timeout); 934 924 } 935 925 936 926 #ifdef CONFIG_PM_SLEEP ··· 952 942 #else /* CONFIG_FW_LOADER_USER_HELPER */ 953 943 static inline int 954 944 fw_load_from_user_helper(struct firmware *firmware, const char *name, 955 - struct device *device, bool uevent, bool nowait, 945 + struct device *device, unsigned int opt_flags, 956 946 long timeout) 957 947 { 958 948 return -ENOENT; ··· 1033 1023 } 1034 1024 1035 1025 static int assign_firmware_buf(struct firmware *fw, struct device *device, 1036 - bool skip_cache) 1026 + unsigned int opt_flags) 1037 1027 { 1038 1028 struct firmware_buf *buf = fw->priv; 1039 1029 ··· 1050 1040 * device may has been deleted already, but the problem 1051 1041 * should be fixed in devres or driver core. 1052 1042 */ 1053 - if (device && !skip_cache) 1043 + /* don't cache firmware handled without uevent */ 1044 + if (device && (opt_flags & FW_OPT_UEVENT)) 1054 1045 fw_add_devm_name(device, buf->fw_id); 1055 1046 1056 1047 /* ··· 1072 1061 /* called from request_firmware() and request_firmware_work_func() */ 1073 1062 static int 1074 1063 _request_firmware(const struct firmware **firmware_p, const char *name, 1075 - struct device *device, bool uevent, bool nowait) 1064 + struct device *device, unsigned int opt_flags) 1076 1065 { 1077 1066 struct firmware *fw; 1078 1067 long timeout; ··· 1087 1076 1088 1077 ret = 0; 1089 1078 timeout = firmware_loading_timeout(); 1090 - if (nowait) { 1079 + if (opt_flags & FW_OPT_NOWAIT) { 1091 1080 timeout = usermodehelper_read_lock_wait(timeout); 1092 1081 if (!timeout) { 1093 1082 dev_dbg(device, "firmware: %s loading timed out\n", ··· 1106 1095 1107 1096 ret = fw_get_filesystem_firmware(device, fw->priv); 1108 1097 if (ret) { 1109 - dev_warn(device, "Direct firmware load failed with error %d\n", 1110 - ret); 1111 - dev_warn(device, "Falling back to user helper\n"); 1112 - ret = fw_load_from_user_helper(fw, name, device, 1113 - uevent, nowait, timeout); 1098 + if (opt_flags & FW_OPT_FALLBACK) { 1099 + dev_warn(device, 1100 + "Direct firmware load failed with error %d\n", 1101 + ret); 1102 + dev_warn(device, "Falling back to user helper\n"); 1103 + ret = fw_load_from_user_helper(fw, name, device, 1104 + opt_flags, timeout); 1105 + } 1114 1106 } 1115 1107 1116 - /* don't cache firmware handled without uevent */ 1117 1108 if (!ret) 1118 - ret = assign_firmware_buf(fw, device, !uevent); 1109 + ret = assign_firmware_buf(fw, device, opt_flags); 1119 1110 1120 1111 usermodehelper_read_unlock(); 1121 1112 ··· 1159 1146 1160 1147 /* Need to pin this module until return */ 1161 1148 __module_get(THIS_MODULE); 1162 - ret = _request_firmware(firmware_p, name, device, true, false); 1149 + ret = _request_firmware(firmware_p, name, device, 1150 + FW_OPT_UEVENT | FW_OPT_FALLBACK); 1163 1151 module_put(THIS_MODULE); 1164 1152 return ret; 1165 1153 } 1166 1154 EXPORT_SYMBOL(request_firmware); 1155 + 1156 + #ifdef CONFIG_FW_LOADER_USER_HELPER 1157 + /** 1158 + * request_firmware: - load firmware directly without usermode helper 1159 + * @firmware_p: pointer to firmware image 1160 + * @name: name of firmware file 1161 + * @device: device for which firmware is being loaded 1162 + * 1163 + * This function works pretty much like request_firmware(), but this doesn't 1164 + * fall back to usermode helper even if the firmware couldn't be loaded 1165 + * directly from fs. Hence it's useful for loading optional firmwares, which 1166 + * aren't always present, without extra long timeouts of udev. 1167 + **/ 1168 + int request_firmware_direct(const struct firmware **firmware_p, 1169 + const char *name, struct device *device) 1170 + { 1171 + int ret; 1172 + __module_get(THIS_MODULE); 1173 + ret = _request_firmware(firmware_p, name, device, FW_OPT_UEVENT); 1174 + module_put(THIS_MODULE); 1175 + return ret; 1176 + } 1177 + EXPORT_SYMBOL_GPL(request_firmware_direct); 1178 + #endif 1167 1179 1168 1180 /** 1169 1181 * release_firmware: - release the resource associated with a firmware image ··· 1212 1174 struct device *device; 1213 1175 void *context; 1214 1176 void (*cont)(const struct firmware *fw, void *context); 1215 - bool uevent; 1177 + unsigned int opt_flags; 1216 1178 }; 1217 1179 1218 1180 static void request_firmware_work_func(struct work_struct *work) ··· 1223 1185 fw_work = container_of(work, struct firmware_work, work); 1224 1186 1225 1187 _request_firmware(&fw, fw_work->name, fw_work->device, 1226 - fw_work->uevent, true); 1188 + fw_work->opt_flags); 1227 1189 fw_work->cont(fw, fw_work->context); 1228 1190 put_device(fw_work->device); /* taken in request_firmware_nowait() */ 1229 1191 ··· 1271 1233 fw_work->device = device; 1272 1234 fw_work->context = context; 1273 1235 fw_work->cont = cont; 1274 - fw_work->uevent = uevent; 1236 + fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK | 1237 + (uevent ? FW_OPT_UEVENT : 0); 1275 1238 1276 1239 if (!try_module_get(module)) { 1277 1240 kfree(fw_work);
+2 -1
drivers/firmware/dmi-sysfs.c
··· 553 553 static void dmi_sysfs_entry_release(struct kobject *kobj) 554 554 { 555 555 struct dmi_sysfs_entry *entry = to_entry(kobj); 556 - sysfs_remove_bin_file(&entry->kobj, &dmi_entry_raw_attr); 556 + 557 557 spin_lock(&entry_list_lock); 558 558 list_del(&entry->list); 559 559 spin_unlock(&entry_list_lock); ··· 685 685 pr_debug("dmi-sysfs: unloading.\n"); 686 686 cleanup_entry_list(); 687 687 kset_unregister(dmi_kset); 688 + kobject_del(dmi_kobj); 688 689 kobject_put(dmi_kobj); 689 690 } 690 691
+2 -2
drivers/gpio/gpiolib.c
··· 393 393 394 394 static irqreturn_t gpio_sysfs_irq(int irq, void *priv) 395 395 { 396 - struct sysfs_dirent *value_sd = priv; 396 + struct kernfs_node *value_sd = priv; 397 397 398 398 sysfs_notify_dirent(value_sd); 399 399 return IRQ_HANDLED; ··· 402 402 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, 403 403 unsigned long gpio_flags) 404 404 { 405 - struct sysfs_dirent *value_sd; 405 + struct kernfs_node *value_sd; 406 406 unsigned long irq_flags; 407 407 int ret, irq, id; 408 408
+1 -1
drivers/md/bitmap.c
··· 1635 1635 sector_t blocks = mddev->resync_max_sectors; 1636 1636 struct file *file = mddev->bitmap_info.file; 1637 1637 int err; 1638 - struct sysfs_dirent *bm = NULL; 1638 + struct kernfs_node *bm = NULL; 1639 1639 1640 1640 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256); 1641 1641
+1 -1
drivers/md/bitmap.h
··· 225 225 wait_queue_head_t overflow_wait; 226 226 wait_queue_head_t behind_wait; 227 227 228 - struct sysfs_dirent *sysfs_can_clear; 228 + struct kernfs_node *sysfs_can_clear; 229 229 }; 230 230 231 231 /* the bitmap API */
+5 -5
drivers/md/md.h
··· 106 106 */ 107 107 struct work_struct del_work; /* used for delayed sysfs removal */ 108 108 109 - struct sysfs_dirent *sysfs_state; /* handle for 'state' 109 + struct kernfs_node *sysfs_state; /* handle for 'state' 110 110 * sysfs entry */ 111 111 112 112 struct badblocks { ··· 379 379 sector_t resync_max; /* resync should pause 380 380 * when it gets here */ 381 381 382 - struct sysfs_dirent *sysfs_state; /* handle for 'array_state' 382 + struct kernfs_node *sysfs_state; /* handle for 'array_state' 383 383 * file in sysfs. 384 384 */ 385 - struct sysfs_dirent *sysfs_action; /* handle for 'sync_action' */ 385 + struct kernfs_node *sysfs_action; /* handle for 'sync_action' */ 386 386 387 387 struct work_struct del_work; /* used for delayed sysfs removal */ 388 388 ··· 501 501 }; 502 502 extern struct attribute_group md_bitmap_group; 503 503 504 - static inline struct sysfs_dirent *sysfs_get_dirent_safe(struct sysfs_dirent *sd, char *name) 504 + static inline struct kernfs_node *sysfs_get_dirent_safe(struct kernfs_node *sd, char *name) 505 505 { 506 506 if (sd) 507 507 return sysfs_get_dirent(sd, name); 508 508 return sd; 509 509 } 510 - static inline void sysfs_notify_dirent_safe(struct sysfs_dirent *sd) 510 + static inline void sysfs_notify_dirent_safe(struct kernfs_node *sd) 511 511 { 512 512 if (sd) 513 513 sysfs_notify_dirent(sd);
+1 -1
drivers/misc/mic/host/mic_device.h
··· 112 112 struct work_struct shutdown_work; 113 113 u8 state; 114 114 u8 shutdown_status; 115 - struct sysfs_dirent *state_sysfs; 115 + struct kernfs_node *state_sysfs; 116 116 struct completion reset_wait; 117 117 void *log_buf_addr; 118 118 int *log_buf_len;
+1 -1
fs/Makefile
··· 53 53 obj-y += quota/ 54 54 55 55 obj-$(CONFIG_PROC_FS) += proc/ 56 - obj-$(CONFIG_SYSFS) += sysfs/ 56 + obj-$(CONFIG_SYSFS) += sysfs/ kernfs/ 57 57 obj-$(CONFIG_CONFIGFS_FS) += configfs/ 58 58 obj-y += devpts/ 59 59
+5
fs/kernfs/Makefile
··· 1 + # 2 + # Makefile for the kernfs pseudo filesystem 3 + # 4 + 5 + obj-y := mount.o inode.o dir.o file.o symlink.o
+1073
fs/kernfs/dir.c
··· 1 + /* 2 + * fs/kernfs/dir.c - kernfs directory implementation 3 + * 4 + * Copyright (c) 2001-3 Patrick Mochel 5 + * Copyright (c) 2007 SUSE Linux Products GmbH 6 + * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org> 7 + * 8 + * This file is released under the GPLv2. 9 + */ 10 + 11 + #include <linux/fs.h> 12 + #include <linux/namei.h> 13 + #include <linux/idr.h> 14 + #include <linux/slab.h> 15 + #include <linux/security.h> 16 + #include <linux/hash.h> 17 + 18 + #include "kernfs-internal.h" 19 + 20 + DEFINE_MUTEX(kernfs_mutex); 21 + 22 + #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb) 23 + 24 + /** 25 + * kernfs_name_hash 26 + * @name: Null terminated string to hash 27 + * @ns: Namespace tag to hash 28 + * 29 + * Returns 31 bit hash of ns + name (so it fits in an off_t ) 30 + */ 31 + static unsigned int kernfs_name_hash(const char *name, const void *ns) 32 + { 33 + unsigned long hash = init_name_hash(); 34 + unsigned int len = strlen(name); 35 + while (len--) 36 + hash = partial_name_hash(*name++, hash); 37 + hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31)); 38 + hash &= 0x7fffffffU; 39 + /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */ 40 + if (hash < 1) 41 + hash += 2; 42 + if (hash >= INT_MAX) 43 + hash = INT_MAX - 1; 44 + return hash; 45 + } 46 + 47 + static int kernfs_name_compare(unsigned int hash, const char *name, 48 + const void *ns, const struct kernfs_node *kn) 49 + { 50 + if (hash != kn->hash) 51 + return hash - kn->hash; 52 + if (ns != kn->ns) 53 + return ns - kn->ns; 54 + return strcmp(name, kn->name); 55 + } 56 + 57 + static int kernfs_sd_compare(const struct kernfs_node *left, 58 + const struct kernfs_node *right) 59 + { 60 + return kernfs_name_compare(left->hash, left->name, left->ns, right); 61 + } 62 + 63 + /** 64 + * kernfs_link_sibling - link kernfs_node into sibling rbtree 65 + * @kn: kernfs_node of interest 66 + * 67 + * Link @kn into its sibling rbtree which starts from 68 + * @kn->parent->dir.children. 69 + * 70 + * Locking: 71 + * mutex_lock(kernfs_mutex) 72 + * 73 + * RETURNS: 74 + * 0 on susccess -EEXIST on failure. 75 + */ 76 + static int kernfs_link_sibling(struct kernfs_node *kn) 77 + { 78 + struct rb_node **node = &kn->parent->dir.children.rb_node; 79 + struct rb_node *parent = NULL; 80 + 81 + if (kernfs_type(kn) == KERNFS_DIR) 82 + kn->parent->dir.subdirs++; 83 + 84 + while (*node) { 85 + struct kernfs_node *pos; 86 + int result; 87 + 88 + pos = rb_to_kn(*node); 89 + parent = *node; 90 + result = kernfs_sd_compare(kn, pos); 91 + if (result < 0) 92 + node = &pos->rb.rb_left; 93 + else if (result > 0) 94 + node = &pos->rb.rb_right; 95 + else 96 + return -EEXIST; 97 + } 98 + /* add new node and rebalance the tree */ 99 + rb_link_node(&kn->rb, parent, node); 100 + rb_insert_color(&kn->rb, &kn->parent->dir.children); 101 + return 0; 102 + } 103 + 104 + /** 105 + * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree 106 + * @kn: kernfs_node of interest 107 + * 108 + * Unlink @kn from its sibling rbtree which starts from 109 + * kn->parent->dir.children. 110 + * 111 + * Locking: 112 + * mutex_lock(kernfs_mutex) 113 + */ 114 + static void kernfs_unlink_sibling(struct kernfs_node *kn) 115 + { 116 + if (kernfs_type(kn) == KERNFS_DIR) 117 + kn->parent->dir.subdirs--; 118 + 119 + rb_erase(&kn->rb, &kn->parent->dir.children); 120 + } 121 + 122 + /** 123 + * kernfs_get_active - get an active reference to kernfs_node 124 + * @kn: kernfs_node to get an active reference to 125 + * 126 + * Get an active reference of @kn. This function is noop if @kn 127 + * is NULL. 128 + * 129 + * RETURNS: 130 + * Pointer to @kn on success, NULL on failure. 131 + */ 132 + struct kernfs_node *kernfs_get_active(struct kernfs_node *kn) 133 + { 134 + if (unlikely(!kn)) 135 + return NULL; 136 + 137 + if (!atomic_inc_unless_negative(&kn->active)) 138 + return NULL; 139 + 140 + if (kn->flags & KERNFS_LOCKDEP) 141 + rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_); 142 + return kn; 143 + } 144 + 145 + /** 146 + * kernfs_put_active - put an active reference to kernfs_node 147 + * @kn: kernfs_node to put an active reference to 148 + * 149 + * Put an active reference to @kn. This function is noop if @kn 150 + * is NULL. 151 + */ 152 + void kernfs_put_active(struct kernfs_node *kn) 153 + { 154 + int v; 155 + 156 + if (unlikely(!kn)) 157 + return; 158 + 159 + if (kn->flags & KERNFS_LOCKDEP) 160 + rwsem_release(&kn->dep_map, 1, _RET_IP_); 161 + v = atomic_dec_return(&kn->active); 162 + if (likely(v != KN_DEACTIVATED_BIAS)) 163 + return; 164 + 165 + /* 166 + * atomic_dec_return() is a mb(), we'll always see the updated 167 + * kn->u.completion. 168 + */ 169 + complete(kn->u.completion); 170 + } 171 + 172 + /** 173 + * kernfs_deactivate - deactivate kernfs_node 174 + * @kn: kernfs_node to deactivate 175 + * 176 + * Deny new active references and drain existing ones. 177 + */ 178 + static void kernfs_deactivate(struct kernfs_node *kn) 179 + { 180 + DECLARE_COMPLETION_ONSTACK(wait); 181 + int v; 182 + 183 + BUG_ON(!(kn->flags & KERNFS_REMOVED)); 184 + 185 + if (!(kernfs_type(kn) & KERNFS_ACTIVE_REF)) 186 + return; 187 + 188 + kn->u.completion = (void *)&wait; 189 + 190 + rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_); 191 + /* atomic_add_return() is a mb(), put_active() will always see 192 + * the updated kn->u.completion. 193 + */ 194 + v = atomic_add_return(KN_DEACTIVATED_BIAS, &kn->active); 195 + 196 + if (v != KN_DEACTIVATED_BIAS) { 197 + lock_contended(&kn->dep_map, _RET_IP_); 198 + wait_for_completion(&wait); 199 + } 200 + 201 + lock_acquired(&kn->dep_map, _RET_IP_); 202 + rwsem_release(&kn->dep_map, 1, _RET_IP_); 203 + } 204 + 205 + /** 206 + * kernfs_get - get a reference count on a kernfs_node 207 + * @kn: the target kernfs_node 208 + */ 209 + void kernfs_get(struct kernfs_node *kn) 210 + { 211 + if (kn) { 212 + WARN_ON(!atomic_read(&kn->count)); 213 + atomic_inc(&kn->count); 214 + } 215 + } 216 + EXPORT_SYMBOL_GPL(kernfs_get); 217 + 218 + /** 219 + * kernfs_put - put a reference count on a kernfs_node 220 + * @kn: the target kernfs_node 221 + * 222 + * Put a reference count of @kn and destroy it if it reached zero. 223 + */ 224 + void kernfs_put(struct kernfs_node *kn) 225 + { 226 + struct kernfs_node *parent; 227 + struct kernfs_root *root; 228 + 229 + if (!kn || !atomic_dec_and_test(&kn->count)) 230 + return; 231 + root = kernfs_root(kn); 232 + repeat: 233 + /* Moving/renaming is always done while holding reference. 234 + * kn->parent won't change beneath us. 235 + */ 236 + parent = kn->parent; 237 + 238 + WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n", 239 + parent ? parent->name : "", kn->name); 240 + 241 + if (kernfs_type(kn) == KERNFS_LINK) 242 + kernfs_put(kn->symlink.target_kn); 243 + if (!(kn->flags & KERNFS_STATIC_NAME)) 244 + kfree(kn->name); 245 + if (kn->iattr) { 246 + if (kn->iattr->ia_secdata) 247 + security_release_secctx(kn->iattr->ia_secdata, 248 + kn->iattr->ia_secdata_len); 249 + simple_xattrs_free(&kn->iattr->xattrs); 250 + } 251 + kfree(kn->iattr); 252 + ida_simple_remove(&root->ino_ida, kn->ino); 253 + kmem_cache_free(kernfs_node_cache, kn); 254 + 255 + kn = parent; 256 + if (kn) { 257 + if (atomic_dec_and_test(&kn->count)) 258 + goto repeat; 259 + } else { 260 + /* just released the root kn, free @root too */ 261 + ida_destroy(&root->ino_ida); 262 + kfree(root); 263 + } 264 + } 265 + EXPORT_SYMBOL_GPL(kernfs_put); 266 + 267 + static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) 268 + { 269 + struct kernfs_node *kn; 270 + 271 + if (flags & LOOKUP_RCU) 272 + return -ECHILD; 273 + 274 + /* Always perform fresh lookup for negatives */ 275 + if (!dentry->d_inode) 276 + goto out_bad_unlocked; 277 + 278 + kn = dentry->d_fsdata; 279 + mutex_lock(&kernfs_mutex); 280 + 281 + /* The kernfs node has been deleted */ 282 + if (kn->flags & KERNFS_REMOVED) 283 + goto out_bad; 284 + 285 + /* The kernfs node has been moved? */ 286 + if (dentry->d_parent->d_fsdata != kn->parent) 287 + goto out_bad; 288 + 289 + /* The kernfs node has been renamed */ 290 + if (strcmp(dentry->d_name.name, kn->name) != 0) 291 + goto out_bad; 292 + 293 + /* The kernfs node has been moved to a different namespace */ 294 + if (kn->parent && kernfs_ns_enabled(kn->parent) && 295 + kernfs_info(dentry->d_sb)->ns != kn->ns) 296 + goto out_bad; 297 + 298 + mutex_unlock(&kernfs_mutex); 299 + out_valid: 300 + return 1; 301 + out_bad: 302 + mutex_unlock(&kernfs_mutex); 303 + out_bad_unlocked: 304 + /* 305 + * @dentry doesn't match the underlying kernfs node, drop the 306 + * dentry and force lookup. If we have submounts we must allow the 307 + * vfs caches to lie about the state of the filesystem to prevent 308 + * leaks and other nasty things, so use check_submounts_and_drop() 309 + * instead of d_drop(). 310 + */ 311 + if (check_submounts_and_drop(dentry) != 0) 312 + goto out_valid; 313 + 314 + return 0; 315 + } 316 + 317 + static void kernfs_dop_release(struct dentry *dentry) 318 + { 319 + kernfs_put(dentry->d_fsdata); 320 + } 321 + 322 + const struct dentry_operations kernfs_dops = { 323 + .d_revalidate = kernfs_dop_revalidate, 324 + .d_release = kernfs_dop_release, 325 + }; 326 + 327 + static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, 328 + const char *name, umode_t mode, 329 + unsigned flags) 330 + { 331 + char *dup_name = NULL; 332 + struct kernfs_node *kn; 333 + int ret; 334 + 335 + if (!(flags & KERNFS_STATIC_NAME)) { 336 + name = dup_name = kstrdup(name, GFP_KERNEL); 337 + if (!name) 338 + return NULL; 339 + } 340 + 341 + kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL); 342 + if (!kn) 343 + goto err_out1; 344 + 345 + ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL); 346 + if (ret < 0) 347 + goto err_out2; 348 + kn->ino = ret; 349 + 350 + atomic_set(&kn->count, 1); 351 + atomic_set(&kn->active, 0); 352 + 353 + kn->name = name; 354 + kn->mode = mode; 355 + kn->flags = flags | KERNFS_REMOVED; 356 + 357 + return kn; 358 + 359 + err_out2: 360 + kmem_cache_free(kernfs_node_cache, kn); 361 + err_out1: 362 + kfree(dup_name); 363 + return NULL; 364 + } 365 + 366 + struct kernfs_node *kernfs_new_node(struct kernfs_node *parent, 367 + const char *name, umode_t mode, 368 + unsigned flags) 369 + { 370 + struct kernfs_node *kn; 371 + 372 + kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags); 373 + if (kn) { 374 + kernfs_get(parent); 375 + kn->parent = parent; 376 + } 377 + return kn; 378 + } 379 + 380 + /** 381 + * kernfs_addrm_start - prepare for kernfs_node add/remove 382 + * @acxt: pointer to kernfs_addrm_cxt to be used 383 + * 384 + * This function is called when the caller is about to add or remove 385 + * kernfs_node. This function acquires kernfs_mutex. @acxt is used 386 + * to keep and pass context to other addrm functions. 387 + * 388 + * LOCKING: 389 + * Kernel thread context (may sleep). kernfs_mutex is locked on 390 + * return. 391 + */ 392 + void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt) 393 + __acquires(kernfs_mutex) 394 + { 395 + memset(acxt, 0, sizeof(*acxt)); 396 + 397 + mutex_lock(&kernfs_mutex); 398 + } 399 + 400 + /** 401 + * kernfs_add_one - add kernfs_node to parent without warning 402 + * @acxt: addrm context to use 403 + * @kn: kernfs_node to be added 404 + * 405 + * The caller must already have initialized @kn->parent. This 406 + * function increments nlink of the parent's inode if @kn is a 407 + * directory and link into the children list of the parent. 408 + * 409 + * This function should be called between calls to 410 + * kernfs_addrm_start() and kernfs_addrm_finish() and should be passed 411 + * the same @acxt as passed to kernfs_addrm_start(). 412 + * 413 + * LOCKING: 414 + * Determined by kernfs_addrm_start(). 415 + * 416 + * RETURNS: 417 + * 0 on success, -EEXIST if entry with the given name already 418 + * exists. 419 + */ 420 + int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn) 421 + { 422 + struct kernfs_node *parent = kn->parent; 423 + bool has_ns = kernfs_ns_enabled(parent); 424 + struct kernfs_iattrs *ps_iattr; 425 + int ret; 426 + 427 + if (has_ns != (bool)kn->ns) { 428 + WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", 429 + has_ns ? "required" : "invalid", parent->name, kn->name); 430 + return -EINVAL; 431 + } 432 + 433 + if (kernfs_type(parent) != KERNFS_DIR) 434 + return -EINVAL; 435 + 436 + if (parent->flags & KERNFS_REMOVED) 437 + return -ENOENT; 438 + 439 + kn->hash = kernfs_name_hash(kn->name, kn->ns); 440 + 441 + ret = kernfs_link_sibling(kn); 442 + if (ret) 443 + return ret; 444 + 445 + /* Update timestamps on the parent */ 446 + ps_iattr = parent->iattr; 447 + if (ps_iattr) { 448 + struct iattr *ps_iattrs = &ps_iattr->ia_iattr; 449 + ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME; 450 + } 451 + 452 + /* Mark the entry added into directory tree */ 453 + kn->flags &= ~KERNFS_REMOVED; 454 + 455 + return 0; 456 + } 457 + 458 + /** 459 + * kernfs_remove_one - remove kernfs_node from parent 460 + * @acxt: addrm context to use 461 + * @kn: kernfs_node to be removed 462 + * 463 + * Mark @kn removed and drop nlink of parent inode if @kn is a 464 + * directory. @kn is unlinked from the children list. 465 + * 466 + * This function should be called between calls to 467 + * kernfs_addrm_start() and kernfs_addrm_finish() and should be 468 + * passed the same @acxt as passed to kernfs_addrm_start(). 469 + * 470 + * LOCKING: 471 + * Determined by kernfs_addrm_start(). 472 + */ 473 + static void kernfs_remove_one(struct kernfs_addrm_cxt *acxt, 474 + struct kernfs_node *kn) 475 + { 476 + struct kernfs_iattrs *ps_iattr; 477 + 478 + /* 479 + * Removal can be called multiple times on the same node. Only the 480 + * first invocation is effective and puts the base ref. 481 + */ 482 + if (kn->flags & KERNFS_REMOVED) 483 + return; 484 + 485 + if (kn->parent) { 486 + kernfs_unlink_sibling(kn); 487 + 488 + /* Update timestamps on the parent */ 489 + ps_iattr = kn->parent->iattr; 490 + if (ps_iattr) { 491 + ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME; 492 + ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME; 493 + } 494 + } 495 + 496 + kn->flags |= KERNFS_REMOVED; 497 + kn->u.removed_list = acxt->removed; 498 + acxt->removed = kn; 499 + } 500 + 501 + /** 502 + * kernfs_addrm_finish - finish up kernfs_node add/remove 503 + * @acxt: addrm context to finish up 504 + * 505 + * Finish up kernfs_node add/remove. Resources acquired by 506 + * kernfs_addrm_start() are released and removed kernfs_nodes are 507 + * cleaned up. 508 + * 509 + * LOCKING: 510 + * kernfs_mutex is released. 511 + */ 512 + void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt) 513 + __releases(kernfs_mutex) 514 + { 515 + /* release resources acquired by kernfs_addrm_start() */ 516 + mutex_unlock(&kernfs_mutex); 517 + 518 + /* kill removed kernfs_nodes */ 519 + while (acxt->removed) { 520 + struct kernfs_node *kn = acxt->removed; 521 + 522 + acxt->removed = kn->u.removed_list; 523 + 524 + kernfs_deactivate(kn); 525 + kernfs_unmap_bin_file(kn); 526 + kernfs_put(kn); 527 + } 528 + } 529 + 530 + /** 531 + * kernfs_find_ns - find kernfs_node with the given name 532 + * @parent: kernfs_node to search under 533 + * @name: name to look for 534 + * @ns: the namespace tag to use 535 + * 536 + * Look for kernfs_node with name @name under @parent. Returns pointer to 537 + * the found kernfs_node on success, %NULL on failure. 538 + */ 539 + static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent, 540 + const unsigned char *name, 541 + const void *ns) 542 + { 543 + struct rb_node *node = parent->dir.children.rb_node; 544 + bool has_ns = kernfs_ns_enabled(parent); 545 + unsigned int hash; 546 + 547 + lockdep_assert_held(&kernfs_mutex); 548 + 549 + if (has_ns != (bool)ns) { 550 + WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", 551 + has_ns ? "required" : "invalid", parent->name, name); 552 + return NULL; 553 + } 554 + 555 + hash = kernfs_name_hash(name, ns); 556 + while (node) { 557 + struct kernfs_node *kn; 558 + int result; 559 + 560 + kn = rb_to_kn(node); 561 + result = kernfs_name_compare(hash, name, ns, kn); 562 + if (result < 0) 563 + node = node->rb_left; 564 + else if (result > 0) 565 + node = node->rb_right; 566 + else 567 + return kn; 568 + } 569 + return NULL; 570 + } 571 + 572 + /** 573 + * kernfs_find_and_get_ns - find and get kernfs_node with the given name 574 + * @parent: kernfs_node to search under 575 + * @name: name to look for 576 + * @ns: the namespace tag to use 577 + * 578 + * Look for kernfs_node with name @name under @parent and get a reference 579 + * if found. This function may sleep and returns pointer to the found 580 + * kernfs_node on success, %NULL on failure. 581 + */ 582 + struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent, 583 + const char *name, const void *ns) 584 + { 585 + struct kernfs_node *kn; 586 + 587 + mutex_lock(&kernfs_mutex); 588 + kn = kernfs_find_ns(parent, name, ns); 589 + kernfs_get(kn); 590 + mutex_unlock(&kernfs_mutex); 591 + 592 + return kn; 593 + } 594 + EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns); 595 + 596 + /** 597 + * kernfs_create_root - create a new kernfs hierarchy 598 + * @kdops: optional directory syscall operations for the hierarchy 599 + * @priv: opaque data associated with the new directory 600 + * 601 + * Returns the root of the new hierarchy on success, ERR_PTR() value on 602 + * failure. 603 + */ 604 + struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv) 605 + { 606 + struct kernfs_root *root; 607 + struct kernfs_node *kn; 608 + 609 + root = kzalloc(sizeof(*root), GFP_KERNEL); 610 + if (!root) 611 + return ERR_PTR(-ENOMEM); 612 + 613 + ida_init(&root->ino_ida); 614 + 615 + kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO, 616 + KERNFS_DIR); 617 + if (!kn) { 618 + ida_destroy(&root->ino_ida); 619 + kfree(root); 620 + return ERR_PTR(-ENOMEM); 621 + } 622 + 623 + kn->flags &= ~KERNFS_REMOVED; 624 + kn->priv = priv; 625 + kn->dir.root = root; 626 + 627 + root->dir_ops = kdops; 628 + root->kn = kn; 629 + 630 + return root; 631 + } 632 + 633 + /** 634 + * kernfs_destroy_root - destroy a kernfs hierarchy 635 + * @root: root of the hierarchy to destroy 636 + * 637 + * Destroy the hierarchy anchored at @root by removing all existing 638 + * directories and destroying @root. 639 + */ 640 + void kernfs_destroy_root(struct kernfs_root *root) 641 + { 642 + kernfs_remove(root->kn); /* will also free @root */ 643 + } 644 + 645 + /** 646 + * kernfs_create_dir_ns - create a directory 647 + * @parent: parent in which to create a new directory 648 + * @name: name of the new directory 649 + * @mode: mode of the new directory 650 + * @priv: opaque data associated with the new directory 651 + * @ns: optional namespace tag of the directory 652 + * 653 + * Returns the created node on success, ERR_PTR() value on failure. 654 + */ 655 + struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent, 656 + const char *name, umode_t mode, 657 + void *priv, const void *ns) 658 + { 659 + struct kernfs_addrm_cxt acxt; 660 + struct kernfs_node *kn; 661 + int rc; 662 + 663 + /* allocate */ 664 + kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR); 665 + if (!kn) 666 + return ERR_PTR(-ENOMEM); 667 + 668 + kn->dir.root = parent->dir.root; 669 + kn->ns = ns; 670 + kn->priv = priv; 671 + 672 + /* link in */ 673 + kernfs_addrm_start(&acxt); 674 + rc = kernfs_add_one(&acxt, kn); 675 + kernfs_addrm_finish(&acxt); 676 + 677 + if (!rc) 678 + return kn; 679 + 680 + kernfs_put(kn); 681 + return ERR_PTR(rc); 682 + } 683 + 684 + static struct dentry *kernfs_iop_lookup(struct inode *dir, 685 + struct dentry *dentry, 686 + unsigned int flags) 687 + { 688 + struct dentry *ret; 689 + struct kernfs_node *parent = dentry->d_parent->d_fsdata; 690 + struct kernfs_node *kn; 691 + struct inode *inode; 692 + const void *ns = NULL; 693 + 694 + mutex_lock(&kernfs_mutex); 695 + 696 + if (kernfs_ns_enabled(parent)) 697 + ns = kernfs_info(dir->i_sb)->ns; 698 + 699 + kn = kernfs_find_ns(parent, dentry->d_name.name, ns); 700 + 701 + /* no such entry */ 702 + if (!kn) { 703 + ret = NULL; 704 + goto out_unlock; 705 + } 706 + kernfs_get(kn); 707 + dentry->d_fsdata = kn; 708 + 709 + /* attach dentry and inode */ 710 + inode = kernfs_get_inode(dir->i_sb, kn); 711 + if (!inode) { 712 + ret = ERR_PTR(-ENOMEM); 713 + goto out_unlock; 714 + } 715 + 716 + /* instantiate and hash dentry */ 717 + ret = d_materialise_unique(dentry, inode); 718 + out_unlock: 719 + mutex_unlock(&kernfs_mutex); 720 + return ret; 721 + } 722 + 723 + static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry, 724 + umode_t mode) 725 + { 726 + struct kernfs_node *parent = dir->i_private; 727 + struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops; 728 + 729 + if (!kdops || !kdops->mkdir) 730 + return -EPERM; 731 + 732 + return kdops->mkdir(parent, dentry->d_name.name, mode); 733 + } 734 + 735 + static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry) 736 + { 737 + struct kernfs_node *kn = dentry->d_fsdata; 738 + struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops; 739 + 740 + if (!kdops || !kdops->rmdir) 741 + return -EPERM; 742 + 743 + return kdops->rmdir(kn); 744 + } 745 + 746 + static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry, 747 + struct inode *new_dir, struct dentry *new_dentry) 748 + { 749 + struct kernfs_node *kn = old_dentry->d_fsdata; 750 + struct kernfs_node *new_parent = new_dir->i_private; 751 + struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops; 752 + 753 + if (!kdops || !kdops->rename) 754 + return -EPERM; 755 + 756 + return kdops->rename(kn, new_parent, new_dentry->d_name.name); 757 + } 758 + 759 + const struct inode_operations kernfs_dir_iops = { 760 + .lookup = kernfs_iop_lookup, 761 + .permission = kernfs_iop_permission, 762 + .setattr = kernfs_iop_setattr, 763 + .getattr = kernfs_iop_getattr, 764 + .setxattr = kernfs_iop_setxattr, 765 + .removexattr = kernfs_iop_removexattr, 766 + .getxattr = kernfs_iop_getxattr, 767 + .listxattr = kernfs_iop_listxattr, 768 + 769 + .mkdir = kernfs_iop_mkdir, 770 + .rmdir = kernfs_iop_rmdir, 771 + .rename = kernfs_iop_rename, 772 + }; 773 + 774 + static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos) 775 + { 776 + struct kernfs_node *last; 777 + 778 + while (true) { 779 + struct rb_node *rbn; 780 + 781 + last = pos; 782 + 783 + if (kernfs_type(pos) != KERNFS_DIR) 784 + break; 785 + 786 + rbn = rb_first(&pos->dir.children); 787 + if (!rbn) 788 + break; 789 + 790 + pos = rb_to_kn(rbn); 791 + } 792 + 793 + return last; 794 + } 795 + 796 + /** 797 + * kernfs_next_descendant_post - find the next descendant for post-order walk 798 + * @pos: the current position (%NULL to initiate traversal) 799 + * @root: kernfs_node whose descendants to walk 800 + * 801 + * Find the next descendant to visit for post-order traversal of @root's 802 + * descendants. @root is included in the iteration and the last node to be 803 + * visited. 804 + */ 805 + static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos, 806 + struct kernfs_node *root) 807 + { 808 + struct rb_node *rbn; 809 + 810 + lockdep_assert_held(&kernfs_mutex); 811 + 812 + /* if first iteration, visit leftmost descendant which may be root */ 813 + if (!pos) 814 + return kernfs_leftmost_descendant(root); 815 + 816 + /* if we visited @root, we're done */ 817 + if (pos == root) 818 + return NULL; 819 + 820 + /* if there's an unvisited sibling, visit its leftmost descendant */ 821 + rbn = rb_next(&pos->rb); 822 + if (rbn) 823 + return kernfs_leftmost_descendant(rb_to_kn(rbn)); 824 + 825 + /* no sibling left, visit parent */ 826 + return pos->parent; 827 + } 828 + 829 + static void __kernfs_remove(struct kernfs_addrm_cxt *acxt, 830 + struct kernfs_node *kn) 831 + { 832 + struct kernfs_node *pos, *next; 833 + 834 + if (!kn) 835 + return; 836 + 837 + pr_debug("kernfs %s: removing\n", kn->name); 838 + 839 + next = NULL; 840 + do { 841 + pos = next; 842 + next = kernfs_next_descendant_post(pos, kn); 843 + if (pos) 844 + kernfs_remove_one(acxt, pos); 845 + } while (next); 846 + } 847 + 848 + /** 849 + * kernfs_remove - remove a kernfs_node recursively 850 + * @kn: the kernfs_node to remove 851 + * 852 + * Remove @kn along with all its subdirectories and files. 853 + */ 854 + void kernfs_remove(struct kernfs_node *kn) 855 + { 856 + struct kernfs_addrm_cxt acxt; 857 + 858 + kernfs_addrm_start(&acxt); 859 + __kernfs_remove(&acxt, kn); 860 + kernfs_addrm_finish(&acxt); 861 + } 862 + 863 + /** 864 + * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it 865 + * @parent: parent of the target 866 + * @name: name of the kernfs_node to remove 867 + * @ns: namespace tag of the kernfs_node to remove 868 + * 869 + * Look for the kernfs_node with @name and @ns under @parent and remove it. 870 + * Returns 0 on success, -ENOENT if such entry doesn't exist. 871 + */ 872 + int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name, 873 + const void *ns) 874 + { 875 + struct kernfs_addrm_cxt acxt; 876 + struct kernfs_node *kn; 877 + 878 + if (!parent) { 879 + WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n", 880 + name); 881 + return -ENOENT; 882 + } 883 + 884 + kernfs_addrm_start(&acxt); 885 + 886 + kn = kernfs_find_ns(parent, name, ns); 887 + if (kn) 888 + __kernfs_remove(&acxt, kn); 889 + 890 + kernfs_addrm_finish(&acxt); 891 + 892 + if (kn) 893 + return 0; 894 + else 895 + return -ENOENT; 896 + } 897 + 898 + /** 899 + * kernfs_rename_ns - move and rename a kernfs_node 900 + * @kn: target node 901 + * @new_parent: new parent to put @sd under 902 + * @new_name: new name 903 + * @new_ns: new namespace tag 904 + */ 905 + int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, 906 + const char *new_name, const void *new_ns) 907 + { 908 + int error; 909 + 910 + mutex_lock(&kernfs_mutex); 911 + 912 + error = -ENOENT; 913 + if ((kn->flags | new_parent->flags) & KERNFS_REMOVED) 914 + goto out; 915 + 916 + error = 0; 917 + if ((kn->parent == new_parent) && (kn->ns == new_ns) && 918 + (strcmp(kn->name, new_name) == 0)) 919 + goto out; /* nothing to rename */ 920 + 921 + error = -EEXIST; 922 + if (kernfs_find_ns(new_parent, new_name, new_ns)) 923 + goto out; 924 + 925 + /* rename kernfs_node */ 926 + if (strcmp(kn->name, new_name) != 0) { 927 + error = -ENOMEM; 928 + new_name = kstrdup(new_name, GFP_KERNEL); 929 + if (!new_name) 930 + goto out; 931 + 932 + if (kn->flags & KERNFS_STATIC_NAME) 933 + kn->flags &= ~KERNFS_STATIC_NAME; 934 + else 935 + kfree(kn->name); 936 + 937 + kn->name = new_name; 938 + } 939 + 940 + /* 941 + * Move to the appropriate place in the appropriate directories rbtree. 942 + */ 943 + kernfs_unlink_sibling(kn); 944 + kernfs_get(new_parent); 945 + kernfs_put(kn->parent); 946 + kn->ns = new_ns; 947 + kn->hash = kernfs_name_hash(kn->name, kn->ns); 948 + kn->parent = new_parent; 949 + kernfs_link_sibling(kn); 950 + 951 + error = 0; 952 + out: 953 + mutex_unlock(&kernfs_mutex); 954 + return error; 955 + } 956 + 957 + /* Relationship between s_mode and the DT_xxx types */ 958 + static inline unsigned char dt_type(struct kernfs_node *kn) 959 + { 960 + return (kn->mode >> 12) & 15; 961 + } 962 + 963 + static int kernfs_dir_fop_release(struct inode *inode, struct file *filp) 964 + { 965 + kernfs_put(filp->private_data); 966 + return 0; 967 + } 968 + 969 + static struct kernfs_node *kernfs_dir_pos(const void *ns, 970 + struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos) 971 + { 972 + if (pos) { 973 + int valid = !(pos->flags & KERNFS_REMOVED) && 974 + pos->parent == parent && hash == pos->hash; 975 + kernfs_put(pos); 976 + if (!valid) 977 + pos = NULL; 978 + } 979 + if (!pos && (hash > 1) && (hash < INT_MAX)) { 980 + struct rb_node *node = parent->dir.children.rb_node; 981 + while (node) { 982 + pos = rb_to_kn(node); 983 + 984 + if (hash < pos->hash) 985 + node = node->rb_left; 986 + else if (hash > pos->hash) 987 + node = node->rb_right; 988 + else 989 + break; 990 + } 991 + } 992 + /* Skip over entries in the wrong namespace */ 993 + while (pos && pos->ns != ns) { 994 + struct rb_node *node = rb_next(&pos->rb); 995 + if (!node) 996 + pos = NULL; 997 + else 998 + pos = rb_to_kn(node); 999 + } 1000 + return pos; 1001 + } 1002 + 1003 + static struct kernfs_node *kernfs_dir_next_pos(const void *ns, 1004 + struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos) 1005 + { 1006 + pos = kernfs_dir_pos(ns, parent, ino, pos); 1007 + if (pos) 1008 + do { 1009 + struct rb_node *node = rb_next(&pos->rb); 1010 + if (!node) 1011 + pos = NULL; 1012 + else 1013 + pos = rb_to_kn(node); 1014 + } while (pos && pos->ns != ns); 1015 + return pos; 1016 + } 1017 + 1018 + static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx) 1019 + { 1020 + struct dentry *dentry = file->f_path.dentry; 1021 + struct kernfs_node *parent = dentry->d_fsdata; 1022 + struct kernfs_node *pos = file->private_data; 1023 + const void *ns = NULL; 1024 + 1025 + if (!dir_emit_dots(file, ctx)) 1026 + return 0; 1027 + mutex_lock(&kernfs_mutex); 1028 + 1029 + if (kernfs_ns_enabled(parent)) 1030 + ns = kernfs_info(dentry->d_sb)->ns; 1031 + 1032 + for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos); 1033 + pos; 1034 + pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) { 1035 + const char *name = pos->name; 1036 + unsigned int type = dt_type(pos); 1037 + int len = strlen(name); 1038 + ino_t ino = pos->ino; 1039 + 1040 + ctx->pos = pos->hash; 1041 + file->private_data = pos; 1042 + kernfs_get(pos); 1043 + 1044 + mutex_unlock(&kernfs_mutex); 1045 + if (!dir_emit(ctx, name, len, ino, type)) 1046 + return 0; 1047 + mutex_lock(&kernfs_mutex); 1048 + } 1049 + mutex_unlock(&kernfs_mutex); 1050 + file->private_data = NULL; 1051 + ctx->pos = INT_MAX; 1052 + return 0; 1053 + } 1054 + 1055 + static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset, 1056 + int whence) 1057 + { 1058 + struct inode *inode = file_inode(file); 1059 + loff_t ret; 1060 + 1061 + mutex_lock(&inode->i_mutex); 1062 + ret = generic_file_llseek(file, offset, whence); 1063 + mutex_unlock(&inode->i_mutex); 1064 + 1065 + return ret; 1066 + } 1067 + 1068 + const struct file_operations kernfs_dir_fops = { 1069 + .read = generic_read_dir, 1070 + .iterate = kernfs_fop_readdir, 1071 + .release = kernfs_dir_fop_release, 1072 + .llseek = kernfs_dir_fop_llseek, 1073 + };
+867
fs/kernfs/file.c
··· 1 + /* 2 + * fs/kernfs/file.c - kernfs file implementation 3 + * 4 + * Copyright (c) 2001-3 Patrick Mochel 5 + * Copyright (c) 2007 SUSE Linux Products GmbH 6 + * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org> 7 + * 8 + * This file is released under the GPLv2. 9 + */ 10 + 11 + #include <linux/fs.h> 12 + #include <linux/seq_file.h> 13 + #include <linux/slab.h> 14 + #include <linux/poll.h> 15 + #include <linux/pagemap.h> 16 + #include <linux/sched.h> 17 + 18 + #include "kernfs-internal.h" 19 + 20 + /* 21 + * There's one kernfs_open_file for each open file and one kernfs_open_node 22 + * for each kernfs_node with one or more open files. 23 + * 24 + * kernfs_node->attr.open points to kernfs_open_node. attr.open is 25 + * protected by kernfs_open_node_lock. 26 + * 27 + * filp->private_data points to seq_file whose ->private points to 28 + * kernfs_open_file. kernfs_open_files are chained at 29 + * kernfs_open_node->files, which is protected by kernfs_open_file_mutex. 30 + */ 31 + static DEFINE_SPINLOCK(kernfs_open_node_lock); 32 + static DEFINE_MUTEX(kernfs_open_file_mutex); 33 + 34 + struct kernfs_open_node { 35 + atomic_t refcnt; 36 + atomic_t event; 37 + wait_queue_head_t poll; 38 + struct list_head files; /* goes through kernfs_open_file.list */ 39 + }; 40 + 41 + static struct kernfs_open_file *kernfs_of(struct file *file) 42 + { 43 + return ((struct seq_file *)file->private_data)->private; 44 + } 45 + 46 + /* 47 + * Determine the kernfs_ops for the given kernfs_node. This function must 48 + * be called while holding an active reference. 49 + */ 50 + static const struct kernfs_ops *kernfs_ops(struct kernfs_node *kn) 51 + { 52 + if (kn->flags & KERNFS_LOCKDEP) 53 + lockdep_assert_held(kn); 54 + return kn->attr.ops; 55 + } 56 + 57 + /* 58 + * As kernfs_seq_stop() is also called after kernfs_seq_start() or 59 + * kernfs_seq_next() failure, it needs to distinguish whether it's stopping 60 + * a seq_file iteration which is fully initialized with an active reference 61 + * or an aborted kernfs_seq_start() due to get_active failure. The 62 + * position pointer is the only context for each seq_file iteration and 63 + * thus the stop condition should be encoded in it. As the return value is 64 + * directly visible to userland, ERR_PTR(-ENODEV) is the only acceptable 65 + * choice to indicate get_active failure. 66 + * 67 + * Unfortunately, this is complicated due to the optional custom seq_file 68 + * operations which may return ERR_PTR(-ENODEV) too. kernfs_seq_stop() 69 + * can't distinguish whether ERR_PTR(-ENODEV) is from get_active failure or 70 + * custom seq_file operations and thus can't decide whether put_active 71 + * should be performed or not only on ERR_PTR(-ENODEV). 72 + * 73 + * This is worked around by factoring out the custom seq_stop() and 74 + * put_active part into kernfs_seq_stop_active(), skipping it from 75 + * kernfs_seq_stop() if ERR_PTR(-ENODEV) while invoking it directly after 76 + * custom seq_file operations fail with ERR_PTR(-ENODEV) - this ensures 77 + * that kernfs_seq_stop_active() is skipped only after get_active failure. 78 + */ 79 + static void kernfs_seq_stop_active(struct seq_file *sf, void *v) 80 + { 81 + struct kernfs_open_file *of = sf->private; 82 + const struct kernfs_ops *ops = kernfs_ops(of->kn); 83 + 84 + if (ops->seq_stop) 85 + ops->seq_stop(sf, v); 86 + kernfs_put_active(of->kn); 87 + } 88 + 89 + static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos) 90 + { 91 + struct kernfs_open_file *of = sf->private; 92 + const struct kernfs_ops *ops; 93 + 94 + /* 95 + * @of->mutex nests outside active ref and is just to ensure that 96 + * the ops aren't called concurrently for the same open file. 97 + */ 98 + mutex_lock(&of->mutex); 99 + if (!kernfs_get_active(of->kn)) 100 + return ERR_PTR(-ENODEV); 101 + 102 + ops = kernfs_ops(of->kn); 103 + if (ops->seq_start) { 104 + void *next = ops->seq_start(sf, ppos); 105 + /* see the comment above kernfs_seq_stop_active() */ 106 + if (next == ERR_PTR(-ENODEV)) 107 + kernfs_seq_stop_active(sf, next); 108 + return next; 109 + } else { 110 + /* 111 + * The same behavior and code as single_open(). Returns 112 + * !NULL if pos is at the beginning; otherwise, NULL. 113 + */ 114 + return NULL + !*ppos; 115 + } 116 + } 117 + 118 + static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos) 119 + { 120 + struct kernfs_open_file *of = sf->private; 121 + const struct kernfs_ops *ops = kernfs_ops(of->kn); 122 + 123 + if (ops->seq_next) { 124 + void *next = ops->seq_next(sf, v, ppos); 125 + /* see the comment above kernfs_seq_stop_active() */ 126 + if (next == ERR_PTR(-ENODEV)) 127 + kernfs_seq_stop_active(sf, next); 128 + return next; 129 + } else { 130 + /* 131 + * The same behavior and code as single_open(), always 132 + * terminate after the initial read. 133 + */ 134 + ++*ppos; 135 + return NULL; 136 + } 137 + } 138 + 139 + static void kernfs_seq_stop(struct seq_file *sf, void *v) 140 + { 141 + struct kernfs_open_file *of = sf->private; 142 + 143 + if (v != ERR_PTR(-ENODEV)) 144 + kernfs_seq_stop_active(sf, v); 145 + mutex_unlock(&of->mutex); 146 + } 147 + 148 + static int kernfs_seq_show(struct seq_file *sf, void *v) 149 + { 150 + struct kernfs_open_file *of = sf->private; 151 + 152 + of->event = atomic_read(&of->kn->attr.open->event); 153 + 154 + return of->kn->attr.ops->seq_show(sf, v); 155 + } 156 + 157 + static const struct seq_operations kernfs_seq_ops = { 158 + .start = kernfs_seq_start, 159 + .next = kernfs_seq_next, 160 + .stop = kernfs_seq_stop, 161 + .show = kernfs_seq_show, 162 + }; 163 + 164 + /* 165 + * As reading a bin file can have side-effects, the exact offset and bytes 166 + * specified in read(2) call should be passed to the read callback making 167 + * it difficult to use seq_file. Implement simplistic custom buffering for 168 + * bin files. 169 + */ 170 + static ssize_t kernfs_file_direct_read(struct kernfs_open_file *of, 171 + char __user *user_buf, size_t count, 172 + loff_t *ppos) 173 + { 174 + ssize_t len = min_t(size_t, count, PAGE_SIZE); 175 + const struct kernfs_ops *ops; 176 + char *buf; 177 + 178 + buf = kmalloc(len, GFP_KERNEL); 179 + if (!buf) 180 + return -ENOMEM; 181 + 182 + /* 183 + * @of->mutex nests outside active ref and is just to ensure that 184 + * the ops aren't called concurrently for the same open file. 185 + */ 186 + mutex_lock(&of->mutex); 187 + if (!kernfs_get_active(of->kn)) { 188 + len = -ENODEV; 189 + mutex_unlock(&of->mutex); 190 + goto out_free; 191 + } 192 + 193 + ops = kernfs_ops(of->kn); 194 + if (ops->read) 195 + len = ops->read(of, buf, len, *ppos); 196 + else 197 + len = -EINVAL; 198 + 199 + kernfs_put_active(of->kn); 200 + mutex_unlock(&of->mutex); 201 + 202 + if (len < 0) 203 + goto out_free; 204 + 205 + if (copy_to_user(user_buf, buf, len)) { 206 + len = -EFAULT; 207 + goto out_free; 208 + } 209 + 210 + *ppos += len; 211 + 212 + out_free: 213 + kfree(buf); 214 + return len; 215 + } 216 + 217 + /** 218 + * kernfs_fop_read - kernfs vfs read callback 219 + * @file: file pointer 220 + * @user_buf: data to write 221 + * @count: number of bytes 222 + * @ppos: starting offset 223 + */ 224 + static ssize_t kernfs_fop_read(struct file *file, char __user *user_buf, 225 + size_t count, loff_t *ppos) 226 + { 227 + struct kernfs_open_file *of = kernfs_of(file); 228 + 229 + if (of->kn->flags & KERNFS_HAS_SEQ_SHOW) 230 + return seq_read(file, user_buf, count, ppos); 231 + else 232 + return kernfs_file_direct_read(of, user_buf, count, ppos); 233 + } 234 + 235 + /** 236 + * kernfs_fop_write - kernfs vfs write callback 237 + * @file: file pointer 238 + * @user_buf: data to write 239 + * @count: number of bytes 240 + * @ppos: starting offset 241 + * 242 + * Copy data in from userland and pass it to the matching kernfs write 243 + * operation. 244 + * 245 + * There is no easy way for us to know if userspace is only doing a partial 246 + * write, so we don't support them. We expect the entire buffer to come on 247 + * the first write. Hint: if you're writing a value, first read the file, 248 + * modify only the the value you're changing, then write entire buffer 249 + * back. 250 + */ 251 + static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf, 252 + size_t count, loff_t *ppos) 253 + { 254 + struct kernfs_open_file *of = kernfs_of(file); 255 + ssize_t len = min_t(size_t, count, PAGE_SIZE); 256 + const struct kernfs_ops *ops; 257 + char *buf; 258 + 259 + buf = kmalloc(len + 1, GFP_KERNEL); 260 + if (!buf) 261 + return -ENOMEM; 262 + 263 + if (copy_from_user(buf, user_buf, len)) { 264 + len = -EFAULT; 265 + goto out_free; 266 + } 267 + buf[len] = '\0'; /* guarantee string termination */ 268 + 269 + /* 270 + * @of->mutex nests outside active ref and is just to ensure that 271 + * the ops aren't called concurrently for the same open file. 272 + */ 273 + mutex_lock(&of->mutex); 274 + if (!kernfs_get_active(of->kn)) { 275 + mutex_unlock(&of->mutex); 276 + len = -ENODEV; 277 + goto out_free; 278 + } 279 + 280 + ops = kernfs_ops(of->kn); 281 + if (ops->write) 282 + len = ops->write(of, buf, len, *ppos); 283 + else 284 + len = -EINVAL; 285 + 286 + kernfs_put_active(of->kn); 287 + mutex_unlock(&of->mutex); 288 + 289 + if (len > 0) 290 + *ppos += len; 291 + out_free: 292 + kfree(buf); 293 + return len; 294 + } 295 + 296 + static void kernfs_vma_open(struct vm_area_struct *vma) 297 + { 298 + struct file *file = vma->vm_file; 299 + struct kernfs_open_file *of = kernfs_of(file); 300 + 301 + if (!of->vm_ops) 302 + return; 303 + 304 + if (!kernfs_get_active(of->kn)) 305 + return; 306 + 307 + if (of->vm_ops->open) 308 + of->vm_ops->open(vma); 309 + 310 + kernfs_put_active(of->kn); 311 + } 312 + 313 + static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 314 + { 315 + struct file *file = vma->vm_file; 316 + struct kernfs_open_file *of = kernfs_of(file); 317 + int ret; 318 + 319 + if (!of->vm_ops) 320 + return VM_FAULT_SIGBUS; 321 + 322 + if (!kernfs_get_active(of->kn)) 323 + return VM_FAULT_SIGBUS; 324 + 325 + ret = VM_FAULT_SIGBUS; 326 + if (of->vm_ops->fault) 327 + ret = of->vm_ops->fault(vma, vmf); 328 + 329 + kernfs_put_active(of->kn); 330 + return ret; 331 + } 332 + 333 + static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma, 334 + struct vm_fault *vmf) 335 + { 336 + struct file *file = vma->vm_file; 337 + struct kernfs_open_file *of = kernfs_of(file); 338 + int ret; 339 + 340 + if (!of->vm_ops) 341 + return VM_FAULT_SIGBUS; 342 + 343 + if (!kernfs_get_active(of->kn)) 344 + return VM_FAULT_SIGBUS; 345 + 346 + ret = 0; 347 + if (of->vm_ops->page_mkwrite) 348 + ret = of->vm_ops->page_mkwrite(vma, vmf); 349 + else 350 + file_update_time(file); 351 + 352 + kernfs_put_active(of->kn); 353 + return ret; 354 + } 355 + 356 + static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr, 357 + void *buf, int len, int write) 358 + { 359 + struct file *file = vma->vm_file; 360 + struct kernfs_open_file *of = kernfs_of(file); 361 + int ret; 362 + 363 + if (!of->vm_ops) 364 + return -EINVAL; 365 + 366 + if (!kernfs_get_active(of->kn)) 367 + return -EINVAL; 368 + 369 + ret = -EINVAL; 370 + if (of->vm_ops->access) 371 + ret = of->vm_ops->access(vma, addr, buf, len, write); 372 + 373 + kernfs_put_active(of->kn); 374 + return ret; 375 + } 376 + 377 + #ifdef CONFIG_NUMA 378 + static int kernfs_vma_set_policy(struct vm_area_struct *vma, 379 + struct mempolicy *new) 380 + { 381 + struct file *file = vma->vm_file; 382 + struct kernfs_open_file *of = kernfs_of(file); 383 + int ret; 384 + 385 + if (!of->vm_ops) 386 + return 0; 387 + 388 + if (!kernfs_get_active(of->kn)) 389 + return -EINVAL; 390 + 391 + ret = 0; 392 + if (of->vm_ops->set_policy) 393 + ret = of->vm_ops->set_policy(vma, new); 394 + 395 + kernfs_put_active(of->kn); 396 + return ret; 397 + } 398 + 399 + static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma, 400 + unsigned long addr) 401 + { 402 + struct file *file = vma->vm_file; 403 + struct kernfs_open_file *of = kernfs_of(file); 404 + struct mempolicy *pol; 405 + 406 + if (!of->vm_ops) 407 + return vma->vm_policy; 408 + 409 + if (!kernfs_get_active(of->kn)) 410 + return vma->vm_policy; 411 + 412 + pol = vma->vm_policy; 413 + if (of->vm_ops->get_policy) 414 + pol = of->vm_ops->get_policy(vma, addr); 415 + 416 + kernfs_put_active(of->kn); 417 + return pol; 418 + } 419 + 420 + static int kernfs_vma_migrate(struct vm_area_struct *vma, 421 + const nodemask_t *from, const nodemask_t *to, 422 + unsigned long flags) 423 + { 424 + struct file *file = vma->vm_file; 425 + struct kernfs_open_file *of = kernfs_of(file); 426 + int ret; 427 + 428 + if (!of->vm_ops) 429 + return 0; 430 + 431 + if (!kernfs_get_active(of->kn)) 432 + return 0; 433 + 434 + ret = 0; 435 + if (of->vm_ops->migrate) 436 + ret = of->vm_ops->migrate(vma, from, to, flags); 437 + 438 + kernfs_put_active(of->kn); 439 + return ret; 440 + } 441 + #endif 442 + 443 + static const struct vm_operations_struct kernfs_vm_ops = { 444 + .open = kernfs_vma_open, 445 + .fault = kernfs_vma_fault, 446 + .page_mkwrite = kernfs_vma_page_mkwrite, 447 + .access = kernfs_vma_access, 448 + #ifdef CONFIG_NUMA 449 + .set_policy = kernfs_vma_set_policy, 450 + .get_policy = kernfs_vma_get_policy, 451 + .migrate = kernfs_vma_migrate, 452 + #endif 453 + }; 454 + 455 + static int kernfs_fop_mmap(struct file *file, struct vm_area_struct *vma) 456 + { 457 + struct kernfs_open_file *of = kernfs_of(file); 458 + const struct kernfs_ops *ops; 459 + int rc; 460 + 461 + /* 462 + * mmap path and of->mutex are prone to triggering spurious lockdep 463 + * warnings and we don't want to add spurious locking dependency 464 + * between the two. Check whether mmap is actually implemented 465 + * without grabbing @of->mutex by testing HAS_MMAP flag. See the 466 + * comment in kernfs_file_open() for more details. 467 + */ 468 + if (!(of->kn->flags & KERNFS_HAS_MMAP)) 469 + return -ENODEV; 470 + 471 + mutex_lock(&of->mutex); 472 + 473 + rc = -ENODEV; 474 + if (!kernfs_get_active(of->kn)) 475 + goto out_unlock; 476 + 477 + ops = kernfs_ops(of->kn); 478 + rc = ops->mmap(of, vma); 479 + 480 + /* 481 + * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup() 482 + * to satisfy versions of X which crash if the mmap fails: that 483 + * substitutes a new vm_file, and we don't then want bin_vm_ops. 484 + */ 485 + if (vma->vm_file != file) 486 + goto out_put; 487 + 488 + rc = -EINVAL; 489 + if (of->mmapped && of->vm_ops != vma->vm_ops) 490 + goto out_put; 491 + 492 + /* 493 + * It is not possible to successfully wrap close. 494 + * So error if someone is trying to use close. 495 + */ 496 + rc = -EINVAL; 497 + if (vma->vm_ops && vma->vm_ops->close) 498 + goto out_put; 499 + 500 + rc = 0; 501 + of->mmapped = 1; 502 + of->vm_ops = vma->vm_ops; 503 + vma->vm_ops = &kernfs_vm_ops; 504 + out_put: 505 + kernfs_put_active(of->kn); 506 + out_unlock: 507 + mutex_unlock(&of->mutex); 508 + 509 + return rc; 510 + } 511 + 512 + /** 513 + * kernfs_get_open_node - get or create kernfs_open_node 514 + * @kn: target kernfs_node 515 + * @of: kernfs_open_file for this instance of open 516 + * 517 + * If @kn->attr.open exists, increment its reference count; otherwise, 518 + * create one. @of is chained to the files list. 519 + * 520 + * LOCKING: 521 + * Kernel thread context (may sleep). 522 + * 523 + * RETURNS: 524 + * 0 on success, -errno on failure. 525 + */ 526 + static int kernfs_get_open_node(struct kernfs_node *kn, 527 + struct kernfs_open_file *of) 528 + { 529 + struct kernfs_open_node *on, *new_on = NULL; 530 + 531 + retry: 532 + mutex_lock(&kernfs_open_file_mutex); 533 + spin_lock_irq(&kernfs_open_node_lock); 534 + 535 + if (!kn->attr.open && new_on) { 536 + kn->attr.open = new_on; 537 + new_on = NULL; 538 + } 539 + 540 + on = kn->attr.open; 541 + if (on) { 542 + atomic_inc(&on->refcnt); 543 + list_add_tail(&of->list, &on->files); 544 + } 545 + 546 + spin_unlock_irq(&kernfs_open_node_lock); 547 + mutex_unlock(&kernfs_open_file_mutex); 548 + 549 + if (on) { 550 + kfree(new_on); 551 + return 0; 552 + } 553 + 554 + /* not there, initialize a new one and retry */ 555 + new_on = kmalloc(sizeof(*new_on), GFP_KERNEL); 556 + if (!new_on) 557 + return -ENOMEM; 558 + 559 + atomic_set(&new_on->refcnt, 0); 560 + atomic_set(&new_on->event, 1); 561 + init_waitqueue_head(&new_on->poll); 562 + INIT_LIST_HEAD(&new_on->files); 563 + goto retry; 564 + } 565 + 566 + /** 567 + * kernfs_put_open_node - put kernfs_open_node 568 + * @kn: target kernfs_nodet 569 + * @of: associated kernfs_open_file 570 + * 571 + * Put @kn->attr.open and unlink @of from the files list. If 572 + * reference count reaches zero, disassociate and free it. 573 + * 574 + * LOCKING: 575 + * None. 576 + */ 577 + static void kernfs_put_open_node(struct kernfs_node *kn, 578 + struct kernfs_open_file *of) 579 + { 580 + struct kernfs_open_node *on = kn->attr.open; 581 + unsigned long flags; 582 + 583 + mutex_lock(&kernfs_open_file_mutex); 584 + spin_lock_irqsave(&kernfs_open_node_lock, flags); 585 + 586 + if (of) 587 + list_del(&of->list); 588 + 589 + if (atomic_dec_and_test(&on->refcnt)) 590 + kn->attr.open = NULL; 591 + else 592 + on = NULL; 593 + 594 + spin_unlock_irqrestore(&kernfs_open_node_lock, flags); 595 + mutex_unlock(&kernfs_open_file_mutex); 596 + 597 + kfree(on); 598 + } 599 + 600 + static int kernfs_fop_open(struct inode *inode, struct file *file) 601 + { 602 + struct kernfs_node *kn = file->f_path.dentry->d_fsdata; 603 + const struct kernfs_ops *ops; 604 + struct kernfs_open_file *of; 605 + bool has_read, has_write, has_mmap; 606 + int error = -EACCES; 607 + 608 + if (!kernfs_get_active(kn)) 609 + return -ENODEV; 610 + 611 + ops = kernfs_ops(kn); 612 + 613 + has_read = ops->seq_show || ops->read || ops->mmap; 614 + has_write = ops->write || ops->mmap; 615 + has_mmap = ops->mmap; 616 + 617 + /* check perms and supported operations */ 618 + if ((file->f_mode & FMODE_WRITE) && 619 + (!(inode->i_mode & S_IWUGO) || !has_write)) 620 + goto err_out; 621 + 622 + if ((file->f_mode & FMODE_READ) && 623 + (!(inode->i_mode & S_IRUGO) || !has_read)) 624 + goto err_out; 625 + 626 + /* allocate a kernfs_open_file for the file */ 627 + error = -ENOMEM; 628 + of = kzalloc(sizeof(struct kernfs_open_file), GFP_KERNEL); 629 + if (!of) 630 + goto err_out; 631 + 632 + /* 633 + * The following is done to give a different lockdep key to 634 + * @of->mutex for files which implement mmap. This is a rather 635 + * crude way to avoid false positive lockdep warning around 636 + * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and 637 + * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under 638 + * which mm->mmap_sem nests, while holding @of->mutex. As each 639 + * open file has a separate mutex, it's okay as long as those don't 640 + * happen on the same file. At this point, we can't easily give 641 + * each file a separate locking class. Let's differentiate on 642 + * whether the file has mmap or not for now. 643 + * 644 + * Both paths of the branch look the same. They're supposed to 645 + * look that way and give @of->mutex different static lockdep keys. 646 + */ 647 + if (has_mmap) 648 + mutex_init(&of->mutex); 649 + else 650 + mutex_init(&of->mutex); 651 + 652 + of->kn = kn; 653 + of->file = file; 654 + 655 + /* 656 + * Always instantiate seq_file even if read access doesn't use 657 + * seq_file or is not requested. This unifies private data access 658 + * and readable regular files are the vast majority anyway. 659 + */ 660 + if (ops->seq_show) 661 + error = seq_open(file, &kernfs_seq_ops); 662 + else 663 + error = seq_open(file, NULL); 664 + if (error) 665 + goto err_free; 666 + 667 + ((struct seq_file *)file->private_data)->private = of; 668 + 669 + /* seq_file clears PWRITE unconditionally, restore it if WRITE */ 670 + if (file->f_mode & FMODE_WRITE) 671 + file->f_mode |= FMODE_PWRITE; 672 + 673 + /* make sure we have open node struct */ 674 + error = kernfs_get_open_node(kn, of); 675 + if (error) 676 + goto err_close; 677 + 678 + /* open succeeded, put active references */ 679 + kernfs_put_active(kn); 680 + return 0; 681 + 682 + err_close: 683 + seq_release(inode, file); 684 + err_free: 685 + kfree(of); 686 + err_out: 687 + kernfs_put_active(kn); 688 + return error; 689 + } 690 + 691 + static int kernfs_fop_release(struct inode *inode, struct file *filp) 692 + { 693 + struct kernfs_node *kn = filp->f_path.dentry->d_fsdata; 694 + struct kernfs_open_file *of = kernfs_of(filp); 695 + 696 + kernfs_put_open_node(kn, of); 697 + seq_release(inode, filp); 698 + kfree(of); 699 + 700 + return 0; 701 + } 702 + 703 + void kernfs_unmap_bin_file(struct kernfs_node *kn) 704 + { 705 + struct kernfs_open_node *on; 706 + struct kernfs_open_file *of; 707 + 708 + if (!(kn->flags & KERNFS_HAS_MMAP)) 709 + return; 710 + 711 + spin_lock_irq(&kernfs_open_node_lock); 712 + on = kn->attr.open; 713 + if (on) 714 + atomic_inc(&on->refcnt); 715 + spin_unlock_irq(&kernfs_open_node_lock); 716 + if (!on) 717 + return; 718 + 719 + mutex_lock(&kernfs_open_file_mutex); 720 + list_for_each_entry(of, &on->files, list) { 721 + struct inode *inode = file_inode(of->file); 722 + unmap_mapping_range(inode->i_mapping, 0, 0, 1); 723 + } 724 + mutex_unlock(&kernfs_open_file_mutex); 725 + 726 + kernfs_put_open_node(kn, NULL); 727 + } 728 + 729 + /* 730 + * Kernfs attribute files are pollable. The idea is that you read 731 + * the content and then you use 'poll' or 'select' to wait for 732 + * the content to change. When the content changes (assuming the 733 + * manager for the kobject supports notification), poll will 734 + * return POLLERR|POLLPRI, and select will return the fd whether 735 + * it is waiting for read, write, or exceptions. 736 + * Once poll/select indicates that the value has changed, you 737 + * need to close and re-open the file, or seek to 0 and read again. 738 + * Reminder: this only works for attributes which actively support 739 + * it, and it is not possible to test an attribute from userspace 740 + * to see if it supports poll (Neither 'poll' nor 'select' return 741 + * an appropriate error code). When in doubt, set a suitable timeout value. 742 + */ 743 + static unsigned int kernfs_fop_poll(struct file *filp, poll_table *wait) 744 + { 745 + struct kernfs_open_file *of = kernfs_of(filp); 746 + struct kernfs_node *kn = filp->f_path.dentry->d_fsdata; 747 + struct kernfs_open_node *on = kn->attr.open; 748 + 749 + /* need parent for the kobj, grab both */ 750 + if (!kernfs_get_active(kn)) 751 + goto trigger; 752 + 753 + poll_wait(filp, &on->poll, wait); 754 + 755 + kernfs_put_active(kn); 756 + 757 + if (of->event != atomic_read(&on->event)) 758 + goto trigger; 759 + 760 + return DEFAULT_POLLMASK; 761 + 762 + trigger: 763 + return DEFAULT_POLLMASK|POLLERR|POLLPRI; 764 + } 765 + 766 + /** 767 + * kernfs_notify - notify a kernfs file 768 + * @kn: file to notify 769 + * 770 + * Notify @kn such that poll(2) on @kn wakes up. 771 + */ 772 + void kernfs_notify(struct kernfs_node *kn) 773 + { 774 + struct kernfs_open_node *on; 775 + unsigned long flags; 776 + 777 + spin_lock_irqsave(&kernfs_open_node_lock, flags); 778 + 779 + if (!WARN_ON(kernfs_type(kn) != KERNFS_FILE)) { 780 + on = kn->attr.open; 781 + if (on) { 782 + atomic_inc(&on->event); 783 + wake_up_interruptible(&on->poll); 784 + } 785 + } 786 + 787 + spin_unlock_irqrestore(&kernfs_open_node_lock, flags); 788 + } 789 + EXPORT_SYMBOL_GPL(kernfs_notify); 790 + 791 + const struct file_operations kernfs_file_fops = { 792 + .read = kernfs_fop_read, 793 + .write = kernfs_fop_write, 794 + .llseek = generic_file_llseek, 795 + .mmap = kernfs_fop_mmap, 796 + .open = kernfs_fop_open, 797 + .release = kernfs_fop_release, 798 + .poll = kernfs_fop_poll, 799 + }; 800 + 801 + /** 802 + * __kernfs_create_file - kernfs internal function to create a file 803 + * @parent: directory to create the file in 804 + * @name: name of the file 805 + * @mode: mode of the file 806 + * @size: size of the file 807 + * @ops: kernfs operations for the file 808 + * @priv: private data for the file 809 + * @ns: optional namespace tag of the file 810 + * @static_name: don't copy file name 811 + * @key: lockdep key for the file's active_ref, %NULL to disable lockdep 812 + * 813 + * Returns the created node on success, ERR_PTR() value on error. 814 + */ 815 + struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent, 816 + const char *name, 817 + umode_t mode, loff_t size, 818 + const struct kernfs_ops *ops, 819 + void *priv, const void *ns, 820 + bool name_is_static, 821 + struct lock_class_key *key) 822 + { 823 + struct kernfs_addrm_cxt acxt; 824 + struct kernfs_node *kn; 825 + unsigned flags; 826 + int rc; 827 + 828 + flags = KERNFS_FILE; 829 + if (name_is_static) 830 + flags |= KERNFS_STATIC_NAME; 831 + 832 + kn = kernfs_new_node(parent, name, (mode & S_IALLUGO) | S_IFREG, flags); 833 + if (!kn) 834 + return ERR_PTR(-ENOMEM); 835 + 836 + kn->attr.ops = ops; 837 + kn->attr.size = size; 838 + kn->ns = ns; 839 + kn->priv = priv; 840 + 841 + #ifdef CONFIG_DEBUG_LOCK_ALLOC 842 + if (key) { 843 + lockdep_init_map(&kn->dep_map, "s_active", key, 0); 844 + kn->flags |= KERNFS_LOCKDEP; 845 + } 846 + #endif 847 + 848 + /* 849 + * kn->attr.ops is accesible only while holding active ref. We 850 + * need to know whether some ops are implemented outside active 851 + * ref. Cache their existence in flags. 852 + */ 853 + if (ops->seq_show) 854 + kn->flags |= KERNFS_HAS_SEQ_SHOW; 855 + if (ops->mmap) 856 + kn->flags |= KERNFS_HAS_MMAP; 857 + 858 + kernfs_addrm_start(&acxt); 859 + rc = kernfs_add_one(&acxt, kn); 860 + kernfs_addrm_finish(&acxt); 861 + 862 + if (rc) { 863 + kernfs_put(kn); 864 + return ERR_PTR(rc); 865 + } 866 + return kn; 867 + }
+377
fs/kernfs/inode.c
··· 1 + /* 2 + * fs/kernfs/inode.c - kernfs inode implementation 3 + * 4 + * Copyright (c) 2001-3 Patrick Mochel 5 + * Copyright (c) 2007 SUSE Linux Products GmbH 6 + * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org> 7 + * 8 + * This file is released under the GPLv2. 9 + */ 10 + 11 + #include <linux/pagemap.h> 12 + #include <linux/backing-dev.h> 13 + #include <linux/capability.h> 14 + #include <linux/errno.h> 15 + #include <linux/slab.h> 16 + #include <linux/xattr.h> 17 + #include <linux/security.h> 18 + 19 + #include "kernfs-internal.h" 20 + 21 + static const struct address_space_operations kernfs_aops = { 22 + .readpage = simple_readpage, 23 + .write_begin = simple_write_begin, 24 + .write_end = simple_write_end, 25 + }; 26 + 27 + static struct backing_dev_info kernfs_bdi = { 28 + .name = "kernfs", 29 + .ra_pages = 0, /* No readahead */ 30 + .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, 31 + }; 32 + 33 + static const struct inode_operations kernfs_iops = { 34 + .permission = kernfs_iop_permission, 35 + .setattr = kernfs_iop_setattr, 36 + .getattr = kernfs_iop_getattr, 37 + .setxattr = kernfs_iop_setxattr, 38 + .removexattr = kernfs_iop_removexattr, 39 + .getxattr = kernfs_iop_getxattr, 40 + .listxattr = kernfs_iop_listxattr, 41 + }; 42 + 43 + void __init kernfs_inode_init(void) 44 + { 45 + if (bdi_init(&kernfs_bdi)) 46 + panic("failed to init kernfs_bdi"); 47 + } 48 + 49 + static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn) 50 + { 51 + struct iattr *iattrs; 52 + 53 + if (kn->iattr) 54 + return kn->iattr; 55 + 56 + kn->iattr = kzalloc(sizeof(struct kernfs_iattrs), GFP_KERNEL); 57 + if (!kn->iattr) 58 + return NULL; 59 + iattrs = &kn->iattr->ia_iattr; 60 + 61 + /* assign default attributes */ 62 + iattrs->ia_mode = kn->mode; 63 + iattrs->ia_uid = GLOBAL_ROOT_UID; 64 + iattrs->ia_gid = GLOBAL_ROOT_GID; 65 + iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME; 66 + 67 + simple_xattrs_init(&kn->iattr->xattrs); 68 + 69 + return kn->iattr; 70 + } 71 + 72 + static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) 73 + { 74 + struct kernfs_iattrs *attrs; 75 + struct iattr *iattrs; 76 + unsigned int ia_valid = iattr->ia_valid; 77 + 78 + attrs = kernfs_iattrs(kn); 79 + if (!attrs) 80 + return -ENOMEM; 81 + 82 + iattrs = &attrs->ia_iattr; 83 + 84 + if (ia_valid & ATTR_UID) 85 + iattrs->ia_uid = iattr->ia_uid; 86 + if (ia_valid & ATTR_GID) 87 + iattrs->ia_gid = iattr->ia_gid; 88 + if (ia_valid & ATTR_ATIME) 89 + iattrs->ia_atime = iattr->ia_atime; 90 + if (ia_valid & ATTR_MTIME) 91 + iattrs->ia_mtime = iattr->ia_mtime; 92 + if (ia_valid & ATTR_CTIME) 93 + iattrs->ia_ctime = iattr->ia_ctime; 94 + if (ia_valid & ATTR_MODE) { 95 + umode_t mode = iattr->ia_mode; 96 + iattrs->ia_mode = kn->mode = mode; 97 + } 98 + return 0; 99 + } 100 + 101 + /** 102 + * kernfs_setattr - set iattr on a node 103 + * @kn: target node 104 + * @iattr: iattr to set 105 + * 106 + * Returns 0 on success, -errno on failure. 107 + */ 108 + int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) 109 + { 110 + int ret; 111 + 112 + mutex_lock(&kernfs_mutex); 113 + ret = __kernfs_setattr(kn, iattr); 114 + mutex_unlock(&kernfs_mutex); 115 + return ret; 116 + } 117 + 118 + int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr) 119 + { 120 + struct inode *inode = dentry->d_inode; 121 + struct kernfs_node *kn = dentry->d_fsdata; 122 + int error; 123 + 124 + if (!kn) 125 + return -EINVAL; 126 + 127 + mutex_lock(&kernfs_mutex); 128 + error = inode_change_ok(inode, iattr); 129 + if (error) 130 + goto out; 131 + 132 + error = __kernfs_setattr(kn, iattr); 133 + if (error) 134 + goto out; 135 + 136 + /* this ignores size changes */ 137 + setattr_copy(inode, iattr); 138 + 139 + out: 140 + mutex_unlock(&kernfs_mutex); 141 + return error; 142 + } 143 + 144 + static int kernfs_node_setsecdata(struct kernfs_node *kn, void **secdata, 145 + u32 *secdata_len) 146 + { 147 + struct kernfs_iattrs *attrs; 148 + void *old_secdata; 149 + size_t old_secdata_len; 150 + 151 + attrs = kernfs_iattrs(kn); 152 + if (!attrs) 153 + return -ENOMEM; 154 + 155 + old_secdata = attrs->ia_secdata; 156 + old_secdata_len = attrs->ia_secdata_len; 157 + 158 + attrs->ia_secdata = *secdata; 159 + attrs->ia_secdata_len = *secdata_len; 160 + 161 + *secdata = old_secdata; 162 + *secdata_len = old_secdata_len; 163 + return 0; 164 + } 165 + 166 + int kernfs_iop_setxattr(struct dentry *dentry, const char *name, 167 + const void *value, size_t size, int flags) 168 + { 169 + struct kernfs_node *kn = dentry->d_fsdata; 170 + struct kernfs_iattrs *attrs; 171 + void *secdata; 172 + int error; 173 + u32 secdata_len = 0; 174 + 175 + attrs = kernfs_iattrs(kn); 176 + if (!attrs) 177 + return -ENOMEM; 178 + 179 + if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) { 180 + const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; 181 + error = security_inode_setsecurity(dentry->d_inode, suffix, 182 + value, size, flags); 183 + if (error) 184 + return error; 185 + error = security_inode_getsecctx(dentry->d_inode, 186 + &secdata, &secdata_len); 187 + if (error) 188 + return error; 189 + 190 + mutex_lock(&kernfs_mutex); 191 + error = kernfs_node_setsecdata(kn, &secdata, &secdata_len); 192 + mutex_unlock(&kernfs_mutex); 193 + 194 + if (secdata) 195 + security_release_secctx(secdata, secdata_len); 196 + return error; 197 + } else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) { 198 + return simple_xattr_set(&attrs->xattrs, name, value, size, 199 + flags); 200 + } 201 + 202 + return -EINVAL; 203 + } 204 + 205 + int kernfs_iop_removexattr(struct dentry *dentry, const char *name) 206 + { 207 + struct kernfs_node *kn = dentry->d_fsdata; 208 + struct kernfs_iattrs *attrs; 209 + 210 + attrs = kernfs_iattrs(kn); 211 + if (!attrs) 212 + return -ENOMEM; 213 + 214 + return simple_xattr_remove(&attrs->xattrs, name); 215 + } 216 + 217 + ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf, 218 + size_t size) 219 + { 220 + struct kernfs_node *kn = dentry->d_fsdata; 221 + struct kernfs_iattrs *attrs; 222 + 223 + attrs = kernfs_iattrs(kn); 224 + if (!attrs) 225 + return -ENOMEM; 226 + 227 + return simple_xattr_get(&attrs->xattrs, name, buf, size); 228 + } 229 + 230 + ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size) 231 + { 232 + struct kernfs_node *kn = dentry->d_fsdata; 233 + struct kernfs_iattrs *attrs; 234 + 235 + attrs = kernfs_iattrs(kn); 236 + if (!attrs) 237 + return -ENOMEM; 238 + 239 + return simple_xattr_list(&attrs->xattrs, buf, size); 240 + } 241 + 242 + static inline void set_default_inode_attr(struct inode *inode, umode_t mode) 243 + { 244 + inode->i_mode = mode; 245 + inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 246 + } 247 + 248 + static inline void set_inode_attr(struct inode *inode, struct iattr *iattr) 249 + { 250 + inode->i_uid = iattr->ia_uid; 251 + inode->i_gid = iattr->ia_gid; 252 + inode->i_atime = iattr->ia_atime; 253 + inode->i_mtime = iattr->ia_mtime; 254 + inode->i_ctime = iattr->ia_ctime; 255 + } 256 + 257 + static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode) 258 + { 259 + struct kernfs_iattrs *attrs = kn->iattr; 260 + 261 + inode->i_mode = kn->mode; 262 + if (attrs) { 263 + /* 264 + * kernfs_node has non-default attributes get them from 265 + * persistent copy in kernfs_node. 266 + */ 267 + set_inode_attr(inode, &attrs->ia_iattr); 268 + security_inode_notifysecctx(inode, attrs->ia_secdata, 269 + attrs->ia_secdata_len); 270 + } 271 + 272 + if (kernfs_type(kn) == KERNFS_DIR) 273 + set_nlink(inode, kn->dir.subdirs + 2); 274 + } 275 + 276 + int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry, 277 + struct kstat *stat) 278 + { 279 + struct kernfs_node *kn = dentry->d_fsdata; 280 + struct inode *inode = dentry->d_inode; 281 + 282 + mutex_lock(&kernfs_mutex); 283 + kernfs_refresh_inode(kn, inode); 284 + mutex_unlock(&kernfs_mutex); 285 + 286 + generic_fillattr(inode, stat); 287 + return 0; 288 + } 289 + 290 + static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) 291 + { 292 + kernfs_get(kn); 293 + inode->i_private = kn; 294 + inode->i_mapping->a_ops = &kernfs_aops; 295 + inode->i_mapping->backing_dev_info = &kernfs_bdi; 296 + inode->i_op = &kernfs_iops; 297 + 298 + set_default_inode_attr(inode, kn->mode); 299 + kernfs_refresh_inode(kn, inode); 300 + 301 + /* initialize inode according to type */ 302 + switch (kernfs_type(kn)) { 303 + case KERNFS_DIR: 304 + inode->i_op = &kernfs_dir_iops; 305 + inode->i_fop = &kernfs_dir_fops; 306 + break; 307 + case KERNFS_FILE: 308 + inode->i_size = kn->attr.size; 309 + inode->i_fop = &kernfs_file_fops; 310 + break; 311 + case KERNFS_LINK: 312 + inode->i_op = &kernfs_symlink_iops; 313 + break; 314 + default: 315 + BUG(); 316 + } 317 + 318 + unlock_new_inode(inode); 319 + } 320 + 321 + /** 322 + * kernfs_get_inode - get inode for kernfs_node 323 + * @sb: super block 324 + * @kn: kernfs_node to allocate inode for 325 + * 326 + * Get inode for @kn. If such inode doesn't exist, a new inode is 327 + * allocated and basics are initialized. New inode is returned 328 + * locked. 329 + * 330 + * LOCKING: 331 + * Kernel thread context (may sleep). 332 + * 333 + * RETURNS: 334 + * Pointer to allocated inode on success, NULL on failure. 335 + */ 336 + struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn) 337 + { 338 + struct inode *inode; 339 + 340 + inode = iget_locked(sb, kn->ino); 341 + if (inode && (inode->i_state & I_NEW)) 342 + kernfs_init_inode(kn, inode); 343 + 344 + return inode; 345 + } 346 + 347 + /* 348 + * The kernfs_node serves as both an inode and a directory entry for 349 + * kernfs. To prevent the kernfs inode numbers from being freed 350 + * prematurely we take a reference to kernfs_node from the kernfs inode. A 351 + * super_operations.evict_inode() implementation is needed to drop that 352 + * reference upon inode destruction. 353 + */ 354 + void kernfs_evict_inode(struct inode *inode) 355 + { 356 + struct kernfs_node *kn = inode->i_private; 357 + 358 + truncate_inode_pages(&inode->i_data, 0); 359 + clear_inode(inode); 360 + kernfs_put(kn); 361 + } 362 + 363 + int kernfs_iop_permission(struct inode *inode, int mask) 364 + { 365 + struct kernfs_node *kn; 366 + 367 + if (mask & MAY_NOT_BLOCK) 368 + return -ECHILD; 369 + 370 + kn = inode->i_private; 371 + 372 + mutex_lock(&kernfs_mutex); 373 + kernfs_refresh_inode(kn, inode); 374 + mutex_unlock(&kernfs_mutex); 375 + 376 + return generic_permission(inode, mask); 377 + }
+122
fs/kernfs/kernfs-internal.h
··· 1 + /* 2 + * fs/kernfs/kernfs-internal.h - kernfs internal header file 3 + * 4 + * Copyright (c) 2001-3 Patrick Mochel 5 + * Copyright (c) 2007 SUSE Linux Products GmbH 6 + * Copyright (c) 2007, 2013 Tejun Heo <teheo@suse.de> 7 + * 8 + * This file is released under the GPLv2. 9 + */ 10 + 11 + #ifndef __KERNFS_INTERNAL_H 12 + #define __KERNFS_INTERNAL_H 13 + 14 + #include <linux/lockdep.h> 15 + #include <linux/fs.h> 16 + #include <linux/mutex.h> 17 + #include <linux/xattr.h> 18 + 19 + #include <linux/kernfs.h> 20 + 21 + struct kernfs_iattrs { 22 + struct iattr ia_iattr; 23 + void *ia_secdata; 24 + u32 ia_secdata_len; 25 + 26 + struct simple_xattrs xattrs; 27 + }; 28 + 29 + #define KN_DEACTIVATED_BIAS INT_MIN 30 + 31 + /* KERNFS_TYPE_MASK and types are defined in include/linux/kernfs.h */ 32 + 33 + /** 34 + * kernfs_root - find out the kernfs_root a kernfs_node belongs to 35 + * @kn: kernfs_node of interest 36 + * 37 + * Return the kernfs_root @kn belongs to. 38 + */ 39 + static inline struct kernfs_root *kernfs_root(struct kernfs_node *kn) 40 + { 41 + /* if parent exists, it's always a dir; otherwise, @sd is a dir */ 42 + if (kn->parent) 43 + kn = kn->parent; 44 + return kn->dir.root; 45 + } 46 + 47 + /* 48 + * Context structure to be used while adding/removing nodes. 49 + */ 50 + struct kernfs_addrm_cxt { 51 + struct kernfs_node *removed; 52 + }; 53 + 54 + /* 55 + * mount.c 56 + */ 57 + struct kernfs_super_info { 58 + /* 59 + * The root associated with this super_block. Each super_block is 60 + * identified by the root and ns it's associated with. 61 + */ 62 + struct kernfs_root *root; 63 + 64 + /* 65 + * Each sb is associated with one namespace tag, currently the 66 + * network namespace of the task which mounted this kernfs 67 + * instance. If multiple tags become necessary, make the following 68 + * an array and compare kernfs_node tag against every entry. 69 + */ 70 + const void *ns; 71 + }; 72 + #define kernfs_info(SB) ((struct kernfs_super_info *)(SB->s_fs_info)) 73 + 74 + extern struct kmem_cache *kernfs_node_cache; 75 + 76 + /* 77 + * inode.c 78 + */ 79 + struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn); 80 + void kernfs_evict_inode(struct inode *inode); 81 + int kernfs_iop_permission(struct inode *inode, int mask); 82 + int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr); 83 + int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry, 84 + struct kstat *stat); 85 + int kernfs_iop_setxattr(struct dentry *dentry, const char *name, const void *value, 86 + size_t size, int flags); 87 + int kernfs_iop_removexattr(struct dentry *dentry, const char *name); 88 + ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf, 89 + size_t size); 90 + ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size); 91 + void kernfs_inode_init(void); 92 + 93 + /* 94 + * dir.c 95 + */ 96 + extern struct mutex kernfs_mutex; 97 + extern const struct dentry_operations kernfs_dops; 98 + extern const struct file_operations kernfs_dir_fops; 99 + extern const struct inode_operations kernfs_dir_iops; 100 + 101 + struct kernfs_node *kernfs_get_active(struct kernfs_node *kn); 102 + void kernfs_put_active(struct kernfs_node *kn); 103 + void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt); 104 + int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn); 105 + void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt); 106 + struct kernfs_node *kernfs_new_node(struct kernfs_node *parent, 107 + const char *name, umode_t mode, 108 + unsigned flags); 109 + 110 + /* 111 + * file.c 112 + */ 113 + extern const struct file_operations kernfs_file_fops; 114 + 115 + void kernfs_unmap_bin_file(struct kernfs_node *kn); 116 + 117 + /* 118 + * symlink.c 119 + */ 120 + extern const struct inode_operations kernfs_symlink_iops; 121 + 122 + #endif /* __KERNFS_INTERNAL_H */
+165
fs/kernfs/mount.c
··· 1 + /* 2 + * fs/kernfs/mount.c - kernfs mount implementation 3 + * 4 + * Copyright (c) 2001-3 Patrick Mochel 5 + * Copyright (c) 2007 SUSE Linux Products GmbH 6 + * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org> 7 + * 8 + * This file is released under the GPLv2. 9 + */ 10 + 11 + #include <linux/fs.h> 12 + #include <linux/mount.h> 13 + #include <linux/init.h> 14 + #include <linux/magic.h> 15 + #include <linux/slab.h> 16 + #include <linux/pagemap.h> 17 + 18 + #include "kernfs-internal.h" 19 + 20 + struct kmem_cache *kernfs_node_cache; 21 + 22 + static const struct super_operations kernfs_sops = { 23 + .statfs = simple_statfs, 24 + .drop_inode = generic_delete_inode, 25 + .evict_inode = kernfs_evict_inode, 26 + }; 27 + 28 + static int kernfs_fill_super(struct super_block *sb) 29 + { 30 + struct kernfs_super_info *info = kernfs_info(sb); 31 + struct inode *inode; 32 + struct dentry *root; 33 + 34 + sb->s_blocksize = PAGE_CACHE_SIZE; 35 + sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 36 + sb->s_magic = SYSFS_MAGIC; 37 + sb->s_op = &kernfs_sops; 38 + sb->s_time_gran = 1; 39 + 40 + /* get root inode, initialize and unlock it */ 41 + mutex_lock(&kernfs_mutex); 42 + inode = kernfs_get_inode(sb, info->root->kn); 43 + mutex_unlock(&kernfs_mutex); 44 + if (!inode) { 45 + pr_debug("kernfs: could not get root inode\n"); 46 + return -ENOMEM; 47 + } 48 + 49 + /* instantiate and link root dentry */ 50 + root = d_make_root(inode); 51 + if (!root) { 52 + pr_debug("%s: could not get root dentry!\n", __func__); 53 + return -ENOMEM; 54 + } 55 + kernfs_get(info->root->kn); 56 + root->d_fsdata = info->root->kn; 57 + sb->s_root = root; 58 + sb->s_d_op = &kernfs_dops; 59 + return 0; 60 + } 61 + 62 + static int kernfs_test_super(struct super_block *sb, void *data) 63 + { 64 + struct kernfs_super_info *sb_info = kernfs_info(sb); 65 + struct kernfs_super_info *info = data; 66 + 67 + return sb_info->root == info->root && sb_info->ns == info->ns; 68 + } 69 + 70 + static int kernfs_set_super(struct super_block *sb, void *data) 71 + { 72 + int error; 73 + error = set_anon_super(sb, data); 74 + if (!error) 75 + sb->s_fs_info = data; 76 + return error; 77 + } 78 + 79 + /** 80 + * kernfs_super_ns - determine the namespace tag of a kernfs super_block 81 + * @sb: super_block of interest 82 + * 83 + * Return the namespace tag associated with kernfs super_block @sb. 84 + */ 85 + const void *kernfs_super_ns(struct super_block *sb) 86 + { 87 + struct kernfs_super_info *info = kernfs_info(sb); 88 + 89 + return info->ns; 90 + } 91 + 92 + /** 93 + * kernfs_mount_ns - kernfs mount helper 94 + * @fs_type: file_system_type of the fs being mounted 95 + * @flags: mount flags specified for the mount 96 + * @root: kernfs_root of the hierarchy being mounted 97 + * @ns: optional namespace tag of the mount 98 + * 99 + * This is to be called from each kernfs user's file_system_type->mount() 100 + * implementation, which should pass through the specified @fs_type and 101 + * @flags, and specify the hierarchy and namespace tag to mount via @root 102 + * and @ns, respectively. 103 + * 104 + * The return value can be passed to the vfs layer verbatim. 105 + */ 106 + struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags, 107 + struct kernfs_root *root, const void *ns) 108 + { 109 + struct super_block *sb; 110 + struct kernfs_super_info *info; 111 + int error; 112 + 113 + info = kzalloc(sizeof(*info), GFP_KERNEL); 114 + if (!info) 115 + return ERR_PTR(-ENOMEM); 116 + 117 + info->root = root; 118 + info->ns = ns; 119 + 120 + sb = sget(fs_type, kernfs_test_super, kernfs_set_super, flags, info); 121 + if (IS_ERR(sb) || sb->s_fs_info != info) 122 + kfree(info); 123 + if (IS_ERR(sb)) 124 + return ERR_CAST(sb); 125 + if (!sb->s_root) { 126 + error = kernfs_fill_super(sb); 127 + if (error) { 128 + deactivate_locked_super(sb); 129 + return ERR_PTR(error); 130 + } 131 + sb->s_flags |= MS_ACTIVE; 132 + } 133 + 134 + return dget(sb->s_root); 135 + } 136 + 137 + /** 138 + * kernfs_kill_sb - kill_sb for kernfs 139 + * @sb: super_block being killed 140 + * 141 + * This can be used directly for file_system_type->kill_sb(). If a kernfs 142 + * user needs extra cleanup, it can implement its own kill_sb() and call 143 + * this function at the end. 144 + */ 145 + void kernfs_kill_sb(struct super_block *sb) 146 + { 147 + struct kernfs_super_info *info = kernfs_info(sb); 148 + struct kernfs_node *root_kn = sb->s_root->d_fsdata; 149 + 150 + /* 151 + * Remove the superblock from fs_supers/s_instances 152 + * so we can't find it, before freeing kernfs_super_info. 153 + */ 154 + kill_anon_super(sb); 155 + kfree(info); 156 + kernfs_put(root_kn); 157 + } 158 + 159 + void __init kernfs_init(void) 160 + { 161 + kernfs_node_cache = kmem_cache_create("kernfs_node_cache", 162 + sizeof(struct kernfs_node), 163 + 0, SLAB_PANIC, NULL); 164 + kernfs_inode_init(); 165 + }
+151
fs/kernfs/symlink.c
··· 1 + /* 2 + * fs/kernfs/symlink.c - kernfs symlink implementation 3 + * 4 + * Copyright (c) 2001-3 Patrick Mochel 5 + * Copyright (c) 2007 SUSE Linux Products GmbH 6 + * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org> 7 + * 8 + * This file is released under the GPLv2. 9 + */ 10 + 11 + #include <linux/fs.h> 12 + #include <linux/gfp.h> 13 + #include <linux/namei.h> 14 + 15 + #include "kernfs-internal.h" 16 + 17 + /** 18 + * kernfs_create_link - create a symlink 19 + * @parent: directory to create the symlink in 20 + * @name: name of the symlink 21 + * @target: target node for the symlink to point to 22 + * 23 + * Returns the created node on success, ERR_PTR() value on error. 24 + */ 25 + struct kernfs_node *kernfs_create_link(struct kernfs_node *parent, 26 + const char *name, 27 + struct kernfs_node *target) 28 + { 29 + struct kernfs_node *kn; 30 + struct kernfs_addrm_cxt acxt; 31 + int error; 32 + 33 + kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, KERNFS_LINK); 34 + if (!kn) 35 + return ERR_PTR(-ENOMEM); 36 + 37 + if (kernfs_ns_enabled(parent)) 38 + kn->ns = target->ns; 39 + kn->symlink.target_kn = target; 40 + kernfs_get(target); /* ref owned by symlink */ 41 + 42 + kernfs_addrm_start(&acxt); 43 + error = kernfs_add_one(&acxt, kn); 44 + kernfs_addrm_finish(&acxt); 45 + 46 + if (!error) 47 + return kn; 48 + 49 + kernfs_put(kn); 50 + return ERR_PTR(error); 51 + } 52 + 53 + static int kernfs_get_target_path(struct kernfs_node *parent, 54 + struct kernfs_node *target, char *path) 55 + { 56 + struct kernfs_node *base, *kn; 57 + char *s = path; 58 + int len = 0; 59 + 60 + /* go up to the root, stop at the base */ 61 + base = parent; 62 + while (base->parent) { 63 + kn = target->parent; 64 + while (kn->parent && base != kn) 65 + kn = kn->parent; 66 + 67 + if (base == kn) 68 + break; 69 + 70 + strcpy(s, "../"); 71 + s += 3; 72 + base = base->parent; 73 + } 74 + 75 + /* determine end of target string for reverse fillup */ 76 + kn = target; 77 + while (kn->parent && kn != base) { 78 + len += strlen(kn->name) + 1; 79 + kn = kn->parent; 80 + } 81 + 82 + /* check limits */ 83 + if (len < 2) 84 + return -EINVAL; 85 + len--; 86 + if ((s - path) + len > PATH_MAX) 87 + return -ENAMETOOLONG; 88 + 89 + /* reverse fillup of target string from target to base */ 90 + kn = target; 91 + while (kn->parent && kn != base) { 92 + int slen = strlen(kn->name); 93 + 94 + len -= slen; 95 + strncpy(s + len, kn->name, slen); 96 + if (len) 97 + s[--len] = '/'; 98 + 99 + kn = kn->parent; 100 + } 101 + 102 + return 0; 103 + } 104 + 105 + static int kernfs_getlink(struct dentry *dentry, char *path) 106 + { 107 + struct kernfs_node *kn = dentry->d_fsdata; 108 + struct kernfs_node *parent = kn->parent; 109 + struct kernfs_node *target = kn->symlink.target_kn; 110 + int error; 111 + 112 + mutex_lock(&kernfs_mutex); 113 + error = kernfs_get_target_path(parent, target, path); 114 + mutex_unlock(&kernfs_mutex); 115 + 116 + return error; 117 + } 118 + 119 + static void *kernfs_iop_follow_link(struct dentry *dentry, struct nameidata *nd) 120 + { 121 + int error = -ENOMEM; 122 + unsigned long page = get_zeroed_page(GFP_KERNEL); 123 + if (page) { 124 + error = kernfs_getlink(dentry, (char *) page); 125 + if (error < 0) 126 + free_page((unsigned long)page); 127 + } 128 + nd_set_link(nd, error ? ERR_PTR(error) : (char *)page); 129 + return NULL; 130 + } 131 + 132 + static void kernfs_iop_put_link(struct dentry *dentry, struct nameidata *nd, 133 + void *cookie) 134 + { 135 + char *page = nd_get_link(nd); 136 + if (!IS_ERR(page)) 137 + free_page((unsigned long)page); 138 + } 139 + 140 + const struct inode_operations kernfs_symlink_iops = { 141 + .setxattr = kernfs_iop_setxattr, 142 + .removexattr = kernfs_iop_removexattr, 143 + .getxattr = kernfs_iop_getxattr, 144 + .listxattr = kernfs_iop_listxattr, 145 + .readlink = generic_readlink, 146 + .follow_link = kernfs_iop_follow_link, 147 + .put_link = kernfs_iop_put_link, 148 + .setattr = kernfs_iop_setattr, 149 + .getattr = kernfs_iop_getattr, 150 + .permission = kernfs_iop_permission, 151 + };
+2
fs/namespace.c
··· 2790 2790 for (u = 0; u < HASH_SIZE; u++) 2791 2791 INIT_LIST_HEAD(&mountpoint_hashtable[u]); 2792 2792 2793 + kernfs_init(); 2794 + 2793 2795 err = sysfs_init(); 2794 2796 if (err) 2795 2797 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
+1 -1
fs/sysfs/Makefile
··· 2 2 # Makefile for the sysfs virtual filesystem 3 3 # 4 4 5 - obj-y := inode.o file.o dir.o symlink.o mount.o group.o 5 + obj-y := file.o dir.o symlink.o mount.o group.o
+35 -1040
fs/sysfs/dir.c
··· 13 13 #undef DEBUG 14 14 15 15 #include <linux/fs.h> 16 - #include <linux/mount.h> 17 - #include <linux/module.h> 18 16 #include <linux/kobject.h> 19 - #include <linux/namei.h> 20 - #include <linux/idr.h> 21 - #include <linux/completion.h> 22 - #include <linux/mutex.h> 23 17 #include <linux/slab.h> 24 - #include <linux/security.h> 25 - #include <linux/hash.h> 26 18 #include "sysfs.h" 27 19 28 - DEFINE_MUTEX(sysfs_mutex); 29 20 DEFINE_SPINLOCK(sysfs_symlink_target_lock); 30 - 31 - #define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb) 32 - 33 - static DEFINE_SPINLOCK(sysfs_ino_lock); 34 - static DEFINE_IDA(sysfs_ino_ida); 35 - 36 - /** 37 - * sysfs_name_hash 38 - * @name: Null terminated string to hash 39 - * @ns: Namespace tag to hash 40 - * 41 - * Returns 31 bit hash of ns + name (so it fits in an off_t ) 42 - */ 43 - static unsigned int sysfs_name_hash(const char *name, const void *ns) 44 - { 45 - unsigned long hash = init_name_hash(); 46 - unsigned int len = strlen(name); 47 - while (len--) 48 - hash = partial_name_hash(*name++, hash); 49 - hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31)); 50 - hash &= 0x7fffffffU; 51 - /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */ 52 - if (hash < 1) 53 - hash += 2; 54 - if (hash >= INT_MAX) 55 - hash = INT_MAX - 1; 56 - return hash; 57 - } 58 - 59 - static int sysfs_name_compare(unsigned int hash, const char *name, 60 - const void *ns, const struct sysfs_dirent *sd) 61 - { 62 - if (hash != sd->s_hash) 63 - return hash - sd->s_hash; 64 - if (ns != sd->s_ns) 65 - return ns - sd->s_ns; 66 - return strcmp(name, sd->s_name); 67 - } 68 - 69 - static int sysfs_sd_compare(const struct sysfs_dirent *left, 70 - const struct sysfs_dirent *right) 71 - { 72 - return sysfs_name_compare(left->s_hash, left->s_name, left->s_ns, 73 - right); 74 - } 75 - 76 - /** 77 - * sysfs_link_sibling - link sysfs_dirent into sibling rbtree 78 - * @sd: sysfs_dirent of interest 79 - * 80 - * Link @sd into its sibling rbtree which starts from 81 - * sd->s_parent->s_dir.children. 82 - * 83 - * Locking: 84 - * mutex_lock(sysfs_mutex) 85 - * 86 - * RETURNS: 87 - * 0 on susccess -EEXIST on failure. 88 - */ 89 - static int sysfs_link_sibling(struct sysfs_dirent *sd) 90 - { 91 - struct rb_node **node = &sd->s_parent->s_dir.children.rb_node; 92 - struct rb_node *parent = NULL; 93 - 94 - if (sysfs_type(sd) == SYSFS_DIR) 95 - sd->s_parent->s_dir.subdirs++; 96 - 97 - while (*node) { 98 - struct sysfs_dirent *pos; 99 - int result; 100 - 101 - pos = to_sysfs_dirent(*node); 102 - parent = *node; 103 - result = sysfs_sd_compare(sd, pos); 104 - if (result < 0) 105 - node = &pos->s_rb.rb_left; 106 - else if (result > 0) 107 - node = &pos->s_rb.rb_right; 108 - else 109 - return -EEXIST; 110 - } 111 - /* add new node and rebalance the tree */ 112 - rb_link_node(&sd->s_rb, parent, node); 113 - rb_insert_color(&sd->s_rb, &sd->s_parent->s_dir.children); 114 - return 0; 115 - } 116 - 117 - /** 118 - * sysfs_unlink_sibling - unlink sysfs_dirent from sibling rbtree 119 - * @sd: sysfs_dirent of interest 120 - * 121 - * Unlink @sd from its sibling rbtree which starts from 122 - * sd->s_parent->s_dir.children. 123 - * 124 - * Locking: 125 - * mutex_lock(sysfs_mutex) 126 - */ 127 - static void sysfs_unlink_sibling(struct sysfs_dirent *sd) 128 - { 129 - if (sysfs_type(sd) == SYSFS_DIR) 130 - sd->s_parent->s_dir.subdirs--; 131 - 132 - rb_erase(&sd->s_rb, &sd->s_parent->s_dir.children); 133 - } 134 - 135 - /** 136 - * sysfs_get_active - get an active reference to sysfs_dirent 137 - * @sd: sysfs_dirent to get an active reference to 138 - * 139 - * Get an active reference of @sd. This function is noop if @sd 140 - * is NULL. 141 - * 142 - * RETURNS: 143 - * Pointer to @sd on success, NULL on failure. 144 - */ 145 - struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd) 146 - { 147 - if (unlikely(!sd)) 148 - return NULL; 149 - 150 - if (!atomic_inc_unless_negative(&sd->s_active)) 151 - return NULL; 152 - 153 - if (likely(!sysfs_ignore_lockdep(sd))) 154 - rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_); 155 - return sd; 156 - } 157 - 158 - /** 159 - * sysfs_put_active - put an active reference to sysfs_dirent 160 - * @sd: sysfs_dirent to put an active reference to 161 - * 162 - * Put an active reference to @sd. This function is noop if @sd 163 - * is NULL. 164 - */ 165 - void sysfs_put_active(struct sysfs_dirent *sd) 166 - { 167 - int v; 168 - 169 - if (unlikely(!sd)) 170 - return; 171 - 172 - if (likely(!sysfs_ignore_lockdep(sd))) 173 - rwsem_release(&sd->dep_map, 1, _RET_IP_); 174 - v = atomic_dec_return(&sd->s_active); 175 - if (likely(v != SD_DEACTIVATED_BIAS)) 176 - return; 177 - 178 - /* atomic_dec_return() is a mb(), we'll always see the updated 179 - * sd->u.completion. 180 - */ 181 - complete(sd->u.completion); 182 - } 183 - 184 - /** 185 - * sysfs_deactivate - deactivate sysfs_dirent 186 - * @sd: sysfs_dirent to deactivate 187 - * 188 - * Deny new active references and drain existing ones. 189 - */ 190 - static void sysfs_deactivate(struct sysfs_dirent *sd) 191 - { 192 - DECLARE_COMPLETION_ONSTACK(wait); 193 - int v; 194 - 195 - BUG_ON(!(sd->s_flags & SYSFS_FLAG_REMOVED)); 196 - 197 - if (!(sysfs_type(sd) & SYSFS_ACTIVE_REF)) 198 - return; 199 - 200 - sd->u.completion = (void *)&wait; 201 - 202 - rwsem_acquire(&sd->dep_map, 0, 0, _RET_IP_); 203 - /* atomic_add_return() is a mb(), put_active() will always see 204 - * the updated sd->u.completion. 205 - */ 206 - v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active); 207 - 208 - if (v != SD_DEACTIVATED_BIAS) { 209 - lock_contended(&sd->dep_map, _RET_IP_); 210 - wait_for_completion(&wait); 211 - } 212 - 213 - lock_acquired(&sd->dep_map, _RET_IP_); 214 - rwsem_release(&sd->dep_map, 1, _RET_IP_); 215 - } 216 - 217 - static int sysfs_alloc_ino(unsigned int *pino) 218 - { 219 - int ino, rc; 220 - 221 - retry: 222 - spin_lock(&sysfs_ino_lock); 223 - rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino); 224 - spin_unlock(&sysfs_ino_lock); 225 - 226 - if (rc == -EAGAIN) { 227 - if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL)) 228 - goto retry; 229 - rc = -ENOMEM; 230 - } 231 - 232 - *pino = ino; 233 - return rc; 234 - } 235 - 236 - static void sysfs_free_ino(unsigned int ino) 237 - { 238 - spin_lock(&sysfs_ino_lock); 239 - ida_remove(&sysfs_ino_ida, ino); 240 - spin_unlock(&sysfs_ino_lock); 241 - } 242 - 243 - void release_sysfs_dirent(struct sysfs_dirent *sd) 244 - { 245 - struct sysfs_dirent *parent_sd; 246 - 247 - repeat: 248 - /* Moving/renaming is always done while holding reference. 249 - * sd->s_parent won't change beneath us. 250 - */ 251 - parent_sd = sd->s_parent; 252 - 253 - WARN(!(sd->s_flags & SYSFS_FLAG_REMOVED), 254 - "sysfs: free using entry: %s/%s\n", 255 - parent_sd ? parent_sd->s_name : "", sd->s_name); 256 - 257 - if (sysfs_type(sd) == SYSFS_KOBJ_LINK) 258 - sysfs_put(sd->s_symlink.target_sd); 259 - if (sysfs_type(sd) & SYSFS_COPY_NAME) 260 - kfree(sd->s_name); 261 - if (sd->s_iattr && sd->s_iattr->ia_secdata) 262 - security_release_secctx(sd->s_iattr->ia_secdata, 263 - sd->s_iattr->ia_secdata_len); 264 - kfree(sd->s_iattr); 265 - sysfs_free_ino(sd->s_ino); 266 - kmem_cache_free(sysfs_dir_cachep, sd); 267 - 268 - sd = parent_sd; 269 - if (sd && atomic_dec_and_test(&sd->s_count)) 270 - goto repeat; 271 - } 272 - 273 - static int sysfs_dentry_delete(const struct dentry *dentry) 274 - { 275 - struct sysfs_dirent *sd = dentry->d_fsdata; 276 - return !(sd && !(sd->s_flags & SYSFS_FLAG_REMOVED)); 277 - } 278 - 279 - static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags) 280 - { 281 - struct sysfs_dirent *sd; 282 - int type; 283 - 284 - if (flags & LOOKUP_RCU) 285 - return -ECHILD; 286 - 287 - sd = dentry->d_fsdata; 288 - mutex_lock(&sysfs_mutex); 289 - 290 - /* The sysfs dirent has been deleted */ 291 - if (sd->s_flags & SYSFS_FLAG_REMOVED) 292 - goto out_bad; 293 - 294 - /* The sysfs dirent has been moved? */ 295 - if (dentry->d_parent->d_fsdata != sd->s_parent) 296 - goto out_bad; 297 - 298 - /* The sysfs dirent has been renamed */ 299 - if (strcmp(dentry->d_name.name, sd->s_name) != 0) 300 - goto out_bad; 301 - 302 - /* The sysfs dirent has been moved to a different namespace */ 303 - type = KOBJ_NS_TYPE_NONE; 304 - if (sd->s_parent) { 305 - type = sysfs_ns_type(sd->s_parent); 306 - if (type != KOBJ_NS_TYPE_NONE && 307 - sysfs_info(dentry->d_sb)->ns[type] != sd->s_ns) 308 - goto out_bad; 309 - } 310 - 311 - mutex_unlock(&sysfs_mutex); 312 - out_valid: 313 - return 1; 314 - out_bad: 315 - /* Remove the dentry from the dcache hashes. 316 - * If this is a deleted dentry we use d_drop instead of d_delete 317 - * so sysfs doesn't need to cope with negative dentries. 318 - * 319 - * If this is a dentry that has simply been renamed we 320 - * use d_drop to remove it from the dcache lookup on its 321 - * old parent. If this dentry persists later when a lookup 322 - * is performed at its new name the dentry will be readded 323 - * to the dcache hashes. 324 - */ 325 - mutex_unlock(&sysfs_mutex); 326 - 327 - /* If we have submounts we must allow the vfs caches 328 - * to lie about the state of the filesystem to prevent 329 - * leaks and other nasty things. 330 - */ 331 - if (check_submounts_and_drop(dentry) != 0) 332 - goto out_valid; 333 - 334 - return 0; 335 - } 336 - 337 - static void sysfs_dentry_release(struct dentry *dentry) 338 - { 339 - sysfs_put(dentry->d_fsdata); 340 - } 341 - 342 - const struct dentry_operations sysfs_dentry_ops = { 343 - .d_revalidate = sysfs_dentry_revalidate, 344 - .d_delete = sysfs_dentry_delete, 345 - .d_release = sysfs_dentry_release, 346 - }; 347 - 348 - struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type) 349 - { 350 - char *dup_name = NULL; 351 - struct sysfs_dirent *sd; 352 - 353 - if (type & SYSFS_COPY_NAME) { 354 - name = dup_name = kstrdup(name, GFP_KERNEL); 355 - if (!name) 356 - return NULL; 357 - } 358 - 359 - sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); 360 - if (!sd) 361 - goto err_out1; 362 - 363 - if (sysfs_alloc_ino(&sd->s_ino)) 364 - goto err_out2; 365 - 366 - atomic_set(&sd->s_count, 1); 367 - atomic_set(&sd->s_active, 0); 368 - 369 - sd->s_name = name; 370 - sd->s_mode = mode; 371 - sd->s_flags = type | SYSFS_FLAG_REMOVED; 372 - 373 - return sd; 374 - 375 - err_out2: 376 - kmem_cache_free(sysfs_dir_cachep, sd); 377 - err_out1: 378 - kfree(dup_name); 379 - return NULL; 380 - } 381 - 382 - /** 383 - * sysfs_addrm_start - prepare for sysfs_dirent add/remove 384 - * @acxt: pointer to sysfs_addrm_cxt to be used 385 - * 386 - * This function is called when the caller is about to add or remove 387 - * sysfs_dirent. This function acquires sysfs_mutex. @acxt is used 388 - * to keep and pass context to other addrm functions. 389 - * 390 - * LOCKING: 391 - * Kernel thread context (may sleep). sysfs_mutex is locked on 392 - * return. 393 - */ 394 - void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt) 395 - __acquires(sysfs_mutex) 396 - { 397 - memset(acxt, 0, sizeof(*acxt)); 398 - 399 - mutex_lock(&sysfs_mutex); 400 - } 401 - 402 - /** 403 - * __sysfs_add_one - add sysfs_dirent to parent without warning 404 - * @acxt: addrm context to use 405 - * @sd: sysfs_dirent to be added 406 - * @parent_sd: the parent sysfs_dirent to add @sd to 407 - * 408 - * Get @parent_sd and set @sd->s_parent to it and increment nlink of 409 - * the parent inode if @sd is a directory and link into the children 410 - * list of the parent. 411 - * 412 - * This function should be called between calls to 413 - * sysfs_addrm_start() and sysfs_addrm_finish() and should be 414 - * passed the same @acxt as passed to sysfs_addrm_start(). 415 - * 416 - * LOCKING: 417 - * Determined by sysfs_addrm_start(). 418 - * 419 - * RETURNS: 420 - * 0 on success, -EEXIST if entry with the given name already 421 - * exists. 422 - */ 423 - int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd, 424 - struct sysfs_dirent *parent_sd) 425 - { 426 - struct sysfs_inode_attrs *ps_iattr; 427 - int ret; 428 - 429 - if (!!sysfs_ns_type(parent_sd) != !!sd->s_ns) { 430 - WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n", 431 - sysfs_ns_type(parent_sd) ? "required" : "invalid", 432 - parent_sd->s_name, sd->s_name); 433 - return -EINVAL; 434 - } 435 - 436 - sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns); 437 - sd->s_parent = sysfs_get(parent_sd); 438 - 439 - ret = sysfs_link_sibling(sd); 440 - if (ret) 441 - return ret; 442 - 443 - /* Update timestamps on the parent */ 444 - ps_iattr = parent_sd->s_iattr; 445 - if (ps_iattr) { 446 - struct iattr *ps_iattrs = &ps_iattr->ia_iattr; 447 - ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME; 448 - } 449 - 450 - /* Mark the entry added into directory tree */ 451 - sd->s_flags &= ~SYSFS_FLAG_REMOVED; 452 - 453 - return 0; 454 - } 455 21 456 22 /** 457 23 * sysfs_pathname - return full path to sysfs dirent 458 - * @sd: sysfs_dirent whose path we want 24 + * @kn: kernfs_node whose path we want 459 25 * @path: caller allocated buffer of size PATH_MAX 460 26 * 461 27 * Gives the name "/" to the sysfs_root entry; any path returned 462 28 * is relative to wherever sysfs is mounted. 463 29 */ 464 - static char *sysfs_pathname(struct sysfs_dirent *sd, char *path) 30 + static char *sysfs_pathname(struct kernfs_node *kn, char *path) 465 31 { 466 - if (sd->s_parent) { 467 - sysfs_pathname(sd->s_parent, path); 32 + if (kn->parent) { 33 + sysfs_pathname(kn->parent, path); 468 34 strlcat(path, "/", PATH_MAX); 469 35 } 470 - strlcat(path, sd->s_name, PATH_MAX); 36 + strlcat(path, kn->name, PATH_MAX); 471 37 return path; 472 38 } 473 39 474 - void sysfs_warn_dup(struct sysfs_dirent *parent, const char *name) 40 + void sysfs_warn_dup(struct kernfs_node *parent, const char *name) 475 41 { 476 42 char *path; 477 43 ··· 55 489 } 56 490 57 491 /** 58 - * sysfs_add_one - add sysfs_dirent to parent 59 - * @acxt: addrm context to use 60 - * @sd: sysfs_dirent to be added 61 - * @parent_sd: the parent sysfs_dirent to add @sd to 62 - * 63 - * Get @parent_sd and set @sd->s_parent to it and increment nlink of 64 - * the parent inode if @sd is a directory and link into the children 65 - * list of the parent. 66 - * 67 - * This function should be called between calls to 68 - * sysfs_addrm_start() and sysfs_addrm_finish() and should be 69 - * passed the same @acxt as passed to sysfs_addrm_start(). 70 - * 71 - * LOCKING: 72 - * Determined by sysfs_addrm_start(). 73 - * 74 - * RETURNS: 75 - * 0 on success, -EEXIST if entry with the given name already 76 - * exists. 77 - */ 78 - int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd, 79 - struct sysfs_dirent *parent_sd) 80 - { 81 - int ret; 82 - 83 - ret = __sysfs_add_one(acxt, sd, parent_sd); 84 - 85 - if (ret == -EEXIST) 86 - sysfs_warn_dup(parent_sd, sd->s_name); 87 - return ret; 88 - } 89 - 90 - /** 91 - * sysfs_remove_one - remove sysfs_dirent from parent 92 - * @acxt: addrm context to use 93 - * @sd: sysfs_dirent to be removed 94 - * 95 - * Mark @sd removed and drop nlink of parent inode if @sd is a 96 - * directory. @sd is unlinked from the children list. 97 - * 98 - * This function should be called between calls to 99 - * sysfs_addrm_start() and sysfs_addrm_finish() and should be 100 - * passed the same @acxt as passed to sysfs_addrm_start(). 101 - * 102 - * LOCKING: 103 - * Determined by sysfs_addrm_start(). 104 - */ 105 - static void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, 106 - struct sysfs_dirent *sd) 107 - { 108 - struct sysfs_inode_attrs *ps_iattr; 109 - 110 - /* 111 - * Removal can be called multiple times on the same node. Only the 112 - * first invocation is effective and puts the base ref. 113 - */ 114 - if (sd->s_flags & SYSFS_FLAG_REMOVED) 115 - return; 116 - 117 - sysfs_unlink_sibling(sd); 118 - 119 - /* Update timestamps on the parent */ 120 - ps_iattr = sd->s_parent->s_iattr; 121 - if (ps_iattr) { 122 - struct iattr *ps_iattrs = &ps_iattr->ia_iattr; 123 - ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME; 124 - } 125 - 126 - sd->s_flags |= SYSFS_FLAG_REMOVED; 127 - sd->u.removed_list = acxt->removed; 128 - acxt->removed = sd; 129 - } 130 - 131 - /** 132 - * sysfs_addrm_finish - finish up sysfs_dirent add/remove 133 - * @acxt: addrm context to finish up 134 - * 135 - * Finish up sysfs_dirent add/remove. Resources acquired by 136 - * sysfs_addrm_start() are released and removed sysfs_dirents are 137 - * cleaned up. 138 - * 139 - * LOCKING: 140 - * sysfs_mutex is released. 141 - */ 142 - void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt) 143 - __releases(sysfs_mutex) 144 - { 145 - /* release resources acquired by sysfs_addrm_start() */ 146 - mutex_unlock(&sysfs_mutex); 147 - 148 - /* kill removed sysfs_dirents */ 149 - while (acxt->removed) { 150 - struct sysfs_dirent *sd = acxt->removed; 151 - 152 - acxt->removed = sd->u.removed_list; 153 - 154 - sysfs_deactivate(sd); 155 - sysfs_unmap_bin_file(sd); 156 - sysfs_put(sd); 157 - } 158 - } 159 - 160 - /** 161 - * sysfs_find_dirent - find sysfs_dirent with the given name 162 - * @parent_sd: sysfs_dirent to search under 163 - * @name: name to look for 164 - * @ns: the namespace tag to use 165 - * 166 - * Look for sysfs_dirent with name @name under @parent_sd. 167 - * 168 - * LOCKING: 169 - * mutex_lock(sysfs_mutex) 170 - * 171 - * RETURNS: 172 - * Pointer to sysfs_dirent if found, NULL if not. 173 - */ 174 - struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd, 175 - const unsigned char *name, 176 - const void *ns) 177 - { 178 - struct rb_node *node = parent_sd->s_dir.children.rb_node; 179 - unsigned int hash; 180 - 181 - if (!!sysfs_ns_type(parent_sd) != !!ns) { 182 - WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n", 183 - sysfs_ns_type(parent_sd) ? "required" : "invalid", 184 - parent_sd->s_name, name); 185 - return NULL; 186 - } 187 - 188 - hash = sysfs_name_hash(name, ns); 189 - while (node) { 190 - struct sysfs_dirent *sd; 191 - int result; 192 - 193 - sd = to_sysfs_dirent(node); 194 - result = sysfs_name_compare(hash, name, ns, sd); 195 - if (result < 0) 196 - node = node->rb_left; 197 - else if (result > 0) 198 - node = node->rb_right; 199 - else 200 - return sd; 201 - } 202 - return NULL; 203 - } 204 - 205 - /** 206 - * sysfs_get_dirent_ns - find and get sysfs_dirent with the given name 207 - * @parent_sd: sysfs_dirent to search under 208 - * @name: name to look for 209 - * @ns: the namespace tag to use 210 - * 211 - * Look for sysfs_dirent with name @name under @parent_sd and get 212 - * it if found. 213 - * 214 - * LOCKING: 215 - * Kernel thread context (may sleep). Grabs sysfs_mutex. 216 - * 217 - * RETURNS: 218 - * Pointer to sysfs_dirent if found, NULL if not. 219 - */ 220 - struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd, 221 - const unsigned char *name, 222 - const void *ns) 223 - { 224 - struct sysfs_dirent *sd; 225 - 226 - mutex_lock(&sysfs_mutex); 227 - sd = sysfs_find_dirent(parent_sd, name, ns); 228 - sysfs_get(sd); 229 - mutex_unlock(&sysfs_mutex); 230 - 231 - return sd; 232 - } 233 - EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns); 234 - 235 - static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, 236 - enum kobj_ns_type type, 237 - const char *name, const void *ns, 238 - struct sysfs_dirent **p_sd) 239 - { 240 - umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 241 - struct sysfs_addrm_cxt acxt; 242 - struct sysfs_dirent *sd; 243 - int rc; 244 - 245 - /* allocate */ 246 - sd = sysfs_new_dirent(name, mode, SYSFS_DIR); 247 - if (!sd) 248 - return -ENOMEM; 249 - 250 - sd->s_flags |= (type << SYSFS_NS_TYPE_SHIFT); 251 - sd->s_ns = ns; 252 - sd->s_dir.kobj = kobj; 253 - 254 - /* link in */ 255 - sysfs_addrm_start(&acxt); 256 - rc = sysfs_add_one(&acxt, sd, parent_sd); 257 - sysfs_addrm_finish(&acxt); 258 - 259 - if (rc == 0) 260 - *p_sd = sd; 261 - else 262 - sysfs_put(sd); 263 - 264 - return rc; 265 - } 266 - 267 - int sysfs_create_subdir(struct kobject *kobj, const char *name, 268 - struct sysfs_dirent **p_sd) 269 - { 270 - return create_dir(kobj, kobj->sd, 271 - KOBJ_NS_TYPE_NONE, name, NULL, p_sd); 272 - } 273 - 274 - /** 275 - * sysfs_read_ns_type: return associated ns_type 276 - * @kobj: the kobject being queried 277 - * 278 - * Each kobject can be tagged with exactly one namespace type 279 - * (i.e. network or user). Return the ns_type associated with 280 - * this object if any 281 - */ 282 - static enum kobj_ns_type sysfs_read_ns_type(struct kobject *kobj) 283 - { 284 - const struct kobj_ns_type_operations *ops; 285 - enum kobj_ns_type type; 286 - 287 - ops = kobj_child_ns_ops(kobj); 288 - if (!ops) 289 - return KOBJ_NS_TYPE_NONE; 290 - 291 - type = ops->type; 292 - BUG_ON(type <= KOBJ_NS_TYPE_NONE); 293 - BUG_ON(type >= KOBJ_NS_TYPES); 294 - BUG_ON(!kobj_ns_type_registered(type)); 295 - 296 - return type; 297 - } 298 - 299 - /** 300 492 * sysfs_create_dir_ns - create a directory for an object with a namespace tag 301 493 * @kobj: object we're creating directory for 302 494 * @ns: the namespace tag to use 303 495 */ 304 496 int sysfs_create_dir_ns(struct kobject *kobj, const void *ns) 305 497 { 306 - enum kobj_ns_type type; 307 - struct sysfs_dirent *parent_sd, *sd; 308 - int error = 0; 498 + struct kernfs_node *parent, *kn; 309 499 310 500 BUG_ON(!kobj); 311 501 312 502 if (kobj->parent) 313 - parent_sd = kobj->parent->sd; 503 + parent = kobj->parent->sd; 314 504 else 315 - parent_sd = &sysfs_root; 505 + parent = sysfs_root_kn; 316 506 317 - if (!parent_sd) 507 + if (!parent) 318 508 return -ENOENT; 319 509 320 - type = sysfs_read_ns_type(kobj); 321 - 322 - error = create_dir(kobj, parent_sd, type, kobject_name(kobj), ns, &sd); 323 - if (!error) 324 - kobj->sd = sd; 325 - return error; 326 - } 327 - 328 - static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry, 329 - unsigned int flags) 330 - { 331 - struct dentry *ret = NULL; 332 - struct dentry *parent = dentry->d_parent; 333 - struct sysfs_dirent *parent_sd = parent->d_fsdata; 334 - struct sysfs_dirent *sd; 335 - struct inode *inode; 336 - enum kobj_ns_type type; 337 - const void *ns; 338 - 339 - mutex_lock(&sysfs_mutex); 340 - 341 - type = sysfs_ns_type(parent_sd); 342 - ns = sysfs_info(dir->i_sb)->ns[type]; 343 - 344 - sd = sysfs_find_dirent(parent_sd, dentry->d_name.name, ns); 345 - 346 - /* no such entry */ 347 - if (!sd) { 348 - ret = ERR_PTR(-ENOENT); 349 - goto out_unlock; 350 - } 351 - dentry->d_fsdata = sysfs_get(sd); 352 - 353 - /* attach dentry and inode */ 354 - inode = sysfs_get_inode(dir->i_sb, sd); 355 - if (!inode) { 356 - ret = ERR_PTR(-ENOMEM); 357 - goto out_unlock; 510 + kn = kernfs_create_dir_ns(parent, kobject_name(kobj), 511 + S_IRWXU | S_IRUGO | S_IXUGO, kobj, ns); 512 + if (IS_ERR(kn)) { 513 + if (PTR_ERR(kn) == -EEXIST) 514 + sysfs_warn_dup(parent, kobject_name(kobj)); 515 + return PTR_ERR(kn); 358 516 } 359 517 360 - /* instantiate and hash dentry */ 361 - ret = d_materialise_unique(dentry, inode); 362 - out_unlock: 363 - mutex_unlock(&sysfs_mutex); 364 - return ret; 365 - } 366 - 367 - const struct inode_operations sysfs_dir_inode_operations = { 368 - .lookup = sysfs_lookup, 369 - .permission = sysfs_permission, 370 - .setattr = sysfs_setattr, 371 - .getattr = sysfs_getattr, 372 - .setxattr = sysfs_setxattr, 373 - }; 374 - 375 - static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos) 376 - { 377 - struct sysfs_dirent *last; 378 - 379 - while (true) { 380 - struct rb_node *rbn; 381 - 382 - last = pos; 383 - 384 - if (sysfs_type(pos) != SYSFS_DIR) 385 - break; 386 - 387 - rbn = rb_first(&pos->s_dir.children); 388 - if (!rbn) 389 - break; 390 - 391 - pos = to_sysfs_dirent(rbn); 392 - } 393 - 394 - return last; 395 - } 396 - 397 - /** 398 - * sysfs_next_descendant_post - find the next descendant for post-order walk 399 - * @pos: the current position (%NULL to initiate traversal) 400 - * @root: sysfs_dirent whose descendants to walk 401 - * 402 - * Find the next descendant to visit for post-order traversal of @root's 403 - * descendants. @root is included in the iteration and the last node to be 404 - * visited. 405 - */ 406 - static struct sysfs_dirent *sysfs_next_descendant_post(struct sysfs_dirent *pos, 407 - struct sysfs_dirent *root) 408 - { 409 - struct rb_node *rbn; 410 - 411 - lockdep_assert_held(&sysfs_mutex); 412 - 413 - /* if first iteration, visit leftmost descendant which may be root */ 414 - if (!pos) 415 - return sysfs_leftmost_descendant(root); 416 - 417 - /* if we visited @root, we're done */ 418 - if (pos == root) 419 - return NULL; 420 - 421 - /* if there's an unvisited sibling, visit its leftmost descendant */ 422 - rbn = rb_next(&pos->s_rb); 423 - if (rbn) 424 - return sysfs_leftmost_descendant(to_sysfs_dirent(rbn)); 425 - 426 - /* no sibling left, visit parent */ 427 - return pos->s_parent; 428 - } 429 - 430 - static void __sysfs_remove(struct sysfs_addrm_cxt *acxt, 431 - struct sysfs_dirent *sd) 432 - { 433 - struct sysfs_dirent *pos, *next; 434 - 435 - if (!sd) 436 - return; 437 - 438 - pr_debug("sysfs %s: removing\n", sd->s_name); 439 - 440 - next = NULL; 441 - do { 442 - pos = next; 443 - next = sysfs_next_descendant_post(pos, sd); 444 - if (pos) 445 - sysfs_remove_one(acxt, pos); 446 - } while (next); 447 - } 448 - 449 - /** 450 - * sysfs_remove - remove a sysfs_dirent recursively 451 - * @sd: the sysfs_dirent to remove 452 - * 453 - * Remove @sd along with all its subdirectories and files. 454 - */ 455 - void sysfs_remove(struct sysfs_dirent *sd) 456 - { 457 - struct sysfs_addrm_cxt acxt; 458 - 459 - sysfs_addrm_start(&acxt); 460 - __sysfs_remove(&acxt, sd); 461 - sysfs_addrm_finish(&acxt); 462 - } 463 - 464 - /** 465 - * sysfs_hash_and_remove - find a sysfs_dirent by name and remove it 466 - * @dir_sd: parent of the target 467 - * @name: name of the sysfs_dirent to remove 468 - * @ns: namespace tag of the sysfs_dirent to remove 469 - * 470 - * Look for the sysfs_dirent with @name and @ns under @dir_sd and remove 471 - * it. Returns 0 on success, -ENOENT if such entry doesn't exist. 472 - */ 473 - int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name, 474 - const void *ns) 475 - { 476 - struct sysfs_addrm_cxt acxt; 477 - struct sysfs_dirent *sd; 478 - 479 - if (!dir_sd) { 480 - WARN(1, KERN_WARNING "sysfs: can not remove '%s', no directory\n", 481 - name); 482 - return -ENOENT; 483 - } 484 - 485 - sysfs_addrm_start(&acxt); 486 - 487 - sd = sysfs_find_dirent(dir_sd, name, ns); 488 - if (sd) 489 - __sysfs_remove(&acxt, sd); 490 - 491 - sysfs_addrm_finish(&acxt); 492 - 493 - if (sd) 494 - return 0; 495 - else 496 - return -ENOENT; 518 + kobj->sd = kn; 519 + return 0; 497 520 } 498 521 499 522 /** ··· 95 940 */ 96 941 void sysfs_remove_dir(struct kobject *kobj) 97 942 { 98 - struct sysfs_dirent *sd = kobj->sd; 943 + struct kernfs_node *kn = kobj->sd; 99 944 100 945 /* 101 946 * In general, kboject owner is responsible for ensuring removal 102 947 * doesn't race with other operations and sysfs doesn't provide any 103 948 * protection; however, when @kobj is used as a symlink target, the 104 949 * symlinking entity usually doesn't own @kobj and thus has no 105 - * control over removal. @kobj->sd may be removed anytime and 106 - * symlink code may end up dereferencing an already freed sd. 950 + * control over removal. @kobj->sd may be removed anytime 951 + * and symlink code may end up dereferencing an already freed node. 107 952 * 108 - * sysfs_symlink_target_lock synchronizes @kobj->sd disassociation 109 - * against symlink operations so that symlink code can safely 110 - * dereference @kobj->sd. 953 + * sysfs_symlink_target_lock synchronizes @kobj->sd 954 + * disassociation against symlink operations so that symlink code 955 + * can safely dereference @kobj->sd. 111 956 */ 112 957 spin_lock(&sysfs_symlink_target_lock); 113 958 kobj->sd = NULL; 114 959 spin_unlock(&sysfs_symlink_target_lock); 115 960 116 - if (sd) { 117 - WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR); 118 - sysfs_remove(sd); 961 + if (kn) { 962 + WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR); 963 + kernfs_remove(kn); 119 964 } 120 - } 121 - 122 - int sysfs_rename(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent_sd, 123 - const char *new_name, const void *new_ns) 124 - { 125 - int error; 126 - 127 - mutex_lock(&sysfs_mutex); 128 - 129 - error = 0; 130 - if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) && 131 - (strcmp(sd->s_name, new_name) == 0)) 132 - goto out; /* nothing to rename */ 133 - 134 - error = -EEXIST; 135 - if (sysfs_find_dirent(new_parent_sd, new_name, new_ns)) 136 - goto out; 137 - 138 - /* rename sysfs_dirent */ 139 - if (strcmp(sd->s_name, new_name) != 0) { 140 - error = -ENOMEM; 141 - new_name = kstrdup(new_name, GFP_KERNEL); 142 - if (!new_name) 143 - goto out; 144 - 145 - kfree(sd->s_name); 146 - sd->s_name = new_name; 147 - } 148 - 149 - /* 150 - * Move to the appropriate place in the appropriate directories rbtree. 151 - */ 152 - sysfs_unlink_sibling(sd); 153 - sysfs_get(new_parent_sd); 154 - sysfs_put(sd->s_parent); 155 - sd->s_ns = new_ns; 156 - sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns); 157 - sd->s_parent = new_parent_sd; 158 - sysfs_link_sibling(sd); 159 - 160 - error = 0; 161 - out: 162 - mutex_unlock(&sysfs_mutex); 163 - return error; 164 965 } 165 966 166 967 int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name, 167 968 const void *new_ns) 168 969 { 169 - struct sysfs_dirent *parent_sd = kobj->sd->s_parent; 970 + struct kernfs_node *parent = kobj->sd->parent; 170 971 171 - return sysfs_rename(kobj->sd, parent_sd, new_name, new_ns); 972 + return kernfs_rename_ns(kobj->sd, parent, new_name, new_ns); 172 973 } 173 974 174 975 int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj, 175 976 const void *new_ns) 176 977 { 177 - struct sysfs_dirent *sd = kobj->sd; 178 - struct sysfs_dirent *new_parent_sd; 978 + struct kernfs_node *kn = kobj->sd; 979 + struct kernfs_node *new_parent; 179 980 180 - BUG_ON(!sd->s_parent); 181 - new_parent_sd = new_parent_kobj && new_parent_kobj->sd ? 182 - new_parent_kobj->sd : &sysfs_root; 981 + BUG_ON(!kn->parent); 982 + new_parent = new_parent_kobj && new_parent_kobj->sd ? 983 + new_parent_kobj->sd : sysfs_root_kn; 183 984 184 - return sysfs_rename(sd, new_parent_sd, sd->s_name, new_ns); 985 + return kernfs_rename_ns(kn, new_parent, kn->name, new_ns); 185 986 } 186 - 187 - /* Relationship between s_mode and the DT_xxx types */ 188 - static inline unsigned char dt_type(struct sysfs_dirent *sd) 189 - { 190 - return (sd->s_mode >> 12) & 15; 191 - } 192 - 193 - static int sysfs_dir_release(struct inode *inode, struct file *filp) 194 - { 195 - sysfs_put(filp->private_data); 196 - return 0; 197 - } 198 - 199 - static struct sysfs_dirent *sysfs_dir_pos(const void *ns, 200 - struct sysfs_dirent *parent_sd, loff_t hash, struct sysfs_dirent *pos) 201 - { 202 - if (pos) { 203 - int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) && 204 - pos->s_parent == parent_sd && 205 - hash == pos->s_hash; 206 - sysfs_put(pos); 207 - if (!valid) 208 - pos = NULL; 209 - } 210 - if (!pos && (hash > 1) && (hash < INT_MAX)) { 211 - struct rb_node *node = parent_sd->s_dir.children.rb_node; 212 - while (node) { 213 - pos = to_sysfs_dirent(node); 214 - 215 - if (hash < pos->s_hash) 216 - node = node->rb_left; 217 - else if (hash > pos->s_hash) 218 - node = node->rb_right; 219 - else 220 - break; 221 - } 222 - } 223 - /* Skip over entries in the wrong namespace */ 224 - while (pos && pos->s_ns != ns) { 225 - struct rb_node *node = rb_next(&pos->s_rb); 226 - if (!node) 227 - pos = NULL; 228 - else 229 - pos = to_sysfs_dirent(node); 230 - } 231 - return pos; 232 - } 233 - 234 - static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns, 235 - struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos) 236 - { 237 - pos = sysfs_dir_pos(ns, parent_sd, ino, pos); 238 - if (pos) 239 - do { 240 - struct rb_node *node = rb_next(&pos->s_rb); 241 - if (!node) 242 - pos = NULL; 243 - else 244 - pos = to_sysfs_dirent(node); 245 - } while (pos && pos->s_ns != ns); 246 - return pos; 247 - } 248 - 249 - static int sysfs_readdir(struct file *file, struct dir_context *ctx) 250 - { 251 - struct dentry *dentry = file->f_path.dentry; 252 - struct sysfs_dirent *parent_sd = dentry->d_fsdata; 253 - struct sysfs_dirent *pos = file->private_data; 254 - enum kobj_ns_type type; 255 - const void *ns; 256 - 257 - type = sysfs_ns_type(parent_sd); 258 - ns = sysfs_info(dentry->d_sb)->ns[type]; 259 - 260 - if (!dir_emit_dots(file, ctx)) 261 - return 0; 262 - mutex_lock(&sysfs_mutex); 263 - for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos); 264 - pos; 265 - pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) { 266 - const char *name = pos->s_name; 267 - unsigned int type = dt_type(pos); 268 - int len = strlen(name); 269 - ino_t ino = pos->s_ino; 270 - ctx->pos = pos->s_hash; 271 - file->private_data = sysfs_get(pos); 272 - 273 - mutex_unlock(&sysfs_mutex); 274 - if (!dir_emit(ctx, name, len, ino, type)) 275 - return 0; 276 - mutex_lock(&sysfs_mutex); 277 - } 278 - mutex_unlock(&sysfs_mutex); 279 - file->private_data = NULL; 280 - ctx->pos = INT_MAX; 281 - return 0; 282 - } 283 - 284 - static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence) 285 - { 286 - struct inode *inode = file_inode(file); 287 - loff_t ret; 288 - 289 - mutex_lock(&inode->i_mutex); 290 - ret = generic_file_llseek(file, offset, whence); 291 - mutex_unlock(&inode->i_mutex); 292 - 293 - return ret; 294 - } 295 - 296 - const struct file_operations sysfs_dir_operations = { 297 - .read = generic_read_dir, 298 - .iterate = sysfs_readdir, 299 - .release = sysfs_dir_release, 300 - .llseek = sysfs_dir_llseek, 301 - };
+187 -784
fs/sysfs/file.c
··· 14 14 #include <linux/kobject.h> 15 15 #include <linux/kallsyms.h> 16 16 #include <linux/slab.h> 17 - #include <linux/fsnotify.h> 18 - #include <linux/namei.h> 19 - #include <linux/poll.h> 20 17 #include <linux/list.h> 21 18 #include <linux/mutex.h> 22 - #include <linux/limits.h> 23 - #include <linux/uaccess.h> 24 19 #include <linux/seq_file.h> 25 - #include <linux/mm.h> 26 20 27 21 #include "sysfs.h" 22 + #include "../kernfs/kernfs-internal.h" 28 23 29 24 /* 30 - * There's one sysfs_open_file for each open file and one sysfs_open_dirent 31 - * for each sysfs_dirent with one or more open files. 32 - * 33 - * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is 34 - * protected by sysfs_open_dirent_lock. 35 - * 36 - * filp->private_data points to seq_file whose ->private points to 37 - * sysfs_open_file. sysfs_open_files are chained at 38 - * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex. 39 - */ 40 - static DEFINE_SPINLOCK(sysfs_open_dirent_lock); 41 - static DEFINE_MUTEX(sysfs_open_file_mutex); 42 - 43 - struct sysfs_open_dirent { 44 - atomic_t refcnt; 45 - atomic_t event; 46 - wait_queue_head_t poll; 47 - struct list_head files; /* goes through sysfs_open_file.list */ 48 - }; 49 - 50 - struct sysfs_open_file { 51 - struct sysfs_dirent *sd; 52 - struct file *file; 53 - struct mutex mutex; 54 - int event; 55 - struct list_head list; 56 - 57 - bool mmapped; 58 - const struct vm_operations_struct *vm_ops; 59 - }; 60 - 61 - static bool sysfs_is_bin(struct sysfs_dirent *sd) 62 - { 63 - return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR; 64 - } 65 - 66 - static struct sysfs_open_file *sysfs_of(struct file *file) 67 - { 68 - return ((struct seq_file *)file->private_data)->private; 69 - } 70 - 71 - /* 72 - * Determine ktype->sysfs_ops for the given sysfs_dirent. This function 25 + * Determine ktype->sysfs_ops for the given kernfs_node. This function 73 26 * must be called while holding an active reference. 74 27 */ 75 - static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd) 28 + static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn) 76 29 { 77 - struct kobject *kobj = sd->s_parent->s_dir.kobj; 30 + struct kobject *kobj = kn->parent->priv; 78 31 79 - if (!sysfs_ignore_lockdep(sd)) 80 - lockdep_assert_held(sd); 32 + if (kn->flags & KERNFS_LOCKDEP) 33 + lockdep_assert_held(kn); 81 34 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL; 82 35 } 83 36 ··· 39 86 * details like buffering and seeking. The following function pipes 40 87 * sysfs_ops->show() result through seq_file. 41 88 */ 42 - static int sysfs_seq_show(struct seq_file *sf, void *v) 89 + static int sysfs_kf_seq_show(struct seq_file *sf, void *v) 43 90 { 44 - struct sysfs_open_file *of = sf->private; 45 - struct kobject *kobj = of->sd->s_parent->s_dir.kobj; 46 - const struct sysfs_ops *ops; 47 - char *buf; 91 + struct kernfs_open_file *of = sf->private; 92 + struct kobject *kobj = of->kn->parent->priv; 93 + const struct sysfs_ops *ops = sysfs_file_ops(of->kn); 48 94 ssize_t count; 95 + char *buf; 49 96 50 97 /* acquire buffer and ensure that it's >= PAGE_SIZE */ 51 98 count = seq_get_buf(sf, &buf); ··· 55 102 } 56 103 57 104 /* 58 - * Need @of->sd for attr and ops, its parent for kobj. @of->mutex 59 - * nests outside active ref and is just to ensure that the ops 60 - * aren't called concurrently for the same open file. 105 + * Invoke show(). Control may reach here via seq file lseek even 106 + * if @ops->show() isn't implemented. 61 107 */ 62 - mutex_lock(&of->mutex); 63 - if (!sysfs_get_active(of->sd)) { 64 - mutex_unlock(&of->mutex); 65 - return -ENODEV; 108 + if (ops->show) { 109 + count = ops->show(kobj, of->kn->priv, buf); 110 + if (count < 0) 111 + return count; 66 112 } 67 - 68 - of->event = atomic_read(&of->sd->s_attr.open->event); 69 - 70 - /* 71 - * Lookup @ops and invoke show(). Control may reach here via seq 72 - * file lseek even if @ops->show() isn't implemented. 73 - */ 74 - ops = sysfs_file_ops(of->sd); 75 - if (ops->show) 76 - count = ops->show(kobj, of->sd->s_attr.attr, buf); 77 - else 78 - count = 0; 79 - 80 - sysfs_put_active(of->sd); 81 - mutex_unlock(&of->mutex); 82 - 83 - if (count < 0) 84 - return count; 85 113 86 114 /* 87 115 * The code works fine with PAGE_SIZE return but it's likely to ··· 78 144 return 0; 79 145 } 80 146 81 - /* 82 - * Read method for bin files. As reading a bin file can have side-effects, 83 - * the exact offset and bytes specified in read(2) call should be passed to 84 - * the read callback making it difficult to use seq_file. Implement 85 - * simplistic custom buffering for bin files. 86 - */ 87 - static ssize_t sysfs_bin_read(struct file *file, char __user *userbuf, 88 - size_t bytes, loff_t *off) 147 + static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf, 148 + size_t count, loff_t pos) 89 149 { 90 - struct sysfs_open_file *of = sysfs_of(file); 91 - struct bin_attribute *battr = of->sd->s_attr.bin_attr; 92 - struct kobject *kobj = of->sd->s_parent->s_dir.kobj; 93 - loff_t size = file_inode(file)->i_size; 94 - int count = min_t(size_t, bytes, PAGE_SIZE); 95 - loff_t offs = *off; 96 - char *buf; 150 + struct bin_attribute *battr = of->kn->priv; 151 + struct kobject *kobj = of->kn->parent->priv; 152 + loff_t size = file_inode(of->file)->i_size; 97 153 98 - if (!bytes) 154 + if (!count) 99 155 return 0; 100 156 101 157 if (size) { 102 - if (offs > size) 158 + if (pos > size) 103 159 return 0; 104 - if (offs + count > size) 105 - count = size - offs; 160 + if (pos + count > size) 161 + count = size - pos; 106 162 } 107 163 108 - buf = kmalloc(count, GFP_KERNEL); 109 - if (!buf) 110 - return -ENOMEM; 164 + if (!battr->read) 165 + return -EIO; 111 166 112 - /* need of->sd for battr, its parent for kobj */ 113 - mutex_lock(&of->mutex); 114 - if (!sysfs_get_active(of->sd)) { 115 - count = -ENODEV; 116 - mutex_unlock(&of->mutex); 117 - goto out_free; 118 - } 119 - 120 - if (battr->read) 121 - count = battr->read(file, kobj, battr, buf, offs, count); 122 - else 123 - count = -EIO; 124 - 125 - sysfs_put_active(of->sd); 126 - mutex_unlock(&of->mutex); 127 - 128 - if (count < 0) 129 - goto out_free; 130 - 131 - if (copy_to_user(userbuf, buf, count)) { 132 - count = -EFAULT; 133 - goto out_free; 134 - } 135 - 136 - pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count); 137 - 138 - *off = offs + count; 139 - 140 - out_free: 141 - kfree(buf); 142 - return count; 167 + return battr->read(of->file, kobj, battr, buf, pos, count); 143 168 } 144 169 145 - /** 146 - * flush_write_buffer - push buffer to kobject 147 - * @of: open file 148 - * @buf: data buffer for file 149 - * @off: file offset to write to 150 - * @count: number of bytes 151 - * 152 - * Get the correct pointers for the kobject and the attribute we're dealing 153 - * with, then call the store() method for it with @buf. 154 - */ 155 - static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off, 156 - size_t count) 170 + /* kernfs write callback for regular sysfs files */ 171 + static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf, 172 + size_t count, loff_t pos) 157 173 { 158 - struct kobject *kobj = of->sd->s_parent->s_dir.kobj; 159 - int rc = 0; 174 + const struct sysfs_ops *ops = sysfs_file_ops(of->kn); 175 + struct kobject *kobj = of->kn->parent->priv; 160 176 161 - /* 162 - * Need @of->sd for attr and ops, its parent for kobj. @of->mutex 163 - * nests outside active ref and is just to ensure that the ops 164 - * aren't called concurrently for the same open file. 165 - */ 166 - mutex_lock(&of->mutex); 167 - if (!sysfs_get_active(of->sd)) { 168 - mutex_unlock(&of->mutex); 169 - return -ENODEV; 170 - } 177 + if (!count) 178 + return 0; 171 179 172 - if (sysfs_is_bin(of->sd)) { 173 - struct bin_attribute *battr = of->sd->s_attr.bin_attr; 174 - 175 - rc = -EIO; 176 - if (battr->write) 177 - rc = battr->write(of->file, kobj, battr, buf, off, 178 - count); 179 - } else { 180 - const struct sysfs_ops *ops = sysfs_file_ops(of->sd); 181 - 182 - rc = ops->store(kobj, of->sd->s_attr.attr, buf, count); 183 - } 184 - 185 - sysfs_put_active(of->sd); 186 - mutex_unlock(&of->mutex); 187 - 188 - return rc; 180 + return ops->store(kobj, of->kn->priv, buf, count); 189 181 } 190 182 191 - /** 192 - * sysfs_write_file - write an attribute 193 - * @file: file pointer 194 - * @user_buf: data to write 195 - * @count: number of bytes 196 - * @ppos: starting offset 197 - * 198 - * Copy data in from userland and pass it to the matching 199 - * sysfs_ops->store() by invoking flush_write_buffer(). 200 - * 201 - * There is no easy way for us to know if userspace is only doing a partial 202 - * write, so we don't support them. We expect the entire buffer to come on 203 - * the first write. Hint: if you're writing a value, first read the file, 204 - * modify only the the value you're changing, then write entire buffer 205 - * back. 206 - */ 207 - static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf, 208 - size_t count, loff_t *ppos) 183 + /* kernfs write callback for bin sysfs files */ 184 + static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf, 185 + size_t count, loff_t pos) 209 186 { 210 - struct sysfs_open_file *of = sysfs_of(file); 211 - ssize_t len = min_t(size_t, count, PAGE_SIZE); 212 - loff_t size = file_inode(file)->i_size; 213 - char *buf; 187 + struct bin_attribute *battr = of->kn->priv; 188 + struct kobject *kobj = of->kn->parent->priv; 189 + loff_t size = file_inode(of->file)->i_size; 214 190 215 - if (sysfs_is_bin(of->sd) && size) { 216 - if (size <= *ppos) 191 + if (size) { 192 + if (size <= pos) 217 193 return 0; 218 - len = min_t(ssize_t, len, size - *ppos); 194 + count = min_t(ssize_t, count, size - pos); 219 195 } 220 - 221 - if (!len) 196 + if (!count) 222 197 return 0; 223 198 224 - buf = kmalloc(len + 1, GFP_KERNEL); 225 - if (!buf) 226 - return -ENOMEM; 199 + if (!battr->write) 200 + return -EIO; 227 201 228 - if (copy_from_user(buf, user_buf, len)) { 229 - len = -EFAULT; 230 - goto out_free; 231 - } 232 - buf[len] = '\0'; /* guarantee string termination */ 233 - 234 - len = flush_write_buffer(of, buf, *ppos, len); 235 - if (len > 0) 236 - *ppos += len; 237 - out_free: 238 - kfree(buf); 239 - return len; 202 + return battr->write(of->file, kobj, battr, buf, pos, count); 240 203 } 241 204 242 - static void sysfs_bin_vma_open(struct vm_area_struct *vma) 205 + static int sysfs_kf_bin_mmap(struct kernfs_open_file *of, 206 + struct vm_area_struct *vma) 243 207 { 244 - struct file *file = vma->vm_file; 245 - struct sysfs_open_file *of = sysfs_of(file); 208 + struct bin_attribute *battr = of->kn->priv; 209 + struct kobject *kobj = of->kn->parent->priv; 246 210 247 - if (!of->vm_ops) 248 - return; 249 - 250 - if (!sysfs_get_active(of->sd)) 251 - return; 252 - 253 - if (of->vm_ops->open) 254 - of->vm_ops->open(vma); 255 - 256 - sysfs_put_active(of->sd); 211 + return battr->mmap(of->file, kobj, battr, vma); 257 212 } 258 213 259 - static int sysfs_bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 214 + void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr) 260 215 { 261 - struct file *file = vma->vm_file; 262 - struct sysfs_open_file *of = sysfs_of(file); 263 - int ret; 216 + struct kernfs_node *kn = kobj->sd, *tmp; 264 217 265 - if (!of->vm_ops) 266 - return VM_FAULT_SIGBUS; 267 - 268 - if (!sysfs_get_active(of->sd)) 269 - return VM_FAULT_SIGBUS; 270 - 271 - ret = VM_FAULT_SIGBUS; 272 - if (of->vm_ops->fault) 273 - ret = of->vm_ops->fault(vma, vmf); 274 - 275 - sysfs_put_active(of->sd); 276 - return ret; 277 - } 278 - 279 - static int sysfs_bin_page_mkwrite(struct vm_area_struct *vma, 280 - struct vm_fault *vmf) 281 - { 282 - struct file *file = vma->vm_file; 283 - struct sysfs_open_file *of = sysfs_of(file); 284 - int ret; 285 - 286 - if (!of->vm_ops) 287 - return VM_FAULT_SIGBUS; 288 - 289 - if (!sysfs_get_active(of->sd)) 290 - return VM_FAULT_SIGBUS; 291 - 292 - ret = 0; 293 - if (of->vm_ops->page_mkwrite) 294 - ret = of->vm_ops->page_mkwrite(vma, vmf); 218 + if (kn && dir) 219 + kn = kernfs_find_and_get(kn, dir); 295 220 else 296 - file_update_time(file); 221 + kernfs_get(kn); 297 222 298 - sysfs_put_active(of->sd); 299 - return ret; 300 - } 301 - 302 - static int sysfs_bin_access(struct vm_area_struct *vma, unsigned long addr, 303 - void *buf, int len, int write) 304 - { 305 - struct file *file = vma->vm_file; 306 - struct sysfs_open_file *of = sysfs_of(file); 307 - int ret; 308 - 309 - if (!of->vm_ops) 310 - return -EINVAL; 311 - 312 - if (!sysfs_get_active(of->sd)) 313 - return -EINVAL; 314 - 315 - ret = -EINVAL; 316 - if (of->vm_ops->access) 317 - ret = of->vm_ops->access(vma, addr, buf, len, write); 318 - 319 - sysfs_put_active(of->sd); 320 - return ret; 321 - } 322 - 323 - #ifdef CONFIG_NUMA 324 - static int sysfs_bin_set_policy(struct vm_area_struct *vma, 325 - struct mempolicy *new) 326 - { 327 - struct file *file = vma->vm_file; 328 - struct sysfs_open_file *of = sysfs_of(file); 329 - int ret; 330 - 331 - if (!of->vm_ops) 332 - return 0; 333 - 334 - if (!sysfs_get_active(of->sd)) 335 - return -EINVAL; 336 - 337 - ret = 0; 338 - if (of->vm_ops->set_policy) 339 - ret = of->vm_ops->set_policy(vma, new); 340 - 341 - sysfs_put_active(of->sd); 342 - return ret; 343 - } 344 - 345 - static struct mempolicy *sysfs_bin_get_policy(struct vm_area_struct *vma, 346 - unsigned long addr) 347 - { 348 - struct file *file = vma->vm_file; 349 - struct sysfs_open_file *of = sysfs_of(file); 350 - struct mempolicy *pol; 351 - 352 - if (!of->vm_ops) 353 - return vma->vm_policy; 354 - 355 - if (!sysfs_get_active(of->sd)) 356 - return vma->vm_policy; 357 - 358 - pol = vma->vm_policy; 359 - if (of->vm_ops->get_policy) 360 - pol = of->vm_ops->get_policy(vma, addr); 361 - 362 - sysfs_put_active(of->sd); 363 - return pol; 364 - } 365 - 366 - static int sysfs_bin_migrate(struct vm_area_struct *vma, const nodemask_t *from, 367 - const nodemask_t *to, unsigned long flags) 368 - { 369 - struct file *file = vma->vm_file; 370 - struct sysfs_open_file *of = sysfs_of(file); 371 - int ret; 372 - 373 - if (!of->vm_ops) 374 - return 0; 375 - 376 - if (!sysfs_get_active(of->sd)) 377 - return 0; 378 - 379 - ret = 0; 380 - if (of->vm_ops->migrate) 381 - ret = of->vm_ops->migrate(vma, from, to, flags); 382 - 383 - sysfs_put_active(of->sd); 384 - return ret; 385 - } 386 - #endif 387 - 388 - static const struct vm_operations_struct sysfs_bin_vm_ops = { 389 - .open = sysfs_bin_vma_open, 390 - .fault = sysfs_bin_fault, 391 - .page_mkwrite = sysfs_bin_page_mkwrite, 392 - .access = sysfs_bin_access, 393 - #ifdef CONFIG_NUMA 394 - .set_policy = sysfs_bin_set_policy, 395 - .get_policy = sysfs_bin_get_policy, 396 - .migrate = sysfs_bin_migrate, 397 - #endif 398 - }; 399 - 400 - static int sysfs_bin_mmap(struct file *file, struct vm_area_struct *vma) 401 - { 402 - struct sysfs_open_file *of = sysfs_of(file); 403 - struct bin_attribute *battr = of->sd->s_attr.bin_attr; 404 - struct kobject *kobj = of->sd->s_parent->s_dir.kobj; 405 - int rc; 406 - 407 - mutex_lock(&of->mutex); 408 - 409 - /* need of->sd for battr, its parent for kobj */ 410 - rc = -ENODEV; 411 - if (!sysfs_get_active(of->sd)) 412 - goto out_unlock; 413 - 414 - if (!battr->mmap) 415 - goto out_put; 416 - 417 - rc = battr->mmap(file, kobj, battr, vma); 418 - if (rc) 419 - goto out_put; 420 - 421 - /* 422 - * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup() 423 - * to satisfy versions of X which crash if the mmap fails: that 424 - * substitutes a new vm_file, and we don't then want bin_vm_ops. 425 - */ 426 - if (vma->vm_file != file) 427 - goto out_put; 428 - 429 - rc = -EINVAL; 430 - if (of->mmapped && of->vm_ops != vma->vm_ops) 431 - goto out_put; 432 - 433 - /* 434 - * It is not possible to successfully wrap close. 435 - * So error if someone is trying to use close. 436 - */ 437 - rc = -EINVAL; 438 - if (vma->vm_ops && vma->vm_ops->close) 439 - goto out_put; 440 - 441 - rc = 0; 442 - of->mmapped = 1; 443 - of->vm_ops = vma->vm_ops; 444 - vma->vm_ops = &sysfs_bin_vm_ops; 445 - out_put: 446 - sysfs_put_active(of->sd); 447 - out_unlock: 448 - mutex_unlock(&of->mutex); 449 - 450 - return rc; 451 - } 452 - 453 - /** 454 - * sysfs_get_open_dirent - get or create sysfs_open_dirent 455 - * @sd: target sysfs_dirent 456 - * @of: sysfs_open_file for this instance of open 457 - * 458 - * If @sd->s_attr.open exists, increment its reference count; 459 - * otherwise, create one. @of is chained to the files list. 460 - * 461 - * LOCKING: 462 - * Kernel thread context (may sleep). 463 - * 464 - * RETURNS: 465 - * 0 on success, -errno on failure. 466 - */ 467 - static int sysfs_get_open_dirent(struct sysfs_dirent *sd, 468 - struct sysfs_open_file *of) 469 - { 470 - struct sysfs_open_dirent *od, *new_od = NULL; 471 - 472 - retry: 473 - mutex_lock(&sysfs_open_file_mutex); 474 - spin_lock_irq(&sysfs_open_dirent_lock); 475 - 476 - if (!sd->s_attr.open && new_od) { 477 - sd->s_attr.open = new_od; 478 - new_od = NULL; 223 + if (kn && attr) { 224 + tmp = kernfs_find_and_get(kn, attr); 225 + kernfs_put(kn); 226 + kn = tmp; 479 227 } 480 228 481 - od = sd->s_attr.open; 482 - if (od) { 483 - atomic_inc(&od->refcnt); 484 - list_add_tail(&of->list, &od->files); 229 + if (kn) { 230 + kernfs_notify(kn); 231 + kernfs_put(kn); 485 232 } 486 - 487 - spin_unlock_irq(&sysfs_open_dirent_lock); 488 - mutex_unlock(&sysfs_open_file_mutex); 489 - 490 - if (od) { 491 - kfree(new_od); 492 - return 0; 493 - } 494 - 495 - /* not there, initialize a new one and retry */ 496 - new_od = kmalloc(sizeof(*new_od), GFP_KERNEL); 497 - if (!new_od) 498 - return -ENOMEM; 499 - 500 - atomic_set(&new_od->refcnt, 0); 501 - atomic_set(&new_od->event, 1); 502 - init_waitqueue_head(&new_od->poll); 503 - INIT_LIST_HEAD(&new_od->files); 504 - goto retry; 505 - } 506 - 507 - /** 508 - * sysfs_put_open_dirent - put sysfs_open_dirent 509 - * @sd: target sysfs_dirent 510 - * @of: associated sysfs_open_file 511 - * 512 - * Put @sd->s_attr.open and unlink @of from the files list. If 513 - * reference count reaches zero, disassociate and free it. 514 - * 515 - * LOCKING: 516 - * None. 517 - */ 518 - static void sysfs_put_open_dirent(struct sysfs_dirent *sd, 519 - struct sysfs_open_file *of) 520 - { 521 - struct sysfs_open_dirent *od = sd->s_attr.open; 522 - unsigned long flags; 523 - 524 - mutex_lock(&sysfs_open_file_mutex); 525 - spin_lock_irqsave(&sysfs_open_dirent_lock, flags); 526 - 527 - if (of) 528 - list_del(&of->list); 529 - 530 - if (atomic_dec_and_test(&od->refcnt)) 531 - sd->s_attr.open = NULL; 532 - else 533 - od = NULL; 534 - 535 - spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); 536 - mutex_unlock(&sysfs_open_file_mutex); 537 - 538 - kfree(od); 539 - } 540 - 541 - static int sysfs_open_file(struct inode *inode, struct file *file) 542 - { 543 - struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; 544 - struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; 545 - struct sysfs_open_file *of; 546 - bool has_read, has_write; 547 - int error = -EACCES; 548 - 549 - /* need attr_sd for attr and ops, its parent for kobj */ 550 - if (!sysfs_get_active(attr_sd)) 551 - return -ENODEV; 552 - 553 - if (sysfs_is_bin(attr_sd)) { 554 - struct bin_attribute *battr = attr_sd->s_attr.bin_attr; 555 - 556 - has_read = battr->read || battr->mmap; 557 - has_write = battr->write || battr->mmap; 558 - } else { 559 - const struct sysfs_ops *ops = sysfs_file_ops(attr_sd); 560 - 561 - /* every kobject with an attribute needs a ktype assigned */ 562 - if (WARN(!ops, KERN_ERR 563 - "missing sysfs attribute operations for kobject: %s\n", 564 - kobject_name(kobj))) 565 - goto err_out; 566 - 567 - has_read = ops->show; 568 - has_write = ops->store; 569 - } 570 - 571 - /* check perms and supported operations */ 572 - if ((file->f_mode & FMODE_WRITE) && 573 - (!(inode->i_mode & S_IWUGO) || !has_write)) 574 - goto err_out; 575 - 576 - if ((file->f_mode & FMODE_READ) && 577 - (!(inode->i_mode & S_IRUGO) || !has_read)) 578 - goto err_out; 579 - 580 - /* allocate a sysfs_open_file for the file */ 581 - error = -ENOMEM; 582 - of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL); 583 - if (!of) 584 - goto err_out; 585 - 586 - /* 587 - * The following is done to give a different lockdep key to 588 - * @of->mutex for files which implement mmap. This is a rather 589 - * crude way to avoid false positive lockdep warning around 590 - * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and 591 - * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under 592 - * which mm->mmap_sem nests, while holding @of->mutex. As each 593 - * open file has a separate mutex, it's okay as long as those don't 594 - * happen on the same file. At this point, we can't easily give 595 - * each file a separate locking class. Let's differentiate on 596 - * whether the file is bin or not for now. 597 - */ 598 - if (sysfs_is_bin(attr_sd)) 599 - mutex_init(&of->mutex); 600 - else 601 - mutex_init(&of->mutex); 602 - 603 - of->sd = attr_sd; 604 - of->file = file; 605 - 606 - /* 607 - * Always instantiate seq_file even if read access doesn't use 608 - * seq_file or is not requested. This unifies private data access 609 - * and readable regular files are the vast majority anyway. 610 - */ 611 - if (sysfs_is_bin(attr_sd)) 612 - error = single_open(file, NULL, of); 613 - else 614 - error = single_open(file, sysfs_seq_show, of); 615 - if (error) 616 - goto err_free; 617 - 618 - /* seq_file clears PWRITE unconditionally, restore it if WRITE */ 619 - if (file->f_mode & FMODE_WRITE) 620 - file->f_mode |= FMODE_PWRITE; 621 - 622 - /* make sure we have open dirent struct */ 623 - error = sysfs_get_open_dirent(attr_sd, of); 624 - if (error) 625 - goto err_close; 626 - 627 - /* open succeeded, put active references */ 628 - sysfs_put_active(attr_sd); 629 - return 0; 630 - 631 - err_close: 632 - single_release(inode, file); 633 - err_free: 634 - kfree(of); 635 - err_out: 636 - sysfs_put_active(attr_sd); 637 - return error; 638 - } 639 - 640 - static int sysfs_release(struct inode *inode, struct file *filp) 641 - { 642 - struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata; 643 - struct sysfs_open_file *of = sysfs_of(filp); 644 - 645 - sysfs_put_open_dirent(sd, of); 646 - single_release(inode, filp); 647 - kfree(of); 648 - 649 - return 0; 650 - } 651 - 652 - void sysfs_unmap_bin_file(struct sysfs_dirent *sd) 653 - { 654 - struct sysfs_open_dirent *od; 655 - struct sysfs_open_file *of; 656 - 657 - if (!sysfs_is_bin(sd)) 658 - return; 659 - 660 - spin_lock_irq(&sysfs_open_dirent_lock); 661 - od = sd->s_attr.open; 662 - if (od) 663 - atomic_inc(&od->refcnt); 664 - spin_unlock_irq(&sysfs_open_dirent_lock); 665 - if (!od) 666 - return; 667 - 668 - mutex_lock(&sysfs_open_file_mutex); 669 - list_for_each_entry(of, &od->files, list) { 670 - struct inode *inode = file_inode(of->file); 671 - unmap_mapping_range(inode->i_mapping, 0, 0, 1); 672 - } 673 - mutex_unlock(&sysfs_open_file_mutex); 674 - 675 - sysfs_put_open_dirent(sd, NULL); 676 - } 677 - 678 - /* Sysfs attribute files are pollable. The idea is that you read 679 - * the content and then you use 'poll' or 'select' to wait for 680 - * the content to change. When the content changes (assuming the 681 - * manager for the kobject supports notification), poll will 682 - * return POLLERR|POLLPRI, and select will return the fd whether 683 - * it is waiting for read, write, or exceptions. 684 - * Once poll/select indicates that the value has changed, you 685 - * need to close and re-open the file, or seek to 0 and read again. 686 - * Reminder: this only works for attributes which actively support 687 - * it, and it is not possible to test an attribute from userspace 688 - * to see if it supports poll (Neither 'poll' nor 'select' return 689 - * an appropriate error code). When in doubt, set a suitable timeout value. 690 - */ 691 - static unsigned int sysfs_poll(struct file *filp, poll_table *wait) 692 - { 693 - struct sysfs_open_file *of = sysfs_of(filp); 694 - struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata; 695 - struct sysfs_open_dirent *od = attr_sd->s_attr.open; 696 - 697 - /* need parent for the kobj, grab both */ 698 - if (!sysfs_get_active(attr_sd)) 699 - goto trigger; 700 - 701 - poll_wait(filp, &od->poll, wait); 702 - 703 - sysfs_put_active(attr_sd); 704 - 705 - if (of->event != atomic_read(&od->event)) 706 - goto trigger; 707 - 708 - return DEFAULT_POLLMASK; 709 - 710 - trigger: 711 - return DEFAULT_POLLMASK|POLLERR|POLLPRI; 712 - } 713 - 714 - void sysfs_notify_dirent(struct sysfs_dirent *sd) 715 - { 716 - struct sysfs_open_dirent *od; 717 - unsigned long flags; 718 - 719 - spin_lock_irqsave(&sysfs_open_dirent_lock, flags); 720 - 721 - if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) { 722 - od = sd->s_attr.open; 723 - if (od) { 724 - atomic_inc(&od->event); 725 - wake_up_interruptible(&od->poll); 726 - } 727 - } 728 - 729 - spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); 730 - } 731 - EXPORT_SYMBOL_GPL(sysfs_notify_dirent); 732 - 733 - void sysfs_notify(struct kobject *k, const char *dir, const char *attr) 734 - { 735 - struct sysfs_dirent *sd = k->sd; 736 - 737 - mutex_lock(&sysfs_mutex); 738 - 739 - if (sd && dir) 740 - sd = sysfs_find_dirent(sd, dir, NULL); 741 - if (sd && attr) 742 - sd = sysfs_find_dirent(sd, attr, NULL); 743 - if (sd) 744 - sysfs_notify_dirent(sd); 745 - 746 - mutex_unlock(&sysfs_mutex); 747 233 } 748 234 EXPORT_SYMBOL_GPL(sysfs_notify); 749 235 750 - const struct file_operations sysfs_file_operations = { 751 - .read = seq_read, 752 - .write = sysfs_write_file, 753 - .llseek = generic_file_llseek, 754 - .open = sysfs_open_file, 755 - .release = sysfs_release, 756 - .poll = sysfs_poll, 236 + static const struct kernfs_ops sysfs_file_kfops_empty = { 757 237 }; 758 238 759 - const struct file_operations sysfs_bin_operations = { 760 - .read = sysfs_bin_read, 761 - .write = sysfs_write_file, 762 - .llseek = generic_file_llseek, 763 - .mmap = sysfs_bin_mmap, 764 - .open = sysfs_open_file, 765 - .release = sysfs_release, 766 - .poll = sysfs_poll, 239 + static const struct kernfs_ops sysfs_file_kfops_ro = { 240 + .seq_show = sysfs_kf_seq_show, 767 241 }; 768 242 769 - int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd, 770 - const struct attribute *attr, int type, 771 - umode_t amode, const void *ns) 243 + static const struct kernfs_ops sysfs_file_kfops_wo = { 244 + .write = sysfs_kf_write, 245 + }; 246 + 247 + static const struct kernfs_ops sysfs_file_kfops_rw = { 248 + .seq_show = sysfs_kf_seq_show, 249 + .write = sysfs_kf_write, 250 + }; 251 + 252 + static const struct kernfs_ops sysfs_bin_kfops_ro = { 253 + .read = sysfs_kf_bin_read, 254 + }; 255 + 256 + static const struct kernfs_ops sysfs_bin_kfops_wo = { 257 + .write = sysfs_kf_bin_write, 258 + }; 259 + 260 + static const struct kernfs_ops sysfs_bin_kfops_rw = { 261 + .read = sysfs_kf_bin_read, 262 + .write = sysfs_kf_bin_write, 263 + }; 264 + 265 + static const struct kernfs_ops sysfs_bin_kfops_mmap = { 266 + .read = sysfs_kf_bin_read, 267 + .write = sysfs_kf_bin_write, 268 + .mmap = sysfs_kf_bin_mmap, 269 + }; 270 + 271 + int sysfs_add_file_mode_ns(struct kernfs_node *parent, 272 + const struct attribute *attr, bool is_bin, 273 + umode_t mode, const void *ns) 772 274 { 773 - umode_t mode = (amode & S_IALLUGO) | S_IFREG; 774 - struct sysfs_addrm_cxt acxt; 775 - struct sysfs_dirent *sd; 776 - int rc; 275 + struct lock_class_key *key = NULL; 276 + const struct kernfs_ops *ops; 277 + struct kernfs_node *kn; 278 + loff_t size; 777 279 778 - sd = sysfs_new_dirent(attr->name, mode, type); 779 - if (!sd) 780 - return -ENOMEM; 280 + if (!is_bin) { 281 + struct kobject *kobj = parent->priv; 282 + const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops; 781 283 782 - sd->s_ns = ns; 783 - sd->s_attr.attr = (void *)attr; 784 - sysfs_dirent_init_lockdep(sd); 284 + /* every kobject with an attribute needs a ktype assigned */ 285 + if (WARN(!sysfs_ops, KERN_ERR 286 + "missing sysfs attribute operations for kobject: %s\n", 287 + kobject_name(kobj))) 288 + return -EINVAL; 785 289 786 - sysfs_addrm_start(&acxt); 787 - rc = sysfs_add_one(&acxt, sd, dir_sd); 788 - sysfs_addrm_finish(&acxt); 290 + if (sysfs_ops->show && sysfs_ops->store) 291 + ops = &sysfs_file_kfops_rw; 292 + else if (sysfs_ops->show) 293 + ops = &sysfs_file_kfops_ro; 294 + else if (sysfs_ops->store) 295 + ops = &sysfs_file_kfops_wo; 296 + else 297 + ops = &sysfs_file_kfops_empty; 789 298 790 - if (rc) 791 - sysfs_put(sd); 299 + size = PAGE_SIZE; 300 + } else { 301 + struct bin_attribute *battr = (void *)attr; 792 302 793 - return rc; 303 + if (battr->mmap) 304 + ops = &sysfs_bin_kfops_mmap; 305 + else if (battr->read && battr->write) 306 + ops = &sysfs_bin_kfops_rw; 307 + else if (battr->read) 308 + ops = &sysfs_bin_kfops_ro; 309 + else if (battr->write) 310 + ops = &sysfs_bin_kfops_wo; 311 + else 312 + ops = &sysfs_file_kfops_empty; 313 + 314 + size = battr->size; 315 + } 316 + 317 + #ifdef CONFIG_DEBUG_LOCK_ALLOC 318 + if (!attr->ignore_lockdep) 319 + key = attr->key ?: (struct lock_class_key *)&attr->skey; 320 + #endif 321 + kn = __kernfs_create_file(parent, attr->name, mode, size, ops, 322 + (void *)attr, ns, true, key); 323 + if (IS_ERR(kn)) { 324 + if (PTR_ERR(kn) == -EEXIST) 325 + sysfs_warn_dup(parent, attr->name); 326 + return PTR_ERR(kn); 327 + } 328 + return 0; 794 329 } 795 330 796 - 797 - int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr, 798 - int type) 331 + int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr, 332 + bool is_bin) 799 333 { 800 - return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL); 334 + return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL); 801 335 } 802 336 803 337 /** ··· 279 877 { 280 878 BUG_ON(!kobj || !kobj->sd || !attr); 281 879 282 - return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR, 283 - attr->mode, ns); 880 + return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns); 284 881 285 882 } 286 883 EXPORT_SYMBOL_GPL(sysfs_create_file_ns); ··· 307 906 int sysfs_add_file_to_group(struct kobject *kobj, 308 907 const struct attribute *attr, const char *group) 309 908 { 310 - struct sysfs_dirent *dir_sd; 909 + struct kernfs_node *parent; 311 910 int error; 312 911 313 - if (group) 314 - dir_sd = sysfs_get_dirent(kobj->sd, group); 315 - else 316 - dir_sd = sysfs_get(kobj->sd); 912 + if (group) { 913 + parent = kernfs_find_and_get(kobj->sd, group); 914 + } else { 915 + parent = kobj->sd; 916 + kernfs_get(parent); 917 + } 317 918 318 - if (!dir_sd) 919 + if (!parent) 319 920 return -ENOENT; 320 921 321 - error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR); 322 - sysfs_put(dir_sd); 922 + error = sysfs_add_file(parent, attr, false); 923 + kernfs_put(parent); 323 924 324 925 return error; 325 926 } ··· 337 934 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr, 338 935 umode_t mode) 339 936 { 340 - struct sysfs_dirent *sd; 937 + struct kernfs_node *kn; 341 938 struct iattr newattrs; 342 939 int rc; 343 940 344 - mutex_lock(&sysfs_mutex); 941 + kn = kernfs_find_and_get(kobj->sd, attr->name); 942 + if (!kn) 943 + return -ENOENT; 345 944 346 - rc = -ENOENT; 347 - sd = sysfs_find_dirent(kobj->sd, attr->name, NULL); 348 - if (!sd) 349 - goto out; 350 - 351 - newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO); 945 + newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO); 352 946 newattrs.ia_valid = ATTR_MODE; 353 - rc = sysfs_sd_setattr(sd, &newattrs); 354 947 355 - out: 356 - mutex_unlock(&sysfs_mutex); 948 + rc = kernfs_setattr(kn, &newattrs); 949 + 950 + kernfs_put(kn); 357 951 return rc; 358 952 } 359 953 EXPORT_SYMBOL_GPL(sysfs_chmod_file); ··· 366 966 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr, 367 967 const void *ns) 368 968 { 369 - struct sysfs_dirent *dir_sd = kobj->sd; 969 + struct kernfs_node *parent = kobj->sd; 370 970 371 - sysfs_hash_and_remove(dir_sd, attr->name, ns); 971 + kernfs_remove_by_name_ns(parent, attr->name, ns); 372 972 } 373 973 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns); 374 974 ··· 389 989 void sysfs_remove_file_from_group(struct kobject *kobj, 390 990 const struct attribute *attr, const char *group) 391 991 { 392 - struct sysfs_dirent *dir_sd; 992 + struct kernfs_node *parent; 393 993 394 - if (group) 395 - dir_sd = sysfs_get_dirent(kobj->sd, group); 396 - else 397 - dir_sd = sysfs_get(kobj->sd); 398 - if (dir_sd) { 399 - sysfs_hash_and_remove(dir_sd, attr->name, NULL); 400 - sysfs_put(dir_sd); 994 + if (group) { 995 + parent = kernfs_find_and_get(kobj->sd, group); 996 + } else { 997 + parent = kobj->sd; 998 + kernfs_get(parent); 999 + } 1000 + 1001 + if (parent) { 1002 + kernfs_remove_by_name(parent, attr->name); 1003 + kernfs_put(parent); 401 1004 } 402 1005 } 403 1006 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group); ··· 415 1012 { 416 1013 BUG_ON(!kobj || !kobj->sd || !attr); 417 1014 418 - return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR); 1015 + return sysfs_add_file(kobj->sd, &attr->attr, true); 419 1016 } 420 1017 EXPORT_SYMBOL_GPL(sysfs_create_bin_file); 421 1018 ··· 427 1024 void sysfs_remove_bin_file(struct kobject *kobj, 428 1025 const struct bin_attribute *attr) 429 1026 { 430 - sysfs_hash_and_remove(kobj->sd, attr->attr.name, NULL); 1027 + kernfs_remove_by_name(kobj->sd, attr->attr.name); 431 1028 } 432 1029 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file); 433 1030
+53 -49
fs/sysfs/group.c
··· 18 18 #include "sysfs.h" 19 19 20 20 21 - static void remove_files(struct sysfs_dirent *dir_sd, struct kobject *kobj, 21 + static void remove_files(struct kernfs_node *parent, struct kobject *kobj, 22 22 const struct attribute_group *grp) 23 23 { 24 24 struct attribute *const *attr; ··· 26 26 27 27 if (grp->attrs) 28 28 for (attr = grp->attrs; *attr; attr++) 29 - sysfs_hash_and_remove(dir_sd, (*attr)->name, NULL); 29 + kernfs_remove_by_name(parent, (*attr)->name); 30 30 if (grp->bin_attrs) 31 31 for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++) 32 32 sysfs_remove_bin_file(kobj, *bin_attr); 33 33 } 34 34 35 - static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj, 35 + static int create_files(struct kernfs_node *parent, struct kobject *kobj, 36 36 const struct attribute_group *grp, int update) 37 37 { 38 38 struct attribute *const *attr; ··· 49 49 * re-adding (if required) the file. 50 50 */ 51 51 if (update) 52 - sysfs_hash_and_remove(dir_sd, (*attr)->name, 53 - NULL); 52 + kernfs_remove_by_name(parent, (*attr)->name); 54 53 if (grp->is_visible) { 55 54 mode = grp->is_visible(kobj, *attr, i); 56 55 if (!mode) 57 56 continue; 58 57 } 59 - error = sysfs_add_file_mode_ns(dir_sd, *attr, 60 - SYSFS_KOBJ_ATTR, 58 + error = sysfs_add_file_mode_ns(parent, *attr, false, 61 59 (*attr)->mode | mode, 62 60 NULL); 63 61 if (unlikely(error)) 64 62 break; 65 63 } 66 64 if (error) { 67 - remove_files(dir_sd, kobj, grp); 65 + remove_files(parent, kobj, grp); 68 66 goto exit; 69 67 } 70 68 } ··· 76 78 break; 77 79 } 78 80 if (error) 79 - remove_files(dir_sd, kobj, grp); 81 + remove_files(parent, kobj, grp); 80 82 } 81 83 exit: 82 84 return error; ··· 86 88 static int internal_create_group(struct kobject *kobj, int update, 87 89 const struct attribute_group *grp) 88 90 { 89 - struct sysfs_dirent *sd; 91 + struct kernfs_node *kn; 90 92 int error; 91 93 92 94 BUG_ON(!kobj || (!update && !kobj->sd)); ··· 100 102 return -EINVAL; 101 103 } 102 104 if (grp->name) { 103 - error = sysfs_create_subdir(kobj, grp->name, &sd); 104 - if (error) 105 - return error; 105 + kn = kernfs_create_dir(kobj->sd, grp->name, 106 + S_IRWXU | S_IRUGO | S_IXUGO, kobj); 107 + if (IS_ERR(kn)) { 108 + if (PTR_ERR(kn) == -EEXIST) 109 + sysfs_warn_dup(kobj->sd, grp->name); 110 + return PTR_ERR(kn); 111 + } 106 112 } else 107 - sd = kobj->sd; 108 - sysfs_get(sd); 109 - error = create_files(sd, kobj, grp, update); 113 + kn = kobj->sd; 114 + kernfs_get(kn); 115 + error = create_files(kn, kobj, grp, update); 110 116 if (error) { 111 117 if (grp->name) 112 - sysfs_remove(sd); 118 + kernfs_remove(kn); 113 119 } 114 - sysfs_put(sd); 120 + kernfs_put(kn); 115 121 return error; 116 122 } 117 123 ··· 205 203 void sysfs_remove_group(struct kobject *kobj, 206 204 const struct attribute_group *grp) 207 205 { 208 - struct sysfs_dirent *dir_sd = kobj->sd; 209 - struct sysfs_dirent *sd; 206 + struct kernfs_node *parent = kobj->sd; 207 + struct kernfs_node *kn; 210 208 211 209 if (grp->name) { 212 - sd = sysfs_get_dirent(dir_sd, grp->name); 213 - if (!sd) { 214 - WARN(!sd, KERN_WARNING 210 + kn = kernfs_find_and_get(parent, grp->name); 211 + if (!kn) { 212 + WARN(!kn, KERN_WARNING 215 213 "sysfs group %p not found for kobject '%s'\n", 216 214 grp, kobject_name(kobj)); 217 215 return; 218 216 } 219 - } else 220 - sd = sysfs_get(dir_sd); 217 + } else { 218 + kn = parent; 219 + kernfs_get(kn); 220 + } 221 221 222 - remove_files(sd, kobj, grp); 222 + remove_files(kn, kobj, grp); 223 223 if (grp->name) 224 - sysfs_remove(sd); 224 + kernfs_remove(kn); 225 225 226 - sysfs_put(sd); 226 + kernfs_put(kn); 227 227 } 228 228 EXPORT_SYMBOL_GPL(sysfs_remove_group); 229 229 ··· 261 257 int sysfs_merge_group(struct kobject *kobj, 262 258 const struct attribute_group *grp) 263 259 { 264 - struct sysfs_dirent *dir_sd; 260 + struct kernfs_node *parent; 265 261 int error = 0; 266 262 struct attribute *const *attr; 267 263 int i; 268 264 269 - dir_sd = sysfs_get_dirent(kobj->sd, grp->name); 270 - if (!dir_sd) 265 + parent = kernfs_find_and_get(kobj->sd, grp->name); 266 + if (!parent) 271 267 return -ENOENT; 272 268 273 269 for ((i = 0, attr = grp->attrs); *attr && !error; (++i, ++attr)) 274 - error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR); 270 + error = sysfs_add_file(parent, *attr, false); 275 271 if (error) { 276 272 while (--i >= 0) 277 - sysfs_hash_and_remove(dir_sd, (*--attr)->name, NULL); 273 + kernfs_remove_by_name(parent, (*--attr)->name); 278 274 } 279 - sysfs_put(dir_sd); 275 + kernfs_put(parent); 280 276 281 277 return error; 282 278 } ··· 290 286 void sysfs_unmerge_group(struct kobject *kobj, 291 287 const struct attribute_group *grp) 292 288 { 293 - struct sysfs_dirent *dir_sd; 289 + struct kernfs_node *parent; 294 290 struct attribute *const *attr; 295 291 296 - dir_sd = sysfs_get_dirent(kobj->sd, grp->name); 297 - if (dir_sd) { 292 + parent = kernfs_find_and_get(kobj->sd, grp->name); 293 + if (parent) { 298 294 for (attr = grp->attrs; *attr; ++attr) 299 - sysfs_hash_and_remove(dir_sd, (*attr)->name, NULL); 300 - sysfs_put(dir_sd); 295 + kernfs_remove_by_name(parent, (*attr)->name); 296 + kernfs_put(parent); 301 297 } 302 298 } 303 299 EXPORT_SYMBOL_GPL(sysfs_unmerge_group); ··· 312 308 int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name, 313 309 struct kobject *target, const char *link_name) 314 310 { 315 - struct sysfs_dirent *dir_sd; 311 + struct kernfs_node *parent; 316 312 int error = 0; 317 313 318 - dir_sd = sysfs_get_dirent(kobj->sd, group_name); 319 - if (!dir_sd) 314 + parent = kernfs_find_and_get(kobj->sd, group_name); 315 + if (!parent) 320 316 return -ENOENT; 321 317 322 - error = sysfs_create_link_sd(dir_sd, target, link_name); 323 - sysfs_put(dir_sd); 318 + error = sysfs_create_link_sd(parent, target, link_name); 319 + kernfs_put(parent); 324 320 325 321 return error; 326 322 } ··· 335 331 void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name, 336 332 const char *link_name) 337 333 { 338 - struct sysfs_dirent *dir_sd; 334 + struct kernfs_node *parent; 339 335 340 - dir_sd = sysfs_get_dirent(kobj->sd, group_name); 341 - if (dir_sd) { 342 - sysfs_hash_and_remove(dir_sd, link_name, NULL); 343 - sysfs_put(dir_sd); 336 + parent = kernfs_find_and_get(kobj->sd, group_name); 337 + if (parent) { 338 + kernfs_remove_by_name(parent, link_name); 339 + kernfs_put(parent); 344 340 } 345 341 } 346 342 EXPORT_SYMBOL_GPL(sysfs_remove_link_from_group);
-331
fs/sysfs/inode.c
··· 1 - /* 2 - * fs/sysfs/inode.c - basic sysfs inode and dentry operations 3 - * 4 - * Copyright (c) 2001-3 Patrick Mochel 5 - * Copyright (c) 2007 SUSE Linux Products GmbH 6 - * Copyright (c) 2007 Tejun Heo <teheo@suse.de> 7 - * 8 - * This file is released under the GPLv2. 9 - * 10 - * Please see Documentation/filesystems/sysfs.txt for more information. 11 - */ 12 - 13 - #undef DEBUG 14 - 15 - #include <linux/pagemap.h> 16 - #include <linux/namei.h> 17 - #include <linux/backing-dev.h> 18 - #include <linux/capability.h> 19 - #include <linux/errno.h> 20 - #include <linux/sched.h> 21 - #include <linux/slab.h> 22 - #include <linux/sysfs.h> 23 - #include <linux/xattr.h> 24 - #include <linux/security.h> 25 - #include "sysfs.h" 26 - 27 - static const struct address_space_operations sysfs_aops = { 28 - .readpage = simple_readpage, 29 - .write_begin = simple_write_begin, 30 - .write_end = simple_write_end, 31 - }; 32 - 33 - static struct backing_dev_info sysfs_backing_dev_info = { 34 - .name = "sysfs", 35 - .ra_pages = 0, /* No readahead */ 36 - .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, 37 - }; 38 - 39 - static const struct inode_operations sysfs_inode_operations = { 40 - .permission = sysfs_permission, 41 - .setattr = sysfs_setattr, 42 - .getattr = sysfs_getattr, 43 - .setxattr = sysfs_setxattr, 44 - }; 45 - 46 - int __init sysfs_inode_init(void) 47 - { 48 - return bdi_init(&sysfs_backing_dev_info); 49 - } 50 - 51 - static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd) 52 - { 53 - struct sysfs_inode_attrs *attrs; 54 - struct iattr *iattrs; 55 - 56 - attrs = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL); 57 - if (!attrs) 58 - return NULL; 59 - iattrs = &attrs->ia_iattr; 60 - 61 - /* assign default attributes */ 62 - iattrs->ia_mode = sd->s_mode; 63 - iattrs->ia_uid = GLOBAL_ROOT_UID; 64 - iattrs->ia_gid = GLOBAL_ROOT_GID; 65 - iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME; 66 - 67 - return attrs; 68 - } 69 - 70 - int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr *iattr) 71 - { 72 - struct sysfs_inode_attrs *sd_attrs; 73 - struct iattr *iattrs; 74 - unsigned int ia_valid = iattr->ia_valid; 75 - 76 - sd_attrs = sd->s_iattr; 77 - 78 - if (!sd_attrs) { 79 - /* setting attributes for the first time, allocate now */ 80 - sd_attrs = sysfs_init_inode_attrs(sd); 81 - if (!sd_attrs) 82 - return -ENOMEM; 83 - sd->s_iattr = sd_attrs; 84 - } 85 - /* attributes were changed at least once in past */ 86 - iattrs = &sd_attrs->ia_iattr; 87 - 88 - if (ia_valid & ATTR_UID) 89 - iattrs->ia_uid = iattr->ia_uid; 90 - if (ia_valid & ATTR_GID) 91 - iattrs->ia_gid = iattr->ia_gid; 92 - if (ia_valid & ATTR_ATIME) 93 - iattrs->ia_atime = iattr->ia_atime; 94 - if (ia_valid & ATTR_MTIME) 95 - iattrs->ia_mtime = iattr->ia_mtime; 96 - if (ia_valid & ATTR_CTIME) 97 - iattrs->ia_ctime = iattr->ia_ctime; 98 - if (ia_valid & ATTR_MODE) { 99 - umode_t mode = iattr->ia_mode; 100 - iattrs->ia_mode = sd->s_mode = mode; 101 - } 102 - return 0; 103 - } 104 - 105 - int sysfs_setattr(struct dentry *dentry, struct iattr *iattr) 106 - { 107 - struct inode *inode = dentry->d_inode; 108 - struct sysfs_dirent *sd = dentry->d_fsdata; 109 - int error; 110 - 111 - if (!sd) 112 - return -EINVAL; 113 - 114 - mutex_lock(&sysfs_mutex); 115 - error = inode_change_ok(inode, iattr); 116 - if (error) 117 - goto out; 118 - 119 - error = sysfs_sd_setattr(sd, iattr); 120 - if (error) 121 - goto out; 122 - 123 - /* this ignores size changes */ 124 - setattr_copy(inode, iattr); 125 - 126 - out: 127 - mutex_unlock(&sysfs_mutex); 128 - return error; 129 - } 130 - 131 - static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata, 132 - u32 *secdata_len) 133 - { 134 - struct sysfs_inode_attrs *iattrs; 135 - void *old_secdata; 136 - size_t old_secdata_len; 137 - 138 - if (!sd->s_iattr) { 139 - sd->s_iattr = sysfs_init_inode_attrs(sd); 140 - if (!sd->s_iattr) 141 - return -ENOMEM; 142 - } 143 - 144 - iattrs = sd->s_iattr; 145 - old_secdata = iattrs->ia_secdata; 146 - old_secdata_len = iattrs->ia_secdata_len; 147 - 148 - iattrs->ia_secdata = *secdata; 149 - iattrs->ia_secdata_len = *secdata_len; 150 - 151 - *secdata = old_secdata; 152 - *secdata_len = old_secdata_len; 153 - return 0; 154 - } 155 - 156 - int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value, 157 - size_t size, int flags) 158 - { 159 - struct sysfs_dirent *sd = dentry->d_fsdata; 160 - void *secdata; 161 - int error; 162 - u32 secdata_len = 0; 163 - 164 - if (!sd) 165 - return -EINVAL; 166 - 167 - if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) { 168 - const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; 169 - error = security_inode_setsecurity(dentry->d_inode, suffix, 170 - value, size, flags); 171 - if (error) 172 - goto out; 173 - error = security_inode_getsecctx(dentry->d_inode, 174 - &secdata, &secdata_len); 175 - if (error) 176 - goto out; 177 - 178 - mutex_lock(&sysfs_mutex); 179 - error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len); 180 - mutex_unlock(&sysfs_mutex); 181 - 182 - if (secdata) 183 - security_release_secctx(secdata, secdata_len); 184 - } else 185 - return -EINVAL; 186 - out: 187 - return error; 188 - } 189 - 190 - static inline void set_default_inode_attr(struct inode *inode, umode_t mode) 191 - { 192 - inode->i_mode = mode; 193 - inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 194 - } 195 - 196 - static inline void set_inode_attr(struct inode *inode, struct iattr *iattr) 197 - { 198 - inode->i_uid = iattr->ia_uid; 199 - inode->i_gid = iattr->ia_gid; 200 - inode->i_atime = iattr->ia_atime; 201 - inode->i_mtime = iattr->ia_mtime; 202 - inode->i_ctime = iattr->ia_ctime; 203 - } 204 - 205 - static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode) 206 - { 207 - struct sysfs_inode_attrs *iattrs = sd->s_iattr; 208 - 209 - inode->i_mode = sd->s_mode; 210 - if (iattrs) { 211 - /* sysfs_dirent has non-default attributes 212 - * get them from persistent copy in sysfs_dirent 213 - */ 214 - set_inode_attr(inode, &iattrs->ia_iattr); 215 - security_inode_notifysecctx(inode, 216 - iattrs->ia_secdata, 217 - iattrs->ia_secdata_len); 218 - } 219 - 220 - if (sysfs_type(sd) == SYSFS_DIR) 221 - set_nlink(inode, sd->s_dir.subdirs + 2); 222 - } 223 - 224 - int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, 225 - struct kstat *stat) 226 - { 227 - struct sysfs_dirent *sd = dentry->d_fsdata; 228 - struct inode *inode = dentry->d_inode; 229 - 230 - mutex_lock(&sysfs_mutex); 231 - sysfs_refresh_inode(sd, inode); 232 - mutex_unlock(&sysfs_mutex); 233 - 234 - generic_fillattr(inode, stat); 235 - return 0; 236 - } 237 - 238 - static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode) 239 - { 240 - struct bin_attribute *bin_attr; 241 - 242 - inode->i_private = sysfs_get(sd); 243 - inode->i_mapping->a_ops = &sysfs_aops; 244 - inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info; 245 - inode->i_op = &sysfs_inode_operations; 246 - 247 - set_default_inode_attr(inode, sd->s_mode); 248 - sysfs_refresh_inode(sd, inode); 249 - 250 - /* initialize inode according to type */ 251 - switch (sysfs_type(sd)) { 252 - case SYSFS_DIR: 253 - inode->i_op = &sysfs_dir_inode_operations; 254 - inode->i_fop = &sysfs_dir_operations; 255 - break; 256 - case SYSFS_KOBJ_ATTR: 257 - inode->i_size = PAGE_SIZE; 258 - inode->i_fop = &sysfs_file_operations; 259 - break; 260 - case SYSFS_KOBJ_BIN_ATTR: 261 - bin_attr = sd->s_attr.bin_attr; 262 - inode->i_size = bin_attr->size; 263 - inode->i_fop = &sysfs_bin_operations; 264 - break; 265 - case SYSFS_KOBJ_LINK: 266 - inode->i_op = &sysfs_symlink_inode_operations; 267 - break; 268 - default: 269 - BUG(); 270 - } 271 - 272 - unlock_new_inode(inode); 273 - } 274 - 275 - /** 276 - * sysfs_get_inode - get inode for sysfs_dirent 277 - * @sb: super block 278 - * @sd: sysfs_dirent to allocate inode for 279 - * 280 - * Get inode for @sd. If such inode doesn't exist, a new inode 281 - * is allocated and basics are initialized. New inode is 282 - * returned locked. 283 - * 284 - * LOCKING: 285 - * Kernel thread context (may sleep). 286 - * 287 - * RETURNS: 288 - * Pointer to allocated inode on success, NULL on failure. 289 - */ 290 - struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd) 291 - { 292 - struct inode *inode; 293 - 294 - inode = iget_locked(sb, sd->s_ino); 295 - if (inode && (inode->i_state & I_NEW)) 296 - sysfs_init_inode(sd, inode); 297 - 298 - return inode; 299 - } 300 - 301 - /* 302 - * The sysfs_dirent serves as both an inode and a directory entry for sysfs. 303 - * To prevent the sysfs inode numbers from being freed prematurely we take a 304 - * reference to sysfs_dirent from the sysfs inode. A 305 - * super_operations.evict_inode() implementation is needed to drop that 306 - * reference upon inode destruction. 307 - */ 308 - void sysfs_evict_inode(struct inode *inode) 309 - { 310 - struct sysfs_dirent *sd = inode->i_private; 311 - 312 - truncate_inode_pages(&inode->i_data, 0); 313 - clear_inode(inode); 314 - sysfs_put(sd); 315 - } 316 - 317 - int sysfs_permission(struct inode *inode, int mask) 318 - { 319 - struct sysfs_dirent *sd; 320 - 321 - if (mask & MAY_NOT_BLOCK) 322 - return -ECHILD; 323 - 324 - sd = inode->i_private; 325 - 326 - mutex_lock(&sysfs_mutex); 327 - sysfs_refresh_inode(sd, inode); 328 - mutex_unlock(&sysfs_mutex); 329 - 330 - return generic_permission(inode, mask); 331 - }
+25 -159
fs/sysfs/mount.c
··· 14 14 15 15 #include <linux/fs.h> 16 16 #include <linux/mount.h> 17 - #include <linux/pagemap.h> 18 17 #include <linux/init.h> 19 - #include <linux/module.h> 20 - #include <linux/magic.h> 21 - #include <linux/slab.h> 22 18 #include <linux/user_namespace.h> 23 19 24 20 #include "sysfs.h" 25 21 26 - 27 - static struct vfsmount *sysfs_mnt; 28 - struct kmem_cache *sysfs_dir_cachep; 29 - 30 - static const struct super_operations sysfs_ops = { 31 - .statfs = simple_statfs, 32 - .drop_inode = generic_delete_inode, 33 - .evict_inode = sysfs_evict_inode, 34 - }; 35 - 36 - struct sysfs_dirent sysfs_root = { 37 - .s_name = "", 38 - .s_count = ATOMIC_INIT(1), 39 - .s_flags = SYSFS_DIR | (KOBJ_NS_TYPE_NONE << SYSFS_NS_TYPE_SHIFT), 40 - .s_mode = S_IFDIR | S_IRUGO | S_IXUGO, 41 - .s_ino = 1, 42 - }; 43 - 44 - static int sysfs_fill_super(struct super_block *sb, void *data, int silent) 45 - { 46 - struct inode *inode; 47 - struct dentry *root; 48 - 49 - sb->s_blocksize = PAGE_CACHE_SIZE; 50 - sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 51 - sb->s_magic = SYSFS_MAGIC; 52 - sb->s_op = &sysfs_ops; 53 - sb->s_time_gran = 1; 54 - 55 - /* get root inode, initialize and unlock it */ 56 - mutex_lock(&sysfs_mutex); 57 - inode = sysfs_get_inode(sb, &sysfs_root); 58 - mutex_unlock(&sysfs_mutex); 59 - if (!inode) { 60 - pr_debug("sysfs: could not get root inode\n"); 61 - return -ENOMEM; 62 - } 63 - 64 - /* instantiate and link root dentry */ 65 - root = d_make_root(inode); 66 - if (!root) { 67 - pr_debug("%s: could not get root dentry!\n", __func__); 68 - return -ENOMEM; 69 - } 70 - root->d_fsdata = &sysfs_root; 71 - sb->s_root = root; 72 - sb->s_d_op = &sysfs_dentry_ops; 73 - return 0; 74 - } 75 - 76 - static int sysfs_test_super(struct super_block *sb, void *data) 77 - { 78 - struct sysfs_super_info *sb_info = sysfs_info(sb); 79 - struct sysfs_super_info *info = data; 80 - enum kobj_ns_type type; 81 - int found = 1; 82 - 83 - for (type = KOBJ_NS_TYPE_NONE; type < KOBJ_NS_TYPES; type++) { 84 - if (sb_info->ns[type] != info->ns[type]) 85 - found = 0; 86 - } 87 - return found; 88 - } 89 - 90 - static int sysfs_set_super(struct super_block *sb, void *data) 91 - { 92 - int error; 93 - error = set_anon_super(sb, data); 94 - if (!error) 95 - sb->s_fs_info = data; 96 - return error; 97 - } 98 - 99 - static void free_sysfs_super_info(struct sysfs_super_info *info) 100 - { 101 - int type; 102 - for (type = KOBJ_NS_TYPE_NONE; type < KOBJ_NS_TYPES; type++) 103 - kobj_ns_drop(type, info->ns[type]); 104 - kfree(info); 105 - } 22 + static struct kernfs_root *sysfs_root; 23 + struct kernfs_node *sysfs_root_kn; 106 24 107 25 static struct dentry *sysfs_mount(struct file_system_type *fs_type, 108 26 int flags, const char *dev_name, void *data) 109 27 { 110 - struct sysfs_super_info *info; 111 - enum kobj_ns_type type; 112 - struct super_block *sb; 113 - int error; 28 + struct dentry *root; 29 + void *ns; 114 30 115 31 if (!(flags & MS_KERNMOUNT)) { 116 32 if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type)) 117 33 return ERR_PTR(-EPERM); 118 34 119 - for (type = KOBJ_NS_TYPE_NONE; type < KOBJ_NS_TYPES; type++) { 120 - if (!kobj_ns_current_may_mount(type)) 121 - return ERR_PTR(-EPERM); 122 - } 35 + if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET)) 36 + return ERR_PTR(-EPERM); 123 37 } 124 38 125 - info = kzalloc(sizeof(*info), GFP_KERNEL); 126 - if (!info) 127 - return ERR_PTR(-ENOMEM); 128 - 129 - for (type = KOBJ_NS_TYPE_NONE; type < KOBJ_NS_TYPES; type++) 130 - info->ns[type] = kobj_ns_grab_current(type); 131 - 132 - sb = sget(fs_type, sysfs_test_super, sysfs_set_super, flags, info); 133 - if (IS_ERR(sb) || sb->s_fs_info != info) 134 - free_sysfs_super_info(info); 135 - if (IS_ERR(sb)) 136 - return ERR_CAST(sb); 137 - if (!sb->s_root) { 138 - error = sysfs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0); 139 - if (error) { 140 - deactivate_locked_super(sb); 141 - return ERR_PTR(error); 142 - } 143 - sb->s_flags |= MS_ACTIVE; 144 - } 145 - 146 - return dget(sb->s_root); 39 + ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET); 40 + root = kernfs_mount_ns(fs_type, flags, sysfs_root, ns); 41 + if (IS_ERR(root)) 42 + kobj_ns_drop(KOBJ_NS_TYPE_NET, ns); 43 + return root; 147 44 } 148 45 149 46 static void sysfs_kill_sb(struct super_block *sb) 150 47 { 151 - struct sysfs_super_info *info = sysfs_info(sb); 152 - /* Remove the superblock from fs_supers/s_instances 153 - * so we can't find it, before freeing sysfs_super_info. 154 - */ 155 - kill_anon_super(sb); 156 - free_sysfs_super_info(info); 48 + void *ns = (void *)kernfs_super_ns(sb); 49 + 50 + kernfs_kill_sb(sb); 51 + kobj_ns_drop(KOBJ_NS_TYPE_NET, ns); 157 52 } 158 53 159 54 static struct file_system_type sysfs_fs_type = { ··· 60 165 61 166 int __init sysfs_init(void) 62 167 { 63 - int err = -ENOMEM; 168 + int err; 64 169 65 - sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache", 66 - sizeof(struct sysfs_dirent), 67 - 0, 0, NULL); 68 - if (!sysfs_dir_cachep) 69 - goto out; 170 + sysfs_root = kernfs_create_root(NULL, NULL); 171 + if (IS_ERR(sysfs_root)) 172 + return PTR_ERR(sysfs_root); 70 173 71 - err = sysfs_inode_init(); 72 - if (err) 73 - goto out_err; 174 + sysfs_root_kn = sysfs_root->kn; 74 175 75 176 err = register_filesystem(&sysfs_fs_type); 76 - if (!err) { 77 - sysfs_mnt = kern_mount(&sysfs_fs_type); 78 - if (IS_ERR(sysfs_mnt)) { 79 - printk(KERN_ERR "sysfs: could not mount!\n"); 80 - err = PTR_ERR(sysfs_mnt); 81 - sysfs_mnt = NULL; 82 - unregister_filesystem(&sysfs_fs_type); 83 - goto out_err; 84 - } 85 - } else 86 - goto out_err; 87 - out: 88 - return err; 89 - out_err: 90 - kmem_cache_destroy(sysfs_dir_cachep); 91 - sysfs_dir_cachep = NULL; 92 - goto out; 93 - } 177 + if (err) { 178 + kernfs_destroy_root(sysfs_root); 179 + return err; 180 + } 94 181 95 - #undef sysfs_get 96 - struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd) 97 - { 98 - return __sysfs_get(sd); 182 + return 0; 99 183 } 100 - EXPORT_SYMBOL_GPL(sysfs_get); 101 - 102 - #undef sysfs_put 103 - void sysfs_put(struct sysfs_dirent *sd) 104 - { 105 - __sysfs_put(sd); 106 - } 107 - EXPORT_SYMBOL_GPL(sysfs_put);
+43 -176
fs/sysfs/symlink.c
··· 11 11 */ 12 12 13 13 #include <linux/fs.h> 14 - #include <linux/gfp.h> 15 - #include <linux/mount.h> 16 14 #include <linux/module.h> 17 15 #include <linux/kobject.h> 18 - #include <linux/namei.h> 19 16 #include <linux/mutex.h> 20 17 #include <linux/security.h> 21 18 22 19 #include "sysfs.h" 23 20 24 - static int sysfs_do_create_link_sd(struct sysfs_dirent *parent_sd, 25 - struct kobject *target, 21 + static int sysfs_do_create_link_sd(struct kernfs_node *parent, 22 + struct kobject *target_kobj, 26 23 const char *name, int warn) 27 24 { 28 - struct sysfs_dirent *target_sd = NULL; 29 - struct sysfs_dirent *sd = NULL; 30 - struct sysfs_addrm_cxt acxt; 31 - enum kobj_ns_type ns_type; 32 - int error; 25 + struct kernfs_node *kn, *target = NULL; 33 26 34 - BUG_ON(!name || !parent_sd); 27 + BUG_ON(!name || !parent); 35 28 36 29 /* 37 - * We don't own @target and it may be removed at any time. 30 + * We don't own @target_kobj and it may be removed at any time. 38 31 * Synchronize using sysfs_symlink_target_lock. See 39 32 * sysfs_remove_dir() for details. 40 33 */ 41 34 spin_lock(&sysfs_symlink_target_lock); 42 - if (target->sd) 43 - target_sd = sysfs_get(target->sd); 35 + if (target_kobj->sd) { 36 + target = target_kobj->sd; 37 + kernfs_get(target); 38 + } 44 39 spin_unlock(&sysfs_symlink_target_lock); 45 40 46 - error = -ENOENT; 47 - if (!target_sd) 48 - goto out_put; 41 + if (!target) 42 + return -ENOENT; 49 43 50 - error = -ENOMEM; 51 - sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK); 52 - if (!sd) 53 - goto out_put; 44 + kn = kernfs_create_link(parent, name, target); 45 + kernfs_put(target); 54 46 55 - ns_type = sysfs_ns_type(parent_sd); 56 - if (ns_type) 57 - sd->s_ns = target_sd->s_ns; 58 - sd->s_symlink.target_sd = target_sd; 59 - target_sd = NULL; /* reference is now owned by the symlink */ 47 + if (!IS_ERR(kn)) 48 + return 0; 60 49 61 - sysfs_addrm_start(&acxt); 62 - /* Symlinks must be between directories with the same ns_type */ 63 - if (!ns_type || 64 - (ns_type == sysfs_ns_type(sd->s_symlink.target_sd->s_parent))) { 65 - if (warn) 66 - error = sysfs_add_one(&acxt, sd, parent_sd); 67 - else 68 - error = __sysfs_add_one(&acxt, sd, parent_sd); 69 - } else { 70 - error = -EINVAL; 71 - WARN(1, KERN_WARNING 72 - "sysfs: symlink across ns_types %s/%s -> %s/%s\n", 73 - parent_sd->s_name, 74 - sd->s_name, 75 - sd->s_symlink.target_sd->s_parent->s_name, 76 - sd->s_symlink.target_sd->s_name); 77 - } 78 - sysfs_addrm_finish(&acxt); 79 - 80 - if (error) 81 - goto out_put; 82 - 83 - return 0; 84 - 85 - out_put: 86 - sysfs_put(target_sd); 87 - sysfs_put(sd); 88 - return error; 50 + if (warn && PTR_ERR(kn) == -EEXIST) 51 + sysfs_warn_dup(parent, name); 52 + return PTR_ERR(kn); 89 53 } 90 54 91 55 /** 92 56 * sysfs_create_link_sd - create symlink to a given object. 93 - * @sd: directory we're creating the link in. 57 + * @kn: directory we're creating the link in. 94 58 * @target: object we're pointing to. 95 59 * @name: name of the symlink. 96 60 */ 97 - int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target, 61 + int sysfs_create_link_sd(struct kernfs_node *kn, struct kobject *target, 98 62 const char *name) 99 63 { 100 - return sysfs_do_create_link_sd(sd, target, name, 1); 64 + return sysfs_do_create_link_sd(kn, target, name, 1); 101 65 } 102 66 103 67 static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target, 104 68 const char *name, int warn) 105 69 { 106 - struct sysfs_dirent *parent_sd = NULL; 70 + struct kernfs_node *parent = NULL; 107 71 108 72 if (!kobj) 109 - parent_sd = &sysfs_root; 73 + parent = sysfs_root_kn; 110 74 else 111 - parent_sd = kobj->sd; 75 + parent = kobj->sd; 112 76 113 - if (!parent_sd) 77 + if (!parent) 114 78 return -EFAULT; 115 79 116 - return sysfs_do_create_link_sd(parent_sd, target, name, warn); 80 + return sysfs_do_create_link_sd(parent, target, name, warn); 117 81 } 118 82 119 83 /** ··· 128 164 * sysfs_remove_dir() for details. 129 165 */ 130 166 spin_lock(&sysfs_symlink_target_lock); 131 - if (targ->sd && sysfs_ns_type(kobj->sd)) 132 - ns = targ->sd->s_ns; 167 + if (targ->sd && kernfs_ns_enabled(kobj->sd)) 168 + ns = targ->sd->ns; 133 169 spin_unlock(&sysfs_symlink_target_lock); 134 - sysfs_hash_and_remove(kobj->sd, name, ns); 170 + kernfs_remove_by_name_ns(kobj->sd, name, ns); 135 171 } 136 172 137 173 /** ··· 141 177 */ 142 178 void sysfs_remove_link(struct kobject *kobj, const char *name) 143 179 { 144 - struct sysfs_dirent *parent_sd = NULL; 180 + struct kernfs_node *parent = NULL; 145 181 146 182 if (!kobj) 147 - parent_sd = &sysfs_root; 183 + parent = sysfs_root_kn; 148 184 else 149 - parent_sd = kobj->sd; 185 + parent = kobj->sd; 150 186 151 - sysfs_hash_and_remove(parent_sd, name, NULL); 187 + kernfs_remove_by_name(parent, name); 152 188 } 153 189 EXPORT_SYMBOL_GPL(sysfs_remove_link); 154 190 ··· 165 201 int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ, 166 202 const char *old, const char *new, const void *new_ns) 167 203 { 168 - struct sysfs_dirent *parent_sd, *sd = NULL; 204 + struct kernfs_node *parent, *kn = NULL; 169 205 const void *old_ns = NULL; 170 206 int result; 171 207 172 208 if (!kobj) 173 - parent_sd = &sysfs_root; 209 + parent = sysfs_root_kn; 174 210 else 175 - parent_sd = kobj->sd; 211 + parent = kobj->sd; 176 212 177 213 if (targ->sd) 178 - old_ns = targ->sd->s_ns; 214 + old_ns = targ->sd->ns; 179 215 180 216 result = -ENOENT; 181 - sd = sysfs_get_dirent_ns(parent_sd, old, old_ns); 182 - if (!sd) 217 + kn = kernfs_find_and_get_ns(parent, old, old_ns); 218 + if (!kn) 183 219 goto out; 184 220 185 221 result = -EINVAL; 186 - if (sysfs_type(sd) != SYSFS_KOBJ_LINK) 222 + if (kernfs_type(kn) != KERNFS_LINK) 187 223 goto out; 188 - if (sd->s_symlink.target_sd->s_dir.kobj != targ) 224 + if (kn->symlink.target_kn->priv != targ) 189 225 goto out; 190 226 191 - result = sysfs_rename(sd, parent_sd, new, new_ns); 227 + result = kernfs_rename_ns(kn, parent, new, new_ns); 192 228 193 229 out: 194 - sysfs_put(sd); 230 + kernfs_put(kn); 195 231 return result; 196 232 } 197 233 EXPORT_SYMBOL_GPL(sysfs_rename_link_ns); 198 - 199 - static int sysfs_get_target_path(struct sysfs_dirent *parent_sd, 200 - struct sysfs_dirent *target_sd, char *path) 201 - { 202 - struct sysfs_dirent *base, *sd; 203 - char *s = path; 204 - int len = 0; 205 - 206 - /* go up to the root, stop at the base */ 207 - base = parent_sd; 208 - while (base->s_parent) { 209 - sd = target_sd->s_parent; 210 - while (sd->s_parent && base != sd) 211 - sd = sd->s_parent; 212 - 213 - if (base == sd) 214 - break; 215 - 216 - strcpy(s, "../"); 217 - s += 3; 218 - base = base->s_parent; 219 - } 220 - 221 - /* determine end of target string for reverse fillup */ 222 - sd = target_sd; 223 - while (sd->s_parent && sd != base) { 224 - len += strlen(sd->s_name) + 1; 225 - sd = sd->s_parent; 226 - } 227 - 228 - /* check limits */ 229 - if (len < 2) 230 - return -EINVAL; 231 - len--; 232 - if ((s - path) + len > PATH_MAX) 233 - return -ENAMETOOLONG; 234 - 235 - /* reverse fillup of target string from target to base */ 236 - sd = target_sd; 237 - while (sd->s_parent && sd != base) { 238 - int slen = strlen(sd->s_name); 239 - 240 - len -= slen; 241 - strncpy(s + len, sd->s_name, slen); 242 - if (len) 243 - s[--len] = '/'; 244 - 245 - sd = sd->s_parent; 246 - } 247 - 248 - return 0; 249 - } 250 - 251 - static int sysfs_getlink(struct dentry *dentry, char *path) 252 - { 253 - struct sysfs_dirent *sd = dentry->d_fsdata; 254 - struct sysfs_dirent *parent_sd = sd->s_parent; 255 - struct sysfs_dirent *target_sd = sd->s_symlink.target_sd; 256 - int error; 257 - 258 - mutex_lock(&sysfs_mutex); 259 - error = sysfs_get_target_path(parent_sd, target_sd, path); 260 - mutex_unlock(&sysfs_mutex); 261 - 262 - return error; 263 - } 264 - 265 - static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd) 266 - { 267 - int error = -ENOMEM; 268 - unsigned long page = get_zeroed_page(GFP_KERNEL); 269 - if (page) { 270 - error = sysfs_getlink(dentry, (char *) page); 271 - if (error < 0) 272 - free_page((unsigned long)page); 273 - } 274 - nd_set_link(nd, error ? ERR_PTR(error) : (char *)page); 275 - return NULL; 276 - } 277 - 278 - static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, 279 - void *cookie) 280 - { 281 - char *page = nd_get_link(nd); 282 - if (!IS_ERR(page)) 283 - free_page((unsigned long)page); 284 - } 285 - 286 - const struct inode_operations sysfs_symlink_inode_operations = { 287 - .setxattr = sysfs_setxattr, 288 - .readlink = generic_readlink, 289 - .follow_link = sysfs_follow_link, 290 - .put_link = sysfs_put_link, 291 - .setattr = sysfs_setattr, 292 - .getattr = sysfs_getattr, 293 - .permission = sysfs_permission, 294 - };
+12 -224
fs/sysfs/sysfs.h
··· 8 8 * This file is released under the GPLv2. 9 9 */ 10 10 11 - #include <linux/lockdep.h> 12 - #include <linux/kobject_ns.h> 13 - #include <linux/fs.h> 14 - #include <linux/rbtree.h> 11 + #ifndef __SYSFS_INTERNAL_H 12 + #define __SYSFS_INTERNAL_H 15 13 16 - struct sysfs_open_dirent; 17 - 18 - /* type-specific structures for sysfs_dirent->s_* union members */ 19 - struct sysfs_elem_dir { 20 - struct kobject *kobj; 21 - 22 - unsigned long subdirs; 23 - /* children rbtree starts here and goes through sd->s_rb */ 24 - struct rb_root children; 25 - }; 26 - 27 - struct sysfs_elem_symlink { 28 - struct sysfs_dirent *target_sd; 29 - }; 30 - 31 - struct sysfs_elem_attr { 32 - union { 33 - struct attribute *attr; 34 - struct bin_attribute *bin_attr; 35 - }; 36 - struct sysfs_open_dirent *open; 37 - }; 38 - 39 - struct sysfs_inode_attrs { 40 - struct iattr ia_iattr; 41 - void *ia_secdata; 42 - u32 ia_secdata_len; 43 - }; 44 - 45 - /* 46 - * sysfs_dirent - the building block of sysfs hierarchy. Each and 47 - * every sysfs node is represented by single sysfs_dirent. 48 - * 49 - * As long as s_count reference is held, the sysfs_dirent itself is 50 - * accessible. Dereferencing s_elem or any other outer entity 51 - * requires s_active reference. 52 - */ 53 - struct sysfs_dirent { 54 - atomic_t s_count; 55 - atomic_t s_active; 56 - #ifdef CONFIG_DEBUG_LOCK_ALLOC 57 - struct lockdep_map dep_map; 58 - #endif 59 - struct sysfs_dirent *s_parent; 60 - const char *s_name; 61 - 62 - struct rb_node s_rb; 63 - 64 - union { 65 - struct completion *completion; 66 - struct sysfs_dirent *removed_list; 67 - } u; 68 - 69 - const void *s_ns; /* namespace tag */ 70 - unsigned int s_hash; /* ns + name hash */ 71 - union { 72 - struct sysfs_elem_dir s_dir; 73 - struct sysfs_elem_symlink s_symlink; 74 - struct sysfs_elem_attr s_attr; 75 - }; 76 - 77 - unsigned short s_flags; 78 - umode_t s_mode; 79 - unsigned int s_ino; 80 - struct sysfs_inode_attrs *s_iattr; 81 - }; 82 - 83 - #define SD_DEACTIVATED_BIAS INT_MIN 84 - 85 - #define SYSFS_TYPE_MASK 0x00ff 86 - #define SYSFS_DIR 0x0001 87 - #define SYSFS_KOBJ_ATTR 0x0002 88 - #define SYSFS_KOBJ_BIN_ATTR 0x0004 89 - #define SYSFS_KOBJ_LINK 0x0008 90 - #define SYSFS_COPY_NAME (SYSFS_DIR | SYSFS_KOBJ_LINK) 91 - #define SYSFS_ACTIVE_REF (SYSFS_KOBJ_ATTR | SYSFS_KOBJ_BIN_ATTR) 92 - 93 - /* identify any namespace tag on sysfs_dirents */ 94 - #define SYSFS_NS_TYPE_MASK 0xf00 95 - #define SYSFS_NS_TYPE_SHIFT 8 96 - 97 - #define SYSFS_FLAG_MASK ~(SYSFS_NS_TYPE_MASK|SYSFS_TYPE_MASK) 98 - #define SYSFS_FLAG_REMOVED 0x02000 99 - 100 - static inline unsigned int sysfs_type(struct sysfs_dirent *sd) 101 - { 102 - return sd->s_flags & SYSFS_TYPE_MASK; 103 - } 104 - 105 - /* 106 - * Return any namespace tags on this dirent. 107 - * enum kobj_ns_type is defined in linux/kobject.h 108 - */ 109 - static inline enum kobj_ns_type sysfs_ns_type(struct sysfs_dirent *sd) 110 - { 111 - return (sd->s_flags & SYSFS_NS_TYPE_MASK) >> SYSFS_NS_TYPE_SHIFT; 112 - } 113 - 114 - #ifdef CONFIG_DEBUG_LOCK_ALLOC 115 - 116 - #define sysfs_dirent_init_lockdep(sd) \ 117 - do { \ 118 - struct attribute *attr = sd->s_attr.attr; \ 119 - struct lock_class_key *key = attr->key; \ 120 - if (!key) \ 121 - key = &attr->skey; \ 122 - \ 123 - lockdep_init_map(&sd->dep_map, "s_active", key, 0); \ 124 - } while (0) 125 - 126 - /* Test for attributes that want to ignore lockdep for read-locking */ 127 - static inline bool sysfs_ignore_lockdep(struct sysfs_dirent *sd) 128 - { 129 - int type = sysfs_type(sd); 130 - 131 - return (type == SYSFS_KOBJ_ATTR || type == SYSFS_KOBJ_BIN_ATTR) && 132 - sd->s_attr.attr->ignore_lockdep; 133 - } 134 - 135 - #else 136 - 137 - #define sysfs_dirent_init_lockdep(sd) do {} while (0) 138 - 139 - static inline bool sysfs_ignore_lockdep(struct sysfs_dirent *sd) 140 - { 141 - return true; 142 - } 143 - 144 - #endif 145 - 146 - /* 147 - * Context structure to be used while adding/removing nodes. 148 - */ 149 - struct sysfs_addrm_cxt { 150 - struct sysfs_dirent *removed; 151 - }; 14 + #include <linux/sysfs.h> 152 15 153 16 /* 154 17 * mount.c 155 18 */ 156 - 157 - /* 158 - * Each sb is associated with a set of namespace tags (i.e. 159 - * the network namespace of the task which mounted this sysfs 160 - * instance). 161 - */ 162 - struct sysfs_super_info { 163 - void *ns[KOBJ_NS_TYPES]; 164 - }; 165 - #define sysfs_info(SB) ((struct sysfs_super_info *)(SB->s_fs_info)) 166 - extern struct sysfs_dirent sysfs_root; 167 - extern struct kmem_cache *sysfs_dir_cachep; 19 + extern struct kernfs_node *sysfs_root_kn; 168 20 169 21 /* 170 22 * dir.c 171 23 */ 172 - extern struct mutex sysfs_mutex; 173 24 extern spinlock_t sysfs_symlink_target_lock; 174 - extern const struct dentry_operations sysfs_dentry_ops; 175 25 176 - extern const struct file_operations sysfs_dir_operations; 177 - extern const struct inode_operations sysfs_dir_inode_operations; 178 - 179 - struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd); 180 - void sysfs_put_active(struct sysfs_dirent *sd); 181 - void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt); 182 - void sysfs_warn_dup(struct sysfs_dirent *parent, const char *name); 183 - int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd, 184 - struct sysfs_dirent *parent_sd); 185 - int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd, 186 - struct sysfs_dirent *parent_sd); 187 - void sysfs_remove(struct sysfs_dirent *sd); 188 - int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name, 189 - const void *ns); 190 - void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt); 191 - 192 - struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd, 193 - const unsigned char *name, 194 - const void *ns); 195 - struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type); 196 - 197 - void release_sysfs_dirent(struct sysfs_dirent *sd); 198 - 199 - int sysfs_create_subdir(struct kobject *kobj, const char *name, 200 - struct sysfs_dirent **p_sd); 201 - 202 - int sysfs_rename(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent_sd, 203 - const char *new_name, const void *new_ns); 204 - 205 - static inline struct sysfs_dirent *__sysfs_get(struct sysfs_dirent *sd) 206 - { 207 - if (sd) { 208 - WARN_ON(!atomic_read(&sd->s_count)); 209 - atomic_inc(&sd->s_count); 210 - } 211 - return sd; 212 - } 213 - #define sysfs_get(sd) __sysfs_get(sd) 214 - 215 - static inline void __sysfs_put(struct sysfs_dirent *sd) 216 - { 217 - if (sd && atomic_dec_and_test(&sd->s_count)) 218 - release_sysfs_dirent(sd); 219 - } 220 - #define sysfs_put(sd) __sysfs_put(sd) 221 - 222 - /* 223 - * inode.c 224 - */ 225 - struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd); 226 - void sysfs_evict_inode(struct inode *inode); 227 - int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr *iattr); 228 - int sysfs_permission(struct inode *inode, int mask); 229 - int sysfs_setattr(struct dentry *dentry, struct iattr *iattr); 230 - int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, 231 - struct kstat *stat); 232 - int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value, 233 - size_t size, int flags); 234 - int sysfs_inode_init(void); 26 + void sysfs_warn_dup(struct kernfs_node *parent, const char *name); 235 27 236 28 /* 237 29 * file.c 238 30 */ 239 - extern const struct file_operations sysfs_file_operations; 240 - extern const struct file_operations sysfs_bin_operations; 241 - 242 - int sysfs_add_file(struct sysfs_dirent *dir_sd, 243 - const struct attribute *attr, int type); 244 - 245 - int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd, 246 - const struct attribute *attr, int type, 31 + int sysfs_add_file(struct kernfs_node *parent, 32 + const struct attribute *attr, bool is_bin); 33 + int sysfs_add_file_mode_ns(struct kernfs_node *parent, 34 + const struct attribute *attr, bool is_bin, 247 35 umode_t amode, const void *ns); 248 - void sysfs_unmap_bin_file(struct sysfs_dirent *sd); 249 36 250 37 /* 251 38 * symlink.c 252 39 */ 253 - extern const struct inode_operations sysfs_symlink_inode_operations; 254 - int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target, 40 + int sysfs_create_link_sd(struct kernfs_node *kn, struct kobject *target, 255 41 const char *name); 42 + 43 + #endif /* __SYSFS_INTERNAL_H */
+32
include/linux/component.h
··· 1 + #ifndef COMPONENT_H 2 + #define COMPONENT_H 3 + 4 + struct device; 5 + 6 + struct component_ops { 7 + int (*bind)(struct device *, struct device *, void *); 8 + void (*unbind)(struct device *, struct device *, void *); 9 + }; 10 + 11 + int component_add(struct device *, const struct component_ops *); 12 + void component_del(struct device *, const struct component_ops *); 13 + 14 + int component_bind_all(struct device *, void *); 15 + void component_unbind_all(struct device *, void *); 16 + 17 + struct master; 18 + 19 + struct component_master_ops { 20 + int (*add_components)(struct device *, struct master *); 21 + int (*bind)(struct device *); 22 + void (*unbind)(struct device *); 23 + }; 24 + 25 + int component_master_add(struct device *, const struct component_master_ops *); 26 + void component_master_del(struct device *, 27 + const struct component_master_ops *); 28 + 29 + int component_master_add_child(struct master *master, 30 + int (*compare)(struct device *, void *), void *compare_data); 31 + 32 + #endif
+7
include/linux/firmware.h
··· 68 68 69 69 #endif 70 70 71 + #ifdef CONFIG_FW_LOADER_USER_HELPER 72 + int request_firmware_direct(const struct firmware **fw, const char *name, 73 + struct device *device); 74 + #else 75 + #define request_firmware_direct request_firmware 76 + #endif 77 + 71 78 #endif
+376
include/linux/kernfs.h
··· 1 + /* 2 + * kernfs.h - pseudo filesystem decoupled from vfs locking 3 + * 4 + * This file is released under the GPLv2. 5 + */ 6 + 7 + #ifndef __LINUX_KERNFS_H 8 + #define __LINUX_KERNFS_H 9 + 10 + #include <linux/kernel.h> 11 + #include <linux/err.h> 12 + #include <linux/list.h> 13 + #include <linux/mutex.h> 14 + #include <linux/idr.h> 15 + #include <linux/lockdep.h> 16 + #include <linux/rbtree.h> 17 + #include <linux/atomic.h> 18 + #include <linux/completion.h> 19 + 20 + struct file; 21 + struct dentry; 22 + struct iattr; 23 + struct seq_file; 24 + struct vm_area_struct; 25 + struct super_block; 26 + struct file_system_type; 27 + 28 + struct kernfs_open_node; 29 + struct kernfs_iattrs; 30 + 31 + enum kernfs_node_type { 32 + KERNFS_DIR = 0x0001, 33 + KERNFS_FILE = 0x0002, 34 + KERNFS_LINK = 0x0004, 35 + }; 36 + 37 + #define KERNFS_TYPE_MASK 0x000f 38 + #define KERNFS_ACTIVE_REF KERNFS_FILE 39 + #define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK 40 + 41 + enum kernfs_node_flag { 42 + KERNFS_REMOVED = 0x0010, 43 + KERNFS_NS = 0x0020, 44 + KERNFS_HAS_SEQ_SHOW = 0x0040, 45 + KERNFS_HAS_MMAP = 0x0080, 46 + KERNFS_LOCKDEP = 0x0100, 47 + KERNFS_STATIC_NAME = 0x0200, 48 + }; 49 + 50 + /* type-specific structures for kernfs_node union members */ 51 + struct kernfs_elem_dir { 52 + unsigned long subdirs; 53 + /* children rbtree starts here and goes through kn->rb */ 54 + struct rb_root children; 55 + 56 + /* 57 + * The kernfs hierarchy this directory belongs to. This fits 58 + * better directly in kernfs_node but is here to save space. 59 + */ 60 + struct kernfs_root *root; 61 + }; 62 + 63 + struct kernfs_elem_symlink { 64 + struct kernfs_node *target_kn; 65 + }; 66 + 67 + struct kernfs_elem_attr { 68 + const struct kernfs_ops *ops; 69 + struct kernfs_open_node *open; 70 + loff_t size; 71 + }; 72 + 73 + /* 74 + * kernfs_node - the building block of kernfs hierarchy. Each and every 75 + * kernfs node is represented by single kernfs_node. Most fields are 76 + * private to kernfs and shouldn't be accessed directly by kernfs users. 77 + * 78 + * As long as s_count reference is held, the kernfs_node itself is 79 + * accessible. Dereferencing elem or any other outer entity requires 80 + * active reference. 81 + */ 82 + struct kernfs_node { 83 + atomic_t count; 84 + atomic_t active; 85 + #ifdef CONFIG_DEBUG_LOCK_ALLOC 86 + struct lockdep_map dep_map; 87 + #endif 88 + /* the following two fields are published */ 89 + struct kernfs_node *parent; 90 + const char *name; 91 + 92 + struct rb_node rb; 93 + 94 + union { 95 + struct completion *completion; 96 + struct kernfs_node *removed_list; 97 + } u; 98 + 99 + const void *ns; /* namespace tag */ 100 + unsigned int hash; /* ns + name hash */ 101 + union { 102 + struct kernfs_elem_dir dir; 103 + struct kernfs_elem_symlink symlink; 104 + struct kernfs_elem_attr attr; 105 + }; 106 + 107 + void *priv; 108 + 109 + unsigned short flags; 110 + umode_t mode; 111 + unsigned int ino; 112 + struct kernfs_iattrs *iattr; 113 + }; 114 + 115 + /* 116 + * kernfs_dir_ops may be specified on kernfs_create_root() to support 117 + * directory manipulation syscalls. These optional callbacks are invoked 118 + * on the matching syscalls and can perform any kernfs operations which 119 + * don't necessarily have to be the exact operation requested. 120 + */ 121 + struct kernfs_dir_ops { 122 + int (*mkdir)(struct kernfs_node *parent, const char *name, 123 + umode_t mode); 124 + int (*rmdir)(struct kernfs_node *kn); 125 + int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent, 126 + const char *new_name); 127 + }; 128 + 129 + struct kernfs_root { 130 + /* published fields */ 131 + struct kernfs_node *kn; 132 + 133 + /* private fields, do not use outside kernfs proper */ 134 + struct ida ino_ida; 135 + struct kernfs_dir_ops *dir_ops; 136 + }; 137 + 138 + struct kernfs_open_file { 139 + /* published fields */ 140 + struct kernfs_node *kn; 141 + struct file *file; 142 + 143 + /* private fields, do not use outside kernfs proper */ 144 + struct mutex mutex; 145 + int event; 146 + struct list_head list; 147 + 148 + bool mmapped; 149 + const struct vm_operations_struct *vm_ops; 150 + }; 151 + 152 + struct kernfs_ops { 153 + /* 154 + * Read is handled by either seq_file or raw_read(). 155 + * 156 + * If seq_show() is present, seq_file path is active. Other seq 157 + * operations are optional and if not implemented, the behavior is 158 + * equivalent to single_open(). @sf->private points to the 159 + * associated kernfs_open_file. 160 + * 161 + * read() is bounced through kernel buffer and a read larger than 162 + * PAGE_SIZE results in partial operation of PAGE_SIZE. 163 + */ 164 + int (*seq_show)(struct seq_file *sf, void *v); 165 + 166 + void *(*seq_start)(struct seq_file *sf, loff_t *ppos); 167 + void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos); 168 + void (*seq_stop)(struct seq_file *sf, void *v); 169 + 170 + ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes, 171 + loff_t off); 172 + 173 + /* 174 + * write() is bounced through kernel buffer and a write larger than 175 + * PAGE_SIZE results in partial operation of PAGE_SIZE. 176 + */ 177 + ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes, 178 + loff_t off); 179 + 180 + int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma); 181 + 182 + #ifdef CONFIG_DEBUG_LOCK_ALLOC 183 + struct lock_class_key lockdep_key; 184 + #endif 185 + }; 186 + 187 + #ifdef CONFIG_SYSFS 188 + 189 + static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn) 190 + { 191 + return kn->flags & KERNFS_TYPE_MASK; 192 + } 193 + 194 + /** 195 + * kernfs_enable_ns - enable namespace under a directory 196 + * @kn: directory of interest, should be empty 197 + * 198 + * This is to be called right after @kn is created to enable namespace 199 + * under it. All children of @kn must have non-NULL namespace tags and 200 + * only the ones which match the super_block's tag will be visible. 201 + */ 202 + static inline void kernfs_enable_ns(struct kernfs_node *kn) 203 + { 204 + WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR); 205 + WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children)); 206 + kn->flags |= KERNFS_NS; 207 + } 208 + 209 + /** 210 + * kernfs_ns_enabled - test whether namespace is enabled 211 + * @kn: the node to test 212 + * 213 + * Test whether namespace filtering is enabled for the children of @ns. 214 + */ 215 + static inline bool kernfs_ns_enabled(struct kernfs_node *kn) 216 + { 217 + return kn->flags & KERNFS_NS; 218 + } 219 + 220 + struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent, 221 + const char *name, const void *ns); 222 + void kernfs_get(struct kernfs_node *kn); 223 + void kernfs_put(struct kernfs_node *kn); 224 + 225 + struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, 226 + void *priv); 227 + void kernfs_destroy_root(struct kernfs_root *root); 228 + 229 + struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent, 230 + const char *name, umode_t mode, 231 + void *priv, const void *ns); 232 + struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent, 233 + const char *name, 234 + umode_t mode, loff_t size, 235 + const struct kernfs_ops *ops, 236 + void *priv, const void *ns, 237 + bool name_is_static, 238 + struct lock_class_key *key); 239 + struct kernfs_node *kernfs_create_link(struct kernfs_node *parent, 240 + const char *name, 241 + struct kernfs_node *target); 242 + void kernfs_remove(struct kernfs_node *kn); 243 + int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name, 244 + const void *ns); 245 + int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, 246 + const char *new_name, const void *new_ns); 247 + int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr); 248 + void kernfs_notify(struct kernfs_node *kn); 249 + 250 + const void *kernfs_super_ns(struct super_block *sb); 251 + struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags, 252 + struct kernfs_root *root, const void *ns); 253 + void kernfs_kill_sb(struct super_block *sb); 254 + 255 + void kernfs_init(void); 256 + 257 + #else /* CONFIG_SYSFS */ 258 + 259 + static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn) 260 + { return 0; } /* whatever */ 261 + 262 + static inline void kernfs_enable_ns(struct kernfs_node *kn) { } 263 + 264 + static inline bool kernfs_ns_enabled(struct kernfs_node *kn) 265 + { return false; } 266 + 267 + static inline struct kernfs_node * 268 + kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name, 269 + const void *ns) 270 + { return NULL; } 271 + 272 + static inline void kernfs_get(struct kernfs_node *kn) { } 273 + static inline void kernfs_put(struct kernfs_node *kn) { } 274 + 275 + static inline struct kernfs_root * 276 + kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv) 277 + { return ERR_PTR(-ENOSYS); } 278 + 279 + static inline void kernfs_destroy_root(struct kernfs_root *root) { } 280 + 281 + static inline struct kernfs_node * 282 + kernfs_create_dir_ns(struct kernfs_node *parent, const char *name, 283 + umode_t mode, void *priv, const void *ns) 284 + { return ERR_PTR(-ENOSYS); } 285 + 286 + static inline struct kernfs_node * 287 + __kernfs_create_file(struct kernfs_node *parent, const char *name, 288 + umode_t mode, loff_t size, const struct kernfs_ops *ops, 289 + void *priv, const void *ns, bool name_is_static, 290 + struct lock_class_key *key) 291 + { return ERR_PTR(-ENOSYS); } 292 + 293 + static inline struct kernfs_node * 294 + kernfs_create_link(struct kernfs_node *parent, const char *name, 295 + struct kernfs_node *target) 296 + { return ERR_PTR(-ENOSYS); } 297 + 298 + static inline void kernfs_remove(struct kernfs_node *kn) { } 299 + 300 + static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn, 301 + const char *name, const void *ns) 302 + { return -ENOSYS; } 303 + 304 + static inline int kernfs_rename_ns(struct kernfs_node *kn, 305 + struct kernfs_node *new_parent, 306 + const char *new_name, const void *new_ns) 307 + { return -ENOSYS; } 308 + 309 + static inline int kernfs_setattr(struct kernfs_node *kn, 310 + const struct iattr *iattr) 311 + { return -ENOSYS; } 312 + 313 + static inline void kernfs_notify(struct kernfs_node *kn) { } 314 + 315 + static inline const void *kernfs_super_ns(struct super_block *sb) 316 + { return NULL; } 317 + 318 + static inline struct dentry * 319 + kernfs_mount_ns(struct file_system_type *fs_type, int flags, 320 + struct kernfs_root *root, const void *ns) 321 + { return ERR_PTR(-ENOSYS); } 322 + 323 + static inline void kernfs_kill_sb(struct super_block *sb) { } 324 + 325 + static inline void kernfs_init(void) { } 326 + 327 + #endif /* CONFIG_SYSFS */ 328 + 329 + static inline struct kernfs_node * 330 + kernfs_find_and_get(struct kernfs_node *kn, const char *name) 331 + { 332 + return kernfs_find_and_get_ns(kn, name, NULL); 333 + } 334 + 335 + static inline struct kernfs_node * 336 + kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode, 337 + void *priv) 338 + { 339 + return kernfs_create_dir_ns(parent, name, mode, priv, NULL); 340 + } 341 + 342 + static inline struct kernfs_node * 343 + kernfs_create_file_ns(struct kernfs_node *parent, const char *name, 344 + umode_t mode, loff_t size, const struct kernfs_ops *ops, 345 + void *priv, const void *ns) 346 + { 347 + struct lock_class_key *key = NULL; 348 + 349 + #ifdef CONFIG_DEBUG_LOCK_ALLOC 350 + key = (struct lock_class_key *)&ops->lockdep_key; 351 + #endif 352 + return __kernfs_create_file(parent, name, mode, size, ops, priv, ns, 353 + false, key); 354 + } 355 + 356 + static inline struct kernfs_node * 357 + kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode, 358 + loff_t size, const struct kernfs_ops *ops, void *priv) 359 + { 360 + return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL); 361 + } 362 + 363 + static inline int kernfs_remove_by_name(struct kernfs_node *parent, 364 + const char *name) 365 + { 366 + return kernfs_remove_by_name_ns(parent, name, NULL); 367 + } 368 + 369 + static inline struct dentry * 370 + kernfs_mount(struct file_system_type *fs_type, int flags, 371 + struct kernfs_root *root) 372 + { 373 + return kernfs_mount_ns(fs_type, flags, root, NULL); 374 + } 375 + 376 + #endif /* __LINUX_KERNFS_H */
-18
include/linux/kobj_completion.h
··· 1 - #ifndef _KOBJ_COMPLETION_H_ 2 - #define _KOBJ_COMPLETION_H_ 3 - 4 - #include <linux/kobject.h> 5 - #include <linux/completion.h> 6 - 7 - struct kobj_completion { 8 - struct kobject kc_kobj; 9 - struct completion kc_unregister; 10 - }; 11 - 12 - #define kobj_to_kobj_completion(kobj) \ 13 - container_of(kobj, struct kobj_completion, kc_kobj) 14 - 15 - void kobj_completion_init(struct kobj_completion *kc, struct kobj_type *ktype); 16 - void kobj_completion_release(struct kobject *kobj); 17 - void kobj_completion_del_and_wait(struct kobj_completion *kc); 18 - #endif /* _KOBJ_COMPLETION_H_ */
+1 -1
include/linux/kobject.h
··· 64 64 struct kobject *parent; 65 65 struct kset *kset; 66 66 struct kobj_type *ktype; 67 - struct sysfs_dirent *sd; 67 + struct kernfs_node *sd; 68 68 struct kref kref; 69 69 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE 70 70 struct delayed_work release;
+1
include/linux/memory.h
··· 35 35 }; 36 36 37 37 int arch_get_memory_phys_device(unsigned long start_pfn); 38 + unsigned long __weak memory_block_size_bytes(void); 38 39 39 40 /* These states are exposed to userspace as text strings in sysfs */ 40 41 #define MEM_ONLINE (1<<0) /* exposed to userspace */
+20 -27
include/linux/sysfs.h
··· 12 12 #ifndef _SYSFS_H_ 13 13 #define _SYSFS_H_ 14 14 15 + #include <linux/kernfs.h> 15 16 #include <linux/compiler.h> 16 17 #include <linux/errno.h> 17 18 #include <linux/list.h> ··· 176 175 ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t); 177 176 }; 178 177 179 - struct sysfs_dirent; 180 - 181 178 #ifdef CONFIG_SYSFS 182 179 183 180 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *), ··· 243 244 const char *link_name); 244 245 245 246 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr); 246 - void sysfs_notify_dirent(struct sysfs_dirent *sd); 247 - struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd, 248 - const unsigned char *name, 249 - const void *ns); 250 - struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd); 251 - void sysfs_put(struct sysfs_dirent *sd); 252 247 253 248 int __must_check sysfs_init(void); 254 249 ··· 412 419 const char *attr) 413 420 { 414 421 } 415 - static inline void sysfs_notify_dirent(struct sysfs_dirent *sd) 416 - { 417 - } 418 - static inline struct sysfs_dirent * 419 - sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd, const unsigned char *name, 420 - const void *ns) 421 - { 422 - return NULL; 423 - } 424 - static inline struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd) 425 - { 426 - return NULL; 427 - } 428 - static inline void sysfs_put(struct sysfs_dirent *sd) 429 - { 430 - } 431 422 432 423 static inline int __must_check sysfs_init(void) 433 424 { ··· 438 461 return sysfs_rename_link_ns(kobj, target, old_name, new_name, NULL); 439 462 } 440 463 441 - static inline struct sysfs_dirent * 442 - sysfs_get_dirent(struct sysfs_dirent *parent_sd, const unsigned char *name) 464 + static inline void sysfs_notify_dirent(struct kernfs_node *kn) 443 465 { 444 - return sysfs_get_dirent_ns(parent_sd, name, NULL); 466 + kernfs_notify(kn); 467 + } 468 + 469 + static inline struct kernfs_node *sysfs_get_dirent(struct kernfs_node *parent, 470 + const unsigned char *name) 471 + { 472 + return kernfs_find_and_get(parent, name); 473 + } 474 + 475 + static inline struct kernfs_node *sysfs_get(struct kernfs_node *kn) 476 + { 477 + kernfs_get(kn); 478 + return kn; 479 + } 480 + 481 + static inline void sysfs_put(struct kernfs_node *kn) 482 + { 483 + kernfs_put(kn); 445 484 } 446 485 447 486 #endif /* _SYSFS_H_ */
+34 -61
lib/kobject.c
··· 13 13 */ 14 14 15 15 #include <linux/kobject.h> 16 - #include <linux/kobj_completion.h> 17 16 #include <linux/string.h> 18 17 #include <linux/export.h> 19 18 #include <linux/stat.h> 20 19 #include <linux/slab.h> 20 + #include <linux/random.h> 21 21 22 22 /** 23 23 * kobject_namespace - return @kobj's namespace tag ··· 65 65 66 66 static int create_dir(struct kobject *kobj) 67 67 { 68 + const struct kobj_ns_type_operations *ops; 68 69 int error; 69 70 70 71 error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj)); 71 - if (!error) { 72 - error = populate_dir(kobj); 73 - if (error) 74 - sysfs_remove_dir(kobj); 72 + if (error) 73 + return error; 74 + 75 + error = populate_dir(kobj); 76 + if (error) { 77 + sysfs_remove_dir(kobj); 78 + return error; 75 79 } 76 80 77 81 /* ··· 84 80 */ 85 81 sysfs_get(kobj->sd); 86 82 87 - return error; 83 + /* 84 + * If @kobj has ns_ops, its children need to be filtered based on 85 + * their namespace tags. Enable namespace support on @kobj->sd. 86 + */ 87 + ops = kobj_child_ns_ops(kobj); 88 + if (ops) { 89 + BUG_ON(ops->type <= KOBJ_NS_TYPE_NONE); 90 + BUG_ON(ops->type >= KOBJ_NS_TYPES); 91 + BUG_ON(!kobj_ns_type_registered(ops->type)); 92 + 93 + kernfs_enable_ns(kobj->sd); 94 + } 95 + 96 + return 0; 88 97 } 89 98 90 99 static int get_kobj_path_length(struct kobject *kobj) ··· 264 247 return 0; 265 248 266 249 kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs); 267 - if (!kobj->name) 250 + if (!kobj->name) { 251 + kobj->name = old_name; 268 252 return -ENOMEM; 253 + } 269 254 270 255 /* ewww... some of these buggers have '/' in the name ... */ 271 256 while ((s = strchr(kobj->name, '/'))) ··· 365 346 * 366 347 * If @parent is set, then the parent of the @kobj will be set to it. 367 348 * If @parent is NULL, then the parent of the @kobj will be set to the 368 - * kobject associted with the kset assigned to this kobject. If no kset 349 + * kobject associated with the kset assigned to this kobject. If no kset 369 350 * is assigned to the kobject, then the kobject will be located in the 370 351 * root of the sysfs tree. 371 352 * ··· 555 536 */ 556 537 void kobject_del(struct kobject *kobj) 557 538 { 558 - struct sysfs_dirent *sd; 539 + struct kernfs_node *sd; 559 540 560 541 if (!kobj) 561 542 return; ··· 644 625 { 645 626 struct kobject *kobj = container_of(kref, struct kobject, kref); 646 627 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE 647 - pr_info("kobject: '%s' (%p): %s, parent %p (delayed)\n", 648 - kobject_name(kobj), kobj, __func__, kobj->parent); 628 + unsigned long delay = HZ + HZ * (get_random_int() & 0x3); 629 + pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n", 630 + kobject_name(kobj), kobj, __func__, kobj->parent, delay); 649 631 INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup); 650 - schedule_delayed_work(&kobj->release, HZ); 632 + 633 + schedule_delayed_work(&kobj->release, delay); 651 634 #else 652 635 kobject_cleanup(kobj); 653 636 #endif ··· 781 760 }; 782 761 783 762 /** 784 - * kobj_completion_init - initialize a kobj_completion object. 785 - * @kc: kobj_completion 786 - * @ktype: type of kobject to initialize 787 - * 788 - * kobj_completion structures can be embedded within structures with different 789 - * lifetime rules. During the release of the enclosing object, we can 790 - * wait on the release of the kobject so that we don't free it while it's 791 - * still busy. 792 - */ 793 - void kobj_completion_init(struct kobj_completion *kc, struct kobj_type *ktype) 794 - { 795 - init_completion(&kc->kc_unregister); 796 - kobject_init(&kc->kc_kobj, ktype); 797 - } 798 - EXPORT_SYMBOL_GPL(kobj_completion_init); 799 - 800 - /** 801 - * kobj_completion_release - release a kobj_completion object 802 - * @kobj: kobject embedded in kobj_completion 803 - * 804 - * Used with kobject_release to notify waiters that the kobject has been 805 - * released. 806 - */ 807 - void kobj_completion_release(struct kobject *kobj) 808 - { 809 - struct kobj_completion *kc = kobj_to_kobj_completion(kobj); 810 - complete(&kc->kc_unregister); 811 - } 812 - EXPORT_SYMBOL_GPL(kobj_completion_release); 813 - 814 - /** 815 - * kobj_completion_del_and_wait - release the kobject and wait for it 816 - * @kc: kobj_completion object to release 817 - * 818 - * Delete the kobject from sysfs and drop the reference count. Then wait 819 - * until any other outstanding references are also dropped. This routine 820 - * is only necessary once other references may have been taken on the 821 - * kobject. Typically this happens when the kobject has been published 822 - * to sysfs via kobject_add. 823 - */ 824 - void kobj_completion_del_and_wait(struct kobj_completion *kc) 825 - { 826 - kobject_del(&kc->kc_kobj); 827 - kobject_put(&kc->kc_kobj); 828 - wait_for_completion(&kc->kc_unregister); 829 - } 830 - EXPORT_SYMBOL_GPL(kobj_completion_del_and_wait); 831 - 832 - /** 833 763 * kset_register - initialize and add a kset. 834 764 * @k: kset. 835 765 */ ··· 807 835 { 808 836 if (!k) 809 837 return; 838 + kobject_del(&k->kobj); 810 839 kobject_put(&k->kobj); 811 840 } 812 841
+1
samples/kobject/kset-example.c
··· 262 262 bar_error: 263 263 destroy_foo_obj(foo_obj); 264 264 foo_error: 265 + kset_unregister(example_kset); 265 266 return -EINVAL; 266 267 } 267 268