Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1 | /* |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 149f607 | 2005-01-10 12:29:28 +0000 | [diff] [blame] | 3 | Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 4 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "fuse_i.h" |
| 10 | |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 11 | #include <linux/init.h> |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 12 | #include <linux/module.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 13 | #include <linux/poll.h> |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 14 | #include <linux/uio.h> |
Miklos Szeredi | 162bcbb | 2004-11-29 23:43:44 +0000 | [diff] [blame] | 15 | #include <linux/miscdevice.h> |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 16 | #include <linux/pagemap.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 17 | #include <linux/file.h> |
Miklos Szeredi | 2a92727 | 2005-01-07 11:14:15 +0000 | [diff] [blame] | 18 | #include <linux/slab.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 19 | |
Miklos Szeredi | ec12fad | 2005-06-08 10:26:34 +0000 | [diff] [blame] | 20 | #ifdef MODULE_ALIAS_MISCDEV |
| 21 | MODULE_ALIAS_MISCDEV(FUSE_MINOR); |
| 22 | #endif |
| 23 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 24 | static kmem_cache_t *fuse_req_cachep; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 25 | |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 26 | static inline struct fuse_conn *fuse_get_conn(struct file *file) |
| 27 | { |
| 28 | struct fuse_conn *fc; |
| 29 | spin_lock(&fuse_lock); |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 30 | fc = file->private_data; |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame] | 31 | if (fc && !fc->mounted) |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 32 | fc = NULL; |
| 33 | spin_unlock(&fuse_lock); |
| 34 | return fc; |
| 35 | } |
| 36 | |
Miklos Szeredi | 31d5119 | 2004-12-07 18:28:35 +0000 | [diff] [blame] | 37 | static inline void fuse_request_init(struct fuse_req *req) |
| 38 | { |
| 39 | memset(req, 0, sizeof(*req)); |
| 40 | INIT_LIST_HEAD(&req->list); |
| 41 | init_waitqueue_head(&req->waitq); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 42 | atomic_set(&req->count, 1); |
Miklos Szeredi | 31d5119 | 2004-12-07 18:28:35 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 45 | struct fuse_req *fuse_request_alloc(void) |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 46 | { |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 47 | struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, SLAB_KERNEL); |
Miklos Szeredi | 31d5119 | 2004-12-07 18:28:35 +0000 | [diff] [blame] | 48 | if (req) |
| 49 | fuse_request_init(req); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 50 | return req; |
| 51 | } |
| 52 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 53 | void fuse_request_free(struct fuse_req *req) |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 54 | { |
| 55 | kmem_cache_free(fuse_req_cachep, req); |
| 56 | } |
| 57 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 58 | #ifdef KERNEL_2_6 |
| 59 | static inline void block_sigs(sigset_t *oldset) |
| 60 | { |
Miklos Szeredi | 4fcbff6 | 2005-01-11 20:21:08 +0000 | [diff] [blame] | 61 | sigset_t mask; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 62 | |
Miklos Szeredi | 4fcbff6 | 2005-01-11 20:21:08 +0000 | [diff] [blame] | 63 | siginitsetinv(&mask, sigmask(SIGKILL)); |
| 64 | sigprocmask(SIG_BLOCK, &mask, oldset); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static inline void restore_sigs(sigset_t *oldset) |
| 68 | { |
| 69 | sigprocmask(SIG_SETMASK, oldset, NULL); |
| 70 | } |
| 71 | #else |
Miklos Szeredi | 5776285 | 2005-01-21 16:46:58 +0000 | [diff] [blame] | 72 | #ifdef HAVE_RECALC_SIGPENDING_TSK |
| 73 | static inline void block_sigs(sigset_t *oldset) |
| 74 | { |
| 75 | spin_lock_irq(¤t->sighand->siglock); |
| 76 | *oldset = current->blocked; |
| 77 | siginitsetinv(¤t->blocked, sigmask(SIGKILL) & ~oldset->sig[0]); |
| 78 | recalc_sigpending(); |
| 79 | spin_unlock_irq(¤t->sighand->siglock); |
| 80 | } |
| 81 | |
| 82 | static inline void restore_sigs(sigset_t *oldset) |
| 83 | { |
| 84 | spin_lock_irq(¤t->sighand->siglock); |
| 85 | current->blocked = *oldset; |
| 86 | recalc_sigpending(); |
| 87 | spin_unlock_irq(¤t->sighand->siglock); |
| 88 | } |
| 89 | #else |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 90 | static inline void block_sigs(sigset_t *oldset) |
| 91 | { |
| 92 | spin_lock_irq(¤t->sigmask_lock); |
| 93 | *oldset = current->blocked; |
| 94 | siginitsetinv(¤t->blocked, sigmask(SIGKILL) & ~oldset->sig[0]); |
| 95 | recalc_sigpending(current); |
| 96 | spin_unlock_irq(¤t->sigmask_lock); |
| 97 | } |
| 98 | |
| 99 | static inline void restore_sigs(sigset_t *oldset) |
| 100 | { |
| 101 | spin_lock_irq(¤t->sigmask_lock); |
| 102 | current->blocked = *oldset; |
| 103 | recalc_sigpending(current); |
| 104 | spin_unlock_irq(¤t->sigmask_lock); |
| 105 | } |
| 106 | #endif |
Miklos Szeredi | 5776285 | 2005-01-21 16:46:58 +0000 | [diff] [blame] | 107 | #endif |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 108 | |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 109 | void fuse_reset_request(struct fuse_req *req) |
| 110 | { |
| 111 | int preallocated = req->preallocated; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 112 | BUG_ON(atomic_read(&req->count) != 1); |
Miklos Szeredi | 31d5119 | 2004-12-07 18:28:35 +0000 | [diff] [blame] | 113 | fuse_request_init(req); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 114 | req->preallocated = preallocated; |
| 115 | } |
| 116 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 117 | static void __fuse_get_request(struct fuse_req *req) |
| 118 | { |
| 119 | atomic_inc(&req->count); |
| 120 | } |
| 121 | |
| 122 | /* Must be called with > 1 refcount */ |
| 123 | static void __fuse_put_request(struct fuse_req *req) |
| 124 | { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 125 | BUG_ON(atomic_read(&req->count) < 2); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 126 | atomic_dec(&req->count); |
| 127 | } |
| 128 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 129 | static struct fuse_req *do_get_request(struct fuse_conn *fc) |
| 130 | { |
| 131 | struct fuse_req *req; |
| 132 | |
| 133 | spin_lock(&fuse_lock); |
| 134 | BUG_ON(list_empty(&fc->unused_list)); |
| 135 | req = list_entry(fc->unused_list.next, struct fuse_req, list); |
| 136 | list_del_init(&req->list); |
| 137 | spin_unlock(&fuse_lock); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 138 | fuse_request_init(req); |
| 139 | req->preallocated = 1; |
Miklos Szeredi | a2c5e56 | 2004-10-19 22:01:21 +0000 | [diff] [blame] | 140 | req->in.h.uid = current->fsuid; |
| 141 | req->in.h.gid = current->fsgid; |
| 142 | req->in.h.pid = current->pid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 143 | return req; |
| 144 | } |
| 145 | |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 146 | /* This can return NULL, but only in case it's interrupted by a SIGKILL */ |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 147 | struct fuse_req *fuse_get_request(struct fuse_conn *fc) |
| 148 | { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 149 | int intr; |
| 150 | sigset_t oldset; |
| 151 | |
| 152 | block_sigs(&oldset); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 153 | intr = down_interruptible(&fc->outstanding_sem); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 154 | restore_sigs(&oldset); |
| 155 | return intr ? NULL : do_get_request(fc); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Miklos Szeredi | 7db35c0 | 2005-01-17 09:46:28 +0000 | [diff] [blame] | 158 | static void fuse_putback_request(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 159 | { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 160 | spin_lock(&fuse_lock); |
| 161 | if (req->preallocated) |
Miklos Szeredi | d3dd2d5 | 2004-06-22 18:46:02 +0000 | [diff] [blame] | 162 | list_add(&req->list, &fc->unused_list); |
Miklos Szeredi | 31fa41c | 2005-03-04 17:28:51 +0000 | [diff] [blame] | 163 | else |
| 164 | fuse_request_free(req); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 165 | |
Miklos Szeredi | fcf9f8d | 2005-09-08 14:28:54 +0000 | [diff] [blame] | 166 | /* If we are in debt decrease that first */ |
| 167 | if (fc->outstanding_debt) |
| 168 | fc->outstanding_debt--; |
| 169 | else |
| 170 | up(&fc->outstanding_sem); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 171 | spin_unlock(&fuse_lock); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 174 | void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req) |
| 175 | { |
| 176 | if (atomic_dec_and_test(&req->count)) |
| 177 | fuse_putback_request(fc, req); |
| 178 | } |
| 179 | |
Miklos Szeredi | d17da46 | 2005-03-21 11:47:04 +0000 | [diff] [blame] | 180 | void fuse_release_background(struct fuse_req *req) |
| 181 | { |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 182 | iput(req->inode); |
| 183 | iput(req->inode2); |
Miklos Szeredi | d17da46 | 2005-03-21 11:47:04 +0000 | [diff] [blame] | 184 | if (req->file) |
| 185 | fput(req->file); |
| 186 | spin_lock(&fuse_lock); |
| 187 | list_del(&req->bg_entry); |
| 188 | spin_unlock(&fuse_lock); |
| 189 | } |
| 190 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 191 | /* |
| 192 | * This function is called when a request is finished. Either a reply |
| 193 | * has arrived or it was interrupted (and not yet sent) or some error |
Miklos Szeredi | b75d4b9 | 2005-10-11 10:12:08 +0000 | [diff] [blame] | 194 | * occurred during communication with userspace, or the device file was |
| 195 | * closed. It decreases the reference count for the request. In case |
| 196 | * of a background request the reference to the stored objects are |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 197 | * released. The requester thread is woken up (if still waiting), and |
| 198 | * finally the request is either freed or put on the unused_list |
| 199 | * |
| 200 | * Called with fuse_lock, unlocks it |
| 201 | */ |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 202 | static void request_end(struct fuse_conn *fc, struct fuse_req *req) |
| 203 | { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 204 | int putback; |
| 205 | req->finished = 1; |
| 206 | putback = atomic_dec_and_test(&req->count); |
| 207 | spin_unlock(&fuse_lock); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 208 | if (req->background) { |
Miklos Szeredi | d17da46 | 2005-03-21 11:47:04 +0000 | [diff] [blame] | 209 | down_read(&fc->sbput_sem); |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame] | 210 | if (fc->mounted) |
Miklos Szeredi | d17da46 | 2005-03-21 11:47:04 +0000 | [diff] [blame] | 211 | fuse_release_background(req); |
| 212 | up_read(&fc->sbput_sem); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 213 | } |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 214 | wake_up(&req->waitq); |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 215 | if (req->in.h.opcode == FUSE_INIT) { |
| 216 | int i; |
Miklos Szeredi | eab72ef | 2005-03-31 19:59:12 +0000 | [diff] [blame] | 217 | |
| 218 | if (req->misc.init_in_out.major != FUSE_KERNEL_VERSION) |
| 219 | fc->conn_error = 1; |
| 220 | |
Miklos Szeredi | 2a807ec | 2005-01-10 11:50:10 +0000 | [diff] [blame] | 221 | /* After INIT reply is received other requests can go |
| 222 | out. So do (FUSE_MAX_OUTSTANDING - 1) number of |
| 223 | up()s on outstanding_sem. The last up() is done in |
| 224 | fuse_putback_request() */ |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 225 | for (i = 1; i < FUSE_MAX_OUTSTANDING; i++) |
| 226 | up(&fc->outstanding_sem); |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame^] | 227 | } else if (req->in.h.opcode == FUSE_RELEASE && req->inode == NULL) { |
| 228 | /* Special case for failed iget in CREATE */ |
| 229 | u64 nodeid = req->in.h.nodeid; |
| 230 | __fuse_get_request(req); |
| 231 | fuse_reset_request(req); |
| 232 | fuse_send_forget(fc, req, nodeid, 1); |
| 233 | putback = 0; |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 234 | } |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 235 | if (putback) |
| 236 | fuse_putback_request(fc, req); |
| 237 | } |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 238 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 239 | /* |
| 240 | * Unfortunately request interruption not just solves the deadlock |
| 241 | * problem, it causes problems too. These stem from the fact, that an |
| 242 | * interrupted request is continued to be processed in userspace, |
| 243 | * while all the locks and object references (inode and file) held |
| 244 | * during the operation are released. |
| 245 | * |
| 246 | * To release the locks is exactly why there's a need to interrupt the |
| 247 | * request, so there's not a lot that can be done about this, except |
| 248 | * introduce additional locking in userspace. |
| 249 | * |
| 250 | * More important is to keep inode and file references until userspace |
| 251 | * has replied, otherwise FORGET and RELEASE could be sent while the |
| 252 | * inode/file is still used by the filesystem. |
| 253 | * |
| 254 | * For this reason the concept of "background" request is introduced. |
| 255 | * An interrupted request is backgrounded if it has been already sent |
| 256 | * to userspace. Backgrounding involves getting an extra reference to |
| 257 | * inode(s) or file used in the request, and adding the request to |
| 258 | * fc->background list. When a reply is received for a background |
| 259 | * request, the object references are released, and the request is |
| 260 | * removed from the list. If the filesystem is unmounted while there |
| 261 | * are still background requests, the list is walked and references |
| 262 | * are released as if a reply was received. |
| 263 | * |
| 264 | * There's one more use for a background request. The RELEASE message is |
| 265 | * always sent as background, since it doesn't return an error or |
| 266 | * data. |
| 267 | */ |
Miklos Szeredi | d17da46 | 2005-03-21 11:47:04 +0000 | [diff] [blame] | 268 | static void background_request(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 269 | { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 270 | req->background = 1; |
Miklos Szeredi | d17da46 | 2005-03-21 11:47:04 +0000 | [diff] [blame] | 271 | list_add(&req->bg_entry, &fc->background); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 272 | if (req->inode) |
| 273 | req->inode = igrab(req->inode); |
| 274 | if (req->inode2) |
| 275 | req->inode2 = igrab(req->inode2); |
| 276 | if (req->file) |
| 277 | get_file(req->file); |
| 278 | } |
| 279 | |
| 280 | /* Called with fuse_lock held. Releases, and then reacquires it. */ |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 281 | static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 282 | { |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 283 | sigset_t oldset; |
Miklos Szeredi | a7acd7f | 2005-02-02 11:31:36 +0000 | [diff] [blame] | 284 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 285 | spin_unlock(&fuse_lock); |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 286 | block_sigs(&oldset); |
| 287 | wait_event_interruptible(req->waitq, req->finished); |
| 288 | restore_sigs(&oldset); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 289 | spin_lock(&fuse_lock); |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 290 | if (req->finished) |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 291 | return; |
| 292 | |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 293 | req->out.h.error = -EINTR; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 294 | req->interrupted = 1; |
| 295 | if (req->locked) { |
| 296 | /* This is uninterruptible sleep, because data is |
| 297 | being copied to/from the buffers of req. During |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 298 | locked state, there mustn't be any filesystem |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 299 | operation (e.g. page fault), since that could lead |
| 300 | to deadlock */ |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 301 | spin_unlock(&fuse_lock); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 302 | wait_event(req->waitq, !req->locked); |
| 303 | spin_lock(&fuse_lock); |
| 304 | } |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 305 | if (!req->sent && !list_empty(&req->list)) { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 306 | list_del(&req->list); |
| 307 | __fuse_put_request(req); |
Miklos Szeredi | ff30a6c | 2005-02-02 10:53:59 +0000 | [diff] [blame] | 308 | } else if (!req->finished && req->sent) |
Miklos Szeredi | d17da46 | 2005-03-21 11:47:04 +0000 | [diff] [blame] | 309 | background_request(fc, req); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static unsigned len_args(unsigned numargs, struct fuse_arg *args) |
| 313 | { |
| 314 | unsigned nbytes = 0; |
| 315 | unsigned i; |
| 316 | |
| 317 | for (i = 0; i < numargs; i++) |
| 318 | nbytes += args[i].size; |
| 319 | |
| 320 | return nbytes; |
| 321 | } |
| 322 | |
| 323 | static void queue_request(struct fuse_conn *fc, struct fuse_req *req) |
| 324 | { |
| 325 | fc->reqctr++; |
| 326 | /* zero is special */ |
| 327 | if (fc->reqctr == 0) |
| 328 | fc->reqctr = 1; |
| 329 | req->in.h.unique = fc->reqctr; |
Miklos Szeredi | cdae16e | 2005-01-14 11:43:22 +0000 | [diff] [blame] | 330 | req->in.h.len = sizeof(struct fuse_in_header) + |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 331 | len_args(req->in.numargs, (struct fuse_arg *) req->in.args); |
Miklos Szeredi | fcf9f8d | 2005-09-08 14:28:54 +0000 | [diff] [blame] | 332 | if (!req->preallocated) { |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 333 | /* If request is not preallocated (either FORGET or |
Miklos Szeredi | fcf9f8d | 2005-09-08 14:28:54 +0000 | [diff] [blame] | 334 | RELEASE), then still decrease outstanding_sem, so |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 335 | user can't open infinite number of files while not |
| 336 | processing the RELEASE requests. However for |
| 337 | efficiency do it without blocking, so if down() |
| 338 | would block, just increase the debt instead */ |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 339 | if (down_trylock(&fc->outstanding_sem)) |
| 340 | fc->outstanding_debt++; |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 341 | } |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 342 | list_add_tail(&req->list, &fc->pending); |
| 343 | wake_up(&fc->waitq); |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 346 | /* |
| 347 | * This can only be interrupted by a SIGKILL |
| 348 | */ |
| 349 | void request_send(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 350 | { |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 351 | req->isreply = 1; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 352 | spin_lock(&fuse_lock); |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame] | 353 | if (!fc->connected) |
Miklos Szeredi | eab72ef | 2005-03-31 19:59:12 +0000 | [diff] [blame] | 354 | req->out.h.error = -ENOTCONN; |
| 355 | else if (fc->conn_error) |
| 356 | req->out.h.error = -ECONNREFUSED; |
| 357 | else { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 358 | queue_request(fc, req); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 359 | /* acquire extra reference, since request is still needed |
| 360 | after request_end() */ |
| 361 | __fuse_get_request(req); |
| 362 | |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 363 | request_wait_answer(fc, req); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 364 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 365 | spin_unlock(&fuse_lock); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Miklos Szeredi | 7db35c0 | 2005-01-17 09:46:28 +0000 | [diff] [blame] | 368 | static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 369 | { |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 370 | spin_lock(&fuse_lock); |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame] | 371 | if (fc->connected) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 372 | queue_request(fc, req); |
Miklos Szeredi | e79dc7e | 2004-06-23 05:57:30 +0000 | [diff] [blame] | 373 | spin_unlock(&fuse_lock); |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 374 | } else { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 375 | req->out.h.error = -ENOTCONN; |
| 376 | request_end(fc, req); |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 377 | } |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 380 | void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req) |
| 381 | { |
| 382 | req->isreply = 0; |
| 383 | request_send_nowait(fc, req); |
| 384 | } |
| 385 | |
| 386 | void request_send_background(struct fuse_conn *fc, struct fuse_req *req) |
| 387 | { |
| 388 | req->isreply = 1; |
Miklos Szeredi | d17da46 | 2005-03-21 11:47:04 +0000 | [diff] [blame] | 389 | spin_lock(&fuse_lock); |
| 390 | background_request(fc, req); |
| 391 | spin_unlock(&fuse_lock); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 392 | request_send_nowait(fc, req); |
| 393 | } |
| 394 | |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 395 | void fuse_send_init(struct fuse_conn *fc) |
| 396 | { |
| 397 | /* This is called from fuse_read_super() so there's guaranteed |
| 398 | to be a request available */ |
| 399 | struct fuse_req *req = do_get_request(fc); |
| 400 | struct fuse_init_in_out *arg = &req->misc.init_in_out; |
| 401 | arg->major = FUSE_KERNEL_VERSION; |
| 402 | arg->minor = FUSE_KERNEL_MINOR_VERSION; |
| 403 | req->in.h.opcode = FUSE_INIT; |
| 404 | req->in.numargs = 1; |
| 405 | req->in.args[0].size = sizeof(*arg); |
| 406 | req->in.args[0].value = arg; |
| 407 | req->out.numargs = 1; |
| 408 | req->out.args[0].size = sizeof(*arg); |
| 409 | req->out.args[0].value = arg; |
| 410 | request_send_background(fc, req); |
| 411 | } |
| 412 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 413 | /* |
| 414 | * Lock the request. Up to the next unlock_request() there mustn't be |
| 415 | * anything that could cause a page-fault. If the request was already |
| 416 | * interrupted bail out. |
| 417 | */ |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 418 | static inline int lock_request(struct fuse_req *req) |
| 419 | { |
| 420 | int err = 0; |
| 421 | if (req) { |
| 422 | spin_lock(&fuse_lock); |
| 423 | if (req->interrupted) |
| 424 | err = -ENOENT; |
| 425 | else |
| 426 | req->locked = 1; |
| 427 | spin_unlock(&fuse_lock); |
| 428 | } |
| 429 | return err; |
| 430 | } |
| 431 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 432 | /* |
| 433 | * Unlock request. If it was interrupted during being locked, the |
| 434 | * requester thread is currently waiting for it to be unlocked, so |
| 435 | * wake it up. |
| 436 | */ |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 437 | static inline void unlock_request(struct fuse_req *req) |
| 438 | { |
| 439 | if (req) { |
| 440 | spin_lock(&fuse_lock); |
| 441 | req->locked = 0; |
| 442 | if (req->interrupted) |
| 443 | wake_up(&req->waitq); |
| 444 | spin_unlock(&fuse_lock); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | struct fuse_copy_state { |
| 449 | int write; |
| 450 | struct fuse_req *req; |
| 451 | const struct iovec *iov; |
| 452 | unsigned long nr_segs; |
| 453 | unsigned long seglen; |
| 454 | unsigned long addr; |
| 455 | struct page *pg; |
| 456 | void *mapaddr; |
| 457 | void *buf; |
| 458 | unsigned len; |
| 459 | }; |
| 460 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 461 | static void fuse_copy_init(struct fuse_copy_state *cs, int write, |
| 462 | struct fuse_req *req, const struct iovec *iov, |
| 463 | unsigned long nr_segs) |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 464 | { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 465 | memset(cs, 0, sizeof(*cs)); |
| 466 | cs->write = write; |
| 467 | cs->req = req; |
| 468 | cs->iov = iov; |
| 469 | cs->nr_segs = nr_segs; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 472 | /* Unmap and put previous page of userspace buffer */ |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 473 | static inline void fuse_copy_finish(struct fuse_copy_state *cs) |
| 474 | { |
| 475 | if (cs->mapaddr) { |
| 476 | kunmap_atomic(cs->mapaddr, KM_USER0); |
| 477 | if (cs->write) { |
| 478 | flush_dcache_page(cs->pg); |
| 479 | set_page_dirty_lock(cs->pg); |
| 480 | } |
| 481 | put_page(cs->pg); |
| 482 | cs->mapaddr = NULL; |
| 483 | } |
| 484 | } |
| 485 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 486 | /* |
| 487 | * Get another pagefull of userspace buffer, and map it to kernel |
| 488 | * address space, and lock request |
| 489 | */ |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 490 | static int fuse_copy_fill(struct fuse_copy_state *cs) |
| 491 | { |
| 492 | unsigned long offset; |
| 493 | int err; |
| 494 | |
| 495 | unlock_request(cs->req); |
| 496 | fuse_copy_finish(cs); |
| 497 | if (!cs->seglen) { |
| 498 | BUG_ON(!cs->nr_segs); |
| 499 | cs->seglen = cs->iov[0].iov_len; |
| 500 | cs->addr = (unsigned long) cs->iov[0].iov_base; |
| 501 | cs->iov ++; |
| 502 | cs->nr_segs --; |
| 503 | } |
| 504 | down_read(¤t->mm->mmap_sem); |
| 505 | err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0, |
| 506 | &cs->pg, NULL); |
| 507 | up_read(¤t->mm->mmap_sem); |
| 508 | if (err < 0) |
| 509 | return err; |
| 510 | BUG_ON(err != 1); |
| 511 | offset = cs->addr % PAGE_SIZE; |
| 512 | cs->mapaddr = kmap_atomic(cs->pg, KM_USER0); |
| 513 | cs->buf = cs->mapaddr + offset; |
| 514 | cs->len = min(PAGE_SIZE - offset, cs->seglen); |
| 515 | cs->seglen -= cs->len; |
| 516 | cs->addr += cs->len; |
| 517 | |
| 518 | return lock_request(cs->req); |
| 519 | } |
| 520 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 521 | /* Do as much copy to/from userspace buffer as we can */ |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 522 | static inline int fuse_copy_do(struct fuse_copy_state *cs, void **val, |
| 523 | unsigned *size) |
| 524 | { |
| 525 | unsigned ncpy = min(*size, cs->len); |
| 526 | if (val) { |
| 527 | if (cs->write) |
| 528 | memcpy(cs->buf, *val, ncpy); |
| 529 | else |
| 530 | memcpy(*val, cs->buf, ncpy); |
| 531 | *val += ncpy; |
| 532 | } |
| 533 | *size -= ncpy; |
| 534 | cs->len -= ncpy; |
| 535 | cs->buf += ncpy; |
| 536 | return ncpy; |
| 537 | } |
| 538 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 539 | /* |
| 540 | * Copy a page in the request to/from the userspace buffer. Must be |
| 541 | * done atomically |
| 542 | */ |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 543 | static inline int fuse_copy_page(struct fuse_copy_state *cs, struct page *page, |
| 544 | unsigned offset, unsigned count, int zeroing) |
| 545 | { |
| 546 | if (page && zeroing && count < PAGE_SIZE) { |
| 547 | void *mapaddr = kmap_atomic(page, KM_USER1); |
| 548 | memset(mapaddr, 0, PAGE_SIZE); |
| 549 | kunmap_atomic(mapaddr, KM_USER1); |
| 550 | } |
| 551 | while (count) { |
| 552 | int err; |
| 553 | if (!cs->len && (err = fuse_copy_fill(cs))) |
| 554 | return err; |
| 555 | if (page) { |
| 556 | void *mapaddr = kmap_atomic(page, KM_USER1); |
| 557 | void *buf = mapaddr + offset; |
| 558 | offset += fuse_copy_do(cs, &buf, &count); |
| 559 | kunmap_atomic(mapaddr, KM_USER1); |
| 560 | } else |
| 561 | offset += fuse_copy_do(cs, NULL, &count); |
| 562 | } |
| 563 | if (page && !cs->write) |
| 564 | flush_dcache_page(page); |
| 565 | return 0; |
| 566 | } |
| 567 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 568 | /* Copy pages in the request to/from userspace buffer */ |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 569 | static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes, |
| 570 | int zeroing) |
| 571 | { |
| 572 | unsigned i; |
| 573 | struct fuse_req *req = cs->req; |
| 574 | unsigned offset = req->page_offset; |
| 575 | unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset); |
| 576 | |
Miklos Szeredi | 46960ce | 2005-06-02 09:05:00 +0000 | [diff] [blame] | 577 | for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 578 | struct page *page = req->pages[i]; |
| 579 | int err = fuse_copy_page(cs, page, offset, count, zeroing); |
| 580 | if (err) |
| 581 | return err; |
| 582 | |
| 583 | nbytes -= count; |
| 584 | count = min(nbytes, (unsigned) PAGE_SIZE); |
| 585 | offset = 0; |
| 586 | } |
| 587 | return 0; |
| 588 | } |
| 589 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 590 | /* Copy a single argument in the request to/from userspace buffer */ |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 591 | static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size) |
| 592 | { |
| 593 | while (size) { |
| 594 | int err; |
| 595 | if (!cs->len && (err = fuse_copy_fill(cs))) |
| 596 | return err; |
| 597 | fuse_copy_do(cs, &val, &size); |
| 598 | } |
| 599 | return 0; |
| 600 | } |
| 601 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 602 | /* Copy request arguments to/from userspace buffer */ |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 603 | static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs, |
| 604 | unsigned argpages, struct fuse_arg *args, |
| 605 | int zeroing) |
| 606 | { |
| 607 | int err = 0; |
| 608 | unsigned i; |
| 609 | |
| 610 | for (i = 0; !err && i < numargs; i++) { |
| 611 | struct fuse_arg *arg = &args[i]; |
| 612 | if (i == numargs - 1 && argpages) |
| 613 | err = fuse_copy_pages(cs, arg->size, zeroing); |
| 614 | else |
| 615 | err = fuse_copy_one(cs, arg->value, arg->size); |
| 616 | } |
| 617 | return err; |
| 618 | } |
| 619 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 620 | /* Wait until a request is available on the pending list */ |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 621 | static void request_wait(struct fuse_conn *fc) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 622 | { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 623 | DECLARE_WAITQUEUE(wait, current); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 624 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 625 | add_wait_queue_exclusive(&fc->waitq, &wait); |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame] | 626 | while (fc->mounted && list_empty(&fc->pending)) { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 627 | set_current_state(TASK_INTERRUPTIBLE); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 628 | if (signal_pending(current)) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 629 | break; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 630 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 631 | spin_unlock(&fuse_lock); |
| 632 | schedule(); |
| 633 | spin_lock(&fuse_lock); |
| 634 | } |
| 635 | set_current_state(TASK_RUNNING); |
| 636 | remove_wait_queue(&fc->waitq, &wait); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Miklos Szeredi | eab72ef | 2005-03-31 19:59:12 +0000 | [diff] [blame] | 639 | #ifndef KERNEL_2_6 |
| 640 | static inline size_t iov_length(const struct iovec *iov, unsigned long nr_segs) |
| 641 | { |
| 642 | unsigned long seg; |
| 643 | size_t ret = 0; |
| 644 | |
| 645 | for (seg = 0; seg < nr_segs; seg++) |
| 646 | ret += iov[seg].iov_len; |
| 647 | return ret; |
| 648 | } |
| 649 | #endif |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 650 | /* |
| 651 | * Read a single request into the userspace filesystem's buffer. This |
| 652 | * function waits until a request is available, then removes it from |
| 653 | * the pending list and copies request data to userspace buffer. If |
| 654 | * no reply is needed (FORGET) or request has been interrupted or |
| 655 | * there was an error during the copying then it's finished by calling |
| 656 | * request_end(). Otherwise add it to the processing list, and set |
| 657 | * the 'sent' flag. |
| 658 | */ |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 659 | static ssize_t fuse_dev_readv(struct file *file, const struct iovec *iov, |
| 660 | unsigned long nr_segs, loff_t *off) |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 661 | { |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 662 | int err; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 663 | struct fuse_conn *fc; |
| 664 | struct fuse_req *req; |
| 665 | struct fuse_in *in; |
| 666 | struct fuse_copy_state cs; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 667 | unsigned reqsize; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 668 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 669 | spin_lock(&fuse_lock); |
| 670 | fc = file->private_data; |
| 671 | err = -EPERM; |
| 672 | if (!fc) |
| 673 | goto err_unlock; |
| 674 | request_wait(fc); |
| 675 | err = -ENODEV; |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame] | 676 | if (!fc->mounted) |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 677 | goto err_unlock; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 678 | err = -ERESTARTSYS; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 679 | if (list_empty(&fc->pending)) |
| 680 | goto err_unlock; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 681 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 682 | req = list_entry(fc->pending.next, struct fuse_req, list); |
| 683 | list_del_init(&req->list); |
| 684 | spin_unlock(&fuse_lock); |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 685 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 686 | in = &req->in; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 687 | reqsize = req->in.h.len; |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 688 | fuse_copy_init(&cs, 1, req, iov, nr_segs); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 689 | err = -EINVAL; |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 690 | if (iov_length(iov, nr_segs) >= reqsize) { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 691 | err = fuse_copy_one(&cs, &in->h, sizeof(in->h)); |
| 692 | if (!err) |
| 693 | err = fuse_copy_args(&cs, in->numargs, in->argpages, |
| 694 | (struct fuse_arg *) in->args, 0); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 695 | } |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 696 | fuse_copy_finish(&cs); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 697 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 698 | spin_lock(&fuse_lock); |
| 699 | req->locked = 0; |
| 700 | if (!err && req->interrupted) |
| 701 | err = -ENOENT; |
| 702 | if (err) { |
| 703 | if (!req->interrupted) |
| 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->sent = 1; |
| 712 | list_add_tail(&req->list, &fc->processing); |
| 713 | spin_unlock(&fuse_lock); |
| 714 | } |
| 715 | return reqsize; |
| 716 | |
| 717 | err_unlock: |
| 718 | spin_unlock(&fuse_lock); |
| 719 | return err; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 720 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 721 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 722 | static ssize_t fuse_dev_read(struct file *file, char __user *buf, |
| 723 | size_t nbytes, loff_t *off) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 724 | { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 725 | struct iovec iov; |
| 726 | iov.iov_len = nbytes; |
| 727 | iov.iov_base = buf; |
| 728 | return fuse_dev_readv(file, &iov, 1, off); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 731 | /* Look up request on processing list by unique ID */ |
Miklos Szeredi | 8d4d1b8 | 2005-07-22 11:58:47 +0000 | [diff] [blame] | 732 | static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 733 | { |
| 734 | struct list_head *entry; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 735 | |
| 736 | list_for_each(entry, &fc->processing) { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 737 | struct fuse_req *req; |
| 738 | req = list_entry(entry, struct fuse_req, list); |
| 739 | if (req->in.h.unique == unique) |
| 740 | return req; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 741 | } |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 742 | return NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 745 | static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out, |
| 746 | unsigned nbytes) |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 747 | { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 748 | unsigned reqsize = sizeof(struct fuse_out_header); |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 749 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 750 | if (out->h.error) |
| 751 | return nbytes != reqsize ? -EINVAL : 0; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 752 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 753 | reqsize += len_args(out->numargs, out->args); |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 754 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 755 | if (reqsize < nbytes || (reqsize > nbytes && !out->argvar)) |
| 756 | return -EINVAL; |
| 757 | else if (reqsize > nbytes) { |
| 758 | struct fuse_arg *lastarg = &out->args[out->numargs-1]; |
| 759 | unsigned diffsize = reqsize - nbytes; |
| 760 | if (diffsize > lastarg->size) |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 761 | return -EINVAL; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 762 | lastarg->size -= diffsize; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 763 | } |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 764 | return fuse_copy_args(cs, out->numargs, out->argpages, out->args, |
| 765 | out->page_zeroing); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 768 | /* |
| 769 | * Write a single reply to a request. First the header is copied from |
| 770 | * the write buffer. The request is then searched on the processing |
| 771 | * list by the unique ID found in the header. If found, then remove |
| 772 | * it from the list and copy the rest of the buffer to the request. |
| 773 | * The request is finished by calling request_end() |
| 774 | */ |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 775 | static ssize_t fuse_dev_writev(struct file *file, const struct iovec *iov, |
| 776 | unsigned long nr_segs, loff_t *off) |
Miklos Szeredi | da4e486 | 2003-09-08 11:14:11 +0000 | [diff] [blame] | 777 | { |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 778 | int err; |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 779 | unsigned nbytes = iov_length(iov, nr_segs); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 780 | struct fuse_req *req; |
| 781 | struct fuse_out_header oh; |
| 782 | struct fuse_copy_state cs; |
| 783 | struct fuse_conn *fc = fuse_get_conn(file); |
| 784 | if (!fc) |
| 785 | return -ENODEV; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 786 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 787 | fuse_copy_init(&cs, 0, NULL, iov, nr_segs); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 788 | if (nbytes < sizeof(struct fuse_out_header)) |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 789 | return -EINVAL; |
Miklos Szeredi | da4e486 | 2003-09-08 11:14:11 +0000 | [diff] [blame] | 790 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 791 | err = fuse_copy_one(&cs, &oh, sizeof(oh)); |
| 792 | if (err) |
| 793 | goto err_finish; |
| 794 | err = -EINVAL; |
Miklos Szeredi | cdae16e | 2005-01-14 11:43:22 +0000 | [diff] [blame] | 795 | if (!oh.unique || oh.error <= -1000 || oh.error > 0 || |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 796 | oh.len != nbytes) |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 797 | goto err_finish; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 798 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 799 | spin_lock(&fuse_lock); |
| 800 | req = request_find(fc, oh.unique); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 801 | err = -EINVAL; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 802 | if (!req) |
| 803 | goto err_unlock; |
Miklos Szeredi | da4e486 | 2003-09-08 11:14:11 +0000 | [diff] [blame] | 804 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 805 | list_del_init(&req->list); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 806 | if (req->interrupted) { |
| 807 | request_end(fc, req); |
| 808 | fuse_copy_finish(&cs); |
| 809 | return -ENOENT; |
| 810 | } |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 811 | req->out.h = oh; |
| 812 | req->locked = 1; |
| 813 | cs.req = req; |
| 814 | spin_unlock(&fuse_lock); |
| 815 | |
| 816 | err = copy_out_args(&cs, &req->out, nbytes); |
| 817 | fuse_copy_finish(&cs); |
| 818 | |
| 819 | spin_lock(&fuse_lock); |
| 820 | req->locked = 0; |
| 821 | if (!err) { |
| 822 | if (req->interrupted) |
| 823 | err = -ENOENT; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 824 | } else if (!req->interrupted) |
| 825 | req->out.h.error = -EIO; |
| 826 | request_end(fc, req); |
| 827 | |
| 828 | return err ? err : nbytes; |
| 829 | |
| 830 | err_unlock: |
| 831 | spin_unlock(&fuse_lock); |
| 832 | err_finish: |
| 833 | fuse_copy_finish(&cs); |
Miklos Szeredi | da4e486 | 2003-09-08 11:14:11 +0000 | [diff] [blame] | 834 | return err; |
| 835 | } |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 836 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 837 | static ssize_t fuse_dev_write(struct file *file, const char __user *buf, |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 838 | size_t nbytes, loff_t *off) |
| 839 | { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 840 | struct iovec iov; |
| 841 | iov.iov_len = nbytes; |
| 842 | iov.iov_base = (char __user *) buf; |
| 843 | return fuse_dev_writev(file, &iov, 1, off); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 846 | static unsigned fuse_dev_poll(struct file *file, poll_table *wait) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 847 | { |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 848 | struct fuse_conn *fc = fuse_get_conn(file); |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 849 | unsigned mask = POLLOUT | POLLWRNORM; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 850 | |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 851 | if (!fc) |
| 852 | return -ENODEV; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 853 | |
| 854 | poll_wait(file, &fc->waitq, wait); |
| 855 | |
| 856 | spin_lock(&fuse_lock); |
| 857 | if (!list_empty(&fc->pending)) |
| 858 | mask |= POLLIN | POLLRDNORM; |
| 859 | spin_unlock(&fuse_lock); |
| 860 | |
| 861 | return mask; |
| 862 | } |
| 863 | |
Miklos Szeredi | 407e6a7 | 2005-03-25 12:19:43 +0000 | [diff] [blame] | 864 | /* Abort all requests on the given list (pending or processing) */ |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 865 | static void end_requests(struct fuse_conn *fc, struct list_head *head) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 866 | { |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 867 | while (!list_empty(head)) { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 868 | struct fuse_req *req; |
| 869 | req = list_entry(head->next, struct fuse_req, list); |
| 870 | list_del_init(&req->list); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 871 | req->out.h.error = -ECONNABORTED; |
| 872 | request_end(fc, req); |
| 873 | spin_lock(&fuse_lock); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
| 877 | static int fuse_dev_release(struct inode *inode, struct file *file) |
| 878 | { |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 879 | struct fuse_conn *fc; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 880 | |
| 881 | spin_lock(&fuse_lock); |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 882 | fc = file->private_data; |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 883 | if (fc) { |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame] | 884 | fc->connected = 0; |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 885 | end_requests(fc, &fc->pending); |
| 886 | end_requests(fc, &fc->processing); |
| 887 | fuse_release_conn(fc); |
| 888 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 889 | spin_unlock(&fuse_lock); |
| 890 | return 0; |
| 891 | } |
| 892 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 893 | struct file_operations fuse_dev_operations = { |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 894 | .owner = THIS_MODULE, |
Miklos Szeredi | 092b50b | 2005-01-20 18:05:07 +0000 | [diff] [blame] | 895 | .llseek = no_llseek, |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 896 | .read = fuse_dev_read, |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 897 | .readv = fuse_dev_readv, |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 898 | .write = fuse_dev_write, |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 899 | .writev = fuse_dev_writev, |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 900 | .poll = fuse_dev_poll, |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 901 | .release = fuse_dev_release, |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 902 | }; |
| 903 | |
Miklos Szeredi | 162bcbb | 2004-11-29 23:43:44 +0000 | [diff] [blame] | 904 | static struct miscdevice fuse_miscdevice = { |
| 905 | .minor = FUSE_MINOR, |
| 906 | .name = "fuse", |
| 907 | .fops = &fuse_dev_operations, |
| 908 | }; |
| 909 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 910 | int __init fuse_dev_init(void) |
| 911 | { |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 912 | int err = -ENOMEM; |
Miklos Szeredi | 2a807ec | 2005-01-10 11:50:10 +0000 | [diff] [blame] | 913 | fuse_req_cachep = kmem_cache_create("fuse_request", |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 914 | sizeof(struct fuse_req), |
| 915 | 0, 0, NULL, NULL); |
| 916 | if (!fuse_req_cachep) |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 917 | goto out; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 918 | |
Miklos Szeredi | 162bcbb | 2004-11-29 23:43:44 +0000 | [diff] [blame] | 919 | err = misc_register(&fuse_miscdevice); |
| 920 | if (err) |
| 921 | goto out_cache_clean; |
| 922 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 923 | return 0; |
Miklos Szeredi | 162bcbb | 2004-11-29 23:43:44 +0000 | [diff] [blame] | 924 | |
| 925 | out_cache_clean: |
| 926 | kmem_cache_destroy(fuse_req_cachep); |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 927 | out: |
| 928 | return err; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 931 | void fuse_dev_cleanup(void) |
| 932 | { |
Miklos Szeredi | 162bcbb | 2004-11-29 23:43:44 +0000 | [diff] [blame] | 933 | misc_deregister(&fuse_miscdevice); |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 934 | kmem_cache_destroy(fuse_req_cachep); |
| 935 | } |