~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

« back to all changes in this revision

Viewing changes to src/DiskIO/DiskDaemon/DiskdFile.cc

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2006-11-11 10:32:06 UTC
  • Revision ID: james.westby@ubuntu.com-20061111103206-f3p0r9g0vq44rp3r
Tags: upstream-3.0.PRE5
ImportĀ upstreamĀ versionĀ 3.0.PRE5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * $Id: DiskdFile.cc,v 1.2 2004/12/21 17:28:29 robertc Exp $
 
4
 *
 
5
 * DEBUG: section 79    Squid-side DISKD I/O functions.
 
6
 * AUTHOR: Duane Wessels
 
7
 *
 
8
 * SQUID Web Proxy Cache          http://www.squid-cache.org/
 
9
 * ----------------------------------------------------------
 
10
 *
 
11
 *  Squid is the result of efforts by numerous individuals from
 
12
 *  the Internet community; see the CONTRIBUTORS file for full
 
13
 *  details.   Many organizations have provided support for Squid's
 
14
 *  development; see the SPONSORS file for full details.  Squid is
 
15
 *  Copyrighted (C) 2001 by the Regents of the University of
 
16
 *  California; see the COPYRIGHT file for full details.  Squid
 
17
 *  incorporates software developed and/or copyrighted by other
 
18
 *  sources; see the CREDITS file for full details.
 
19
 *
 
20
 *  This program is free software; you can redistribute it and/or modify
 
21
 *  it under the terms of the GNU General Public License as published by
 
22
 *  the Free Software Foundation; either version 2 of the License, or
 
23
 *  (at your option) any later version.
 
24
 *  
 
25
 *  This program is distributed in the hope that it will be useful,
 
26
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
27
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
28
 *  GNU General Public License for more details.
 
29
 *  
 
30
 *  You should have received a copy of the GNU General Public License
 
31
 *  along with this program; if not, write to the Free Software
 
32
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 
33
 *
 
34
 * CopyRight (c) 2003, Robert Collins <robertc@squid-cache.org>
 
35
 */
 
36
 
 
37
#include "squid.h"
 
38
 
 
39
#include <sys/ipc.h>
 
40
#include <sys/msg.h>
 
41
#include <sys/shm.h>
 
42
 
 
43
#include "DiskdFile.h"
 
44
#include "ConfigOption.h"
 
45
#include "diomsg.h"
 
46
 
 
47
#include "DiskdIOStrategy.h"
 
48
#include "DiskIO/IORequestor.h"
 
49
#include "DiskIO/ReadRequest.h"
 
50
#include "DiskIO/WriteRequest.h"
 
51
CBDATA_CLASS_INIT(DiskdFile);
 
52
 
 
53
void *
 
54
DiskdFile::operator new (size_t)
 
55
{
 
56
    CBDATA_INIT_TYPE(DiskdFile);
 
57
    DiskdFile *result = cbdataAlloc(DiskdFile);
 
58
    /* Mark result as being owned - we want the refcounter to do the delete
 
59
     * call */
 
60
    debug (79,3)("diskdFile with base %p allocating\n", result);
 
61
    return result;
 
62
}
 
63
 
 
64
void
 
65
DiskdFile::operator delete (void *address)
 
66
{
 
67
    DiskdFile *t = static_cast<DiskdFile *>(address);
 
68
    debug (79,3)("diskdFile with base %p deleting\n",t);
 
69
    cbdataFree(t);
 
70
}
 
71
 
 
72
DiskdFile::DiskdFile (char const *aPath, DiskdIOStrategy *anIO) : errorOccured (false), IO(anIO),
 
73
        inProgressIOs (0)
 
74
{
 
75
    assert (aPath);
 
76
    debug (79,3)("DiskdFile::DiskdFile: %s\n", aPath);
 
77
    path_ = xstrdup (aPath);
 
78
    id = diskd_stats.sio_id++;
 
79
}
 
80
 
 
81
DiskdFile::~DiskdFile()
 
82
{
 
83
    assert (inProgressIOs == 0);
 
84
    safe_free (path_);
 
85
}
 
