file.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. /*
  2. * fs/sysfs/file.c - sysfs regular (text) file implementation
  3. *
  4. * Copyright (c) 2001-3 Patrick Mochel
  5. * Copyright (c) 2007 SUSE Linux Products GmbH
  6. * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
  7. *
  8. * This file is released under the GPLv2.
  9. *
  10. * Please see Documentation/filesystems/sysfs.txt for more information.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kobject.h>
  14. #include <linux/kallsyms.h>
  15. #include <linux/slab.h>
  16. #include <linux/fsnotify.h>
  17. #include <linux/namei.h>
  18. #include <linux/poll.h>
  19. #include <linux/list.h>
  20. #include <linux/mutex.h>
  21. #include <linux/limits.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/mm.h>
  25. #include "sysfs.h"
  26. /*
  27. * There's one sysfs_open_file for each open file and one sysfs_open_dirent
  28. * for each sysfs_dirent with one or more open files.
  29. *
  30. * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
  31. * protected by sysfs_open_dirent_lock.
  32. *
  33. * filp->private_data points to seq_file whose ->private points to
  34. * sysfs_open_file. sysfs_open_files are chained at
  35. * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
  36. */
  37. static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
  38. static DEFINE_MUTEX(sysfs_open_file_mutex);
  39. struct sysfs_open_dirent {
  40. atomic_t refcnt;
  41. atomic_t event;
  42. wait_queue_head_t poll;
  43. struct list_head files; /* goes through sysfs_open_file.list */
  44. };
  45. struct sysfs_open_file {
  46. struct sysfs_dirent *sd;
  47. struct file *file;
  48. struct mutex mutex;
  49. int event;
  50. struct list_head list;
  51. bool mmapped;
  52. const struct vm_operations_struct *vm_ops;
  53. };
  54. static bool sysfs_is_bin(struct sysfs_dirent *sd)
  55. {
  56. return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR;
  57. }
  58. static struct sysfs_open_file *sysfs_of(struct file *file)
  59. {
  60. return ((struct seq_file *)file->private_data)->private;
  61. }
  62. /*
  63. * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
  64. * must be called while holding an active reference.
  65. */
  66. static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
  67. {
  68. struct kobject *kobj = sd->s_parent->s_dir.kobj;
  69. if (!sysfs_ignore_lockdep(sd))
  70. lockdep_assert_held(sd);
  71. return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
  72. }
  73. /*
  74. * Reads on sysfs are handled through seq_file, which takes care of hairy
  75. * details like buffering and seeking. The following function pipes
  76. * sysfs_ops->show() result through seq_file.
  77. */
  78. static int sysfs_seq_show(struct seq_file *sf, void *v)
  79. {
  80. struct sysfs_open_file *of = sf->private;
  81. struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
  82. const struct sysfs_ops *ops;
  83. char *buf;
  84. ssize_t count;
  85. /* acquire buffer and ensure that it's >= PAGE_SIZE */
  86. count = seq_get_buf(sf, &buf);
  87. if (count < PAGE_SIZE) {
  88. seq_commit(sf, -1);
  89. return 0;
  90. }
  91. /*
  92. * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
  93. * nests outside active ref and is just to ensure that the ops
  94. * aren't called concurrently for the same open file.
  95. */
  96. mutex_lock(&of->mutex);
  97. if (!sysfs_get_active(of->sd)) {
  98. mutex_unlock(&of->mutex);
  99. return -ENODEV;
  100. }
  101. of->event = atomic_read(&of->sd->s_attr.open->event);
  102. /*
  103. * Lookup @ops and invoke show(). Control may reach here via seq
  104. * file lseek even if @ops->show() isn't implemented.
  105. */
  106. ops = sysfs_file_ops(of->sd);
  107. if (ops->show)
  108. count = ops->show(kobj, of->sd->s_attr.attr, buf);
  109. else
  110. count = 0;
  111. sysfs_put_active(of->sd);
  112. mutex_unlock(&of->mutex);
  113. if (count < 0)
  114. return count;
  115. /*
  116. * The code works fine with PAGE_SIZE return but it's likely to
  117. * indicate truncated result or overflow in normal use cases.
  118. */
  119. if (count >= (ssize_t)PAGE_SIZE) {
  120. print_symbol("fill_read_buffer: %s returned bad count\n",
  121. (unsigned long)ops->show);
  122. /* Try to struggle along */
  123. count = PAGE_SIZE - 1;
  124. }
  125. seq_commit(sf, count);
  126. return 0;
  127. }
  128. /*
  129. * Read method for bin files. As reading a bin file can have side-effects,
  130. * the exact offset and bytes specified in read(2) call should be passed to
  131. * the read callback making it difficult to use seq_file. Implement
  132. * simplistic custom buffering for bin files.
  133. */
  134. static ssize_t sysfs_bin_read(struct file *file, char __user *userbuf,
  135. size_t bytes, loff_t *off)
  136. {
  137. struct sysfs_open_file *of = sysfs_of(file);
  138. struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
  139. struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
  140. loff_t size = file_inode(file)->i_size;
  141. int count = min_t(size_t, bytes, PAGE_SIZE);
  142. loff_t offs = *off;
  143. char *buf;
  144. if (!bytes)
  145. return 0;
  146. if (size) {
  147. if (offs > size)
  148. return 0;
  149. if (offs + count > size)
  150. count = size - offs;
  151. }
  152. buf = kmalloc(count, GFP_KERNEL);
  153. if (!buf)
  154. return -ENOMEM;
  155. /* need of->sd for battr, its parent for kobj */
  156. mutex_lock(&of->mutex);
  157. if (!sysfs_get_active(of->sd)) {
  158. count = -ENODEV;
  159. mutex_unlock(&of->mutex);
  160. goto out_free;
  161. }
  162. if (battr->read)
  163. count = battr->read(file, kobj, battr, buf, offs, count);
  164. else
  165. count = -EIO;
  166. sysfs_put_active(of->sd);
  167. mutex_unlock(&of->mutex);
  168. if (count < 0)
  169. goto out_free;
  170. if (copy_to_user(userbuf, buf, count)) {
  171. count = -EFAULT;
  172. goto out_free;
  173. }
  174. pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
  175. *off = offs + count;
  176. out_free:
  177. kfree(buf);
  178. return count;
  179. }
  180. /**
  181. * flush_write_buffer - push buffer to kobject
  182. * @of: open file
  183. * @buf: data buffer for file
  184. * @off: file offset to write to
  185. * @count: number of bytes
  186. *
  187. * Get the correct pointers for the kobject and the attribute we're dealing
  188. * with, then call the store() method for it with @buf.
  189. */
  190. static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off,
  191. size_t count)
  192. {
  193. struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
  194. int rc = 0;
  195. /*
  196. * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
  197. * nests outside active ref and is just to ensure that the ops
  198. * aren't called concurrently for the same open file.
  199. */
  200. mutex_lock(&of->mutex);
  201. if (!sysfs_get_active(of->sd)) {
  202. mutex_unlock(&of->mutex);
  203. return -ENODEV;
  204. }
  205. if (sysfs_is_bin(of->sd)) {
  206. struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
  207. rc = -EIO;
  208. if (battr->write)
  209. rc = battr->write(of->file, kobj, battr, buf, off,
  210. count);
  211. } else {
  212. const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
  213. rc = ops->store(kobj, of->sd->s_attr.attr, buf, count);
  214. }
  215. sysfs_put_active(of->sd);
  216. mutex_unlock(&of->mutex);
  217. return rc;
  218. }
  219. /**
  220. * sysfs_write_file - write an attribute
  221. * @file: file pointer
  222. * @user_buf: data to write
  223. * @count: number of bytes
  224. * @ppos: starting offset
  225. *
  226. * Copy data in from userland and pass it to the matching
  227. * sysfs_ops->store() by invoking flush_write_buffer().
  228. *
  229. * There is no easy way for us to know if userspace is only doing a partial
  230. * write, so we don't support them. We expect the entire buffer to come on
  231. * the first write. Hint: if you're writing a value, first read the file,
  232. * modify only the the value you're changing, then write entire buffer
  233. * back.
  234. */
  235. static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
  236. size_t count, loff_t *ppos)
  237. {
  238. struct sysfs_open_file *of = sysfs_of(file);
  239. ssize_t len = min_t(size_t, count, PAGE_SIZE);
  240. loff_t size = file_inode(file)->i_size;
  241. char *buf;
  242. if (sysfs_is_bin(of->sd) && size) {
  243. if (size <= *ppos)
  244. return 0;
  245. len = min_t(ssize_t, len, size - *ppos);
  246. }
  247. if (!len)
  248. return 0;
  249. buf = kmalloc(len + 1, GFP_KERNEL);
  250. if (!buf)
  251. return -ENOMEM;
  252. if (copy_from_user(buf, user_buf, len)) {
  253. len = -EFAULT;
  254. goto out_free;
  255. }
  256. buf[len] = '\0'; /* guarantee string termination */
  257. len = flush_write_buffer(of, buf, *ppos, len);
  258. if (len > 0)
  259. *ppos += len;
  260. out_free:
  261. kfree(buf);
  262. return len;
  263. }
  264. static void sysfs_bin_vma_open(struct vm_area_struct *vma)
  265. {
  266. struct file *file = vma->vm_file;
  267. struct sysfs_open_file *of = sysfs_of(file);
  268. if (!of->vm_ops)
  269. return;
  270. if (!sysfs_get_active(of->sd))
  271. return;
  272. if (of->vm_ops->open)
  273. of->vm_ops->open(vma);
  274. sysfs_put_active(of->sd);
  275. }
  276. static int sysfs_bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  277. {
  278. struct file *file = vma->vm_file;
  279. struct sysfs_open_file *of = sysfs_of(file);
  280. int ret;
  281. if (!of->vm_ops)
  282. return VM_FAULT_SIGBUS;
  283. if (!sysfs_get_active(of->sd))
  284. return VM_FAULT_SIGBUS;
  285. ret = VM_FAULT_SIGBUS;
  286. if (of->vm_ops->fault)
  287. ret = of->vm_ops->fault(vma, vmf);
  288. sysfs_put_active(of->sd);
  289. return ret;
  290. }
  291. static int sysfs_bin_page_mkwrite(struct vm_area_struct *vma,
  292. struct vm_fault *vmf)
  293. {
  294. struct file *file = vma->vm_file;
  295. struct sysfs_open_file *of = sysfs_of(file);
  296. int ret;
  297. if (!of->vm_ops)
  298. return VM_FAULT_SIGBUS;
  299. if (!sysfs_get_active(of->sd))
  300. return VM_FAULT_SIGBUS;
  301. ret = 0;
  302. if (of->vm_ops->page_mkwrite)
  303. ret = of->vm_ops->page_mkwrite(vma, vmf);
  304. else
  305. file_update_time(file);
  306. sysfs_put_active(of->sd);
  307. return ret;
  308. }
  309. static int sysfs_bin_access(struct vm_area_struct *vma, unsigned long addr,
  310. void *buf, int len, int write)
  311. {
  312. struct file *file = vma->vm_file;
  313. struct sysfs_open_file *of = sysfs_of(file);
  314. int ret;
  315. if (!of->vm_ops)
  316. return -EINVAL;
  317. if (!sysfs_get_active(of->sd))
  318. return -EINVAL;
  319. ret = -EINVAL;
  320. if (of->vm_ops->access)
  321. ret = of->vm_ops->access(vma, addr, buf, len, write);
  322. sysfs_put_active(of->sd);
  323. return ret;
  324. }
  325. #ifdef CONFIG_NUMA
  326. static int sysfs_bin_set_policy(struct vm_area_struct *vma,
  327. struct mempolicy *new)
  328. {
  329. struct file *file = vma->vm_file;
  330. struct sysfs_open_file *of = sysfs_of(file);
  331. int ret;
  332. if (!of->vm_ops)
  333. return 0;
  334. if (!sysfs_get_active(of->sd))
  335. return -EINVAL;
  336. ret = 0;
  337. if (of->vm_ops->set_policy)
  338. ret = of->vm_ops->set_policy(vma, new);
  339. sysfs_put_active(of->sd);
  340. return ret;
  341. }
  342. static struct mempolicy *sysfs_bin_get_policy(struct vm_area_struct *vma,
  343. unsigned long addr)
  344. {
  345. struct file *file = vma->vm_file;
  346. struct sysfs_open_file *of = sysfs_of(file);
  347. struct mempolicy *pol;
  348. if (!of->vm_ops)
  349. return vma->vm_policy;
  350. if (!sysfs_get_active(of->sd))
  351. return vma->vm_policy;
  352. pol = vma->vm_policy;
  353. if (of->vm_ops->get_policy)
  354. pol = of->vm_ops->get_policy(vma, addr);
  355. sysfs_put_active(of->sd);
  356. return pol;
  357. }
  358. static int sysfs_bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
  359. const nodemask_t *to, unsigned long flags)
  360. {
  361. struct file *file = vma->vm_file;
  362. struct sysfs_open_file *of = sysfs_of(file);
  363. int ret;
  364. if (!of->vm_ops)
  365. return 0;
  366. if (!sysfs_get_active(of->sd))
  367. return 0;
  368. ret = 0;
  369. if (of->vm_ops->migrate)
  370. ret = of->vm_ops->migrate(vma, from, to, flags);
  371. sysfs_put_active(of->sd);
  372. return ret;
  373. }
  374. #endif
  375. static const struct vm_operations_struct sysfs_bin_vm_ops = {
  376. .open = sysfs_bin_vma_open,
  377. .fault = sysfs_bin_fault,
  378. .page_mkwrite = sysfs_bin_page_mkwrite,
  379. .access = sysfs_bin_access,
  380. #ifdef CONFIG_NUMA
  381. .set_policy = sysfs_bin_set_policy,
  382. .get_policy = sysfs_bin_get_policy,
  383. .migrate = sysfs_bin_migrate,
  384. #endif
  385. };
  386. static int sysfs_bin_mmap(struct file *file, struct vm_area_struct *vma)
  387. {
  388. struct sysfs_open_file *of = sysfs_of(file);
  389. struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
  390. struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
  391. int rc;
  392. mutex_lock(&of->mutex);
  393. /* need of->sd for battr, its parent for kobj */
  394. rc = -ENODEV;
  395. if (!sysfs_get_active(of->sd))
  396. goto out_unlock;
  397. rc = -EINVAL;
  398. if (!battr->mmap)
  399. goto out_put;
  400. rc = battr->mmap(file, kobj, battr, vma);
  401. if (rc)
  402. goto out_put;
  403. /*
  404. * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
  405. * to satisfy versions of X which crash if the mmap fails: that
  406. * substitutes a new vm_file, and we don't then want bin_vm_ops.
  407. */
  408. if (vma->vm_file != file)
  409. goto out_put;
  410. rc = -EINVAL;
  411. if (of->mmapped && of->vm_ops != vma->vm_ops)
  412. goto out_put;
  413. /*
  414. * It is not possible to successfully wrap close.
  415. * So error if someone is trying to use close.
  416. */
  417. rc = -EINVAL;
  418. if (vma->vm_ops && vma->vm_ops->close)
  419. goto out_put;
  420. rc = 0;
  421. of->mmapped = 1;
  422. of->vm_ops = vma->vm_ops;
  423. vma->vm_ops = &sysfs_bin_vm_ops;
  424. out_put:
  425. sysfs_put_active(of->sd);
  426. out_unlock:
  427. mutex_unlock(&of->mutex);
  428. return rc;
  429. }
  430. /**
  431. * sysfs_get_open_dirent - get or create sysfs_open_dirent
  432. * @sd: target sysfs_dirent
  433. * @of: sysfs_open_file for this instance of open
  434. *
  435. * If @sd->s_attr.open exists, increment its reference count;
  436. * otherwise, create one. @of is chained to the files list.
  437. *
  438. * LOCKING:
  439. * Kernel thread context (may sleep).
  440. *
  441. * RETURNS:
  442. * 0 on success, -errno on failure.
  443. */
  444. static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
  445. struct sysfs_open_file *of)
  446. {
  447. struct sysfs_open_dirent *od, *new_od = NULL;
  448. retry:
  449. mutex_lock(&sysfs_open_file_mutex);
  450. spin_lock_irq(&sysfs_open_dirent_lock);
  451. if (!sd->s_attr.open && new_od) {
  452. sd->s_attr.open = new_od;
  453. new_od = NULL;
  454. }
  455. od = sd->s_attr.open;
  456. if (od) {
  457. atomic_inc(&od->refcnt);
  458. list_add_tail(&of->list, &od->files);
  459. }
  460. spin_unlock_irq(&sysfs_open_dirent_lock);
  461. mutex_unlock(&sysfs_open_file_mutex);
  462. if (od) {
  463. kfree(new_od);
  464. return 0;
  465. }
  466. /* not there, initialize a new one and retry */
  467. new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
  468. if (!new_od)
  469. return -ENOMEM;
  470. atomic_set(&new_od->refcnt, 0);
  471. atomic_set(&new_od->event, 1);
  472. init_waitqueue_head(&new_od->poll);
  473. INIT_LIST_HEAD(&new_od->files);
  474. goto retry;
  475. }
  476. /**
  477. * sysfs_put_open_dirent - put sysfs_open_dirent
  478. * @sd: target sysfs_dirent
  479. * @of: associated sysfs_open_file
  480. *
  481. * Put @sd->s_attr.open and unlink @of from the files list. If
  482. * reference count reaches zero, disassociate and free it.
  483. *
  484. * LOCKING:
  485. * None.
  486. */
  487. static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
  488. struct sysfs_open_file *of)
  489. {
  490. struct sysfs_open_dirent *od = sd->s_attr.open;
  491. unsigned long flags;
  492. mutex_lock(&sysfs_open_file_mutex);
  493. spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
  494. if (of)
  495. list_del(&of->list);
  496. if (atomic_dec_and_test(&od->refcnt))
  497. sd->s_attr.open = NULL;
  498. else
  499. od = NULL;
  500. spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
  501. mutex_unlock(&sysfs_open_file_mutex);
  502. kfree(od);
  503. }
  504. static int sysfs_open_file(struct inode *inode, struct file *file)
  505. {
  506. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  507. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  508. struct sysfs_open_file *of;
  509. bool has_read, has_write;
  510. int error = -EACCES;
  511. /* need attr_sd for attr and ops, its parent for kobj */
  512. if (!sysfs_get_active(attr_sd))
  513. return -ENODEV;
  514. if (sysfs_is_bin(attr_sd)) {
  515. struct bin_attribute *battr = attr_sd->s_bin_attr.bin_attr;
  516. has_read = battr->read || battr->mmap;
  517. has_write = battr->write || battr->mmap;
  518. } else {
  519. const struct sysfs_ops *ops = sysfs_file_ops(attr_sd);
  520. /* every kobject with an attribute needs a ktype assigned */
  521. if (WARN(!ops, KERN_ERR
  522. "missing sysfs attribute operations for kobject: %s\n",
  523. kobject_name(kobj)))
  524. goto err_out;
  525. has_read = ops->show;
  526. has_write = ops->store;
  527. }
  528. /* check perms and supported operations */
  529. if ((file->f_mode & FMODE_WRITE) &&
  530. (!(inode->i_mode & S_IWUGO) || !has_write))
  531. goto err_out;
  532. if ((file->f_mode & FMODE_READ) &&
  533. (!(inode->i_mode & S_IRUGO) || !has_read))
  534. goto err_out;
  535. /* allocate a sysfs_open_file for the file */
  536. error = -ENOMEM;
  537. of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
  538. if (!of)
  539. goto err_out;
  540. mutex_init(&of->mutex);
  541. of->sd = attr_sd;
  542. of->file = file;
  543. /*
  544. * Always instantiate seq_file even if read access doesn't use
  545. * seq_file or is not requested. This unifies private data access
  546. * and readable regular files are the vast majority anyway.
  547. */
  548. if (sysfs_is_bin(attr_sd))
  549. error = single_open(file, NULL, of);
  550. else
  551. error = single_open(file, sysfs_seq_show, of);
  552. if (error)
  553. goto err_free;
  554. /* seq_file clears PWRITE unconditionally, restore it if WRITE */
  555. if (file->f_mode & FMODE_WRITE)
  556. file->f_mode |= FMODE_PWRITE;
  557. /* make sure we have open dirent struct */
  558. error = sysfs_get_open_dirent(attr_sd, of);
  559. if (error)
  560. goto err_close;
  561. /* open succeeded, put active references */
  562. sysfs_put_active(attr_sd);
  563. return 0;
  564. err_close:
  565. single_release(inode, file);
  566. err_free:
  567. kfree(of);
  568. err_out:
  569. sysfs_put_active(attr_sd);
  570. return error;
  571. }
  572. static int sysfs_release(struct inode *inode, struct file *filp)
  573. {
  574. struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
  575. struct sysfs_open_file *of = sysfs_of(filp);
  576. sysfs_put_open_dirent(sd, of);
  577. single_release(inode, filp);
  578. kfree(of);
  579. return 0;
  580. }
  581. void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
  582. {
  583. struct sysfs_open_dirent *od;
  584. struct sysfs_open_file *of;
  585. if (!sysfs_is_bin(sd))
  586. return;
  587. spin_lock_irq(&sysfs_open_dirent_lock);
  588. od = sd->s_attr.open;
  589. if (od)
  590. atomic_inc(&od->refcnt);
  591. spin_unlock_irq(&sysfs_open_dirent_lock);
  592. if (!od)
  593. return;
  594. mutex_lock(&sysfs_open_file_mutex);
  595. list_for_each_entry(of, &od->files, list) {
  596. struct inode *inode = file_inode(of->file);
  597. unmap_mapping_range(inode->i_mapping, 0, 0, 1);
  598. }
  599. mutex_unlock(&sysfs_open_file_mutex);
  600. sysfs_put_open_dirent(sd, NULL);
  601. }
  602. /* Sysfs attribute files are pollable. The idea is that you read
  603. * the content and then you use 'poll' or 'select' to wait for
  604. * the content to change. When the content changes (assuming the
  605. * manager for the kobject supports notification), poll will
  606. * return POLLERR|POLLPRI, and select will return the fd whether
  607. * it is waiting for read, write, or exceptions.
  608. * Once poll/select indicates that the value has changed, you
  609. * need to close and re-open the file, or seek to 0 and read again.
  610. * Reminder: this only works for attributes which actively support
  611. * it, and it is not possible to test an attribute from userspace
  612. * to see if it supports poll (Neither 'poll' nor 'select' return
  613. * an appropriate error code). When in doubt, set a suitable timeout value.
  614. */
  615. static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
  616. {
  617. struct sysfs_open_file *of = sysfs_of(filp);
  618. struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
  619. struct sysfs_open_dirent *od = attr_sd->s_attr.open;
  620. /* need parent for the kobj, grab both */
  621. if (!sysfs_get_active(attr_sd))
  622. goto trigger;
  623. poll_wait(filp, &od->poll, wait);
  624. sysfs_put_active(attr_sd);
  625. if (of->event != atomic_read(&od->event))
  626. goto trigger;
  627. return DEFAULT_POLLMASK;
  628. trigger:
  629. return DEFAULT_POLLMASK|POLLERR|POLLPRI;
  630. }
  631. void sysfs_notify_dirent(struct sysfs_dirent *sd)
  632. {
  633. struct sysfs_open_dirent *od;
  634. unsigned long flags;
  635. spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
  636. if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
  637. od = sd->s_attr.open;
  638. if (od) {
  639. atomic_inc(&od->event);
  640. wake_up_interruptible(&od->poll);
  641. }
  642. }
  643. spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
  644. }
  645. EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
  646. void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
  647. {
  648. struct sysfs_dirent *sd = k->sd;
  649. mutex_lock(&sysfs_mutex);
  650. if (sd && dir)
  651. sd = sysfs_find_dirent(sd, dir, NULL);
  652. if (sd && attr)
  653. sd = sysfs_find_dirent(sd, attr, NULL);
  654. if (sd)
  655. sysfs_notify_dirent(sd);
  656. mutex_unlock(&sysfs_mutex);
  657. }
  658. EXPORT_SYMBOL_GPL(sysfs_notify);
  659. const struct file_operations sysfs_file_operations = {
  660. .read = seq_read,
  661. .write = sysfs_write_file,
  662. .llseek = seq_lseek,
  663. .open = sysfs_open_file,
  664. .release = sysfs_release,
  665. .poll = sysfs_poll,
  666. };
  667. const struct file_operations sysfs_bin_operations = {
  668. .read = sysfs_bin_read,
  669. .write = sysfs_write_file,
  670. .llseek = generic_file_llseek,
  671. .mmap = sysfs_bin_mmap,
  672. .open = sysfs_open_file,
  673. .release = sysfs_release,
  674. .poll = sysfs_poll,
  675. };
  676. int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
  677. const struct attribute *attr, int type,
  678. umode_t amode, const void *ns)
  679. {
  680. umode_t mode = (amode & S_IALLUGO) | S_IFREG;
  681. struct sysfs_addrm_cxt acxt;
  682. struct sysfs_dirent *sd;
  683. int rc;
  684. sd = sysfs_new_dirent(attr->name, mode, type);
  685. if (!sd)
  686. return -ENOMEM;
  687. sd->s_ns = ns;
  688. sd->s_attr.attr = (void *)attr;
  689. sysfs_dirent_init_lockdep(sd);
  690. sysfs_addrm_start(&acxt);
  691. rc = sysfs_add_one(&acxt, sd, dir_sd);
  692. sysfs_addrm_finish(&acxt);
  693. if (rc)
  694. sysfs_put(sd);
  695. return rc;
  696. }
  697. int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
  698. int type)
  699. {
  700. return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
  701. }
  702. /**
  703. * sysfs_create_file_ns - create an attribute file for an object with custom ns
  704. * @kobj: object we're creating for
  705. * @attr: attribute descriptor
  706. * @ns: namespace the new file should belong to
  707. */
  708. int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
  709. const void *ns)
  710. {
  711. BUG_ON(!kobj || !kobj->sd || !attr);
  712. return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
  713. attr->mode, ns);
  714. }
  715. EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
  716. int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
  717. {
  718. int err = 0;
  719. int i;
  720. for (i = 0; ptr[i] && !err; i++)
  721. err = sysfs_create_file(kobj, ptr[i]);
  722. if (err)
  723. while (--i >= 0)
  724. sysfs_remove_file(kobj, ptr[i]);
  725. return err;
  726. }
  727. EXPORT_SYMBOL_GPL(sysfs_create_files);
  728. /**
  729. * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
  730. * @kobj: object we're acting for.
  731. * @attr: attribute descriptor.
  732. * @group: group name.
  733. */
  734. int sysfs_add_file_to_group(struct kobject *kobj,
  735. const struct attribute *attr, const char *group)
  736. {
  737. struct sysfs_dirent *dir_sd;
  738. int error;
  739. if (group)
  740. dir_sd = sysfs_get_dirent(kobj->sd, group);
  741. else
  742. dir_sd = sysfs_get(kobj->sd);
  743. if (!dir_sd)
  744. return -ENOENT;
  745. error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
  746. sysfs_put(dir_sd);
  747. return error;
  748. }
  749. EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
  750. /**
  751. * sysfs_chmod_file - update the modified mode value on an object attribute.
  752. * @kobj: object we're acting for.
  753. * @attr: attribute descriptor.
  754. * @mode: file permissions.
  755. *
  756. */
  757. int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
  758. umode_t mode)
  759. {
  760. struct sysfs_dirent *sd;
  761. struct iattr newattrs;
  762. int rc;
  763. mutex_lock(&sysfs_mutex);
  764. rc = -ENOENT;
  765. sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
  766. if (!sd)
  767. goto out;
  768. newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
  769. newattrs.ia_valid = ATTR_MODE;
  770. rc = sysfs_sd_setattr(sd, &newattrs);
  771. out:
  772. mutex_unlock(&sysfs_mutex);
  773. return rc;
  774. }
  775. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  776. /**
  777. * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
  778. * @kobj: object we're acting for
  779. * @attr: attribute descriptor
  780. * @ns: namespace tag of the file to remove
  781. *
  782. * Hash the attribute name and namespace tag and kill the victim.
  783. */
  784. void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
  785. const void *ns)
  786. {
  787. struct sysfs_dirent *dir_sd = kobj->sd;
  788. sysfs_hash_and_remove(dir_sd, attr->name, ns);
  789. }
  790. EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
  791. void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
  792. {
  793. int i;
  794. for (i = 0; ptr[i]; i++)
  795. sysfs_remove_file(kobj, ptr[i]);
  796. }
  797. EXPORT_SYMBOL_GPL(sysfs_remove_files);
  798. /**
  799. * sysfs_remove_file_from_group - remove an attribute file from a group.
  800. * @kobj: object we're acting for.
  801. * @attr: attribute descriptor.
  802. * @group: group name.
  803. */
  804. void sysfs_remove_file_from_group(struct kobject *kobj,
  805. const struct attribute *attr, const char *group)
  806. {
  807. struct sysfs_dirent *dir_sd;
  808. if (group)
  809. dir_sd = sysfs_get_dirent(kobj->sd, group);
  810. else
  811. dir_sd = sysfs_get(kobj->sd);
  812. if (dir_sd) {
  813. sysfs_hash_and_remove(dir_sd, attr->name, NULL);
  814. sysfs_put(dir_sd);
  815. }
  816. }
  817. EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
  818. /**
  819. * sysfs_create_bin_file - create binary file for object.
  820. * @kobj: object.
  821. * @attr: attribute descriptor.
  822. */
  823. int sysfs_create_bin_file(struct kobject *kobj,
  824. const struct bin_attribute *attr)
  825. {
  826. BUG_ON(!kobj || !kobj->sd || !attr);
  827. return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
  828. }
  829. EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
  830. /**
  831. * sysfs_remove_bin_file - remove binary file for object.
  832. * @kobj: object.
  833. * @attr: attribute descriptor.
  834. */
  835. void sysfs_remove_bin_file(struct kobject *kobj,
  836. const struct bin_attribute *attr)
  837. {
  838. sysfs_hash_and_remove(kobj->sd, attr->attr.name, NULL);
  839. }
  840. EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
  841. struct sysfs_schedule_callback_struct {
  842. struct list_head workq_list;
  843. struct kobject *kobj;
  844. void (*func)(void *);
  845. void *data;
  846. struct module *owner;
  847. struct work_struct work;
  848. };
  849. static struct workqueue_struct *sysfs_workqueue;
  850. static DEFINE_MUTEX(sysfs_workq_mutex);
  851. static LIST_HEAD(sysfs_workq);
  852. static void sysfs_schedule_callback_work(struct work_struct *work)
  853. {
  854. struct sysfs_schedule_callback_struct *ss = container_of(work,
  855. struct sysfs_schedule_callback_struct, work);
  856. (ss->func)(ss->data);
  857. kobject_put(ss->kobj);
  858. module_put(ss->owner);
  859. mutex_lock(&sysfs_workq_mutex);
  860. list_del(&ss->workq_list);
  861. mutex_unlock(&sysfs_workq_mutex);
  862. kfree(ss);
  863. }
  864. /**
  865. * sysfs_schedule_callback - helper to schedule a callback for a kobject
  866. * @kobj: object we're acting for.
  867. * @func: callback function to invoke later.
  868. * @data: argument to pass to @func.
  869. * @owner: module owning the callback code
  870. *
  871. * sysfs attribute methods must not unregister themselves or their parent
  872. * kobject (which would amount to the same thing). Attempts to do so will
  873. * deadlock, since unregistration is mutually exclusive with driver
  874. * callbacks.
  875. *
  876. * Instead methods can call this routine, which will attempt to allocate
  877. * and schedule a workqueue request to call back @func with @data as its
  878. * argument in the workqueue's process context. @kobj will be pinned
  879. * until @func returns.
  880. *
  881. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  882. * be allocated, -ENODEV if a reference to @owner isn't available,
  883. * -EAGAIN if a callback has already been scheduled for @kobj.
  884. */
  885. int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
  886. void *data, struct module *owner)
  887. {
  888. struct sysfs_schedule_callback_struct *ss, *tmp;
  889. if (!try_module_get(owner))
  890. return -ENODEV;
  891. mutex_lock(&sysfs_workq_mutex);
  892. list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
  893. if (ss->kobj == kobj) {
  894. module_put(owner);
  895. mutex_unlock(&sysfs_workq_mutex);
  896. return -EAGAIN;
  897. }
  898. mutex_unlock(&sysfs_workq_mutex);
  899. if (sysfs_workqueue == NULL) {
  900. sysfs_workqueue = create_singlethread_workqueue("sysfsd");
  901. if (sysfs_workqueue == NULL) {
  902. module_put(owner);
  903. return -ENOMEM;
  904. }
  905. }
  906. ss = kmalloc(sizeof(*ss), GFP_KERNEL);
  907. if (!ss) {
  908. module_put(owner);
  909. return -ENOMEM;
  910. }
  911. kobject_get(kobj);
  912. ss->kobj = kobj;
  913. ss->func = func;
  914. ss->data = data;
  915. ss->owner = owner;
  916. INIT_WORK(&ss->work, sysfs_schedule_callback_work);
  917. INIT_LIST_HEAD(&ss->workq_list);
  918. mutex_lock(&sysfs_workq_mutex);
  919. list_add_tail(&ss->workq_list, &sysfs_workq);
  920. mutex_unlock(&sysfs_workq_mutex);
  921. queue_work(sysfs_workqueue, &ss->work);
  922. return 0;
  923. }
  924. EXPORT_SYMBOL_GPL(sysfs_schedule_callback);