file.c 15 KB

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