dev.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU GPL.
  5. See the file COPYING.
  6. */
  7. #include "fuse_i.h"
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/poll.h>
  11. #include <linux/uio.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/file.h>
  15. #include <linux/slab.h>
  16. MODULE_ALIAS_MISCDEV(FUSE_MINOR);
  17. static struct kmem_cache *fuse_req_cachep;
  18. static struct fuse_conn *fuse_get_conn(struct file *file)
  19. {
  20. /*
  21. * Lockless access is OK, because file->private data is set
  22. * once during mount and is valid until the file is released.
  23. */
  24. return file->private_data;
  25. }
  26. static void fuse_request_init(struct fuse_req *req)
  27. {
  28. memset(req, 0, sizeof(*req));
  29. INIT_LIST_HEAD(&req->list);
  30. INIT_LIST_HEAD(&req->intr_entry);
  31. init_waitqueue_head(&req->waitq);
  32. atomic_set(&req->count, 1);
  33. }
  34. struct fuse_req *fuse_request_alloc(void)
  35. {
  36. struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_KERNEL);
  37. if (req)
  38. fuse_request_init(req);
  39. return req;
  40. }
  41. struct fuse_req *fuse_request_alloc_nofs(void)
  42. {
  43. struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_NOFS);
  44. if (req)
  45. fuse_request_init(req);
  46. return req;
  47. }
  48. void fuse_request_free(struct fuse_req *req)
  49. {
  50. kmem_cache_free(fuse_req_cachep, req);
  51. }
  52. static void block_sigs(sigset_t *oldset)
  53. {
  54. sigset_t mask;
  55. siginitsetinv(&mask, sigmask(SIGKILL));
  56. sigprocmask(SIG_BLOCK, &mask, oldset);
  57. }
  58. static void restore_sigs(sigset_t *oldset)
  59. {
  60. sigprocmask(SIG_SETMASK, oldset, NULL);
  61. }
  62. static void __fuse_get_request(struct fuse_req *req)
  63. {
  64. atomic_inc(&req->count);
  65. }
  66. /* Must be called with > 1 refcount */
  67. static void __fuse_put_request(struct fuse_req *req)
  68. {
  69. BUG_ON(atomic_read(&req->count) < 2);
  70. atomic_dec(&req->count);
  71. }
  72. static void fuse_req_init_context(struct fuse_req *req)
  73. {
  74. req->in.h.uid = current->fsuid;
  75. req->in.h.gid = current->fsgid;
  76. req->in.h.pid = current->pid;
  77. }
  78. struct fuse_req *fuse_get_req(struct fuse_conn *fc)
  79. {
  80. struct fuse_req *req;
  81. sigset_t oldset;
  82. int intr;
  83. int err;
  84. atomic_inc(&fc->num_waiting);
  85. block_sigs(&oldset);
  86. intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
  87. restore_sigs(&oldset);
  88. err = -EINTR;
  89. if (intr)
  90. goto out;
  91. err = -ENOTCONN;
  92. if (!fc->connected)
  93. goto out;
  94. req = fuse_request_alloc();
  95. err = -ENOMEM;
  96. if (!req)
  97. goto out;
  98. fuse_req_init_context(req);
  99. req->waiting = 1;
  100. return req;
  101. out:
  102. atomic_dec(&fc->num_waiting);
  103. return ERR_PTR(err);
  104. }
  105. /*
  106. * Return request in fuse_file->reserved_req. However that may
  107. * currently be in use. If that is the case, wait for it to become
  108. * available.
  109. */
  110. static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
  111. struct file *file)
  112. {
  113. struct fuse_req *req = NULL;
  114. struct fuse_file *ff = file->private_data;
  115. do {
  116. wait_event(fc->reserved_req_waitq, ff->reserved_req);
  117. spin_lock(&fc->lock);
  118. if (ff->reserved_req) {
  119. req = ff->reserved_req;
  120. ff->reserved_req = NULL;
  121. get_file(file);
  122. req->stolen_file = file;
  123. }
  124. spin_unlock(&fc->lock);
  125. } while (!req);
  126. return req;
  127. }
  128. /*
  129. * Put stolen request back into fuse_file->reserved_req
  130. */
  131. static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
  132. {
  133. struct file *file = req->stolen_file;
  134. struct fuse_file *ff = file->private_data;
  135. spin_lock(&fc->lock);
  136. fuse_request_init(req);
  137. BUG_ON(ff->reserved_req);
  138. ff->reserved_req = req;
  139. wake_up_all(&fc->reserved_req_waitq);
  140. spin_unlock(&fc->lock);
  141. fput(file);
  142. }
  143. /*
  144. * Gets a requests for a file operation, always succeeds
  145. *
  146. * This is used for sending the FLUSH request, which must get to
  147. * userspace, due to POSIX locks which may need to be unlocked.
  148. *
  149. * If allocation fails due to OOM, use the reserved request in
  150. * fuse_file.
  151. *
  152. * This is very unlikely to deadlock accidentally, since the
  153. * filesystem should not have it's own file open. If deadlock is
  154. * intentional, it can still be broken by "aborting" the filesystem.
  155. */
  156. struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
  157. {
  158. struct fuse_req *req;
  159. atomic_inc(&fc->num_waiting);
  160. wait_event(fc->blocked_waitq, !fc->blocked);
  161. req = fuse_request_alloc();
  162. if (!req)
  163. req = get_reserved_req(fc, file);
  164. fuse_req_init_context(req);
  165. req->waiting = 1;
  166. return req;
  167. }
  168. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
  169. {
  170. if (atomic_dec_and_test(&req->count)) {
  171. if (req->waiting)
  172. atomic_dec(&fc->num_waiting);
  173. if (req->stolen_file)
  174. put_reserved_req(fc, req);
  175. else
  176. fuse_request_free(req);
  177. }
  178. }
  179. static unsigned len_args(unsigned numargs, struct fuse_arg *args)
  180. {
  181. unsigned nbytes = 0;
  182. unsigned i;
  183. for (i = 0; i < numargs; i++)
  184. nbytes += args[i].size;
  185. return nbytes;
  186. }
  187. static u64 fuse_get_unique(struct fuse_conn *fc)
  188. {
  189. fc->reqctr++;
  190. /* zero is special */
  191. if (fc->reqctr == 0)
  192. fc->reqctr = 1;
  193. return fc->reqctr;
  194. }
  195. static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
  196. {
  197. req->in.h.unique = fuse_get_unique(fc);
  198. req->in.h.len = sizeof(struct fuse_in_header) +
  199. len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
  200. list_add_tail(&req->list, &fc->pending);
  201. req->state = FUSE_REQ_PENDING;
  202. if (!req->waiting) {
  203. req->waiting = 1;
  204. atomic_inc(&fc->num_waiting);
  205. }
  206. wake_up(&fc->waitq);
  207. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  208. }
  209. static void flush_bg_queue(struct fuse_conn *fc)
  210. {
  211. while (fc->active_background < FUSE_MAX_BACKGROUND &&
  212. !list_empty(&fc->bg_queue)) {
  213. struct fuse_req *req;
  214. req = list_entry(fc->bg_queue.next, struct fuse_req, list);
  215. list_del(&req->list);
  216. fc->active_background++;
  217. queue_request(fc, req);
  218. }
  219. }
  220. /*
  221. * This function is called when a request is finished. Either a reply
  222. * has arrived or it was aborted (and not yet sent) or some error
  223. * occurred during communication with userspace, or the device file
  224. * was closed. The requester thread is woken up (if still waiting),
  225. * the 'end' callback is called if given, else the reference to the
  226. * request is released
  227. *
  228. * Called with fc->lock, unlocks it
  229. */
  230. static void request_end(struct fuse_conn *fc, struct fuse_req *req)
  231. __releases(fc->lock)
  232. {
  233. void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
  234. req->end = NULL;
  235. list_del(&req->list);
  236. list_del(&req->intr_entry);
  237. req->state = FUSE_REQ_FINISHED;
  238. if (req->background) {
  239. if (fc->num_background == FUSE_MAX_BACKGROUND) {
  240. fc->blocked = 0;
  241. wake_up_all(&fc->blocked_waitq);
  242. }
  243. if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
  244. clear_bdi_congested(&fc->bdi, READ);
  245. clear_bdi_congested(&fc->bdi, WRITE);
  246. }
  247. fc->num_background--;
  248. fc->active_background--;
  249. flush_bg_queue(fc);
  250. }
  251. spin_unlock(&fc->lock);
  252. wake_up(&req->waitq);
  253. if (end)
  254. end(fc, req);
  255. fuse_put_request(fc, req);
  256. }
  257. static void wait_answer_interruptible(struct fuse_conn *fc,
  258. struct fuse_req *req)
  259. __releases(fc->lock) __acquires(fc->lock)
  260. {
  261. if (signal_pending(current))
  262. return;
  263. spin_unlock(&fc->lock);
  264. wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
  265. spin_lock(&fc->lock);
  266. }
  267. static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
  268. {
  269. list_add_tail(&req->intr_entry, &fc->interrupts);
  270. wake_up(&fc->waitq);
  271. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  272. }
  273. static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
  274. __releases(fc->lock) __acquires(fc->lock)
  275. {
  276. if (!fc->no_interrupt) {
  277. /* Any signal may interrupt this */
  278. wait_answer_interruptible(fc, req);
  279. if (req->aborted)
  280. goto aborted;
  281. if (req->state == FUSE_REQ_FINISHED)
  282. return;
  283. req->interrupted = 1;
  284. if (req->state == FUSE_REQ_SENT)
  285. queue_interrupt(fc, req);
  286. }
  287. if (!req->force) {
  288. sigset_t oldset;
  289. /* Only fatal signals may interrupt this */
  290. block_sigs(&oldset);
  291. wait_answer_interruptible(fc, req);
  292. restore_sigs(&oldset);
  293. if (req->aborted)
  294. goto aborted;
  295. if (req->state == FUSE_REQ_FINISHED)
  296. return;
  297. /* Request is not yet in userspace, bail out */
  298. if (req->state == FUSE_REQ_PENDING) {
  299. list_del(&req->list);
  300. __fuse_put_request(req);
  301. req->out.h.error = -EINTR;
  302. return;
  303. }
  304. }
  305. /*
  306. * Either request is already in userspace, or it was forced.
  307. * Wait it out.
  308. */
  309. spin_unlock(&fc->lock);
  310. wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
  311. spin_lock(&fc->lock);
  312. if (!req->aborted)
  313. return;
  314. aborted:
  315. BUG_ON(req->state != FUSE_REQ_FINISHED);
  316. if (req->locked) {
  317. /* This is uninterruptible sleep, because data is
  318. being copied to/from the buffers of req. During
  319. locked state, there mustn't be any filesystem
  320. operation (e.g. page fault), since that could lead
  321. to deadlock */
  322. spin_unlock(&fc->lock);
  323. wait_event(req->waitq, !req->locked);
  324. spin_lock(&fc->lock);
  325. }
  326. }
  327. void request_send(struct fuse_conn *fc, struct fuse_req *req)
  328. {
  329. req->isreply = 1;
  330. spin_lock(&fc->lock);
  331. if (!fc->connected)
  332. req->out.h.error = -ENOTCONN;
  333. else if (fc->conn_error)
  334. req->out.h.error = -ECONNREFUSED;
  335. else {
  336. queue_request(fc, req);
  337. /* acquire extra reference, since request is still needed
  338. after request_end() */
  339. __fuse_get_request(req);
  340. request_wait_answer(fc, req);
  341. }
  342. spin_unlock(&fc->lock);
  343. }
  344. static void request_send_nowait_locked(struct fuse_conn *fc,
  345. struct fuse_req *req)
  346. {
  347. req->background = 1;
  348. fc->num_background++;
  349. if (fc->num_background == FUSE_MAX_BACKGROUND)
  350. fc->blocked = 1;
  351. if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
  352. set_bdi_congested(&fc->bdi, READ);
  353. set_bdi_congested(&fc->bdi, WRITE);
  354. }
  355. list_add_tail(&req->list, &fc->bg_queue);
  356. flush_bg_queue(fc);
  357. }
  358. static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
  359. {
  360. spin_lock(&fc->lock);
  361. if (fc->connected) {
  362. request_send_nowait_locked(fc, req);
  363. spin_unlock(&fc->lock);
  364. } else {
  365. req->out.h.error = -ENOTCONN;
  366. request_end(fc, req);
  367. }
  368. }
  369. void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
  370. {
  371. req->isreply = 0;
  372. request_send_nowait(fc, req);
  373. }
  374. void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
  375. {
  376. req->isreply = 1;
  377. request_send_nowait(fc, req);
  378. }
  379. /*
  380. * Called under fc->lock
  381. *
  382. * fc->connected must have been checked previously
  383. */
  384. void request_send_background_locked(struct fuse_conn *fc, struct fuse_req *req)
  385. {
  386. req->isreply = 1;
  387. request_send_nowait_locked(fc, req);
  388. }
  389. /*
  390. * Lock the request. Up to the next unlock_request() there mustn't be
  391. * anything that could cause a page-fault. If the request was already
  392. * aborted bail out.
  393. */
  394. static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
  395. {
  396. int err = 0;
  397. if (req) {
  398. spin_lock(&fc->lock);
  399. if (req->aborted)
  400. err = -ENOENT;
  401. else
  402. req->locked = 1;
  403. spin_unlock(&fc->lock);
  404. }
  405. return err;
  406. }
  407. /*
  408. * Unlock request. If it was aborted during being locked, the
  409. * requester thread is currently waiting for it to be unlocked, so
  410. * wake it up.
  411. */
  412. static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
  413. {
  414. if (req) {
  415. spin_lock(&fc->lock);
  416. req->locked = 0;
  417. if (req->aborted)
  418. wake_up(&req->waitq);
  419. spin_unlock(&fc->lock);
  420. }
  421. }
  422. struct fuse_copy_state {
  423. struct fuse_conn *fc;
  424. int write;
  425. struct fuse_req *req;
  426. const struct iovec *iov;
  427. unsigned long nr_segs;
  428. unsigned long seglen;
  429. unsigned long addr;
  430. struct page *pg;
  431. void *mapaddr;
  432. void *buf;
  433. unsigned len;
  434. };
  435. static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
  436. int write, struct fuse_req *req,
  437. const struct iovec *iov, unsigned long nr_segs)
  438. {
  439. memset(cs, 0, sizeof(*cs));
  440. cs->fc = fc;
  441. cs->write = write;
  442. cs->req = req;
  443. cs->iov = iov;
  444. cs->nr_segs = nr_segs;
  445. }
  446. /* Unmap and put previous page of userspace buffer */
  447. static void fuse_copy_finish(struct fuse_copy_state *cs)
  448. {
  449. if (cs->mapaddr) {
  450. kunmap_atomic(cs->mapaddr, KM_USER0);
  451. if (cs->write) {
  452. flush_dcache_page(cs->pg);
  453. set_page_dirty_lock(cs->pg);
  454. }
  455. put_page(cs->pg);
  456. cs->mapaddr = NULL;
  457. }
  458. }
  459. /*
  460. * Get another pagefull of userspace buffer, and map it to kernel
  461. * address space, and lock request
  462. */
  463. static int fuse_copy_fill(struct fuse_copy_state *cs)
  464. {
  465. unsigned long offset;
  466. int err;
  467. unlock_request(cs->fc, cs->req);
  468. fuse_copy_finish(cs);
  469. if (!cs->seglen) {
  470. BUG_ON(!cs->nr_segs);
  471. cs->seglen = cs->iov[0].iov_len;
  472. cs->addr = (unsigned long) cs->iov[0].iov_base;
  473. cs->iov++;
  474. cs->nr_segs--;
  475. }
  476. down_read(&current->mm->mmap_sem);
  477. err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
  478. &cs->pg, NULL);
  479. up_read(&current->mm->mmap_sem);
  480. if (err < 0)
  481. return err;
  482. BUG_ON(err != 1);
  483. offset = cs->addr % PAGE_SIZE;
  484. cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
  485. cs->buf = cs->mapaddr + offset;
  486. cs->len = min(PAGE_SIZE - offset, cs->seglen);
  487. cs->seglen -= cs->len;
  488. cs->addr += cs->len;
  489. return lock_request(cs->fc, cs->req);
  490. }
  491. /* Do as much copy to/from userspace buffer as we can */
  492. static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
  493. {
  494. unsigned ncpy = min(*size, cs->len);
  495. if (val) {
  496. if (cs->write)
  497. memcpy(cs->buf, *val, ncpy);
  498. else
  499. memcpy(*val, cs->buf, ncpy);
  500. *val += ncpy;
  501. }
  502. *size -= ncpy;
  503. cs->len -= ncpy;
  504. cs->buf += ncpy;
  505. return ncpy;
  506. }
  507. /*
  508. * Copy a page in the request to/from the userspace buffer. Must be
  509. * done atomically
  510. */
  511. static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
  512. unsigned offset, unsigned count, int zeroing)
  513. {
  514. if (page && zeroing && count < PAGE_SIZE) {
  515. void *mapaddr = kmap_atomic(page, KM_USER1);
  516. memset(mapaddr, 0, PAGE_SIZE);
  517. kunmap_atomic(mapaddr, KM_USER1);
  518. }
  519. while (count) {
  520. if (!cs->len) {
  521. int err = fuse_copy_fill(cs);
  522. if (err)
  523. return err;
  524. }
  525. if (page) {
  526. void *mapaddr = kmap_atomic(page, KM_USER1);
  527. void *buf = mapaddr + offset;
  528. offset += fuse_copy_do(cs, &buf, &count);
  529. kunmap_atomic(mapaddr, KM_USER1);
  530. } else
  531. offset += fuse_copy_do(cs, NULL, &count);
  532. }
  533. if (page && !cs->write)
  534. flush_dcache_page(page);
  535. return 0;
  536. }
  537. /* Copy pages in the request to/from userspace buffer */
  538. static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
  539. int zeroing)
  540. {
  541. unsigned i;
  542. struct fuse_req *req = cs->req;
  543. unsigned offset = req->page_offset;
  544. unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
  545. for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
  546. struct page *page = req->pages[i];
  547. int err = fuse_copy_page(cs, page, offset, count, zeroing);
  548. if (err)
  549. return err;
  550. nbytes -= count;
  551. count = min(nbytes, (unsigned) PAGE_SIZE);
  552. offset = 0;
  553. }
  554. return 0;
  555. }
  556. /* Copy a single argument in the request to/from userspace buffer */
  557. static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
  558. {
  559. while (size) {
  560. if (!cs->len) {
  561. int err = fuse_copy_fill(cs);
  562. if (err)
  563. return err;
  564. }
  565. fuse_copy_do(cs, &val, &size);
  566. }
  567. return 0;
  568. }
  569. /* Copy request arguments to/from userspace buffer */
  570. static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
  571. unsigned argpages, struct fuse_arg *args,
  572. int zeroing)
  573. {
  574. int err = 0;
  575. unsigned i;
  576. for (i = 0; !err && i < numargs; i++) {
  577. struct fuse_arg *arg = &args[i];
  578. if (i == numargs - 1 && argpages)
  579. err = fuse_copy_pages(cs, arg->size, zeroing);
  580. else
  581. err = fuse_copy_one(cs, arg->value, arg->size);
  582. }
  583. return err;
  584. }
  585. static int request_pending(struct fuse_conn *fc)
  586. {
  587. return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
  588. }
  589. /* Wait until a request is available on the pending list */
  590. static void request_wait(struct fuse_conn *fc)
  591. {
  592. DECLARE_WAITQUEUE(wait, current);
  593. add_wait_queue_exclusive(&fc->waitq, &wait);
  594. while (fc->connected && !request_pending(fc)) {
  595. set_current_state(TASK_INTERRUPTIBLE);
  596. if (signal_pending(current))
  597. break;
  598. spin_unlock(&fc->lock);
  599. schedule();
  600. spin_lock(&fc->lock);
  601. }
  602. set_current_state(TASK_RUNNING);
  603. remove_wait_queue(&fc->waitq, &wait);
  604. }
  605. /*
  606. * Transfer an interrupt request to userspace
  607. *
  608. * Unlike other requests this is assembled on demand, without a need
  609. * to allocate a separate fuse_req structure.
  610. *
  611. * Called with fc->lock held, releases it
  612. */
  613. static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
  614. const struct iovec *iov, unsigned long nr_segs)
  615. __releases(fc->lock)
  616. {
  617. struct fuse_copy_state cs;
  618. struct fuse_in_header ih;
  619. struct fuse_interrupt_in arg;
  620. unsigned reqsize = sizeof(ih) + sizeof(arg);
  621. int err;
  622. list_del_init(&req->intr_entry);
  623. req->intr_unique = fuse_get_unique(fc);
  624. memset(&ih, 0, sizeof(ih));
  625. memset(&arg, 0, sizeof(arg));
  626. ih.len = reqsize;
  627. ih.opcode = FUSE_INTERRUPT;
  628. ih.unique = req->intr_unique;
  629. arg.unique = req->in.h.unique;
  630. spin_unlock(&fc->lock);
  631. if (iov_length(iov, nr_segs) < reqsize)
  632. return -EINVAL;
  633. fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
  634. err = fuse_copy_one(&cs, &ih, sizeof(ih));
  635. if (!err)
  636. err = fuse_copy_one(&cs, &arg, sizeof(arg));
  637. fuse_copy_finish(&cs);
  638. return err ? err : reqsize;
  639. }
  640. /*
  641. * Read a single request into the userspace filesystem's buffer. This
  642. * function waits until a request is available, then removes it from
  643. * the pending list and copies request data to userspace buffer. If
  644. * no reply is needed (FORGET) or request has been aborted or there
  645. * was an error during the copying then it's finished by calling
  646. * request_end(). Otherwise add it to the processing list, and set
  647. * the 'sent' flag.
  648. */
  649. static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
  650. unsigned long nr_segs, loff_t pos)
  651. {
  652. int err;
  653. struct fuse_req *req;
  654. struct fuse_in *in;
  655. struct fuse_copy_state cs;
  656. unsigned reqsize;
  657. struct file *file = iocb->ki_filp;
  658. struct fuse_conn *fc = fuse_get_conn(file);
  659. if (!fc)
  660. return -EPERM;
  661. restart:
  662. spin_lock(&fc->lock);
  663. err = -EAGAIN;
  664. if ((file->f_flags & O_NONBLOCK) && fc->connected &&
  665. !request_pending(fc))
  666. goto err_unlock;
  667. request_wait(fc);
  668. err = -ENODEV;
  669. if (!fc->connected)
  670. goto err_unlock;
  671. err = -ERESTARTSYS;
  672. if (!request_pending(fc))
  673. goto err_unlock;
  674. if (!list_empty(&fc->interrupts)) {
  675. req = list_entry(fc->interrupts.next, struct fuse_req,
  676. intr_entry);
  677. return fuse_read_interrupt(fc, req, iov, nr_segs);
  678. }
  679. req = list_entry(fc->pending.next, struct fuse_req, list);
  680. req->state = FUSE_REQ_READING;
  681. list_move(&req->list, &fc->io);
  682. in = &req->in;
  683. reqsize = in->h.len;
  684. /* If request is too large, reply with an error and restart the read */
  685. if (iov_length(iov, nr_segs) < reqsize) {
  686. req->out.h.error = -EIO;
  687. /* SETXATTR is special, since it may contain too large data */
  688. if (in->h.opcode == FUSE_SETXATTR)
  689. req->out.h.error = -E2BIG;
  690. request_end(fc, req);
  691. goto restart;
  692. }
  693. spin_unlock(&fc->lock);
  694. fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
  695. err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
  696. if (!err)
  697. err = fuse_copy_args(&cs, in->numargs, in->argpages,
  698. (struct fuse_arg *) in->args, 0);
  699. fuse_copy_finish(&cs);
  700. spin_lock(&fc->lock);
  701. req->locked = 0;
  702. if (req->aborted) {
  703. request_end(fc, req);
  704. return -ENODEV;
  705. }
  706. if (err) {
  707. req->out.h.error = -EIO;
  708. request_end(fc, req);
  709. return err;
  710. }
  711. if (!req->isreply)
  712. request_end(fc, req);
  713. else {
  714. req->state = FUSE_REQ_SENT;
  715. list_move_tail(&req->list, &fc->processing);
  716. if (req->interrupted)
  717. queue_interrupt(fc, req);
  718. spin_unlock(&fc->lock);
  719. }
  720. return reqsize;
  721. err_unlock:
  722. spin_unlock(&fc->lock);
  723. return err;
  724. }
  725. /* Look up request on processing list by unique ID */
  726. static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
  727. {
  728. struct list_head *entry;
  729. list_for_each(entry, &fc->processing) {
  730. struct fuse_req *req;
  731. req = list_entry(entry, struct fuse_req, list);
  732. if (req->in.h.unique == unique || req->intr_unique == unique)
  733. return req;
  734. }
  735. return NULL;
  736. }
  737. static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
  738. unsigned nbytes)
  739. {
  740. unsigned reqsize = sizeof(struct fuse_out_header);
  741. if (out->h.error)
  742. return nbytes != reqsize ? -EINVAL : 0;
  743. reqsize += len_args(out->numargs, out->args);
  744. if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
  745. return -EINVAL;
  746. else if (reqsize > nbytes) {
  747. struct fuse_arg *lastarg = &out->args[out->numargs-1];
  748. unsigned diffsize = reqsize - nbytes;
  749. if (diffsize > lastarg->size)
  750. return -EINVAL;
  751. lastarg->size -= diffsize;
  752. }
  753. return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
  754. out->page_zeroing);
  755. }
  756. /*
  757. * Write a single reply to a request. First the header is copied from
  758. * the write buffer. The request is then searched on the processing
  759. * list by the unique ID found in the header. If found, then remove
  760. * it from the list and copy the rest of the buffer to the request.
  761. * The request is finished by calling request_end()
  762. */
  763. static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
  764. unsigned long nr_segs, loff_t pos)
  765. {
  766. int err;
  767. unsigned nbytes = iov_length(iov, nr_segs);
  768. struct fuse_req *req;
  769. struct fuse_out_header oh;
  770. struct fuse_copy_state cs;
  771. struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
  772. if (!fc)
  773. return -EPERM;
  774. fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
  775. if (nbytes < sizeof(struct fuse_out_header))
  776. return -EINVAL;
  777. err = fuse_copy_one(&cs, &oh, sizeof(oh));
  778. if (err)
  779. goto err_finish;
  780. err = -EINVAL;
  781. if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
  782. oh.len != nbytes)
  783. goto err_finish;
  784. spin_lock(&fc->lock);
  785. err = -ENOENT;
  786. if (!fc->connected)
  787. goto err_unlock;
  788. req = request_find(fc, oh.unique);
  789. if (!req)
  790. goto err_unlock;
  791. if (req->aborted) {
  792. spin_unlock(&fc->lock);
  793. fuse_copy_finish(&cs);
  794. spin_lock(&fc->lock);
  795. request_end(fc, req);
  796. return -ENOENT;
  797. }
  798. /* Is it an interrupt reply? */
  799. if (req->intr_unique == oh.unique) {
  800. err = -EINVAL;
  801. if (nbytes != sizeof(struct fuse_out_header))
  802. goto err_unlock;
  803. if (oh.error == -ENOSYS)
  804. fc->no_interrupt = 1;
  805. else if (oh.error == -EAGAIN)
  806. queue_interrupt(fc, req);
  807. spin_unlock(&fc->lock);
  808. fuse_copy_finish(&cs);
  809. return nbytes;
  810. }
  811. req->state = FUSE_REQ_WRITING;
  812. list_move(&req->list, &fc->io);
  813. req->out.h = oh;
  814. req->locked = 1;
  815. cs.req = req;
  816. spin_unlock(&fc->lock);
  817. err = copy_out_args(&cs, &req->out, nbytes);
  818. fuse_copy_finish(&cs);
  819. spin_lock(&fc->lock);
  820. req->locked = 0;
  821. if (!err) {
  822. if (req->aborted)
  823. err = -ENOENT;
  824. } else if (!req->aborted)
  825. req->out.h.error = -EIO;
  826. request_end(fc, req);
  827. return err ? err : nbytes;
  828. err_unlock:
  829. spin_unlock(&fc->lock);
  830. err_finish:
  831. fuse_copy_finish(&cs);
  832. return err;
  833. }
  834. static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
  835. {
  836. unsigned mask = POLLOUT | POLLWRNORM;
  837. struct fuse_conn *fc = fuse_get_conn(file);
  838. if (!fc)
  839. return POLLERR;
  840. poll_wait(file, &fc->waitq, wait);
  841. spin_lock(&fc->lock);
  842. if (!fc->connected)
  843. mask = POLLERR;
  844. else if (request_pending(fc))
  845. mask |= POLLIN | POLLRDNORM;
  846. spin_unlock(&fc->lock);
  847. return mask;
  848. }
  849. /*
  850. * Abort all requests on the given list (pending or processing)
  851. *
  852. * This function releases and reacquires fc->lock
  853. */
  854. static void end_requests(struct fuse_conn *fc, struct list_head *head)
  855. {
  856. while (!list_empty(head)) {
  857. struct fuse_req *req;
  858. req = list_entry(head->next, struct fuse_req, list);
  859. req->out.h.error = -ECONNABORTED;
  860. request_end(fc, req);
  861. spin_lock(&fc->lock);
  862. }
  863. }
  864. /*
  865. * Abort requests under I/O
  866. *
  867. * The requests are set to aborted and finished, and the request
  868. * waiter is woken up. This will make request_wait_answer() wait
  869. * until the request is unlocked and then return.
  870. *
  871. * If the request is asynchronous, then the end function needs to be
  872. * called after waiting for the request to be unlocked (if it was
  873. * locked).
  874. */
  875. static void end_io_requests(struct fuse_conn *fc)
  876. __releases(fc->lock) __acquires(fc->lock)
  877. {
  878. while (!list_empty(&fc->io)) {
  879. struct fuse_req *req =
  880. list_entry(fc->io.next, struct fuse_req, list);
  881. void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
  882. req->aborted = 1;
  883. req->out.h.error = -ECONNABORTED;
  884. req->state = FUSE_REQ_FINISHED;
  885. list_del_init(&req->list);
  886. wake_up(&req->waitq);
  887. if (end) {
  888. req->end = NULL;
  889. __fuse_get_request(req);
  890. spin_unlock(&fc->lock);
  891. wait_event(req->waitq, !req->locked);
  892. end(fc, req);
  893. fuse_put_request(fc, req);
  894. spin_lock(&fc->lock);
  895. }
  896. }
  897. }
  898. /*
  899. * Abort all requests.
  900. *
  901. * Emergency exit in case of a malicious or accidental deadlock, or
  902. * just a hung filesystem.
  903. *
  904. * The same effect is usually achievable through killing the
  905. * filesystem daemon and all users of the filesystem. The exception
  906. * is the combination of an asynchronous request and the tricky
  907. * deadlock (see Documentation/filesystems/fuse.txt).
  908. *
  909. * During the aborting, progression of requests from the pending and
  910. * processing lists onto the io list, and progression of new requests
  911. * onto the pending list is prevented by req->connected being false.
  912. *
  913. * Progression of requests under I/O to the processing list is
  914. * prevented by the req->aborted flag being true for these requests.
  915. * For this reason requests on the io list must be aborted first.
  916. */
  917. void fuse_abort_conn(struct fuse_conn *fc)
  918. {
  919. spin_lock(&fc->lock);
  920. if (fc->connected) {
  921. fc->connected = 0;
  922. fc->blocked = 0;
  923. end_io_requests(fc);
  924. end_requests(fc, &fc->pending);
  925. end_requests(fc, &fc->processing);
  926. wake_up_all(&fc->waitq);
  927. wake_up_all(&fc->blocked_waitq);
  928. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  929. }
  930. spin_unlock(&fc->lock);
  931. }
  932. static int fuse_dev_release(struct inode *inode, struct file *file)
  933. {
  934. struct fuse_conn *fc = fuse_get_conn(file);
  935. if (fc) {
  936. spin_lock(&fc->lock);
  937. fc->connected = 0;
  938. end_requests(fc, &fc->pending);
  939. end_requests(fc, &fc->processing);
  940. spin_unlock(&fc->lock);
  941. fuse_conn_put(fc);
  942. }
  943. return 0;
  944. }
  945. static int fuse_dev_fasync(int fd, struct file *file, int on)
  946. {
  947. struct fuse_conn *fc = fuse_get_conn(file);
  948. if (!fc)
  949. return -EPERM;
  950. /* No locking - fasync_helper does its own locking */
  951. return fasync_helper(fd, file, on, &fc->fasync);
  952. }
  953. const struct file_operations fuse_dev_operations = {
  954. .owner = THIS_MODULE,
  955. .llseek = no_llseek,
  956. .read = do_sync_read,
  957. .aio_read = fuse_dev_read,
  958. .write = do_sync_write,
  959. .aio_write = fuse_dev_write,
  960. .poll = fuse_dev_poll,
  961. .release = fuse_dev_release,
  962. .fasync = fuse_dev_fasync,
  963. };
  964. static struct miscdevice fuse_miscdevice = {
  965. .minor = FUSE_MINOR,
  966. .name = "fuse",
  967. .fops = &fuse_dev_operations,
  968. };
  969. int __init fuse_dev_init(void)
  970. {
  971. int err = -ENOMEM;
  972. fuse_req_cachep = kmem_cache_create("fuse_request",
  973. sizeof(struct fuse_req),
  974. 0, 0, NULL);
  975. if (!fuse_req_cachep)
  976. goto out;
  977. err = misc_register(&fuse_miscdevice);
  978. if (err)
  979. goto out_cache_clean;
  980. return 0;
  981. out_cache_clean:
  982. kmem_cache_destroy(fuse_req_cachep);
  983. out:
  984. return err;
  985. }
  986. void fuse_dev_cleanup(void)
  987. {
  988. misc_deregister(&fuse_miscdevice);
  989. kmem_cache_destroy(fuse_req_cachep);
  990. }