file.c 28 KB

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