file.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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->sd->s_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 sysfs_dirent *dir_sd, const struct attribute *attr,
  354. int type)
  355. {
  356. umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
  357. struct sysfs_addrm_cxt acxt;
  358. struct sysfs_dirent *sd;
  359. sd = sysfs_new_dirent(attr->name, mode, type);
  360. if (!sd)
  361. return -ENOMEM;
  362. sd->s_elem.attr.attr = (void *)attr;
  363. sysfs_addrm_start(&acxt, dir_sd);
  364. if (!sysfs_find_dirent(dir_sd, attr->name)) {
  365. sysfs_add_one(&acxt, sd);
  366. sysfs_link_sibling(sd);
  367. }
  368. if (sysfs_addrm_finish(&acxt))
  369. return 0;
  370. sysfs_put(sd);
  371. return -EEXIST;
  372. }
  373. /**
  374. * sysfs_create_file - create an attribute file for an object.
  375. * @kobj: object we're creating for.
  376. * @attr: atrribute descriptor.
  377. */
  378. int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
  379. {
  380. BUG_ON(!kobj || !kobj->sd || !attr);
  381. return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR);
  382. }
  383. /**
  384. * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
  385. * @kobj: object we're acting for.
  386. * @attr: attribute descriptor.
  387. * @group: group name.
  388. */
  389. int sysfs_add_file_to_group(struct kobject *kobj,
  390. const struct attribute *attr, const char *group)
  391. {
  392. struct sysfs_dirent *dir_sd;
  393. int error;
  394. dir_sd = sysfs_get_dirent(kobj->sd, group);
  395. if (!dir_sd)
  396. return -ENOENT;
  397. error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
  398. sysfs_put(dir_sd);
  399. return error;
  400. }
  401. EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
  402. /**
  403. * sysfs_update_file - update the modified timestamp on an object attribute.
  404. * @kobj: object we're acting for.
  405. * @attr: attribute descriptor.
  406. */
  407. int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
  408. {
  409. struct dentry *dir = kobj->sd->s_dentry;
  410. struct dentry * victim;
  411. int res = -ENOENT;
  412. mutex_lock(&dir->d_inode->i_mutex);
  413. victim = lookup_one_len(attr->name, dir, strlen(attr->name));
  414. if (!IS_ERR(victim)) {
  415. /* make sure dentry is really there */
  416. if (victim->d_inode &&
  417. (victim->d_parent->d_inode == dir->d_inode)) {
  418. victim->d_inode->i_mtime = CURRENT_TIME;
  419. fsnotify_modify(victim);
  420. res = 0;
  421. } else
  422. d_drop(victim);
  423. /**
  424. * Drop the reference acquired from lookup_one_len() above.
  425. */
  426. dput(victim);
  427. }
  428. mutex_unlock(&dir->d_inode->i_mutex);
  429. return res;
  430. }
  431. /**
  432. * sysfs_chmod_file - update the modified mode value on an object attribute.
  433. * @kobj: object we're acting for.
  434. * @attr: attribute descriptor.
  435. * @mode: file permissions.
  436. *
  437. */
  438. int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
  439. {
  440. struct dentry *dir = kobj->sd->s_dentry;
  441. struct dentry *victim;
  442. struct inode * inode;
  443. struct iattr newattrs;
  444. int res = -ENOENT;
  445. mutex_lock(&dir->d_inode->i_mutex);
  446. victim = lookup_one_len(attr->name, dir, strlen(attr->name));
  447. if (!IS_ERR(victim)) {
  448. if (victim->d_inode &&
  449. (victim->d_parent->d_inode == dir->d_inode)) {
  450. inode = victim->d_inode;
  451. mutex_lock(&inode->i_mutex);
  452. newattrs.ia_mode = (mode & S_IALLUGO) |
  453. (inode->i_mode & ~S_IALLUGO);
  454. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  455. res = notify_change(victim, &newattrs);
  456. mutex_unlock(&inode->i_mutex);
  457. }
  458. dput(victim);
  459. }
  460. mutex_unlock(&dir->d_inode->i_mutex);
  461. return res;
  462. }
  463. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  464. /**
  465. * sysfs_remove_file - remove an object attribute.
  466. * @kobj: object we're acting for.
  467. * @attr: attribute descriptor.
  468. *
  469. * Hash the attribute name and kill the victim.
  470. */
  471. void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
  472. {
  473. sysfs_hash_and_remove(kobj->sd, attr->name);
  474. }
  475. /**
  476. * sysfs_remove_file_from_group - remove an attribute file from a group.
  477. * @kobj: object we're acting for.
  478. * @attr: attribute descriptor.
  479. * @group: group name.
  480. */
  481. void sysfs_remove_file_from_group(struct kobject *kobj,
  482. const struct attribute *attr, const char *group)
  483. {
  484. struct sysfs_dirent *dir_sd;
  485. dir_sd = sysfs_get_dirent(kobj->sd, group);
  486. if (dir_sd) {
  487. sysfs_hash_and_remove(dir_sd, attr->name);
  488. sysfs_put(dir_sd);
  489. }
  490. }
  491. EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
  492. struct sysfs_schedule_callback_struct {
  493. struct kobject *kobj;
  494. void (*func)(void *);
  495. void *data;
  496. struct module *owner;
  497. struct work_struct work;
  498. };
  499. static void sysfs_schedule_callback_work(struct work_struct *work)
  500. {
  501. struct sysfs_schedule_callback_struct *ss = container_of(work,
  502. struct sysfs_schedule_callback_struct, work);
  503. (ss->func)(ss->data);
  504. kobject_put(ss->kobj);
  505. module_put(ss->owner);
  506. kfree(ss);
  507. }
  508. /**
  509. * sysfs_schedule_callback - helper to schedule a callback for a kobject
  510. * @kobj: object we're acting for.
  511. * @func: callback function to invoke later.
  512. * @data: argument to pass to @func.
  513. * @owner: module owning the callback code
  514. *
  515. * sysfs attribute methods must not unregister themselves or their parent
  516. * kobject (which would amount to the same thing). Attempts to do so will
  517. * deadlock, since unregistration is mutually exclusive with driver
  518. * callbacks.
  519. *
  520. * Instead methods can call this routine, which will attempt to allocate
  521. * and schedule a workqueue request to call back @func with @data as its
  522. * argument in the workqueue's process context. @kobj will be pinned
  523. * until @func returns.
  524. *
  525. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  526. * be allocated, -ENODEV if a reference to @owner isn't available.
  527. */
  528. int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
  529. void *data, struct module *owner)
  530. {
  531. struct sysfs_schedule_callback_struct *ss;
  532. if (!try_module_get(owner))
  533. return -ENODEV;
  534. ss = kmalloc(sizeof(*ss), GFP_KERNEL);
  535. if (!ss) {
  536. module_put(owner);
  537. return -ENOMEM;
  538. }
  539. kobject_get(kobj);
  540. ss->kobj = kobj;
  541. ss->func = func;
  542. ss->data = data;
  543. ss->owner = owner;
  544. INIT_WORK(&ss->work, sysfs_schedule_callback_work);
  545. schedule_work(&ss->work);
  546. return 0;
  547. }
  548. EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
  549. EXPORT_SYMBOL_GPL(sysfs_create_file);
  550. EXPORT_SYMBOL_GPL(sysfs_remove_file);
  551. EXPORT_SYMBOL_GPL(sysfs_update_file);