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 | |
| 206 | if ((clp->cl_exchange_flags & EXCHGID4_FLAG_MASK_PNFS) != 0) { |
| 207 | if (!is_ds_client(clp)) { |
| 208 | status = -ENODEV; |
| 209 | goto out_put; |
| 210 | } |
| 211 | ds->ds_clp = clp; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 212 | dprintk("%s [existing] server=%s\n", __func__, |
| 213 | ds->ds_remotestr); |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 214 | goto out; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Do not set NFS_CS_CHECK_LEASE_TIME instead set the DS lease to |
| 219 | * be equal to the MDS lease. Renewal is scheduled in create_session. |
| 220 | */ |
| 221 | spin_lock(&mds_srv->nfs_client->cl_lock); |
| 222 | clp->cl_lease_time = mds_srv->nfs_client->cl_lease_time; |
| 223 | spin_unlock(&mds_srv->nfs_client->cl_lock); |
| 224 | clp->cl_last_renewal = jiffies; |
| 225 | |
| 226 | /* New nfs_client */ |
| 227 | status = nfs4_init_ds_session(clp); |
| 228 | if (status) |
| 229 | goto out_put; |
| 230 | |
| 231 | ds->ds_clp = clp; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 232 | dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr); |
Andy Adamson | d83217c | 2011-03-01 01:34:17 +0000 | [diff] [blame] | 233 | out: |
| 234 | return status; |
| 235 | out_put: |
| 236 | nfs_put_client(clp); |
| 237 | goto out; |
| 238 | } |
| 239 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 240 | static void |
| 241 | destroy_ds(struct nfs4_pnfs_ds *ds) |
| 242 | { |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 243 | struct nfs4_pnfs_ds_addr *da; |
| 244 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 245 | dprintk("--> %s\n", __func__); |
| 246 | ifdebug(FACILITY) |
| 247 | print_ds(ds); |
| 248 | |
| 249 | if (ds->ds_clp) |
| 250 | nfs_put_client(ds->ds_clp); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 251 | |
| 252 | while (!list_empty(&ds->ds_addrs)) { |
| 253 | da = list_first_entry(&ds->ds_addrs, |
| 254 | struct nfs4_pnfs_ds_addr, |
| 255 | da_node); |
| 256 | list_del_init(&da->da_node); |
| 257 | kfree(da->da_remotestr); |
| 258 | kfree(da); |
| 259 | } |
| 260 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 261 | kfree(ds->ds_remotestr); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 262 | kfree(ds); |
| 263 | } |
| 264 | |
Benny Halevy | 1775bc3 | 2011-05-20 13:47:33 +0200 | [diff] [blame] | 265 | void |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 266 | nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr) |
| 267 | { |
| 268 | struct nfs4_pnfs_ds *ds; |
| 269 | int i; |
| 270 | |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 271 | nfs4_print_deviceid(&dsaddr->id_node.deviceid); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 272 | |
| 273 | for (i = 0; i < dsaddr->ds_num; i++) { |
| 274 | ds = dsaddr->ds_list[i]; |
| 275 | if (ds != NULL) { |
| 276 | if (atomic_dec_and_lock(&ds->ds_count, |
| 277 | &nfs4_ds_cache_lock)) { |
| 278 | list_del_init(&ds->ds_node); |
| 279 | spin_unlock(&nfs4_ds_cache_lock); |
| 280 | destroy_ds(ds); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | kfree(dsaddr->stripe_indices); |
| 285 | kfree(dsaddr); |
| 286 | } |
| 287 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 288 | /* |
| 289 | * Create a string with a human readable address and port to avoid |
| 290 | * complicated setup around many dprinks. |
| 291 | */ |
| 292 | static char * |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 293 | nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 294 | { |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 295 | struct nfs4_pnfs_ds_addr *da; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 296 | char *remotestr; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 297 | size_t len; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 298 | char *p; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 299 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 300 | len = 3; /* '{', '}' and eol */ |
| 301 | list_for_each_entry(da, dsaddrs, da_node) { |
| 302 | len += strlen(da->da_remotestr) + 1; /* string plus comma */ |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 303 | } |
| 304 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 305 | remotestr = kzalloc(len, gfp_flags); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 306 | if (!remotestr) |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 307 | return NULL; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 308 | |
| 309 | p = remotestr; |
| 310 | *(p++) = '{'; |
| 311 | len--; |
| 312 | list_for_each_entry(da, dsaddrs, da_node) { |
| 313 | size_t ll = strlen(da->da_remotestr); |
| 314 | |
| 315 | if (ll > len) |
| 316 | goto out_err; |
| 317 | |
| 318 | memcpy(p, da->da_remotestr, ll); |
| 319 | p += ll; |
| 320 | len -= ll; |
| 321 | |
| 322 | if (len < 1) |
| 323 | goto out_err; |
| 324 | (*p++) = ','; |
| 325 | len--; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 326 | } |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 327 | if (len < 2) |
| 328 | goto out_err; |
| 329 | *(p++) = '}'; |
| 330 | *p = '\0'; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 331 | return remotestr; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 332 | out_err: |
| 333 | kfree(remotestr); |
| 334 | return NULL; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static struct nfs4_pnfs_ds * |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 338 | 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] | 339 | { |
| 340 | struct nfs4_pnfs_ds *tmp_ds, *ds = NULL; |
| 341 | char *remotestr; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 342 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 343 | if (list_empty(dsaddrs)) { |
| 344 | dprintk("%s: no addresses defined\n", __func__); |
| 345 | goto out; |
| 346 | } |
| 347 | |
| 348 | ds = kzalloc(sizeof(*ds), gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 349 | if (!ds) |
| 350 | goto out; |
| 351 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 352 | /* 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] | 353 | remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 354 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 355 | spin_lock(&nfs4_ds_cache_lock); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 356 | tmp_ds = _data_server_lookup_locked(dsaddrs); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 357 | if (tmp_ds == NULL) { |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 358 | INIT_LIST_HEAD(&ds->ds_addrs); |
| 359 | list_splice_init(dsaddrs, &ds->ds_addrs); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 360 | ds->ds_remotestr = remotestr; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 361 | atomic_set(&ds->ds_count, 1); |
| 362 | INIT_LIST_HEAD(&ds->ds_node); |
| 363 | ds->ds_clp = NULL; |
| 364 | list_add(&ds->ds_node, &nfs4_data_server_cache); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 365 | dprintk("%s add new data server %s\n", __func__, |
| 366 | ds->ds_remotestr); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 367 | } else { |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 368 | kfree(remotestr); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 369 | kfree(ds); |
| 370 | atomic_inc(&tmp_ds->ds_count); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 371 | dprintk("%s data server %s found, inc'ed ds_count to %d\n", |
| 372 | __func__, tmp_ds->ds_remotestr, |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 373 | atomic_read(&tmp_ds->ds_count)); |
| 374 | ds = tmp_ds; |
| 375 | } |
| 376 | spin_unlock(&nfs4_ds_cache_lock); |
| 377 | out: |
| 378 | return ds; |
| 379 | } |
| 380 | |
| 381 | /* |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 382 | * Currently only supports ipv4, ipv6 and one multi-path address. |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 383 | */ |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 384 | static struct nfs4_pnfs_ds_addr * |
Stanislav Kinsbursky | 1709427 | 2012-01-19 19:05:57 +0400 | [diff] [blame] | 385 | 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] | 386 | { |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 387 | struct nfs4_pnfs_ds_addr *da = NULL; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 388 | char *buf, *portstr; |
Dan Carpenter | 13fff2f | 2012-01-12 10:07:35 +0300 | [diff] [blame] | 389 | __be16 port; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 390 | int nlen, rlen; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 391 | int tmp[2]; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 392 | __be32 *p; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 393 | char *netid, *match_netid; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 394 | size_t len, match_netid_len; |
| 395 | char *startsep = ""; |
| 396 | char *endsep = ""; |
| 397 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 398 | |
| 399 | /* r_netid */ |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 400 | p = xdr_inline_decode(streamp, 4); |
| 401 | if (unlikely(!p)) |
| 402 | goto out_err; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 403 | nlen = be32_to_cpup(p++); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 404 | |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 405 | p = xdr_inline_decode(streamp, nlen); |
| 406 | if (unlikely(!p)) |
| 407 | goto out_err; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 408 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 409 | netid = kmalloc(nlen+1, gfp_flags); |
| 410 | if (unlikely(!netid)) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 411 | goto out_err; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 412 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 413 | netid[nlen] = '\0'; |
| 414 | memcpy(netid, p, nlen); |
| 415 | |
| 416 | /* 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] | 417 | p = xdr_inline_decode(streamp, 4); |
| 418 | if (unlikely(!p)) |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 419 | goto out_free_netid; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 420 | rlen = be32_to_cpup(p); |
| 421 | |
| 422 | p = xdr_inline_decode(streamp, rlen); |
| 423 | if (unlikely(!p)) |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 424 | goto out_free_netid; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 425 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 426 | /* port is ".ABC.DEF", 8 chars max */ |
| 427 | if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) { |
Jesper Juhl | ad3d2ee | 2011-01-17 18:41:50 +0000 | [diff] [blame] | 428 | dprintk("%s: Invalid address, length %d\n", __func__, |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 429 | rlen); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 430 | goto out_free_netid; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 431 | } |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 432 | buf = kmalloc(rlen + 1, gfp_flags); |
Stanislav Fomichev | b9f8105 | 2011-02-05 23:13:01 +0000 | [diff] [blame] | 433 | if (!buf) { |
| 434 | dprintk("%s: Not enough memory\n", __func__); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 435 | goto out_free_netid; |
Stanislav Fomichev | b9f8105 | 2011-02-05 23:13:01 +0000 | [diff] [blame] | 436 | } |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 437 | buf[rlen] = '\0'; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 438 | memcpy(buf, p, rlen); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 439 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 440 | /* replace port '.' with '-' */ |
| 441 | portstr = strrchr(buf, '.'); |
| 442 | if (!portstr) { |
| 443 | dprintk("%s: Failed finding expected dot in port\n", |
| 444 | __func__); |
| 445 | goto out_free_buf; |
| 446 | } |
| 447 | *portstr = '-'; |
| 448 | |
| 449 | /* find '.' between address and port */ |
| 450 | portstr = strrchr(buf, '.'); |
| 451 | if (!portstr) { |
| 452 | dprintk("%s: Failed finding expected dot between address and " |
| 453 | "port\n", __func__); |
| 454 | goto out_free_buf; |
| 455 | } |
| 456 | *portstr = '\0'; |
| 457 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 458 | da = kzalloc(sizeof(*da), gfp_flags); |
| 459 | if (unlikely(!da)) |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 460 | goto out_free_buf; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 461 | |
| 462 | INIT_LIST_HEAD(&da->da_node); |
| 463 | |
Stanislav Kinsbursky | 1709427 | 2012-01-19 19:05:57 +0400 | [diff] [blame] | 464 | 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] | 465 | sizeof(da->da_addr))) { |
| 466 | dprintk("%s: error parsing address %s\n", __func__, buf); |
| 467 | goto out_free_da; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 468 | } |
| 469 | |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 470 | portstr++; |
| 471 | sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 472 | port = htons((tmp[0] << 8) | (tmp[1])); |
| 473 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 474 | switch (da->da_addr.ss_family) { |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 475 | case AF_INET: |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 476 | ((struct sockaddr_in *)&da->da_addr)->sin_port = port; |
| 477 | da->da_addrlen = sizeof(struct sockaddr_in); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 478 | match_netid = "tcp"; |
| 479 | match_netid_len = 3; |
| 480 | break; |
| 481 | |
| 482 | case AF_INET6: |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 483 | ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port; |
| 484 | da->da_addrlen = sizeof(struct sockaddr_in6); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 485 | match_netid = "tcp6"; |
| 486 | match_netid_len = 4; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 487 | startsep = "["; |
| 488 | endsep = "]"; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 489 | break; |
| 490 | |
| 491 | default: |
| 492 | dprintk("%s: unsupported address family: %u\n", |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 493 | __func__, da->da_addr.ss_family); |
| 494 | goto out_free_da; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) { |
| 498 | dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n", |
| 499 | __func__, netid, match_netid); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 500 | goto out_free_da; |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 501 | } |
| 502 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 503 | /* save human readable address */ |
| 504 | len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7; |
| 505 | da->da_remotestr = kzalloc(len, gfp_flags); |
| 506 | |
| 507 | /* NULL is ok, only used for dprintk */ |
| 508 | if (da->da_remotestr) |
| 509 | snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep, |
| 510 | buf, endsep, ntohs(port)); |
| 511 | |
| 512 | dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr); |
| 513 | kfree(buf); |
| 514 | kfree(netid); |
| 515 | return da; |
| 516 | |
| 517 | out_free_da: |
| 518 | kfree(da); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 519 | out_free_buf: |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 520 | dprintk("%s: Error parsing DS addr: %s\n", __func__, buf); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 521 | kfree(buf); |
Weston Andros Adamson | c9895cb | 2011-05-31 18:48:56 -0400 | [diff] [blame] | 522 | out_free_netid: |
| 523 | kfree(netid); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 524 | out_err: |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 525 | return NULL; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /* Decode opaque device data and return the result */ |
| 529 | static struct nfs4_file_layout_dsaddr* |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 530 | 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] | 531 | { |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 532 | int i; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 533 | u32 cnt, num; |
| 534 | u8 *indexp; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 535 | __be32 *p; |
| 536 | u8 *stripe_indices; |
| 537 | u8 max_stripe_index; |
| 538 | struct nfs4_file_layout_dsaddr *dsaddr = NULL; |
| 539 | struct xdr_stream stream; |
Benny Halevy | f7da7a1 | 2011-05-19 14:16:47 -0400 | [diff] [blame] | 540 | struct xdr_buf buf; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 541 | struct page *scratch; |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 542 | struct list_head dsaddrs; |
| 543 | struct nfs4_pnfs_ds_addr *da; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 544 | |
| 545 | /* set up xdr stream */ |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 546 | scratch = alloc_page(gfp_flags); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 547 | if (!scratch) |
| 548 | goto out_err; |
| 549 | |
Benny Halevy | f7da7a1 | 2011-05-19 14:16:47 -0400 | [diff] [blame] | 550 | xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 551 | xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 552 | |
| 553 | /* Get the stripe count (number of stripe index) */ |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 554 | p = xdr_inline_decode(&stream, 4); |
| 555 | if (unlikely(!p)) |
| 556 | goto out_err_free_scratch; |
| 557 | |
| 558 | cnt = be32_to_cpup(p); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 559 | dprintk("%s stripe count %d\n", __func__, cnt); |
| 560 | if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 561 | printk(KERN_WARNING "NFS: %s: stripe count %d greater than " |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 562 | "supported maximum %d\n", __func__, |
| 563 | cnt, NFS4_PNFS_MAX_STRIPE_CNT); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 564 | goto out_err_free_scratch; |
| 565 | } |
| 566 | |
| 567 | /* read stripe indices */ |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 568 | stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 569 | if (!stripe_indices) |
| 570 | goto out_err_free_scratch; |
| 571 | |
| 572 | p = xdr_inline_decode(&stream, cnt << 2); |
| 573 | if (unlikely(!p)) |
| 574 | goto out_err_free_stripe_indices; |
| 575 | |
| 576 | indexp = &stripe_indices[0]; |
| 577 | max_stripe_index = 0; |
| 578 | for (i = 0; i < cnt; i++) { |
| 579 | *indexp = be32_to_cpup(p++); |
| 580 | max_stripe_index = max(max_stripe_index, *indexp); |
| 581 | indexp++; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /* Check the multipath list count */ |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 585 | p = xdr_inline_decode(&stream, 4); |
| 586 | if (unlikely(!p)) |
| 587 | goto out_err_free_stripe_indices; |
| 588 | |
| 589 | num = be32_to_cpup(p); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 590 | dprintk("%s ds_num %u\n", __func__, num); |
| 591 | if (num > NFS4_PNFS_MAX_MULTI_CNT) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 592 | printk(KERN_WARNING "NFS: %s: multipath count %d greater than " |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 593 | "supported maximum %d\n", __func__, |
| 594 | num, NFS4_PNFS_MAX_MULTI_CNT); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 595 | goto out_err_free_stripe_indices; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 596 | } |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 597 | |
| 598 | /* validate stripe indices are all < num */ |
| 599 | if (max_stripe_index >= num) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 600 | 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] | 601 | __func__, max_stripe_index, num); |
| 602 | goto out_err_free_stripe_indices; |
| 603 | } |
| 604 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 605 | dsaddr = kzalloc(sizeof(*dsaddr) + |
| 606 | (sizeof(struct nfs4_pnfs_ds *) * (num - 1)), |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 607 | gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 608 | if (!dsaddr) |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 609 | goto out_err_free_stripe_indices; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 610 | |
| 611 | dsaddr->stripe_count = cnt; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 612 | dsaddr->stripe_indices = stripe_indices; |
| 613 | stripe_indices = NULL; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 614 | dsaddr->ds_num = num; |
Benny Halevy | 1775bc3 | 2011-05-20 13:47:33 +0200 | [diff] [blame] | 615 | nfs4_init_deviceid_node(&dsaddr->id_node, |
| 616 | NFS_SERVER(ino)->pnfs_curr_ld, |
| 617 | NFS_SERVER(ino)->nfs_client, |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 618 | &pdev->dev_id); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 619 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 620 | INIT_LIST_HEAD(&dsaddrs); |
| 621 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 622 | for (i = 0; i < dsaddr->ds_num; i++) { |
| 623 | int j; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 624 | u32 mp_count; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 625 | |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 626 | p = xdr_inline_decode(&stream, 4); |
| 627 | if (unlikely(!p)) |
| 628 | goto out_err_free_deviceid; |
| 629 | |
| 630 | mp_count = be32_to_cpup(p); /* multipath count */ |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 631 | for (j = 0; j < mp_count; j++) { |
Chuck Lever | 73ea666 | 2012-05-21 22:44:50 -0400 | [diff] [blame^] | 632 | da = decode_ds_addr(NFS_SERVER(ino)->nfs_client->cl_net, |
Stanislav Kinsbursky | 1709427 | 2012-01-19 19:05:57 +0400 | [diff] [blame] | 633 | &stream, gfp_flags); |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 634 | if (da) |
| 635 | list_add_tail(&da->da_node, &dsaddrs); |
| 636 | } |
| 637 | if (list_empty(&dsaddrs)) { |
| 638 | dprintk("%s: no suitable DS addresses found\n", |
| 639 | __func__); |
| 640 | goto out_err_free_deviceid; |
| 641 | } |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 642 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 643 | dsaddr->ds_list[i] = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags); |
| 644 | if (!dsaddr->ds_list[i]) |
| 645 | goto out_err_drain_dsaddrs; |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 646 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 647 | /* If DS was already in cache, free ds addrs */ |
| 648 | while (!list_empty(&dsaddrs)) { |
| 649 | da = list_first_entry(&dsaddrs, |
| 650 | struct nfs4_pnfs_ds_addr, |
| 651 | da_node); |
| 652 | list_del_init(&da->da_node); |
| 653 | kfree(da->da_remotestr); |
| 654 | kfree(da); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 655 | } |
| 656 | } |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 657 | |
| 658 | __free_page(scratch); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 659 | return dsaddr; |
| 660 | |
Weston Andros Adamson | 14f9a60 | 2011-05-31 18:48:57 -0400 | [diff] [blame] | 661 | out_err_drain_dsaddrs: |
| 662 | while (!list_empty(&dsaddrs)) { |
| 663 | da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr, |
| 664 | da_node); |
| 665 | list_del_init(&da->da_node); |
| 666 | kfree(da->da_remotestr); |
| 667 | kfree(da); |
| 668 | } |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 669 | out_err_free_deviceid: |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 670 | nfs4_fl_free_deviceid(dsaddr); |
Weston Andros Adamson | 35124a0 | 2011-03-24 16:48:21 -0400 | [diff] [blame] | 671 | /* stripe_indicies was part of dsaddr */ |
| 672 | goto out_err_free_scratch; |
| 673 | out_err_free_stripe_indices: |
| 674 | kfree(stripe_indices); |
| 675 | out_err_free_scratch: |
| 676 | __free_page(scratch); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 677 | out_err: |
| 678 | dprintk("%s ERROR: returning NULL\n", __func__); |
| 679 | return NULL; |
| 680 | } |
| 681 | |
| 682 | /* |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 683 | * Decode the opaque device specified in 'dev' and add it to the cache of |
| 684 | * available devices. |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 685 | */ |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 686 | static struct nfs4_file_layout_dsaddr * |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 687 | 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] | 688 | { |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 689 | struct nfs4_deviceid_node *d; |
| 690 | struct nfs4_file_layout_dsaddr *n, *new; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 691 | |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 692 | new = decode_device(inode, dev, gfp_flags); |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 693 | if (!new) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 694 | printk(KERN_WARNING "NFS: %s: Could not decode or add device\n", |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 695 | __func__); |
| 696 | return NULL; |
| 697 | } |
| 698 | |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 699 | d = nfs4_insert_deviceid_node(&new->id_node); |
| 700 | n = container_of(d, struct nfs4_file_layout_dsaddr, id_node); |
| 701 | if (n != new) { |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 702 | nfs4_fl_free_deviceid(new); |
Benny Halevy | a1eaecb | 2011-05-19 22:14:47 -0400 | [diff] [blame] | 703 | return n; |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 704 | } |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 705 | |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 706 | return new; |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /* |
| 710 | * Retrieve the information for dev_id, add it to the list |
| 711 | * of available devices, and return it. |
| 712 | */ |
| 713 | struct nfs4_file_layout_dsaddr * |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 714 | 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] | 715 | { |
| 716 | struct pnfs_device *pdev = NULL; |
| 717 | u32 max_resp_sz; |
| 718 | int max_pages; |
| 719 | struct page **pages = NULL; |
| 720 | struct nfs4_file_layout_dsaddr *dsaddr = NULL; |
| 721 | int rc, i; |
| 722 | struct nfs_server *server = NFS_SERVER(inode); |
| 723 | |
| 724 | /* |
| 725 | * Use the session max response size as the basis for setting |
| 726 | * GETDEVICEINFO's maxcount |
| 727 | */ |
| 728 | 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] | 729 | max_pages = nfs_page_array_len(0, max_resp_sz); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 730 | dprintk("%s inode %p max_resp_sz %u max_pages %d\n", |
| 731 | __func__, inode, max_resp_sz, max_pages); |
| 732 | |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 733 | pdev = kzalloc(sizeof(struct pnfs_device), gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 734 | if (pdev == NULL) |
| 735 | return NULL; |
| 736 | |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 737 | pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 738 | if (pages == NULL) { |
| 739 | kfree(pdev); |
| 740 | return NULL; |
| 741 | } |
| 742 | for (i = 0; i < max_pages; i++) { |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 743 | pages[i] = alloc_page(gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 744 | if (!pages[i]) |
| 745 | goto out_free; |
| 746 | } |
| 747 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 748 | memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id)); |
| 749 | pdev->layout_type = LAYOUT_NFSV4_1_FILES; |
| 750 | pdev->pages = pages; |
| 751 | pdev->pgbase = 0; |
| 752 | pdev->pglen = PAGE_SIZE * max_pages; |
| 753 | pdev->mincount = 0; |
| 754 | |
| 755 | rc = nfs4_proc_getdeviceinfo(server, pdev); |
| 756 | dprintk("%s getdevice info returns %d\n", __func__, rc); |
| 757 | if (rc) |
| 758 | goto out_free; |
| 759 | |
| 760 | /* |
| 761 | * Found new device, need to decode it and then add it to the |
| 762 | * list of known devices for this mountpoint. |
| 763 | */ |
Trond Myklebust | a75b9df | 2011-05-11 18:00:51 -0400 | [diff] [blame] | 764 | dsaddr = decode_and_add_device(inode, pdev, gfp_flags); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 765 | out_free: |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 766 | for (i = 0; i < max_pages; i++) |
| 767 | __free_page(pages[i]); |
| 768 | kfree(pages); |
| 769 | kfree(pdev); |
| 770 | dprintk("<-- %s dsaddr %p\n", __func__, dsaddr); |
| 771 | return dsaddr; |
| 772 | } |
| 773 | |
Christoph Hellwig | ea8eecd | 2011-03-01 01:34:21 +0000 | [diff] [blame] | 774 | void |
| 775 | nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr) |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 776 | { |
Benny Halevy | 1775bc3 | 2011-05-20 13:47:33 +0200 | [diff] [blame] | 777 | nfs4_put_deviceid_node(&dsaddr->id_node); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 778 | } |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 779 | |
| 780 | /* |
| 781 | * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit |
| 782 | * Then: ((res + fsi) % dsaddr->stripe_count) |
| 783 | */ |
| 784 | u32 |
| 785 | nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset) |
| 786 | { |
| 787 | struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg); |
| 788 | u64 tmp; |
| 789 | |
| 790 | tmp = offset - flseg->pattern_offset; |
| 791 | do_div(tmp, flseg->stripe_unit); |
| 792 | tmp += flseg->first_stripe_index; |
| 793 | return do_div(tmp, flseg->dsaddr->stripe_count); |
| 794 | } |
| 795 | |
| 796 | u32 |
| 797 | nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j) |
| 798 | { |
| 799 | return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j]; |
| 800 | } |
| 801 | |
| 802 | struct nfs_fh * |
| 803 | nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j) |
| 804 | { |
| 805 | struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg); |
| 806 | u32 i; |
| 807 | |
| 808 | if (flseg->stripe_type == STRIPE_SPARSE) { |
| 809 | if (flseg->num_fh == 1) |
| 810 | i = 0; |
| 811 | else if (flseg->num_fh == 0) |
| 812 | /* Use the MDS OPEN fh set in nfs_read_rpcsetup */ |
| 813 | return NULL; |
| 814 | else |
| 815 | i = nfs4_fl_calc_ds_index(lseg, j); |
| 816 | } else |
| 817 | i = j; |
| 818 | return flseg->fh_array[i]; |
| 819 | } |
| 820 | |
| 821 | struct nfs4_pnfs_ds * |
| 822 | nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx) |
| 823 | { |
| 824 | struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr; |
| 825 | struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx]; |
Andy Adamson | 554d458 | 2012-04-27 17:53:42 -0400 | [diff] [blame] | 826 | struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg); |
| 827 | |
| 828 | if (filelayout_test_devid_invalid(devid)) |
| 829 | return NULL; |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 830 | |
| 831 | if (ds == NULL) { |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 832 | 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] | 833 | __func__, ds_idx); |
Andy Adamson | 554d458 | 2012-04-27 17:53:42 -0400 | [diff] [blame] | 834 | goto mark_dev_invalid; |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | if (!ds->ds_clp) { |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 838 | struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode); |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 839 | int err; |
| 840 | |
Andy Adamson | 568e8c4 | 2011-03-01 01:34:22 +0000 | [diff] [blame] | 841 | err = nfs4_ds_connect(s, ds); |
Andy Adamson | 554d458 | 2012-04-27 17:53:42 -0400 | [diff] [blame] | 842 | if (err) |
| 843 | goto mark_dev_invalid; |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 844 | } |
| 845 | return ds; |
Andy Adamson | 554d458 | 2012-04-27 17:53:42 -0400 | [diff] [blame] | 846 | |
| 847 | mark_dev_invalid: |
| 848 | filelayout_mark_devid_invalid(devid); |
| 849 | return NULL; |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 850 | } |
Andy Adamson | 98fc685 | 2012-04-27 17:53:45 -0400 | [diff] [blame] | 851 | |
| 852 | module_param(dataserver_retrans, uint, 0644); |
| 853 | MODULE_PARM_DESC(dataserver_retrans, "The number of times the NFSv4.1 client " |
| 854 | "retries a request before it attempts further " |
| 855 | " recovery action."); |
| 856 | module_param(dataserver_timeo, uint, 0644); |
| 857 | MODULE_PARM_DESC(dataserver_timeo, "The time (in tenths of a second) the " |
| 858 | "NFSv4.1 client waits for a response from a " |
| 859 | " data server before it retries an NFS request."); |