86
 
 
87
void
 
88
DiskdFile::open (int flags, mode_t aMode, IORequestor::Pointer callback)
 
89
{
 
90
    debug (79,3)("DiskdFile::open: %p opening for %p\n", this, callback.getRaw());
 
91
    assert (ioRequestor.getRaw() == NULL);
 
92
    ioRequestor = callback;
 
93
    assert (callback.getRaw());
 
94
    mode = flags;
 
95
    off_t shm_offset;
 
96
    char *buf = (char *)IO->shm.get(&shm_offset);
 
97
    xstrncpy(buf, path_, SHMBUF_BLKSZ);
 
98
    ioAway();
 
99
    int x = IO->send(_MQD_OPEN,
 
100
                     id,
 
101
                     this,
 
102
                     strlen(buf) + 1,
 
103
                     mode,
 
104
                     shm_offset,
 
105
                     NULL);
 
106
 
 
107
    if (x < 0) {
 
108
        ioCompleted();
 
109
        errorOccured = true;
 
110
        //        IO->shm.put (shm_offset);
 
111
        ioRequestor->ioCompletedNotification();
 
112
        ioRequestor = NULL;
 
113
    }
 
114
 
 
115
    diskd_stats.open.ops++;
 
116
}
 
117
 
 
118
void
 
119
DiskdFile::create (int flags, mode_t aMode, IORequestor::Pointer callback)
 
120
{
 
121
    debug (79,3)("DiskdFile::create: %p creating for %p\n", this, callback.getRaw());
 
122
    assert (ioRequestor.getRaw() == NULL);
 
123
    ioRequestor = callback;
 
124
    assert (callback.getRaw());
 
125
    mode = flags;
 
126
    off_t shm_offset;
 
127
    char *buf = (char *)IO->shm.get(&shm_offset);
 
128
    xstrncpy(buf, path_, SHMBUF_BLKSZ);
 
129
    ioAway();
 
130
    int x = IO->send(_MQD_CREATE,
 
131
                     id,
 
132
                     this,
 
133
                     strlen(buf) + 1,
 
134
                     mode,
 
135
                     shm_offset,
 
136
                     NULL);
 
137
 
 
138
    if (x < 0) {
 
139
        ioCompleted();
 
140
        errorOccured = true;
 
141
        //        IO->shm.put (shm_offset);
 
142
        debug(79, 1) ("storeDiskdSend CREATE: %s\n", xstrerror());
 
143
        notifyClient();
 
144
        ioRequestor = NULL;
 
145
        return;
 
146
    }
 
147
 
 
148
    diskd_stats.create.ops++;
 
149
}
 
150
 
 
151
void
 
152
DiskdFile::read(ReadRequest *aRead)
 
153
{
 
154
    assert (ioRequestor.getRaw() != NULL);
 
155
    off_t shm_offset;
 
156
    char *rbuf = (char *)IO->shm.get(&shm_offset);
 
157
    assert(rbuf);
 
158
    ioAway();
 
159
    int x = IO->send(_MQD_READ,
 
160
                     id,
 
161
                     this,
 
162
                     (int) aRead->len,
 
163
                     (int) aRead->offset,
 
164
                     shm_offset,
 
165
                     aRead);
 
166
 
 
167
    if (x < 0) {
 
168
        ioCompleted();
 
169
        errorOccured = true;
 
170
        //        IO->shm.put (shm_offset);
 
171
        debug(79, 1) ("storeDiskdSend READ: %s\n", xstrerror());
 
172
        notifyClient();
 
173
        ioRequestor = NULL;
 
174
        return;
 
175
    }
 
176
 
 
177
    diskd_stats.read.ops++;
 
178
}
 
179
 
 
180
void
 
181
DiskdFile::close()
 
