Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Device operations for the pnfs nfs4 file layout driver. |
| 3 | * |
| 4 | * Copyright (c) 2002 |
| 5 | * The Regents of the University of Michigan |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * Dean Hildebrand <dhildebz@umich.edu> |
| 9 | * Garth Goodson <Garth.Goodson@netapp.com> |
| 10 | * |
| 11 | * Permission is granted to use, copy, create derivative works, and |
| 12 | * redistribute this software and such derivative works for any purpose, |
| 13 | * so long as the name of the University of Michigan is not used in |
| 14 | * any advertising or publicity pertaining to the use or distribution |
| 15 | * of this software without specific, written prior authorization. If |
| 16 | * the above copyright notice or any other identification of the |
| 17 | * University of Michigan is included in any copy of any portion of |
| 18 | * this software, then the disclaimer below must also be included. |
| 19 | * |
| 20 | * This software is provided as is, without representation or warranty |
| 21 | * of any kind either express or implied, including without limitation |
| 22 | * the implied warranties of merchantability, fitness for a particular |
| 23 | * purpose, or noninfringement. The Regents of the University of |
| 24 | * Michigan shall not be liable for any damages, including special, |
| 25 | * indirect, incidental, or consequential damages, with respect to any |
| 26 | * claim arising out of or in connection with the use of the software, |
| 27 | * even if it has been or is hereafter advised of the possibility of |
| 28 | * such damages. |
| 29 | */ |
| 30 | |
| 31 | #include <linux/nfs_fs.h> |
| 32 | #include <linux/vmalloc.h> |
Andy Adamson | 98fc685 | 2012-04-27 17:53:45 -0400 | [diff] [blame] | 33 | #include <linux/module.h> |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 34 | |
| 35 | #include "internal.h" |
| 36 | #include "nfs4filelayout.h" |
| 37 | |
| 38 | #define NFSDBG_FACILITY NFSDBG_PNFS_LD |
| 39 | |
Andy Adamson | 98fc685 | 2012-04-27 17:53:45 -0400 | [diff] [blame] | 40 | static unsigned int dataserver_timeo = NFS4_DEF_DS_TIMEO; |
| 41 | static unsigned int dataserver_retrans = NFS4_DEF_DS_RETRANS; |
| 42 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 43 | /* |
| 44 | * Data server cache |
| 45 | * |
| 46 | * Data servers can be mapped to different device ids. |
| 47 | * nfs4_pnfs_ds reference counting |
| 48 | * - set to 1 on allocation |
| 49 | * - incremented when a device id maps a data server already in the cache. |
| 50 | * - decremented when deviceid is removed from the cache. |
| 51 | */ |
Trond Myklebust | 1728017 | 2012-03-11 13:11:00 -0400 | [diff] [blame] | 52 | static DEFINE_SPINLOCK(nfs4_ds_cache_lock); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 53 | static LIST_HEAD(nfs4_data_server_cache); |
| 54 | |
| 55 | /* Debug routines */ |
| 56 | void |
| 57 | print_ds(struct nfs4_pnfs_ds *ds) |
| 58 | { |
| 59 | if (ds == NULL) { |
| 60 | printk("%s NULL device\n", __func__); |
| 61 | return; |
| 62 | } |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 63 | printk(" ds %s\n" |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 64 | " ref count %d\n" |
| 65 | " client %p\n" |
| 66 | " cl_exchange_flags %x\n", |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 67 | ds->ds_remotestr, |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 68 | atomic_read(&ds->ds_count), ds->ds_clp, |
| 69 | ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0); |
| 70 | } |
| 71 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 72 | static bool |
| 73 | same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 74 | { |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 75 | struct sockaddr_in *a, *b; |
| 76 | struct sockaddr_in6 *a6, *b6; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 77 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 78 | if (addr1->sa_family != addr2->sa_family) |
| 79 | return false; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 80 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 81 | switch (addr1->sa_family) { |
| 82 | case AF_INET: |
| 83 | a = (struct sockaddr_in *)addr1; |
| 84 | b = (struct sockaddr_in *)addr2; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 85 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 86 | if (a->sin_addr.s_addr == b->sin_addr.s_addr && |
| 87 | a->sin_port == b->sin_port) |
| 88 | return true; |
| 89 | break; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 90 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 91 | case AF_INET6: |
| 92 | a6 = (struct sockaddr_in6 *)addr1; |
| 93 | b6 = (struct sockaddr_in6 *)addr2; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 94 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 95 | /* LINKLOCAL addresses must have matching scope_id */ |
| 96 | if (ipv6_addr_scope(&a6->sin6_addr) == |
| 97 | IPV6_ADDR_SCOPE_LINKLOCAL && |
| 98 | a6->sin6_scope_id != b6->sin6_scope_id) |
| 99 | return false; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 100 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 101 | if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) && |
| 102 | a6->sin6_port == b6->sin6_port) |
| 103 | return true; |
| 104 | break; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 105 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 106 | default: |
| 107 | dprintk("%s: unhandled address family: %u\n", |
| 108 | __func__, addr1->sa_family); |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | return false; |
| 113 | } |
| 114 | |
Trond Myklebust | 1728017 | 2012-03-11 13:11:00 -0400 | [diff] [blame] | 115 | static bool |
Weston Andros Adamson | 2d3fe01 | 2012-02-03 15:45:40 -0500 | [diff] [blame] | 116 | _same_data_server_addrs_locked(const struct list_head *dsaddrs1, |
| 117 | const struct list_head *dsaddrs2) |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 118 | { |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 119 | struct nfs4_pnfs_ds_addr *da1, *da2; |
| 120 | |
Weston Andros Adamson | 2d3fe01 | 2012-02-03 15:45:40 -0500 | [diff] [blame] | 121 | /* step through both lists, comparing as we go */ |
| 122 | for (da1 = list_first_entry(dsaddrs1, typeof(*da1), da_node), |
| 123 | da2 = list_first_entry(dsaddrs2, typeof(*da2), da_node); |
| 124 | da1 != NULL && da2 != NULL; |
| 125 | da1 = list_entry(da1->da_node.next, typeof(*da1), da_node), |
| 126 | da2 = list_entry(da2->da_node.next, typeof(*da2), da_node)) { |
| 127 | if (!same_sockaddr((struct sockaddr *)&da1->da_addr, |
| 128 | (struct sockaddr *)&da2->da_addr)) |
| 129 | return false; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 130 | } |
Weston Andros Adamson | 2d3fe01 | 2012-02-03 15:45:40 -0500 | [diff] [blame] | 131 | if (da1 == NULL && da2 == NULL) |
| 132 | return true; |
| 133 | |
| 134 | return false; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 137 | /* |
Weston Andros Adamson | 2d3fe01 | 2012-02-03 15:45:40 -0500 | [diff] [blame] | 138 | * Lookup DS by addresses. nfs4_ds_cache_lock is held |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 139 | */ |
Weston Andros Adamson | 2d3fe01 | 2012-02-03 15:45:40 -0500 | [diff] [blame] | 140 | static struct nfs4_pnfs_ds * |
| 141 | _data_server_lookup_locked(const struct list_head *dsaddrs) |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 142 | { |
Weston Andros Adamson | 2d3fe01 | 2012-02-03 15:45:40 -0500 | [diff] [blame] | 143 | struct nfs4_pnfs_ds *ds; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 144 | |
Weston Andros Adamson | 2d3fe01 | 2012-02-03 15:45:40 -0500 | [diff] [blame] | 145 | list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) |
| 146 | if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs)) |
| 147 | return ds; |
| 148 | return NULL; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* |
Andy Adamson | b4a2967 | 2012-04-27 17:53:52 -0400 | [diff] [blame] | 152 | * Lookup DS by nfs_client pointer. Zero data server client pointer |
| 153 | */ |
| 154 | void nfs4_ds_disconnect(struct nfs_client *clp) |
| 155 | { |
| 156 | struct nfs4_pnfs_ds *ds; |
| 157 | struct nfs_client *found = NULL; |
| 158 | |
| 159 | dprintk("%s clp %p\n", __func__, clp); |
| 160 | spin_lock(&nfs4_ds_cache_lock); |
| 161 | list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) |
| 162 | if (ds->ds_clp && ds->ds_clp == clp) { |
| 163 | found = ds->ds_clp; |
| 164 | ds->ds_clp = NULL; |
| 165 | } |
| 166 | spin_unlock(&nfs4_ds_cache_lock); |
| 167 | if (found) { |
| 168 | set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state); |
| 169 | nfs_put_client(clp); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 174 | * Create an rpc connection to the nfs4_pnfs_ds data server |
Weston Andros Adamson | 35dbbc9 | 2011-06-01 16:32:21 -0400 | [diff] [blame] | 175 | * Currently only supports IPv4 and IPv6 addresses |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 176 | */ |
| 177 | static int |
| 178 | nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds) |
| 179 | { |
Weston Andros Adamson | 7e574f0 | 2011-05-31 18:48:58 -0400 | [diff] [blame] | 180 | struct nfs_client *clp = ERR_PTR(-EIO); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 181 | struct nfs4_pnfs_ds_addr *da; |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 182 | int status = 0; |
| 183 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 184 | dprintk("--> %s DS %s au_flavor %d\n", __func__, ds->ds_remotestr, |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 185 | mds_srv->nfs_client->cl_rpcclient->cl_auth->au_flavor); |
| 186 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 187 | BUG_ON(list_empty(&ds->ds_addrs)); |
| 188 | |
Weston Andros Adamson | 7e574f0 | 2011-05-31 18:48:58 -0400 | [diff] [blame] | 189 | list_for_each_entry(da, &ds->ds_addrs, da_node) { |
| 190 | dprintk("%s: DS %s: trying address %s\n", |
| 191 | __func__, ds->ds_remotestr, da->da_remotestr); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 192 | |
Weston Andros Adamson | 7e574f0 | 2011-05-31 18:48:58 -0400 | [diff] [blame] | 193 | clp = nfs4_set_ds_client(mds_srv->nfs_client, |
Andy Adamson | 98fc685 | 2012-04-27 17:53:45 -0400 | [diff] [blame] | 194 | (struct sockaddr *)&da->da_addr, |
| 195 | da->da_addrlen, IPPROTO_TCP, |
| 196 | dataserver_timeo, dataserver_retrans); |
Weston Andros Adamson | 7e574f0 | 2011-05-31 18:48:58 -0400 | [diff] [blame] | 197 | if (!IS_ERR(clp)) |
| 198 | break; |
| 199 | } |
| 200 | |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 201 | if (IS_ERR(clp)) { |
| 202 | status = PTR_ERR(clp); |
| 203 | goto out; |
| 204 | } |
| 205 | |
Trond Myklebust | 7b38c36 | 2012-05-23 13:23:31 -0400 | [diff] [blame] | 206 | status = nfs4_init_ds_session(clp, mds_srv->nfs_client->cl_lease_time); |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 207 | if (status) |
| 208 | goto out_put; |
| 209 | |
| 210 | ds->ds_clp = clp; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 211 | dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr); |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 212 | out: |
| 213 | return status; |
| 214 | out_put: |
| 215 | nfs_put_client(clp); |
| 216 | goto out; |
| 217 | } |
| 218 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 219 | static void |
| 220 | destroy_ds(struct nfs4_pnfs_ds *ds) |
| 221 | { |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 222 | struct nfs4_pnfs_ds_addr *da; |
| 223 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 224 | dprintk("--> %s\n", __func__); |
| 225 | ifdebug(FACILITY) |
| 226 | print_ds(ds); |
| 227 | |
| 228 | if (ds->ds_clp) |
| 229 | nfs_put_client(ds->ds_clp); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 230 | |
| 231 | while (!list_empty(&ds->ds_addrs)) { |
| 232 | da = list_first_entry(&ds->ds_addrs, |
| 233 | struct nfs4_pnfs_ds_addr, |
| 234 | da_node); |
| 235 | list_del_init(&da->da_node); |
| 236 | kfree(da->da_remotestr); |
| 237 | kfree(da); |
| 238 | } |
| 239 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 240 | kfree(ds->ds_remotestr); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 241 | kfree(ds); |
| 242 | } |
| 243 | |
Benny Halevy | 1775bc3 | 2011-05-20 13:47:33 +0200 | [diff] [blame] | 244 | void |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 245 | nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr) |
| 246 | { |
| 247 | struct nfs4_pnfs_ds *ds; |
| 248 | int i; |
| 249 | |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 250 | nfs4_print_deviceid(&dsaddr->id_node.deviceid); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 251 | |
| 252 | for (i = 0; i < dsaddr->ds_num; i++) { |
| 253 | ds = dsaddr->ds_list[i]; |
| 254 | if (ds != NULL) { |
| 255 | if (atomic_dec_and_lock(&ds->ds_count, |
| 256 | &nfs4_ds_cache_lock)) { |
| 257 | list_del_init(&ds->ds_node); |
| 258 | spin_unlock(&nfs4_ds_cache_lock); |
| 259 | destroy_ds(ds); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | kfree(dsaddr->stripe_indices); |
| 264 | kfree(dsaddr); |
| 265 | } |
| 266 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 267 | /* |
| 268 | * Create a string with a human readable address and port to avoid |
| 269 | * complicated setup around many dprinks. |
| 270 | */ |
| 271 | static char * |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 272 | nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 273 | { |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 274 | struct nfs4_pnfs_ds_addr *da; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 275 | char *remotestr; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 276 | size_t len; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 277 | char *p; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 278 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 279 | len = 3; /* '{', '}' and eol */ |
| 280 | list_for_each_entry(da, dsaddrs, da_node) { |
| 281 | len += strlen(da->da_remotestr) + 1; /* string plus comma */ |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 282 | } |
| 283 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 284 | remotestr = kzalloc(len, gfp_flags); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 285 | if (!remotestr) |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 286 | return NULL; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 287 | |
| 288 | p = remotestr; |
| 289 | *(p++) = '{'; |
| 290 | len--; |
| 291 | list_for_each_entry(da, dsaddrs, da_node) { |
| 292 | size_t ll = strlen(da->da_remotestr); |
| 293 | |
| 294 | if (ll > len) |
| 295 | goto out_err; |
| 296 | |
| 297 | memcpy(p, da->da_remotestr, ll); |
| 298 | p += ll; |
| 299 | len -= ll; |
| 300 | |
| 301 | if (len < 1) |
| 302 | goto out_err; |
| 303 | (*p++) = ','; |
| 304 | len--; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 305 | } |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 306 | if (len < 2) |
| 307 | goto out_err; |
| 308 | *(p++) = '}'; |
| 309 | *p = '\0'; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 310 | return remotestr; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 311 | out_err: |
| 312 | kfree(remotestr); |
| 313 | return NULL; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static struct nfs4_pnfs_ds * |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 317 | nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags) |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 318 | { |
| 319 | struct nfs4_pnfs_ds *tmp_ds, *ds = NULL; |
| 320 | char *remotestr; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 321 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 322 | if (list_empty(dsaddrs)) { |
| 323 | dprintk("%s: no addresses defined\n", __func__); |
| 324 | goto out; |
| 325 | } |
| 326 | |
| 327 | ds = kzalloc(sizeof(*ds), gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 328 | if (!ds) |
| 329 | goto out; |
| 330 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 331 | /* this is only used for debugging, so it's ok if its NULL */ |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 332 | remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 333 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 334 | spin_lock(&nfs4_ds_cache_lock); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 335 | tmp_ds = _data_server_lookup_locked(dsaddrs); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 336 | if (tmp_ds == NULL) { |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 337 | INIT_LIST_HEAD(&ds->ds_addrs); |
| 338 | list_splice_init(dsaddrs, &ds->ds_addrs); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 339 | ds->ds_remotestr = remotestr; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 340 | atomic_set(&ds->ds_count, 1); |
| 341 | INIT_LIST_HEAD(&ds->ds_node); |
| 342 | ds->ds_clp = NULL; |
| 343 | list_add(&ds->ds_node, &nfs4_data_server_cache); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 344 | dprintk("%s add new data server %s\n", __func__, |
| 345 | ds->ds_remotestr); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 346 | } else { |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 347 | kfree(remotestr); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 348 | kfree(ds); |
| 349 | atomic_inc(&tmp_ds->ds_count); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 350 | dprintk("%s data server %s found, inc'ed ds_count to %d\n", |
| 351 | __func__, tmp_ds->ds_remotestr, |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 352 | atomic_read(&tmp_ds->ds_count)); |
| 353 | ds = tmp_ds; |
| 354 | } |
| 355 | spin_unlock(&nfs4_ds_cache_lock); |
| 356 | out: |
| 357 | return ds; |
| 358 | } |
| 359 | |
| 360 | /* |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 361 | * Currently only supports ipv4, ipv6 and one multi-path address. |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 362 | */ |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 363 | static struct nfs4_pnfs_ds_addr * |
Stanislav Kinsbursky | 1709427 | 2012-01-19 19:05:57 +0400 | [diff] [blame] | 364 | decode_ds_addr(struct net *net, struct xdr_stream *streamp, gfp_t gfp_flags) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 365 | { |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 366 | struct nfs4_pnfs_ds_addr *da = NULL; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 367 | char *buf, *portstr; |
Dan Carpenter | 13fff2f | 2012-01-12 10:07:35 +0300 | [diff] [blame] | 368 | __be16 port; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 369 | int nlen, rlen; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 370 | int tmp[2]; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 371 | __be32 *p; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 372 | char *netid, *match_netid; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 373 | size_t len, match_netid_len; |
| 374 | char *startsep = ""; |
| 375 | char *endsep = ""; |
| 376 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 377 | |
| 378 | /* r_netid */ |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 379 | p = xdr_inline_decode(streamp, 4); |
| 380 | if (unlikely(!p)) |
| 381 | goto out_err; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 382 | nlen = be32_to_cpup(p++); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 383 | |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 384 | p = xdr_inline_decode(streamp, nlen); |
| 385 | if (unlikely(!p)) |
| 386 | goto out_err; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 387 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 388 | netid = kmalloc(nlen+1, gfp_flags); |
| 389 | if (unlikely(!netid)) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 390 | goto out_err; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 391 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 392 | netid[nlen] = '\0'; |
| 393 | memcpy(netid, p, nlen); |
| 394 | |
| 395 | /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */ |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 396 | p = xdr_inline_decode(streamp, 4); |
| 397 | if (unlikely(!p)) |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 398 | goto out_free_netid; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 399 | rlen = be32_to_cpup(p); |
| 400 | |
| 401 | p = xdr_inline_decode(streamp, rlen); |
| 402 | if (unlikely(!p)) |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 403 | goto out_free_netid; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 404 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 405 | /* port is ".ABC.DEF", 8 chars max */ |
| 406 | if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) { |
Jesper Juhl | ad3d2ee | 2011-01-17 18:41:50 +0000 | [diff] [blame] | 407 | dprintk("%s: Invalid address, length %d\n", __func__, |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 408 | rlen); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 409 | goto out_free_netid; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 410 | } |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 411 | buf = kmalloc(rlen + 1, gfp_flags); |
Stanislav Fomichev | b9f8105 | 2011-02-05 23:13:01 +0000 | [diff] [blame] | 412 | if (!buf) { |
| 413 | dprintk("%s: Not enough memory\n", __func__); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 414 | goto out_free_netid; |
Stanislav Fomichev | b9f8105 | 2011-02-05 23:13:01 +0000 | [diff] [blame] | 415 | } |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 416 | buf[rlen] = '\0'; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 417 | memcpy(buf, p, rlen); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 418 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 419 | /* replace port '.' with '-' */ |
| 420 | portstr = strrchr(buf, '.'); |
| 421 | if (!portstr) { |
| 422 | dprintk("%s: Failed finding expected dot in port\n", |
| 423 | __func__); |
| 424 | goto out_free_buf; |
| 425 | } |
| 426 | *portstr = '-'; |
| 427 | |
| 428 | /* find '.' between address and port */ |
| 429 | portstr = strrchr(buf, '.'); |
| 430 | if (!portstr) { |
| 431 | dprintk("%s: Failed finding expected dot between address and " |
| 432 | "port\n", __func__); |
| 433 | goto out_free_buf; |
| 434 | } |
| 435 | *portstr = '\0'; |
| 436 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 437 | da = kzalloc(sizeof(*da), gfp_flags); |
| 438 | if (unlikely(!da)) |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 439 | goto out_free_buf; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 440 | |
| 441 | INIT_LIST_HEAD(&da->da_node); |
| 442 | |
Stanislav Kinsbursky | 1709427 | 2012-01-19 19:05:57 +0400 | [diff] [blame] | 443 | if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr, |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 444 | sizeof(da->da_addr))) { |
| 445 | dprintk("%s: error parsing address %s\n", __func__, buf); |
| 446 | goto out_free_da; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 447 | } |
| 448 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 449 | portstr++; |
| 450 | sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 451 | port = htons((tmp[0] << 8) | (tmp[1])); |
| 452 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 453 | switch (da->da_addr.ss_family) { |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 454 | case AF_INET: |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 455 | ((struct sockaddr_in *)&da->da_addr)->sin_port = port; |
| 456 | da->da_addrlen = sizeof(struct sockaddr_in); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 457 | match_netid = "tcp"; |
| 458 | match_netid_len = 3; |
| 459 | break; |
| 460 | |
| 461 | case AF_INET6: |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 462 | ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port; |
| 463 | da->da_addrlen = sizeof(struct sockaddr_in6); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 464 | match_netid = "tcp6"; |
| 465 | match_netid_len = 4; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 466 | startsep = "["; |
| 467 | endsep = "]"; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 468 | break; |
| 469 | |
| 470 | default: |
| 471 | dprintk("%s: unsupported address family: %u\n", |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 472 | __func__, da->da_addr.ss_family); |
| 473 | goto out_free_da; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) { |
| 477 | dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n", |
| 478 | __func__, netid, match_netid); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 479 | goto out_free_da; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 480 | } |
| 481 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 482 | /* save human readable address */ |
| 483 | len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7; |
| 484 | da->da_remotestr = kzalloc(len, gfp_flags); |
| 485 | |
| 486 | /* NULL is ok, only used for dprintk */ |
| 487 | if (da->da_remotestr) |
| 488 | snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep, |
| 489 | buf, endsep, ntohs(port)); |
| 490 | |
| 491 | dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr); |
| 492 | kfree(buf); |
| 493 | kfree(netid); |
| 494 | return da; |
| 495 | |
| 496 | out_free_da: |
| 497 | kfree(da); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 498 | out_free_buf: |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 499 | dprintk("%s: Error parsing DS addr: %s\n", __func__, buf); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 500 | kfree(buf); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 501 | out_free_netid: |
| 502 | kfree(netid); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 503 | out_err: |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 504 | return NULL; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /* Decode opaque device data and return the result */ |
| 508 | static struct nfs4_file_layout_dsaddr* |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 509 | decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 510 | { |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 511 | int i; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 512 | u32 cnt, num; |
| 513 | u8 *indexp; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 514 | __be32 *p; |
| 515 | u8 *stripe_indices; |
| 516 | u8 max_stripe_index; |
| 517 | struct nfs4_file_layout_dsaddr *dsaddr = NULL; |
| 518 | struct xdr_stream stream; |
Benny Halevy | f7da7a1 | 2011-05-19 14:16:47 -0400 | [diff] [blame] | 519 | struct xdr_buf buf; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 520 | struct page *scratch; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 521 | struct list_head dsaddrs; |
| 522 | struct nfs4_pnfs_ds_addr *da; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 523 | |
| 524 | /* set up xdr stream */ |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 525 | scratch = alloc_page(gfp_flags); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 526 | if (!scratch) |
| 527 | goto out_err; |
| 528 | |
Benny Halevy | f7da7a1 | 2011-05-19 14:16:47 -0400 | [diff] [blame] | 529 | xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 530 | xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 531 | |
| 532 | /* Get the stripe count (number of stripe index) */ |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 533 | p = xdr_inline_decode(&stream, 4); |
| 534 | if (unlikely(!p)) |
| 535 | goto out_err_free_scratch; |
| 536 | |
| 537 | cnt = be32_to_cpup(p); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 538 | dprintk("%s stripe count %d\n", __func__, cnt); |
| 539 | if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 540 | printk(KERN_WARNING "NFS: %s: stripe count %d greater than " |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 541 | "supported maximum %d\n", __func__, |
| 542 | cnt, NFS4_PNFS_MAX_STRIPE_CNT); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 543 | goto out_err_free_scratch; |
| 544 | } |
| 545 | |
| 546 | /* read stripe indices */ |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 547 | stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 548 | if (!stripe_indices) |
| 549 | goto out_err_free_scratch; |
| 550 | |
| 551 | p = xdr_inline_decode(&stream, cnt << 2); |
| 552 | if (unlikely(!p)) |
| 553 | goto out_err_free_stripe_indices; |
| 554 | |
| 555 | indexp = &stripe_indices[0]; |
| 556 | max_stripe_index = 0; |
| 557 | for (i = 0; i < cnt; i++) { |
| 558 | *indexp = be32_to_cpup(p++); |
| 559 | max_stripe_index = max(max_stripe_index, *indexp); |
| 560 | indexp++; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /* Check the multipath list count */ |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 564 | p = xdr_inline_decode(&stream, 4); |
| 565 | if (unlikely(!p)) |
| 566 | goto out_err_free_stripe_indices; |
| 567 | |
| 568 | num = be32_to_cpup(p); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 569 | dprintk("%s ds_num %u\n", __func__, num); |
| 570 | if (num > NFS4_PNFS_MAX_MULTI_CNT) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 571 | printk(KERN_WARNING "NFS: %s: multipath count %d greater than " |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 572 | "supported maximum %d\n", __func__, |
| 573 | num, NFS4_PNFS_MAX_MULTI_CNT); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 574 | goto out_err_free_stripe_indices; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 575 | } |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 576 | |
| 577 | /* validate stripe indices are all < num */ |
| 578 | if (max_stripe_index >= num) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 579 | printk(KERN_WARNING "NFS: %s: stripe index %u >= num ds %u\n", |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 580 | __func__, max_stripe_index, num); |
| 581 | goto out_err_free_stripe_indices; |
| 582 | } |
| 583 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 584 | dsaddr = kzalloc(sizeof(*dsaddr) + |
| 585 | (sizeof(struct nfs4_pnfs_ds *) * (num - 1)), |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 586 | gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 587 | if (!dsaddr) |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 588 | goto out_err_free_stripe_indices; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 589 | |
| 590 | dsaddr->stripe_count = cnt; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 591 | dsaddr->stripe_indices = stripe_indices; |
| 592 | stripe_indices = NULL; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 593 | dsaddr->ds_num = num; |
Benny Halevy | 1775bc3 | 2011-05-20 13:47:33 +0200 | [diff] [blame] | 594 | nfs4_init_deviceid_node(&dsaddr->id_node, |
| 595 | NFS_SERVER(ino)->pnfs_curr_ld, |
| 596 | NFS_SERVER(ino)->nfs_client, |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 597 | &pdev->dev_id); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 598 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 599 | INIT_LIST_HEAD(&dsaddrs); |
| 600 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 601 | for (i = 0; i < dsaddr->ds_num; i++) { |
| 602 | int j; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 603 | u32 mp_count; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 604 | |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 605 | p = xdr_inline_decode(&stream, 4); |
| 606 | if (unlikely(!p)) |
| 607 | goto out_err_free_deviceid; |
| 608 | |
| 609 | mp_count = be32_to_cpup(p); /* multipath count */ |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 610 | for (j = 0; j < mp_count; j++) { |
Chuck Lever | 73ea666 | 2012-05-21 22:44:50 -0400 | [diff] [blame] | 611 | da = decode_ds_addr(NFS_SERVER(ino)->nfs_client->cl_net, |
Stanislav Kinsbursky | 1709427 | 2012-01-19 19:05:57 +0400 | [diff] [blame] | 612 | &stream, gfp_flags); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 613 | if (da) |
| 614 | list_add_tail(&da->da_node, &dsaddrs); |
| 615 | } |
| 616 | if (list_empty(&dsaddrs)) { |
| 617 | dprintk("%s: no suitable DS addresses found\n", |
| 618 | __func__); |
| 619 | goto out_err_free_deviceid; |
| 620 | } |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 621 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 622 | dsaddr->ds_list[i] = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags); |
| 623 | if (!dsaddr->ds_list[i]) |
| 624 | goto out_err_drain_dsaddrs; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 625 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 626 | /* If DS was already in cache, free ds addrs */ |
| 627 | while (!list_empty(&dsaddrs)) { |
| 628 | da = list_first_entry(&dsaddrs, |
| 629 | struct nfs4_pnfs_ds_addr, |
| 630 | da_node); |
| 631 | list_del_init(&da->da_node); |
| 632 | kfree(da->da_remotestr); |
| 633 | kfree(da); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 634 | } |
| 635 | } |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 636 | |
| 637 | __free_page(scratch); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 638 | return dsaddr; |
| 639 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 640 | out_err_drain_dsaddrs: |
| 641 | while (!list_empty(&dsaddrs)) { |
| 642 | da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr, |
| 643 | da_node); |
| 644 | list_del_init(&da->da_node); |
| 645 | kfree(da->da_remotestr); |
| 646 | kfree(da); |
| 647 | } |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 648 | out_err_free_deviceid: |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 649 | nfs4_fl_free_deviceid(dsaddr); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 650 | /* stripe_indicies was part of dsaddr */ |
| 651 | goto out_err_free_scratch; |
| 652 | out_err_free_stripe_indices: |
| 653 | kfree(stripe_indices); |
| 654 | out_err_free_scratch: |
| 655 | __free_page(scratch); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 656 | out_err: |
| 657 | dprintk("%s ERROR: returning NULL\n", __func__); |
| 658 | return NULL; |
| 659 | } |
| 660 | |
| 661 | /* |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 662 | * Decode the opaque device specified in 'dev' and add it to the cache of |
| 663 | * available devices. |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 664 | */ |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 665 | static struct nfs4_file_layout_dsaddr * |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 666 | decode_and_add_device(struct inode *inode, struct pnfs_device *dev, gfp_t gfp_flags) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 667 | { |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 668 | struct nfs4_deviceid_node *d; |
| 669 | struct nfs4_file_layout_dsaddr *n, *new; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 670 | |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 671 | new = decode_device(inode, dev, gfp_flags); |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 672 | if (!new) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 673 | printk(KERN_WARNING "NFS: %s: Could not decode or add device\n", |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 674 | __func__); |
| 675 | return NULL; |
| 676 | } |
| 677 | |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 678 | d = nfs4_insert_deviceid_node(&new->id_node); |
| 679 | n = container_of(d, struct nfs4_file_layout_dsaddr, id_node); |
| 680 | if (n != new) { |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 681 | nfs4_fl_free_deviceid(new); |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 682 | return n; |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 683 | } |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 684 | |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 685 | return new; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /* |
| 689 | * Retrieve the information for dev_id, add it to the list |
| 690 | * of available devices, and return it. |
| 691 | */ |
| 692 | struct nfs4_file_layout_dsaddr * |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 693 | get_device_info(struct inode *inode, struct nfs4_deviceid *dev_id, gfp_t gfp_flags) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 694 | { |
| 695 | struct pnfs_device *pdev = NULL; |
| 696 | u32 max_resp_sz; |
| 697 | int max_pages; |
| 698 | struct page **pages = NULL; |
| 699 | struct nfs4_file_layout_dsaddr *dsaddr = NULL; |
| 700 | int rc, i; |
| 701 | struct nfs_server *server = NFS_SERVER(inode); |
| 702 | |
| 703 | /* |
| 704 | * Use the session max response size as the basis for setting |
| 705 | * GETDEVICEINFO's maxcount |
| 706 | */ |
| 707 | max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; |
Andy Adamson | e5265a0 | 2012-04-14 03:56:35 -0400 | [diff] [blame] | 708 | max_pages = nfs_page_array_len(0, max_resp_sz); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 709 | dprintk("%s inode %p max_resp_sz %u max_pages %d\n", |
| 710 | __func__, inode, max_resp_sz, max_pages); |
| 711 | |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 712 | pdev = kzalloc(sizeof(struct pnfs_device), gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 713 | if (pdev == NULL) |
| 714 | return NULL; |
| 715 | |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 716 | pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 717 | if (pages == NULL) { |
| 718 | kfree(pdev); |
| 719 | return NULL; |
| 720 | } |
| 721 | for (i = 0; i < max_pages; i++) { |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 722 | pages[i] = alloc_page(gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 723 | if (!pages[i]) |
| 724 | goto out_free; |
| 725 | } |
| 726 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 727 | memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id)); |
| 728 | pdev->layout_type = LAYOUT_NFSV4_1_FILES; |
| 729 | pdev->pages = pages; |
| 730 | pdev->pgbase = 0; |
Andy Adamson | 05bf14a | 2012-06-06 23:57:13 -0400 | [diff] [blame] | 731 | pdev->pglen = max_resp_sz; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 732 | pdev->mincount = 0; |
| 733 | |
| 734 | rc = nfs4_proc_getdeviceinfo(server, pdev); |
| 735 | dprintk("%s getdevice info returns %d\n", __func__, rc); |
| 736 | if (rc) |
| 737 | goto out_free; |
| 738 | |
| 739 | /* |
| 740 | * Found new device, need to decode it and then add it to the |
| 741 | * list of known devices for this mountpoint. |
| 742 | */ |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 743 | dsaddr = decode_and_add_device(inode, pdev, gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 744 | out_free: |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 745 | for (i = 0; i < max_pages; i++) |
| 746 | __free_page(pages[i]); |
| 747 | kfree(pages); |
| 748 | kfree(pdev); |
| 749 | dprintk("<-- %s dsaddr %p\n", __func__, dsaddr); |
| 750 | return dsaddr; |
| 751 | } |
| 752 | |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 753 | void |
| 754 | nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 755 | { |
Benny Halevy | 1775bc3 | 2011-05-20 13:47:33 +0200 | [diff] [blame] | 756 | nfs4_put_deviceid_node(&dsaddr->id_node); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 757 | } |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 758 | |
| 759 | /* |
| 760 | * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit |
| 761 | * Then: ((res + fsi) % dsaddr->stripe_count) |
| 762 | */ |
| 763 | u32 |
| 764 | nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset) |
| 765 | { |
| 766 | struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg); |
| 767 | u64 tmp; |
| 768 | |
| 769 | tmp = offset - flseg->pattern_offset; |
| 770 | do_div(tmp, flseg->stripe_unit); |
| 771 | tmp += flseg->first_stripe_index; |
| 772 | return do_div(tmp, flseg->dsaddr->stripe_count); |
| 773 | } |
| 774 | |
| 775 | u32 |
| 776 | nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j) |
| 777 | { |
| 778 | return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j]; |
| 779 | } |
| 780 | |
| 781 | struct nfs_fh * |
| 782 | nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j) |
| 783 | { |
| 784 | struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg); |
| 785 | u32 i; |
| 786 | |
| 787 | if (flseg->stripe_type == STRIPE_SPARSE) { |
| 788 | if (flseg->num_fh == 1) |
| 789 | i = 0; |
| 790 | else if (flseg->num_fh == 0) |
| 791 | /* Use the MDS OPEN fh set in nfs_read_rpcsetup */ |
| 792 | return NULL; |
| 793 | else |
| 794 | i = nfs4_fl_calc_ds_index(lseg, j); |
| 795 | } else |
| 796 | i = j; |
| 797 | return flseg->fh_array[i]; |
| 798 | } |
| 799 | |
| 800 | struct nfs4_pnfs_ds * |
| 801 | nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx) |
| 802 | { |
| 803 | struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr; |
| 804 | struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx]; |
Andy Adamson | 554d458 | 2012-04-27 17:53:42 -0400 | [diff] [blame] | 805 | struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg); |
| 806 | |
| 807 | if (filelayout_test_devid_invalid(devid)) |
| 808 | return NULL; |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 809 | |
| 810 | if (ds == NULL) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 811 | printk(KERN_ERR "NFS: %s: No data server for offset index %d\n", |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 812 | __func__, ds_idx); |
Andy Adamson | 554d458 | 2012-04-27 17:53:42 -0400 | [diff] [blame] | 813 | goto mark_dev_invalid; |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | if (!ds->ds_clp) { |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 817 | struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode); |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 818 | int err; |
| 819 | |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 820 | err = nfs4_ds_connect(s, ds); |
Andy Adamson | 554d458 | 2012-04-27 17:53:42 -0400 | [diff] [blame] | 821 | if (err) |
| 822 | goto mark_dev_invalid; |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 823 | } |
| 824 | return ds; |
Andy Adamson | 554d458 | 2012-04-27 17:53:42 -0400 | [diff] [blame] | 825 | |
| 826 | mark_dev_invalid: |
| 827 | filelayout_mark_devid_invalid(devid); |
| 828 | return NULL; |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 829 | } |
Andy Adamson | 98fc685 | 2012-04-27 17:53:45 -0400 | [diff] [blame] | 830 | |
| 831 | module_param(dataserver_retrans, uint, 0644); |
| 832 | MODULE_PARM_DESC(dataserver_retrans, "The number of times the NFSv4.1 client " |
| 833 | "retries a request before it attempts further " |
| 834 | " recovery action."); |
| 835 | module_param(dataserver_timeo, uint, 0644); |
| 836 | MODULE_PARM_DESC(dataserver_timeo, "The time (in tenths of a second) the " |
| 837 | "NFSv4.1 client waits for a response from a " |
| 838 | " data server before it retries an NFS request."); |