file.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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_path.dentry,buffer)))
  136. goto out;
  137. }
  138. pr_debug("%s: count = %zd, 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. /* if buf is assumed to contain a string, terminate it by \0,
  167. so e.g. sscanf() can scan the string easily */
  168. buffer->page[count] = 0;
  169. return error ? -EFAULT : count;
  170. }
  171. /**
  172. * flush_write_buffer - push buffer to kobject.
  173. * @dentry: dentry to the attribute
  174. * @buffer: data buffer for file.
  175. * @count: number of bytes
  176. *
  177. * Get the correct pointers for the kobject and the attribute we're
  178. * dealing with, then call the store() method for the attribute,
  179. * passing the buffer that we acquired in fill_write_buffer().
  180. */
  181. static int
  182. flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
  183. {
  184. struct attribute * attr = to_attr(dentry);
  185. struct kobject * kobj = to_kobj(dentry->d_parent);
  186. struct sysfs_ops * ops = buffer->ops;
  187. return ops->store(kobj,attr,buffer->page,count);
  188. }
  189. /**
  190. * sysfs_write_file - write an attribute.
  191. * @file: file pointer
  192. * @buf: data to write
  193. * @count: number of bytes
  194. * @ppos: starting offset
  195. *
  196. * Similar to sysfs_read_file(), though working in the opposite direction.
  197. * We allocate and fill the data from the user in fill_write_buffer(),
  198. * then push it to the kobject in flush_write_buffer().
  199. * There is no easy way for us to know if userspace is only doing a partial
  200. * write, so we don't support them. We expect the entire buffer to come
  201. * on the first write.
  202. * Hint: if you're writing a value, first read the file, modify only the
  203. * the value you're changing, then write entire buffer back.
  204. */
  205. static ssize_t
  206. sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  207. {
  208. struct sysfs_buffer * buffer = file->private_data;
  209. ssize_t len;
  210. down(&buffer->sem);
  211. len = fill_write_buffer(buffer, buf, count);
  212. if (len > 0)
  213. len = flush_write_buffer(file->f_path.dentry, buffer, len);
  214. if (len > 0)
  215. *ppos += len;
  216. up(&buffer->sem);
  217. return len;
  218. }
  219. static int check_perm(struct inode * inode, struct file * file)
  220. {
  221. struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
  222. struct attribute * attr = to_attr(file->f_path.dentry);
  223. struct sysfs_buffer * buffer;
  224. struct sysfs_ops * ops = NULL;
  225. int error = 0;
  226. if (!kobj || !attr)
  227. goto Einval;
  228. /* Grab the module reference for this attribute if we have one */
  229. if (!try_module_get(attr->owner)) {
  230. error = -ENODEV;
  231. goto Done;
  232. }
  233. /* if the kobject has no ktype, then we assume that it is a subsystem
  234. * itself, and use ops for it.
  235. */
  236. if (kobj->kset && kobj->kset->ktype)
  237. ops = kobj->kset->ktype->sysfs_ops;
  238. else if (kobj->ktype)
  239. ops = kobj->ktype->sysfs_ops;
  240. else
  241. ops = &subsys_sysfs_ops;
  242. /* No sysfs operations, either from having no subsystem,
  243. * or the subsystem have no operations.
  244. */
  245. if (!ops)
  246. goto Eaccess;
  247. /* File needs write support.
  248. * The inode's perms must say it's ok,
  249. * and we must have a store method.
  250. */
  251. if (file->f_mode & FMODE_WRITE) {
  252. if (!(inode->i_mode & S_IWUGO) || !ops->store)
  253. goto Eaccess;
  254. }
  255. /* File needs read support.
  256. * The inode's perms must say it's ok, and we there
  257. * must be a show method for it.
  258. */
  259. if (file->f_mode & FMODE_READ) {
  260. if (!(inode->i_mode & S_IRUGO) || !ops->show)
  261. goto Eaccess;
  262. }
  263. /* No error? Great, allocate a buffer for the file, and store it
  264. * it in file->private_data for easy access.
  265. */
  266. buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
  267. if (buffer) {
  268. init_MUTEX(&buffer->sem);
  269. buffer->needs_read_fill = 1;
  270. buffer->ops = ops;
  271. file->private_data = buffer;
  272. } else
  273. error = -ENOMEM;
  274. goto Done;
  275. Einval:
  276. error = -EINVAL;
  277. goto Done;
  278. Eaccess:
  279. error = -EACCES;
  280. module_put(attr->owner);
  281. Done:
  282. if (error && kobj)
  283. kobject_put(kobj);
  284. return error;
  285. }
  286. static int sysfs_open_file(struct inode * inode, struct file * filp)
  287. {
  288. return check_perm(inode,filp);
  289. }
  290. static int sysfs_release(struct inode * inode, struct file * filp)
  291. {
  292. struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
  293. struct attribute * attr = to_attr(filp->f_path.dentry);
  294. struct module * owner = attr->owner;
  295. struct sysfs_buffer * buffer = filp->private_data;
  296. if (kobj)
  297. kobject_put(kobj);
  298. /* After this point, attr should not be accessed. */
  299. module_put(owner);
  300. if (buffer) {
  301. if (buffer->page)
  302. free_page((unsigned long)buffer->page);
  303. kfree(buffer);
  304. }
  305. return 0;
  306. }
  307. /* Sysfs attribute files are pollable. The idea is that you read
  308. * the content and then you use 'poll' or 'select' to wait for
  309. * the content to change. When the content changes (assuming the
  310. * manager for the kobject supports notification), poll will
  311. * return POLLERR|POLLPRI, and select will return the fd whether
  312. * it is waiting for read, write, or exceptions.
  313. * Once poll/select indicates that the value has changed, you
  314. * need to close and re-open the file, as simply seeking and reading
  315. * again will not get new data, or reset the state of 'poll'.
  316. * Reminder: this only works for attributes which actively support
  317. * it, and it is not possible to test an attribute from userspace
  318. * to see if it supports poll (Nether 'poll' or 'select' return
  319. * an appropriate error code). When in doubt, set a suitable timeout value.
  320. */
  321. static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
  322. {
  323. struct sysfs_buffer * buffer = filp->private_data;
  324. struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
  325. struct sysfs_dirent * sd = filp->f_path.dentry->d_fsdata;
  326. int res = 0;
  327. poll_wait(filp, &kobj->poll, wait);
  328. if (buffer->event != atomic_read(&sd->s_event)) {
  329. res = POLLERR|POLLPRI;
  330. buffer->needs_read_fill = 1;
  331. }
  332. return res;
  333. }
  334. static struct dentry *step_down(struct dentry *dir, const char * name)
  335. {
  336. struct dentry * de;
  337. if (dir == NULL || dir->d_inode == NULL)
  338. return NULL;
  339. mutex_lock(&dir->d_inode->i_mutex);
  340. de = lookup_one_len(name, dir, strlen(name));
  341. mutex_unlock(&dir->d_inode->i_mutex);
  342. dput(dir);
  343. if (IS_ERR(de))
  344. return NULL;
  345. if (de->d_inode == NULL) {
  346. dput(de);
  347. return NULL;
  348. }
  349. return de;
  350. }
  351. void sysfs_notify(struct kobject * k, char *dir, char *attr)
  352. {
  353. struct dentry *de = k->dentry;
  354. if (de)
  355. dget(de);
  356. if (de && dir)
  357. de = step_down(de, dir);
  358. if (de && attr)
  359. de = step_down(de, attr);
  360. if (de) {
  361. struct sysfs_dirent * sd = de->d_fsdata;
  362. if (sd)
  363. atomic_inc(&sd->s_event);
  364. wake_up_interruptible(&k->poll);
  365. dput(de);
  366. }
  367. }
  368. EXPORT_SYMBOL_GPL(sysfs_notify);
  369. const struct file_operations sysfs_file_operations = {
  370. .read = sysfs_read_file,
  371. .write = sysfs_write_file,
  372. .llseek = generic_file_llseek,
  373. .open = sysfs_open_file,
  374. .release = sysfs_release,
  375. .poll = sysfs_poll,
  376. };
  377. int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
  378. {
  379. struct sysfs_dirent * parent_sd = dir->d_fsdata;
  380. umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
  381. int error = -EEXIST;
  382. mutex_lock(&dir->d_inode->i_mutex);
  383. if (!sysfs_dirent_exist(parent_sd, attr->name))
  384. error = sysfs_make_dirent(parent_sd, NULL, (void *)attr,
  385. mode, type);
  386. mutex_unlock(&dir->d_inode->i_mutex);
  387. return error;
  388. }
  389. /**
  390. * sysfs_create_file - create an attribute file for an object.
  391. * @kobj: object we're creating for.
  392. * @attr: atrribute descriptor.
  393. */
  394. int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
  395. {
  396. BUG_ON(!kobj || !kobj->dentry || !attr);
  397. return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
  398. }
  399. /**
  400. * sysfs_update_file - update the modified timestamp on an object attribute.
  401. * @kobj: object we're acting for.
  402. * @attr: attribute descriptor.
  403. */
  404. int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
  405. {
  406. struct dentry * dir = kobj->dentry;
  407. struct dentry * victim;
  408. int res = -ENOENT;
  409. mutex_lock(&dir->d_inode->i_mutex);
  410. victim = lookup_one_len(attr->name, dir, strlen(attr->name));
  411. if (!IS_ERR(victim)) {
  412. /* make sure dentry is really there */
  413. if (victim->d_inode &&
  414. (victim->d_parent->d_inode == dir->d_inode)) {
  415. victim->d_inode->i_mtime = CURRENT_TIME;
  416. fsnotify_modify(victim);
  417. res = 0;
  418. } else
  419. d_drop(victim);
  420. /**
  421. * Drop the reference acquired from lookup_one_len() above.
  422. */
  423. dput(victim);
  424. }
  425. mutex_unlock(&dir->d_inode->i_mutex);
  426. return res;
  427. }
  428. /**
  429. * sysfs_chmod_file - update the modified mode value on an object attribute.
  430. * @kobj: object we're acting for.
  431. * @attr: attribute descriptor.
  432. * @mode: file permissions.
  433. *
  434. */
  435. int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
  436. {
  437. struct dentry *dir = kobj->dentry;
  438. struct dentry *victim;
  439. struct inode * inode;
  440. struct iattr newattrs;
  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. if (victim->d_inode &&
  446. (victim->d_parent->d_inode == dir->d_inode)) {
  447. inode = victim->d_inode;
  448. mutex_lock(&inode->i_mutex);
  449. newattrs.ia_mode = (mode & S_IALLUGO) |
  450. (inode->i_mode & ~S_IALLUGO);
  451. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  452. res = notify_change(victim, &newattrs);
  453. mutex_unlock(&inode->i_mutex);
  454. }
  455. dput(victim);
  456. }
  457. mutex_unlock(&dir->d_inode->i_mutex);
  458. return res;
  459. }
  460. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  461. /**
  462. * sysfs_remove_file - remove an object attribute.
  463. * @kobj: object we're acting for.
  464. * @attr: attribute descriptor.
  465. *
  466. * Hash the attribute name and kill the victim.
  467. */
  468. void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
  469. {
  470. sysfs_hash_and_remove(kobj->dentry,attr->name);
  471. }
  472. EXPORT_SYMBOL_GPL(sysfs_create_file);
  473. EXPORT_SYMBOL_GPL(sysfs_remove_file);
  474. EXPORT_SYMBOL_GPL(sysfs_update_file);