shm_ofd.go

  1//go:build (linux || darwin) && (386 || arm || amd64 || arm64 || riscv64 || ppc64le) && !(sqlite3_flock || sqlite3_dotlk)
  2
  3package vfs
  4
  5import (
  6	"context"
  7	"io"
  8	"os"
  9	"sync"
 10	"time"
 11
 12	"github.com/tetratelabs/wazero/api"
 13	"golang.org/x/sys/unix"
 14
 15	"github.com/ncruces/go-sqlite3/internal/util"
 16)
 17
 18type vfsShm struct {
 19	*os.File
 20	path     string
 21	regions  []*util.MappedRegion
 22	readOnly bool
 23	fileLock bool
 24	blocking bool
 25	sync.Mutex
 26}
 27
 28var _ blockingSharedMemory = &vfsShm{}
 29
 30func (s *vfsShm) shmOpen() _ErrorCode {
 31	if s.File == nil {
 32		f, err := os.OpenFile(s.path,
 33			os.O_RDWR|os.O_CREATE|_O_NOFOLLOW, 0666)
 34		if err != nil {
 35			f, err = os.OpenFile(s.path,
 36				os.O_RDONLY|os.O_CREATE|_O_NOFOLLOW, 0666)
 37			s.readOnly = true
 38		}
 39		if err != nil {
 40			return _CANTOPEN
 41		}
 42		s.File = f
 43	}
 44	if s.fileLock {
 45		return _OK
 46	}
 47
 48	// Dead man's switch.
 49	if lock, rc := osTestLock(s.File, _SHM_DMS, 1); rc != _OK {
 50		return _IOERR_LOCK
 51	} else if lock == unix.F_WRLCK {
 52		return _BUSY
 53	} else if lock == unix.F_UNLCK {
 54		if s.readOnly {
 55			return _READONLY_CANTINIT
 56		}
 57		// Do not use a blocking lock here.
 58		// If the lock cannot be obtained immediately,
 59		// it means some other connection is truncating the file.
 60		// And after it has done so, it will not release its lock,
 61		// but only downgrade it to a shared lock.
 62		// So no point in blocking here.
 63		// The call below to obtain the shared DMS lock may use a blocking lock.
 64		if rc := osWriteLock(s.File, _SHM_DMS, 1, 0); rc != _OK {
 65			return rc
 66		}
 67		if err := s.Truncate(0); err != nil {
 68			return _IOERR_SHMOPEN
 69		}
 70	}
 71	rc := osReadLock(s.File, _SHM_DMS, 1, time.Millisecond)
 72	s.fileLock = rc == _OK
 73	return rc
 74}
 75
 76func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, extend bool) (ptr_t, _ErrorCode) {
 77	// Ensure size is a multiple of the OS page size.
 78	if int(size)&(unix.Getpagesize()-1) != 0 {
 79		return 0, _IOERR_SHMMAP
 80	}
 81
 82	if rc := s.shmOpen(); rc != _OK {
 83		return 0, rc
 84	}
 85
 86	// Check if file is big enough.
 87	o, err := s.Seek(0, io.SeekEnd)
 88	if err != nil {
 89		return 0, _IOERR_SHMSIZE
 90	}
 91	if n := (int64(id) + 1) * int64(size); n > o {
 92		if !extend {
 93			return 0, _OK
 94		}
 95		if s.readOnly || osAllocate(s.File, n) != nil {
 96			return 0, _IOERR_SHMSIZE
 97		}
 98	}
 99
100	r, err := util.MapRegion(ctx, mod, s.File, int64(id)*int64(size), size, s.readOnly)
101	if err != nil {
102		return 0, _IOERR_SHMMAP
103	}
104	s.regions = append(s.regions, r)
105	if s.readOnly {
106		return r.Ptr, _READONLY
107	}
108	return r.Ptr, _OK
109}
110
111func (s *vfsShm) shmLock(offset, n int32, flags _ShmFlag) _ErrorCode {
112	// Argument check.
113	switch {
114	case n <= 0:
115		panic(util.AssertErr())
116	case offset < 0 || offset+n > _SHM_NLOCK:
117		panic(util.AssertErr())
118	case n != 1 && flags&_SHM_EXCLUSIVE == 0:
119		panic(util.AssertErr())
120	}
121	switch flags {
122	case
123		_SHM_LOCK | _SHM_SHARED,
124		_SHM_LOCK | _SHM_EXCLUSIVE,
125		_SHM_UNLOCK | _SHM_SHARED,
126		_SHM_UNLOCK | _SHM_EXCLUSIVE:
127		//
128	default:
129		panic(util.AssertErr())
130	}
131
132	var timeout time.Duration
133	if s.blocking {
134		timeout = time.Millisecond
135	}
136
137	switch {
138	case flags&_SHM_UNLOCK != 0:
139		return osUnlock(s.File, _SHM_BASE+int64(offset), int64(n))
140	case flags&_SHM_SHARED != 0:
141		return osReadLock(s.File, _SHM_BASE+int64(offset), int64(n), timeout)
142	case flags&_SHM_EXCLUSIVE != 0:
143		return osWriteLock(s.File, _SHM_BASE+int64(offset), int64(n), timeout)
144	default:
145		panic(util.AssertErr())
146	}
147}
148
149func (s *vfsShm) shmUnmap(delete bool) {
150	if s.File == nil {
151		return
152	}
153
154	// Unmap regions.
155	for _, r := range s.regions {
156		r.Unmap()
157	}
158	s.regions = nil
159
160	// Close the file.
161	if delete {
162		os.Remove(s.path)
163	}
164	s.Close()
165	s.File = nil
166}
167
168func (s *vfsShm) shmBarrier() {
169	s.Lock()
170	//lint:ignore SA2001 memory barrier.
171	s.Unlock()
172}
173
174func (s *vfsShm) shmEnableBlocking(block bool) {
175	s.blocking = block
176}