]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/nfs/objlayout/objlayout.c
NFS: Revert pnfs ugliness from the generic NFS read code path
[karo-tx-linux.git] / fs / nfs / objlayout / objlayout.c
1 /*
2  *  pNFS Objects layout driver high level definitions
3  *
4  *  Copyright (C) 2007 Panasas Inc. [year of first publication]
5  *  All rights reserved.
6  *
7  *  Benny Halevy <bhalevy@panasas.com>
8  *  Boaz Harrosh <bharrosh@panasas.com>
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License version 2
12  *  See the file COPYING included with this distribution for more details.
13  *
14  *  Redistribution and use in source and binary forms, with or without
15  *  modification, are permitted provided that the following conditions
16  *  are met:
17  *
18  *  1. Redistributions of source code must retain the above copyright
19  *     notice, this list of conditions and the following disclaimer.
20  *  2. Redistributions in binary form must reproduce the above copyright
21  *     notice, this list of conditions and the following disclaimer in the
22  *     documentation and/or other materials provided with the distribution.
23  *  3. Neither the name of the Panasas company nor the names of its
24  *     contributors may be used to endorse or promote products derived
25  *     from this software without specific prior written permission.
26  *
27  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
28  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
34  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  */
39
40 #include <scsi/osd_initiator.h>
41 #include "objlayout.h"
42
43 #define NFSDBG_FACILITY         NFSDBG_PNFS_LD
44 /*
45  * Create a objlayout layout structure for the given inode and return it.
46  */
47 struct pnfs_layout_hdr *
48 objlayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
49 {
50         struct objlayout *objlay;
51
52         objlay = kzalloc(sizeof(struct objlayout), gfp_flags);
53         if (objlay) {
54                 spin_lock_init(&objlay->lock);
55                 INIT_LIST_HEAD(&objlay->err_list);
56         }
57         dprintk("%s: Return %p\n", __func__, objlay);
58         return &objlay->pnfs_layout;
59 }
60
61 /*
62  * Free an objlayout layout structure
63  */
64 void
65 objlayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
66 {
67         struct objlayout *objlay = OBJLAYOUT(lo);
68
69         dprintk("%s: objlay %p\n", __func__, objlay);
70
71         WARN_ON(!list_empty(&objlay->err_list));
72         kfree(objlay);
73 }
74
75 /*
76  * Unmarshall layout and store it in pnfslay.
77  */
78 struct pnfs_layout_segment *
79 objlayout_alloc_lseg(struct pnfs_layout_hdr *pnfslay,
80                      struct nfs4_layoutget_res *lgr,
81                      gfp_t gfp_flags)
82 {
83         int status = -ENOMEM;
84         struct xdr_stream stream;
85         struct xdr_buf buf = {
86                 .pages =  lgr->layoutp->pages,
87                 .page_len =  lgr->layoutp->len,
88                 .buflen =  lgr->layoutp->len,
89                 .len = lgr->layoutp->len,
90         };
91         struct page *scratch;
92         struct pnfs_layout_segment *lseg;
93
94         dprintk("%s: Begin pnfslay %p\n", __func__, pnfslay);
95
96         scratch = alloc_page(gfp_flags);
97         if (!scratch)
98                 goto err_nofree;
99
100         xdr_init_decode(&stream, &buf, NULL);
101         xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
102
103         status = objio_alloc_lseg(&lseg, pnfslay, &lgr->range, &stream, gfp_flags);
104         if (unlikely(status)) {
105                 dprintk("%s: objio_alloc_lseg Return err %d\n", __func__,
106                         status);
107                 goto err;
108         }
109
110         __free_page(scratch);
111
112         dprintk("%s: Return %p\n", __func__, lseg);
113         return lseg;
114
115 err:
116         __free_page(scratch);
117 err_nofree:
118         dprintk("%s: Err Return=>%d\n", __func__, status);
119         return ERR_PTR(status);
120 }
121
122 /*
123  * Free a layout segement
124  */
125 void
126 objlayout_free_lseg(struct pnfs_layout_segment *lseg)
127 {
128         dprintk("%s: freeing layout segment %p\n", __func__, lseg);
129
130         if (unlikely(!lseg))
131                 return;
132
133         objio_free_lseg(lseg);
134 }
135
136 /*
137  * I/O Operations
138  */
139 static inline u64
140 end_offset(u64 start, u64 len)
141 {
142         u64 end;
143
144         end = start + len;
145         return end >= start ? end : NFS4_MAX_UINT64;
146 }
147
148 /* last octet in a range */
149 static inline u64
150 last_byte_offset(u64 start, u64 len)
151 {
152         u64 end;
153
154         BUG_ON(!len);
155         end = start + len;
156         return end > start ? end - 1 : NFS4_MAX_UINT64;
157 }
158
159 void _fix_verify_io_params(struct pnfs_layout_segment *lseg,
160                            struct page ***p_pages, unsigned *p_pgbase,
161                            u64 offset, unsigned long count)
162 {
163         u64 lseg_end_offset;
164
165         BUG_ON(offset < lseg->pls_range.offset);
166         lseg_end_offset = end_offset(lseg->pls_range.offset,
167                                      lseg->pls_range.length);
168         BUG_ON(offset >= lseg_end_offset);
169         WARN_ON(offset + count > lseg_end_offset);
170
171         if (*p_pgbase > PAGE_SIZE) {
172                 dprintk("%s: pgbase(0x%x) > PAGE_SIZE\n", __func__, *p_pgbase);
173                 *p_pages += *p_pgbase >> PAGE_SHIFT;
174                 *p_pgbase &= ~PAGE_MASK;
175         }
176 }
177
178 /*
179  * I/O done common code
180  */
181 static void
182 objlayout_iodone(struct objlayout_io_res *oir)
183 {
184         if (likely(oir->status >= 0)) {
185                 objio_free_result(oir);
186         } else {
187                 struct objlayout *objlay = oir->objlay;
188
189                 spin_lock(&objlay->lock);
190                 objlay->delta_space_valid = OBJ_DSU_INVALID;
191                 list_add(&objlay->err_list, &oir->err_list);
192                 spin_unlock(&objlay->lock);
193         }
194 }
195
196 /*
197  * objlayout_io_set_result - Set an osd_error code on a specific osd comp.
198  *
199  * The @index component IO failed (error returned from target). Register
200  * the error for later reporting at layout-return.
201  */
202 void
203 objlayout_io_set_result(struct objlayout_io_res *oir, unsigned index,
204                         struct pnfs_osd_objid *pooid, int osd_error,
205                         u64 offset, u64 length, bool is_write)
206 {
207         struct pnfs_osd_ioerr *ioerr = &oir->ioerrs[index];
208
209         BUG_ON(index >= oir->num_comps);
210         if (osd_error) {
211                 ioerr->oer_component = *pooid;
212                 ioerr->oer_comp_offset = offset;
213                 ioerr->oer_comp_length = length;
214                 ioerr->oer_iswrite = is_write;
215                 ioerr->oer_errno = osd_error;
216
217                 dprintk("%s: err[%d]: errno=%d is_write=%d dev(%llx:%llx) "
218                         "par=0x%llx obj=0x%llx offset=0x%llx length=0x%llx\n",
219                         __func__, index, ioerr->oer_errno,
220                         ioerr->oer_iswrite,
221                         _DEVID_LO(&ioerr->oer_component.oid_device_id),
222                         _DEVID_HI(&ioerr->oer_component.oid_device_id),
223                         ioerr->oer_component.oid_partition_id,
224                         ioerr->oer_component.oid_object_id,
225                         ioerr->oer_comp_offset,
226                         ioerr->oer_comp_length);
227         } else {
228                 /* User need not call if no error is reported */
229                 ioerr->oer_errno = 0;
230         }
231 }
232
233 /* Function scheduled on rpc workqueue to call ->nfs_readlist_complete().
234  * This is because the osd completion is called with ints-off from
235  * the block layer
236  */
237 static void _rpc_read_complete(struct work_struct *work)
238 {
239         struct rpc_task *task;
240         struct nfs_read_data *rdata;
241
242         dprintk("%s enter\n", __func__);
243         task = container_of(work, struct rpc_task, u.tk_work);
244         rdata = container_of(task, struct nfs_read_data, task);
245
246         pnfs_ld_read_done(rdata);
247 }
248
249 void
250 objlayout_read_done(struct objlayout_io_res *oir, ssize_t status, bool sync)
251 {
252         struct nfs_read_data *rdata = oir->rpcdata;
253
254         oir->status = rdata->task.tk_status = status;
255         if (status >= 0)
256                 rdata->res.count = status;
257         objlayout_iodone(oir);
258         /* must not use oir after this point */
259
260         dprintk("%s: Return status=%zd eof=%d sync=%d\n", __func__,
261                 status, rdata->res.eof, sync);
262
263         if (sync)
264                 pnfs_ld_read_done(rdata);
265         else {
266                 INIT_WORK(&rdata->task.u.tk_work, _rpc_read_complete);
267                 schedule_work(&rdata->task.u.tk_work);
268         }
269 }
270
271 /*
272  * Perform sync or async reads.
273  */
274 enum pnfs_try_status
275 objlayout_read_pagelist(struct nfs_read_data *rdata)
276 {
277         loff_t offset = rdata->args.offset;
278         size_t count = rdata->args.count;
279         int err;
280         loff_t eof;
281
282         eof = i_size_read(rdata->inode);
283         if (unlikely(offset + count > eof)) {
284                 if (offset >= eof) {
285                         err = 0;
286                         rdata->res.count = 0;
287                         rdata->res.eof = 1;
288                         /*FIXME: do we need to call pnfs_ld_read_done() */
289                         goto out;
290                 }
291                 count = eof - offset;
292         }
293
294         rdata->res.eof = (offset + count) >= eof;
295         _fix_verify_io_params(rdata->lseg, &rdata->args.pages,
296                               &rdata->args.pgbase,
297                               rdata->args.offset, rdata->args.count);
298
299         dprintk("%s: inode(%lx) offset 0x%llx count 0x%Zx eof=%d\n",
300                 __func__, rdata->inode->i_ino, offset, count, rdata->res.eof);
301
302         err = objio_read_pagelist(rdata);
303  out:
304         if (unlikely(err)) {
305                 rdata->pnfs_error = err;
306                 dprintk("%s: Returned Error %d\n", __func__, err);
307                 return PNFS_NOT_ATTEMPTED;
308         }
309         return PNFS_ATTEMPTED;
310 }
311
312 /* Function scheduled on rpc workqueue to call ->nfs_writelist_complete().
313  * This is because the osd completion is called with ints-off from
314  * the block layer
315  */
316 static void _rpc_write_complete(struct work_struct *work)
317 {
318         struct rpc_task *task;
319         struct nfs_write_data *wdata;
320
321         dprintk("%s enter\n", __func__);
322         task = container_of(work, struct rpc_task, u.tk_work);
323         wdata = container_of(task, struct nfs_write_data, task);
324
325         pnfs_ld_write_done(wdata);
326 }
327
328 void
329 objlayout_write_done(struct objlayout_io_res *oir, ssize_t status, bool sync)
330 {
331         struct nfs_write_data *wdata = oir->rpcdata;
332
333         oir->status = wdata->task.tk_status = status;
334         if (status >= 0) {
335                 wdata->res.count = status;
336                 wdata->verf.committed = oir->committed;
337         }
338         objlayout_iodone(oir);
339         /* must not use oir after this point */
340
341         dprintk("%s: Return status %zd committed %d sync=%d\n", __func__,
342                 status, wdata->verf.committed, sync);
343
344         if (sync)
345                 pnfs_ld_write_done(wdata);
346         else {
347                 INIT_WORK(&wdata->task.u.tk_work, _rpc_write_complete);
348                 schedule_work(&wdata->task.u.tk_work);
349         }
350 }
351
352 /*
353  * Perform sync or async writes.
354  */
355 enum pnfs_try_status
356 objlayout_write_pagelist(struct nfs_write_data *wdata,
357                          int how)
358 {
359         int err;
360
361         _fix_verify_io_params(wdata->lseg, &wdata->args.pages,
362                               &wdata->args.pgbase,
363                               wdata->args.offset, wdata->args.count);
364
365         err = objio_write_pagelist(wdata, how);
366         if (unlikely(err)) {
367                 wdata->pnfs_error = err;
368                 dprintk("%s: Returned Error %d\n", __func__, err);
369                 return PNFS_NOT_ATTEMPTED;
370         }
371         return PNFS_ATTEMPTED;
372 }
373
374 void
375 objlayout_encode_layoutcommit(struct pnfs_layout_hdr *pnfslay,
376                               struct xdr_stream *xdr,
377                               const struct nfs4_layoutcommit_args *args)
378 {
379         struct objlayout *objlay = OBJLAYOUT(pnfslay);
380         struct pnfs_osd_layoutupdate lou;
381         __be32 *start;
382
383         dprintk("%s: Begin\n", __func__);
384
385         spin_lock(&objlay->lock);
386         lou.dsu_valid = (objlay->delta_space_valid == OBJ_DSU_VALID);
387         lou.dsu_delta = objlay->delta_space_used;
388         objlay->delta_space_used = 0;
389         objlay->delta_space_valid = OBJ_DSU_INIT;
390         lou.olu_ioerr_flag = !list_empty(&objlay->err_list);
391         spin_unlock(&objlay->lock);
392
393         start = xdr_reserve_space(xdr, 4);
394
395         BUG_ON(pnfs_osd_xdr_encode_layoutupdate(xdr, &lou));
396
397         *start = cpu_to_be32((xdr->p - start - 1) * 4);
398
399         dprintk("%s: Return delta_space_used %lld err %d\n", __func__,
400                 lou.dsu_delta, lou.olu_ioerr_flag);
401 }
402
403 static int
404 err_prio(u32 oer_errno)
405 {
406         switch (oer_errno) {
407         case 0:
408                 return 0;
409
410         case PNFS_OSD_ERR_RESOURCE:
411                 return OSD_ERR_PRI_RESOURCE;
412         case PNFS_OSD_ERR_BAD_CRED:
413                 return OSD_ERR_PRI_BAD_CRED;
414         case PNFS_OSD_ERR_NO_ACCESS:
415                 return OSD_ERR_PRI_NO_ACCESS;
416         case PNFS_OSD_ERR_UNREACHABLE:
417                 return OSD_ERR_PRI_UNREACHABLE;
418         case PNFS_OSD_ERR_NOT_FOUND:
419                 return OSD_ERR_PRI_NOT_FOUND;
420         case PNFS_OSD_ERR_NO_SPACE:
421                 return OSD_ERR_PRI_NO_SPACE;
422         default:
423                 WARN_ON(1);
424                 /* fallthrough */
425         case PNFS_OSD_ERR_EIO:
426                 return OSD_ERR_PRI_EIO;
427         }
428 }
429
430 static void
431 merge_ioerr(struct pnfs_osd_ioerr *dest_err,
432             const struct pnfs_osd_ioerr *src_err)
433 {
434         u64 dest_end, src_end;
435
436         if (!dest_err->oer_errno) {
437                 *dest_err = *src_err;
438                 /* accumulated device must be blank */
439                 memset(&dest_err->oer_component.oid_device_id, 0,
440                         sizeof(dest_err->oer_component.oid_device_id));
441
442                 return;
443         }
444
445         if (dest_err->oer_component.oid_partition_id !=
446                                 src_err->oer_component.oid_partition_id)
447                 dest_err->oer_component.oid_partition_id = 0;
448
449         if (dest_err->oer_component.oid_object_id !=
450                                 src_err->oer_component.oid_object_id)
451                 dest_err->oer_component.oid_object_id = 0;
452
453         if (dest_err->oer_comp_offset > src_err->oer_comp_offset)
454                 dest_err->oer_comp_offset = src_err->oer_comp_offset;
455
456         dest_end = end_offset(dest_err->oer_comp_offset,
457                               dest_err->oer_comp_length);
458         src_end =  end_offset(src_err->oer_comp_offset,
459                               src_err->oer_comp_length);
460         if (dest_end < src_end)
461                 dest_end = src_end;
462
463         dest_err->oer_comp_length = dest_end - dest_err->oer_comp_offset;
464
465         if ((src_err->oer_iswrite == dest_err->oer_iswrite) &&
466             (err_prio(src_err->oer_errno) > err_prio(dest_err->oer_errno))) {
467                         dest_err->oer_errno = src_err->oer_errno;
468         } else if (src_err->oer_iswrite) {
469                 dest_err->oer_iswrite = true;
470                 dest_err->oer_errno = src_err->oer_errno;
471         }
472 }
473
474 static void
475 encode_accumulated_error(struct objlayout *objlay, __be32 *p)
476 {
477         struct objlayout_io_res *oir, *tmp;
478         struct pnfs_osd_ioerr accumulated_err = {.oer_errno = 0};
479
480         list_for_each_entry_safe(oir, tmp, &objlay->err_list, err_list) {
481                 unsigned i;
482
483                 for (i = 0; i < oir->num_comps; i++) {
484                         struct pnfs_osd_ioerr *ioerr = &oir->ioerrs[i];
485
486                         if (!ioerr->oer_errno)
487                                 continue;
488
489                         printk(KERN_ERR "%s: err[%d]: errno=%d is_write=%d "
490                                 "dev(%llx:%llx) par=0x%llx obj=0x%llx "
491                                 "offset=0x%llx length=0x%llx\n",
492                                 __func__, i, ioerr->oer_errno,
493                                 ioerr->oer_iswrite,
494                                 _DEVID_LO(&ioerr->oer_component.oid_device_id),
495                                 _DEVID_HI(&ioerr->oer_component.oid_device_id),
496                                 ioerr->oer_component.oid_partition_id,
497                                 ioerr->oer_component.oid_object_id,
498                                 ioerr->oer_comp_offset,
499                                 ioerr->oer_comp_length);
500
501                         merge_ioerr(&accumulated_err, ioerr);
502                 }
503                 list_del(&oir->err_list);
504                 objio_free_result(oir);
505         }
506
507         pnfs_osd_xdr_encode_ioerr(p, &accumulated_err);
508 }
509
510 void
511 objlayout_encode_layoutreturn(struct pnfs_layout_hdr *pnfslay,
512                               struct xdr_stream *xdr,
513                               const struct nfs4_layoutreturn_args *args)
514 {
515         struct objlayout *objlay = OBJLAYOUT(pnfslay);
516         struct objlayout_io_res *oir, *tmp;
517         __be32 *start;
518
519         dprintk("%s: Begin\n", __func__);
520         start = xdr_reserve_space(xdr, 4);
521         BUG_ON(!start);
522
523         spin_lock(&objlay->lock);
524
525         list_for_each_entry_safe(oir, tmp, &objlay->err_list, err_list) {
526                 __be32 *last_xdr = NULL, *p;
527                 unsigned i;
528                 int res = 0;
529
530                 for (i = 0; i < oir->num_comps; i++) {
531                         struct pnfs_osd_ioerr *ioerr = &oir->ioerrs[i];
532
533                         if (!ioerr->oer_errno)
534                                 continue;
535
536                         dprintk("%s: err[%d]: errno=%d is_write=%d "
537                                 "dev(%llx:%llx) par=0x%llx obj=0x%llx "
538                                 "offset=0x%llx length=0x%llx\n",
539                                 __func__, i, ioerr->oer_errno,
540                                 ioerr->oer_iswrite,
541                                 _DEVID_LO(&ioerr->oer_component.oid_device_id),
542                                 _DEVID_HI(&ioerr->oer_component.oid_device_id),
543                                 ioerr->oer_component.oid_partition_id,
544                                 ioerr->oer_component.oid_object_id,
545                                 ioerr->oer_comp_offset,
546                                 ioerr->oer_comp_length);
547
548                         p = pnfs_osd_xdr_ioerr_reserve_space(xdr);
549                         if (unlikely(!p)) {
550                                 res = -E2BIG;
551                                 break; /* accumulated_error */
552                         }
553
554                         last_xdr = p;
555                         pnfs_osd_xdr_encode_ioerr(p, &oir->ioerrs[i]);
556                 }
557
558                 /* TODO: use xdr_write_pages */
559                 if (unlikely(res)) {
560                         /* no space for even one error descriptor */
561                         BUG_ON(!last_xdr);
562
563                         /* we've encountered a situation with lots and lots of
564                          * errors and no space to encode them all. Use the last
565                          * available slot to report the union of all the
566                          * remaining errors.
567                          */
568                         encode_accumulated_error(objlay, last_xdr);
569                         goto loop_done;
570                 }
571                 list_del(&oir->err_list);
572                 objio_free_result(oir);
573         }
574 loop_done:
575         spin_unlock(&objlay->lock);
576
577         *start = cpu_to_be32((xdr->p - start - 1) * 4);
578         dprintk("%s: Return\n", __func__);
579 }
580
581
582 /*
583  * Get Device Info API for io engines
584  */
585 struct objlayout_deviceinfo {
586         struct page *page;
587         struct pnfs_osd_deviceaddr da; /* This must be last */
588 };
589
590 /* Initialize and call nfs_getdeviceinfo, then decode and return a
591  * "struct pnfs_osd_deviceaddr *" Eventually objlayout_put_deviceinfo()
592  * should be called.
593  */
594 int objlayout_get_deviceinfo(struct pnfs_layout_hdr *pnfslay,
595         struct nfs4_deviceid *d_id, struct pnfs_osd_deviceaddr **deviceaddr,
596         gfp_t gfp_flags)
597 {
598         struct objlayout_deviceinfo *odi;
599         struct pnfs_device pd;
600         struct super_block *sb;
601         struct page *page, **pages;
602         u32 *p;
603         int err;
604
605         page = alloc_page(gfp_flags);
606         if (!page)
607                 return -ENOMEM;
608
609         pages = &page;
610         pd.pages = pages;
611
612         memcpy(&pd.dev_id, d_id, sizeof(*d_id));
613         pd.layout_type = LAYOUT_OSD2_OBJECTS;
614         pd.pages = &page;
615         pd.pgbase = 0;
616         pd.pglen = PAGE_SIZE;
617         pd.mincount = 0;
618
619         sb = pnfslay->plh_inode->i_sb;
620         err = nfs4_proc_getdeviceinfo(NFS_SERVER(pnfslay->plh_inode), &pd);
621         dprintk("%s nfs_getdeviceinfo returned %d\n", __func__, err);
622         if (err)
623                 goto err_out;
624
625         p = page_address(page);
626         odi = kzalloc(sizeof(*odi), gfp_flags);
627         if (!odi) {
628                 err = -ENOMEM;
629                 goto err_out;
630         }
631         pnfs_osd_xdr_decode_deviceaddr(&odi->da, p);
632         odi->page = page;
633         *deviceaddr = &odi->da;
634         return 0;
635
636 err_out:
637         __free_page(page);
638         return err;
639 }
640
641 void objlayout_put_deviceinfo(struct pnfs_osd_deviceaddr *deviceaddr)
642 {
643         struct objlayout_deviceinfo *odi = container_of(deviceaddr,
644                                                 struct objlayout_deviceinfo,
645                                                 da);
646
647         __free_page(odi->page);
648         kfree(odi);
649 }