This file is indexed.

/usr/src/blktap-2.0.93/request.c is in blktap-dkms 2.0.93-0ubuntu4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
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
443
444
445
446
447
448
449
450
451
452
/*
 *
 * Copyright (C) 2011 Citrix Systems Inc.
 *
 * This file is part of Blktap2.
 *
 * Blktap2 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * Blktap2 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License version 2 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with Blktap2.  If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 *
 */

#include <linux/mempool.h>
#include <linux/spinlock.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/device.h>
#include <linux/slab.h>

#include "blktap.h"

/* max pages per shared pool. just to prevent accidental dos. */
#define POOL_MAX_PAGES           (256*BLKTAP_SEGMENT_MAX)

/* default page pool size. when considering to shrink a shared pool,
 * note that paused tapdisks may grab a whole lot of pages for a long
 * time. */
#define POOL_DEFAULT_PAGES       (2 * BLKTAP_RING_SIZE * BLKTAP_SEGMENT_MAX)

/* max number of pages allocatable per request. */
#define POOL_MAX_REQUEST_PAGES   BLKTAP_SEGMENT_MAX

/* min request structs per pool. These grow dynamically. */
#define POOL_MIN_REQS            BLKTAP_RING_SIZE

static struct kset *pool_set;

#define kobj_to_pool(_kobj) \
	container_of(_kobj, struct blktap_page_pool, kobj)

static struct kmem_cache *request_cache;
static mempool_t *request_pool;

static void
__page_pool_wake(struct blktap_page_pool *pool)
{
	mempool_t *mem = pool->bufs;

	/*
	  NB. slightly wasteful to always wait for a full segment
	  set. but this ensures the next disk makes
	  progress. presently, the repeated request struct
	  alloc/release cycles would otherwise keep everyone spinning.
	*/

	if (mem->curr_nr >= POOL_MAX_REQUEST_PAGES)
		wake_up(&pool->wait);
}

int
blktap_request_get_pages(struct blktap *tap,
			 struct blktap_request *request, int nr_pages)
{
	struct blktap_page_pool *pool = tap->pool;
	mempool_t *mem = pool->bufs;
	struct page *page;

	BUG_ON(request->nr_pages != 0);
	BUG_ON(nr_pages > POOL_MAX_REQUEST_PAGES);

	if (mem->curr_nr < nr_pages)
		return -ENOMEM;

	/* NB. avoid thundering herds of tapdisks colliding. */
	spin_lock(&pool->lock);

	if (mem->curr_nr < nr_pages) {
		spin_unlock(&pool->lock);
		return -ENOMEM;
	}

	while (request->nr_pages < nr_pages) {
		page = mempool_alloc(mem, GFP_NOWAIT);
		BUG_ON(!page);
		request->pages[request->nr_pages++] = page;
	}

	spin_unlock(&pool->lock);

	return 0;
}

static void
blktap_request_put_pages(struct blktap *tap,
			 struct blktap_request *request)
{
	struct blktap_page_pool *pool = tap->pool;
	struct page *page;

	while (request->nr_pages) {
		page = request->pages[--request->nr_pages];
		mempool_free(page, pool->bufs);
	}
}

size_t
blktap_request_debug(struct blktap *tap, char *buf, size_t size)
{
	struct blktap_page_pool *pool = tap->pool;
	mempool_t *mem = pool->bufs;
	char *s = buf, *end = buf + size;

	s += snprintf(buf, end - s,
		      "pool:%s pages:%d free:%d\n",
		      kobject_name(&pool->kobj),
		      mem->min_nr, mem->curr_nr);

	return s - buf;
}

struct blktap_request*
blktap_request_alloc(struct blktap *tap)
{
	struct blktap_request *request;

	request = mempool_alloc(request_pool, GFP_NOWAIT);
	if (request)
		request->tap = tap;

	return request;
}

void
blktap_request_free(struct blktap *tap,
		    struct blktap_request *request)
{
	blktap_request_put_pages(tap, request);

