2 * SPDX-License-Identifier: BSD-2-Clause
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
31 #include <sys/cdefs.h>
32 #include <sys/systm.h>
35 #include <sys/filedesc.h>
36 #include <sys/ioccom.h>
38 #include <sys/mount.h>
40 #include <sys/vnode.h>
42 #include <security/mac/mac_framework.h>
44 #include <ufs/ufs/extattr.h>
45 #include <ufs/ufs/quota.h>
46 #include <ufs/ufs/ufsmount.h>
47 #include <ufs/ufs/inode.h>
49 #include <ufs/ffs/fs.h>
50 #include <ufs/ffs/ffs_extern.h>
52 static d_open_t ffs_susp_open;
53 static d_write_t ffs_susp_rdwr;
54 static d_ioctl_t ffs_susp_ioctl;
56 static struct cdevsw ffs_susp_cdevsw = {
57 .d_version = D_VERSION,
58 .d_open = ffs_susp_open,
59 .d_read = ffs_susp_rdwr,
60 .d_write = ffs_susp_rdwr,
61 .d_ioctl = ffs_susp_ioctl,
65 static struct cdev *ffs_susp_dev;
66 static struct sx ffs_susp_lock;
69 ffs_susp_suspended(struct mount *mp)
73 sx_assert(&ffs_susp_lock, SA_LOCKED);
76 if ((ump->um_flags & UM_WRITESUSPENDED) != 0)
82 ffs_susp_open(struct cdev *dev __unused, int flags __unused,
83 int fmt __unused, struct thread *td __unused)
90 ffs_susp_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
102 sx_slock(&ffs_susp_lock);
104 error = devfs_get_cdevpriv((void **)&mp);
106 sx_sunlock(&ffs_susp_lock);
111 devvp = ump->um_devvp;
114 if (ffs_susp_suspended(mp) == 0) {
115 sx_sunlock(&ffs_susp_lock);
119 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
120 ("neither UIO_READ or UIO_WRITE"));
121 KASSERT(uio->uio_segflg == UIO_USERSPACE,
122 ("uio->uio_segflg != UIO_USERSPACE"));
124 cnt = uio->uio_resid;
126 for (i = 0; i < uio->uio_iovcnt; i++) {
127 while (uio->uio_iov[i].iov_len) {
128 base = uio->uio_iov[i].iov_base;
129 len = uio->uio_iov[i].iov_len;
130 if (len > fs->fs_bsize)
132 if (fragoff(fs, uio->uio_offset) != 0 ||
133 fragoff(fs, len) != 0) {
137 error = bread(devvp, btodb(uio->uio_offset), len,
141 if (uio->uio_rw == UIO_WRITE) {
142 error = copyin(base, bp->b_data, len);
144 bp->b_flags |= B_INVAL | B_NOCACHE;
152 error = copyout(bp->b_data, base, len);
157 uio->uio_iov[i].iov_base =
158 (char *)uio->uio_iov[i].iov_base + len;
159 uio->uio_iov[i].iov_len -= len;
160 uio->uio_resid -= len;
161 uio->uio_offset += len;
166 sx_sunlock(&ffs_susp_lock);
168 if (uio->uio_resid < cnt)
175 ffs_susp_suspend(struct mount *mp)
177 struct ufsmount *ump;
180 sx_assert(&ffs_susp_lock, SA_XLOCKED);
182 if (!ffs_own_mount(mp))
184 if (ffs_susp_suspended(mp))
190 * Make sure the calling thread is permitted to access the mounted
191 * device. The permissions can change after we unlock the vnode;
194 vn_lock(ump->um_odevvp, LK_EXCLUSIVE | LK_RETRY);
195 error = VOP_ACCESS(ump->um_odevvp, VREAD | VWRITE,
196 curthread->td_ucred, curthread);
197 VOP_UNLOCK(ump->um_odevvp);
201 if (mac_mount_check_stat(curthread->td_ucred, mp) != 0)
205 if ((error = vfs_write_suspend(mp, VS_SKIP_UNMOUNT)) != 0)
209 ump->um_flags |= UM_WRITESUSPENDED;
216 ffs_susp_unsuspend(struct mount *mp)
218 struct ufsmount *ump;
220 sx_assert(&ffs_susp_lock, SA_XLOCKED);
223 * XXX: The status is kept per-process; the vfs_write_resume() routine
224 * asserts that the resuming thread is the same one that called
225 * vfs_write_suspend(). The cdevpriv data, however, is attached
226 * to the file descriptor, e.g. is inherited during fork. Thus,
227 * it's possible that the resuming process will be different from
228 * the one that started the suspension.
230 * Work around by fooling the check in vfs_write_resume().
232 mp->mnt_susp_owner = curthread;
234 vfs_write_resume(mp, 0);
237 ump->um_flags &= ~UM_WRITESUSPENDED;
243 ffs_susp_dtor(void *data)
246 struct ufsmount *ump;
250 sx_xlock(&ffs_susp_lock);
252 mp = (struct mount *)data;
256 if (ffs_susp_suspended(mp) == 0) {
257 sx_xunlock(&ffs_susp_lock);
261 KASSERT((mp->mnt_kern_flag & MNTK_SUSPEND) != 0,
262 ("MNTK_SUSPEND not set"));
264 error = ffs_reload(mp, FFSR_FORCE | FFSR_UNSUSPEND);
266 panic("failed to unsuspend writes on %s", fs->fs_fsmnt);
268 ffs_susp_unsuspend(mp);
269 sx_xunlock(&ffs_susp_lock);
273 ffs_susp_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
281 * No suspend inside the jail. Allowing it would require making
282 * sure that e.g. the devfs ruleset for that jail permits access
285 if (jailed(td->td_ucred))
288 sx_xlock(&ffs_susp_lock);
292 fsidp = (fsid_t *)addr;
293 mp = vfs_getvfs(fsidp);
298 error = vfs_busy(mp, 0);
304 * Require single-thread curproc so that the check is not racey.
305 * XXXKIB: might consider to singlethread curproc instead.
307 error = curproc->p_numthreads > 1 ? EDEADLK :
308 descrip_check_write_mp(curproc->p_fd, mp);
314 error = ffs_susp_suspend(mp);
319 error = devfs_set_cdevpriv(mp, ffs_susp_dtor);
321 ffs_susp_unsuspend(mp);
324 error = devfs_get_cdevpriv((void **)&mp);
328 * This calls ffs_susp_dtor, which in turn unsuspends the fs.
329 * The dtor expects to be called without lock held, because
330 * sometimes it's called from here, and sometimes due to the
331 * file being closed or process exiting.
333 sx_xunlock(&ffs_susp_lock);
334 devfs_clear_cdevpriv();
341 sx_xunlock(&ffs_susp_lock);
347 ffs_susp_initialize(void)
350 sx_init(&ffs_susp_lock, "ffs_susp");
351 ffs_susp_dev = make_dev(&ffs_susp_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
356 ffs_susp_uninitialize(void)
359 destroy_dev(ffs_susp_dev);
360 sx_destroy(&ffs_susp_lock);