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 = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
  260. if (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 = -EEXIST;
  312. mutex_lock(&dir->d_inode->i_mutex);
  313. if (!sysfs_dirent_exist(parent_sd, attr->name))
  314. error = sysfs_make_dirent(parent_sd, NULL, (void *)attr,
  315. mode, type);
  316. mutex_unlock(&dir->d_inode->i_mutex);
  317. return error;
  318. }
  319. /**
  320. * sysfs_create_file - create an attribute file for an object.
  321. * @kobj: object we're creating for.
  322. * @attr: atrribute descriptor.
  323. */
  324. int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
  325. {
  326. BUG_ON(!kobj || !kobj->dentry || !attr);
  327. return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
  328. }
  329. /**
  330. * sysfs_update_file - update the modified timestamp on an object attribute.
  331. * @kobj: object we're acting for.
  332. * @attr: attribute descriptor.
  333. */
  334. int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
  335. {
  336. struct dentry * dir = kobj->dentry;
  337. struct dentry * victim;
  338. int res = -ENOENT;
  339. mutex_lock(&dir->d_inode->i_mutex);
  340. victim = lookup_one_len(attr->name, dir, strlen(attr->name));
  341. if (!IS_ERR(victim)) {
  342. /* make sure dentry is really there */
  343. if (victim->d_inode &&
  344. (victim->d_parent->d_inode == dir->d_inode)) {
  345. victim->d_inode->i_mtime = CURRENT_TIME;
  346. fsnotify_modify(victim);
  347. /**
  348. * Drop reference from initial sysfs_get_dentry().
  349. */
  350. dput(victim);
  351. res = 0;
  352. } else
  353. d_drop(victim);
  354. /**
  355. * Drop the reference acquired from sysfs_get_dentry() above.
  356. */
  357. dput(victim);
  358. }
  359. mutex_unlock(&dir->d_inode->i_mutex);
  360. return res;
  361. }
  362. /**
  363. * sysfs_chmod_file - update the modified mode value on an object attribute.
  364. * @kobj: object we're acting for.
  365. * @attr: attribute descriptor.
  366. * @mode: file permissions.
  367. *
  368. */
  369. int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
  370. {
  371. struct dentry *dir = kobj->dentry;
  372. struct dentry *victim;
  373. struct inode * inode;
  374. struct iattr newattrs;
  375. int res = -ENOENT;
  376. mutex_lock(&dir->d_inode->i_mutex);
  377. victim = lookup_one_len(attr->name, dir, strlen(attr->name));
  378. if (!IS_ERR(victim)) {
  379. if (victim->d_inode &&
  380. (victim->d_parent->d_inode == dir->d_inode)) {
  381. inode = victim->d_inode;
  382. mutex_lock(&inode->i_mutex);
  383. newattrs.ia_mode = (mode & S_IALLUGO) |
  384. (inode->i_mode & ~S_IALLUGO);
  385. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  386. res = notify_change(victim, &newattrs);
  387. mutex_unlock(&inode->i_mutex);
  388. }
  389. dput(victim);
  390. }
  391. mutex_unlock(&dir->d_inode->i_mutex);
  392. return res;
  393. }
  394. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  395. /**
  396. * sysfs_remove_file - remove an object attribute.
  397. * @kobj: object we're acting for.
  398. * @attr: attribute descriptor.
  399. *
  400. * Hash the attribute name and kill the victim.
  401. */
  402. void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
  403. {
  404. sysfs_hash_and_remove(kobj->dentry,attr->name);
  405. }
  406. EXPORT_SYMBOL_GPL(sysfs_create_file);
  407. EXPORT_SYMBOL_GPL(sysfs_remove_file);
  408. EXPORT_SYMBOL_GPL(sysfs_update_file);