file.c 15 KB

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