NFSv4.1: turn off pNFS on ds connection failure
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / nfs4filelayoutdev.c
blob68143c162e3be30ef824027a1db99de69975acfc
1 /*
2 * Device operations for the pnfs nfs4 file layout driver.
4 * Copyright (c) 2002
5 * The Regents of the University of Michigan
6 * All Rights Reserved
8 * Dean Hildebrand <dhildebz@umich.edu>
9 * Garth Goodson <Garth.Goodson@netapp.com>
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.
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.
31 #include <linux/nfs_fs.h>
32 #include <linux/vmalloc.h>
34 #include "internal.h"
35 #include "nfs4filelayout.h"
37 #define NFSDBG_FACILITY NFSDBG_PNFS_LD
40 * Device ID RCU cache. A device ID is unique per client ID and layout type.
42 #define NFS4_FL_DEVICE_ID_HASH_BITS 5
43 #define NFS4_FL_DEVICE_ID_HASH_SIZE (1 << NFS4_FL_DEVICE_ID_HASH_BITS)
44 #define NFS4_FL_DEVICE_ID_HASH_MASK (NFS4_FL_DEVICE_ID_HASH_SIZE - 1)
46 static inline u32
47 nfs4_fl_deviceid_hash(struct nfs4_deviceid *id)
49 unsigned char *cptr = (unsigned char *)id->data;
50 unsigned int nbytes = NFS4_DEVICEID4_SIZE;
51 u32 x = 0;
53 while (nbytes--) {
54 x *= 37;
55 x += *cptr++;
57 return x & NFS4_FL_DEVICE_ID_HASH_MASK;
60 static struct hlist_head filelayout_deviceid_cache[NFS4_FL_DEVICE_ID_HASH_SIZE];
61 static DEFINE_SPINLOCK(filelayout_deviceid_lock);
64 * Data server cache
66 * Data servers can be mapped to different device ids.
67 * nfs4_pnfs_ds reference counting
68 * - set to 1 on allocation
69 * - incremented when a device id maps a data server already in the cache.
70 * - decremented when deviceid is removed from the cache.
72 DEFINE_SPINLOCK(nfs4_ds_cache_lock);
73 static LIST_HEAD(nfs4_data_server_cache);
75 /* Debug routines */
76 void
77 print_ds(struct nfs4_pnfs_ds *ds)
79 if (ds == NULL) {
80 printk("%s NULL device\n", __func__);
81 return;
83 printk(" ip_addr %x port %hu\n"
84 " ref count %d\n"
85 " client %p\n"
86 " cl_exchange_flags %x\n",
87 ntohl(ds->ds_ip_addr), ntohs(ds->ds_port),
88 atomic_read(&ds->ds_count), ds->ds_clp,
89 ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
92 void
93 print_ds_list(struct nfs4_file_layout_dsaddr *dsaddr)
95 int i;
97 ifdebug(FACILITY) {
98 printk("%s dsaddr->ds_num %d\n", __func__,
99 dsaddr->ds_num);
100 for (i = 0; i < dsaddr->ds_num; i++)
101 print_ds(dsaddr->ds_list[i]);
105 void print_deviceid(struct nfs4_deviceid *id)
107 u32 *p = (u32 *)id;
109 dprintk("%s: device id= [%x%x%x%x]\n", __func__,
110 p[0], p[1], p[2], p[3]);
113 /* nfs4_ds_cache_lock is held */
114 static struct nfs4_pnfs_ds *
115 _data_server_lookup_locked(u32 ip_addr, u32 port)
117 struct nfs4_pnfs_ds *ds;
119 dprintk("_data_server_lookup: ip_addr=%x port=%hu\n",
120 ntohl(ip_addr), ntohs(port));
122 list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) {
123 if (ds->ds_ip_addr == ip_addr &&
124 ds->ds_port == port) {
125 return ds;
128 return NULL;
132 * Create an rpc connection to the nfs4_pnfs_ds data server
133 * Currently only support IPv4
135 static int
136 nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds)
138 struct nfs_client *clp;
139 struct sockaddr_in sin;
140 int status = 0;
142 dprintk("--> %s ip:port %x:%hu au_flavor %d\n", __func__,
143 ntohl(ds->ds_ip_addr), ntohs(ds->ds_port),
144 mds_srv->nfs_client->cl_rpcclient->cl_auth->au_flavor);
146 sin.sin_family = AF_INET;
147 sin.sin_addr.s_addr = ds->ds_ip_addr;
148 sin.sin_port = ds->ds_port;
150 clp = nfs4_set_ds_client(mds_srv->nfs_client, (struct sockaddr *)&sin,
151 sizeof(sin), IPPROTO_TCP);
152 if (IS_ERR(clp)) {
153 status = PTR_ERR(clp);
154 goto out;
157 if ((clp->cl_exchange_flags & EXCHGID4_FLAG_MASK_PNFS) != 0) {
158 if (!is_ds_client(clp)) {
159 status = -ENODEV;
160 goto out_put;
162 ds->ds_clp = clp;
163 dprintk("%s [existing] ip=%x, port=%hu\n", __func__,
164 ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
165 goto out;
169 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the DS lease to
170 * be equal to the MDS lease. Renewal is scheduled in create_session.
172 spin_lock(&mds_srv->nfs_client->cl_lock);
173 clp->cl_lease_time = mds_srv->nfs_client->cl_lease_time;
174 spin_unlock(&mds_srv->nfs_client->cl_lock);
175 clp->cl_last_renewal = jiffies;
177 /* New nfs_client */
178 status = nfs4_init_ds_session(clp);
179 if (status)
180 goto out_put;
182 ds->ds_clp = clp;
183 dprintk("%s [new] ip=%x, port=%hu\n", __func__, ntohl(ds->ds_ip_addr),
184 ntohs(ds->ds_port));
185 out:
186 return status;
187 out_put:
188 nfs_put_client(clp);
189 goto out;
192 static void
193 destroy_ds(struct nfs4_pnfs_ds *ds)
195 dprintk("--> %s\n", __func__);
196 ifdebug(FACILITY)
197 print_ds(ds);
199 if (ds->ds_clp)
200 nfs_put_client(ds->ds_clp);
201 kfree(ds);
204 static void
205 nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
207 struct nfs4_pnfs_ds *ds;
208 int i;
210 print_deviceid(&dsaddr->deviceid);
212 for (i = 0; i < dsaddr->ds_num; i++) {
213 ds = dsaddr->ds_list[i];
214 if (ds != NULL) {
215 if (atomic_dec_and_lock(&ds->ds_count,
216 &nfs4_ds_cache_lock)) {
217 list_del_init(&ds->ds_node);
218 spin_unlock(&nfs4_ds_cache_lock);
219 destroy_ds(ds);
223 kfree(dsaddr->stripe_indices);
224 kfree(dsaddr);
227 static struct nfs4_pnfs_ds *
228 nfs4_pnfs_ds_add(struct inode *inode, u32 ip_addr, u32 port)
230 struct nfs4_pnfs_ds *tmp_ds, *ds;
232 ds = kzalloc(sizeof(*tmp_ds), GFP_KERNEL);
233 if (!ds)
234 goto out;
236 spin_lock(&nfs4_ds_cache_lock);
237 tmp_ds = _data_server_lookup_locked(ip_addr, port);
238 if (tmp_ds == NULL) {
239 ds->ds_ip_addr = ip_addr;
240 ds->ds_port = port;
241 atomic_set(&ds->ds_count, 1);
242 INIT_LIST_HEAD(&ds->ds_node);
243 ds->ds_clp = NULL;
244 list_add(&ds->ds_node, &nfs4_data_server_cache);
245 dprintk("%s add new data server ip 0x%x\n", __func__,
246 ds->ds_ip_addr);
247 } else {
248 kfree(ds);
249 atomic_inc(&tmp_ds->ds_count);
250 dprintk("%s data server found ip 0x%x, inc'ed ds_count to %d\n",
251 __func__, tmp_ds->ds_ip_addr,
252 atomic_read(&tmp_ds->ds_count));
253 ds = tmp_ds;
255 spin_unlock(&nfs4_ds_cache_lock);
256 out:
257 return ds;
261 * Currently only support ipv4, and one multi-path address.
263 static struct nfs4_pnfs_ds *
264 decode_and_add_ds(__be32 **pp, struct inode *inode)
266 struct nfs4_pnfs_ds *ds = NULL;
267 char *buf;
268 const char *ipend, *pstr;
269 u32 ip_addr, port;
270 int nlen, rlen, i;
271 int tmp[2];
272 __be32 *r_netid, *r_addr, *p = *pp;
274 /* r_netid */
275 nlen = be32_to_cpup(p++);
276 r_netid = p;
277 p += XDR_QUADLEN(nlen);
279 /* r_addr */
280 rlen = be32_to_cpup(p++);
281 r_addr = p;
282 p += XDR_QUADLEN(rlen);
283 *pp = p;
285 /* Check that netid is "tcp" */
286 if (nlen != 3 || memcmp((char *)r_netid, "tcp", 3)) {
287 dprintk("%s: ERROR: non ipv4 TCP r_netid\n", __func__);
288 goto out_err;
291 /* ipv6 length plus port is legal */
292 if (rlen > INET6_ADDRSTRLEN + 8) {
293 dprintk("%s: Invalid address, length %d\n", __func__,
294 rlen);
295 goto out_err;
297 buf = kmalloc(rlen + 1, GFP_KERNEL);
298 if (!buf) {
299 dprintk("%s: Not enough memory\n", __func__);
300 goto out_err;
302 buf[rlen] = '\0';
303 memcpy(buf, r_addr, rlen);
305 /* replace the port dots with dashes for the in4_pton() delimiter*/
306 for (i = 0; i < 2; i++) {
307 char *res = strrchr(buf, '.');
308 if (!res) {
309 dprintk("%s: Failed finding expected dots in port\n",
310 __func__);
311 goto out_free;
313 *res = '-';
316 /* Currently only support ipv4 address */
317 if (in4_pton(buf, rlen, (u8 *)&ip_addr, '-', &ipend) == 0) {
318 dprintk("%s: Only ipv4 addresses supported\n", __func__);
319 goto out_free;
322 /* port */
323 pstr = ipend;
324 sscanf(pstr, "-%d-%d", &tmp[0], &tmp[1]);
325 port = htons((tmp[0] << 8) | (tmp[1]));
327 ds = nfs4_pnfs_ds_add(inode, ip_addr, port);
328 dprintk("%s: Decoded address and port %s\n", __func__, buf);
329 out_free:
330 kfree(buf);
331 out_err:
332 return ds;
335 /* Decode opaque device data and return the result */
336 static struct nfs4_file_layout_dsaddr*
337 decode_device(struct inode *ino, struct pnfs_device *pdev)
339 int i, dummy;
340 u32 cnt, num;
341 u8 *indexp;
342 __be32 *p = (__be32 *)pdev->area, *indicesp;
343 struct nfs4_file_layout_dsaddr *dsaddr;
345 /* Get the stripe count (number of stripe index) */
346 cnt = be32_to_cpup(p++);
347 dprintk("%s stripe count %d\n", __func__, cnt);
348 if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) {
349 printk(KERN_WARNING "%s: stripe count %d greater than "
350 "supported maximum %d\n", __func__,
351 cnt, NFS4_PNFS_MAX_STRIPE_CNT);
352 goto out_err;
355 /* Check the multipath list count */
356 indicesp = p;
357 p += XDR_QUADLEN(cnt << 2);
358 num = be32_to_cpup(p++);
359 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);
364 goto out_err;
366 dsaddr = kzalloc(sizeof(*dsaddr) +
367 (sizeof(struct nfs4_pnfs_ds *) * (num - 1)),
368 GFP_KERNEL);
369 if (!dsaddr)
370 goto out_err;
372 dsaddr->stripe_indices = kzalloc(sizeof(u8) * cnt, GFP_KERNEL);
373 if (!dsaddr->stripe_indices)
374 goto out_err_free;
376 dsaddr->stripe_count = cnt;
377 dsaddr->ds_num = num;
379 memcpy(&dsaddr->deviceid, &pdev->dev_id, sizeof(pdev->dev_id));
381 /* Go back an read stripe indices */
382 p = indicesp;
383 indexp = &dsaddr->stripe_indices[0];
384 for (i = 0; i < dsaddr->stripe_count; i++) {
385 *indexp = be32_to_cpup(p++);
386 if (*indexp >= num)
387 goto out_err_free;
388 indexp++;
390 /* Skip already read multipath list count */
391 p++;
393 for (i = 0; i < dsaddr->ds_num; i++) {
394 int j;
396 dummy = be32_to_cpup(p++); /* multipath count */
397 if (dummy > 1) {
398 printk(KERN_WARNING
399 "%s: Multipath count %d not supported, "
400 "skipping all greater than 1\n", __func__,
401 dummy);
403 for (j = 0; j < dummy; j++) {
404 if (j == 0) {
405 dsaddr->ds_list[i] = decode_and_add_ds(&p, ino);
406 if (dsaddr->ds_list[i] == NULL)
407 goto out_err_free;
408 } else {
409 u32 len;
410 /* skip extra multipath */
411 len = be32_to_cpup(p++);
412 p += XDR_QUADLEN(len);
413 len = be32_to_cpup(p++);
414 p += XDR_QUADLEN(len);
415 continue;
419 return dsaddr;
421 out_err_free:
422 nfs4_fl_free_deviceid(dsaddr);
423 out_err:
424 dprintk("%s ERROR: returning NULL\n", __func__);
425 return NULL;
429 * Decode the opaque device specified in 'dev' and add it to the cache of
430 * available devices.
432 static struct nfs4_file_layout_dsaddr *
433 decode_and_add_device(struct inode *inode, struct pnfs_device *dev)
435 struct nfs4_file_layout_dsaddr *d, *new;
436 long hash;
438 new = decode_device(inode, dev);
439 if (!new) {
440 printk(KERN_WARNING "%s: Could not decode or add device\n",
441 __func__);
442 return NULL;
445 spin_lock(&filelayout_deviceid_lock);
446 d = nfs4_fl_find_get_deviceid(&new->deviceid);
447 if (d) {
448 spin_unlock(&filelayout_deviceid_lock);
449 nfs4_fl_free_deviceid(new);
450 return d;
453 INIT_HLIST_NODE(&new->node);
454 atomic_set(&new->ref, 1);
455 hash = nfs4_fl_deviceid_hash(&new->deviceid);
456 hlist_add_head_rcu(&new->node, &filelayout_deviceid_cache[hash]);
457 spin_unlock(&filelayout_deviceid_lock);
459 return new;
463 * Retrieve the information for dev_id, add it to the list
464 * of available devices, and return it.
466 struct nfs4_file_layout_dsaddr *
467 get_device_info(struct inode *inode, struct nfs4_deviceid *dev_id)
469 struct pnfs_device *pdev = NULL;
470 u32 max_resp_sz;
471 int max_pages;
472 struct page **pages = NULL;
473 struct nfs4_file_layout_dsaddr *dsaddr = NULL;
474 int rc, i;
475 struct nfs_server *server = NFS_SERVER(inode);
478 * Use the session max response size as the basis for setting
479 * GETDEVICEINFO's maxcount
481 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
482 max_pages = max_resp_sz >> PAGE_SHIFT;
483 dprintk("%s inode %p max_resp_sz %u max_pages %d\n",
484 __func__, inode, max_resp_sz, max_pages);
486 pdev = kzalloc(sizeof(struct pnfs_device), GFP_KERNEL);
487 if (pdev == NULL)
488 return NULL;
490 pages = kzalloc(max_pages * sizeof(struct page *), GFP_KERNEL);
491 if (pages == NULL) {
492 kfree(pdev);
493 return NULL;
495 for (i = 0; i < max_pages; i++) {
496 pages[i] = alloc_page(GFP_KERNEL);
497 if (!pages[i])
498 goto out_free;
501 /* set pdev->area */
502 pdev->area = vmap(pages, max_pages, VM_MAP, PAGE_KERNEL);
503 if (!pdev->area)
504 goto out_free;
506 memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id));
507 pdev->layout_type = LAYOUT_NFSV4_1_FILES;
508 pdev->pages = pages;
509 pdev->pgbase = 0;
510 pdev->pglen = PAGE_SIZE * max_pages;
511 pdev->mincount = 0;
513 rc = nfs4_proc_getdeviceinfo(server, pdev);
514 dprintk("%s getdevice info returns %d\n", __func__, rc);
515 if (rc)
516 goto out_free;
519 * Found new device, need to decode it and then add it to the
520 * list of known devices for this mountpoint.
522 dsaddr = decode_and_add_device(inode, pdev);
523 out_free:
524 if (pdev->area != NULL)
525 vunmap(pdev->area);
526 for (i = 0; i < max_pages; i++)
527 __free_page(pages[i]);
528 kfree(pages);
529 kfree(pdev);
530 dprintk("<-- %s dsaddr %p\n", __func__, dsaddr);
531 return dsaddr;
534 void
535 nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
537 if (atomic_dec_and_lock(&dsaddr->ref, &filelayout_deviceid_lock)) {
538 hlist_del_rcu(&dsaddr->node);
539 spin_unlock(&filelayout_deviceid_lock);
541 synchronize_rcu();
542 nfs4_fl_free_deviceid(dsaddr);
546 struct nfs4_file_layout_dsaddr *
547 nfs4_fl_find_get_deviceid(struct nfs4_deviceid *id)
549 struct nfs4_file_layout_dsaddr *d;
550 struct hlist_node *n;
551 long hash = nfs4_fl_deviceid_hash(id);
554 rcu_read_lock();
555 hlist_for_each_entry_rcu(d, n, &filelayout_deviceid_cache[hash], node) {
556 if (!memcmp(&d->deviceid, id, sizeof(*id))) {
557 if (!atomic_inc_not_zero(&d->ref))
558 goto fail;
559 rcu_read_unlock();
560 return d;
563 fail:
564 rcu_read_unlock();
565 return NULL;
569 * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit
570 * Then: ((res + fsi) % dsaddr->stripe_count)
573 nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset)
575 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
576 u64 tmp;
578 tmp = offset - flseg->pattern_offset;
579 do_div(tmp, flseg->stripe_unit);
580 tmp += flseg->first_stripe_index;
581 return do_div(tmp, flseg->dsaddr->stripe_count);
585 nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j)
587 return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j];
590 struct nfs_fh *
591 nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j)
593 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
594 u32 i;
596 if (flseg->stripe_type == STRIPE_SPARSE) {
597 if (flseg->num_fh == 1)
598 i = 0;
599 else if (flseg->num_fh == 0)
600 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
601 return NULL;
602 else
603 i = nfs4_fl_calc_ds_index(lseg, j);
604 } else
605 i = j;
606 return flseg->fh_array[i];
609 static void
610 filelayout_mark_devid_negative(struct nfs4_file_layout_dsaddr *dsaddr,
611 int err, u32 ds_addr)
613 u32 *p = (u32 *)&dsaddr->deviceid;
615 printk(KERN_ERR "NFS: data server %x connection error %d."
616 " Deviceid [%x%x%x%x] marked out of use.\n",
617 ds_addr, err, p[0], p[1], p[2], p[3]);
619 spin_lock(&filelayout_deviceid_lock);
620 dsaddr->flags |= NFS4_DEVICE_ID_NEG_ENTRY;
621 spin_unlock(&filelayout_deviceid_lock);
624 struct nfs4_pnfs_ds *
625 nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx)
627 struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr;
628 struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx];
630 if (ds == NULL) {
631 printk(KERN_ERR "%s: No data server for offset index %d\n",
632 __func__, ds_idx);
633 return NULL;
636 if (!ds->ds_clp) {
637 struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode);
638 int err;
640 if (dsaddr->flags & NFS4_DEVICE_ID_NEG_ENTRY) {
641 /* Already tried to connect, don't try again */
642 dprintk("%s Deviceid marked out of use\n", __func__);
643 return NULL;
645 err = nfs4_ds_connect(s, ds);
646 if (err) {
647 filelayout_mark_devid_negative(dsaddr, err,
648 ntohl(ds->ds_ip_addr));
649 return NULL;
652 return ds;