Boaz Harrosh | 09f5bf4 | 2011-05-22 19:50:20 +0300 | [diff] [blame] | 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 | /* |
Benny Halevy | e51b841 | 2011-05-22 19:51:48 +0300 | [diff] [blame^] | 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 | dprintk("%s: Return %p\n", __func__, objlay); |
| 54 | return &objlay->pnfs_layout; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Free an objlayout layout structure |
| 59 | */ |
| 60 | void |
| 61 | objlayout_free_layout_hdr(struct pnfs_layout_hdr *lo) |
| 62 | { |
| 63 | struct objlayout *objlay = OBJLAYOUT(lo); |
| 64 | |
| 65 | dprintk("%s: objlay %p\n", __func__, objlay); |
| 66 | |
| 67 | kfree(objlay); |
| 68 | } |
| 69 | |
| 70 | /* |
Boaz Harrosh | 09f5bf4 | 2011-05-22 19:50:20 +0300 | [diff] [blame] | 71 | * Unmarshall layout and store it in pnfslay. |
| 72 | */ |
| 73 | struct pnfs_layout_segment * |
| 74 | objlayout_alloc_lseg(struct pnfs_layout_hdr *pnfslay, |
| 75 | struct nfs4_layoutget_res *lgr, |
| 76 | gfp_t gfp_flags) |
| 77 | { |
| 78 | int status = -ENOMEM; |
| 79 | struct xdr_stream stream; |
| 80 | struct xdr_buf buf = { |
| 81 | .pages = lgr->layoutp->pages, |
| 82 | .page_len = lgr->layoutp->len, |
| 83 | .buflen = lgr->layoutp->len, |
| 84 | .len = lgr->layoutp->len, |
| 85 | }; |
| 86 | struct page *scratch; |
| 87 | struct pnfs_layout_segment *lseg; |
| 88 | |
| 89 | dprintk("%s: Begin pnfslay %p\n", __func__, pnfslay); |
| 90 | |
| 91 | scratch = alloc_page(gfp_flags); |
| 92 | if (!scratch) |
| 93 | goto err_nofree; |
| 94 | |
| 95 | xdr_init_decode(&stream, &buf, NULL); |
| 96 | xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); |
| 97 | |
| 98 | status = objio_alloc_lseg(&lseg, pnfslay, &lgr->range, &stream, gfp_flags); |
| 99 | if (unlikely(status)) { |
| 100 | dprintk("%s: objio_alloc_lseg Return err %d\n", __func__, |
| 101 | status); |
| 102 | goto err; |
| 103 | } |
| 104 | |
| 105 | __free_page(scratch); |
| 106 | |
| 107 | dprintk("%s: Return %p\n", __func__, lseg); |
| 108 | return lseg; |
| 109 | |
| 110 | err: |
| 111 | __free_page(scratch); |
| 112 | err_nofree: |
| 113 | dprintk("%s: Err Return=>%d\n", __func__, status); |
| 114 | return ERR_PTR(status); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Free a layout segement |
| 119 | */ |
| 120 | void |
| 121 | objlayout_free_lseg(struct pnfs_layout_segment *lseg) |
| 122 | { |
| 123 | dprintk("%s: freeing layout segment %p\n", __func__, lseg); |
| 124 | |
| 125 | if (unlikely(!lseg)) |
| 126 | return; |
| 127 | |
| 128 | objio_free_lseg(lseg); |
| 129 | } |
| 130 | |
Boaz Harrosh | b6c05f1 | 2011-05-26 21:45:34 +0300 | [diff] [blame] | 131 | /* |
| 132 | * Get Device Info API for io engines |
| 133 | */ |
| 134 | struct objlayout_deviceinfo { |
| 135 | struct page *page; |
| 136 | struct pnfs_osd_deviceaddr da; /* This must be last */ |
| 137 | }; |
| 138 | |
| 139 | /* Initialize and call nfs_getdeviceinfo, then decode and return a |
| 140 | * "struct pnfs_osd_deviceaddr *" Eventually objlayout_put_deviceinfo() |
| 141 | * should be called. |
| 142 | */ |
| 143 | int objlayout_get_deviceinfo(struct pnfs_layout_hdr *pnfslay, |
| 144 | struct nfs4_deviceid *d_id, struct pnfs_osd_deviceaddr **deviceaddr, |
| 145 | gfp_t gfp_flags) |
| 146 | { |
| 147 | struct objlayout_deviceinfo *odi; |
| 148 | struct pnfs_device pd; |
| 149 | struct super_block *sb; |
| 150 | struct page *page, **pages; |
| 151 | u32 *p; |
| 152 | int err; |
| 153 | |
| 154 | page = alloc_page(gfp_flags); |
| 155 | if (!page) |
| 156 | return -ENOMEM; |
| 157 | |
| 158 | pages = &page; |
| 159 | pd.pages = pages; |
| 160 | |
| 161 | memcpy(&pd.dev_id, d_id, sizeof(*d_id)); |
| 162 | pd.layout_type = LAYOUT_OSD2_OBJECTS; |
| 163 | pd.pages = &page; |
| 164 | pd.pgbase = 0; |
| 165 | pd.pglen = PAGE_SIZE; |
| 166 | pd.mincount = 0; |
| 167 | |
| 168 | sb = pnfslay->plh_inode->i_sb; |
| 169 | err = nfs4_proc_getdeviceinfo(NFS_SERVER(pnfslay->plh_inode), &pd); |
| 170 | dprintk("%s nfs_getdeviceinfo returned %d\n", __func__, err); |
| 171 | if (err) |
| 172 | goto err_out; |
| 173 | |
| 174 | p = page_address(page); |
| 175 | odi = kzalloc(sizeof(*odi), gfp_flags); |
| 176 | if (!odi) { |
| 177 | err = -ENOMEM; |
| 178 | goto err_out; |
| 179 | } |
| 180 | pnfs_osd_xdr_decode_deviceaddr(&odi->da, p); |
| 181 | odi->page = page; |
| 182 | *deviceaddr = &odi->da; |
| 183 | return 0; |
| 184 | |
| 185 | err_out: |
| 186 | __free_page(page); |
| 187 | return err; |
| 188 | } |
| 189 | |
| 190 | void objlayout_put_deviceinfo(struct pnfs_osd_deviceaddr *deviceaddr) |
| 191 | { |
| 192 | struct objlayout_deviceinfo *odi = container_of(deviceaddr, |
| 193 | struct objlayout_deviceinfo, |
| 194 | da); |
| 195 | |
| 196 | __free_page(odi->page); |
| 197 | kfree(odi); |
| 198 | } |