	mempool_free(request, request_pool);

	__page_pool_wake(tap->pool);
}

static void
blktap_request_bounce_seg(struct blktap *tap,
			  struct blktap_request *request,
			  int seg, int write)
{
	struct scatterlist *sg = &request->sg_table[seg];
	void *s, *p;

	BUG_ON(seg >= request->nr_pages);

	s = sg_virt(sg);
	p = page_address(request->pages[seg]) + sg->offset;

	if (write)
		memcpy(p, s, sg->length);
	else
		memcpy(s, p, sg->length);
}

void
blktap_request_bounce(struct blktap *tap,
		      struct blktap_request *request,
		      int write)
{
	int seg;

	for (seg = 0; seg < request->nr_pages; seg++)
		blktap_request_bounce_seg(tap, request, seg, write);
}

static void
blktap_request_ctor(void *obj)
{
	struct blktap_request *request = obj;

	memset(request, 0, sizeof(*request));
	sg_init_table(request->sg_table, ARRAY_SIZE(request->sg_table));
}

static int
blktap_page_pool_resize(struct blktap_page_pool *pool, int target)
{
	mempool_t *bufs = pool->bufs;
	int err;

	/* NB. mempool asserts min_nr >= 1 */
	target = max(1, target);

	err = mempool_resize(bufs, target, GFP_KERNEL);
	if (err)
		return err;

	__page_pool_wake(pool);

	return 0;
}

struct pool_attribute {
	struct attribute attr;

	ssize_t (*show)(struct blktap_page_pool *pool,
			char *buf);

	ssize_t (*store)(struct blktap_page_pool *pool,
			 const char *buf, size_t count);
};

#define kattr_to_pool_attr(_kattr) \
	container_of(_kattr, struct pool_attribute, attr)

static ssize_t
blktap_page_pool_show_size(struct blktap_page_pool *pool,
			   char *buf)
{
	mempool_t *mem = pool->bufs;
	return sprintf(buf, "%d", mem->min_nr);
}

static ssize_t
blktap_page_pool_store_size(struct blktap_page_pool *pool,
			    const char *buf, size_t size)
{
	int target;

	/*
	 * NB. target fixup to avoid undesired results. less than a
	 * full segment set can wedge the disk. much more than a
	 * couple times the physical queue depth is rarely useful.
	 */

	target = simple_strtoul(buf, NULL, 0);
	target = max(POOL_MAX_REQUEST_PAGES, target);
	target = min(target, POOL_MAX_PAGES);

	return blktap_page_pool_resize(pool, target) ? : size;
}

static struct pool_attribute blktap_page_pool_attr_size =
	__ATTR(size, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
	       blktap_page_pool_show_size,
	       blktap_page_pool_store_size);

static ssize_t
blktap_page_pool_show_free(struct blktap_page_pool *pool,
			   char *buf)
{
	mempool_t *mem = pool->bufs;
	return sprintf(buf, "%d", mem->curr_nr);
}

static struct pool_attribute blktap_page_pool_attr_free =
	__ATTR(free, S_IRUSR|S_IRGRP|S_IROTH,
	       blktap_page_pool_show_free,
	       NULL);

static struct attribute *blktap_page_pool_attrs[] = {
	&blktap_page_pool_attr_size.attr,
	&blktap_page_pool_attr_free.attr,
	NULL,
};

static inline struct kobject*
__blktap_kset_find_obj(struct kset *kset, const char *name)
{
	struct kobject *k;
	struct kobject *ret = NULL;

	spin_lock(&kset->list_lock);
	list_for_each_entry(k, &kset->list, entry) {
		if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
			ret = kobject_get(k);
			break;
		}
	}
	spin_unlock(&kset->list_lock);
	return ret;
}

