summaryrefslogtreecommitdiff
path: root/fs/exofs/osd.c
blob: 99375382ae573edd55f9ef8281e71ee73e1548c0 (plain)
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
/*
 * Copyright (C) 2005, 2006
 * Avishay Traeger (avishay@gmail.com) (avishay@il.ibm.com)
 * Copyright (C) 2005, 2006
 * International Business Machines
 * Copyright (C) 2008, 2009
 * Boaz Harrosh <bharrosh@panasas.com>
 *
 * This file is part of exofs.
 *
 * exofs is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.  Since it is based on ext2, and the only
 * valid version of GPL for the Linux kernel is version 2, the only valid
 * version of GPL for exofs is version 2.
 *
 * exofs 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 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with exofs; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <scsi/scsi_device.h>
#include <scsi/osd_sense.h>

#include "exofs.h"

int exofs_check_ok(struct osd_request *or)
{
	struct osd_sense_info osi;
	int ret = osd_req_decode_sense(or, &osi);

	if (ret) { /* translate to Linux codes */
		if (osi.additional_code == scsi_invalid_field_in_cdb) {
			if (osi.cdb_field_offset == OSD_CFO_STARTING_BYTE)
				ret = -EFAULT;
			if (osi.cdb_field_offset == OSD_CFO_OBJECT_ID)
				ret = -ENOENT;
			else
				ret = -EINVAL;
		} else if (osi.additional_code == osd_quota_error)
			ret = -ENOSPC;
		else
			ret = -EIO;
	}

	return ret;
}

void exofs_make_credential(uint8_t cred_a[OSD_CAP_LEN], uint64_t pid,
			   uint64_t oid)
{
	struct osd_obj_id obj = {
		.partition = pid,
		.id = oid
	};

	osd_sec_init_nosec_doall_caps(cred_a, &obj, false, true);
}

/*
 * Perform a synchronous OSD operation.
 */
int exofs_sync_op(struct osd_request *or, int timeout, uint8_t *credential)
{
	int ret;

	or->timeout = timeout;
	ret = osd_finalize_request(or, 0, credential, NULL);
	if (ret) {
		EXOFS_DBGMSG("Faild to osd_finalize_request() => %d\n", ret);
		return ret;
	}

	ret = osd_execute_request(or);

	if (ret)
		EXOFS_DBGMSG("osd_execute_request() => %d\n", ret);
	/* osd_req_decode_sense(or, ret); */
	return ret;
}

/*
 * Perform an asynchronous OSD operation.
 */
int exofs_async_op(struct osd_request *or, osd_req_done_fn *async_done,
		   void *caller_context, char *credential)
{
	int ret;

	ret = osd_finalize_request(or, 0, credential, NULL);
	if (ret) {
		EXOFS_DBGMSG("Faild to osd_finalize_request() => %d\n", ret);
		return ret;
	}

	ret = osd_execute_request_async(or, async_done, caller_context);

	if (ret)
		EXOFS_DBGMSG("osd_execute_request_async() => %d\n", ret);
	return ret;
}

int prepare_get_attr_list_add_entry(struct osd_request *or,
				    uint32_t page_num, uint32_t attr_num,
				    uint32_t attr_len)
{
	struct osd_attr attr = {
		.attr_page = page_num,
		.attr_id = attr_num,
		.len = attr_len,
	};

	return osd_req_add_get_attr_list(or, &attr, 1);
}

int prepare_set_attr_list_add_entry(struct osd_request *or,
				    uint32_t page_num, uint32_t attr_num,
				    uint16_t attr_len, const u8 *attr_val)
{
	struct osd_attr attr = {
		.attr_page = page_num,
		.attr_id = attr_num,
		.len = attr_len,
		.val_ptr = (u8 *)attr_val,
	};

	return osd_req_add_set_attr_list(or, &attr, 1);
}

int extract_next_attr_from_req(struct osd_request *or,
	uint32_t *page_num, uint32_t *attr_num,
	uint16_t *attr_len, uint8_t **attr_val)
{
	struct osd_attr attr = {.attr_page = 0}; /* start with zeros */
	void *iter = NULL;
	int nelem;

	do {
		nelem = 1;
		osd_req_decode_get_attr_list(or, &attr, &nelem, &iter);
		if ((attr.attr_page == *page_num) &&
		    (attr.attr_id == *attr_num)) {
			*attr_len = attr.len;
			*attr_val = attr.val_ptr;
			return 0;
		}
	} while (iter);

	return -EIO;
}

struct osd_request *prepare_osd_format_lun(struct osd_dev *od,
					   uint64_t formatted_capacity)
{
	struct osd_request *or = osd_start_request(od, GFP_KERNEL);

	if (!or)
		return NULL;

	osd_req_format(or, formatted_capacity);

	return or;
}

struct osd_request *prepare_osd_create_partition(struct osd_dev *od,
						 uint64_t requested_id)
{
	struct osd_request *or = osd_start_request(od, GFP_KERNEL);

	if (!or)
		return NULL;

	osd_req_create_partition(or, requested_id);

	return or;
}

struct osd_request *prepare_osd_remove_partition(struct osd_dev *od,
						 uint64_t requested_id)
{
	struct osd_request *or = osd_start_request(od, GFP_KERNEL);

	if (!or)
		return NULL;

	osd_req_remove_partition(or, requested_id);

	return or;
}

struct osd_request *prepare_osd_create(struct osd_dev *od,
				     uint64_t part_id,
				     uint64_t requested_id)
{
	struct osd_obj_id obj = {
		.partition = part_id,
		.id = requested_id
	};
	struct osd_request *or = osd_start_request(od, GFP_KERNEL);

	if (!or)
		return NULL;

	osd_req_create_object(or, &obj);

	return or;
}

