]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/exofs/ios.c
exofs: Micro-optimize exofs_i_info
[mirror_ubuntu-bionic-kernel.git] / fs / exofs / ios.c
CommitLineData
b14f8ab2
BH
1/*
2 * Copyright (C) 2005, 2006
27d2e149 3 * Avishay Traeger (avishay@gmail.com)
b14f8ab2
BH
4 * Copyright (C) 2008, 2009
5 * Boaz Harrosh <bharrosh@panasas.com>
6 *
7 * This file is part of exofs.
8 *
9 * exofs is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation. Since it is based on ext2, and the only
12 * valid version of GPL for the Linux kernel is version 2, the only valid
13 * version of GPL for exofs is version 2.
14 *
15 * exofs is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with exofs; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <scsi/scsi_device.h>
b14f8ab2
BH
26
27#include "exofs.h"
28
34ce4e7c
BH
29#define EXOFS_DBGMSG2(M...) do {} while (0)
30/* #define EXOFS_DBGMSG2 EXOFS_DBGMSG */
31
06886a5a 32void exofs_make_credential(u8 cred_a[OSD_CAP_LEN], const struct osd_obj_id *obj)
b14f8ab2 33{
06886a5a
BH
34 osd_sec_init_nosec_doall_caps(cred_a, obj, false, true);
35}
36
37int exofs_read_kern(struct osd_dev *od, u8 *cred, struct osd_obj_id *obj,
38 u64 offset, void *p, unsigned length)
39{
40 struct osd_request *or = osd_start_request(od, GFP_KERNEL);
41/* struct osd_sense_info osi = {.key = 0};*/
42 int ret;
43
44 if (unlikely(!or)) {
45 EXOFS_DBGMSG("%s: osd_start_request failed.\n", __func__);
46 return -ENOMEM;
47 }
48 ret = osd_req_read_kern(or, obj, offset, p, length);
49 if (unlikely(ret)) {
50 EXOFS_DBGMSG("%s: osd_req_read_kern failed.\n", __func__);
51 goto out;
b14f8ab2
BH
52 }
53
06886a5a
BH
54 ret = osd_finalize_request(or, 0, cred, NULL);
55 if (unlikely(ret)) {
56 EXOFS_DBGMSG("Faild to osd_finalize_request() => %d\n", ret);
57 goto out;
58 }
b14f8ab2 59
06886a5a
BH
60 ret = osd_execute_request(or);
61 if (unlikely(ret))
62 EXOFS_DBGMSG("osd_execute_request() => %d\n", ret);
63 /* osd_req_decode_sense(or, ret); */
b14f8ab2 64
06886a5a
BH
65out:
66 osd_end_request(or);
b14f8ab2
BH
67 return ret;
68}
69
06886a5a 70int exofs_get_io_state(struct exofs_sb_info *sbi, struct exofs_io_state** pios)
b14f8ab2 71{
06886a5a
BH
72 struct exofs_io_state *ios;
73
74 /*TODO: Maybe use kmem_cach per sbi of size
75 * exofs_io_state_size(sbi->s_numdevs)
76 */
04dc1e88 77 ios = kzalloc(exofs_io_state_size(sbi->s_numdevs), GFP_KERNEL);
06886a5a 78 if (unlikely(!ios)) {
34ce4e7c
BH
79 EXOFS_DBGMSG("Faild kzalloc bytes=%d\n",
80 exofs_io_state_size(sbi->s_numdevs));
06886a5a
BH
81 *pios = NULL;
82 return -ENOMEM;
83 }
84
85 ios->sbi = sbi;
86 ios->obj.partition = sbi->s_pid;
87 *pios = ios;
88 return 0;
b14f8ab2
BH
89}
90
06886a5a 91void exofs_put_io_state(struct exofs_io_state *ios)
b14f8ab2 92{
06886a5a
BH
93 if (ios) {
94 unsigned i;
b14f8ab2 95
06886a5a
BH
96 for (i = 0; i < ios->numdevs; i++) {
97 struct exofs_per_dev_state *per_dev = &ios->per_dev[i];
98
99 if (per_dev->or)
100 osd_end_request(per_dev->or);
101 if (per_dev->bio)
102 bio_put(per_dev->bio);
103 }
104
105 kfree(ios);
b14f8ab2 106 }
06886a5a 107}
b14f8ab2 108
06886a5a
BH
109static void _sync_done(struct exofs_io_state *ios, void *p)
110{
111 struct completion *waiting = p;
b14f8ab2 112
06886a5a
BH
113 complete(waiting);
114}
115
116static void _last_io(struct kref *kref)
117{
118 struct exofs_io_state *ios = container_of(
119 kref, struct exofs_io_state, kref);
120
121 ios->done(ios, ios->private);
122}
123
124static void _done_io(struct osd_request *or, void *p)
125{
126 struct exofs_io_state *ios = p;
127
128 kref_put(&ios->kref, _last_io);
129}
130
131static int exofs_io_execute(struct exofs_io_state *ios)
132{
133 DECLARE_COMPLETION_ONSTACK(wait);
134 bool sync = (ios->done == NULL);
135 int i, ret;
136
137 if (sync) {
138 ios->done = _sync_done;
139 ios->private = &wait;
140 }
141
142 for (i = 0; i < ios->numdevs; i++) {
143 struct osd_request *or = ios->per_dev[i].or;
144 if (unlikely(!or))
145 continue;
146
147 ret = osd_finalize_request(or, 0, ios->cred, NULL);
148 if (unlikely(ret)) {
149 EXOFS_DBGMSG("Faild to osd_finalize_request() => %d\n",
150 ret);
151 return ret;
152 }
153 }
154
155 kref_init(&ios->kref);
156
157 for (i = 0; i < ios->numdevs; i++) {
158 struct osd_request *or = ios->per_dev[i].or;
159 if (unlikely(!or))
160 continue;
161
162 kref_get(&ios->kref);
163 osd_execute_request_async(or, _done_io, ios);
164 }
165
166 kref_put(&ios->kref, _last_io);
167 ret = 0;
168
169 if (sync) {
170 wait_for_completion(&wait);
171 ret = exofs_check_io(ios, NULL);
172 }
b14f8ab2
BH
173 return ret;
174}
175
06886a5a 176int exofs_check_io(struct exofs_io_state *ios, u64 *resid)
b14f8ab2 177{
06886a5a
BH
178 enum osd_err_priority acumulated_osd_err = 0;
179 int acumulated_lin_err = 0;
180 int i;
b14f8ab2 181
06886a5a
BH
182 for (i = 0; i < ios->numdevs; i++) {
183 struct osd_sense_info osi;
184 int ret = osd_req_decode_sense(ios->per_dev[i].or, &osi);
185
186 if (likely(!ret))
187 continue;
188
189 if (unlikely(ret == -EFAULT)) {
190 EXOFS_DBGMSG("%s: EFAULT Need page clear\n", __func__);
191 /*FIXME: All the pages in this device range should:
192 * clear_highpage(page);
193 */
194 }
195
196 if (osi.osd_err_pri >= acumulated_osd_err) {
197 acumulated_osd_err = osi.osd_err_pri;
198 acumulated_lin_err = ret;
199 }
200 }
201
202 /* TODO: raid specific residual calculations */
203 if (resid) {
204 if (likely(!acumulated_lin_err))
205 *resid = 0;
206 else
207 *resid = ios->length;
208 }
209
210 return acumulated_lin_err;
211}
212
213int exofs_sbi_create(struct exofs_io_state *ios)
214{
215 int i, ret;
216
04dc1e88 217 for (i = 0; i < ios->sbi->s_numdevs; i++) {
06886a5a
BH
218 struct osd_request *or;
219
04dc1e88 220 or = osd_start_request(ios->sbi->s_ods[i], GFP_KERNEL);
06886a5a
BH
221 if (unlikely(!or)) {
222 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
223 ret = -ENOMEM;
224 goto out;
225 }
226 ios->per_dev[i].or = or;
227 ios->numdevs++;
228
229 osd_req_create_object(or, &ios->obj);
230 }
231 ret = exofs_io_execute(ios);
232
233out:
234 return ret;
235}
236
237int exofs_sbi_remove(struct exofs_io_state *ios)
238{
239 int i, ret;
240
04dc1e88 241 for (i = 0; i < ios->sbi->s_numdevs; i++) {
06886a5a
BH
242 struct osd_request *or;
243
04dc1e88 244 or = osd_start_request(ios->sbi->s_ods[i], GFP_KERNEL);
06886a5a
BH
245 if (unlikely(!or)) {
246 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
247 ret = -ENOMEM;
248 goto out;
249 }
250 ios->per_dev[i].or = or;
251 ios->numdevs++;
252
253 osd_req_remove_object(or, &ios->obj);
254 }
255 ret = exofs_io_execute(ios);
256
257out:
258 return ret;
259}
260
261int exofs_sbi_write(struct exofs_io_state *ios)
262{
263 int i, ret;
264
04dc1e88 265 for (i = 0; i < ios->sbi->s_numdevs; i++) {
06886a5a
BH
266 struct osd_request *or;
267
04dc1e88 268 or = osd_start_request(ios->sbi->s_ods[i], GFP_KERNEL);
06886a5a
BH
269 if (unlikely(!or)) {
270 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
271 ret = -ENOMEM;
272 goto out;
273 }
274 ios->per_dev[i].or = or;
275 ios->numdevs++;
276
277 if (ios->bio) {
278 struct bio *bio;
279
04dc1e88
BH
280 if (i != 0) {
281 bio = bio_kmalloc(GFP_KERNEL,
282 ios->bio->bi_max_vecs);
283 if (unlikely(!bio)) {
34ce4e7c
BH
284 EXOFS_DBGMSG(
285 "Faild to allocate BIO size=%u\n",
286 ios->bio->bi_max_vecs);
04dc1e88
BH
287 ret = -ENOMEM;
288 goto out;
289 }
290
291 __bio_clone(bio, ios->bio);
292 bio->bi_bdev = NULL;
293 bio->bi_next = NULL;
294 ios->per_dev[i].bio = bio;
295 } else {
296 bio = ios->bio;
297 }
06886a5a
BH
298
299 osd_req_write(or, &ios->obj, ios->offset, bio,
300 ios->length);
34ce4e7c
BH
301 EXOFS_DBGMSG("write(0x%llx) offset=0x%llx "
302 "length=0x%llx dev=%d\n",
303 _LLU(ios->obj.id), _LLU(ios->offset),
304 _LLU(ios->length), i);
06886a5a
BH
305 } else if (ios->kern_buff) {
306 osd_req_write_kern(or, &ios->obj, ios->offset,
307 ios->kern_buff, ios->length);
34ce4e7c
BH
308 EXOFS_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
309 "length=0x%llx dev=%d\n",
310 _LLU(ios->obj.id), _LLU(ios->offset),
311 _LLU(ios->length), i);
06886a5a
BH
312 } else {
313 osd_req_set_attributes(or, &ios->obj);
34ce4e7c
BH
314 EXOFS_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
315 _LLU(ios->obj.id), ios->out_attr_len, i);
06886a5a
BH
316 }
317
318 if (ios->out_attr)
319 osd_req_add_set_attr_list(or, ios->out_attr,
320 ios->out_attr_len);
321
322 if (ios->in_attr)
323 osd_req_add_get_attr_list(or, ios->in_attr,
324 ios->in_attr_len);
b14f8ab2 325 }
06886a5a
BH
326 ret = exofs_io_execute(ios);
327
328out:
329 return ret;
330}
331
332int exofs_sbi_read(struct exofs_io_state *ios)
333{
334 int i, ret;
335
336 for (i = 0; i < 1; i++) {
337 struct osd_request *or;
04dc1e88 338 unsigned first_dev = (unsigned)ios->obj.id;
06886a5a 339
04dc1e88
BH
340 first_dev %= ios->sbi->s_numdevs;
341 or = osd_start_request(ios->sbi->s_ods[first_dev], GFP_KERNEL);
06886a5a
BH
342 if (unlikely(!or)) {
343 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
344 ret = -ENOMEM;
345 goto out;
346 }
347 ios->per_dev[i].or = or;
348 ios->numdevs++;
349
350 if (ios->bio) {
351 osd_req_read(or, &ios->obj, ios->offset, ios->bio,
352 ios->length);
34ce4e7c
BH
353 EXOFS_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
354 " dev=%d\n", _LLU(ios->obj.id),
355 _LLU(ios->offset),
356 _LLU(ios->length),
357 first_dev);
06886a5a
BH
358 } else if (ios->kern_buff) {
359 osd_req_read_kern(or, &ios->obj, ios->offset,
360 ios->kern_buff, ios->length);
34ce4e7c
BH
361 EXOFS_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
362 "length=0x%llx dev=%d\n",
363 _LLU(ios->obj.id),
364 _LLU(ios->offset),
365 _LLU(ios->length),
366 first_dev);
06886a5a
BH
367 } else {
368 osd_req_get_attributes(or, &ios->obj);
34ce4e7c
BH
369 EXOFS_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
370 _LLU(ios->obj.id), ios->in_attr_len,
371 first_dev);
06886a5a
BH
372 }
373
374 if (ios->out_attr)
375 osd_req_add_set_attr_list(or, ios->out_attr,
376 ios->out_attr_len);
b14f8ab2 377
06886a5a
BH
378 if (ios->in_attr)
379 osd_req_add_get_attr_list(or, ios->in_attr,
380 ios->in_attr_len);
381 }
382 ret = exofs_io_execute(ios);
b14f8ab2 383
06886a5a 384out:
b14f8ab2
BH
385 return ret;
386}
387
06886a5a 388int extract_attr_from_ios(struct exofs_io_state *ios, struct osd_attr *attr)
b14f8ab2
BH
389{
390 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
391 void *iter = NULL;
392 int nelem;
393
394 do {
395 nelem = 1;
06886a5a
BH
396 osd_req_decode_get_attr_list(ios->per_dev[0].or,
397 &cur_attr, &nelem, &iter);
b14f8ab2
BH
398 if ((cur_attr.attr_page == attr->attr_page) &&
399 (cur_attr.attr_id == attr->attr_id)) {
400 attr->len = cur_attr.len;
401 attr->val_ptr = cur_attr.val_ptr;
402 return 0;
403 }
404 } while (iter);
405
406 return -EIO;
407}
06886a5a
BH
408
409int exofs_oi_truncate(struct exofs_i_info *oi, u64 size)
410{
411 struct exofs_sb_info *sbi = oi->vfs_inode.i_sb->s_fs_info;
412 struct exofs_io_state *ios;
413 struct osd_attr attr;
414 __be64 newsize;
415 int i, ret;
416
417 if (exofs_get_io_state(sbi, &ios))
418 return -ENOMEM;
419
420 ios->obj.id = exofs_oi_objno(oi);
421 ios->cred = oi->i_cred;
422
423 newsize = cpu_to_be64(size);
424 attr = g_attr_logical_length;
425 attr.val_ptr = &newsize;
426
04dc1e88 427 for (i = 0; i < sbi->s_numdevs; i++) {
06886a5a
BH
428 struct osd_request *or;
429
04dc1e88 430 or = osd_start_request(sbi->s_ods[i], GFP_KERNEL);
06886a5a
BH
431 if (unlikely(!or)) {
432 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
433 ret = -ENOMEM;
434 goto out;
435 }
436 ios->per_dev[i].or = or;
437 ios->numdevs++;
438
439 osd_req_set_attributes(or, &ios->obj);
440 osd_req_add_set_attr_list(or, &attr, 1);
441 }
442 ret = exofs_io_execute(ios);
443
444out:
445 exofs_put_io_state(ios);
446 return ret;
447}