file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * file.c - operations for regular (text) files.
  3. */
  4. #include <linux/module.h>
  5. #include <linux/dnotify.h>
  6. #include <linux/kobject.h>
  7. #include <linux/namei.h>
  8. #include <asm/uaccess.h>
  9. #include <asm/semaphore.h>
  10. #include "sysfs.h"
  11. #define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
  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 subsystem * s = to_subsys(kobj);
  22. struct subsys_attribute * sattr = to_sattr(attr);
  23. ssize_t ret = -EIO;
  24. if (sattr->show)
  25. ret = sattr->show(s,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 subsystem * s = to_subsys(kobj);
  33. struct subsys_attribute * sattr = to_sattr(attr);
  34. ssize_t ret = -EIO;
  35. if (sattr->store)
  36. ret = sattr->store(s,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 semaphore sem;
  49. int needs_read_fill;
  50. };
  51. /**
  52. * fill_read_buffer - allocate and fill buffer from object.
  53. * @dentry: dentry pointer.
  54. * @buffer: data buffer for file.
  55. *
  56. * Allocate @buffer->page, if it hasn't been already, then call the
  57. * kobject's show() method to fill the buffer with this attribute's
  58. * data.
  59. * This is called only once, on the file's first read.
  60. */
  61. static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
  62. {
  63. struct attribute * attr = to_attr(dentry);
  64. struct kobject * kobj = to_kobj(dentry->d_parent);
  65. struct sysfs_ops * ops = buffer->ops;
  66. int ret = 0;
  67. ssize_t count;
  68. if (!buffer->page)
  69. buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
  70. if (!buffer->page)
  71. return -ENOMEM;
  72. count = ops->show(kobj,attr,buffer->page);
  73. buffer->needs_read_fill = 0;
  74. BUG_ON(count > (ssize_t)PAGE_SIZE);
  75. if (count >= 0)
  76. buffer->count = count;
  77. else
  78. ret = count;
  79. return ret;
  80. }
  81. /**
  82. * flush_read_buffer - push buffer to userspace.
  83. * @buffer: data buffer for file.
  84. * @buf: user-passed buffer.
  85. * @count: number of bytes requested.
  86. * @ppos: file position.
  87. *
  88. * Copy the buffer we filled in fill_read_buffer() to userspace.
  89. * This is done at the reader's leisure, copying and advancing
  90. * the amount they specify each time.
  91. * This may be called continuously until the buffer is empty.
  92. */
  93. static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf,
  94. size_t count, loff_t * ppos)
  95. {
  96. int error;
  97. if (*ppos > buffer->count)
  98. return 0;
  99. if (count > (buffer->count - *ppos))
  100. count = buffer->count - *ppos;
  101. error = copy_to_user(buf,buffer->page + *ppos,count);
  102. if (!error)
  103. *ppos += count;
  104. return error ? -EFAULT : count;
  105. }
  106. /**
  107. * sysfs_read_file - read an attribute.
  108. * @file: file pointer.
  109. * @buf: buffer to fill.
  110. * @count: number of bytes to read.
  111. * @ppos: starting offset in file.
  112. *
  113. * Userspace wants to read an attribute file. The attribute descriptor
  114. * is in the file's ->d_fsdata. The target object is in the directory's
  115. * ->d_fsdata.
  116. *
  117. * We call fill_read_buffer() to allocate and fill the buffer from the
  118. * object's show() method exactly once (if the read is happening from
  119. * the beginning of the file). That should fill the entire buffer with
  120. * all the data the object has to offer for that attribute.
  121. * We then call flush_read_buffer() to copy the buffer to userspace
  122. * in the increments specified.
  123. */
  124. static ssize_t
  125. sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  126. {
  127. struct sysfs_buffer * buffer = file->private_data;
  128. ssize_t retval = 0;
  129. down(&buffer->sem);
  130. if (buffer->needs_read_fill) {
  131. if ((retval = fill_read_buffer(file->f_dentry,buffer)))
  132. goto out;
  133. }
  134. pr_debug("%s: count = %d, ppos = %lld, buf = %s\n",
  135. __FUNCTION__,count,*ppos,buffer->page);
  136. retval = flush_read_buffer(buffer,buf,count,ppos);
  137. out:
  138. up(&buffer->sem);
  139. return retval;
  140. }
  141. /**
  142. * fill_write_buffer - copy buffer from userspace.
  143. * @buffer: data buffer for file.
  144. * @buf: data from user.
  145. * @count: number of bytes in @userbuf.
  146. *
  147. * Allocate @buffer->page if it hasn't been already, then
  148. * copy the user-supplied buffer into it.
  149. */
  150. static int
  151. fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
  152. {
  153. int error;
  154. if (!buffer->page)
  155. buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
  156. if (!buffer->page)
  157. return -ENOMEM;
  158. if (count >= PAGE_SIZE)
  159. count = PAGE_SIZE;
  160. error = copy_from_user(buffer->page,buf,count);
  161. buffer->needs_read_fill = 1;
  162. return error ? -EFAULT : count;
  163. }
  164. /**
  165. * flush_write_buffer - push buffer to kobject.
  166. * @file: file pointer.
  167. * @buffer: data buffer for file.
  168. *
  169. * Get the correct pointers for the kobject and the attribute we're
  170. * dealing with, then call the store() method for the attribute,
  171. * passing the buffer that we acquired in fill_write_buffer().
  172. */
  173. static int
  174. flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
  175. {
  176. struct attribute * attr = to_attr(dentry);
  177. struct kobject * kobj = to_kobj(dentry->d_parent);
  178. struct sysfs_ops * ops = buffer->ops;
  179. return ops->store(kobj,attr,buffer->page,count);
  180. }
  181. /**
  182. * sysfs_write_file - write an attribute.
  183. * @file: file pointer
  184. * @buf: data to write
  185. * @count: number of bytes
  186. * @ppos: starting offset
  187. *
  188. * Similar to sysfs_read_file(), though working in the opposite direction.
  189. * We allocate and fill the data from the user in fill_write_buffer(),
  190. * then push it to the kobject in flush_write_buffer().
  191. * There is no easy way for us to know if userspace is only doing a partial
  192. * write, so we don't support them. We expect the entire buffer to come
  193. * on the first write.
  194. * Hint: if you're writing a value, first read the file, modify only the
  195. * the value you're changing, then write entire buffer back.
  196. */
  197. static ssize_t
  198. sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  199. {
  200. struct sysfs_buffer * buffer = file->private_data;
  201. ssize_t len;
  202. down(&buffer->sem);
  203. len = fill_write_buffer(buffer, buf, count);
  204. if (len > 0)
  205. len = flush_write_buffer(file->f_dentry, buffer, len);
  206. if (len > 0)
  207. *ppos += len;
  208. up(&buffer->sem);
  209. return len;
  210. }
  211. static int check_perm(struct inode * inode, struct file * file)
  212. {
  213. struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent);
  214. struct attribute * attr = to_attr(file->f_dentry);
  215. struct sysfs_buffer * buffer;
  216. struct sysfs_ops * ops = NULL;
  217. int error = 0;
  218. if (!kobj || !attr)
  219. goto Einval;
  220. /* Grab the module reference for this attribute if we have one */
  221. if (!try_module_get(attr->owner)) {
  222. error = -ENODEV;
  223. goto Done;
  224. }
  225. /* if the kobject has no ktype, then we assume that it is a subsystem
  226. * itself, and use ops for it.
  227. */
  228. if (kobj->kset && kobj->kset->ktype)
  229. ops = kobj->kset->ktype->sysfs_ops;
  230. else if (kobj->ktype)
  231. ops = kobj->ktype->sysfs_ops;
  232. else
  233. ops = &subsys_sysfs_ops;
  234. /* No sysfs operations, either from having no subsystem,
  235. * or the subsystem have no operations.
  236. */
  237. if (!ops)
  238. goto Eaccess;
  239. /* File needs write support.
  240. * The inode's perms must say it's ok,
  241. * and we must have a store method.
  242. */
  243. if (file->f_mode & FMODE_WRITE) {
  244. if (!(inode->i_mode & S_IWUGO) || !ops->store)
  245. goto Eaccess;
  246. }
  247. /* File needs read support.
  248. * The inode's perms must say it's ok, and we there
  249. * must be a show method for it.
  250. */
  251. if (file->f_mode & FMODE_READ) {
  252. if (!(inode->i_mode & S_IRUGO) || !ops->show)
  253. goto Eaccess;
  254. }
  255. /* No error? Great, allocate a buffer for the file, and store it
  256. * it in file->private_data for easy access.
  257. */
  258. buffer = kmalloc(sizeof(struct sysfs_buffer),GFP_KERNEL);
  259. if (buffer) {
  260. memset(buffer,0,sizeof(struct sysfs_buffer));
  261. init_MUTEX(&buffer->sem);
  262. buffer->needs_read_fill = 1;
  263. buffer->ops = ops;
  264. file->private_data = buffer;
  265. } else
  266. error = -ENOMEM;
  267. goto Done;
  268. Einval:
  269. error = -EINVAL;
  270. goto Done;
  271. Eaccess:
  272. error = -EACCES;
  273. module_put(attr->owner);
  274. Done:
  275. if (error && kobj)
  276. kobject_put(kobj);
  277. return error;
  278. }
  279. static int sysfs_open_file(struct inode * inode, struct file * filp)
  280. {
  281. return check_perm(inode,filp);
  282. }
  283. static int sysfs_release(struct inode * inode, struct file * filp)
  284. {
  285. struct kobject * kobj = to_kobj(filp->f_dentry->d_parent);
  286. struct attribute * attr = to_attr(filp->f_dentry);
  287. struct module * owner = attr->owner;
  288. struct sysfs_buffer * buffer = filp->private_data;
  289. if (kobj)
  290. kobject_put(kobj);
  291. /* After this point, attr should not be accessed. */
  292. module_put(owner);
  293. if (buffer) {
  294. if (buffer->page)
  295. free_page((unsigned long)buffer->page);
  296. kfree(buffer);
  297. }
  298. return 0;
  299. }
  300. struct file_operations sysfs_file_operations = {
  301. .read = sysfs_read_file,
  302. .write = sysfs_write_file,
  303. .llseek = generic_file_llseek,
  304. .open = sysfs_open_file,
  305. .release = sysfs_release,
  306. };
  307. int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
  308. {
  309. struct sysfs_dirent * parent_sd = dir->d_fsdata;
  310. umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
  311. int error = 0;
  312. down(&dir->d_inode->i_sem);
  313. error = sysfs_make_dirent(parent_sd, NULL, (void *) attr, mode, type);
  314. up(&dir->d_inode->i_sem);
  315. return error;
  316. }
  317. /**
  318. * sysfs_create_file - create an attribute file for an object.
  319. * @kobj: object we're creating for.
  320. * @attr: atrribute descriptor.
  321. */
  322. int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
  323. {
  324. BUG_ON(!kobj || !kobj->dentry || !attr);
  325. return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
  326. }
  327. /**
  328. * sysfs_update_file - update the modified timestamp on an object attribute.
  329. * @kobj: object we're acting for.
  330. * @attr: attribute descriptor.
  331. *
  332. * Also call dnotify for the dentry, which lots of userspace programs
  333. * use.
  334. */
  335. int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
  336. {
  337. struct dentry * dir = kobj->dentry;
  338. struct dentry * victim;
  339. int res = -ENOENT;
  340. down(&dir->d_inode->i_sem);
  341. victim = lookup_one_len(attr->name, dir, strlen(attr->name));
  342. if (!IS_ERR(victim)) {
  343. /* make sure dentry is really there */
  344. if (victim->d_inode &&
  345. (victim->d_parent->d_inode == dir->d_inode)) {
  346. victim->d_inode->i_mtime = CURRENT_TIME;
  347. dnotify_parent(victim, DN_MODIFY);
  348. /**
  349. * Drop reference from initial sysfs_get_dentry().
  350. */
  351. dput(victim);
  352. res = 0;
  353. } else
  354. d_drop(victim);
  355. /**
  356. * Drop the reference acquired from sysfs_get_dentry() above.
  357. */
  358. dput(victim);
  359. }
  360. up(&dir->d_inode->i_sem);
  361. return res;
  362. }
  363. /**
  364. * sysfs_chmod_file - update the modified mode value on an object attribute.
  365. * @kobj: object we're acting for.
  366. * @attr: attribute descriptor.
  367. * @mode: file permissions.
  368. *
  369. */
  370. int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
  371. {
  372. struct dentry *dir = kobj->dentry;
  373. struct dentry *victim;
  374. struct sysfs_dirent *sd;
  375. umode_t umode = (mode & S_IALLUGO) | S_IFREG;
  376. int res = -ENOENT;
  377. down(&dir->d_inode->i_sem);
  378. victim = lookup_one_len(attr->name, dir, strlen(attr->name));
  379. if (!IS_ERR(victim)) {
  380. if (victim->d_inode &&
  381. (victim->d_parent->d_inode == dir->d_inode)) {
  382. sd = victim->d_fsdata;
  383. attr->mode = mode;
  384. sd->s_mode = umode;
  385. victim->d_inode->i_mode = umode;
  386. dput(victim);
  387. res = 0;
  388. }
  389. }
  390. up(&dir->d_inode->i_sem);
  391. return res;
  392. }
  393. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  394. /**
  395. * sysfs_remove_file - remove an object attribute.
  396. * @kobj: object we're acting for.
  397. * @attr: attribute descriptor.
  398. *
  399. * Hash the attribute name and kill the victim.
  400. */
  401. void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
  402. {
  403. sysfs_hash_and_remove(kobj->dentry,attr->name);
  404. }
  405. EXPORT_SYMBOL_GPL(sysfs_create_file);
  406. EXPORT_SYMBOL_GPL(sysfs_remove_file);
  407. EXPORT_SYMBOL_GPL(sysfs_update_file);