Blame view

fs/fuse/virtio_fs.c 36.8 KB
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1
2
3
4
5
6
7
  // SPDX-License-Identifier: GPL-2.0
  /*
   * virtio-fs: Virtio Filesystem
   * Copyright (C) 2018 Red Hat, Inc.
   */
  
  #include <linux/fs.h>
22f3787e9   Stefan Hajnoczi   virtiofs: set up ...
8
9
10
  #include <linux/dax.h>
  #include <linux/pci.h>
  #include <linux/pfn_t.h>
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
11
12
13
14
15
  #include <linux/module.h>
  #include <linux/virtio.h>
  #include <linux/virtio_fs.h>
  #include <linux/delay.h>
  #include <linux/fs_context.h>
1dd539577   Vivek Goyal   virtiofs: add a m...
16
  #include <linux/fs_parser.h>
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
17
  #include <linux/highmem.h>
22f3787e9   Stefan Hajnoczi   virtiofs: set up ...
18
  #include <linux/uio.h>
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
19
20
21
22
23
24
25
26
27
28
29
30
  #include "fuse_i.h"
  
  /* List of virtio-fs device instances and a lock for the list. Also provides
   * mutual exclusion in device removal and mounting path
   */
  static DEFINE_MUTEX(virtio_fs_mutex);
  static LIST_HEAD(virtio_fs_instances);
  
  enum {
  	VQ_HIPRIO,
  	VQ_REQUEST
  };
b43b7e81e   Vivek Goyal   virtiofs: provide...
31
  #define VQ_NAME_LEN	24
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
32
33
34
35
36
37
  /* Per-virtqueue state */
  struct virtio_fs_vq {
  	spinlock_t lock;
  	struct virtqueue *vq;     /* protected by ->lock */
  	struct work_struct done_work;
  	struct list_head queued_reqs;
51fecdd25   Vivek Goyal   virtiofs: Do not ...
38
  	struct list_head end_reqs;	/* End these requests */
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
39
40
41
42
  	struct delayed_work dispatch_work;
  	struct fuse_dev *fud;
  	bool connected;
  	long in_flight;
724c15a43   Vivek Goyal   virtiofs: Use com...
43
  	struct completion in_flight_zero; /* No inflight requests */
b43b7e81e   Vivek Goyal   virtiofs: provide...
44
  	char name[VQ_NAME_LEN];
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
45
46
47
48
49
50
51
52
53
54
  } ____cacheline_aligned_in_smp;
  
  /* A virtio-fs device instance */
  struct virtio_fs {
  	struct kref refcount;
  	struct list_head list;    /* on virtio_fs_instances */
  	char *tag;
  	struct virtio_fs_vq *vqs;
  	unsigned int nvqs;               /* number of virtqueues */
  	unsigned int num_request_queues; /* number of request queues */
22f3787e9   Stefan Hajnoczi   virtiofs: set up ...
55
56
57
58
59
60
  	struct dax_device *dax_dev;
  
  	/* DAX memory window where file contents are mapped */
  	void *window_kaddr;
  	phys_addr_t window_phys_addr;
  	size_t window_len;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
61
  };
1efcf39eb   Vivek Goyal   virtiofs: Do not ...
62
  struct virtio_fs_forget_req {
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
63
64
  	struct fuse_in_header ih;
  	struct fuse_forget_in arg;
1efcf39eb   Vivek Goyal   virtiofs: Do not ...
65
66
67
  };
  
  struct virtio_fs_forget {
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
68
69
  	/* This request can be temporarily queued on virt queue */
  	struct list_head list;
1efcf39eb   Vivek Goyal   virtiofs: Do not ...
70
  	struct virtio_fs_forget_req req;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
71
  };
bb737bbe4   Vivek Goyal   virtiofs: schedul...
72
73
74
75
76
  struct virtio_fs_req_work {
  	struct fuse_req *req;
  	struct virtio_fs_vq *fsvq;
  	struct work_struct done_work;
  };
a9bfd9dd3   Vivek Goyal   virtiofs: Retry r...
77
78
  static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
  				 struct fuse_req *req, bool in_flight);
1dd539577   Vivek Goyal   virtiofs: add a m...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  enum {
  	OPT_DAX,
  };
  
  static const struct fs_parameter_spec virtio_fs_parameters[] = {
  	fsparam_flag("dax", OPT_DAX),
  	{}
  };
  
  static int virtio_fs_parse_param(struct fs_context *fc,
  				 struct fs_parameter *param)
  {
  	struct fs_parse_result result;
  	struct fuse_fs_context *ctx = fc->fs_private;
  	int opt;
  
  	opt = fs_parse(fc, virtio_fs_parameters, param, &result);
  	if (opt < 0)
  		return opt;
  
  	switch (opt) {
  	case OPT_DAX:
  		ctx->dax = 1;
  		break;
  	default:
  		return -EINVAL;
  	}
  
  	return 0;
  }
  
  static void virtio_fs_free_fc(struct fs_context *fc)
  {
  	struct fuse_fs_context *ctx = fc->fs_private;
  
  	kfree(ctx);
  }
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
116
117
118
119
120
121
122
123
124
125
126
  static inline struct virtio_fs_vq *vq_to_fsvq(struct virtqueue *vq)
  {
  	struct virtio_fs *fs = vq->vdev->priv;
  
  	return &fs->vqs[vq->index];
  }
  
  static inline struct fuse_pqueue *vq_to_fpq(struct virtqueue *vq)
  {
  	return &vq_to_fsvq(vq)->fud->pq;
  }
c17ea0096   Vivek Goyal   virtiofs: Count p...
127
128
129
130
131
132
133
134
135
136
137
  /* Should be called with fsvq->lock held. */
  static inline void inc_in_flight_req(struct virtio_fs_vq *fsvq)
  {
  	fsvq->in_flight++;
  }
  
  /* Should be called with fsvq->lock held. */
  static inline void dec_in_flight_req(struct virtio_fs_vq *fsvq)
  {
  	WARN_ON(fsvq->in_flight <= 0);
  	fsvq->in_flight--;
724c15a43   Vivek Goyal   virtiofs: Use com...
138
139
  	if (!fsvq->in_flight)
  		complete(&fsvq->in_flight_zero);
c17ea0096   Vivek Goyal   virtiofs: Count p...
140
  }
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  static void release_virtio_fs_obj(struct kref *ref)
  {
  	struct virtio_fs *vfs = container_of(ref, struct virtio_fs, refcount);
  
  	kfree(vfs->vqs);
  	kfree(vfs);
  }
  
  /* Make sure virtiofs_mutex is held */
  static void virtio_fs_put(struct virtio_fs *fs)
  {
  	kref_put(&fs->refcount, release_virtio_fs_obj);
  }
  
  static void virtio_fs_fiq_release(struct fuse_iqueue *fiq)
  {
  	struct virtio_fs *vfs = fiq->priv;
  
  	mutex_lock(&virtio_fs_mutex);
  	virtio_fs_put(vfs);
  	mutex_unlock(&virtio_fs_mutex);
  }
  
  static void virtio_fs_drain_queue(struct virtio_fs_vq *fsvq)
  {
  	WARN_ON(fsvq->in_flight < 0);
  
  	/* Wait for in flight requests to finish.*/
724c15a43   Vivek Goyal   virtiofs: Use com...
169
170
171
172
173
174
175
176
177
  	spin_lock(&fsvq->lock);
  	if (fsvq->in_flight) {
  		/* We are holding virtio_fs_mutex. There should not be any
  		 * waiters waiting for completion.
  		 */
  		reinit_completion(&fsvq->in_flight_zero);
  		spin_unlock(&fsvq->lock);
  		wait_for_completion(&fsvq->in_flight_zero);
  	} else {
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
178
  		spin_unlock(&fsvq->lock);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
179
180
181
182
183
  	}
  
  	flush_work(&fsvq->done_work);
  	flush_delayed_work(&fsvq->dispatch_work);
  }
