summaryrefslogtreecommitdiff
path: root/src/mongo/db/storage/mmap_v1/data_file.cpp
blob: 0c88f5184cdea6831650b32ab4e7f77c6f9078ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// data_file.cpp

/**
*    Copyright (C) 2013 10gen Inc.
*
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*    As a special exception, the copyright holders give permission to link the
*    code of portions of this program with the OpenSSL library under certain
*    conditions as described in each individual source file and distribute
*    linked combinations including the program with the OpenSSL library. You
*    must comply with the GNU Affero General Public License in all respects for
*    all of the code used other than as permitted herein. If you modify file(s)
*    with this exception, you may extend this exception to your version of the
*    file(s), but you are not obligated to do so. If you do not wish to do so,
*    delete this exception statement from your version. If you delete this
*    exception statement from all source files in the program, then also delete
*    it in the license file.
*/

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kStorage

#include "mongo/platform/basic.h"

#include "mongo/db/storage/mmap_v1/data_file.h"

#include <boost/filesystem/operations.hpp>

#include "mongo/db/storage/mmap_v1/dur.h"
#include "mongo/db/storage/mmap_v1/mmap_v1_options.h"
#include "mongo/db/operation_context.h"
#include "mongo/util/file_allocator.h"
#include "mongo/util/log.h"

namespace mongo {

using std::endl;

namespace {

void data_file_check(void* _mb) {
    if (sizeof(char*) == 4) {
        uassert(10084,
                "can't map file memory - mongo requires 64 bit build for larger datasets",
                _mb != NULL);
    } else {
        uassert(10085, "can't map file memory", _mb != NULL);
    }
}

}  // namespace


BOOST_STATIC_ASSERT(DataFileHeader::HeaderSize == 8192);
BOOST_STATIC_ASSERT(sizeof(static_cast<DataFileHeader*>(NULL)->data) == 4);
BOOST_STATIC_ASSERT(sizeof(DataFileHeader) - sizeof(static_cast<DataFileHeader*>(NULL)->data) ==
                    DataFileHeader::HeaderSize);

int DataFile::maxSize() {
    if (sizeof(int*) == 4) {
        return 512 * 1024 * 1024;
    } else if (mmapv1GlobalOptions.smallfiles) {
        return 0x7ff00000 >> 2;
    } else {
        return 0x7ff00000;
    }
}

NOINLINE_DECL void DataFile::badOfs(int ofs) const {
    msgasserted(13440,
                str::stream() << "bad offset:" << ofs << " accessing file: " << mmf.filename()
                              << ". See http://dochub.mongodb.org/core/data-recovery");
}

int DataFile::_defaultSize() const {
    int size;

    if (_fileNo <= 4) {
        size = (64 * 1024 * 1024) << _fileNo;
    } else {
        size = 0x7ff00000;
    }

    if (mmapv1GlobalOptions.smallfiles) {
        size = size >> 2;
    }

    return size;
}

/** @return true if found and opened. if uninitialized (prealloc only) does not open. */
Status DataFile::openExisting(const char* filename) {
    invariant(_mb == 0);

    if (!boost::filesystem::exists(filename)) {
        return Status(ErrorCodes::InvalidPath, "DataFile::openExisting - file does not exist");
    }

    if (!mmf.open(filename, false)) {
        return Status(ErrorCodes::InternalError, "DataFile::openExisting - mmf.open failed");
    }

    // The mapped view of the file should never be NULL if the open call above succeeded.
    _mb = mmf.getView();
    invariant(_mb);

    const uint64_t sz = mmf.length();
    invariant(sz <= 0x7fffffff);
    invariant(sz % 4096 == 0);

    if (sz < 64 * 1024 * 1024 && !mmapv1GlobalOptions.smallfiles) {
        if (sz >= 16 * 1024 * 1024 && sz % (1024 * 1024) == 0) {
            log() << "info openExisting file size " << sz
                  << " but mmapv1GlobalOptions.smallfiles=false: " << filename << endl;
        } else {
            log() << "openExisting size " << sz << " less than minimum file size expectation "
                  << filename << endl;
            verify(false);
        }
    }

    data_file_check(_mb);
    return Status::OK();
}

void DataFile::open(OperationContext* txn,
                    const char* filename,
                    int minSize,
                    bool preallocateOnly) {
    long size = _defaultSize();

    while (size < minSize) {
        if (size < maxSize() / 2) {
            size *= 2;
        } else {
            size = maxSize();
            break;
        }
    }

    if (size > maxSize()) {
        size = maxSize();
    }

    invariant(size >= 64 * 1024 * 1024 || mmapv1GlobalOptions.smallfiles);
    invariant(size % 4096 == 0);

    if (preallocateOnly) {
        if (mmapv1GlobalOptions.prealloc) {
            FileAllocator::get()->requestAllocation(filename, size);
        }
        return;
    }

    {
        invariant(_mb == 0);
        unsigned long long sz = size;
        if (mmf.create(filename, sz, false)) {
            _mb = mmf.getView();
        }

        invariant(sz <= 0x7fffffff);
        size = (int)sz;
    }

    data_file_check(_mb);
    header()->init(txn, _fileNo, size, filename);
}

void DataFile::flush(bool sync) {
    mmf.flush(sync);
}

DiskLoc DataFile::allocExtentArea(OperationContext* txn, int size) {
    // The header would be NULL if file open failed. However, if file open failed we should
    // never be entering here.
    invariant(header());
    invariant(size <= header()->unusedLength);

    int offset = header()->unused.getOfs();

    DataFileHeader* h = header();
    *txn->recoveryUnit()->writing(&h->unused) = DiskLoc(_fileNo, offset + size);
    txn->recoveryUnit()->writingInt(h->unusedLength) = h->unusedLength - size;

    return DiskLoc(_fileNo, offset);
}

// -------------------------------------------------------------------------------

void DataFileHeader::init(OperationContext* txn, int fileno, int filelength, const char* filename) {
    if (uninitialized()) {
        DEV log() << "datafileheader::init initializing " << filename << " n:" << fileno << endl;

        massert(13640,
                str::stream() << "DataFileHeader looks corrupt at file open filelength:"
                              << filelength << " fileno:" << fileno,
                filelength > 32768);

        // The writes done in this function must not be rolled back. If the containing
        // UnitOfWork rolls back it should roll back to the state *after* these writes. This
        // will leave the file empty, but available for future use. That is why we go directly
        // to the global dur dirty list rather than going through the OperationContext.
        getDur().createdFile(filename, filelength);

        DataFileHeader* const h = getDur().writing(this);
        h->fileLength = filelength;
        h->version = DataFileVersion::defaultForNewFiles();
        h->unused.set(fileno, HeaderSize);
        h->unusedLength = fileLength - HeaderSize - 16;
        h->freeListStart.Null();
        h->freeListEnd.Null();
    } else {
        checkUpgrade(txn);
    }
}

void DataFileHeader::checkUpgrade(OperationContext* txn) {
    if (freeListStart == DiskLoc(0, 0)) {
        // we are upgrading from 2.4 to 2.6
        invariant(freeListEnd == DiskLoc(0, 0));  // both start and end should be (0,0) or real
        WriteUnitOfWork wunit(txn);
        *txn->recoveryUnit()->writing(&freeListStart) = DiskLoc();
        *txn->recoveryUnit()->writing(&freeListEnd) = DiskLoc();
        wunit.commit();
    }
}
}