dev.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2006 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. else
  256. fuse_put_request(fc, req);
  257. }
  258. static void wait_answer_interruptible(struct fuse_conn *fc,
  259. struct fuse_req *req)
  260. __releases(fc->lock) __acquires(fc->lock)
  261. {
  262. if (signal_pending(current))
  263. return;
  264. spin_unlock(&fc->lock);
  265. wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
  266. spin_lock(&fc->lock);
  267. }
  268. static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
  269. {
  270. list_add_tail(&req->intr_entry, &fc->interrupts);
  271. wake_up(&fc->waitq);
  272. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  273. }
  274. static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
  275. __releases(fc->lock) __acquires(fc->lock)
  276. {
  277. if (!fc->no_interrupt) {
  278. /* Any signal may interrupt this */
  279. wait_answer_interruptible(fc, req);
  280. if (req->aborted)
  281. goto aborted;
  282. if (req->state == FUSE_REQ_FINISHED)
  283. return;
  284. req->interrupted = 1;
  285. if (req->state == FUSE_REQ_SENT)
  286. queue_interrupt(fc, req);
  287. }
  288. if (!req->force) {
  289. sigset_t oldset;
  290. /* Only fatal signals may interrupt this */
  291. block_sigs(&oldset);
  292. wait_answer_interruptible(fc, req);
  293. restore_sigs(&oldset);
  294. if (req->aborted)
  295. goto aborted;
  296. if (req->state == FUSE_REQ_FINISHED)
  297. return;
  298. /* Request is not yet in userspace, bail out */
  299. if (req->state == FUSE_REQ_PENDING) {
  300. list_del(&req->list);
  301. __fuse_put_request(req);
  302. req->out.h.error = -EINTR;
  303. return;
  304. }
  305. }
  306. /*
  307. * Either request is already in userspace, or it was forced.
  308. * Wait it out.
  309. */
  310. spin_unlock(&fc->lock);
  311. wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
  312. spin_lock(&fc->lock);
  313. if (!req->aborted)
  314. return;
  315. aborted:
  316. BUG_ON(req->state != FUSE_REQ_FINISHED);
  317. if (req->locked) {
  318. /* This is uninterruptible sleep, because data is
  319. being copied to/from the buffers of req. During
  320. locked state, there mustn't be any filesystem
  321. operation (e.g. page fault), since that could lead
  322. to deadlock */
  323. spin_unlock(&fc->lock);
  324. wait_event(req->waitq, !req->locked);
  325. spin_lock(&fc->lock);
  326. }
  327. }
  328. void request_send(struct fuse_conn *fc, struct fuse_req *req)
  329. {
  330. req->isreply = 1;
  331. spin_lock(&fc->lock);
  332. if (!fc->connected)
  333. req->out.h.error = -ENOTCONN;
  334. else if (fc->conn_error)
  335. req->out.h.error = -ECONNREFUSED;
  336. else {
  337. queue_request(fc, req);
  338. /* acquire extra reference, since request is still needed
  339. after request_end() */
  340. __fuse_get_request(req);
  341. request_wait_answer(fc, req);
  342. }
  343. spin_unlock(&fc->lock);
  344. }
  345. static void request_send_nowait_locked(struct fuse_conn *fc,
  346. struct fuse_req *req)
  347. {
  348. req->background = 1;
  349. fc->num_background++;
  350. if (fc->num_background == FUSE_MAX_BACKGROUND)
  351. fc->blocked = 1;
  352. if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
  353. set_bdi_congested(&fc->bdi, READ);
  354. set_bdi_congested(&fc->bdi, WRITE);
  355. }
  356. list_add_tail(&req->list, &fc->bg_queue);
  357. flush_bg_queue(fc);
  358. }
  359. static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
  360. {
  361. spin_lock(&fc->lock);
  362. if (fc->connected) {
  363. request_send_nowait_locked(fc, req);
  364. spin_unlock(&fc->lock);
  365. } else {
  366. req->out.h.error = -ENOTCONN;
  367. request_end(fc, req);
  368. }
  369. }
  370. void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
  371. {
  372. req->isreply = 0;
  373. request_send_nowait(fc, req);
  374. }
  375. void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
  376. {
  377. req->isreply = 1;
  378. request_send_nowait(fc, req);
  379. }
  380. /*
  381. * Called under fc->lock
  382. *
  383. * fc->connected must have been checked previously
  384. */
  385. void request_send_background_locked(struct fuse_conn *fc, struct fuse_req *req)
  386. {
  387. req->isreply = 1;
  388. request_send_nowait_locked(fc, req);
  389. }
  390. /*
  391. * Lock the request. Up to the next unlock_request() there mustn't be
  392. * anything that could cause a page-fault. If the request was already
  393. * aborted bail out.
  394. */
  395. static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
  396. {
  397. int err = 0;
  398. if (req) {
  399. spin_lock(&fc->lock);
  400. if (req->aborted)
  401. err = -ENOENT;
  402. else
  403. req->locked = 1;
  404. spin_unlock(&fc->lock);
  405. }
  406. return err;
  407. }
  408. /*
  409. * Unlock request. If it was aborted during being locked, the
  410. * requester thread is currently waiting for it to be unlocked, so
  411. * wake it up.
  412. */
  413. static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
  414. {
  415. if (req) {
  416. spin_lock(&fc->lock);
  417. req->locked = 0;
  418. if (req->aborted)
  419. wake_up(&req->waitq);
  420. spin_unlock(&fc->lock);
  421. }
  422. }
  423. struct fuse_copy_state {
  424. struct fuse_conn *fc;
  425. int write;
  426. struct fuse_req *req;
  427. const struct iovec *iov;
  428. unsigned long nr_segs;
  429. unsigned long seglen;
  430. unsigned long addr;
  431. struct page *pg;
  432. void *mapaddr;
  433. void *buf;
  434. unsigned len;
  435. };
  436. static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
  437. int write, struct fuse_req *req,
  438. const struct iovec *iov, unsigned long nr_segs)
  439. {
  440. memset(cs, 0, sizeof(*cs));
  441. cs->fc = fc;
  442. cs->write = write;
  443. cs->req = req;
  444. cs->iov = iov;
  445. cs->nr_segs = nr_segs;
  446. }
  447. /* Unmap and put previous page of userspace buffer */
  448. static void fuse_copy_finish(struct fuse_copy_state *cs)
  449. {
  450. if (cs->mapaddr) {
  451. kunmap_atomic(cs->mapaddr, KM_USER0);
  452. if (cs->write) {
  453. flush_dcache_page(cs->pg);
  454. set_page_dirty_lock(cs->pg);
  455. }
  456. put_page(cs->pg);
  457. cs->mapaddr = NULL;
  458. }
  459. }
  460. /*
  461. * Get another pagefull of userspace buffer, and map it to kernel
  462. * address space, and lock request
  463. */
  464. static int fuse_copy_fill(struct fuse_copy_state *cs)
  465. {
  466. unsigned long offset;
  467. int err;
  468. unlock_request(cs->fc, cs->req);
  469. fuse_copy_finish(cs);
  470. if (!cs->seglen) {
  471. BUG_ON(!cs->nr_segs);
  472. cs->seglen = cs->iov[0].iov_len;
  473. cs->addr = (unsigned long) cs->iov[0].iov_base;
  474. cs->iov ++;
  475. cs->nr_segs --;
  476. }
  477. down_read(&current->mm->mmap_sem);
  478. err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
  479. &cs->pg, NULL);
  480. up_read(&current->mm->mmap_sem);
  481. if (err < 0)
  482. return err;
  483. BUG_ON(err != 1);
  484. offset = cs->addr % PAGE_SIZE;
  485. cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
  486. cs->buf = cs->mapaddr + offset;
  487. cs->len = min(PAGE_SIZE - offset, cs->seglen);
  488. cs->seglen -= cs->len;
  489. cs->addr += cs->len;
  490. return lock_request(cs->fc, cs->req);
  491. }
  492. /* Do as much copy to/from userspace buffer as we can */
  493. static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
  494. {
  495. unsigned ncpy = min(*size, cs->len);
  496. if (val) {
  497. if (cs->write)
  498. memcpy(cs->buf, *val, ncpy);
  499. else
  500. memcpy(*val, cs->buf, ncpy);
  501. *val += ncpy;
  502. }
  503. *size -= ncpy;
  504. cs->len -= ncpy;
  505. cs->buf += ncpy;
  506. return ncpy;
  507. }
  508. /*
  509. * Copy a page in the request to/from the userspace buffer. Must be
  510. * done atomically
  511. */
  512. static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
  513. unsigned offset, unsigned count, int zeroing)
  514. {
  515. if (page && zeroing && count < PAGE_SIZE) {
  516. void *mapaddr = kmap_atomic(page, KM_USER1);
  517. memset(mapaddr, 0, PAGE_SIZE);
  518. kunmap_atomic(mapaddr, KM_USER1);
  519. }
  520. while (count) {
  521. int err;
  522. if (!cs->len && (err = fuse_copy_fill(cs)))
  523. return err;
  524. if (page) {
  525. void *mapaddr = kmap_atomic(page, KM_USER1);
  526. void *buf = mapaddr + offset;
  527. offset += fuse_copy_do(cs, &buf, &count);
  528. kunmap_atomic(mapaddr, KM_USER1);
  529. } else
  530. offset += fuse_copy_do(cs, NULL, &count);
  531. }
  532. if (page && !cs->write)
  533. flush_dcache_page(page);
  534. return 0;
  535. }
  536. /* Copy pages in the request to/from userspace buffer */
  537. static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
  538. int zeroing)
  539. {
  540. unsigned i;
  541. struct fuse_req *req = cs->req;
  542. unsigned offset = req->page_offset;
  543. unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
  544. for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
  545. struct page *page = req->pages[i];
  546. int err = fuse_copy_page(cs, page, offset, count, zeroing);
  547. if (err)
  548. return err;
  549. nbytes -= count;
  550. count = min(nbytes, (unsigned) PAGE_SIZE);
  551. offset = 0;
  552. }
  553. return 0;
  554. }
  555. /* Copy a single argument in the request to/from userspace buffer */
  556. static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
  557. {
  558. while (size) {
  559. int err;
  560. if (!cs->len && (err = fuse_copy_fill(cs)))
  561. return err;
  562. fuse_copy_do(cs, &val, &size);
  563. }
  564. return 0;
  565. }
  566. /* Copy request arguments to/from userspace buffer */
  567. static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
  568. unsigned argpages, struct fuse_arg *args,
  569. int zeroing)
  570. {
  571. int err = 0;
  572. unsigned i;
  573. for (i = 0; !err && i < numargs; i++) {
  574. struct fuse_arg *arg = &args[i];
  575. if (i == numargs - 1 && argpages)
  576. err = fuse_copy_pages(cs, arg->size, zeroing);
  577. else
  578. err = fuse_copy_one(cs, arg->value, arg->size);
  579. }
  580. return err;
  581. }
  582. static int request_pending(struct fuse_conn *fc)
  583. {
  584. return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
  585. }
  586. /* Wait until a request is available on the pending list */
  587. static void request_wait(struct fuse_conn *fc)
  588. {
  589. DECLARE_WAITQUEUE(wait, current);
  590. add_wait_queue_exclusive(&fc->waitq, &wait);
  591. while (fc->connected && !request_pending(fc)) {
  592. set_current_state(TASK_INTERRUPTIBLE);
  593. if (signal_pending(current))
  594. break;
  595. spin_unlock(&fc->lock);
  596. schedule();
  597. spin_lock(&fc->lock);
  598. }
  599. set_current_state(TASK_RUNNING);
  600. remove_wait_queue(&fc->waitq, &wait);
  601. }
  602. /*
  603. * Transfer an interrupt request to userspace
  604. *
  605. * Unlike other requests this is assembled on demand, without a need
  606. * to allocate a separate fuse_req structure.
  607. *
  608. * Called with fc->lock held, releases it
  609. */
  610. static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
  611. const struct iovec *iov, unsigned long nr_segs)
  612. __releases(fc->lock)
  613. {
  614. struct fuse_copy_state cs;
  615. struct fuse_in_header ih;
  616. struct fuse_interrupt_in arg;
  617. unsigned reqsize = sizeof(ih) + sizeof(arg);
  618. int err;
  619. list_del_init(&req->intr_entry);
  620. req->intr_unique = fuse_get_unique(fc);
  621. memset(&ih, 0, sizeof(ih));
  622. memset(&arg, 0, sizeof(arg));
  623. ih.len = reqsize;
  624. ih.opcode = FUSE_INTERRUPT;
  625. ih.unique = req->intr_unique;
  626. arg.unique = req->in.h.unique;
  627. spin_unlock(&fc->lock);
  628. if (iov_length(iov, nr_segs) < reqsize)
  629. return -EINVAL;
  630. fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
  631. err = fuse_copy_one(&cs, &ih, sizeof(ih));
  632. if (!err)
  633. err = fuse_copy_one(&cs, &arg, sizeof(arg));
  634. fuse_copy_finish(&cs);
  635. return err ? err : reqsize;
  636. }
  637. /*
  638. * Read a single request into the userspace filesystem's buffer. This
  639. * function waits until a request is available, then removes it from
  640. * the pending list and copies request data to userspace buffer. If
  641. * no reply is needed (FORGET) or request has been aborted or there
  642. * was an error during the copying then it's finished by calling
  643. * request_end(). Otherwise add it to the processing list, and set
  644. * the 'sent' flag.
  645. */
  646. static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
  647. unsigned long nr_segs, loff_t pos)
  648. {
  649. int err;
  650. struct fuse_req *req;
  651. struct fuse_in *in;
  652. struct fuse_copy_state cs;
  653. unsigned reqsize;
  654. struct file *file = iocb->ki_filp;
  655. struct fuse_conn *fc = fuse_get_conn(file);
  656. if (!fc)
  657. return -EPERM;
  658. restart:
  659. spin_lock(&fc->lock);
  660. err = -EAGAIN;
  661. if ((file->f_flags & O_NONBLOCK) && fc->connected &&
  662. !request_pending(fc))
  663. goto err_unlock;
  664. request_wait(fc);
  665. err = -ENODEV;
  666. if (!fc->connected)
  667. goto err_unlock;
  668. err = -ERESTARTSYS;
  669. if (!request_pending(fc))
  670. goto err_unlock;
  671. if (!list_empty(&fc->interrupts)) {
  672. req = list_entry(fc->interrupts.next, struct fuse_req,
  673. intr_entry);
  674. return fuse_read_interrupt(fc, req, iov, nr_segs);
  675. }
  676. req = list_entry(fc->pending.next, struct fuse_req, list);
  677. req->state = FUSE_REQ_READING;
  678. list_move(&req->list, &fc->io);
  679. in = &req->in;
  680. reqsize = in->h.len;
  681. /* If request is too large, reply with an error and restart the read */
  682. if (iov_length(iov, nr_segs) < reqsize) {
  683. req->out.h.error = -EIO;
  684. /* SETXATTR is special, since it may contain too large data */
  685. if (in->h.opcode == FUSE_SETXATTR)
  686. req->out.h.error = -E2BIG;
  687. request_end(fc, req);
  688. goto restart;
  689. }
  690. spin_unlock(&fc->lock);
  691. fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
  692. err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
  693. if (!err)
  694. err = fuse_copy_args(&cs, in->numargs, in->argpages,
  695. (struct fuse_arg *) in->args, 0);
  696. fuse_copy_finish(&cs);
  697. spin_lock(&fc->lock);
  698. req->locked = 0;
  699. if (req->aborted) {
  700. request_end(fc, req);
  701. return -ENODEV;
  702. }
  703. if (err) {
  704. req->out.h.error = -EIO;
  705. request_end(fc, req);
  706. return err;
  707. }
  708. if (!req->isreply)
  709. request_end(fc, req);
  710. else {
  711. req->state = FUSE_REQ_SENT;
  712. list_move_tail(&req->list, &fc->processing);
  713. if (req->interrupted)
  714. queue_interrupt(fc, req);
  715. spin_unlock(&fc->lock);
  716. }
  717. return reqsize;
  718. err_unlock:
  719. spin_unlock(&fc->lock);
  720. return err;
  721. }
  722. /* Look up request on processing list by unique ID */
  723. static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
  724. {
  725. struct list_head *entry;
  726. list_for_each(entry, &fc->processing) {
  727. struct fuse_req *req;
  728. req = list_entry(entry, struct fuse_req, list);
  729. if (req->in.h.unique == unique || req->intr_unique == unique)
  730. return req;
  731. }
  732. return NULL;
  733. }
  734. static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
  735. unsigned nbytes)
  736. {
  737. unsigned reqsize = sizeof(struct fuse_out_header);
  738. if (out->h.error)
  739. return nbytes != reqsize ? -EINVAL : 0;
  740. reqsize += len_args(out->numargs, out->args);
  741. if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
  742. return -EINVAL;
  743. else if (reqsize > nbytes) {
  744. struct fuse_arg *lastarg = &out->args[out->numargs-1];
  745. unsigned diffsize = reqsize - nbytes;
  746. if (diffsize > lastarg->size)
  747. return -EINVAL;
  748. lastarg->size -= diffsize;
  749. }
  750. return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
  751. out->page_zeroing);
  752. }
  753. /*
  754. * Write a single reply to a request. First the header is copied from
  755. * the write buffer. The request is then searched on the processing
  756. * list by the unique ID found in the header. If found, then remove
  757. * it from the list and copy the rest of the buffer to the request.
  758. * The request is finished by calling request_end()
  759. */
  760. static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
  761. unsigned long nr_segs, loff_t pos)
  762. {
  763. int err;
  764. unsigned nbytes = iov_length(iov, nr_segs);
  765. struct fuse_req *req;
  766. struct fuse_out_header oh;
  767. struct fuse_copy_state cs;
  768. struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
  769. if (!fc)
  770. return -EPERM;
  771. fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
  772. if (nbytes < sizeof(struct fuse_out_header))
  773. return -EINVAL;
  774. err = fuse_copy_one(&cs, &oh, sizeof(oh));
  775. if (err)
  776. goto err_finish;
  777. err = -EINVAL;
  778. if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
  779. oh.len != nbytes)
  780. goto err_finish;
  781. spin_lock(&fc->lock);
  782. err = -ENOENT;
  783. if (!fc->connected)
  784. goto err_unlock;
  785. req = request_find(fc, oh.unique);
  786. if (!req)
  787. goto err_unlock;
  788. if (req->aborted) {
  789. spin_unlock(&fc->lock);
  790. fuse_copy_finish(&cs);
  791. spin_lock(&fc->lock);
  792. request_end(fc, req);
  793. return -ENOENT;
  794. }
  795. /* Is it an interrupt reply? */
  796. if (req->intr_unique == oh.unique) {
  797. err = -EINVAL;
  798. if (nbytes != sizeof(struct fuse_out_header))
  799. goto err_unlock;
  800. if (oh.error == -ENOSYS)
  801. fc->no_interrupt = 1;
  802. else if (oh.error == -EAGAIN)
  803. queue_interrupt(fc, req);
  804. spin_unlock(&fc->lock);
  805. fuse_copy_finish(&cs);
  806. return nbytes;
  807. }
  808. req->state = FUSE_REQ_WRITING;
  809. list_move(&req->list, &fc->io);
  810. req->out.h = oh;
  811. req->locked = 1;
  812. cs.req = req;
  813. spin_unlock(&fc->lock);
  814. err = copy_out_args(&cs, &req->out, nbytes);
  815. fuse_copy_finish(&cs);
  816. spin_lock(&fc->lock);
  817. req->locked = 0;
  818. if (!err) {
  819. if (req->aborted)
  820. err = -ENOENT;
  821. } else if (!req->aborted)
  822. req->out.h.error = -EIO;
  823. request_end(fc, req);
  824. return err ? err : nbytes;
  825. err_unlock:
  826. spin_unlock(&fc->lock);
  827. err_finish:
  828. fuse_copy_finish(&cs);
  829. return err;
  830. }
  831. static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
  832. {
  833. unsigned mask = POLLOUT | POLLWRNORM;
  834. struct fuse_conn *fc = fuse_get_conn(file);
  835. if (!fc)
  836. return POLLERR;
  837. poll_wait(file, &fc->waitq, wait);
  838. spin_lock(&fc->lock);
  839. if (!fc->connected)
  840. mask = POLLERR;
  841. else if (request_pending(fc))
  842. mask |= POLLIN | POLLRDNORM;
  843. spin_unlock(&fc->lock);
  844. return mask;
  845. }
  846. /*
  847. * Abort all requests on the given list (pending or processing)
  848. *
  849. * This function releases and reacquires fc->lock
  850. */
  851. static void end_requests(struct fuse_conn *fc, struct list_head *head)
  852. {
  853. while (!list_empty(head)) {
  854. struct fuse_req *req;
  855. req = list_entry(head->next, struct fuse_req, list);
  856. req->out.h.error = -ECONNABORTED;
  857. request_end(fc, req);
  858. spin_lock(&fc->lock);
  859. }
  860. }
  861. /*
  862. * Abort requests under I/O
  863. *
  864. * The requests are set to aborted and finished, and the request
  865. * waiter is woken up. This will make request_wait_answer() wait
  866. * until the request is unlocked and then return.
  867. *
  868. * If the request is asynchronous, then the end function needs to be
  869. * called after waiting for the request to be unlocked (if it was
  870. * locked).
  871. */
  872. static void end_io_requests(struct fuse_conn *fc)
  873. __releases(fc->lock) __acquires(fc->lock)
  874. {
  875. while (!list_empty(&fc->io)) {
  876. struct fuse_req *req =
  877. list_entry(fc->io.next, struct fuse_req, list);
  878. void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
  879. req->aborted = 1;
  880. req->out.h.error = -ECONNABORTED;
  881. req->state = FUSE_REQ_FINISHED;
  882. list_del_init(&req->list);
  883. wake_up(&req->waitq);
  884. if (end) {
  885. req->end = NULL;
  886. /* The end function will consume this reference */
  887. __fuse_get_request(req);
  888. spin_unlock(&fc->lock);
  889. wait_event(req->waitq, !req->locked);
  890. end(fc, req);
  891. spin_lock(&fc->lock);
  892. }
  893. }
  894. }
  895. /*
  896. * Abort all requests.
  897. *
  898. * Emergency exit in case of a malicious or accidental deadlock, or
  899. * just a hung filesystem.
  900. *
  901. * The same effect is usually achievable through killing the
  902. * filesystem daemon and all users of the filesystem. The exception
  903. * is the combination of an asynchronous request and the tricky
  904. * deadlock (see Documentation/filesystems/fuse.txt).
  905. *
  906. * During the aborting, progression of requests from the pending and
  907. * processing lists onto the io list, and progression of new requests
  908. * onto the pending list is prevented by req->connected being false.
  909. *
  910. * Progression of requests under I/O to the processing list is
  911. * prevented by the req->aborted flag being true for these requests.
  912. * For this reason requests on the io list must be aborted first.
  913. */
  914. void fuse_abort_conn(struct fuse_conn *fc)
  915. {
  916. spin_lock(&fc->lock);
  917. if (fc->connected) {
  918. fc->connected = 0;
  919. fc->blocked = 0;
  920. end_io_requests(fc);
  921. end_requests(fc, &fc->pending);
  922. end_requests(fc, &fc->processing);
  923. wake_up_all(&fc->waitq);
  924. wake_up_all(&fc->blocked_waitq);
  925. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  926. }
  927. spin_unlock(&fc->lock);
  928. }
  929. static int fuse_dev_release(struct inode *inode, struct file *file)
  930. {
  931. struct fuse_conn *fc = fuse_get_conn(file);
  932. if (fc) {
  933. spin_lock(&fc->lock);
  934. fc->connected = 0;
  935. end_requests(fc, &fc->pending);
  936. end_requests(fc, &fc->processing);
  937. spin_unlock(&fc->lock);
  938. fuse_conn_put(fc);
  939. }
  940. return 0;
  941. }
  942. static int fuse_dev_fasync(int fd, struct file *file, int on)
  943. {
  944. struct fuse_conn *fc = fuse_get_conn(file);
  945. if (!fc)
  946. return -EPERM;
  947. /* No locking - fasync_helper does its own locking */
  948. return fasync_helper(fd, file, on, &fc->fasync);
  949. }
  950. const struct file_operations fuse_dev_operations = {
  951. .owner = THIS_MODULE,
  952. .llseek = no_llseek,
  953. .read = do_sync_read,
  954. .aio_read = fuse_dev_read,
  955. .write = do_sync_write,
  956. .aio_write = fuse_dev_write,
  957. .poll = fuse_dev_poll,
  958. .release = fuse_dev_release,
  959. .fasync = fuse_dev_fasync,
  960. };
  961. static struct miscdevice fuse_miscdevice = {
  962. .minor = FUSE_MINOR,
  963. .name = "fuse",
  964. .fops = &fuse_dev_operations,
  965. };
  966. int __init fuse_dev_init(void)
  967. {
  968. int err = -ENOMEM;
  969. fuse_req_cachep = kmem_cache_create("fuse_request",
  970. sizeof(struct fuse_req),
  971. 0, 0, NULL);
  972. if (!fuse_req_cachep)
  973. goto out;
  974. err = misc_register(&fuse_miscdevice);
  975. if (err)
  976. goto out_cache_clean;
  977. return 0;
  978. out_cache_clean:
  979. kmem_cache_destroy(fuse_req_cachep);
  980. out:
  981. return err;
  982. }
  983. void fuse_dev_cleanup(void)
  984. {
  985. misc_deregister(&fuse_miscdevice);
  986. kmem_cache_destroy(fuse_req_cachep);
  987. }