file.c 15 KB

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