2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/ioccom.h>
40 #include <sys/mount.h>
41 #include <sys/vnode.h>
46 #include <security/mac/mac_framework.h>
48 #include <ufs/ufs/extattr.h>
49 #include <ufs/ufs/quota.h>
50 #include <ufs/ufs/ufsmount.h>
51 #include <ufs/ufs/inode.h>
53 #include <ufs/ffs/fs.h>
54 #include <ufs/ffs/ffs_extern.h>
56 static d_open_t ffs_susp_open;
57 static d_write_t ffs_susp_rdwr;
58 static d_ioctl_t ffs_susp_ioctl;
60 static struct cdevsw ffs_susp_cdevsw = {
61 .d_version = D_VERSION,
62 .d_open = ffs_susp_open,
63 .d_read = ffs_susp_rdwr,
64 .d_write = ffs_susp_rdwr,
65 .d_ioctl = ffs_susp_ioctl,
69 static struct cdev *ffs_susp_dev;
70 static struct sx ffs_susp_lock;
73 ffs_susp_suspended(struct mount *mp)
77 sx_assert(&ffs_susp_lock, SA_LOCKED);
80 if ((ump->um_flags & UM_WRITESUSPENDED) != 0)
86 ffs_susp_open(struct cdev *dev __unused, int flags __unused,
87 int fmt __unused, struct thread *td __unused)
94 ffs_susp_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
106 sx_slock(&ffs_susp_lock);
108 error = devfs_get_cdevpriv((void **)&mp);
110 sx_sunlock(&ffs_susp_lock);
115 devvp = ump->um_devvp;
118 if (ffs_susp_suspended(mp) == 0) {
119 sx_sunlock(&ffs_susp_lock);
123 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
124 ("neither UIO_READ or UIO_WRITE"));
125 KASSERT(uio->uio_segflg == UIO_USERSPACE,
126 ("uio->uio_segflg != UIO_USERSPACE"));
128 cnt = uio->uio_resid;
130 for (i = 0; i < uio->uio_iovcnt; i++) {
131 while (uio->uio_iov[i].iov_len) {
132 base = uio->uio_iov[i].iov_base;
133 len = uio->uio_iov[i].iov_len;
134 if (len > fs->fs_bsize)
136 if (fragoff(fs, uio->uio_offset) != 0 ||
137 fragoff(fs, len) != 0) {
141 error = bread(devvp, btodb(uio->uio_offset), len,
145 if (uio->uio_rw == UIO_WRITE) {
146 error = copyin(base, bp->b_data, len);
148 bp->b_flags |= B_INVAL | B_NOCACHE;
156 error = copyout(bp->b_data, base, len);
161 uio->uio_iov[i].iov_base =
162 (char *)uio->uio_iov[i].iov_base + len;
163 uio->uio_iov[i].iov_len -= len;
164 uio->uio_resid -= len;
165 uio->uio_offset += len;
170 sx_sunlock(&ffs_susp_lock);
172 if (uio->uio_resid < cnt)
179 ffs_susp_suspend(struct mount *mp)
181 struct ufsmount *ump;
184 sx_assert(&ffs_susp_lock, SA_XLOCKED);
186 if (!ffs_own_mount(mp))
188 if (ffs_susp_suspended(mp))
194 * Make sure the calling thread is permitted to access the mounted
195 * device. The permissions can change after we unlock the vnode;
198 vn_lock(ump->um_odevvp, LK_EXCLUSIVE | LK_RETRY);
199 error = VOP_ACCESS(ump->um_odevvp, VREAD | VWRITE,
200 curthread->td_ucred, curthread);
201 VOP_UNLOCK(ump->um_odevvp);
205 if (mac_mount_check_stat(curthread->td_ucred, mp) != 0)
209 if ((error = vfs_write_suspend(mp, VS_SKIP_UNMOUNT)) != 0)
213 ump->um_flags |= UM_WRITESUSPENDED;
220 ffs_susp_unsuspend(struct mount *mp)
222 struct ufsmount *ump;
224 sx_assert(&ffs_susp_lock, SA_XLOCKED);
227 * XXX: The status is kept per-process; the vfs_write_resume() routine
228 * asserts that the resuming thread is the same one that called
229 * vfs_write_suspend(). The cdevpriv data, however, is attached
230 * to the file descriptor, e.g. is inherited during fork. Thus,
231 * it's possible that the resuming process will be different from
232 * the one that started the suspension.
234 * Work around by fooling the check in vfs_write_resume().
236 mp->mnt_susp_owner = curthread;
238 vfs_write_resume(mp, 0);
241 ump->um_flags &= ~UM_WRITESUSPENDED;
247 ffs_susp_dtor(void *data)
250 struct ufsmount *ump;
254 sx_xlock(&ffs_susp_lock);
256 mp = (struct mount *)data;
260 if (ffs_susp_suspended(mp) == 0) {
261 sx_xunlock(&ffs_susp_lock);
265 KASSERT((mp->mnt_kern_flag & MNTK_SUSPEND) != 0,
266 ("MNTK_SUSPEND not set"));
268 error = ffs_reload(mp, curthread, FFSR_FORCE | FFSR_UNSUSPEND);
270 panic("failed to unsuspend writes on %s", fs->fs_fsmnt);
272 ffs_susp_unsuspend(mp);
273 sx_xunlock(&ffs_susp_lock);
277 ffs_susp_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
285 * No suspend inside the jail. Allowing it would require making
286 * sure that e.g. the devfs ruleset for that jail permits access
289 if (jailed(td->td_ucred))
292 sx_xlock(&ffs_susp_lock);
296 fsidp = (fsid_t *)addr;
297 mp = vfs_getvfs(fsidp);
302 error = vfs_busy(mp, 0);
306 error = ffs_susp_suspend(mp);
311 error = devfs_set_cdevpriv(mp, ffs_susp_dtor);
313 ffs_susp_unsuspend(mp);
316 error = devfs_get_cdevpriv((void **)&mp);
320 * This calls ffs_susp_dtor, which in turn unsuspends the fs.
321 * The dtor expects to be called without lock held, because
322 * sometimes it's called from here, and sometimes due to the
323 * file being closed or process exiting.
325 sx_xunlock(&ffs_susp_lock);
326 devfs_clear_cdevpriv();
333 sx_xunlock(&ffs_susp_lock);
339 ffs_susp_initialize(void)
342 sx_init(&ffs_susp_lock, "ffs_susp");
343 ffs_susp_dev = make_dev(&ffs_susp_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
348 ffs_susp_uninitialize(void)
351 destroy_dev(ffs_susp_dev);
352 sx_destroy(&ffs_susp_lock);