|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package mount |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "math/rand" |
| 22 | + "os" |
| 23 | + "strings" |
| 24 | + "syscall" |
| 25 | + "time" |
| 26 | + "unsafe" |
| 27 | + |
| 28 | + "github.com/pkg/errors" |
| 29 | + "golang.org/x/sys/unix" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + loopControlPath = "/dev/loop-control" |
| 34 | + loopDevFormat = "/dev/loop%d" |
| 35 | + |
| 36 | + ebusyString = "device or resource busy" |
| 37 | +) |
| 38 | + |
| 39 | +// LoopParams parameters to control loop device setup |
| 40 | +type LoopParams struct { |
| 41 | + // Loop device should forbid write |
| 42 | + Readonly bool |
| 43 | + // Loop device is automatically cleared by kernel when the |
| 44 | + // last opener closes it |
| 45 | + Autoclear bool |
| 46 | + // Use direct IO to access the loop backing file |
| 47 | + Direct bool |
| 48 | +} |
| 49 | + |
| 50 | +func ioctl(fd, req, args uintptr) (uintptr, uintptr, error) { |
| 51 | + r1, r2, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, req, args) |
| 52 | + if errno != 0 { |
| 53 | + return 0, 0, errno |
| 54 | + } |
| 55 | + |
| 56 | + return r1, r2, nil |
| 57 | +} |
| 58 | + |
| 59 | +func getFreeLoopDev() (uint32, error) { |
| 60 | + ctrl, err := os.OpenFile(loopControlPath, os.O_RDWR, 0) |
| 61 | + if err != nil { |
| 62 | + return 0, errors.Errorf("could not open %v: %v", loopControlPath, err) |
| 63 | + } |
| 64 | + defer ctrl.Close() |
| 65 | + num, _, err := ioctl(ctrl.Fd(), unix.LOOP_CTL_GET_FREE, 0) |
| 66 | + if err != nil { |
| 67 | + return 0, errors.Wrap(err, "could not get free loop device") |
| 68 | + } |
| 69 | + return uint32(num), nil |
| 70 | +} |
| 71 | + |
| 72 | +func setupLoopDev(backingFile, loopDev string, param LoopParams) error { |
| 73 | + // 1. Open backing file and loop device |
| 74 | + flags := os.O_RDWR |
| 75 | + if param.Readonly { |
| 76 | + flags = os.O_RDONLY |
| 77 | + } |
| 78 | + |
| 79 | + back, err := os.OpenFile(backingFile, flags, 0) |
| 80 | + if err != nil { |
| 81 | + return errors.Wrapf(err, "could not open backing file: %s", backingFile) |
| 82 | + } |
| 83 | + defer back.Close() |
| 84 | + |
| 85 | + loop, err := os.OpenFile(loopDev, flags, 0) |
| 86 | + if err != nil { |
| 87 | + return errors.Wrapf(err, "could not open loop device: %s", loopDev) |
| 88 | + } |
| 89 | + defer loop.Close() |
| 90 | + |
| 91 | + // 2. Set FD |
| 92 | + if _, _, err = ioctl(loop.Fd(), unix.LOOP_SET_FD, back.Fd()); err != nil { |
| 93 | + return errors.Wrapf(err, "could not set loop fd for device: %s", loopDev) |
| 94 | + } |
| 95 | + |
| 96 | + // 3. Set Info |
| 97 | + info := unix.LoopInfo64{} |
| 98 | + copy(info.File_name[:], backingFile) |
| 99 | + if param.Readonly { |
| 100 | + info.Flags |= unix.LO_FLAGS_READ_ONLY |
| 101 | + } |
| 102 | + |
| 103 | + if param.Autoclear { |
| 104 | + info.Flags |= unix.LO_FLAGS_AUTOCLEAR |
| 105 | + } |
| 106 | + |
| 107 | + if param.Direct { |
| 108 | + info.Flags |= unix.LO_FLAGS_DIRECT_IO |
| 109 | + } |
| 110 | + |
| 111 | + _, _, err = ioctl(loop.Fd(), unix.LOOP_SET_STATUS64, uintptr(unsafe.Pointer(&info))) |
| 112 | + if err == nil { |
| 113 | + return nil |
| 114 | + } |
| 115 | + |
| 116 | + if param.Direct { |
| 117 | + // Retry w/o direct IO flag in case kernel does not support it. The downside is that |
| 118 | + // it will suffer from double cache problem. |
| 119 | + info.Flags &= ^(uint32(unix.LO_FLAGS_DIRECT_IO)) |
| 120 | + _, _, err = ioctl(loop.Fd(), unix.LOOP_SET_STATUS64, uintptr(unsafe.Pointer(&info))) |
| 121 | + if err == nil { |
| 122 | + return nil |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Cleanup loop fd and return error |
| 127 | + _, _, _ = ioctl(loop.Fd(), unix.LOOP_CLR_FD, 0) |
| 128 | + return errors.Errorf("failed to set loop device info: %v", err) |
| 129 | +} |
| 130 | + |
| 131 | +// setupLoop looks for (and possibly creates) a free loop device, and |
| 132 | +// then attaches backingFile to it. |
| 133 | +// |
| 134 | +// When autoclear is true, caller should take care to close it when |
| 135 | +// done with the loop device. The loop device file handle keeps |
| 136 | +// loFlagsAutoclear in effect and we rely on it to clean up the loop |
| 137 | +// device. If caller closes the file handle after mounting the device, |
| 138 | +// kernel will clear the loop device after it is umounted. Otherwise |
| 139 | +// the loop device is cleared when the file handle is closed. |
| 140 | +// |
| 141 | +// When autoclear is false, caller should be responsible to remove |
| 142 | +// the loop device when done with it. |
| 143 | +// |
| 144 | +// Upon success, the file handle to the loop device is returned. |
| 145 | +func setupLoop(backingFile string, param LoopParams) (string, error) { |
| 146 | + for retry := 1; retry < 100; retry++ { |
| 147 | + num, err := getFreeLoopDev() |
| 148 | + if err != nil { |
| 149 | + return "", err |
| 150 | + } |
| 151 | + |
| 152 | + loopDev := fmt.Sprintf(loopDevFormat, num) |
| 153 | + if err := setupLoopDev(backingFile, loopDev, param); err != nil { |
| 154 | + // Per util-linux/sys-utils/losetup.c:create_loop(), |
| 155 | + // free loop device can race and we end up failing |
| 156 | + // with EBUSY when trying to set it up. |
| 157 | + if strings.Contains(err.Error(), ebusyString) { |
| 158 | + // Fallback a bit to avoid live lock |
| 159 | + time.Sleep(time.Millisecond * time.Duration(rand.Intn(retry*10))) |
| 160 | + continue |
| 161 | + } |
| 162 | + return "", err |
| 163 | + } |
| 164 | + |
| 165 | + return loopDev, nil |
| 166 | + } |
| 167 | + |
| 168 | + return "", errors.New("timeout creating new loopback device") |
| 169 | +} |
| 170 | + |
| 171 | +func removeLoop(loopdev string) error { |
| 172 | + file, err := os.Open(loopdev) |
| 173 | + if err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + defer file.Close() |
| 177 | + |
| 178 | + _, _, err = ioctl(file.Fd(), unix.LOOP_CLR_FD, 0) |
| 179 | + return err |
| 180 | +} |
| 181 | + |
| 182 | +// Attach a specified backing file to a loop device |
| 183 | +func AttachLoopDevice(backingFile string) (string, error) { |
| 184 | + return setupLoop(backingFile, LoopParams{}) |
| 185 | +} |
| 186 | + |
| 187 | +// Detach a loop device |
| 188 | +func DetachLoopDevice(devices ...string) error { |
| 189 | + for _, dev := range devices { |
| 190 | + if err := removeLoop(dev); err != nil { |
| 191 | + return errors.Wrapf(err, "failed to remove loop device: %s", dev) |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return nil |
| 196 | +} |
0 commit comments