This file is indexed.

/usr/include/mongo/util/mmap.h is in mongodb-dev 1:2.4.9-1ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// mmap.h

/*    Copyright 2009 10gen Inc.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

#pragma once
#include <boost/thread/xtime.hpp>
#include "concurrency/rwlock.h"

namespace mongo {

    extern const size_t g_minOSPageSizeBytes;
    void minOSPageSizeBytesTest(size_t minOSPageSizeBytes);  // lame-o

    class MAdvise { 
        void *_p;
        unsigned _len;
    public:
        enum Advice { Sequential=1 , Random=2 };
        MAdvise(void *p, unsigned len, Advice a); 
        ~MAdvise(); // destructor resets the range to MADV_NORMAL
    };

    // lock order: lock dbMutex before this if you lock both
    class LockMongoFilesShared { 
        friend class LockMongoFilesExclusive;
        static RWLockRecursiveNongreedy mmmutex;
        static unsigned era;
        RWLockRecursive::Shared lk;
    public:
        LockMongoFilesShared() : lk(mmmutex) { }

        /** era changes anytime memory maps come and go.  thus you can use this as a cheap way to check 
            if nothing has changed since the last time you locked.  Of course you must be shared locked 
            at the time of this call, otherwise someone could be in progress.  
            
            This is used for yielding; see PageFaultException::touch().
        */
        static unsigned getEra() { return era; }

        static void assertExclusivelyLocked() { mmmutex.assertExclusivelyLocked(); }
        static void assertAtLeastReadLocked() { mmmutex.assertAtLeastReadLocked(); }
    };

    class LockMongoFilesExclusive { 
        RWLockRecursive::Exclusive lk;
    public:
        LockMongoFilesExclusive() : lk(LockMongoFilesShared::mmmutex) { 
            LockMongoFilesShared::era++;
        }
    };

    /* the administrative-ish stuff here */
    class MongoFile : boost::noncopyable {
    public:
        /** Flushable has to fail nicely if the underlying object gets killed */
        class Flushable {
        public:
            virtual ~Flushable() {}
            virtual void flush() = 0;
        };

        virtual ~MongoFile() {}

        enum Options {
            SEQUENTIAL = 1, // hint - e.g. FILE_FLAG_SEQUENTIAL_SCAN on windows
            READONLY = 2    // not contractually guaranteed, but if specified the impl has option to fault writes
        };

        /** @param fun is called for each MongoFile.
            called from within a mutex that MongoFile uses. so be careful not to deadlock.
        */
        template < class F >
        static void forEach( F fun );

        /** note: you need to be in mmmutex when using this. forEach (above) handles that for you automatically. 
*/
        static set<MongoFile*>& getAllFiles();

        // callbacks if you need them
        static void (*notifyPreFlush)();
        static void (*notifyPostFlush)();

        static int flushAll( bool sync ); // returns n flushed
        static long long totalMappedLength();
        static void closeAllFiles( stringstream &message );

        virtual bool isMongoMMF() { return false; }

        string filename() const { return _filename; }
        void setFilename(const std::string& fn);

    private:
        string _filename;
        static int _flushAll( bool sync ); // returns n flushed
    protected:
        virtual void close() = 0;
        virtual void flush(bool sync) = 0;
        /**
         * returns a thread safe object that you can call flush on
         * Flushable has to fail nicely if the underlying object gets killed
         */
        virtual Flushable * prepareFlush() = 0;

        void created(); /* subclass must call after create */

        /* subclass must call in destructor (or at close).
           removes this from pathToFile and other maps
           safe to call more than once, albeit might be wasted work
           ideal to call close to the close, if the close is well before object destruction
        */
        void destroyed(); 

        virtual unsigned long long length() const = 0;
    };

    /** look up a MMF by filename. scoped mutex locking convention.
        example:
          MMFFinderByName finder;
          MongoMMF *a = finder.find("file_name_a");
          MongoMMF *b = finder.find("file_name_b");
    */
    class MongoFileFinder : boost::noncopyable {
    public:
        /** @return The MongoFile object associated with the specified file name.  If no file is open
                    with the specified name, returns null.
        */
        MongoFile* findByPath(const std::string& path) const;

    private:
        LockMongoFilesShared _lk;
    };

    class MemoryMappedFile : public MongoFile {
    protected:
        virtual void* viewForFlushing() { 
            if( views.size() == 0 )
                return 0;
            verify( views.size() == 1 );
            return views[0];
        }
    public:
        MemoryMappedFile();

        virtual ~MemoryMappedFile() {
            LockMongoFilesExclusive lk;
            close();
        }

        virtual void close();

        // Throws exception if file doesn't exist. (dm may2010: not sure if this is always true?)
        void* map(const char *filename);

        /** @param options see MongoFile::Options 
        */
        void* mapWithOptions(const char *filename, int options);

        /* Creates with length if DNE, otherwise uses existing file length,
           passed length.
           @param options MongoFile::Options bits
        */
        void* map(const char *filename, unsigned long long &length, int options = 0 );

        /* Create. Must not exist.
           @param zero fill file with zeros when true
        */
        void* create(const std::string& filename, unsigned long long len, bool zero);

        void flush(bool sync);
        virtual Flushable * prepareFlush();

        long shortLength() const          { return (long) len; }
        unsigned long long length() const { return len; }
        HANDLE getFd() const              { return fd; }
        /** create a new view with the specified properties.
            automatically cleaned up upon close/destruction of the MemoryMappedFile object.
            */
        void* createReadOnlyMap();
        void* createPrivateMap();

        /** make the private map range writable (necessary for our windows implementation) */
        static void makeWritable(void *, unsigned len)
#if defined(_WIN32)
            ;
#else
        { }
#endif

    private:
        static void updateLength( const char *filename, unsigned long long &length );

        HANDLE fd;
        HANDLE maphandle;
        vector<void *> views;
        unsigned long long len;
        
#ifdef _WIN32
        boost::shared_ptr<mutex> _flushMutex;
        void clearWritableBits(void *privateView);
    public:
        static const unsigned ChunkSize = 64 * 1024 * 1024;
        static const unsigned NChunks = 1024 * 1024;
#else
        void clearWritableBits(void *privateView) { }
#endif

    protected:

        /** close the current private view and open a new replacement */
        void* remapPrivateView(void *oldPrivateAddr);
    };

    typedef MemoryMappedFile MMF;

    /** p is called from within a mutex that MongoFile uses.  so be careful not to deadlock. */
    template < class F >
    inline void MongoFile::forEach( F p ) {
        LockMongoFilesShared lklk;
        const set<MongoFile*>& mmfiles = MongoFile::getAllFiles();
        for ( set<MongoFile*>::const_iterator i = mmfiles.begin(); i != mmfiles.end(); i++ )
            p(*i);
    }

