file.c 28 KB

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