file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 <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. * @dentry: dentry to the attribute
  167. * @buffer: data buffer for file.
  168. * @count: number of bytes
  169. *
  170. * Get the correct pointers for the kobject and the attribute we're
  171. * dealing with, then call the store() method for the attribute,
  172. * passing the buffer that we acquired in fill_write_buffer().
  173. */
  174. static int
  175. flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
  176. {
  177. struct attribute * attr = to_attr(dentry);
  178. struct kobject * kobj = to_kobj(dentry->d_parent);
  179. struct sysfs_ops * ops = buffer->ops;
  180. return ops->store(kobj,attr,buffer->page,count);
  181. }
  182. /**
  183. * sysfs_write_file - write an attribute.
  184. * @file: file pointer
  185. * @buf: data to write
  186. * @count: number of bytes
  187. * @ppos: starting offset
  188. *
  189. * Similar to sysfs_read_file(), though working in the opposite direction.
  190. * We allocate and fill the data from the user in fill_write_buffer(),
  191. * then push it to the kobject in flush_write_buffer().
  192. * There is no easy way for us to know if userspace is only doing a partial
  193. * write, so we don't support them. We expect the entire buffer to come
  194. * on the first write.
  195. * Hint: if you're writing a value, first read the file, modify only the
  196. * the value you're changing, then write entire buffer back.
  197. */
  198. static ssize_t
  199. sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  200. {
  201. struct sysfs_buffer * buffer = file->private_data;
  202. ssize_t len;
  203. down(&buffer->sem);
  204. len = fill_write_buffer(buffer, buf, count);
  205. if (len > 0)
  206. len = flush_write_buffer(file->f_dentry, buffer, len);
  207. if (len > 0)
  208. *ppos += len;
  209. up(&buffer->sem);
  210. return len;
  211. }
  212. static int check_perm(struct inode * inode, struct file * file)
  213. {
  214. struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent);
  215. struct attribute * attr = to_attr(file->f_dentry);
  216. struct sysfs_buffer * buffer;
  217. struct sysfs_ops * ops = NULL;
  218. int error = 0;
  219. if (!kobj || !attr)
  220. goto Einval;
  221. /* Grab the module reference for this attribute if we have one */
  222. if (!try_module_get(attr->owner)) {
  223. error = -ENODEV;
  224. goto Done;
  225. }
  226. /* if the kobject has no ktype, then we assume that it is a subsystem
  227. * itself, and use ops for it.
  228. */
  229. if (kobj->kset && kobj->kset->ktype)
  230. ops = kobj->kset->ktype->sysfs_ops;
  231. else if (kobj->ktype)
  232. ops = kobj->ktype->sysfs_ops;
  233. else
  234. ops = &subsys_sysfs_ops;
  235. /* No sysfs operations, either from having no subsystem,
  236. * or the subsystem have no operations.
  237. */
  238. if (!ops)
  239. goto Eaccess;
  240. /* File needs write support.
  241. * The inode's perms must say it's ok,
  242. * and we must have a store method.
  243. */
  244. if (file->f_mode & FMODE_WRITE) {
  245. if (!(inode->i_mode & S_IWUGO) || !ops->store)
  246. goto Eaccess;
  247. }
  248. /* File needs read support.
  249. * The inode's perms must say it's ok, and we there
  250. * must be a show method for it.
  251. */
  252. if (file->f_mode & FMODE_READ) {
  253. if (!(inode->i_mode & S_IRUGO) || !ops->show)
  254. goto Eaccess;
  255. }
  256. /* No error? Great, allocate a buffer for the file, and store it
  257. * it in file->private_data for easy access.
  258. */
  259. buffer = kmalloc(sizeof(struct sysfs_buffer),GFP_KERNEL);
  260. if (buffer) {
  261. memset(buffer,0,sizeof(struct sysfs_buffer));
  262. init_MUTEX(&buffer->sem);
  263. buffer->needs_read_fill = 1;
  264. buffer->ops = ops;
  265. file->private_data = buffer;
  266. } else
  267. error = -ENOMEM;
  268. goto Done;
  269. Einval:
  270. error = -EINVAL;
  271. goto Done;
  272. Eaccess:
  273. error = -EACCES;
  274. module_put(attr->owner);
  275. Done:
  276. if (error && kobj)
  277. kobject_put(kobj);
  278. return error;
  279. }
  280. static int sysfs_open_file(struct inode * inode, struct file * filp)
  281. {
  282. return check_perm(inode,filp);
  283. }
  284. static int sysfs_release(struct inode * inode, struct file * filp)
  285. {
  286. struct kobject * kobj = to_kobj(filp->f_dentry->d_parent);
  287. struct attribute * attr = to_attr(filp->f_dentry);
  288. struct module * owner = attr->owner;
  289. struct sysfs_buffer * buffer = filp->private_data;
  290. if (kobj)
  291. kobject_put(kobj);
  292. /* After this point, attr should not be accessed. */
  293. module_put(owner);
  294. if (buffer) {
  295. if (buffer->page)
  296. free_page((unsigned long)buffer->page);
  297. kfree(buffer);
  298. }
  299. return 0;
  300. }
  301. struct file_operations sysfs_file_operations = {
  302. .read = sysfs_read_file,
  303. .write = sysfs_write_file,
  304. .llseek = generic_file_llseek,
  305. .open = sysfs_open_file,
  306. .release = sysfs_release,
  307. };
  308. int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
  309. {
  310. struct sysfs_dirent * parent_sd = dir->d_fsdata;
  311. umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
  312. int error = 0;
  313. down(&dir->d_inode->i_sem);
  314. error = sysfs_make_dirent(parent_sd, NULL, (void *) attr, mode, type);
  315. up(&dir->d_inode->i_sem);
  316. return error;
  317. }
  318. /**
  319. * sysfs_create_file - create an attribute file for an object.
  320. * @kobj: object we're creating for.
  321. * @attr: atrribute descriptor.
  322. */
  323. int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
  324. {
  325. BUG_ON(!kobj || !kobj->dentry || !attr);
  326. return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
  327. }
  328. /**
  329. * sysfs_update_file - update the modified timestamp on an object attribute.
  330. * @kobj: object we're acting for.
  331. * @attr: attribute descriptor.
  332. */
  333. int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
  334. {
  335. struct dentry * dir = kobj->dentry;
  336. struct dentry * victim;
  337. int res = -ENOENT;
  338. down(&dir->d_inode->i_sem);
  339. victim = lookup_one_len(attr->name, dir, strlen(attr->name));
  340. if (!IS_ERR(victim)) {
  341. /* make sure dentry is really there */
  342. if (victim->d_inode &&
  343. (victim->d_parent->d_inode == dir->d_inode)) {
  344. victim->d_inode->i_mtime = CURRENT_TIME;
  345. fsnotify_modify(victim);
  346. /**
  347. * Drop reference from initial sysfs_get_dentry().
  348. */
  349. dput(victim);
  350. res = 0;
  351. } else
  352. d_drop(victim);
  353. /**
  354. * Drop the reference acquired from sysfs_get_dentry() above.
  355. */
  356. dput(victim);
  357. }
  358. up(&dir->d_inode->i_sem);
  359. return res;
  360. }
  361. /**
  362. * sysfs_chmod_file - update the modified mode value on an object attribute.
  363. * @kobj: object we're acting for.
  364. * @attr: attribute descriptor.
  365. * @mode: file permissions.
  366. *
  367. */
  368. int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
  369. {
  370. struct dentry *dir = kobj->dentry;
  371. struct dentry *victim;
  372. struct inode * inode;
  373. struct iattr newattrs;
  374. int res = -ENOENT;
  375. down(&dir->d_inode->i_sem);
  376. victim = lookup_one_len(attr->name, dir, strlen(attr->name));
  377. if (!IS_ERR(victim)) {
  378. if (victim->d_inode &&
  379. (victim->d_parent->d_inode == dir->d_inode)) {
  380. inode = victim->d_inode;
  381. down(&inode->i_sem);
  382. newattrs.ia_mode = (mode & S_IALLUGO) |
  383. (inode->i_mode & ~S_IALLUGO);
  384. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  385. res = notify_change(victim, &newattrs);
  386. up(&inode->i_sem);
  387. }
  388. dput(victim);
  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);