182
{
 
183
    debug (79,3)("DiskdFile::close: %p closing for %p\n", this, ioRequestor.getRaw());
 
184
    assert (ioRequestor.getRaw());
 
185
    ioAway();
 
186
    int x = IO->send(_MQD_CLOSE,
 
187
                     id,
 
188
                     this,
 
189
                     0,
 
190
                     0,
 
191
                     -1,
 
192
                     NULL);
 
193
 
 
194
    if (x < 0) {
 
195
        ioCompleted();
 
196
        errorOccured = true;
 
197
        debug(79, 1) ("storeDiskdSend CLOSE: %s\n", xstrerror());
 
198
        notifyClient();
 
199
        ioRequestor = NULL;
 
200
        return;
 
201
    }
 
202
 
 
203
    diskd_stats.close.ops++;
 
204
}
 
205
 
 
206
bool
 
207
DiskdFile::error() const
 
208
{
 
209
    return errorOccured;
 
210
}
 
211
 
 
212
bool
 
213
DiskdFile::canRead() const
 
214
{
 
215
    return !error();
 
216
}
 
217
 
 
218
bool
 
219
DiskdFile::canNotifyClient() const
 
220
{
 
221
    if (!ioRequestor.getRaw()) {
 
222
        debug (79,3)("DiskdFile::canNotifyClient: No ioRequestor to notify\n");
 
223
        return false;
 
224
    }
 
225
 
 
226
    return true;
 
227
}
 
228
 
 
229
void
 
230
DiskdFile::notifyClient()
 
231
{
 
232
    if (!canNotifyClient()) {
 
233
        return;
 
234
    }
 
235
 
 
236
    ioRequestor->ioCompletedNotification();
 
237
}
 
238
 
 
239
void
 
240
DiskdFile::completed(diomsg *M)
 
241
{
 
242
    assert (M->newstyle);
 
243
 
 
244
    switch (M->mtype) {
 
245
 
 
246
    case _MQD_OPEN:
 
247
        openDone(M);
 
248
        break;
 
249
 
 
250
    case _MQD_CREATE:
 
251
        createDone(M);
 
252
        break;
 
253
 
 
254
    case _MQD_CLOSE:
 
255
        closeDone(M);
 
256
        break;
 
257
 
 
258
    case _MQD_READ:
 
259
        readDone(M);
 
260
        break;
 
261
 
 
262
    case _MQD_WRITE:
 
263
        writeDone(M);
 
264
        break;
 
265
 
 
266
    case _MQD_UNLINK:
 
267
        assert (0);
 
268
        break;
 
269
 
 
270
    default:
 
271
        assert(0);
 
272
        break;
 
273
    }
 
274
}
 
275
 
 
276
void
 
277
DiskdFile::openDone(diomsg *M)
 
278
{
 
279
    statCounter.syscalls.disk.opens++;
 
280
    debug(79, 3) ("storeDiskdOpenDone: status %d\n", M->status);
 
281
 
 
282
    if (M->status < 0) {
 
283
        diskd_stats.open.fail++;
 
284
        errorOccured = true;
 
285
    } else {
 
286
        diskd_stats.open.success++;
 
287
    }
 
288
 
 
289
    ioCompleted();
 
290
    notifyClient();
 
291
}
 
292
 
 
293
void
 
294
DiskdFile::createDone(diomsg *M)
 
295
{
 
296
    statCounter.syscalls.disk.opens++;
 
297
    debug(79, 3) ("storeDiskdCreateDone: status %d\n", M->status);
 
298
 
 
299
    if (M->status < 0) {
 
300
        diskd_stats.create.fail++;
 
301
        errorOccured = true;
 
302
    } else {
 
303
        diskd_stats.create.success++;
 
304
    }
 
305
 
 
306
    ioCompleted();
 
307
    notifyClient();
 
308
}
 
309
 
 
310
void
 
311
DiskdFile::write(WriteRequest *aRequest)
 