724c15a43   Vivek Goyal   virtiofs: Use com...
184
  static void virtio_fs_drain_all_queues_locked(struct virtio_fs *fs)
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
185
186
187
188
189
190
  {
  	struct virtio_fs_vq *fsvq;
  	int i;
  
  	for (i = 0; i < fs->nvqs; i++) {
  		fsvq = &fs->vqs[i];
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
191
192
193
  		virtio_fs_drain_queue(fsvq);
  	}
  }
724c15a43   Vivek Goyal   virtiofs: Use com...
194
195
196
197
198
199
200
201
202
203
204
205
  static void virtio_fs_drain_all_queues(struct virtio_fs *fs)
  {
  	/* Provides mutual exclusion between ->remove and ->kill_sb
  	 * paths. We don't want both of these draining queue at the
  	 * same time. Current completion logic reinits completion
  	 * and that means there should not be any other thread
  	 * doing reinit or waiting for completion already.
  	 */
  	mutex_lock(&virtio_fs_mutex);
  	virtio_fs_drain_all_queues_locked(fs);
  	mutex_unlock(&virtio_fs_mutex);
  }
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  static void virtio_fs_start_all_queues(struct virtio_fs *fs)
  {
  	struct virtio_fs_vq *fsvq;
  	int i;
  
  	for (i = 0; i < fs->nvqs; i++) {
  		fsvq = &fs->vqs[i];
  		spin_lock(&fsvq->lock);
  		fsvq->connected = true;
  		spin_unlock(&fsvq->lock);
  	}
  }
  
  /* Add a new instance to the list or return -EEXIST if tag name exists*/
  static int virtio_fs_add_instance(struct virtio_fs *fs)
  {
  	struct virtio_fs *fs2;
  	bool duplicate = false;
  
  	mutex_lock(&virtio_fs_mutex);
  
  	list_for_each_entry(fs2, &virtio_fs_instances, list) {
  		if (strcmp(fs->tag, fs2->tag) == 0)
  			duplicate = true;
  	}
  
  	if (!duplicate)
  		list_add_tail(&fs->list, &virtio_fs_instances);
  
  	mutex_unlock(&virtio_fs_mutex);
  
  	if (duplicate)
  		return -EEXIST;
  	return 0;
  }
  
  /* Return the virtio_fs with a given tag, or NULL */
  static struct virtio_fs *virtio_fs_find_instance(const char *tag)
  {
  	struct virtio_fs *fs;
  
  	mutex_lock(&virtio_fs_mutex);
  
  	list_for_each_entry(fs, &virtio_fs_instances, list) {
  		if (strcmp(fs->tag, tag) == 0) {
  			kref_get(&fs->refcount);
  			goto found;
  		}
  	}
  
  	fs = NULL; /* not found */
  
  found:
  	mutex_unlock(&virtio_fs_mutex);
  
  	return fs;
  }
  
  static void virtio_fs_free_devs(struct virtio_fs *fs)
  {
  	unsigned int i;
  
  	for (i = 0; i < fs->nvqs; i++) {
  		struct virtio_fs_vq *fsvq = &fs->vqs[i];
  
  		if (!fsvq->fud)
  			continue;
  
  		fuse_dev_free(fsvq->fud);
  		fsvq->fud = NULL;
  	}
  }
  
  /* Read filesystem name from virtio config into fs->tag (must kfree()). */
  static int virtio_fs_read_tag(struct virtio_device *vdev, struct virtio_fs *fs)
  {
  	char tag_buf[sizeof_field(struct virtio_fs_config, tag)];
  	char *end;
  	size_t len;
  
  	virtio_cread_bytes(vdev, offsetof(struct virtio_fs_config, tag),
  			   &tag_buf, sizeof(tag_buf));
  	end = memchr(tag_buf, '\0', sizeof(tag_buf));
  	if (end == tag_buf)
  		return -EINVAL; /* empty tag */
  	if (!end)
  		end = &tag_buf[sizeof(tag_buf)];
  
  	len = end - tag_buf;
  	fs->tag = devm_kmalloc(&vdev->dev, len + 1, GFP_KERNEL);
  	if (!fs->tag)
  		return -ENOMEM;
  	memcpy(fs->tag, tag_buf, len);
  	fs->tag[len] = '\0';
  	return 0;
  }
  
  /* Work function for hiprio completion */
  static void virtio_fs_hiprio_done_work(struct work_struct *work)
  {
  	struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
  						 done_work);
  	struct virtqueue *vq = fsvq->vq;
  
  	/* Free completed FUSE_FORGET requests */
  	spin_lock(&fsvq->lock);
  	do {
  		unsigned int len;
  		void *req;
  
  		virtqueue_disable_cb(vq);
  
  		while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
  			kfree(req);
c17ea0096   Vivek Goyal   virtiofs: Count p...
320
  			dec_in_flight_req(fsvq);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
321
322
323
324
  		}
  	} while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq)));
  	spin_unlock(&fsvq->lock);
  }
51fecdd25   Vivek Goyal   virtiofs: Do not ...
325
  static void virtio_fs_request_dispatch_work(struct work_struct *work)
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
326
  {
51fecdd25   Vivek Goyal   virtiofs: Do not ...
327
328
329
  	struct fuse_req *req;
  	struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
  						 dispatch_work.work);
a9bfd9dd3   Vivek Goyal   virtiofs: Retry r...
330
  	int ret;
51fecdd25   Vivek Goyal   virtiofs: Do not ...
331
332
333
334
335
336
337
338
339
  
  	pr_debug("virtio-fs: worker %s called.
  ", __func__);
  	while (1) {
  		spin_lock(&fsvq->lock);
  		req = list_first_entry_or_null(&fsvq->end_reqs, struct fuse_req,
  					       list);
  		if (!req) {
  			spin_unlock(&fsvq->lock);
a9bfd9dd3   Vivek Goyal   virtiofs: Retry r...
340
  			break;
51fecdd25   Vivek Goyal   virtiofs: Do not ...
341
342
343
344
  		}
  
  		list_del_init(&req->list);
  		spin_unlock(&fsvq->lock);
8f622e949   Max Reitz   fuse: drop fuse_c...
345
  		fuse_request_end(req);
51fecdd25   Vivek Goyal   virtiofs: Do not ...
346
  	}
a9bfd9dd3   Vivek Goyal   virtiofs: Retry r...
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
  
  	/* Dispatch pending requests */
  	while (1) {
  		spin_lock(&fsvq->lock);
  		req = list_first_entry_or_null(&fsvq->queued_reqs,
  					       struct fuse_req, list);
  		if (!req) {
  			spin_unlock(&fsvq->lock);
  			return;
  		}
  		list_del_init(&req->list);
  		spin_unlock(&fsvq->lock);
  
  		ret = virtio_fs_enqueue_req(fsvq, req, true);
  		if (ret < 0) {
  			if (ret == -ENOMEM || ret == -ENOSPC) {
  				spin_lock(&fsvq->lock);
  				list_add_tail(&req->list, &fsvq->queued_reqs);
  				schedule_delayed_work(&fsvq->dispatch_work,
  						      msecs_to_jiffies(1));
  				spin_unlock(&fsvq->lock);
  				return;
  			}
  			req->out.h.error = ret;
  			spin_lock(&fsvq->lock);
  			dec_in_flight_req(fsvq);
  			spin_unlock(&fsvq->lock);
  			pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d
  ",
  			       ret);
8f622e949   Max Reitz   fuse: drop fuse_c...
377
  			fuse_request_end(req);
a9bfd9dd3   Vivek Goyal   virtiofs: Retry r...
378
379
  		}
  	}
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
380
  }
