aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Elder <elder@dreamhost.com>2012-02-07 13:03:36 -0500
committerAlex Elder <elder@dreamhost.com>2012-03-22 11:47:50 -0400
commit00f1f36ffa29a6b8e0529770bce2a44585ab3af0 (patch)
tree573bed76392a12b4568adcf704ec7b77e108be08
parentfed4c143ba8f08c8bddfdc7c69738e691a06d565 (diff)
rbd: do some refactoring
A few blocks of code are rearranged a bit here: - In rbd_header_from_disk(): - Don't bother computing snap_count until we're sure the on-disk header starts with a good signature. - Move a few independent lines of code so they are *after* a check for a failed memory allocation. - Get rid of unnecessary local variable "ret". - Make a few other changes in rbd_read_header(), similar to the above--just moving things around a bit while preserving the functionality. - In rbd_rq_fn(), just assign rq in the while loop's controlling expression rather than duplicating it before and at the end of the loop body. This allows the use of "continue" rather than "goto next" in a number of spots. - Rearrange the logic in snap_by_name(). End result is the same. Signed-off-by: Alex Elder <elder@dreamhost.com>
-rw-r--r--drivers/block/rbd.c80
1 files changed, 40 insertions, 40 deletions
diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
index b0f6812f8f99..a42b28e7f3fa 100644
--- a/drivers/block/rbd.c
+++ b/drivers/block/rbd.c
@@ -486,19 +486,20 @@ static int rbd_header_from_disk(struct rbd_image_header *header,
486 gfp_t gfp_flags) 486 gfp_t gfp_flags)
487{ 487{
488 int i; 488 int i;
489 u32 snap_count = le32_to_cpu(ondisk->snap_count); 489 u32 snap_count;
490 int ret = -ENOMEM;
491 490
492 if (memcmp(ondisk, RBD_HEADER_TEXT, sizeof(RBD_HEADER_TEXT))) 491 if (memcmp(ondisk, RBD_HEADER_TEXT, sizeof(RBD_HEADER_TEXT)))
493 return -ENXIO; 492 return -ENXIO;
494 493
495 init_rwsem(&header->snap_rwsem); 494 snap_count = le32_to_cpu(ondisk->snap_count);
496 header->snap_names_len = le64_to_cpu(ondisk->snap_names_len);
497 header->snapc = kmalloc(sizeof(struct ceph_snap_context) + 495 header->snapc = kmalloc(sizeof(struct ceph_snap_context) +
498 snap_count * sizeof (*ondisk), 496 snap_count * sizeof (*ondisk),
499 gfp_flags); 497 gfp_flags);
500 if (!header->snapc) 498 if (!header->snapc)
501 return -ENOMEM; 499 return -ENOMEM;
500
501 init_rwsem(&header->snap_rwsem);
502 header->snap_names_len = le64_to_cpu(ondisk->snap_names_len);
502 if (snap_count) { 503 if (snap_count) {
503 header->snap_names = kmalloc(header->snap_names_len, 504 header->snap_names = kmalloc(header->snap_names_len,
504 GFP_KERNEL); 505 GFP_KERNEL);
@@ -544,7 +545,7 @@ err_names:
544 kfree(header->snap_names); 545 kfree(header->snap_names);
545err_snapc: 546err_snapc:
546 kfree(header->snapc); 547 kfree(header->snapc);
547 return ret; 548 return -ENOMEM;
548} 549}
549 550
550static int snap_index(struct rbd_image_header *header, int snap_num) 551static int snap_index(struct rbd_image_header *header, int snap_num)
@@ -568,19 +569,20 @@ static int snap_by_name(struct rbd_image_header *header, const char *snap_name,
568 int i; 569 int i;
569 char *p = header->snap_names; 570 char *p = header->snap_names;
570 571
571 for (i = 0; i < header->total_snaps; i++, p += strlen(p) + 1) { 572 for (i = 0; i < header->total_snaps; i++) {
572 if (strcmp(snap_name, p) == 0) 573 if (!strcmp(snap_name, p)) {
573 break;
574 }
575 if (i == header->total_snaps)
576 return -ENOENT;
577 if (seq)
578 *seq = header->snapc->snaps[i];
579 574
580 if (size) 575 /* Found it. Pass back its id and/or size */
581 *size = header->snap_sizes[i];
582 576
583 return i; 577 if (seq)
578 *seq = header->snapc->snaps[i];
579 if (size)
580 *size = header->snap_sizes[i];
581 return i;
582 }
583 p += strlen(p) + 1; /* Skip ahead to the next name */
584 }
585 return -ENOENT;
584} 586}
585 587
586static int rbd_header_set_snap(struct rbd_device *dev, u64 *size) 588static int rbd_header_set_snap(struct rbd_device *dev, u64 *size)
@@ -1444,9 +1446,7 @@ static void rbd_rq_fn(struct request_queue *q)
1444 struct request *rq; 1446 struct request *rq;
1445 struct bio_pair *bp = NULL; 1447 struct bio_pair *bp = NULL;
1446 1448
1447 rq = blk_fetch_request(q); 1449 while ((rq = blk_fetch_request(q))) {
1448
1449 while (1) {
1450 struct bio *bio; 1450 struct bio *bio;
1451 struct bio *rq_bio, *next_bio = NULL; 1451 struct bio *rq_bio, *next_bio = NULL;
1452 bool do_write; 1452 bool do_write;
@@ -1464,7 +1464,7 @@ static void rbd_rq_fn(struct request_queue *q)
1464 /* filter out block requests we don't understand */ 1464 /* filter out block requests we don't understand */
1465 if ((rq->cmd_type != REQ_TYPE_FS)) { 1465 if ((rq->cmd_type != REQ_TYPE_FS)) {
1466 __blk_end_request_all(rq, 0); 1466 __blk_end_request_all(rq, 0);
1467 goto next; 1467 continue;
1468 } 1468 }
1469 1469
1470 /* deduce our operation (read, write) */ 1470 /* deduce our operation (read, write) */
@@ -1475,7 +1475,7 @@ static void rbd_rq_fn(struct request_queue *q)
1475 rq_bio = rq->bio; 1475 rq_bio = rq->bio;
1476 if (do_write && rbd_dev->read_only) { 1476 if (do_write && rbd_dev->read_only) {
1477 __blk_end_request_all(rq, -EROFS); 1477 __blk_end_request_all(rq, -EROFS);
1478 goto next; 1478 continue;
1479 } 1479 }
1480 1480
1481 spin_unlock_irq(q->queue_lock); 1481 spin_unlock_irq(q->queue_lock);
@@ -1489,7 +1489,7 @@ static void rbd_rq_fn(struct request_queue *q)
1489 if (!coll) { 1489 if (!coll) {
1490 spin_lock_irq(q->queue_lock); 1490 spin_lock_irq(q->queue_lock);
1491 __blk_end_request_all(rq, -ENOMEM); 1491 __blk_end_request_all(rq, -ENOMEM);
1492 goto next; 1492 continue;
1493 } 1493 }
1494 1494
1495 do { 1495 do {
@@ -1535,8 +1535,6 @@ next_seg:
1535 if (bp) 1535 if (bp)
1536 bio_pair_release(bp); 1536 bio_pair_release(bp);
1537 spin_lock_irq(q->queue_lock); 1537 spin_lock_irq(q->queue_lock);
1538next:
1539 rq = blk_fetch_request(q);
1540 } 1538 }
1541} 1539}
1542 1540
@@ -1588,15 +1586,16 @@ static int rbd_read_header(struct rbd_device *rbd_dev,
1588 ssize_t rc; 1586 ssize_t rc;
1589 struct rbd_image_header_ondisk *dh; 1587 struct rbd_image_header_ondisk *dh;
1590 int snap_count = 0; 1588 int snap_count = 0;
1591 u64 snap_names_len = 0;
1592 u64 ver; 1589 u64 ver;
1590 size_t len;
1593 1591
1592 /*
1593 * First reads the fixed-size header to determine the number
1594 * of snapshots, then re-reads it, along with all snapshot
1595 * records as well as their stored names.
1596 */
1597 len = sizeof (*dh);
1594 while (1) { 1598 while (1) {
1595 int len = sizeof(*dh) +
1596 snap_count * sizeof(struct rbd_image_snap_ondisk) +
1597 snap_names_len;
1598
1599 rc = -ENOMEM;
1600 dh = kmalloc(len, GFP_KERNEL); 1599 dh = kmalloc(len, GFP_KERNEL);
1601 if (!dh) 1600 if (!dh)
1602 return -ENOMEM; 1601 return -ENOMEM;
@@ -1611,21 +1610,22 @@ static int rbd_read_header(struct rbd_device *rbd_dev,
1611 1610
1612 rc = rbd_header_from_disk(header, dh, snap_count, GFP_KERNEL); 1611 rc = rbd_header_from_disk(header, dh, snap_count, GFP_KERNEL);
1613 if (rc < 0) { 1612 if (rc < 0) {
1614 if (rc == -ENXIO) { 1613 if (rc == -ENXIO)
1615 pr_warning("unrecognized header format" 1614 pr_warning("unrecognized header format"
1616 " for image %s", rbd_dev->obj); 1615 " for image %s", rbd_dev->obj);
1617 }
1618 goto out_dh; 1616 goto out_dh;
1619 } 1617 }
1620 1618
1621 if (snap_count != header->total_snaps) { 1619 if (snap_count == header->total_snaps)
1622 snap_count = header->total_snaps; 1620 break;
1623 snap_names_len = header->snap_names_len; 1621
1624 rbd_header_free(header); 1622 snap_count = header->total_snaps;
1625 kfree(dh); 1623 len = sizeof (*dh) +
1626 continue; 1624 snap_count * sizeof(struct rbd_image_snap_ondisk) +
1627 } 1625 header->snap_names_len;
1628 break; 1626
1627 rbd_header_free(header);
1628 kfree(dh);
1629 } 1629 }
1630 header->obj_version = ver; 1630 header->obj_version = ver;
1631 1631