dev.c 25 KB

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