58ada94f9   Vivek Goyal   virtiofs: Use a c...
381
382
383
384
385
386
387
388
389
390
391
392
  /*
   * Returns 1 if queue is full and sender should wait a bit before sending
   * next request, 0 otherwise.
   */
  static int send_forget_request(struct virtio_fs_vq *fsvq,
  			       struct virtio_fs_forget *forget,
  			       bool in_flight)
  {
  	struct scatterlist sg;
  	struct virtqueue *vq;
  	int ret = 0;
  	bool notify;
1efcf39eb   Vivek Goyal   virtiofs: Do not ...
393
  	struct virtio_fs_forget_req *req = &forget->req;
58ada94f9   Vivek Goyal   virtiofs: Use a c...
394
395
396
397
398
399
400
401
  
  	spin_lock(&fsvq->lock);
  	if (!fsvq->connected) {
  		if (in_flight)
  			dec_in_flight_req(fsvq);
  		kfree(forget);
  		goto out;
  	}
1efcf39eb   Vivek Goyal   virtiofs: Do not ...
402
  	sg_init_one(&sg, req, sizeof(*req));
58ada94f9   Vivek Goyal   virtiofs: Use a c...
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
  	vq = fsvq->vq;
  	dev_dbg(&vq->vdev->dev, "%s
  ", __func__);
  
  	ret = virtqueue_add_outbuf(vq, &sg, 1, forget, GFP_ATOMIC);
  	if (ret < 0) {
  		if (ret == -ENOMEM || ret == -ENOSPC) {
  			pr_debug("virtio-fs: Could not queue FORGET: err=%d. Will try later
  ",
  				 ret);
  			list_add_tail(&forget->list, &fsvq->queued_reqs);
  			schedule_delayed_work(&fsvq->dispatch_work,
  					      msecs_to_jiffies(1));
  			if (!in_flight)
  				inc_in_flight_req(fsvq);
  			/* Queue is full */
  			ret = 1;
  		} else {
  			pr_debug("virtio-fs: Could not queue FORGET: err=%d. Dropping it.
  ",
  				 ret);
  			kfree(forget);
  			if (in_flight)
  				dec_in_flight_req(fsvq);
  		}
  		goto out;
  	}
  
  	if (!in_flight)
  		inc_in_flight_req(fsvq);
  	notify = virtqueue_kick_prepare(vq);
  	spin_unlock(&fsvq->lock);
  
  	if (notify)
  		virtqueue_notify(vq);
  	return ret;
  out:
  	spin_unlock(&fsvq->lock);
  	return ret;
  }
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
443
444
445
446
447
  static void virtio_fs_hiprio_dispatch_work(struct work_struct *work)
  {
  	struct virtio_fs_forget *forget;
  	struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
  						 dispatch_work.work);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
448
449
450
451
452
453
454
455
456
457
458
459
  	pr_debug("virtio-fs: worker %s called.
  ", __func__);
  	while (1) {
  		spin_lock(&fsvq->lock);
  		forget = list_first_entry_or_null(&fsvq->queued_reqs,
  					struct virtio_fs_forget, list);
  		if (!forget) {
  			spin_unlock(&fsvq->lock);
  			return;
  		}
  
  		list_del(&forget->list);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
460
  		spin_unlock(&fsvq->lock);
58ada94f9   Vivek Goyal   virtiofs: Use a c...
461
462
  		if (send_forget_request(fsvq, forget, true))
  			return;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
  	}
  }
  
  /* Allocate and copy args into req->argbuf */
  static int copy_args_to_argbuf(struct fuse_req *req)
  {
  	struct fuse_args *args = req->args;
  	unsigned int offset = 0;
  	unsigned int num_in;
  	unsigned int num_out;
  	unsigned int len;
  	unsigned int i;
  
  	num_in = args->in_numargs - args->in_pages;
  	num_out = args->out_numargs - args->out_pages;
  	len = fuse_len_args(num_in, (struct fuse_arg *) args->in_args) +
  	      fuse_len_args(num_out, args->out_args);
  
  	req->argbuf = kmalloc(len, GFP_ATOMIC);
  	if (!req->argbuf)
  		return -ENOMEM;
  
  	for (i = 0; i < num_in; i++) {
  		memcpy(req->argbuf + offset,
  		       args->in_args[i].value,
  		       args->in_args[i].size);
  		offset += args->in_args[i].size;
  	}
  
  	return 0;
  }
  
  /* Copy args out of and free req->argbuf */
  static void copy_args_from_argbuf(struct fuse_args *args, struct fuse_req *req)
  {
  	unsigned int remaining;
  	unsigned int offset;
  	unsigned int num_in;
  	unsigned int num_out;
  	unsigned int i;
  
  	remaining = req->out.h.len - sizeof(req->out.h);
  	num_in = args->in_numargs - args->in_pages;
  	num_out = args->out_numargs - args->out_pages;
  	offset = fuse_len_args(num_in, (struct fuse_arg *)args->in_args);
  
  	for (i = 0; i < num_out; i++) {
  		unsigned int argsize = args->out_args[i].size;
  
  		if (args->out_argvar &&
  		    i == args->out_numargs - 1 &&
  		    argsize > remaining) {
  			argsize = remaining;
  		}
  
  		memcpy(args->out_args[i].value, req->argbuf + offset, argsize);
  		offset += argsize;
  
  		if (i != args->out_numargs - 1)
  			remaining -= argsize;
  	}
  
  	/* Store the actual size of the variable-length arg */
  	if (args->out_argvar)
  		args->out_args[args->out_numargs - 1].size = remaining;
  
  	kfree(req->argbuf);
  	req->argbuf = NULL;
  }
  
  /* Work function for request completion */
bb737bbe4   Vivek Goyal   virtiofs: schedul...
534
535
536
537
  static void virtio_fs_request_complete(struct fuse_req *req,
  				       struct virtio_fs_vq *fsvq)
  {
  	struct fuse_pqueue *fpq = &fsvq->fud->pq;
bb737bbe4   Vivek Goyal   virtiofs: schedul...
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
  	struct fuse_args *args;
  	struct fuse_args_pages *ap;
  	unsigned int len, i, thislen;
  	struct page *page;
  
  	/*
  	 * TODO verify that server properly follows FUSE protocol
  	 * (oh.uniq, oh.len)
  	 */
  	args = req->args;
  	copy_args_from_argbuf(args, req);
  
  	if (args->out_pages && args->page_zeroing) {
  		len = args->out_args[args->out_numargs - 1].size;
  		ap = container_of(args, typeof(*ap), args);
  		for (i = 0; i < ap->num_pages; i++) {
  			thislen = ap->descs[i].length;
  			if (len < thislen) {
  				WARN_ON(ap->descs[i].offset);
  				page = ap->pages[i];
  				zero_user_segment(page, len, thislen);
  				len = 0;
  			} else {
  				len -= thislen;
  			}
  		}
  	}
  
  	spin_lock(&fpq->lock);
  	clear_bit(FR_SENT, &req->flags);
  	spin_unlock(&fpq->lock);
8f622e949   Max Reitz   fuse: drop fuse_c...
569
  	fuse_request_end(req);
bb737bbe4   Vivek Goyal   virtiofs: schedul...
570
571
572
573
574
575
576
577
578
579
580
581
582
  	spin_lock(&fsvq->lock);
  	dec_in_flight_req(fsvq);
  	spin_unlock(&fsvq->lock);
  }
  
  static void virtio_fs_complete_req_work(struct work_struct *work)
  {
  	struct virtio_fs_req_work *w =
  		container_of(work, typeof(*w), done_work);
  
  	virtio_fs_request_complete(w->req, w->fsvq);
  	kfree(w);
  }
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
583
584
585
586
587
  static void virtio_fs_requests_done_work(struct work_struct *work)
  {
  	struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
  						 done_work);
  	struct fuse_pqueue *fpq = &fsvq->fud->pq;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
588
589
  	struct virtqueue *vq = fsvq->vq;
  	struct fuse_req *req;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
590
  	struct fuse_req *next;
bb737bbe4   Vivek Goyal   virtiofs: schedul...
591
  	unsigned int len;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
  	LIST_HEAD(reqs);
  
  	/* Collect completed requests off the virtqueue */
  	spin_lock(&fsvq->lock);
  	do {
  		virtqueue_disable_cb(vq);
  
  		while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
  			spin_lock(&fpq->lock);
  			list_move_tail(&req->list, &reqs);
  			spin_unlock(&fpq->lock);
  		}
  	} while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq)));
  	spin_unlock(&fsvq->lock);
  
  	/* End requests */
  	list_for_each_entry_safe(req, next, &reqs, list) {
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
609
  		list_del_init(&req->list);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
610

bb737bbe4   Vivek Goyal   virtiofs: schedul...
611
612
613
614
615
616
617
618
619
620
621
622
  		/* blocking async request completes in a worker context */
  		if (req->args->may_block) {
  			struct virtio_fs_req_work *w;
  
  			w = kzalloc(sizeof(*w), GFP_NOFS | __GFP_NOFAIL);
  			INIT_WORK(&w->done_work, virtio_fs_complete_req_work);
  			w->fsvq = fsvq;
  			w->req = req;
  			schedule_work(&w->done_work);
  		} else {
  			virtio_fs_request_complete(req, fsvq);
  		}
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
623
624
625
626
627
628
629
630
631
632
633
634
635
  	}
  }
  
  /* Virtqueue interrupt handler */
  static void virtio_fs_vq_done(struct virtqueue *vq)
  {
  	struct virtio_fs_vq *fsvq = vq_to_fsvq(vq);
  
  	dev_dbg(&vq->vdev->dev, "%s %s
  ", __func__, fsvq->name);
  
  	schedule_work(&fsvq->done_work);
  }
b43b7e81e   Vivek Goyal   virtiofs: provide...
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
  static void virtio_fs_init_vq(struct virtio_fs_vq *fsvq, char *name,
  			      int vq_type)
  {
  	strncpy(fsvq->name, name, VQ_NAME_LEN);
  	spin_lock_init(&fsvq->lock);
  	INIT_LIST_HEAD(&fsvq->queued_reqs);
  	INIT_LIST_HEAD(&fsvq->end_reqs);
  	init_completion(&fsvq->in_flight_zero);
  
  	if (vq_type == VQ_REQUEST) {
  		INIT_WORK(&fsvq->done_work, virtio_fs_requests_done_work);
  		INIT_DELAYED_WORK(&fsvq->dispatch_work,
  				  virtio_fs_request_dispatch_work);
  	} else {
  		INIT_WORK(&fsvq->done_work, virtio_fs_hiprio_done_work);
  		INIT_DELAYED_WORK(&fsvq->dispatch_work,
  				  virtio_fs_hiprio_dispatch_work);
  	}
  }
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
655
656
657
658
659
660
661
662
663
  /* Initialize virtqueues */
  static int virtio_fs_setup_vqs(struct virtio_device *vdev,
  			       struct virtio_fs *fs)
  {
  	struct virtqueue **vqs;
  	vq_callback_t **callbacks;
  	const char **names;
  	unsigned int i;
  	int ret = 0;
2c0349ec1   Michael S. Tsirkin   virtio_fs: conver...
664
665
  	virtio_cread_le(vdev, struct virtio_fs_config, num_request_queues,
  			&fs->num_request_queues);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
666
667
  	if (fs->num_request_queues == 0)
  		return -EINVAL;
b43b7e81e   Vivek Goyal   virtiofs: provide...
668
  	fs->nvqs = VQ_REQUEST + fs->num_request_queues;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
669
670
671
672
673
674
675
676
677
678
679
680
  	fs->vqs = kcalloc(fs->nvqs, sizeof(fs->vqs[VQ_HIPRIO]), GFP_KERNEL);
  	if (!fs->vqs)
  		return -ENOMEM;
  
  	vqs = kmalloc_array(fs->nvqs, sizeof(vqs[VQ_HIPRIO]), GFP_KERNEL);
  	callbacks = kmalloc_array(fs->nvqs, sizeof(callbacks[VQ_HIPRIO]),
  					GFP_KERNEL);
  	names = kmalloc_array(fs->nvqs, sizeof(names[VQ_HIPRIO]), GFP_KERNEL);
  	if (!vqs || !callbacks || !names) {
  		ret = -ENOMEM;
  		goto out;
  	}
b43b7e81e   Vivek Goyal   virtiofs: provide...
681
  	/* Initialize the hiprio/forget request virtqueue */
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
682
  	callbacks[VQ_HIPRIO] = virtio_fs_vq_done;
b43b7e81e   Vivek Goyal   virtiofs: provide...
683
  	virtio_fs_init_vq(&fs->vqs[VQ_HIPRIO], "hiprio", VQ_HIPRIO);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
684
  	names[VQ_HIPRIO] = fs->vqs[VQ_HIPRIO].name;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
685
686
687
  
  	/* Initialize the requests virtqueues */
  	for (i = VQ_REQUEST; i < fs->nvqs; i++) {
b43b7e81e   Vivek Goyal   virtiofs: provide...
688
689
690
691
  		char vq_name[VQ_NAME_LEN];
  
  		snprintf(vq_name, VQ_NAME_LEN, "requests.%u", i - VQ_REQUEST);
  		virtio_fs_init_vq(&fs->vqs[i], vq_name, VQ_REQUEST);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
  		callbacks[i] = virtio_fs_vq_done;
  		names[i] = fs->vqs[i].name;
  	}
  
  	ret = virtio_find_vqs(vdev, fs->nvqs, vqs, callbacks, names, NULL);
  	if (ret < 0)
  		goto out;
  
  	for (i = 0; i < fs->nvqs; i++)
  		fs->vqs[i].vq = vqs[i];
  
  	virtio_fs_start_all_queues(fs);
  out:
  	kfree(names);
  	kfree(callbacks);
  	kfree(vqs);
  	if (ret)
  		kfree(fs->vqs);
  	return ret;
  }
  
  /* Free virtqueues (device must already be reset) */
  static void virtio_fs_cleanup_vqs(struct virtio_device *vdev,
  				  struct virtio_fs *fs)
  {
  	vdev->config->del_vqs(vdev);
  }
22f3787e9   Stefan Hajnoczi   virtiofs: set up ...
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
  /* Map a window offset to a page frame number.  The window offset will have
   * been produced by .iomap_begin(), which maps a file offset to a window
   * offset.
   */
  static long virtio_fs_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
  				    long nr_pages, void **kaddr, pfn_t *pfn)
  {
  	struct virtio_fs *fs = dax_get_private(dax_dev);
  	phys_addr_t offset = PFN_PHYS(pgoff);
  	size_t max_nr_pages = fs->window_len/PAGE_SIZE - pgoff;
  
  	if (kaddr)
  		*kaddr = fs->window_kaddr + offset;
  	if (pfn)
  		*pfn = phys_to_pfn_t(fs->window_phys_addr + offset,
  					PFN_DEV | PFN_MAP);
  	return nr_pages > max_nr_pages ? max_nr_pages : nr_pages;
  }
  
  static size_t virtio_fs_copy_from_iter(struct dax_device *dax_dev,
  				       pgoff_t pgoff, void *addr,
  				       size_t bytes, struct iov_iter *i)
  {
  	return copy_from_iter(addr, bytes, i);
  }
  
  static size_t virtio_fs_copy_to_iter(struct dax_device *dax_dev,
  				       pgoff_t pgoff, void *addr,
  				       size_t bytes, struct iov_iter *i)
  {
  	return copy_to_iter(addr, bytes, i);
  }
  
  static int virtio_fs_zero_page_range(struct dax_device *dax_dev,
  				     pgoff_t pgoff, size_t nr_pages)
  {
  	long rc;
  	void *kaddr;
  
  	rc = dax_direct_access(dax_dev, pgoff, nr_pages, &kaddr, NULL);
  	if (rc < 0)
  		return rc;
  	memset(kaddr, 0, nr_pages << PAGE_SHIFT);
  	dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT);
  	return 0;
  }
  
  static const struct dax_operations virtio_fs_dax_ops = {
  	.direct_access = virtio_fs_direct_access,
  	.copy_from_iter = virtio_fs_copy_from_iter,
  	.copy_to_iter = virtio_fs_copy_to_iter,
  	.zero_page_range = virtio_fs_zero_page_range,
  };
  
  static void virtio_fs_cleanup_dax(void *data)
  {
  	struct dax_device *dax_dev = data;
  
  	kill_dax(dax_dev);
  	put_dax(dax_dev);
  }
  
  static int virtio_fs_setup_dax(struct virtio_device *vdev, struct virtio_fs *fs)
  {
  	struct virtio_shm_region cache_reg;
  	struct dev_pagemap *pgmap;
  	bool have_cache;
  
  	if (!IS_ENABLED(CONFIG_FUSE_DAX))
  		return 0;
  
  	/* Get cache region */
  	have_cache = virtio_get_shm_region(vdev, &cache_reg,
  					   (u8)VIRTIO_FS_SHMCAP_ID_CACHE);
  	if (!have_cache) {
  		dev_notice(&vdev->dev, "%s: No cache capability
  ", __func__);
  		return 0;
  	}
  
  	if (!devm_request_mem_region(&vdev->dev, cache_reg.addr, cache_reg.len,
  				     dev_name(&vdev->dev))) {
  		dev_warn(&vdev->dev, "could not reserve region addr=0x%llx len=0x%llx
  ",
  			 cache_reg.addr, cache_reg.len);
  		return -EBUSY;
  	}
  
  	dev_notice(&vdev->dev, "Cache len: 0x%llx @ 0x%llx
  ", cache_reg.len,
  		   cache_reg.addr);
  
  	pgmap = devm_kzalloc(&vdev->dev, sizeof(*pgmap), GFP_KERNEL);
  	if (!pgmap)
  		return -ENOMEM;
  
  	pgmap->type = MEMORY_DEVICE_FS_DAX;
  
  	/* Ideally we would directly use the PCI BAR resource but
  	 * devm_memremap_pages() wants its own copy in pgmap.  So
  	 * initialize a struct resource from scratch (only the start
  	 * and end fields will be used).
  	 */
694565356   Linus Torvalds   Merge tag 'fuse-u...
822
  	pgmap->range = (struct range) {
22f3787e9   Stefan Hajnoczi   virtiofs: set up ...
823
824
825
  		.start = (phys_addr_t) cache_reg.addr,
  		.end = (phys_addr_t) cache_reg.addr + cache_reg.len - 1,
  	};
694565356   Linus Torvalds   Merge tag 'fuse-u...
826
  	pgmap->nr_range = 1;
22f3787e9   Stefan Hajnoczi   virtiofs: set up ...
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
  
  	fs->window_kaddr = devm_memremap_pages(&vdev->dev, pgmap);
  	if (IS_ERR(fs->window_kaddr))
  		return PTR_ERR(fs->window_kaddr);
  
  	fs->window_phys_addr = (phys_addr_t) cache_reg.addr;
  	fs->window_len = (phys_addr_t) cache_reg.len;
  
  	dev_dbg(&vdev->dev, "%s: window kaddr 0x%px phys_addr 0x%llx len 0x%llx
  ",
  		__func__, fs->window_kaddr, cache_reg.addr, cache_reg.len);
  
  	fs->dax_dev = alloc_dax(fs, NULL, &virtio_fs_dax_ops, 0);
  	if (IS_ERR(fs->dax_dev))
  		return PTR_ERR(fs->dax_dev);
  
  	return devm_add_action_or_reset(&vdev->dev, virtio_fs_cleanup_dax,
  					fs->dax_dev);
  }
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
  static int virtio_fs_probe(struct virtio_device *vdev)
  {
  	struct virtio_fs *fs;
  	int ret;
  
  	fs = kzalloc(sizeof(*fs), GFP_KERNEL);
  	if (!fs)
  		return -ENOMEM;
  	kref_init(&fs->refcount);
  	vdev->priv = fs;
  
  	ret = virtio_fs_read_tag(vdev, fs);
  	if (ret < 0)
  		goto out;
  
  	ret = virtio_fs_setup_vqs(vdev, fs);
  	if (ret < 0)
  		goto out;
  
  	/* TODO vq affinity */
22f3787e9   Stefan Hajnoczi   virtiofs: set up ...
866
867
868
  	ret = virtio_fs_setup_dax(vdev, fs);
  	if (ret < 0)
  		goto out_vqs;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
  	/* Bring the device online in case the filesystem is mounted and
  	 * requests need to be sent before we return.
  	 */
  	virtio_device_ready(vdev);
  
  	ret = virtio_fs_add_instance(fs);
  	if (ret < 0)
  		goto out_vqs;
  
  	return 0;
  
  out_vqs:
  	vdev->config->reset(vdev);
  	virtio_fs_cleanup_vqs(vdev, fs);
  
  out:
  	vdev->priv = NULL;
  	kfree(fs);
  	return ret;
  }
  
  static void virtio_fs_stop_all_queues(struct virtio_fs *fs)
  {
  	struct virtio_fs_vq *fsvq;
  	int i;
  
  	for (i = 0; i < fs->nvqs; i++) {
  		fsvq = &fs->vqs[i];
  		spin_lock(&fsvq->lock);
  		fsvq->connected = false;
  		spin_unlock(&fsvq->lock);
  	}
  }
  
  static void virtio_fs_remove(struct virtio_device *vdev)
  {
  	struct virtio_fs *fs = vdev->priv;
  
  	mutex_lock(&virtio_fs_mutex);
  	/* This device is going away. No one should get new reference */
  	list_del_init(&fs->list);
  	virtio_fs_stop_all_queues(fs);
724c15a43   Vivek Goyal   virtiofs: Use com...
911
  	virtio_fs_drain_all_queues_locked(fs);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
  	vdev->config->reset(vdev);
  	virtio_fs_cleanup_vqs(vdev, fs);
  
  	vdev->priv = NULL;
  	/* Put device reference on virtio_fs object */
  	virtio_fs_put(fs);
  	mutex_unlock(&virtio_fs_mutex);
  }
  
  #ifdef CONFIG_PM_SLEEP
  static int virtio_fs_freeze(struct virtio_device *vdev)
  {
  	/* TODO need to save state here */
  	pr_warn("virtio-fs: suspend/resume not yet supported
  ");
  	return -EOPNOTSUPP;
  }
  
  static int virtio_fs_restore(struct virtio_device *vdev)
  {
  	 /* TODO need to restore state here */
  	return 0;
  }
  #endif /* CONFIG_PM_SLEEP */
00929447f   YueHaibing   virtiofs: Fix old...
936
  static const struct virtio_device_id id_table[] = {
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
937
938
939
  	{ VIRTIO_ID_FS, VIRTIO_DEV_ANY_ID },
  	{},
  };
00929447f   YueHaibing   virtiofs: Fix old...
940
  static const unsigned int feature_table[] = {};
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
  
  static struct virtio_driver virtio_fs_driver = {
  	.driver.name		= KBUILD_MODNAME,
  	.driver.owner		= THIS_MODULE,
  	.id_table		= id_table,
  	.feature_table		= feature_table,
  	.feature_table_size	= ARRAY_SIZE(feature_table),
  	.probe			= virtio_fs_probe,
  	.remove			= virtio_fs_remove,
  #ifdef CONFIG_PM_SLEEP
  	.freeze			= virtio_fs_freeze,
  	.restore		= virtio_fs_restore,
  #endif
  };
  
  static void virtio_fs_wake_forget_and_unlock(struct fuse_iqueue *fiq)
  __releases(fiq->lock)
  {
  	struct fuse_forget_link *link;
  	struct virtio_fs_forget *forget;
1efcf39eb   Vivek Goyal   virtiofs: Do not ...
961
  	struct virtio_fs_forget_req *req;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
962
  	struct virtio_fs *fs;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
963
  	struct virtio_fs_vq *fsvq;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
964
  	u64 unique;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
965
966
967
968
969
970
971
972
973
974
  
  	link = fuse_dequeue_forget(fiq, 1, NULL);
  	unique = fuse_get_unique(fiq);
  
  	fs = fiq->priv;
  	fsvq = &fs->vqs[VQ_HIPRIO];
  	spin_unlock(&fiq->lock);
  
  	/* Allocate a buffer for the request */
  	forget = kmalloc(sizeof(*forget), GFP_NOFS | __GFP_NOFAIL);
1efcf39eb   Vivek Goyal   virtiofs: Do not ...
975
  	req = &forget->req;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
976

1efcf39eb   Vivek Goyal   virtiofs: Do not ...
977
  	req->ih = (struct fuse_in_header){
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
978
979
980
  		.opcode = FUSE_FORGET,
  		.nodeid = link->forget_one.nodeid,
  		.unique = unique,
1efcf39eb   Vivek Goyal   virtiofs: Do not ...
981
  		.len = sizeof(*req),
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
982
  	};
1efcf39eb   Vivek Goyal   virtiofs: Do not ...
983
  	req->arg = (struct fuse_forget_in){
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
984
985
  		.nlookup = link->forget_one.nlookup,
  	};
58ada94f9   Vivek Goyal   virtiofs: Use a c...
986
  	send_forget_request(fsvq, forget, false);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
  	kfree(link);
  }
  
  static void virtio_fs_wake_interrupt_and_unlock(struct fuse_iqueue *fiq)
  __releases(fiq->lock)
  {
  	/*
  	 * TODO interrupts.
  	 *
  	 * Normal fs operations on a local filesystems aren't interruptible.
  	 * Exceptions are blocking lock operations; for example fcntl(F_SETLKW)
  	 * with shared lock between host and guest.
  	 */
  	spin_unlock(&fiq->lock);
  }
42d3e2d04   Vivek Goyal   virtiofs: calcula...
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
  /* Count number of scatter-gather elements required */
  static unsigned int sg_count_fuse_pages(struct fuse_page_desc *page_descs,
  				       unsigned int num_pages,
  				       unsigned int total_len)
  {
  	unsigned int i;
  	unsigned int this_len;
  
  	for (i = 0; i < num_pages && total_len; i++) {
  		this_len =  min(page_descs[i].length, total_len);
  		total_len -= this_len;
  	}
  
  	return i;
  }
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1017
1018
1019
1020
1021
  /* Return the number of scatter-gather list elements required */
  static unsigned int sg_count_fuse_req(struct fuse_req *req)
  {
  	struct fuse_args *args = req->args;
  	struct fuse_args_pages *ap = container_of(args, typeof(*ap), args);
42d3e2d04   Vivek Goyal   virtiofs: calcula...
1022
  	unsigned int size, total_sgs = 1 /* fuse_in_header */;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1023
1024
1025
  
  	if (args->in_numargs - args->in_pages)
  		total_sgs += 1;
42d3e2d04   Vivek Goyal   virtiofs: calcula...
1026
1027
1028
1029
1030
  	if (args->in_pages) {
  		size = args->in_args[args->in_numargs - 1].size;
  		total_sgs += sg_count_fuse_pages(ap->descs, ap->num_pages,
  						 size);
  	}
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1031
1032
1033
1034
1035
1036
1037
1038
  
  	if (!test_bit(FR_ISREPLY, &req->flags))
  		return total_sgs;
  
  	total_sgs += 1 /* fuse_out_header */;
  
  	if (args->out_numargs - args->out_pages)
  		total_sgs += 1;
42d3e2d04   Vivek Goyal   virtiofs: calcula...
1039
1040
1041
1042
1043
  	if (args->out_pages) {
  		size = args->out_args[args->out_numargs - 1].size;
  		total_sgs += sg_count_fuse_pages(ap->descs, ap->num_pages,
  						 size);
  	}
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
  
  	return total_sgs;
  }
  
  /* Add pages to scatter-gather list and return number of elements used */
  static unsigned int sg_init_fuse_pages(struct scatterlist *sg,
  				       struct page **pages,
  				       struct fuse_page_desc *page_descs,
  				       unsigned int num_pages,
  				       unsigned int total_len)
  {
  	unsigned int i;
  	unsigned int this_len;
  
  	for (i = 0; i < num_pages && total_len; i++) {
  		sg_init_table(&sg[i], 1);
  		this_len =  min(page_descs[i].length, total_len);
  		sg_set_page(&sg[i], pages[i], this_len, page_descs[i].offset);
  		total_len -= this_len;
  	}
  
  	return i;
  }
  
  /* Add args to scatter-gather list and return number of elements used */
  static unsigned int sg_init_fuse_args(struct scatterlist *sg,
  				      struct fuse_req *req,
  				      struct fuse_arg *args,
  				      unsigned int numargs,
  				      bool argpages,
  				      void *argbuf,
  				      unsigned int *len_used)
  {
  	struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args);
  	unsigned int total_sgs = 0;
  	unsigned int len;
  
  	len = fuse_len_args(numargs - argpages, args);
  	if (len)
  		sg_init_one(&sg[total_sgs++], argbuf, len);
  
  	if (argpages)
  		total_sgs += sg_init_fuse_pages(&sg[total_sgs],
  						ap->pages, ap->descs,
  						ap->num_pages,
  						args[numargs - 1].size);
  
  	if (len_used)
  		*len_used = len;
  
  	return total_sgs;
  }
  
  /* Add a request to a virtqueue and kick the device */
  static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
a9bfd9dd3   Vivek Goyal   virtiofs: Retry r...
1099
  				 struct fuse_req *req, bool in_flight)
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
  {
  	/* requests need at least 4 elements */
  	struct scatterlist *stack_sgs[6];
  	struct scatterlist stack_sg[ARRAY_SIZE(stack_sgs)];
  	struct scatterlist **sgs = stack_sgs;
  	struct scatterlist *sg = stack_sg;
  	struct virtqueue *vq;
  	struct fuse_args *args = req->args;
  	unsigned int argbuf_used = 0;
  	unsigned int out_sgs = 0;
  	unsigned int in_sgs = 0;
  	unsigned int total_sgs;
  	unsigned int i;
  	int ret;
  	bool notify;
5dbe190f3   Vivek Goyal   virtiofs: Set FR_...
1115
  	struct fuse_pqueue *fpq;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
  
  	/* Does the sglist fit on the stack? */
  	total_sgs = sg_count_fuse_req(req);
  	if (total_sgs > ARRAY_SIZE(stack_sgs)) {
  		sgs = kmalloc_array(total_sgs, sizeof(sgs[0]), GFP_ATOMIC);
  		sg = kmalloc_array(total_sgs, sizeof(sg[0]), GFP_ATOMIC);
  		if (!sgs || !sg) {
  			ret = -ENOMEM;
  			goto out;
  		}
  	}
  
  	/* Use a bounce buffer since stack args cannot be mapped */
  	ret = copy_args_to_argbuf(req);
  	if (ret < 0)
  		goto out;
  
  	/* Request elements */
  	sg_init_one(&sg[out_sgs++], &req->in.h, sizeof(req->in.h));
  	out_sgs += sg_init_fuse_args(&sg[out_sgs], req,
  				     (struct fuse_arg *)args->in_args,
  				     args->in_numargs, args->in_pages,
  				     req->argbuf, &argbuf_used);
  
  	/* Reply elements */
  	if (test_bit(FR_ISREPLY, &req->flags)) {
  		sg_init_one(&sg[out_sgs + in_sgs++],
  			    &req->out.h, sizeof(req->out.h));
  		in_sgs += sg_init_fuse_args(&sg[out_sgs + in_sgs], req,
  					    args->out_args, args->out_numargs,
  					    args->out_pages,
  					    req->argbuf + argbuf_used, NULL);
  	}
  
  	WARN_ON(out_sgs + in_sgs != total_sgs);
  
  	for (i = 0; i < total_sgs; i++)
  		sgs[i] = &sg[i];
  
  	spin_lock(&fsvq->lock);
  
  	if (!fsvq->connected) {
  		spin_unlock(&fsvq->lock);
  		ret = -ENOTCONN;
  		goto out;
  	}
  
  	vq = fsvq->vq;
  	ret = virtqueue_add_sgs(vq, sgs, out_sgs, in_sgs, req, GFP_ATOMIC);
  	if (ret < 0) {
  		spin_unlock(&fsvq->lock);
  		goto out;
  	}
5dbe190f3   Vivek Goyal   virtiofs: Set FR_...
1169
1170
1171
1172
1173
1174
1175
1176
  	/* Request successfully sent. */
  	fpq = &fsvq->fud->pq;
  	spin_lock(&fpq->lock);
  	list_add_tail(&req->list, fpq->processing);
  	spin_unlock(&fpq->lock);
  	set_bit(FR_SENT, &req->flags);
  	/* matches barrier in request_wait_answer() */
  	smp_mb__after_atomic();
a9bfd9dd3   Vivek Goyal   virtiofs: Retry r...
1177
1178
  	if (!in_flight)
  		inc_in_flight_req(fsvq);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
  	notify = virtqueue_kick_prepare(vq);
  
  	spin_unlock(&fsvq->lock);
  
  	if (notify)
  		virtqueue_notify(vq);
  
  out:
  	if (ret < 0 && req->argbuf) {
  		kfree(req->argbuf);
  		req->argbuf = NULL;
  	}
  	if (sgs != stack_sgs) {
  		kfree(sgs);
  		kfree(sg);
  	}
  
  	return ret;
  }
  
  static void virtio_fs_wake_pending_and_unlock(struct fuse_iqueue *fiq)
  __releases(fiq->lock)
  {
  	unsigned int queue_id = VQ_REQUEST; /* TODO multiqueue */
  	struct virtio_fs *fs;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1204
  	struct fuse_req *req;
51fecdd25   Vivek Goyal   virtiofs: Do not ...
1205
  	struct virtio_fs_vq *fsvq;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
  	int ret;
  
  	WARN_ON(list_empty(&fiq->pending));
  	req = list_last_entry(&fiq->pending, struct fuse_req, list);
  	clear_bit(FR_PENDING, &req->flags);
  	list_del_init(&req->list);
  	WARN_ON(!list_empty(&fiq->pending));
  	spin_unlock(&fiq->lock);
  
  	fs = fiq->priv;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1216
1217
1218
1219
1220
1221
  
  	pr_debug("%s: opcode %u unique %#llx nodeid %#llx in.len %u out.len %u
  ",
  		  __func__, req->in.h.opcode, req->in.h.unique,
  		 req->in.h.nodeid, req->in.h.len,
  		 fuse_len_args(req->args->out_numargs, req->args->out_args));
51fecdd25   Vivek Goyal   virtiofs: Do not ...
1222
  	fsvq = &fs->vqs[queue_id];
a9bfd9dd3   Vivek Goyal   virtiofs: Retry r...
1223
  	ret = virtio_fs_enqueue_req(fsvq, req, false);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1224
1225
  	if (ret < 0) {
  		if (ret == -ENOMEM || ret == -ENOSPC) {
a9bfd9dd3   Vivek Goyal   virtiofs: Retry r...
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
  			/*
  			 * Virtqueue full. Retry submission from worker
  			 * context as we might be holding fc->bg_lock.
  			 */
  			spin_lock(&fsvq->lock);
  			list_add_tail(&req->list, &fsvq->queued_reqs);
  			inc_in_flight_req(fsvq);
  			schedule_delayed_work(&fsvq->dispatch_work,
  						msecs_to_jiffies(1));
  			spin_unlock(&fsvq->lock);
  			return;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1237
1238
1239
1240
  		}
  		req->out.h.error = ret;
  		pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d
  ", ret);
51fecdd25   Vivek Goyal   virtiofs: Do not ...
1241
1242
1243
1244
1245
1246
  
  		/* Can't end request in submission context. Use a worker */
  		spin_lock(&fsvq->lock);
  		list_add_tail(&req->list, &fsvq->end_reqs);
  		schedule_delayed_work(&fsvq->dispatch_work, 0);
  		spin_unlock(&fsvq->lock);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1247
1248
1249
  		return;
  	}
  }