struct osd_request *prepare_osd_remove(struct osd_dev *od,
				     uint64_t part_id,
				     uint64_t obj_id)
{
	struct osd_obj_id obj = {
		.partition = part_id,
		.id = obj_id
	};
	struct osd_request *or = osd_start_request(od, GFP_KERNEL);

	if (!or)
		return NULL;

	osd_req_remove_object(or, &obj);

	return or;
}

struct osd_request *prepare_osd_set_attr(struct osd_dev *od,
				       uint64_t part_id,
				       uint64_t obj_id)
{
	struct osd_obj_id obj = {
		.partition = part_id,
		.id = obj_id
	};
	struct osd_request *or = osd_start_request(od, GFP_KERNEL);

	if (!or)
		return NULL;

	osd_req_set_attributes(or, &obj);

	return or;
}

struct osd_request *prepare_osd_get_attr(struct osd_dev *od,
				       uint64_t part_id,
				       uint64_t obj_id)
{
	struct osd_obj_id obj = {
		.partition = part_id,
		.id = obj_id
	};
	struct osd_request *or = osd_start_request(od, GFP_KERNEL);

	if (!or)
		return NULL;

	osd_req_get_attributes(or, &obj);

	return or;
}

static struct bio *_bio_map_pages(struct request_queue *req_q,
				  struct page **pages, unsigned page_count,
				  size_t length, gfp_t gfp_mask)
{
	struct bio *bio;
	int i;

	bio = bio_alloc(gfp_mask, page_count);
	if (!bio) {
		EXOFS_DBGMSG("Failed to bio_alloc page_count=%d\n", page_count);
		return NULL;
	}

	for (i = 0; i < page_count && length; i++) {
		size_t use_len = min(length, PAGE_SIZE);

		if (use_len !=
		    bio_add_pc_page(req_q, bio, pages[i], use_len, 0)) {
			EXOFS_ERR("Failed bio_add_pc_page req_q=%p pages[i]=%p "
				  "use_len=%Zd page_count=%d length=%Zd\n",
				  req_q, pages[i], use_len, page_count, length);
			bio_put(bio);
			return NULL;
		}

		length -= use_len;
	}

	WARN_ON(length);
	return bio;
}

static struct osd_request *_prepare_read(struct osd_dev *od,
				   uint64_t part_id, uint64_t obj_id,
				   uint64_t offset, struct bio *bio)
{
	struct osd_obj_id obj = {
		.partition = part_id,
		.id = obj_id
	};
	struct osd_request *or = osd_start_request(od, GFP_KERNEL);

	if (!or)
		return NULL;

	osd_req_read(or, &obj, bio, offset);
	EXOFS_DBGMSG("osd_req_read(p=%llX, ob=%llX, l=%llu, of=%llu)\n",
		_LLU(part_id), _LLU(obj_id), _LLU(bio->bi_size), _LLU(offset));
	return or;
}

struct osd_request *prepare_osd_read_pages(struct osd_dev *od,
				   uint64_t part_id, uint64_t obj_id,
				   uint64_t length, uint64_t offset,
				   struct page **pages, int page_count)
{
	struct request_queue *req_q = od->scsi_device->request_queue;
	struct bio *bio = _bio_map_pages(req_q, pages, page_count, length,
					 GFP_KERNEL);

	if (!bio)
		return NULL;
	return _prepare_read(od, part_id, obj_id, offset, bio);
}

struct osd_request *prepare_osd_read(struct osd_dev *od,
				   uint64_t part_id, uint64_t obj_id,
				   uint64_t length, uint64_t offset,
				   void *data)
{
	struct request_queue *req_q = od->scsi_device->request_queue;
	struct bio *bio = bio_map_kern(req_q, data, length, GFP_KERNEL);

	if (!bio)
		return NULL;
	return _prepare_read(od, part_id, obj_id, offset, bio);
}

static struct osd_request *_prepare_write(struct osd_dev *od,
				    uint64_t part_id, uint64_t obj_id,
				    uint64_t offset, struct bio *bio)
{
	struct osd_obj_id obj = {
		.partition = part_id,
		.id = obj_id
	};
	struct osd_request *or = osd_start_request(od, GFP_KERNEL);

	if (!or)
		return NULL;

	osd_req_write(or, &obj, bio, offset);
	EXOFS_DBGMSG("osd_req_write(p=%llX, ob=%llX, l=%llu, of=%llu)\n",
		_LLU(part_id), _LLU(obj_id), _LLU(bio->bi_size), _LLU(offset));
	return or;
}

struct osd_request *prepare_osd_write_pages(struct osd_dev *od,
				    uint64_t part_id, uint64_t obj_id,
				    uint64_t length, uint64_t offset,
				    struct page **pages, int page_count)
{
	struct request_queue *req_q = od->scsi_device->request_queue;
	struct bio *bio = _bio_map_pages(req_q, pages, page_count, length,
					 GFP_KERNEL);

	if (!bio)
		return NULL;
	return _prepare_write(od, part_id, obj_id, offset, bio);
}

struct osd_request *prepare_osd_write(struct osd_dev *od,
				    uint64_t part_id, uint64_t obj_id,
				    uint64_t length, uint64_t offset,
				    void *data)
{
	struct request_queue *req_q = od->scsi_device->request_queue;
	struct bio *bio = bio_map_kern(req_q, data, length, GFP_KERNEL);

	if (!bio)
		return NULL;
	return _prepare_write(od, part_id, obj_id, offset, bio);
}

void free_osd_req(struct osd_request *or)
{
	osd_end_request(or);
}