file.c 17 KB

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