file.c 26 KB

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