file_descriptor.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // Copyright (C) 2012 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "update_engine/payload_consumer/file_descriptor.h"
  17. #include <fcntl.h>
  18. #include <linux/fs.h>
  19. #include <sys/ioctl.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <base/posix/eintr_wrapper.h>
  23. #include "update_engine/common/utils.h"
  24. namespace chromeos_update_engine {
  25. bool EintrSafeFileDescriptor::Open(const char* path, int flags, mode_t mode) {
  26. CHECK_EQ(fd_, -1);
  27. return ((fd_ = HANDLE_EINTR(open(path, flags, mode))) >= 0);
  28. }
  29. bool EintrSafeFileDescriptor::Open(const char* path, int flags) {
  30. CHECK_EQ(fd_, -1);
  31. return ((fd_ = HANDLE_EINTR(open(path, flags))) >= 0);
  32. }
  33. ssize_t EintrSafeFileDescriptor::Read(void* buf, size_t count) {
  34. CHECK_GE(fd_, 0);
  35. return HANDLE_EINTR(read(fd_, buf, count));
  36. }
  37. ssize_t EintrSafeFileDescriptor::Write(const void* buf, size_t count) {
  38. CHECK_GE(fd_, 0);
  39. // Attempt repeated writes, as long as some progress is being made.
  40. char* char_buf = const_cast<char*>(reinterpret_cast<const char*>(buf));
  41. ssize_t written = 0;
  42. while (count > 0) {
  43. ssize_t ret = HANDLE_EINTR(write(fd_, char_buf, count));
  44. // Fail on either an error or no progress.
  45. if (ret <= 0)
  46. return (written ? written : ret);
  47. written += ret;
  48. count -= ret;
  49. char_buf += ret;
  50. }
  51. return written;
  52. }
  53. off64_t EintrSafeFileDescriptor::Seek(off64_t offset, int whence) {
  54. CHECK_GE(fd_, 0);
  55. return lseek64(fd_, offset, whence);
  56. }
  57. uint64_t EintrSafeFileDescriptor::BlockDevSize() {
  58. if (fd_ < 0)
  59. return 0;
  60. struct stat stbuf;
  61. if (fstat(fd_, &stbuf) < 0) {
  62. PLOG(ERROR) << "Error stat-ing fd " << fd_;
  63. return 0;
  64. }
  65. if (!S_ISBLK(stbuf.st_mode))
  66. return 0;
  67. off_t block_size = utils::BlockDevSize(fd_);
  68. return block_size < 0 ? 0 : block_size;
  69. }
  70. bool EintrSafeFileDescriptor::BlkIoctl(int request,
  71. uint64_t start,
  72. uint64_t length,
  73. int* result) {
  74. // If the ioctl BLKZEROOUT is not defined, just fail to perform any of these
  75. // operations.
  76. #ifndef BLKZEROOUT
  77. return false;
  78. #else // defined(BLKZEROOUT)
  79. DCHECK(request == BLKDISCARD || request == BLKZEROOUT ||
  80. request == BLKSECDISCARD);
  81. // On some devices, the BLKDISCARD will actually read back as zeros, instead
  82. // of "undefined" data. The BLKDISCARDZEROES ioctl tells whether that's the
  83. // case, so we issue a BLKDISCARD in those cases to speed up the writes.
  84. unsigned int arg;
  85. if (request == BLKZEROOUT && ioctl(fd_, BLKDISCARDZEROES, &arg) == 0 && arg)
  86. request = BLKDISCARD;
  87. // Ensure the |fd_| is in O_DIRECT mode during this operation, so the write
  88. // cache for this region is invalidated. This is required since otherwise
  89. // reading back this region could consume stale data from the cache.
  90. int flags = fcntl(fd_, F_GETFL, 0);
  91. if (flags == -1) {
  92. PLOG(WARNING) << "Couldn't get flags on fd " << fd_;
  93. return false;
  94. }
  95. if ((flags & O_DIRECT) == 0 && fcntl(fd_, F_SETFL, flags | O_DIRECT) == -1) {
  96. PLOG(WARNING) << "Couldn't set O_DIRECT on fd " << fd_;
  97. return false;
  98. }
  99. uint64_t range[2] = {start, length};
  100. *result = ioctl(fd_, request, range);
  101. if ((flags & O_DIRECT) == 0 && fcntl(fd_, F_SETFL, flags) == -1) {
  102. PLOG(WARNING) << "Couldn't remove O_DIRECT on fd " << fd_;
  103. return false;
  104. }
  105. return true;
  106. #endif // defined(BLKZEROOUT)
  107. }
  108. bool EintrSafeFileDescriptor::Flush() {
  109. CHECK_GE(fd_, 0);
  110. return true;
  111. }
  112. bool EintrSafeFileDescriptor::Close() {
  113. CHECK_GE(fd_, 0);
  114. if (IGNORE_EINTR(close(fd_)))
  115. return false;
  116. fd_ = -1;
  117. return true;
  118. }
  119. } // namespace chromeos_update_engine