Merge branch 'for-3.5-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[firefly-linux-kernel-4.4.55.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 <linux/kmod.h>
41 #include <linux/moduleparam.h>
42 #include <linux/ratelimit.h>
43 #include <scsi/osd_initiator.h>
44 #include "objlayout.h"
45
46 #define NFSDBG_FACILITY         NFSDBG_PNFS_LD
47 /*
48  * Create a objlayout layout structure for the given inode and return it.
49  */
50 struct pnfs_layout_hdr *
51 objlayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
52 {
53         struct objlayout *objlay;
54
55         objlay = kzalloc(sizeof(struct objlayout), gfp_flags);
56         if (objlay) {
57                 spin_lock_init(&objlay->lock);
58                 INIT_LIST_HEAD(&objlay->err_list);
59         }
60         dprintk("%s: Return %p\n", __func__, objlay);
61         return &objlay->pnfs_layout;
62 }
63
64 /*
65  * Free an objlayout layout structure
66  */
67 void
68 objlayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
69 {
70         struct objlayout *objlay = OBJLAYOUT(lo);
71
72         dprintk("%s: objlay %p\n", __func__, objlay);
73
74         WARN_ON(!list_empty(&objlay->err_list));
75         kfree(objlay);
76 }
77
78 /*
79  * Unmarshall layout and store it in pnfslay.
80  */
81 struct pnfs_layout_segment *
82 objlayout_alloc_lseg(struct pnfs_layout_hdr *pnfslay,
83                      struct nfs4_layoutget_res *lgr,
84                      gfp_t gfp_flags)
85 {
86         int status = -ENOMEM;
87         struct xdr_stream stream;
88         struct xdr_buf buf = {
89                 .pages =  lgr->layoutp->pages,
90                 .page_len =  lgr->layoutp->len,
91                 .buflen =  lgr->layoutp->len,
92                 .len = lgr->layoutp->len,
93         };
94         struct page *scratch;
95         struct pnfs_layout_segment *lseg;
96
97         dprintk("%s: Begin pnfslay %p\n", __func__, pnfslay);
98
99         scratch = alloc_page(gfp_flags);
100         if (!scratch)
101                 goto err_nofree;
102
103         xdr_init_decode(&stream, &buf, NULL);
104         xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
105
106         status = objio_alloc_lseg(&lseg, pnfslay, &lgr->range, &stream, gfp_flags);
107         if (unlikely(status)) {
108                 dprintk("%s: objio_alloc_lseg Return err %d\n", __func__,
109                         status);
110                 goto err;
111         }
112
113         __free_page(scratch);
114
115         dprintk("%s: Return %p\n", __func__, lseg);
116         return lseg;
117
118 err:
119         __free_page(scratch);
120 err_nofree:
121         dprintk("%s: Err Return=>%d\n", __func__, status);
122         return ERR_PTR(status);
123 }
124
125 /*
126  * Free a layout segement
127  */
128 void
129 objlayout_free_lseg(struct pnfs_layout_segment *lseg)
130 {
131         dprintk("%s: freeing layout segment %p\n", __func__, lseg);
132
133         if (unlikely(!lseg))
134                 return;
135
136         objio_free_lseg(lseg);
137 }
138
139 /*
140  * I/O Operations
141  */
142 static inline u64
143 end_offset(u64 start, u64 len)
144 {
145         u64 end;
146
147         end = start + len;
148         return end >= start ? end : NFS4_MAX_UINT64;
149 }
150
151 /* last octet in a range */
152 static inline u64
153 last_byte_offset(u64 start, u64 len)
154 {
155         u64 end;
156
157         BUG_ON(!len);
158         end = start + len;
159         return end > start ? end - 1 : NFS4_MAX_UINT64;
160 }
161
162 static void _fix_verify_io_params(struct pnfs_layout_segment *lseg,
163                            struct page ***p_pages, unsigned *p_pgbase,
164                            u64 offset, unsigned long count)
165 {
166         u64 lseg_end_offset;
167
168         BUG_ON(offset < lseg->pls_range.offset);
169         lseg_end_offset = end_offset(lseg->pls_range.offset,
170                                      lseg->pls_range.length);
171         BUG_ON(offset >= lseg_end_offset);
172         WARN_ON(offset + count > lseg_end_offset);
173
174         if (*p_pgbase > PAGE_SIZE) {
175                 dprintk("%s: pgbase(0x%x) > PAGE_SIZE\n", __func__, *p_pgbase);
176                 *p_pages += *p_pgbase >> PAGE_SHIFT;
177                 *p_pgbase &= ~PAGE_MASK;
178         }
179 }
180
181 /*
182  * I/O done common code
183  */
184 static void
185 objlayout_iodone(struct objlayout_io_res *oir)
186 {
187         if (likely(oir->status >= 0)) {
188                 objio_free_result(oir);
189         } else {
190                 struct objlayout *objlay = oir->objlay;
191
192                 spin_lock(&objlay->lock);
193                 objlay->delta_space_valid = OBJ_DSU_INVALID;
194                 list_add(&objlay->err_list, &oir->err_list);
195                 spin_unlock(&objlay->lock);
196         }
197 }
198
199 /*
200  * objlayout_io_set_result - Set an osd_error code on a specific osd comp.
201  *
202  * The @index component IO failed (error returned from target). Register
203  * the error for later reporting at layout-return.
204  */
205 void
206 objlayout_io_set_result(struct objlayout_io_res *oir, unsigned index,
207                         struct pnfs_osd_objid *pooid, int osd_error,
208                         u64 offset, u64 length, bool is_write)
209 {
210         struct pnfs_osd_ioerr *ioerr = &oir->ioerrs[index];
211
212         BUG_ON(index >= oir->num_comps);
213         if (osd_error) {
214                 ioerr->oer_component = *pooid;
215                 ioerr->oer_comp_offset = offset;
216                 ioerr->oer_comp_length = length;
217                 ioerr->oer_iswrite = is_write;
218                 ioerr->oer_errno = osd_error;
219
220                 dprintk("%s: err[%d]: errno=%d is_write=%d dev(%llx:%llx) "
221                         "par=0x%llx obj=0x%llx offset=0x%llx length=0x%llx\n",
222                         __func__, index, ioerr->oer_errno,
223                         ioerr->oer_iswrite,
224                         _DEVID_LO(&ioerr->oer_component.oid_device_id),
225                         _DEVID_HI(&ioerr->oer_component.oid_device_id),
226                         ioerr->oer_component.oid_partition_id,
227                         ioerr->oer_component.oid_object_id,
228                         ioerr->oer_comp_offset,
229                         ioerr->oer_comp_length);
230         } else {
231                 /* User need not call if no error is reported */
232                 ioerr->oer_errno = 0;
233         }
234 }
235
236 /* Function scheduled on rpc workqueue to call ->nfs_readlist_complete().
237  * This is because the osd completion is called with ints-off from
238  * the block layer
239  */
240 static void _rpc_read_complete(struct work_struct *work)
241 {
242         struct rpc_task *task;
243         struct nfs_read_data *rdata;
244
245         dprintk("%s enter\n", __func__);
246         task = container_of(work, struct rpc_task, u.tk_work);
247         rdata = container_of(task, struct nfs_read_data, task);
248
249         pnfs_ld_read_done(rdata);
250 }
251
252 void
253 objlayout_read_done(struct objlayout_io_res *oir, ssize_t status, bool sync)
254 {
255         struct nfs_read_data *rdata = oir->rpcdata;
256
257         oir->status = rdata->task.tk_status = status;
258         if (status >= 0)
259                 rdata->res.count = status;
260         else
261                 rdata->header->pnfs_error = status;
262         objlayout_iodone(oir);
263         /* must not use oir after this point */
264
265         dprintk("%s: Return status=%zd eof=%d sync=%d\n", __func__,
266                 status, rdata->res.eof, sync);
267
268         if (sync)
269                 pnfs_ld_read_done(rdata);
270         else {
271                 INIT_WORK(&rdata->task.u.tk_work, _rpc_read_complete);
272                 schedule_work(&rdata->task.u.tk_work);
273         }
274 }
275
276 /*
277  * Perform sync or async reads.
278  */
279 enum pnfs_try_status
280 objlayout_read_pagelist(struct nfs_read_data *rdata)
281 {
282         struct nfs_pgio_header *hdr = rdata->header;
283         struct inode *inode = hdr->inode;
284         loff_t offset = rdata->args.offset;
285         size_t count = rdata->args.count;
286         int err;
287         loff_t eof;
288
289         eof = i_size_read(inode);
290         if (unlikely(offset + count > eof)) {
291                 if (offset >= eof) {
292                         err = 0;
293                         rdata->res.count = 0;
294                         rdata->res.eof = 1;
295                         /*FIXME: do we need to call pnfs_ld_read_done() */
296                         goto out;
297                 }
298                 count = eof - offset;
299         }
300
301         rdata->res.eof = (offset + count) >= eof;
302         _fix_verify_io_params(hdr->lseg, &rdata->args.pages,
303                               &rdata->args.pgbase,
304                               rdata->args.offset, rdata->args.count);
305
306         dprintk("%s: inode(%lx) offset 0x%llx count 0x%Zx eof=%d\n",
307                 __func__, inode->i_ino, offset, count, rdata->res.eof);
308
309         err = objio_read_pagelist(rdata);
310  out:
311         if (unlikely(err)) {
312                 hdr->pnfs_error = err;
313                 dprintk("%s: Returned Error %d\n", __func__, err);
314                 return PNFS_NOT_ATTEMPTED;
315         }
316         return PNFS_ATTEMPTED;
317 }
318
319 /* Function scheduled on rpc workqueue to call ->nfs_writelist_complete().
320  * This is because the osd completion is called with ints-off from
321  * the block layer
322  */
323 static void _rpc_write_complete(struct work_struct *work)
324 {
325         struct rpc_task *task;
326         struct nfs_write_data *wdata;
327
328         dprintk("%s enter\n", __func__);
329         task = container_of(work, struct rpc_task, u.tk_work);
330         wdata = container_of(task, struct nfs_write_data, task);
331
332         pnfs_ld_write_done(wdata);
333 }
334
335 void
336 objlayout_write_done(struct objlayout_io_res *oir, ssize_t status, bool sync)
337 {
338         struct nfs_write_data *wdata = oir->rpcdata;
339
340         oir->status = wdata->task.tk_status = status;
341         if (status >= 0) {
342                 wdata->res.count = status;
343                 wdata->verf.committed = oir->committed;
344         } else {
345                 wdata->header->pnfs_error = status;
346         }
347         objlayout_iodone(oir);
348         /* must not use oir after this point */
349
350         dprintk("%s: Return status %zd committed %d sync=%d\n", __func__,
351                 status, wdata->verf.committed, sync);
352
353         if (sync)
354                 pnfs_ld_write_done(wdata);
355         else {
356                 INIT_WORK(&wdata->task.u.tk_work, _rpc_write_complete);
357                 schedule_work(&wdata->task.u.tk_work);
358         }
359 }
360
361 /*
362  * Perform sync or async writes.
363  */
364 enum pnfs_try_status
365 objlayout_write_pagelist(struct nfs_write_data *wdata,
366                          int how)
367 {
368         struct nfs_pgio_header *hdr = wdata->header;
369         int err;
370
371         _fix_verify_io_params(hdr->lseg, &wdata->args.pages,
372                               &wdata->args.pgbase,
373                               wdata->args.offset, wdata->args.count);
374
375         err = objio_write_pagelist(wdata, how);
376         if (unlikely(err)) {
377                 hdr->pnfs_error = err;
378                 dprintk("%s: Returned Error %d\n", __func__, err);
379                 return PNFS_NOT_ATTEMPTED;
380         }
381         return PNFS_ATTEMPTED;
382 }
383
384 void
385 objlayout_encode_layoutcommit(struct pnfs_layout_hdr *pnfslay,
386                               struct xdr_stream *xdr,
387                               const struct nfs4_layoutcommit_args *args)
388 {
389         struct objlayout *objlay = OBJLAYOUT(pnfslay);
390         struct pnfs_osd_layoutupdate lou;
391         __be32 *start;
392
393         dprintk("%s: Begin\n", __func__);
394
395         spin_lock(&objlay->lock);
396         lou.dsu_valid = (objlay->delta_space_valid == OBJ_DSU_VALID);
397         lou.dsu_delta = objlay->delta_space_used;
398         objlay->delta_space_used = 0;
399         objlay->delta_space_valid = OBJ_DSU_INIT;
400         lou.olu_ioerr_flag = !list_empty(&objlay->err_list);
401         spin_unlock(&objlay->lock);
402
403         start = xdr_reserve_space(xdr, 4);
404
405         BUG_ON(pnfs_osd_xdr_encode_layoutupdate(xdr, &lou));
406
407         *start = cpu_to_be32((xdr->p - start - 1) * 4);
408
409         dprintk("%s: Return delta_space_used %lld err %d\n", __func__,
410                 lou.dsu_delta, lou.olu_ioerr_flag);
411 }
412
413 static int
414 err_prio(u32 oer_errno)
415 {
416         switch (oer_errno) {
417         case 0:
418                 return 0;
419
420         case PNFS_OSD_ERR_RESOURCE:
421                 return OSD_ERR_PRI_RESOURCE;
422         case PNFS_OSD_ERR_BAD_CRED:
423                 return OSD_ERR_PRI_BAD_CRED;
424         case PNFS_OSD_ERR_NO_ACCESS:
425                 return OSD_ERR_PRI_NO_ACCESS;
426         case PNFS_OSD_ERR_UNREACHABLE:
427                 return OSD_ERR_PRI_UNREACHABLE;
428         case PNFS_OSD_ERR_NOT_FOUND:
429                 return OSD_ERR_PRI_NOT_FOUND;
430         case PNFS_OSD_ERR_NO_SPACE:
431                 return OSD_ERR_PRI_NO_SPACE;
432         default:
433                 WARN_ON(1);
434                 /* fallthrough */
435         case PNFS_OSD_ERR_EIO:
436                 return OSD_ERR_PRI_EIO;
437         }
438 }
439
440 static void
441 merge_ioerr(struct pnfs_osd_ioerr *dest_err,
442             const struct pnfs_osd_ioerr *src_err)
443 {
444         u64 dest_end, src_end;
445
446         if (!dest_err->oer_errno) {
447                 *dest_err = *src_err;
448                 /* accumulated device must be blank */
449                 memset(&dest_err->oer_component.oid_device_id, 0,
450                         sizeof(dest_err->oer_component.oid_device_id));
451
452                 return;
453         }
454
455         if (dest_err->oer_component.oid_partition_id !=
456                                 src_err->oer_component.oid_partition_id)
457                 dest_err->oer_component.oid_partition_id = 0;
458
459         if (dest_err->oer_component.oid_object_id !=
460                                 src_err->oer_component.oid_object_id)
461                 dest_err->oer_component.oid_object_id = 0;
462
463         if (dest_err->oer_comp_offset > src_err->oer_comp_offset)
464                 dest_err->oer_comp_offset = src_err->oer_comp_offset;
465
466         dest_end = end_offset(dest_err->oer_comp_offset,
467                               dest_err->oer_comp_length);
468         src_end =  end_offset(src_err->oer_comp_offset,
469                               src_err->oer_comp_length);
470         if (dest_end < src_end)
471                 dest_end = src_end;
472
473         dest_err->oer_comp_length = dest_end - dest_err->oer_comp_offset;
474
475         if ((src_err->oer_iswrite == dest_err->oer_iswrite) &&
476             (err_prio(src_err->oer_errno) > err_prio(dest_err->oer_errno))) {
477                         dest_err->oer_errno = src_err->oer_errno;
478         } else if (src_err->oer_iswrite) {
479                 dest_err->oer_iswrite = true;
480                 dest_err->oer_errno = src_err->oer_errno;
481         }
482 }
483
484 static void
485 encode_accumulated_error(struct objlayout *objlay, __be32 *p)
486 {
487         struct objlayout_io_res *oir, *tmp;
488         struct pnfs_osd_ioerr accumulated_err = {.oer_errno = 0};
489
490         list_for_each_entry_safe(oir, tmp, &objlay->err_list, err_list) {
491                 unsigned i;
492
493                 for (i = 0; i < oir->num_comps; i++) {
494                         struct pnfs_osd_ioerr *ioerr = &oir->ioerrs[i];
495
496                         if (!ioerr->oer_errno)
497                                 continue;
498
499                         printk(KERN_ERR "NFS: %s: err[%d]: errno=%d "
500                                 "is_write=%d dev(%llx:%llx) par=0x%llx "
501                                 "obj=0x%llx offset=0x%llx length=0x%llx\n",
502                                 __func__, i, ioerr->oer_errno,
503                                 ioerr->oer_iswrite,
504                                 _DEVID_LO(&ioerr->oer_component.oid_device_id),
505                                 _DEVID_HI(&ioerr->oer_component.oid_device_id),
506                                 ioerr->oer_component.oid_partition_id,
507                                 ioerr->oer_component.oid_object_id,
508                                 ioerr->oer_comp_offset,
509                                 ioerr->oer_comp_length);
510
511                         merge_ioerr(&accumulated_err, ioerr);
512                 }
513                 list_del(&oir->err_list);
514                 objio_free_result(oir);
515         }
516
517         pnfs_osd_xdr_encode_ioerr(p, &accumulated_err);
518 }
519
520 void
521 objlayout_encode_layoutreturn(struct pnfs_layout_hdr *pnfslay,
522                               struct xdr_stream *xdr,
523                               const struct nfs4_layoutreturn_args *args)
524 {
525         struct objlayout *objlay = OBJLAYOUT(pnfslay);
526         struct objlayout_io_res *oir, *tmp;
527         __be32 *start;
528
529         dprintk("%s: Begin\n", __func__);
530         start = xdr_reserve_space(xdr, 4);
531         BUG_ON(!start);
532
533         spin_lock(&objlay->lock);
534
535         list_for_each_entry_safe(oir, tmp, &objlay->err_list, err_list) {
536                 __be32 *last_xdr = NULL, *p;
537                 unsigned i;
538                 int res = 0;
539
540                 for (i = 0; i < oir->num_comps; i++) {
541                         struct pnfs_osd_ioerr *ioerr = &oir->ioerrs[i];
542
543                         if (!ioerr->oer_errno)
544                                 continue;
545
546                         dprintk("%s: err[%d]: errno=%d is_write=%d "
547                                 "dev(%llx:%llx) par=0x%llx obj=0x%llx "
548                                 "offset=0x%llx length=0x%llx\n",
549                                 __func__, i, ioerr->oer_errno,
550                                 ioerr->oer_iswrite,
551                                 _DEVID_LO(&ioerr->oer_component.oid_device_id),
552                                 _DEVID_HI(&ioerr->oer_component.oid_device_id),
553                                 ioerr->oer_component.oid_partition_id,
554                                 ioerr->oer_component.oid_object_id,
555                                 ioerr->oer_comp_offset,
556                                 ioerr->oer_comp_length);
557
558                         p = pnfs_osd_xdr_ioerr_reserve_space(xdr);
559                         if (unlikely(!p)) {
560                                 res = -E2BIG;
561                                 break; /* accumulated_error */
562                         }
563
564                         last_xdr = p;
565                         pnfs_osd_xdr_encode_ioerr(p, &oir->ioerrs[i]);
566                 }
567
568                 /* TODO: use xdr_write_pages */
569                 if (unlikely(res)) {
570                         /* no space for even one error descriptor */
571                         BUG_ON(!last_xdr);
572
573                         /* we've encountered a situation with lots and lots of
574                          * errors and no space to encode them all. Use the last
575                          * available slot to report the union of all the
576                          * remaining errors.
577                          */
578                         encode_accumulated_error(objlay, last_xdr);
579                         goto loop_done;
580                 }
581                 list_del(&oir->err_list);
582                 objio_free_result(oir);
583         }
584 loop_done:
585         spin_unlock(&objlay->lock);
586
587         *start = cpu_to_be32((xdr->p - start - 1) * 4);
588         dprintk("%s: Return\n", __func__);
589 }
590
591
592 /*
593  * Get Device Info API for io engines
594  */
595 struct objlayout_deviceinfo {
596         struct page *page;
597         struct pnfs_osd_deviceaddr da; /* This must be last */
598 };
599
600 /* Initialize and call nfs_getdeviceinfo, then decode and return a
601  * "struct pnfs_osd_deviceaddr *" Eventually objlayout_put_deviceinfo()
602  * should be called.
603  */
604 int objlayout_get_deviceinfo(struct pnfs_layout_hdr *pnfslay,
605         struct nfs4_deviceid *d_id, struct pnfs_osd_deviceaddr **deviceaddr,
606         gfp_t gfp_flags)
607 {
608         struct objlayout_deviceinfo *odi;
609         struct pnfs_device pd;
610         struct page *page, **pages;
611         u32 *p;
612         int err;
613
614         page = alloc_page(gfp_flags);
615         if (!page)
616                 return -ENOMEM;
617
618         pages = &page;
619         pd.pages = pages;
620
621         memcpy(&pd.dev_id, d_id, sizeof(*d_id));
622         pd.layout_type = LAYOUT_OSD2_OBJECTS;
623         pd.pages = &page;
624         pd.pgbase = 0;
625         pd.pglen = PAGE_SIZE;
626         pd.mincount = 0;
627
628         err = nfs4_proc_getdeviceinfo(NFS_SERVER(pnfslay->plh_inode), &pd);
629         dprintk("%s nfs_getdeviceinfo returned %d\n", __func__, err);
630         if (err)
631                 goto err_out;
632
633         p = page_address(page);
634         odi = kzalloc(sizeof(*odi), gfp_flags);
635         if (!odi) {
636                 err = -ENOMEM;
637                 goto err_out;
638         }
639         pnfs_osd_xdr_decode_deviceaddr(&odi->da, p);
640         odi->page = page;
641         *deviceaddr = &odi->da;
642         return 0;
643
644 err_out:
645         __free_page(page);
646         return err;
647 }
648
649 void objlayout_put_deviceinfo(struct pnfs_osd_deviceaddr *deviceaddr)
650 {
651         struct objlayout_deviceinfo *odi = container_of(deviceaddr,
652                                                 struct objlayout_deviceinfo,
653                                                 da);
654
655         __free_page(odi->page);
656         kfree(odi);
657 }
658
659 enum {
660         OBJLAYOUT_MAX_URI_LEN = 256, OBJLAYOUT_MAX_OSDNAME_LEN = 64,
661         OBJLAYOUT_MAX_SYSID_HEX_LEN = OSD_SYSTEMID_LEN * 2 + 1,
662         OSD_LOGIN_UPCALL_PATHLEN  = 256
663 };
664
665 static char osd_login_prog[OSD_LOGIN_UPCALL_PATHLEN] = "/sbin/osd_login";
666
667 module_param_string(osd_login_prog, osd_login_prog, sizeof(osd_login_prog),
668                     0600);
669 MODULE_PARM_DESC(osd_login_prog, "Path to the osd_login upcall program");
670
671 struct __auto_login {
672         char uri[OBJLAYOUT_MAX_URI_LEN];
673         char osdname[OBJLAYOUT_MAX_OSDNAME_LEN];
674         char systemid_hex[OBJLAYOUT_MAX_SYSID_HEX_LEN];
675 };
676
677 static int __objlayout_upcall(struct __auto_login *login)
678 {
679         static char *envp[] = { "HOME=/",
680                 "TERM=linux",
681                 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
682                 NULL
683         };
684         char *argv[8];
685         int ret;
686
687         if (unlikely(!osd_login_prog[0])) {
688                 dprintk("%s: osd_login_prog is disabled\n", __func__);
689                 return -EACCES;
690         }
691
692         dprintk("%s uri: %s\n", __func__, login->uri);
693         dprintk("%s osdname %s\n", __func__, login->osdname);
694         dprintk("%s systemid_hex %s\n", __func__, login->systemid_hex);
695
696         argv[0] = (char *)osd_login_prog;
697         argv[1] = "-u";
698         argv[2] = login->uri;
699         argv[3] = "-o";
700         argv[4] = login->osdname;
701         argv[5] = "-s";
702         argv[6] = login->systemid_hex;
703         argv[7] = NULL;
704
705         ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
706         /*
707          * Disable the upcall mechanism if we're getting an ENOENT or
708          * EACCES error. The admin can re-enable it on the fly by using
709          * sysfs to set the objlayoutdriver.osd_login_prog module parameter once
710          * the problem has been fixed.
711          */
712         if (ret == -ENOENT || ret == -EACCES) {
713                 printk(KERN_ERR "PNFS-OBJ: %s was not found please set "
714                         "objlayoutdriver.osd_login_prog kernel parameter!\n",
715                         osd_login_prog);
716                 osd_login_prog[0] = '\0';
717         }
718         dprintk("%s %s return value: %d\n", __func__, osd_login_prog, ret);
719
720         return ret;
721 }
722
723 /* Assume dest is all zeros */
724 static void __copy_nfsS_and_zero_terminate(struct nfs4_string s,
725                                            char *dest, int max_len,
726                                            const char *var_name)
727 {
728         if (!s.len)
729                 return;
730
731         if (s.len >= max_len) {
732                 pr_warn_ratelimited(
733                         "objlayout_autologin: %s: s.len(%d) >= max_len(%d)",
734                         var_name, s.len, max_len);
735                 s.len = max_len - 1; /* space for null terminator */
736         }
737
738         memcpy(dest, s.data, s.len);
739 }
740
741 /* Assume sysid is all zeros */
742 static void _sysid_2_hex(struct nfs4_string s,
743                   char sysid[OBJLAYOUT_MAX_SYSID_HEX_LEN])
744 {
745         int i;
746         char *cur;
747
748         if (!s.len)
749                 return;
750
751         if (s.len != OSD_SYSTEMID_LEN) {
752                 pr_warn_ratelimited(
753                     "objlayout_autologin: systemid_len(%d) != OSD_SYSTEMID_LEN",
754                     s.len);
755                 if (s.len > OSD_SYSTEMID_LEN)
756                         s.len = OSD_SYSTEMID_LEN;
757         }
758
759         cur = sysid;
760         for (i = 0; i < s.len; i++)
761                 cur = hex_byte_pack(cur, s.data[i]);
762 }
763
764 int objlayout_autologin(struct pnfs_osd_deviceaddr *deviceaddr)
765 {
766         int rc;
767         struct __auto_login login;
768
769         if (!deviceaddr->oda_targetaddr.ota_netaddr.r_addr.len)
770                 return -ENODEV;
771
772         memset(&login, 0, sizeof(login));
773         __copy_nfsS_and_zero_terminate(
774                 deviceaddr->oda_targetaddr.ota_netaddr.r_addr,
775                 login.uri, sizeof(login.uri), "URI");
776
777         __copy_nfsS_and_zero_terminate(
778                 deviceaddr->oda_osdname,
779                 login.osdname, sizeof(login.osdname), "OSDNAME");
780
781         _sysid_2_hex(deviceaddr->oda_systemid, login.systemid_hex);
782
783         rc = __objlayout_upcall(&login);
784         if (rc > 0) /* script returns positive values */
785                 rc = -ENODEV;
786
787         return rc;
788 }