file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 && kobj)
  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. if (kobj)
  330. kobject_put(kobj);
  331. /* After this point, attr should not be accessed. */
  332. module_put(owner);
  333. if (buffer) {
  334. if (buffer->page)
  335. free_page((unsigned long)buffer->page);
  336. kfree(buffer);
  337. }
  338. return 0;
  339. }
  340. /* Sysfs attribute files are pollable. The idea is that you read
  341. * the content and then you use 'poll' or 'select' to wait for
  342. * the content to change. When the content changes (assuming the
  343. * manager for the kobject supports notification), poll will
  344. * return POLLERR|POLLPRI, and select will return the fd whether
  345. * it is waiting for read, write, or exceptions.
  346. * Once poll/select indicates that the value has changed, you
  347. * need to close and re-open the file, as simply seeking and reading
  348. * again will not get new data, or reset the state of 'poll'.
  349. * Reminder: this only works for attributes which actively support
  350. * it, and it is not possible to test an attribute from userspace
  351. * to see if it supports poll (Nether 'poll' or 'select' return
  352. * an appropriate error code). When in doubt, set a suitable timeout value.
  353. */
  354. static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
  355. {
  356. struct sysfs_buffer * buffer = filp->private_data;
  357. struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
  358. struct sysfs_dirent * sd = filp->f_path.dentry->d_fsdata;
  359. int res = 0;
  360. poll_wait(filp, &kobj->poll, wait);
  361. if (buffer->event != atomic_read(&sd->s_event)) {
  362. res = POLLERR|POLLPRI;
  363. buffer->needs_read_fill = 1;
  364. }
  365. return res;
  366. }
  367. static struct dentry *step_down(struct dentry *dir, const char * name)
  368. {
  369. struct dentry * de;
  370. if (dir == NULL || dir->d_inode == NULL)
  371. return NULL;
  372. mutex_lock(&dir->d_inode->i_mutex);
  373. de = lookup_one_len(name, dir, strlen(name));
  374. mutex_unlock(&dir->d_inode->i_mutex);
  375. dput(dir);
  376. if (IS_ERR(de))
  377. return NULL;
  378. if (de->d_inode == NULL) {
  379. dput(de);
  380. return NULL;
  381. }
  382. return de;
  383. }
  384. void sysfs_notify(struct kobject * k, char *dir, char *attr)
  385. {
  386. struct dentry *de = k->dentry;
  387. if (de)
  388. dget(de);
  389. if (de && dir)
  390. de = step_down(de, dir);
  391. if (de && attr)
  392. de = step_down(de, attr);
  393. if (de) {
  394. struct sysfs_dirent * sd = de->d_fsdata;
  395. if (sd)
  396. atomic_inc(&sd->s_event);
  397. wake_up_interruptible(&k->poll);
  398. dput(de);
  399. }
  400. }
  401. EXPORT_SYMBOL_GPL(sysfs_notify);
  402. const struct file_operations sysfs_file_operations = {
  403. .read = sysfs_read_file,
  404. .write = sysfs_write_file,
  405. .llseek = generic_file_llseek,
  406. .open = sysfs_open_file,
  407. .release = sysfs_release,
  408. .poll = sysfs_poll,
  409. };
  410. int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
  411. {
  412. struct sysfs_dirent * parent_sd = dir->d_fsdata;
  413. umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
  414. int error = -EEXIST;
  415. mutex_lock(&dir->d_inode->i_mutex);
  416. if (!sysfs_dirent_exist(parent_sd, attr->name))
  417. error = sysfs_make_dirent(parent_sd, NULL, (void *)attr,
  418. mode, type);
  419. mutex_unlock(&dir->d_inode->i_mutex);
  420. return error;
  421. }
  422. /**
  423. * sysfs_create_file - create an attribute file for an object.
  424. * @kobj: object we're creating for.
  425. * @attr: atrribute descriptor.
  426. */
  427. int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
  428. {
  429. BUG_ON(!kobj || !kobj->dentry || !attr);
  430. return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
  431. }
  432. /**
  433. * sysfs_update_file - update the modified timestamp on an object attribute.
  434. * @kobj: object we're acting for.
  435. * @attr: attribute descriptor.
  436. */
  437. int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
  438. {
  439. struct dentry * dir = kobj->dentry;
  440. struct dentry * victim;
  441. int res = -ENOENT;
  442. mutex_lock(&dir->d_inode->i_mutex);
  443. victim = lookup_one_len(attr->name, dir, strlen(attr->name));
  444. if (!IS_ERR(victim)) {
  445. /* make sure dentry is really there */
  446. if (victim->d_inode &&
  447. (victim->d_parent->d_inode == dir->d_inode)) {
  448. victim->d_inode->i_mtime = CURRENT_TIME;
  449. fsnotify_modify(victim);
  450. res = 0;
  451. } else
  452. d_drop(victim);
  453. /**
  454. * Drop the reference acquired from lookup_one_len() above.
  455. */
  456. dput(victim);
  457. }
  458. mutex_unlock(&dir->d_inode->i_mutex);
  459. return res;
  460. }
  461. /**
  462. * sysfs_chmod_file - update the modified mode value on an object attribute.
  463. * @kobj: object we're acting for.
  464. * @attr: attribute descriptor.
  465. * @mode: file permissions.
  466. *
  467. */
  468. int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
  469. {
  470. struct dentry *dir = kobj->dentry;
  471. struct dentry *victim;
  472. struct inode * inode;
  473. struct iattr newattrs;
  474. int res = -ENOENT;
  475. mutex_lock(&dir->d_inode->i_mutex);
  476. victim = lookup_one_len(attr->name, dir, strlen(attr->name));
  477. if (!IS_ERR(victim)) {
  478. if (victim->d_inode &&
  479. (victim->d_parent->d_inode == dir->d_inode)) {
  480. inode = victim->d_inode;
  481. mutex_lock(&inode->i_mutex);
  482. newattrs.ia_mode = (mode & S_IALLUGO) |
  483. (inode->i_mode & ~S_IALLUGO);
  484. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  485. res = notify_change(victim, &newattrs);
  486. mutex_unlock(&inode->i_mutex);
  487. }
  488. dput(victim);
  489. }
  490. mutex_unlock(&dir->d_inode->i_mutex);
  491. return res;
  492. }
  493. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  494. /**
  495. * sysfs_remove_file - remove an object attribute.
  496. * @kobj: object we're acting for.
  497. * @attr: attribute descriptor.
  498. *
  499. * Hash the attribute name and kill the victim.
  500. */
  501. void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
  502. {
  503. sysfs_hash_and_remove(kobj->dentry, attr->name);
  504. }
  505. EXPORT_SYMBOL_GPL(sysfs_create_file);
  506. EXPORT_SYMBOL_GPL(sysfs_remove_file);
  507. EXPORT_SYMBOL_GPL(sysfs_update_file);