312
{
 
313
    debugs(79, 3, "DiskdFile::write: this " << (void *)this << ", buf " << (void *)aRequest->buf << ", off " << aRequest->offset << ", len " << aRequest->len);
 
314
    off_t shm_offset;
 
315
    char *sbuf = (char *)IO->shm.get(&shm_offset);
 
316
    xmemcpy(sbuf, aRequest->buf, aRequest->len);
 
317
 
 
318
    if (aRequest->free_func)
 
319
        aRequest->free_func(const_cast<char *>(aRequest->buf));
 
320
 
 
321
    ioAway();
 
322
 
 
323
    int x = IO->send(_MQD_WRITE,
 
324
                     id,
 
325
                     this,
 
326
                     (int) aRequest->len,
 
327
                     (int) aRequest->offset,
 
328
                     shm_offset,
 
329
                     aRequest);
 
330
 
 
331
    if (x < 0) {
 
332
        ioCompleted()
 
333
        ;
 
334
        errorOccured = true;
 
335
        debug(79, 1) ("storeDiskdSend WRITE: %s\n", xstrerror());
 
336
        //        IO->shm.put (shm_offset);
 
337
        notifyClient();
 
338
        ioRequestor = NULL;
 
339
        return;
 
340
    }
 
341
 
 
342
    diskd_stats.write.ops++;
 
343
}
 
344
 
 
345
void
 
346
DiskdFile::ioAway()
 
347
{
 
348
    ++inProgressIOs;
 
349
}
 
350
 
 
351
void
 
352
DiskdFile::ioCompleted()
 
353
{
 
354
    --inProgressIOs;
 
355
}
 
356
 
 
357
void
 
358
DiskdFile::closeDone(diomsg * M)
 
359
{
 
360
    statCounter.syscalls.disk.closes++;
 
361
    debug(79, 3) ("DiskdFile::closeDone: status %d\n", M->status);
 
362
 
 
363
    if (M->status < 0) {
 
364
        diskd_stats.close.fail++;
 
365
        errorOccured = true;
 
366
    } else {
 
367
        diskd_stats.close.success++;
 
368
    }
 
369
 
 
370
    ioCompleted();
 
371
 
 
372
    if (canNotifyClient())
 
373
        ioRequestor->closeCompleted();
 
374
 
 
375
    ioRequestor = NULL;
 
376
}
 
377
 
 
378
void
 
379
DiskdFile::readDone(diomsg * M)
 
380
{
 
381
    statCounter.syscalls.disk.reads++;
 
382
    debug(79, 3) ("DiskdFile::readDone: status %d\n", M->status);
 
383
    assert (M->requestor);
 
384
    ReadRequest::Pointer readRequest = dynamic_cast<ReadRequest *>(M->requestor);
 
385
    /* remove the free protection */
 
386
    readRequest->RefCountDereference();
 
387
 
 
388
    if (M->status < 0) {
 
389
        diskd_stats.read.fail++;
 
390
        ioCompleted();
 
391
        errorOccured = true;
 
392
        ioRequestor->readCompleted(NULL, -1, DISK_ERROR, readRequest);
 
393
        return;
 
394
    }
 
395
 
 
396
    diskd_stats.read.success++;
 
397
 
 
398
    ioCompleted();
 
399
    ioRequestor->readCompleted (IO->shm.buf + M->shm_offset,  M->status, DISK_OK, readRequest);
 
400
}
 
401
 
 
402
void
 
403
DiskdFile::writeDone(diomsg *M)
 
404
{
 
405
    statCounter.syscalls.disk.writes++;
 
406
    debug(79, 3) ("storeDiskdWriteDone: status %d\n", M->status);
 
407
    assert (M->requestor);
 
408
    WriteRequest::Pointer writeRequest = dynamic_cast<WriteRequest *>(M->requestor);
 
409
    /* remove the free protection */
 
410
    writeRequest->RefCountDereference();
 
411
 
 
412
    if (M->status < 0) {
 
413
        errorOccured = true;
 
414
        diskd_stats.write.fail++;
 
415
        ioCompleted();
 
416
        ioRequestor->writeCompleted (DISK_ERROR,0, writeRequest);
 
417
        return;
 
418
    }
 
419
 
 
420
    diskd_stats.write.success++;
 
421
    ioCompleted();
 
422
    ioRequestor->writeCompleted (DISK_OK,M->status, writeRequest);
 
423
}
 
424
 
 
425
bool
 
426
DiskdFile::ioInProgress()const
 
427
{
 
428
    return inProgressIOs != 0;
 
429
}