blob: 3b7bf13772641ac8409aef9e4e70d6eca99e7ec3 [file] [log] [blame]
Andy Adamson16b374c2010-10-20 00:18:04 -04001/*
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>
33
34#include "internal.h"
35#include "nfs4filelayout.h"
36
37#define NFSDBG_FACILITY NFSDBG_PNFS_LD
38
39/*
40 * Data server cache
41 *
42 * Data servers can be mapped to different device ids.
43 * nfs4_pnfs_ds reference counting
44 * - set to 1 on allocation
45 * - incremented when a device id maps a data server already in the cache.
46 * - decremented when deviceid is removed from the cache.
47 */
48DEFINE_SPINLOCK(nfs4_ds_cache_lock);
49static LIST_HEAD(nfs4_data_server_cache);
50
51/* Debug routines */
52void
53print_ds(struct nfs4_pnfs_ds *ds)
54{
55 if (ds == NULL) {
56 printk("%s NULL device\n", __func__);
57 return;
58 }
59 printk(" ip_addr %x port %hu\n"
60 " ref count %d\n"
61 " client %p\n"
62 " cl_exchange_flags %x\n",
63 ntohl(ds->ds_ip_addr), ntohs(ds->ds_port),
64 atomic_read(&ds->ds_count), ds->ds_clp,
65 ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
66}
67
Andy Adamson16b374c2010-10-20 00:18:04 -040068/* nfs4_ds_cache_lock is held */
69static struct nfs4_pnfs_ds *
70_data_server_lookup_locked(u32 ip_addr, u32 port)
71{
72 struct nfs4_pnfs_ds *ds;
73
74 dprintk("_data_server_lookup: ip_addr=%x port=%hu\n",
75 ntohl(ip_addr), ntohs(port));
76
77 list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) {
78 if (ds->ds_ip_addr == ip_addr &&
79 ds->ds_port == port) {
80 return ds;
81 }
82 }
83 return NULL;
84}
85
Andy Adamsond83217c2011-03-01 01:34:17 +000086/*
87 * Create an rpc connection to the nfs4_pnfs_ds data server
88 * Currently only support IPv4
89 */
90static int
91nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds)
92{
93 struct nfs_client *clp;
94 struct sockaddr_in sin;
95 int status = 0;
96
97 dprintk("--> %s ip:port %x:%hu au_flavor %d\n", __func__,
98 ntohl(ds->ds_ip_addr), ntohs(ds->ds_port),
99 mds_srv->nfs_client->cl_rpcclient->cl_auth->au_flavor);
100
101 sin.sin_family = AF_INET;
102 sin.sin_addr.s_addr = ds->ds_ip_addr;
103 sin.sin_port = ds->ds_port;
104
105 clp = nfs4_set_ds_client(mds_srv->nfs_client, (struct sockaddr *)&sin,
106 sizeof(sin), IPPROTO_TCP);
107 if (IS_ERR(clp)) {
108 status = PTR_ERR(clp);
109 goto out;
110 }
111
112 if ((clp->cl_exchange_flags & EXCHGID4_FLAG_MASK_PNFS) != 0) {
113 if (!is_ds_client(clp)) {
114 status = -ENODEV;
115 goto out_put;
116 }
117 ds->ds_clp = clp;
118 dprintk("%s [existing] ip=%x, port=%hu\n", __func__,
119 ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
120 goto out;
121 }
122
123 /*
124 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the DS lease to
125 * be equal to the MDS lease. Renewal is scheduled in create_session.
126 */
127 spin_lock(&mds_srv->nfs_client->cl_lock);
128 clp->cl_lease_time = mds_srv->nfs_client->cl_lease_time;
129 spin_unlock(&mds_srv->nfs_client->cl_lock);
130 clp->cl_last_renewal = jiffies;
131
132 /* New nfs_client */
133 status = nfs4_init_ds_session(clp);
134 if (status)
135 goto out_put;
136
137 ds->ds_clp = clp;
138 dprintk("%s [new] ip=%x, port=%hu\n", __func__, ntohl(ds->ds_ip_addr),
139 ntohs(ds->ds_port));
140out:
141 return status;
142out_put:
143 nfs_put_client(clp);
144 goto out;
145}
146
Andy Adamson16b374c2010-10-20 00:18:04 -0400147static void
148destroy_ds(struct nfs4_pnfs_ds *ds)
149{
150 dprintk("--> %s\n", __func__);
151 ifdebug(FACILITY)
152 print_ds(ds);
153
154 if (ds->ds_clp)
155 nfs_put_client(ds->ds_clp);
156 kfree(ds);
157}
158
Benny Halevy1775bc32011-05-20 13:47:33 +0200159void
Andy Adamson16b374c2010-10-20 00:18:04 -0400160nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
161{
162 struct nfs4_pnfs_ds *ds;
163 int i;
164
Benny Halevya1eaecb2011-05-19 22:14:47 -0400165 nfs4_print_deviceid(&dsaddr->id_node.deviceid);
Andy Adamson16b374c2010-10-20 00:18:04 -0400166
167 for (i = 0; i < dsaddr->ds_num; i++) {
168 ds = dsaddr->ds_list[i];
169 if (ds != NULL) {
170 if (atomic_dec_and_lock(&ds->ds_count,
171 &nfs4_ds_cache_lock)) {
172 list_del_init(&ds->ds_node);
173 spin_unlock(&nfs4_ds_cache_lock);
174 destroy_ds(ds);
175 }
176 }
177 }
178 kfree(dsaddr->stripe_indices);
179 kfree(dsaddr);
180}
181
Andy Adamson16b374c2010-10-20 00:18:04 -0400182static struct nfs4_pnfs_ds *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400183nfs4_pnfs_ds_add(struct inode *inode, u32 ip_addr, u32 port, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400184{
185 struct nfs4_pnfs_ds *tmp_ds, *ds;
186
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400187 ds = kzalloc(sizeof(*tmp_ds), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400188 if (!ds)
189 goto out;
190
191 spin_lock(&nfs4_ds_cache_lock);
192 tmp_ds = _data_server_lookup_locked(ip_addr, port);
193 if (tmp_ds == NULL) {
194 ds->ds_ip_addr = ip_addr;
195 ds->ds_port = port;
196 atomic_set(&ds->ds_count, 1);
197 INIT_LIST_HEAD(&ds->ds_node);
198 ds->ds_clp = NULL;
199 list_add(&ds->ds_node, &nfs4_data_server_cache);
200 dprintk("%s add new data server ip 0x%x\n", __func__,
201 ds->ds_ip_addr);
202 } else {
203 kfree(ds);
204 atomic_inc(&tmp_ds->ds_count);
205 dprintk("%s data server found ip 0x%x, inc'ed ds_count to %d\n",
206 __func__, tmp_ds->ds_ip_addr,
207 atomic_read(&tmp_ds->ds_count));
208 ds = tmp_ds;
209 }
210 spin_unlock(&nfs4_ds_cache_lock);
211out:
212 return ds;
213}
214
215/*
216 * Currently only support ipv4, and one multi-path address.
217 */
218static struct nfs4_pnfs_ds *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400219decode_and_add_ds(struct xdr_stream *streamp, struct inode *inode, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400220{
221 struct nfs4_pnfs_ds *ds = NULL;
222 char *buf;
223 const char *ipend, *pstr;
224 u32 ip_addr, port;
225 int nlen, rlen, i;
226 int tmp[2];
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400227 __be32 *p;
Andy Adamson16b374c2010-10-20 00:18:04 -0400228
229 /* r_netid */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400230 p = xdr_inline_decode(streamp, 4);
231 if (unlikely(!p))
232 goto out_err;
Andy Adamson16b374c2010-10-20 00:18:04 -0400233 nlen = be32_to_cpup(p++);
Andy Adamson16b374c2010-10-20 00:18:04 -0400234
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400235 p = xdr_inline_decode(streamp, nlen);
236 if (unlikely(!p))
237 goto out_err;
Andy Adamson16b374c2010-10-20 00:18:04 -0400238
239 /* Check that netid is "tcp" */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400240 if (nlen != 3 || memcmp((char *)p, "tcp", 3)) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400241 dprintk("%s: ERROR: non ipv4 TCP r_netid\n", __func__);
242 goto out_err;
243 }
244
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400245 /* r_addr */
246 p = xdr_inline_decode(streamp, 4);
247 if (unlikely(!p))
248 goto out_err;
249 rlen = be32_to_cpup(p);
250
251 p = xdr_inline_decode(streamp, rlen);
252 if (unlikely(!p))
253 goto out_err;
254
Andy Adamson16b374c2010-10-20 00:18:04 -0400255 /* ipv6 length plus port is legal */
256 if (rlen > INET6_ADDRSTRLEN + 8) {
Jesper Juhlad3d2ee2011-01-17 18:41:50 +0000257 dprintk("%s: Invalid address, length %d\n", __func__,
Andy Adamson16b374c2010-10-20 00:18:04 -0400258 rlen);
259 goto out_err;
260 }
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400261 buf = kmalloc(rlen + 1, gfp_flags);
Stanislav Fomichevb9f81052011-02-05 23:13:01 +0000262 if (!buf) {
263 dprintk("%s: Not enough memory\n", __func__);
264 goto out_err;
265 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400266 buf[rlen] = '\0';
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400267 memcpy(buf, p, rlen);
Andy Adamson16b374c2010-10-20 00:18:04 -0400268
269 /* replace the port dots with dashes for the in4_pton() delimiter*/
270 for (i = 0; i < 2; i++) {
271 char *res = strrchr(buf, '.');
Jesper Juhlad3d2ee2011-01-17 18:41:50 +0000272 if (!res) {
273 dprintk("%s: Failed finding expected dots in port\n",
274 __func__);
275 goto out_free;
276 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400277 *res = '-';
278 }
279
280 /* Currently only support ipv4 address */
281 if (in4_pton(buf, rlen, (u8 *)&ip_addr, '-', &ipend) == 0) {
282 dprintk("%s: Only ipv4 addresses supported\n", __func__);
283 goto out_free;
284 }
285
286 /* port */
287 pstr = ipend;
288 sscanf(pstr, "-%d-%d", &tmp[0], &tmp[1]);
289 port = htons((tmp[0] << 8) | (tmp[1]));
290
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400291 ds = nfs4_pnfs_ds_add(inode, ip_addr, port, gfp_flags);
Jesper Juhlad3d2ee2011-01-17 18:41:50 +0000292 dprintk("%s: Decoded address and port %s\n", __func__, buf);
Andy Adamson16b374c2010-10-20 00:18:04 -0400293out_free:
294 kfree(buf);
295out_err:
296 return ds;
297}
298
299/* Decode opaque device data and return the result */
300static struct nfs4_file_layout_dsaddr*
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400301decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400302{
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400303 int i;
Andy Adamson16b374c2010-10-20 00:18:04 -0400304 u32 cnt, num;
305 u8 *indexp;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400306 __be32 *p;
307 u8 *stripe_indices;
308 u8 max_stripe_index;
309 struct nfs4_file_layout_dsaddr *dsaddr = NULL;
310 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400311 struct xdr_buf buf;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400312 struct page *scratch;
313
314 /* set up xdr stream */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400315 scratch = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400316 if (!scratch)
317 goto out_err;
318
Benny Halevyf7da7a12011-05-19 14:16:47 -0400319 xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400320 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
Andy Adamson16b374c2010-10-20 00:18:04 -0400321
322 /* Get the stripe count (number of stripe index) */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400323 p = xdr_inline_decode(&stream, 4);
324 if (unlikely(!p))
325 goto out_err_free_scratch;
326
327 cnt = be32_to_cpup(p);
Andy Adamson16b374c2010-10-20 00:18:04 -0400328 dprintk("%s stripe count %d\n", __func__, cnt);
329 if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) {
330 printk(KERN_WARNING "%s: stripe count %d greater than "
331 "supported maximum %d\n", __func__,
332 cnt, NFS4_PNFS_MAX_STRIPE_CNT);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400333 goto out_err_free_scratch;
334 }
335
336 /* read stripe indices */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400337 stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400338 if (!stripe_indices)
339 goto out_err_free_scratch;
340
341 p = xdr_inline_decode(&stream, cnt << 2);
342 if (unlikely(!p))
343 goto out_err_free_stripe_indices;
344
345 indexp = &stripe_indices[0];
346 max_stripe_index = 0;
347 for (i = 0; i < cnt; i++) {
348 *indexp = be32_to_cpup(p++);
349 max_stripe_index = max(max_stripe_index, *indexp);
350 indexp++;
Andy Adamson16b374c2010-10-20 00:18:04 -0400351 }
352
353 /* Check the multipath list count */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400354 p = xdr_inline_decode(&stream, 4);
355 if (unlikely(!p))
356 goto out_err_free_stripe_indices;
357
358 num = be32_to_cpup(p);
Andy Adamson16b374c2010-10-20 00:18:04 -0400359 dprintk("%s ds_num %u\n", __func__, num);
360 if (num > NFS4_PNFS_MAX_MULTI_CNT) {
361 printk(KERN_WARNING "%s: multipath count %d greater than "
362 "supported maximum %d\n", __func__,
363 num, NFS4_PNFS_MAX_MULTI_CNT);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400364 goto out_err_free_stripe_indices;
Andy Adamson16b374c2010-10-20 00:18:04 -0400365 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400366
367 /* validate stripe indices are all < num */
368 if (max_stripe_index >= num) {
369 printk(KERN_WARNING "%s: stripe index %u >= num ds %u\n",
370 __func__, max_stripe_index, num);
371 goto out_err_free_stripe_indices;
372 }
373
Andy Adamson16b374c2010-10-20 00:18:04 -0400374 dsaddr = kzalloc(sizeof(*dsaddr) +
375 (sizeof(struct nfs4_pnfs_ds *) * (num - 1)),
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400376 gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400377 if (!dsaddr)
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400378 goto out_err_free_stripe_indices;
Andy Adamson16b374c2010-10-20 00:18:04 -0400379
380 dsaddr->stripe_count = cnt;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400381 dsaddr->stripe_indices = stripe_indices;
382 stripe_indices = NULL;
Andy Adamson16b374c2010-10-20 00:18:04 -0400383 dsaddr->ds_num = num;
Benny Halevy1775bc32011-05-20 13:47:33 +0200384 nfs4_init_deviceid_node(&dsaddr->id_node,
385 NFS_SERVER(ino)->pnfs_curr_ld,
386 NFS_SERVER(ino)->nfs_client,
Benny Halevya1eaecb2011-05-19 22:14:47 -0400387 &pdev->dev_id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400388
Andy Adamson16b374c2010-10-20 00:18:04 -0400389 for (i = 0; i < dsaddr->ds_num; i++) {
390 int j;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400391 u32 mp_count;
Andy Adamson16b374c2010-10-20 00:18:04 -0400392
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400393 p = xdr_inline_decode(&stream, 4);
394 if (unlikely(!p))
395 goto out_err_free_deviceid;
396
397 mp_count = be32_to_cpup(p); /* multipath count */
398 if (mp_count > 1) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400399 printk(KERN_WARNING
400 "%s: Multipath count %d not supported, "
401 "skipping all greater than 1\n", __func__,
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400402 mp_count);
Andy Adamson16b374c2010-10-20 00:18:04 -0400403 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400404 for (j = 0; j < mp_count; j++) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400405 if (j == 0) {
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400406 dsaddr->ds_list[i] = decode_and_add_ds(&stream,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400407 ino, gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400408 if (dsaddr->ds_list[i] == NULL)
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400409 goto out_err_free_deviceid;
Andy Adamson16b374c2010-10-20 00:18:04 -0400410 } else {
411 u32 len;
412 /* skip extra multipath */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400413
414 /* read len, skip */
415 p = xdr_inline_decode(&stream, 4);
416 if (unlikely(!p))
417 goto out_err_free_deviceid;
418 len = be32_to_cpup(p);
419
420 p = xdr_inline_decode(&stream, len);
421 if (unlikely(!p))
422 goto out_err_free_deviceid;
423
424 /* read len, skip */
425 p = xdr_inline_decode(&stream, 4);
426 if (unlikely(!p))
427 goto out_err_free_deviceid;
428 len = be32_to_cpup(p);
429
430 p = xdr_inline_decode(&stream, len);
431 if (unlikely(!p))
432 goto out_err_free_deviceid;
Andy Adamson16b374c2010-10-20 00:18:04 -0400433 }
434 }
435 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400436
437 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400438 return dsaddr;
439
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400440out_err_free_deviceid:
Andy Adamson16b374c2010-10-20 00:18:04 -0400441 nfs4_fl_free_deviceid(dsaddr);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400442 /* stripe_indicies was part of dsaddr */
443 goto out_err_free_scratch;
444out_err_free_stripe_indices:
445 kfree(stripe_indices);
446out_err_free_scratch:
447 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400448out_err:
449 dprintk("%s ERROR: returning NULL\n", __func__);
450 return NULL;
451}
452
453/*
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000454 * Decode the opaque device specified in 'dev' and add it to the cache of
455 * available devices.
Andy Adamson16b374c2010-10-20 00:18:04 -0400456 */
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000457static struct nfs4_file_layout_dsaddr *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400458decode_and_add_device(struct inode *inode, struct pnfs_device *dev, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400459{
Benny Halevya1eaecb2011-05-19 22:14:47 -0400460 struct nfs4_deviceid_node *d;
461 struct nfs4_file_layout_dsaddr *n, *new;
Andy Adamson16b374c2010-10-20 00:18:04 -0400462
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400463 new = decode_device(inode, dev, gfp_flags);
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000464 if (!new) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400465 printk(KERN_WARNING "%s: Could not decode or add device\n",
466 __func__);
467 return NULL;
468 }
469
Benny Halevya1eaecb2011-05-19 22:14:47 -0400470 d = nfs4_insert_deviceid_node(&new->id_node);
471 n = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
472 if (n != new) {
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000473 nfs4_fl_free_deviceid(new);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400474 return n;
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000475 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400476
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000477 return new;
Andy Adamson16b374c2010-10-20 00:18:04 -0400478}
479
480/*
481 * Retrieve the information for dev_id, add it to the list
482 * of available devices, and return it.
483 */
484struct nfs4_file_layout_dsaddr *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400485get_device_info(struct inode *inode, struct nfs4_deviceid *dev_id, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400486{
487 struct pnfs_device *pdev = NULL;
488 u32 max_resp_sz;
489 int max_pages;
490 struct page **pages = NULL;
491 struct nfs4_file_layout_dsaddr *dsaddr = NULL;
492 int rc, i;
493 struct nfs_server *server = NFS_SERVER(inode);
494
495 /*
496 * Use the session max response size as the basis for setting
497 * GETDEVICEINFO's maxcount
498 */
499 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
500 max_pages = max_resp_sz >> PAGE_SHIFT;
501 dprintk("%s inode %p max_resp_sz %u max_pages %d\n",
502 __func__, inode, max_resp_sz, max_pages);
503
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400504 pdev = kzalloc(sizeof(struct pnfs_device), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400505 if (pdev == NULL)
506 return NULL;
507
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400508 pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400509 if (pages == NULL) {
510 kfree(pdev);
511 return NULL;
512 }
513 for (i = 0; i < max_pages; i++) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400514 pages[i] = alloc_page(gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400515 if (!pages[i])
516 goto out_free;
517 }
518
Andy Adamson16b374c2010-10-20 00:18:04 -0400519 memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id));
520 pdev->layout_type = LAYOUT_NFSV4_1_FILES;
521 pdev->pages = pages;
522 pdev->pgbase = 0;
523 pdev->pglen = PAGE_SIZE * max_pages;
524 pdev->mincount = 0;
525
526 rc = nfs4_proc_getdeviceinfo(server, pdev);
527 dprintk("%s getdevice info returns %d\n", __func__, rc);
528 if (rc)
529 goto out_free;
530
531 /*
532 * Found new device, need to decode it and then add it to the
533 * list of known devices for this mountpoint.
534 */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400535 dsaddr = decode_and_add_device(inode, pdev, gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400536out_free:
Andy Adamson16b374c2010-10-20 00:18:04 -0400537 for (i = 0; i < max_pages; i++)
538 __free_page(pages[i]);
539 kfree(pages);
540 kfree(pdev);
541 dprintk("<-- %s dsaddr %p\n", __func__, dsaddr);
542 return dsaddr;
543}
544
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000545void
546nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
Andy Adamson16b374c2010-10-20 00:18:04 -0400547{
Benny Halevy1775bc32011-05-20 13:47:33 +0200548 nfs4_put_deviceid_node(&dsaddr->id_node);
Andy Adamson16b374c2010-10-20 00:18:04 -0400549}
Fred Isamancfe7f412011-03-01 01:34:18 +0000550
551/*
552 * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit
553 * Then: ((res + fsi) % dsaddr->stripe_count)
554 */
555u32
556nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset)
557{
558 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
559 u64 tmp;
560
561 tmp = offset - flseg->pattern_offset;
562 do_div(tmp, flseg->stripe_unit);
563 tmp += flseg->first_stripe_index;
564 return do_div(tmp, flseg->dsaddr->stripe_count);
565}
566
567u32
568nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j)
569{
570 return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j];
571}
572
573struct nfs_fh *
574nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j)
575{
576 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
577 u32 i;
578
579 if (flseg->stripe_type == STRIPE_SPARSE) {
580 if (flseg->num_fh == 1)
581 i = 0;
582 else if (flseg->num_fh == 0)
583 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
584 return NULL;
585 else
586 i = nfs4_fl_calc_ds_index(lseg, j);
587 } else
588 i = j;
589 return flseg->fh_array[i];
590}
591
Andy Adamson568e8c42011-03-01 01:34:22 +0000592static void
593filelayout_mark_devid_negative(struct nfs4_file_layout_dsaddr *dsaddr,
594 int err, u32 ds_addr)
595{
Benny Halevya1eaecb2011-05-19 22:14:47 -0400596 u32 *p = (u32 *)&dsaddr->id_node.deviceid;
Andy Adamson568e8c42011-03-01 01:34:22 +0000597
598 printk(KERN_ERR "NFS: data server %x connection error %d."
599 " Deviceid [%x%x%x%x] marked out of use.\n",
600 ds_addr, err, p[0], p[1], p[2], p[3]);
601
Benny Halevya1eaecb2011-05-19 22:14:47 -0400602 spin_lock(&nfs4_ds_cache_lock);
Andy Adamson568e8c42011-03-01 01:34:22 +0000603 dsaddr->flags |= NFS4_DEVICE_ID_NEG_ENTRY;
Benny Halevya1eaecb2011-05-19 22:14:47 -0400604 spin_unlock(&nfs4_ds_cache_lock);
Andy Adamson568e8c42011-03-01 01:34:22 +0000605}
606
Fred Isamancfe7f412011-03-01 01:34:18 +0000607struct nfs4_pnfs_ds *
608nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx)
609{
610 struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr;
611 struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx];
612
613 if (ds == NULL) {
614 printk(KERN_ERR "%s: No data server for offset index %d\n",
615 __func__, ds_idx);
616 return NULL;
617 }
618
619 if (!ds->ds_clp) {
Andy Adamson568e8c42011-03-01 01:34:22 +0000620 struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode);
Fred Isamancfe7f412011-03-01 01:34:18 +0000621 int err;
622
Andy Adamson568e8c42011-03-01 01:34:22 +0000623 if (dsaddr->flags & NFS4_DEVICE_ID_NEG_ENTRY) {
624 /* Already tried to connect, don't try again */
625 dprintk("%s Deviceid marked out of use\n", __func__);
626 return NULL;
627 }
628 err = nfs4_ds_connect(s, ds);
Fred Isamancfe7f412011-03-01 01:34:18 +0000629 if (err) {
Andy Adamson568e8c42011-03-01 01:34:22 +0000630 filelayout_mark_devid_negative(dsaddr, err,
631 ntohl(ds->ds_ip_addr));
Fred Isamancfe7f412011-03-01 01:34:18 +0000632 return NULL;
633 }
634 }
635 return ds;
636}