#if defined(_WIN32)    
    class ourbitset {
        volatile unsigned bits[MemoryMappedFile::NChunks]; // volatile as we are doing double check locking
    public:
        ourbitset() { 
            memset((void*) bits, 0, sizeof(bits));
        }
        bool get(unsigned i) const { 
            unsigned x = i / 32;
            verify( x < MemoryMappedFile::NChunks );
            return (bits[x] & (1 << (i%32))) != 0;
        }
        void set(unsigned i) { 
            unsigned x = i / 32;
            wassert( x < (MemoryMappedFile::NChunks*2/3) ); // warn if getting close to limit
            verify( x < MemoryMappedFile::NChunks );
            bits[x] |= (1 << (i%32));
        }
        void clear(unsigned i) { 
            unsigned x = i / 32;
            verify( x < MemoryMappedFile::NChunks );
            bits[x] &= ~(1 << (i%32));
        }
    };
    extern ourbitset writable;
    void makeChunkWritable(size_t chunkno);
    inline void MemoryMappedFile::makeWritable(void *_p, unsigned len) {
        size_t p = (size_t) _p;
        unsigned a = p/ChunkSize;
        unsigned b = (p+len)/ChunkSize;
        for( unsigned i = a; i <= b; i++ ) {
            if( !writable.get(i) ) {
                makeChunkWritable(i);
            }
        }
    }

#endif

} // namespace mongo