AIODiskFile.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 79 Disk IO Routines */
10 
23 #include "squid.h"
24 #include "debug/Stream.h"
25 #include "DiskIO/AIO/AIODiskFile.h"
27 #include "DiskIO/IORequestor.h"
28 #include "DiskIO/ReadRequest.h"
29 #include "DiskIO/WriteRequest.h"
30 #include "fs_io.h"
31 #include "globals.h"
32 
33 #include <cerrno>
34 
35 CBDATA_CLASS_INIT(AIODiskFile);
36 
37 AIODiskFile::AIODiskFile(char const *aPath, AIODiskIOStrategy *aStrategy) : fd(-1), closed(true), error_(false)
38 {
39  assert (aPath);
40  path = aPath;
41  strategy = aStrategy;
42  debugs(79, 3, "AIODiskFile::AIODiskFile: " << aPath);
43 }
44 
45 AIODiskFile::~AIODiskFile()
46 {}
47 
48 void
49 AIODiskFile::error(bool const &aBool)
50 {
51  error_ = aBool;
52 }
53 
54 void
55 AIODiskFile::open(int flags, mode_t, RefCount<IORequestor> callback)
56 {
57  /* Simulate async calls */
58 #if _SQUID_WINDOWS_
59  fd = aio_open(path.termedBuf(), flags);
60 #else
61  fd = file_open(path.termedBuf(), flags);
62 #endif
63 
64  ioRequestor = callback;
65 
66  if (fd < 0) {
67  debugs(79, 3, "got failure (" << errno << ")");
68  error(true);
69  } else {
70  closed = false;
72  debugs(79, 3, "opened FD " << fd);
73  }
74 
75  callback->ioCompletedNotification();
76 }
77 
78 void
79 AIODiskFile::create(int flags, mode_t mode, RefCount<IORequestor> callback)
80 {
81  /* We use the same logic path for open */
82  open(flags, mode, callback);
83 }
84 
85 void
86 AIODiskFile::read(ReadRequest *request)
87 {
88  int slot;
89  async_queue_entry_t *qe;
90 
91  assert(strategy->aq.aq_state == AQ_STATE_SETUP);
92 
93  /* Find a free slot */
94  slot = strategy->findSlot();
95 
96  if (slot < 0) {
97  /* No free slot? Callback error, and return */
98  fatal("Aiee! out of aiocb slots! - TODO fix and wrap file_read\n");
99  debugs(79, DBG_IMPORTANT, "WARNING: out of aiocb slots!");
100  /* fall back to blocking method */
101  // file_read(fd, request->buf, request->len, request->offset, callback, data);
102  return;
103  }
104 
105  /* Mark slot as ours */
106  qe = &strategy->aq.aq_queue[slot];
107 
108  qe->aq_e_state = AQ_ENTRY_USED;
109 
110  qe->aq_e_callback_data = cbdataReference(request);
111 
112  qe->theFile = cbdataReference(this);
113 
114  qe->aq_e_type = AQ_ENTRY_READ;
115 
116  qe->aq_e_free = nullptr;
117 
118  qe->aq_e_buf = request->buf;
119 
120  qe->aq_e_fd = getFD();
121 
122  qe->aq_e_aiocb.aio_fildes = getFD();
123 
124  qe->aq_e_aiocb.aio_nbytes = request->len;
125 
126  qe->aq_e_aiocb.aio_offset = request->offset;
127 
128  qe->aq_e_aiocb.aio_buf = request->buf;
129 
130  /* Account */
131  ++ strategy->aq.aq_numpending;
132 
133  /* Initiate aio */
134  if (aio_read(&qe->aq_e_aiocb) < 0) {
135  int xerrno = errno;
136  fatalf("Aiee! aio_read() returned error (%d) TODO fix and wrap file_read !\n", xerrno);
137  debugs(79, DBG_IMPORTANT, "WARNING: aio_read() returned error: " << xstrerr(xerrno));
138  /* fall back to blocking method */
139  // file_read(fd, request->buf, request->len, request->offset, callback, data);
140  }
141 
142 }
143 
144 void
145 AIODiskFile::write(WriteRequest *request)
146 {
147  int slot;
148  async_queue_entry_t *qe;
149 
150  assert(strategy->aq.aq_state == AQ_STATE_SETUP);
151 
152  /* Find a free slot */
153  slot = strategy->findSlot();
154 
155  if (slot < 0) {
156  /* No free slot? Callback error, and return */
157  fatal("Aiee! out of aiocb slots TODO fix and wrap file_write !\n");
158  debugs(79, DBG_IMPORTANT, "WARNING: out of aiocb slots!");
159  /* fall back to blocking method */
160  // file_write(fd, offset, buf, len, callback, data, freefunc);
161  return;
162  }
163 
164  /* Mark slot as ours */
165  qe = &strategy->aq.aq_queue[slot];
166 
167  qe->aq_e_state = AQ_ENTRY_USED;
168 
169  qe->aq_e_callback_data = cbdataReference(request);
170 
171  qe->theFile = cbdataReference(this);
172 
173  qe->aq_e_type = AQ_ENTRY_WRITE;
174 
175  qe->aq_e_free = request->free_func;
176 
177  qe->aq_e_buf = (void *)request->buf;
178 
179  qe->aq_e_fd = fd;
180 
181  qe->aq_e_aiocb.aio_fildes = fd;
182 
183  qe->aq_e_aiocb.aio_nbytes = request->len;
184 
185  qe->aq_e_aiocb.aio_offset = request->offset;
186 
187  qe->aq_e_aiocb.aio_buf = (void *)request->buf;
188 
189  /* Account */
190  ++strategy->aq.aq_numpending;
191 
192  /* Initiate aio */
193  if (aio_write(&qe->aq_e_aiocb) < 0) {
194  int xerrno = errno;
195  fatalf("Aiee! aio_write() returned error (%d) TODO fix and wrap file_write !\n", xerrno);
196  debugs(79, DBG_IMPORTANT, "WARNING: aio_write() returned error: " << xstrerr(xerrno));
197  /* fall back to blocking method */
198  // file_write(fd, offset, buf, len, callback, data, freefunc);
199  }
200 }
201 
202 void
203 AIODiskFile::close ()
204 {
205  assert (!closed);
206 #if _SQUID_WINDOWS_
207  aio_close(fd);
208 #else
209  file_close(fd);
210 #endif
211 
212  fd = -1;
213  closed = true;
214  assert (ioRequestor != nullptr);
215  ioRequestor->closeCompleted();
216 }
217 
218 bool
219 AIODiskFile::canRead() const
220 {
221  return true;
222 }
223 
224 bool
225 AIODiskFile::canWrite() const
226 {
227  return true;
228 }
229 
230 int
231 AIODiskFile::getFD() const
232 {
233  return fd;
234 }
235 
236 bool
237 AIODiskFile::error() const
238 {
239  return error_;
240 }
241 
242 bool
243 AIODiskFile::ioInProgress() const
244 {
245  return false;
246 }
247 
void fatal(const char *message)
Definition: fatal.cc:28
const char * xstrerr(int error)
Definition: xstrerror.cc:83
void error(char *format,...)
void file_close(int fd)
Definition: fs_io.cc:93
#define cbdataReference(var)
Definition: cbdata.h:348
int store_open_disk_fd
CBDATA_CLASS_INIT(AIODiskFile)
virtual void ioCompletedNotification()=0
#define assert(EX)
Definition: assert.h:17
void fatalf(const char *fmt,...)
Definition: fatal.cc:68
size_t len
Definition: ReadRequest.h:26
FREE * free_func
Definition: WriteRequest.h:28
char * buf
Definition: ReadRequest.h:24
const char * buf
Definition: WriteRequest.h:25
unsigned short mode_t
Definition: types.h:129
#define DBG_IMPORTANT
Definition: Stream.h:38
int file_open(const char *path, int mode)
Definition: fs_io.cc:65
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
off_t offset
Definition: ReadRequest.h:25

 

Introduction

Documentation

Support

Miscellaneous