Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * file.c - operations for regular (text) files. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/module.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 6 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/kobject.h> |
Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 8 | #include <linux/namei.h> |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 9 | #include <linux/poll.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <asm/uaccess.h> |
| 11 | #include <asm/semaphore.h> |
| 12 | |
| 13 | #include "sysfs.h" |
| 14 | |
| 15 | #define to_subsys(k) container_of(k,struct subsystem,kset.kobj) |
| 16 | #define to_sattr(a) container_of(a,struct subsys_attribute,attr) |
| 17 | |
Martin Waitz | 3d41088 | 2005-06-23 22:05:21 -0700 | [diff] [blame] | 18 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * Subsystem file operations. |
| 20 | * These operations allow subsystems to have files that can be |
| 21 | * read/written. |
| 22 | */ |
| 23 | static ssize_t |
| 24 | subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page) |
| 25 | { |
| 26 | struct subsystem * s = to_subsys(kobj); |
| 27 | struct subsys_attribute * sattr = to_sattr(attr); |
Dmitry Torokhov | c76d0ab | 2005-04-29 01:22:00 -0500 | [diff] [blame] | 28 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | if (sattr->show) |
| 31 | ret = sattr->show(s,page); |
| 32 | return ret; |
| 33 | } |
| 34 | |
| 35 | static ssize_t |
| 36 | subsys_attr_store(struct kobject * kobj, struct attribute * attr, |
| 37 | const char * page, size_t count) |
| 38 | { |
| 39 | struct subsystem * s = to_subsys(kobj); |
| 40 | struct subsys_attribute * sattr = to_sattr(attr); |
Dmitry Torokhov | c76d0ab | 2005-04-29 01:22:00 -0500 | [diff] [blame] | 41 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | if (sattr->store) |
| 44 | ret = sattr->store(s,page,count); |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | static struct sysfs_ops subsys_sysfs_ops = { |
| 49 | .show = subsys_attr_show, |
| 50 | .store = subsys_attr_store, |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | struct sysfs_buffer { |
| 55 | size_t count; |
| 56 | loff_t pos; |
| 57 | char * page; |
| 58 | struct sysfs_ops * ops; |
| 59 | struct semaphore sem; |
| 60 | int needs_read_fill; |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 61 | int event; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * fill_read_buffer - allocate and fill buffer from object. |
| 67 | * @dentry: dentry pointer. |
| 68 | * @buffer: data buffer for file. |
| 69 | * |
| 70 | * Allocate @buffer->page, if it hasn't been already, then call the |
| 71 | * kobject's show() method to fill the buffer with this attribute's |
| 72 | * data. |
| 73 | * This is called only once, on the file's first read. |
| 74 | */ |
| 75 | static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer) |
| 76 | { |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 77 | struct sysfs_dirent * sd = dentry->d_fsdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | struct attribute * attr = to_attr(dentry); |
| 79 | struct kobject * kobj = to_kobj(dentry->d_parent); |
| 80 | struct sysfs_ops * ops = buffer->ops; |
| 81 | int ret = 0; |
| 82 | ssize_t count; |
| 83 | |
| 84 | if (!buffer->page) |
| 85 | buffer->page = (char *) get_zeroed_page(GFP_KERNEL); |
| 86 | if (!buffer->page) |
| 87 | return -ENOMEM; |
| 88 | |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 89 | buffer->event = atomic_read(&sd->s_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | count = ops->show(kobj,attr,buffer->page); |
| 91 | buffer->needs_read_fill = 0; |
| 92 | BUG_ON(count > (ssize_t)PAGE_SIZE); |
| 93 | if (count >= 0) |
| 94 | buffer->count = count; |
| 95 | else |
| 96 | ret = count; |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * flush_read_buffer - push buffer to userspace. |
| 103 | * @buffer: data buffer for file. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 104 | * @buf: user-passed buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | * @count: number of bytes requested. |
| 106 | * @ppos: file position. |
| 107 | * |
| 108 | * Copy the buffer we filled in fill_read_buffer() to userspace. |
| 109 | * This is done at the reader's leisure, copying and advancing |
| 110 | * the amount they specify each time. |
| 111 | * This may be called continuously until the buffer is empty. |
| 112 | */ |
| 113 | static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf, |
| 114 | size_t count, loff_t * ppos) |
| 115 | { |
| 116 | int error; |
| 117 | |
| 118 | if (*ppos > buffer->count) |
| 119 | return 0; |
| 120 | |
| 121 | if (count > (buffer->count - *ppos)) |
| 122 | count = buffer->count - *ppos; |
| 123 | |
| 124 | error = copy_to_user(buf,buffer->page + *ppos,count); |
| 125 | if (!error) |
| 126 | *ppos += count; |
| 127 | return error ? -EFAULT : count; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * sysfs_read_file - read an attribute. |
| 132 | * @file: file pointer. |
| 133 | * @buf: buffer to fill. |
| 134 | * @count: number of bytes to read. |
| 135 | * @ppos: starting offset in file. |
| 136 | * |
| 137 | * Userspace wants to read an attribute file. The attribute descriptor |
| 138 | * is in the file's ->d_fsdata. The target object is in the directory's |
| 139 | * ->d_fsdata. |
| 140 | * |
| 141 | * We call fill_read_buffer() to allocate and fill the buffer from the |
| 142 | * object's show() method exactly once (if the read is happening from |
| 143 | * the beginning of the file). That should fill the entire buffer with |
| 144 | * all the data the object has to offer for that attribute. |
| 145 | * We then call flush_read_buffer() to copy the buffer to userspace |
| 146 | * in the increments specified. |
| 147 | */ |
| 148 | |
| 149 | static ssize_t |
| 150 | sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 151 | { |
| 152 | struct sysfs_buffer * buffer = file->private_data; |
| 153 | ssize_t retval = 0; |
| 154 | |
| 155 | down(&buffer->sem); |
| 156 | if (buffer->needs_read_fill) { |
| 157 | if ((retval = fill_read_buffer(file->f_dentry,buffer))) |
| 158 | goto out; |
| 159 | } |
| 160 | pr_debug("%s: count = %d, ppos = %lld, buf = %s\n", |
| 161 | __FUNCTION__,count,*ppos,buffer->page); |
| 162 | retval = flush_read_buffer(buffer,buf,count,ppos); |
| 163 | out: |
| 164 | up(&buffer->sem); |
| 165 | return retval; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * fill_write_buffer - copy buffer from userspace. |
| 171 | * @buffer: data buffer for file. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 172 | * @buf: data from user. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | * @count: number of bytes in @userbuf. |
| 174 | * |
| 175 | * Allocate @buffer->page if it hasn't been already, then |
| 176 | * copy the user-supplied buffer into it. |
| 177 | */ |
| 178 | |
| 179 | static int |
| 180 | fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count) |
| 181 | { |
| 182 | int error; |
| 183 | |
| 184 | if (!buffer->page) |
| 185 | buffer->page = (char *)get_zeroed_page(GFP_KERNEL); |
| 186 | if (!buffer->page) |
| 187 | return -ENOMEM; |
| 188 | |
| 189 | if (count >= PAGE_SIZE) |
Greg Kroah-Hartman | 6e0dd74 | 2006-03-31 15:37:06 -0800 | [diff] [blame] | 190 | count = PAGE_SIZE - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | error = copy_from_user(buffer->page,buf,count); |
| 192 | buffer->needs_read_fill = 1; |
| 193 | return error ? -EFAULT : count; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /** |
| 198 | * flush_write_buffer - push buffer to kobject. |
Martin Waitz | 3d41088 | 2005-06-23 22:05:21 -0700 | [diff] [blame] | 199 | * @dentry: dentry to the attribute |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | * @buffer: data buffer for file. |
Martin Waitz | 3d41088 | 2005-06-23 22:05:21 -0700 | [diff] [blame] | 201 | * @count: number of bytes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | * |
| 203 | * Get the correct pointers for the kobject and the attribute we're |
| 204 | * dealing with, then call the store() method for the attribute, |
| 205 | * passing the buffer that we acquired in fill_write_buffer(). |
| 206 | */ |
| 207 | |
| 208 | static int |
| 209 | flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count) |
| 210 | { |
| 211 | struct attribute * attr = to_attr(dentry); |
| 212 | struct kobject * kobj = to_kobj(dentry->d_parent); |
| 213 | struct sysfs_ops * ops = buffer->ops; |
| 214 | |
| 215 | return ops->store(kobj,attr,buffer->page,count); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * sysfs_write_file - write an attribute. |
| 221 | * @file: file pointer |
| 222 | * @buf: data to write |
| 223 | * @count: number of bytes |
| 224 | * @ppos: starting offset |
| 225 | * |
| 226 | * Similar to sysfs_read_file(), though working in the opposite direction. |
| 227 | * We allocate and fill the data from the user in fill_write_buffer(), |
| 228 | * then push it to the kobject in flush_write_buffer(). |
| 229 | * There is no easy way for us to know if userspace is only doing a partial |
| 230 | * write, so we don't support them. We expect the entire buffer to come |
| 231 | * on the first write. |
| 232 | * Hint: if you're writing a value, first read the file, modify only the |
| 233 | * the value you're changing, then write entire buffer back. |
| 234 | */ |
| 235 | |
| 236 | static ssize_t |
| 237 | sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
| 238 | { |
| 239 | struct sysfs_buffer * buffer = file->private_data; |
| 240 | ssize_t len; |
| 241 | |
| 242 | down(&buffer->sem); |
| 243 | len = fill_write_buffer(buffer, buf, count); |
| 244 | if (len > 0) |
| 245 | len = flush_write_buffer(file->f_dentry, buffer, len); |
| 246 | if (len > 0) |
| 247 | *ppos += len; |
| 248 | up(&buffer->sem); |
| 249 | return len; |
| 250 | } |
| 251 | |
| 252 | static int check_perm(struct inode * inode, struct file * file) |
| 253 | { |
| 254 | struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent); |
| 255 | struct attribute * attr = to_attr(file->f_dentry); |
| 256 | struct sysfs_buffer * buffer; |
| 257 | struct sysfs_ops * ops = NULL; |
| 258 | int error = 0; |
| 259 | |
| 260 | if (!kobj || !attr) |
| 261 | goto Einval; |
| 262 | |
| 263 | /* Grab the module reference for this attribute if we have one */ |
| 264 | if (!try_module_get(attr->owner)) { |
| 265 | error = -ENODEV; |
| 266 | goto Done; |
| 267 | } |
| 268 | |
| 269 | /* if the kobject has no ktype, then we assume that it is a subsystem |
| 270 | * itself, and use ops for it. |
| 271 | */ |
| 272 | if (kobj->kset && kobj->kset->ktype) |
| 273 | ops = kobj->kset->ktype->sysfs_ops; |
| 274 | else if (kobj->ktype) |
| 275 | ops = kobj->ktype->sysfs_ops; |
| 276 | else |
| 277 | ops = &subsys_sysfs_ops; |
| 278 | |
| 279 | /* No sysfs operations, either from having no subsystem, |
| 280 | * or the subsystem have no operations. |
| 281 | */ |
| 282 | if (!ops) |
| 283 | goto Eaccess; |
| 284 | |
| 285 | /* File needs write support. |
| 286 | * The inode's perms must say it's ok, |
| 287 | * and we must have a store method. |
| 288 | */ |
| 289 | if (file->f_mode & FMODE_WRITE) { |
| 290 | |
| 291 | if (!(inode->i_mode & S_IWUGO) || !ops->store) |
| 292 | goto Eaccess; |
| 293 | |
| 294 | } |
| 295 | |
| 296 | /* File needs read support. |
| 297 | * The inode's perms must say it's ok, and we there |
| 298 | * must be a show method for it. |
| 299 | */ |
| 300 | if (file->f_mode & FMODE_READ) { |
| 301 | if (!(inode->i_mode & S_IRUGO) || !ops->show) |
| 302 | goto Eaccess; |
| 303 | } |
| 304 | |
| 305 | /* No error? Great, allocate a buffer for the file, and store it |
| 306 | * it in file->private_data for easy access. |
| 307 | */ |
Eric Sesterhenn | 58d4928 | 2006-02-22 11:18:15 +0100 | [diff] [blame] | 308 | buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | if (buffer) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | init_MUTEX(&buffer->sem); |
| 311 | buffer->needs_read_fill = 1; |
| 312 | buffer->ops = ops; |
| 313 | file->private_data = buffer; |
| 314 | } else |
| 315 | error = -ENOMEM; |
| 316 | goto Done; |
| 317 | |
| 318 | Einval: |
| 319 | error = -EINVAL; |
| 320 | goto Done; |
| 321 | Eaccess: |
| 322 | error = -EACCES; |
| 323 | module_put(attr->owner); |
| 324 | Done: |
| 325 | if (error && kobj) |
| 326 | kobject_put(kobj); |
| 327 | return error; |
| 328 | } |
| 329 | |
| 330 | static int sysfs_open_file(struct inode * inode, struct file * filp) |
| 331 | { |
| 332 | return check_perm(inode,filp); |
| 333 | } |
| 334 | |
| 335 | static int sysfs_release(struct inode * inode, struct file * filp) |
| 336 | { |
| 337 | struct kobject * kobj = to_kobj(filp->f_dentry->d_parent); |
| 338 | struct attribute * attr = to_attr(filp->f_dentry); |
| 339 | struct module * owner = attr->owner; |
| 340 | struct sysfs_buffer * buffer = filp->private_data; |
| 341 | |
| 342 | if (kobj) |
| 343 | kobject_put(kobj); |
| 344 | /* After this point, attr should not be accessed. */ |
| 345 | module_put(owner); |
| 346 | |
| 347 | if (buffer) { |
| 348 | if (buffer->page) |
| 349 | free_page((unsigned long)buffer->page); |
| 350 | kfree(buffer); |
| 351 | } |
| 352 | return 0; |
| 353 | } |
| 354 | |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 355 | /* Sysfs attribute files are pollable. The idea is that you read |
| 356 | * the content and then you use 'poll' or 'select' to wait for |
| 357 | * the content to change. When the content changes (assuming the |
| 358 | * manager for the kobject supports notification), poll will |
| 359 | * return POLLERR|POLLPRI, and select will return the fd whether |
| 360 | * it is waiting for read, write, or exceptions. |
| 361 | * Once poll/select indicates that the value has changed, you |
| 362 | * need to close and re-open the file, as simply seeking and reading |
| 363 | * again will not get new data, or reset the state of 'poll'. |
| 364 | * Reminder: this only works for attributes which actively support |
| 365 | * it, and it is not possible to test an attribute from userspace |
| 366 | * to see if it supports poll (Nether 'poll' or 'select' return |
| 367 | * an appropriate error code). When in doubt, set a suitable timeout value. |
| 368 | */ |
| 369 | static unsigned int sysfs_poll(struct file *filp, poll_table *wait) |
| 370 | { |
| 371 | struct sysfs_buffer * buffer = filp->private_data; |
| 372 | struct kobject * kobj = to_kobj(filp->f_dentry->d_parent); |
| 373 | struct sysfs_dirent * sd = filp->f_dentry->d_fsdata; |
| 374 | int res = 0; |
| 375 | |
| 376 | poll_wait(filp, &kobj->poll, wait); |
| 377 | |
| 378 | if (buffer->event != atomic_read(&sd->s_event)) { |
| 379 | res = POLLERR|POLLPRI; |
| 380 | buffer->needs_read_fill = 1; |
| 381 | } |
| 382 | |
| 383 | return res; |
| 384 | } |
| 385 | |
| 386 | |
| 387 | static struct dentry *step_down(struct dentry *dir, const char * name) |
| 388 | { |
| 389 | struct dentry * de; |
| 390 | |
| 391 | if (dir == NULL || dir->d_inode == NULL) |
| 392 | return NULL; |
| 393 | |
| 394 | mutex_lock(&dir->d_inode->i_mutex); |
| 395 | de = lookup_one_len(name, dir, strlen(name)); |
| 396 | mutex_unlock(&dir->d_inode->i_mutex); |
| 397 | dput(dir); |
| 398 | if (IS_ERR(de)) |
| 399 | return NULL; |
| 400 | if (de->d_inode == NULL) { |
| 401 | dput(de); |
| 402 | return NULL; |
| 403 | } |
| 404 | return de; |
| 405 | } |
| 406 | |
| 407 | void sysfs_notify(struct kobject * k, char *dir, char *attr) |
| 408 | { |
| 409 | struct dentry *de = k->dentry; |
| 410 | if (de) |
| 411 | dget(de); |
| 412 | if (de && dir) |
| 413 | de = step_down(de, dir); |
| 414 | if (de && attr) |
| 415 | de = step_down(de, attr); |
| 416 | if (de) { |
| 417 | struct sysfs_dirent * sd = de->d_fsdata; |
| 418 | if (sd) |
| 419 | atomic_inc(&sd->s_event); |
| 420 | wake_up_interruptible(&k->poll); |
| 421 | dput(de); |
| 422 | } |
| 423 | } |
| 424 | EXPORT_SYMBOL_GPL(sysfs_notify); |
| 425 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 426 | const struct file_operations sysfs_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | .read = sysfs_read_file, |
| 428 | .write = sysfs_write_file, |
| 429 | .llseek = generic_file_llseek, |
| 430 | .open = sysfs_open_file, |
| 431 | .release = sysfs_release, |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 432 | .poll = sysfs_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | }; |
| 434 | |
| 435 | |
| 436 | int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type) |
| 437 | { |
| 438 | struct sysfs_dirent * parent_sd = dir->d_fsdata; |
| 439 | umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG; |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 440 | int error = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 442 | mutex_lock(&dir->d_inode->i_mutex); |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 443 | if (!sysfs_dirent_exist(parent_sd, attr->name)) |
| 444 | error = sysfs_make_dirent(parent_sd, NULL, (void *)attr, |
| 445 | mode, type); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 446 | mutex_unlock(&dir->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
| 448 | return error; |
| 449 | } |
| 450 | |
| 451 | |
| 452 | /** |
| 453 | * sysfs_create_file - create an attribute file for an object. |
| 454 | * @kobj: object we're creating for. |
| 455 | * @attr: atrribute descriptor. |
| 456 | */ |
| 457 | |
| 458 | int sysfs_create_file(struct kobject * kobj, const struct attribute * attr) |
| 459 | { |
| 460 | BUG_ON(!kobj || !kobj->dentry || !attr); |
| 461 | |
| 462 | return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR); |
| 463 | |
| 464 | } |
| 465 | |
| 466 | |
| 467 | /** |
| 468 | * sysfs_update_file - update the modified timestamp on an object attribute. |
| 469 | * @kobj: object we're acting for. |
| 470 | * @attr: attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | */ |
| 472 | int sysfs_update_file(struct kobject * kobj, const struct attribute * attr) |
| 473 | { |
| 474 | struct dentry * dir = kobj->dentry; |
| 475 | struct dentry * victim; |
| 476 | int res = -ENOENT; |
| 477 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 478 | mutex_lock(&dir->d_inode->i_mutex); |
Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 479 | victim = lookup_one_len(attr->name, dir, strlen(attr->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | if (!IS_ERR(victim)) { |
| 481 | /* make sure dentry is really there */ |
| 482 | if (victim->d_inode && |
| 483 | (victim->d_parent->d_inode == dir->d_inode)) { |
| 484 | victim->d_inode->i_mtime = CURRENT_TIME; |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 485 | fsnotify_modify(victim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
| 487 | /** |
| 488 | * Drop reference from initial sysfs_get_dentry(). |
| 489 | */ |
| 490 | dput(victim); |
| 491 | res = 0; |
| 492 | } else |
| 493 | d_drop(victim); |
| 494 | |
| 495 | /** |
| 496 | * Drop the reference acquired from sysfs_get_dentry() above. |
| 497 | */ |
| 498 | dput(victim); |
| 499 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 500 | mutex_unlock(&dir->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
| 502 | return res; |
| 503 | } |
| 504 | |
| 505 | |
| 506 | /** |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 507 | * sysfs_chmod_file - update the modified mode value on an object attribute. |
| 508 | * @kobj: object we're acting for. |
| 509 | * @attr: attribute descriptor. |
| 510 | * @mode: file permissions. |
| 511 | * |
| 512 | */ |
| 513 | int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode) |
| 514 | { |
| 515 | struct dentry *dir = kobj->dentry; |
| 516 | struct dentry *victim; |
Maneesh Soni | bc062b1 | 2005-07-29 12:13:35 -0700 | [diff] [blame] | 517 | struct inode * inode; |
| 518 | struct iattr newattrs; |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 519 | int res = -ENOENT; |
| 520 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 521 | mutex_lock(&dir->d_inode->i_mutex); |
Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 522 | victim = lookup_one_len(attr->name, dir, strlen(attr->name)); |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 523 | if (!IS_ERR(victim)) { |
| 524 | if (victim->d_inode && |
| 525 | (victim->d_parent->d_inode == dir->d_inode)) { |
Maneesh Soni | bc062b1 | 2005-07-29 12:13:35 -0700 | [diff] [blame] | 526 | inode = victim->d_inode; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 527 | mutex_lock(&inode->i_mutex); |
Maneesh Soni | bc062b1 | 2005-07-29 12:13:35 -0700 | [diff] [blame] | 528 | newattrs.ia_mode = (mode & S_IALLUGO) | |
| 529 | (inode->i_mode & ~S_IALLUGO); |
| 530 | newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; |
| 531 | res = notify_change(victim, &newattrs); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 532 | mutex_unlock(&inode->i_mutex); |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 533 | } |
Maneesh Soni | bc062b1 | 2005-07-29 12:13:35 -0700 | [diff] [blame] | 534 | dput(victim); |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 535 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 536 | mutex_unlock(&dir->d_inode->i_mutex); |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 537 | |
| 538 | return res; |
| 539 | } |
| 540 | EXPORT_SYMBOL_GPL(sysfs_chmod_file); |
| 541 | |
| 542 | |
| 543 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | * sysfs_remove_file - remove an object attribute. |
| 545 | * @kobj: object we're acting for. |
| 546 | * @attr: attribute descriptor. |
| 547 | * |
| 548 | * Hash the attribute name and kill the victim. |
| 549 | */ |
| 550 | |
| 551 | void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr) |
| 552 | { |
| 553 | sysfs_hash_and_remove(kobj->dentry,attr->name); |
| 554 | } |
| 555 | |
| 556 | |
| 557 | EXPORT_SYMBOL_GPL(sysfs_create_file); |
| 558 | EXPORT_SYMBOL_GPL(sysfs_remove_file); |
| 559 | EXPORT_SYMBOL_GPL(sysfs_update_file); |