file_zodb.cpp 3.17 KB
Newer Older
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
1 2
// Copyright (C) 2019-2020  Nexedi SA and Contributors.
//                          Kirill Smelkov <kirr@nexedi.com>
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.

Kirill Smelkov's avatar
.  
Kirill Smelkov committed
20
// File file_zodb.cpp provides blkmmapper functions for _ZBigFile.
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
21
// MMapping is implemented via wcfs client.
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
22

Kirill Smelkov's avatar
.  
Kirill Smelkov committed
23
#include "wcfs/client/wcfs.h"
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
24 25
#include "wendelin/bigfile/file.h"
#include "wendelin/bigfile/virtmem.h"
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
26
#include "bigfile/_bigfile.h"
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
27
#include "bigfile/_file_zodb.h"
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
28
#include <ccan/container_of/container_of.h>
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
29

Kirill Smelkov's avatar
.  
Kirill Smelkov committed
30
static void* zfile_mmap_setup_read(VMA *vma, BigFile *file, blk_t blk, size_t blklen) {
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
31
    _ZBigFile* _zfile = container_of(file, _ZBigFile, __pyx_base.file);
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
32 33

    wcfs::FileH fileh = _zfile->wfileh;
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
34 35 36
    wcfs::Mapping mmap;
    error err;

Kirill Smelkov's avatar
.  
Kirill Smelkov committed
37 38 39
    if (fileh == nil)
        panic("BUG: zfile_mmap_setup_read: ZBigFile.fileh_open did not set .wfileh");

Kirill Smelkov's avatar
.  
Kirill Smelkov committed
40 41
    tie(mmap, err) = fileh->mmap(blk, blklen, vma);
    if (err != nil)
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
42
        panic(v(err));  // XXX
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
43

Kirill Smelkov's avatar
.  
Kirill Smelkov committed
44
    return (void*)vma->addr_start;  // XXX (?) kill - we set vma->addr_*
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
45 46
}

Kirill Smelkov's avatar
.  
Kirill Smelkov committed
47
static int zfile_remmap_blk_read(VMA *vma, BigFile *file, blk_t blk) {
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
48 49 50 51 52
    wcfs::_Mapping* mmap   = static_cast<wcfs::_Mapping*>(vma->mmap_overlay_server);
    _ZBigFile*      _zfile = container_of(file, _ZBigFile, __pyx_base.file);

    if (mmap->fileh != _zfile->wfileh)
        panic("BUG: zfile_remmap_blk_read: vma and _zfile point to different wcfs::FileH");
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
53

Kirill Smelkov's avatar
.  
Kirill Smelkov committed
54 55 56 57
    error err;
    err = mmap->remmap_blk(blk);
    if (err != nil)
        panic(v(err));  // XXX
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
58 59 60 61
    return 0;
}


Kirill Smelkov's avatar
.  
Kirill Smelkov committed
62
static void zfile_munmap(VMA *vma, BigFile *file) {
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
63 64 65 66 67 68
    wcfs::_Mapping* mmap   = static_cast<wcfs::_Mapping*>(vma->mmap_overlay_server);
    _ZBigFile*      _zfile = container_of(file, _ZBigFile, __pyx_base.file);

    if (mmap->fileh != _zfile->wfileh)
        panic("BUG: zfile_remmap_blk_read: vma and _zfile point to different wcfs::FileH");

Kirill Smelkov's avatar
.  
Kirill Smelkov committed
69 70 71 72
    error err;
    err = mmap->unmap();
    if (err != nil)
        panic(v(err));  // XXX
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
73
}
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
74 75


Kirill Smelkov's avatar
.  
Kirill Smelkov committed
76
// NOTE reusing whole bigfile_ops for just .mmap* ops.
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
77
extern const bigfile_ops ZBigFile_mmap_ops;
Kirill Smelkov's avatar
Kirill Smelkov committed
78 79 80 81 82 83 84 85 86
static bigfile_ops _mkZBigFile_mmap_ops() {
    // workaround for "sorry, unimplemented: non-trivial designated initializers not supported"
    bigfile_ops _;
    _.mmap_setup_read   = zfile_mmap_setup_read;
    _.remmap_blk_read   = zfile_remmap_blk_read;
    _.munmap            = zfile_munmap;
    _.loadblk  = NULL;
    _.storeblk = NULL;
    return _;
Kirill Smelkov's avatar
.  
Kirill Smelkov committed
87
};
Kirill Smelkov's avatar
Kirill Smelkov committed
88
const bigfile_ops ZBigFile_mmap_ops = _mkZBigFile_mmap_ops();