file.c 14 KB

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