file.c 17 KB

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