dev.c 25 KB

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