static ssize_t
blktap_page_pool_show_attr(struct kobject *kobj, struct attribute *kattr,
			   char *buf)
{
	struct blktap_page_pool *pool = kobj_to_pool(kobj);
	struct pool_attribute *attr = kattr_to_pool_attr(kattr);

	if (attr->show)
		return attr->show(pool, buf);

	return -EIO;
}

static ssize_t
blktap_page_pool_store_attr(struct kobject *kobj, struct attribute *kattr,
			    const char *buf, size_t size)
{
	struct blktap_page_pool *pool = kobj_to_pool(kobj);
	struct pool_attribute *attr = kattr_to_pool_attr(kattr);

	if (attr->show)
		return attr->store(pool, buf, size);

	return -EIO;
}

static struct sysfs_ops blktap_page_pool_sysfs_ops = {
	.show		= blktap_page_pool_show_attr,
	.store		= blktap_page_pool_store_attr,
};

static void
blktap_page_pool_release(struct kobject *kobj)
{
	struct blktap_page_pool *pool = kobj_to_pool(kobj);
	mempool_destroy(pool->bufs);
	kfree(pool);
}

struct kobj_type blktap_page_pool_ktype = {
	.release       = blktap_page_pool_release,
	.sysfs_ops     = &blktap_page_pool_sysfs_ops,
	.default_attrs = blktap_page_pool_attrs,
};

static void*
__mempool_page_alloc(gfp_t gfp_mask, void *pool_data)
{
	struct page *page;

	if (!(gfp_mask & __GFP_WAIT))
		return NULL;

	page = alloc_page(gfp_mask);
	if (page)
		SetPageReserved(page);

	return page;
}

static void
__mempool_page_free(void *element, void *pool_data)
{
	struct page *page = element;

	ClearPageReserved(page);
	put_page(page);
}

static struct kobject*
blktap_page_pool_create(const char *name, int nr_pages)
{
	struct blktap_page_pool *pool;
	int err;

	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
	if (!pool)
		goto fail;

	spin_lock_init(&pool->lock);
	init_waitqueue_head(&pool->wait);

	pool->bufs = mempool_create(nr_pages,
				    __mempool_page_alloc, __mempool_page_free,
				    pool);
	if (!pool->bufs)
		goto fail_pool;

	kobject_init(&pool->kobj, &blktap_page_pool_ktype);
	pool->kobj.kset = pool_set;
	err = kobject_add(&pool->kobj, &pool_set->kobj, "%s", name);
	if (err)
		goto fail_bufs;

	return &pool->kobj;

	kobject_del(&pool->kobj);
fail_bufs:
	mempool_destroy(pool->bufs);
fail_pool:
	kfree(pool);
fail:
	return NULL;
}

struct blktap_page_pool*
blktap_page_pool_get(const char *name)
{
	struct kobject *kobj;

	kobj = __blktap_kset_find_obj(pool_set, name);
	if (!kobj)
		kobj = blktap_page_pool_create(name,
					       POOL_DEFAULT_PAGES);
	if (!kobj)
		return ERR_PTR(-ENOMEM);

	return kobj_to_pool(kobj);
}

int __init
blktap_page_pool_init(struct kobject *parent)
{
	request_cache =
		kmem_cache_create("blktap-request",
				  sizeof(struct blktap_request), 0,
				  0, blktap_request_ctor);
	if (!request_cache)
		return -ENOMEM;

	request_pool =
		mempool_create_slab_pool(POOL_MIN_REQS, request_cache);
	if (!request_pool)
		return -ENOMEM;

	pool_set = kset_create_and_add("pools", NULL, parent);
	if (!pool_set)
		return -ENOMEM;

	return 0;
}

void
blktap_page_pool_exit(void)
{
	if (pool_set) {
		BUG_ON(!list_empty(&pool_set->list));
		kset_unregister(pool_set);
		pool_set = NULL;
	}

	if (request_pool) {
		mempool_destroy(request_pool);
		request_pool = NULL;
	}

	if (request_cache) {
		kmem_cache_destroy(request_cache);
		request_cache = NULL;
	}
}