osd.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2005, 2006
  3. * Avishay Traeger (avishay@gmail.com) (avishay@il.ibm.com)
  4. * Copyright (C) 2005, 2006
  5. * International Business Machines
  6. * Copyright (C) 2008, 2009
  7. * Boaz Harrosh <bharrosh@panasas.com>
  8. *
  9. * This file is part of exofs.
  10. *
  11. * exofs is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation. Since it is based on ext2, and the only
  14. * valid version of GPL for the Linux kernel is version 2, the only valid
  15. * version of GPL for exofs is version 2.
  16. *
  17. * exofs is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with exofs; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include <scsi/scsi_device.h>
  27. #include <scsi/osd_sense.h>
  28. #include "exofs.h"
  29. int exofs_check_ok_resid(struct osd_request *or, u64 *in_resid, u64 *out_resid)
  30. {
  31. struct osd_sense_info osi;
  32. int ret = osd_req_decode_sense(or, &osi);
  33. if (ret) { /* translate to Linux codes */
  34. if (osi.additional_code == scsi_invalid_field_in_cdb) {
  35. if (osi.cdb_field_offset == OSD_CFO_STARTING_BYTE)
  36. ret = -EFAULT;
  37. if (osi.cdb_field_offset == OSD_CFO_OBJECT_ID)
  38. ret = -ENOENT;
  39. else
  40. ret = -EINVAL;
  41. } else if (osi.additional_code == osd_quota_error)
  42. ret = -ENOSPC;
  43. else
  44. ret = -EIO;
  45. }
  46. /* FIXME: should be include in osd_sense_info */
  47. if (in_resid)
  48. *in_resid = or->in.req ? or->in.req->data_len : 0;
  49. if (out_resid)
  50. *out_resid = or->out.req ? or->out.req->data_len : 0;
  51. return ret;
  52. }
  53. void exofs_make_credential(u8 cred_a[OSD_CAP_LEN], const struct osd_obj_id *obj)
  54. {
  55. osd_sec_init_nosec_doall_caps(cred_a, obj, false, true);
  56. }
  57. /*
  58. * Perform a synchronous OSD operation.
  59. */
  60. int exofs_sync_op(struct osd_request *or, int timeout, uint8_t *credential)
  61. {
  62. int ret;
  63. or->timeout = timeout;
  64. ret = osd_finalize_request(or, 0, credential, NULL);
  65. if (ret) {
  66. EXOFS_DBGMSG("Faild to osd_finalize_request() => %d\n", ret);
  67. return ret;
  68. }
  69. ret = osd_execute_request(or);
  70. if (ret)
  71. EXOFS_DBGMSG("osd_execute_request() => %d\n", ret);
  72. /* osd_req_decode_sense(or, ret); */
  73. return ret;
  74. }
  75. /*
  76. * Perform an asynchronous OSD operation.
  77. */
  78. int exofs_async_op(struct osd_request *or, osd_req_done_fn *async_done,
  79. void *caller_context, u8 *cred)
  80. {
  81. int ret;
  82. ret = osd_finalize_request(or, 0, cred, NULL);
  83. if (ret) {
  84. EXOFS_DBGMSG("Faild to osd_finalize_request() => %d\n", ret);
  85. return ret;
  86. }
  87. ret = osd_execute_request_async(or, async_done, caller_context);
  88. if (ret)
  89. EXOFS_DBGMSG("osd_execute_request_async() => %d\n", ret);
  90. return ret;
  91. }
  92. int extract_attr_from_req(struct osd_request *or, struct osd_attr *attr)
  93. {
  94. struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
  95. void *iter = NULL;
  96. int nelem;
  97. do {
  98. nelem = 1;
  99. osd_req_decode_get_attr_list(or, &cur_attr, &nelem, &iter);
  100. if ((cur_attr.attr_page == attr->attr_page) &&
  101. (cur_attr.attr_id == attr->attr_id)) {
  102. attr->len = cur_attr.len;
  103. attr->val_ptr = cur_attr.val_ptr;
  104. return 0;
  105. }
  106. } while (iter);
  107. return -EIO;
  108. }
  109. int osd_req_read_kern(struct osd_request *or,
  110. const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
  111. {
  112. struct request_queue *req_q = or->osd_dev->scsi_device->request_queue;
  113. struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
  114. if (!bio)
  115. return -ENOMEM;
  116. osd_req_read(or, obj, bio, offset);
  117. return 0;
  118. }
  119. int osd_req_write_kern(struct osd_request *or,
  120. const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
  121. {
  122. struct request_queue *req_q = or->osd_dev->scsi_device->request_queue;
  123. struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
  124. if (!bio)
  125. return -ENOMEM;
  126. osd_req_write(or, obj, bio, offset);
  127. return 0;
  128. }