bchlib_write.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /****************************************************************************
  2. * drivers/bch/bchlib_write.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. #include <string.h>
  28. #include <errno.h>
  29. #include <assert.h>
  30. #include <debug.h>
  31. #include <nuttx/fs/fs.h>
  32. #include "bch.h"
  33. /****************************************************************************
  34. * Public Functions
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Name: bchlib_write
  38. *
  39. * Description:
  40. * Write to the block device set-up by bchlib_setup as if it were a
  41. * character device.
  42. *
  43. ****************************************************************************/
  44. ssize_t bchlib_write(FAR void *handle, FAR const char *buffer, size_t offset,
  45. size_t len)
  46. {
  47. FAR struct bchlib_s *bch = (FAR struct bchlib_s *)handle;
  48. size_t nsectors;
  49. size_t sector;
  50. uint16_t sectoffset;
  51. size_t nbytes;
  52. size_t byteswritten;
  53. int ret;
  54. /* Get rid of this special case right away */
  55. if (len < 1)
  56. {
  57. return 0;
  58. }
  59. /* Convert the file position into a sector number and offset. */
  60. sector = offset / bch->sectsize;
  61. sectoffset = offset - sector * bch->sectsize;
  62. if (sector >= bch->nsectors)
  63. {
  64. return -EFBIG;
  65. }
  66. /* Write the initial partial sector */
  67. byteswritten = 0;
  68. if (sectoffset > 0)
  69. {
  70. /* Read the full sector into the sector buffer */
  71. ret = bchlib_readsector(bch, sector);
  72. if (ret < 0)
  73. {
  74. return ret;
  75. }
  76. /* Copy the tail end of the sector from the user buffer */
  77. if (sectoffset + len > bch->sectsize)
  78. {
  79. nbytes = bch->sectsize - sectoffset;
  80. }
  81. else
  82. {
  83. nbytes = len;
  84. }
  85. memcpy(&bch->buffer[sectoffset], buffer, nbytes);
  86. bch->dirty = true;
  87. /* Adjust pointers and counts */
  88. sector++;
  89. if (sector >= bch->nsectors)
  90. {
  91. return nbytes;
  92. }
  93. byteswritten = nbytes;
  94. buffer += nbytes;
  95. len -= nbytes;
  96. }
  97. /* Then write all of the full sectors following the partial sector
  98. * directly from the user buffer.
  99. */
  100. if (len >= bch->sectsize)
  101. {
  102. nsectors = len / bch->sectsize;
  103. if (sector + nsectors > bch->nsectors)
  104. {
  105. nsectors = bch->nsectors - sector;
  106. }
  107. /* Flush the dirty sector to keep the sector sequence */
  108. ret = bchlib_flushsector(bch);
  109. if (ret < 0)
  110. {
  111. ferr("ERROR: Flush failed: %d\n", ret);
  112. return ret;
  113. }
  114. /* Write the contiguous sectors */
  115. ret = bch->inode->u.i_bops->write(bch->inode, (FAR uint8_t *)buffer,
  116. sector, nsectors);
  117. if (ret < 0)
  118. {
  119. ferr("ERROR: Write failed: %d\n", ret);
  120. return ret;
  121. }
  122. /* Adjust pointers and counts */
  123. sector += nsectors;
  124. nbytes = nsectors * bch->sectsize;
  125. byteswritten += nbytes;
  126. if (sector >= bch->nsectors)
  127. {
  128. return byteswritten;
  129. }
  130. buffer += nbytes;
  131. len -= nbytes;
  132. }
  133. /* Then write any partial final sector */
  134. if (len > 0)
  135. {
  136. /* Read the sector into the sector buffer */
  137. ret = bchlib_readsector(bch, sector);
  138. if (ret < 0)
  139. {
  140. return ret;
  141. }
  142. /* Copy the head end of the sector from the user buffer */
  143. memcpy(bch->buffer, buffer, len);
  144. bch->dirty = true;
  145. /* Adjust counts */
  146. byteswritten += len;
  147. }
  148. return byteswritten;
  149. }