00929447f   YueHaibing   virtiofs: Fix old...
1250
  static const struct fuse_iqueue_ops virtio_fs_fiq_ops = {
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1251
1252
1253
1254
1255
  	.wake_forget_and_unlock		= virtio_fs_wake_forget_and_unlock,
  	.wake_interrupt_and_unlock	= virtio_fs_wake_interrupt_and_unlock,
  	.wake_pending_and_unlock	= virtio_fs_wake_pending_and_unlock,
  	.release			= virtio_fs_fiq_release,
  };
1dd539577   Vivek Goyal   virtiofs: add a m...
1256
  static inline void virtio_fs_ctx_set_defaults(struct fuse_fs_context *ctx)
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1257
  {
1dd539577   Vivek Goyal   virtiofs: add a m...
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
  	ctx->rootmode = S_IFDIR;
  	ctx->default_permissions = 1;
  	ctx->allow_other = 1;
  	ctx->max_read = UINT_MAX;
  	ctx->blksize = 512;
  	ctx->destroy = true;
  	ctx->no_control = true;
  	ctx->no_force_umount = true;
  }
  
  static int virtio_fs_fill_super(struct super_block *sb, struct fs_context *fsc)
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1269
  {
fcee216be   Max Reitz   fuse: split fuse_...
1270
1271
  	struct fuse_mount *fm = get_fuse_mount_super(sb);
  	struct fuse_conn *fc = fm->fc;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1272
  	struct virtio_fs *fs = fc->iq.priv;
1dd539577   Vivek Goyal   virtiofs: add a m...
1273
  	struct fuse_fs_context *ctx = fsc->fs_private;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1274
1275
  	unsigned int i;
  	int err;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1276

1dd539577   Vivek Goyal   virtiofs: add a m...
1277
  	virtio_fs_ctx_set_defaults(ctx);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
  	mutex_lock(&virtio_fs_mutex);
  
  	/* After holding mutex, make sure virtiofs device is still there.
  	 * Though we are holding a reference to it, drive ->remove might
  	 * still have cleaned up virtual queues. In that case bail out.
  	 */
  	err = -EINVAL;
  	if (list_empty(&fs->list)) {
  		pr_info("virtio-fs: tag <%s> not found
  ", fs->tag);
  		goto err;
  	}
  
  	err = -ENOMEM;
  	/* Allocate fuse_dev for hiprio and notification queues */
7fd3abfa8   Vivek Goyal   virtiofs: do not ...
1293
  	for (i = 0; i < fs->nvqs; i++) {
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1294
1295
1296
1297
1298
1299
  		struct virtio_fs_vq *fsvq = &fs->vqs[i];
  
  		fsvq->fud = fuse_dev_alloc();
  		if (!fsvq->fud)
  			goto err_free_fuse_devs;
  	}
7fd3abfa8   Vivek Goyal   virtiofs: do not ...
1300
  	/* virtiofs allocates and installs its own fuse devices */
1dd539577   Vivek Goyal   virtiofs: add a m...
1301
1302
1303
1304
  	ctx->fudptr = NULL;
  	if (ctx->dax)
  		ctx->dax_dev = fs->dax_dev;
  	err = fuse_fill_super_common(sb, ctx);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1305
1306
  	if (err < 0)
  		goto err_free_fuse_devs;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1307
1308
  	for (i = 0; i < fs->nvqs; i++) {
  		struct virtio_fs_vq *fsvq = &fs->vqs[i];
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1309
1310
1311
1312
1313
  		fuse_dev_install(fsvq->fud, fc);
  	}
  
  	/* Previous unmount will stop all queues. Start these again */
  	virtio_fs_start_all_queues(fs);
fcee216be   Max Reitz   fuse: split fuse_...
1314
  	fuse_send_init(fm);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1315
1316
1317
1318
1319
1320
1321
1322
1323
  	mutex_unlock(&virtio_fs_mutex);
  	return 0;
  
  err_free_fuse_devs:
  	virtio_fs_free_devs(fs);
  err:
  	mutex_unlock(&virtio_fs_mutex);
  	return err;
  }
fcee216be   Max Reitz   fuse: split fuse_...
1324
  static void virtio_fs_conn_destroy(struct fuse_mount *fm)
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1325
  {
fcee216be   Max Reitz   fuse: split fuse_...
1326
1327
1328
  	struct fuse_conn *fc = fm->fc;
  	struct virtio_fs *vfs = fc->iq.priv;
  	struct virtio_fs_vq *fsvq = &vfs->vqs[VQ_HIPRIO];
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1329

fcee216be   Max Reitz   fuse: split fuse_...
1330
1331
  	/* Stop dax worker. Soon evict_inodes() will be called which
  	 * will free all memory ranges belonging to all inodes.
9a752d18c   Vivek Goyal   virtiofs: add log...
1332
1333
1334
  	 */
  	if (IS_ENABLED(CONFIG_FUSE_DAX))
  		fuse_dax_cancel_work(fc);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1335
1336
1337
1338
1339
1340
  
  	/* Stop forget queue. Soon destroy will be sent */
  	spin_lock(&fsvq->lock);
  	fsvq->connected = false;
  	spin_unlock(&fsvq->lock);
  	virtio_fs_drain_all_queues(vfs);
fcee216be   Max Reitz   fuse: split fuse_...
1341
  	fuse_conn_destroy(fm);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1342

fcee216be   Max Reitz   fuse: split fuse_...
1343
  	/* fuse_conn_destroy() must have sent destroy. Stop all queues
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1344
1345
1346
1347
1348
1349
1350
1351
  	 * and drain one more time and free fuse devices. Freeing fuse
  	 * devices will drop their reference on fuse_conn and that in
  	 * turn will drop its reference on virtio_fs object.
  	 */
  	virtio_fs_stop_all_queues(vfs);
  	virtio_fs_drain_all_queues(vfs);
  	virtio_fs_free_devs(vfs);
  }
fcee216be   Max Reitz   fuse: split fuse_...
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
  static void virtio_kill_sb(struct super_block *sb)
  {
  	struct fuse_mount *fm = get_fuse_mount_super(sb);
  	bool last;
  
  	/* If mount failed, we can still be called without any fc */
  	if (fm) {
  		last = fuse_mount_remove(fm);
  		if (last)
  			virtio_fs_conn_destroy(fm);
  	}
  	kill_anon_super(sb);
  }
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1365
1366
1367
  static int virtio_fs_test_super(struct super_block *sb,
  				struct fs_context *fsc)
  {
fcee216be   Max Reitz   fuse: split fuse_...
1368
1369
  	struct fuse_mount *fsc_fm = fsc->s_fs_info;
  	struct fuse_mount *sb_fm = get_fuse_mount_super(sb);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1370

fcee216be   Max Reitz   fuse: split fuse_...
1371
  	return fsc_fm->fc->iq.priv == sb_fm->fc->iq.priv;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1372
1373
1374
1375
1376
1377
1378
1379
1380
  }
  
  static int virtio_fs_set_super(struct super_block *sb,
  			       struct fs_context *fsc)
  {
  	int err;
  
  	err = get_anon_bdev(&sb->s_dev);
  	if (!err)
fcee216be   Max Reitz   fuse: split fuse_...
1381
  		fuse_mount_get(fsc->s_fs_info);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1382
1383
1384
1385
1386
1387
1388
1389
1390
  
  	return err;
  }
  
  static int virtio_fs_get_tree(struct fs_context *fsc)
  {
  	struct virtio_fs *fs;
  	struct super_block *sb;
  	struct fuse_conn *fc;
fcee216be   Max Reitz   fuse: split fuse_...
1391
  	struct fuse_mount *fm;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
  	int err;
  
  	/* This gets a reference on virtio_fs object. This ptr gets installed
  	 * in fc->iq->priv. Once fuse_conn is going away, it calls ->put()
  	 * to drop the reference to this object.
  	 */
  	fs = virtio_fs_find_instance(fsc->source);
  	if (!fs) {
  		pr_info("virtio-fs: tag <%s> not found
  ", fsc->source);
  		return -EINVAL;
  	}
  
  	fc = kzalloc(sizeof(struct fuse_conn), GFP_KERNEL);
  	if (!fc) {
  		mutex_lock(&virtio_fs_mutex);
  		virtio_fs_put(fs);
  		mutex_unlock(&virtio_fs_mutex);
  		return -ENOMEM;
  	}
fcee216be   Max Reitz   fuse: split fuse_...
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
  	fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL);
  	if (!fm) {
  		mutex_lock(&virtio_fs_mutex);
  		virtio_fs_put(fs);
  		mutex_unlock(&virtio_fs_mutex);
  		kfree(fc);
  		return -ENOMEM;
  	}
  
  	fuse_conn_init(fc, fm, get_user_ns(current_user_ns()),
  		       &virtio_fs_fiq_ops, fs);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1423
1424
  	fc->release = fuse_free_conn;
  	fc->delete_stale = true;
bf109c640   Max Reitz   fuse: implement c...
1425
  	fc->auto_submounts = true;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1426

fcee216be   Max Reitz   fuse: split fuse_...
1427
  	fsc->s_fs_info = fm;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1428
  	sb = sget_fc(fsc, virtio_fs_test_super, virtio_fs_set_super);
fcee216be   Max Reitz   fuse: split fuse_...
1429
  	fuse_mount_put(fm);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1430
1431
1432
1433
  	if (IS_ERR(sb))
  		return PTR_ERR(sb);
  
  	if (!sb->s_root) {
1dd539577   Vivek Goyal   virtiofs: add a m...
1434
  		err = virtio_fs_fill_super(sb, fsc);
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1435
  		if (err) {
941327d40   Miklos Szeredi   virtiofs fix leak...
1436
1437
  			fuse_mount_put(fm);
  			sb->s_fs_info = NULL;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
  			deactivate_locked_super(sb);
  			return err;
  		}
  
  		sb->s_flags |= SB_ACTIVE;
  	}
  
  	WARN_ON(fsc->root);
  	fsc->root = dget(sb->s_root);
  	return 0;
  }
  
  static const struct fs_context_operations virtio_fs_context_ops = {
1dd539577   Vivek Goyal   virtiofs: add a m...
1451
1452
  	.free		= virtio_fs_free_fc,
  	.parse_param	= virtio_fs_parse_param,
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1453
1454
1455
1456
1457
  	.get_tree	= virtio_fs_get_tree,
  };
  
  static int virtio_fs_init_fs_context(struct fs_context *fsc)
  {
1dd539577   Vivek Goyal   virtiofs: add a m...
1458
1459
1460
1461
1462
1463
  	struct fuse_fs_context *ctx;
  
  	ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
  	if (!ctx)
  		return -ENOMEM;
  	fsc->fs_private = ctx;
a62a8ef9d   Stefan Hajnoczi   virtio-fs: add vi...
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
  	fsc->ops = &virtio_fs_context_ops;
  	return 0;
  }
  
  static struct file_system_type virtio_fs_type = {
  	.owner		= THIS_MODULE,
  	.name		= "virtiofs",
  	.init_fs_context = virtio_fs_init_fs_context,
  	.kill_sb	= virtio_kill_sb,
  };
  
  static int __init virtio_fs_init(void)
  {
  	int ret;
  
  	ret = register_virtio_driver(&virtio_fs_driver);
  	if (ret < 0)
  		return ret;
  
  	ret = register_filesystem(&virtio_fs_type);
  	if (ret < 0) {
  		unregister_virtio_driver(&virtio_fs_driver);
  		return ret;
  	}
  
  	return 0;
  }
  module_init(virtio_fs_init);
  
  static void __exit virtio_fs_exit(void)
  {
  	unregister_filesystem(&virtio_fs_type);
  	unregister_virtio_driver(&virtio_fs_driver);
  }
  module_exit(virtio_fs_exit);
  
  MODULE_AUTHOR("Stefan Hajnoczi <stefanha@redhat.com>");
  MODULE_DESCRIPTION("Virtio Filesystem");
  MODULE_LICENSE("GPL");
  MODULE_ALIAS_FS(KBUILD_MODNAME);
  MODULE_DEVICE_TABLE(virtio, id_table);