file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 */
  259. sysfs_put_active_two(attr_sd);
  260. return 0;
  261. err_out:
  262. sysfs_put_active_two(attr_sd);
  263. return error;
  264. }
  265. static int sysfs_release(struct inode * inode, struct file * filp)
  266. {
  267. struct sysfs_buffer *buffer = filp->private_data;
  268. if (buffer) {
  269. if (buffer->page)
  270. free_page((unsigned long)buffer->page);
  271. kfree(buffer);
  272. }
  273. return 0;
  274. }
  275. /* Sysfs attribute files are pollable. The idea is that you read
  276. * the content and then you use 'poll' or 'select' to wait for
  277. * the content to change. When the content changes (assuming the
  278. * manager for the kobject supports notification), poll will
  279. * return POLLERR|POLLPRI, and select will return the fd whether
  280. * it is waiting for read, write, or exceptions.
  281. * Once poll/select indicates that the value has changed, you
  282. * need to close and re-open the file, as simply seeking and reading
  283. * again will not get new data, or reset the state of 'poll'.
  284. * Reminder: this only works for attributes which actively support
  285. * it, and it is not possible to test an attribute from userspace
  286. * to see if it supports poll (Neither 'poll' nor 'select' return
  287. * an appropriate error code). When in doubt, set a suitable timeout value.
  288. */
  289. static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
  290. {
  291. struct sysfs_buffer * buffer = filp->private_data;
  292. struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
  293. struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj;
  294. /* need parent for the kobj, grab both */
  295. if (!sysfs_get_active_two(attr_sd))
  296. goto trigger;
  297. poll_wait(filp, &kobj->poll, wait);
  298. sysfs_put_active_two(attr_sd);
  299. if (buffer->event != atomic_read(&attr_sd->s_event))
  300. goto trigger;
  301. return 0;
  302. trigger:
  303. buffer->needs_read_fill = 1;
  304. return POLLERR|POLLPRI;
  305. }
  306. void sysfs_notify(struct kobject *k, char *dir, char *attr)
  307. {
  308. struct sysfs_dirent *sd = k->sd;
  309. mutex_lock(&sysfs_mutex);
  310. if (sd && dir)
  311. sd = sysfs_find_dirent(sd, dir);
  312. if (sd && attr)
  313. sd = sysfs_find_dirent(sd, attr);
  314. if (sd) {
  315. atomic_inc(&sd->s_event);
  316. wake_up_interruptible(&k->poll);
  317. }
  318. mutex_unlock(&sysfs_mutex);
  319. }
  320. EXPORT_SYMBOL_GPL(sysfs_notify);
  321. const struct file_operations sysfs_file_operations = {
  322. .read = sysfs_read_file,
  323. .write = sysfs_write_file,
  324. .llseek = generic_file_llseek,
  325. .open = sysfs_open_file,
  326. .release = sysfs_release,
  327. .poll = sysfs_poll,
  328. };
  329. int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
  330. int type)
  331. {
  332. umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
  333. struct sysfs_addrm_cxt acxt;
  334. struct sysfs_dirent *sd;
  335. int rc;
  336. sd = sysfs_new_dirent(attr->name, mode, type);
  337. if (!sd)
  338. return -ENOMEM;
  339. sd->s_elem.attr.attr = (void *)attr;
  340. sysfs_addrm_start(&acxt, dir_sd);
  341. rc = sysfs_add_one(&acxt, sd);
  342. sysfs_addrm_finish(&acxt);
  343. if (rc)
  344. sysfs_put(sd);
  345. return rc;
  346. }
  347. /**
  348. * sysfs_create_file - create an attribute file for an object.
  349. * @kobj: object we're creating for.
  350. * @attr: atrribute descriptor.
  351. */
  352. int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
  353. {
  354. BUG_ON(!kobj || !kobj->sd || !attr);
  355. return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR);
  356. }
  357. /**
  358. * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
  359. * @kobj: object we're acting for.
  360. * @attr: attribute descriptor.
  361. * @group: group name.
  362. */
  363. int sysfs_add_file_to_group(struct kobject *kobj,
  364. const struct attribute *attr, const char *group)
  365. {
  366. struct sysfs_dirent *dir_sd;
  367. int error;
  368. dir_sd = sysfs_get_dirent(kobj->sd, group);
  369. if (!dir_sd)
  370. return -ENOENT;
  371. error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
  372. sysfs_put(dir_sd);
  373. return error;
  374. }
  375. EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
  376. /**
  377. * sysfs_chmod_file - update the modified mode value on an object attribute.
  378. * @kobj: object we're acting for.
  379. * @attr: attribute descriptor.
  380. * @mode: file permissions.
  381. *
  382. */
  383. int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
  384. {
  385. struct sysfs_dirent *victim_sd = NULL;
  386. struct dentry *victim = NULL;
  387. struct inode * inode;
  388. struct iattr newattrs;
  389. int rc;
  390. rc = -ENOENT;
  391. victim_sd = sysfs_get_dirent(kobj->sd, attr->name);
  392. if (!victim_sd)
  393. goto out;
  394. mutex_lock(&sysfs_rename_mutex);
  395. victim = sysfs_get_dentry(victim_sd);
  396. mutex_unlock(&sysfs_rename_mutex);
  397. if (IS_ERR(victim)) {
  398. rc = PTR_ERR(victim);
  399. victim = NULL;
  400. goto out;
  401. }
  402. inode = victim->d_inode;
  403. mutex_lock(&inode->i_mutex);
  404. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  405. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  406. rc = notify_change(victim, &newattrs);
  407. if (rc == 0) {
  408. mutex_lock(&sysfs_mutex);
  409. victim_sd->s_mode = newattrs.ia_mode;
  410. mutex_unlock(&sysfs_mutex);
  411. }
  412. mutex_unlock(&inode->i_mutex);
  413. out:
  414. dput(victim);
  415. sysfs_put(victim_sd);
  416. return rc;
  417. }
  418. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  419. /**
  420. * sysfs_remove_file - remove an object attribute.
  421. * @kobj: object we're acting for.
  422. * @attr: attribute descriptor.
  423. *
  424. * Hash the attribute name and kill the victim.
  425. */
  426. void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
  427. {
  428. sysfs_hash_and_remove(kobj->sd, attr->name);
  429. }
  430. /**
  431. * sysfs_remove_file_from_group - remove an attribute file from a group.
  432. * @kobj: object we're acting for.
  433. * @attr: attribute descriptor.
  434. * @group: group name.
  435. */
  436. void sysfs_remove_file_from_group(struct kobject *kobj,
  437. const struct attribute *attr, const char *group)
  438. {
  439. struct sysfs_dirent *dir_sd;
  440. dir_sd = sysfs_get_dirent(kobj->sd, group);
  441. if (dir_sd) {
  442. sysfs_hash_and_remove(dir_sd, attr->name);
  443. sysfs_put(dir_sd);
  444. }
  445. }
  446. EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
  447. struct sysfs_schedule_callback_struct {
  448. struct kobject *kobj;
  449. void (*func)(void *);
  450. void *data;
  451. struct module *owner;
  452. struct work_struct work;
  453. };
  454. static void sysfs_schedule_callback_work(struct work_struct *work)
  455. {
  456. struct sysfs_schedule_callback_struct *ss = container_of(work,
  457. struct sysfs_schedule_callback_struct, work);
  458. (ss->func)(ss->data);
  459. kobject_put(ss->kobj);
  460. module_put(ss->owner);
  461. kfree(ss);
  462. }
  463. /**
  464. * sysfs_schedule_callback - helper to schedule a callback for a kobject
  465. * @kobj: object we're acting for.
  466. * @func: callback function to invoke later.
  467. * @data: argument to pass to @func.
  468. * @owner: module owning the callback code
  469. *
  470. * sysfs attribute methods must not unregister themselves or their parent
  471. * kobject (which would amount to the same thing). Attempts to do so will
  472. * deadlock, since unregistration is mutually exclusive with driver
  473. * callbacks.
  474. *
  475. * Instead methods can call this routine, which will attempt to allocate
  476. * and schedule a workqueue request to call back @func with @data as its
  477. * argument in the workqueue's process context. @kobj will be pinned
  478. * until @func returns.
  479. *
  480. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  481. * be allocated, -ENODEV if a reference to @owner isn't available.
  482. */
  483. int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
  484. void *data, struct module *owner)
  485. {
  486. struct sysfs_schedule_callback_struct *ss;
  487. if (!try_module_get(owner))
  488. return -ENODEV;
  489. ss = kmalloc(sizeof(*ss), GFP_KERNEL);
  490. if (!ss) {
  491. module_put(owner);
  492. return -ENOMEM;
  493. }
  494. kobject_get(kobj);
  495. ss->kobj = kobj;
  496. ss->func = func;
  497. ss->data = data;
  498. ss->owner = owner;
  499. INIT_WORK(&ss->work, sysfs_schedule_callback_work);
  500. schedule_work(&ss->work);
  501. return 0;
  502. }
  503. EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
  504. EXPORT_SYMBOL_GPL(sysfs_create_file);
  505. EXPORT_SYMBOL_GPL(sysfs_remove_file);