2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/ioccom.h>
41 #include <sys/mount.h>
42 #include <sys/vnode.h>
47 #include <security/mac/mac_framework.h>
49 #include <ufs/ufs/extattr.h>
50 #include <ufs/ufs/quota.h>
51 #include <ufs/ufs/ufsmount.h>
52 #include <ufs/ufs/inode.h>
54 #include <ufs/ffs/fs.h>
55 #include <ufs/ffs/ffs_extern.h>
57 static d_open_t ffs_susp_open;
58 static d_write_t ffs_susp_rdwr;
59 static d_ioctl_t ffs_susp_ioctl;
61 static struct cdevsw ffs_susp_cdevsw = {
62 .d_version = D_VERSION,
63 .d_open = ffs_susp_open,
64 .d_read = ffs_susp_rdwr,
65 .d_write = ffs_susp_rdwr,
66 .d_ioctl = ffs_susp_ioctl,
70 static struct cdev *ffs_susp_dev;
71 static struct sx ffs_susp_lock;
74 ffs_susp_suspended(struct mount *mp)
78 sx_assert(&ffs_susp_lock, SA_LOCKED);
81 if (ump->um_writesuspended)
87 ffs_susp_open(struct cdev *dev __unused, int flags __unused,
88 int fmt __unused, struct thread *td __unused)
95 ffs_susp_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
100 struct ufsmount *ump;
107 sx_slock(&ffs_susp_lock);
109 error = devfs_get_cdevpriv((void **)&mp);
111 sx_sunlock(&ffs_susp_lock);
116 devvp = ump->um_devvp;
119 if (ffs_susp_suspended(mp) == 0) {
120 sx_sunlock(&ffs_susp_lock);
124 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
125 ("neither UIO_READ or UIO_WRITE"));
126 KASSERT(uio->uio_segflg == UIO_USERSPACE,
127 ("uio->uio_segflg != UIO_USERSPACE"));
129 cnt = uio->uio_resid;
131 for (i = 0; i < uio->uio_iovcnt; i++) {
132 while (uio->uio_iov[i].iov_len) {
133 base = uio->uio_iov[i].iov_base;
134 len = uio->uio_iov[i].iov_len;
135 if (len > fs->fs_bsize)
137 if (fragoff(fs, uio->uio_offset) != 0 ||
138 fragoff(fs, len) != 0) {
142 error = bread(devvp, btodb(uio->uio_offset), len,
146 if (uio->uio_rw == UIO_WRITE) {
147 error = copyin(base, bp->b_data, len);
149 bp->b_flags |= B_INVAL | B_NOCACHE;
157 error = copyout(bp->b_data, base, len);
162 uio->uio_iov[i].iov_base =
163 (char *)uio->uio_iov[i].iov_base + len;
164 uio->uio_iov[i].iov_len -= len;
165 uio->uio_resid -= len;
166 uio->uio_offset += len;
171 sx_sunlock(&ffs_susp_lock);
173 if (uio->uio_resid < cnt)
180 ffs_susp_suspend(struct mount *mp)
182 struct ufsmount *ump;
185 sx_assert(&ffs_susp_lock, SA_XLOCKED);
187 if (!ffs_own_mount(mp))
189 if (ffs_susp_suspended(mp))
195 * Make sure the calling thread is permitted to access the mounted
196 * device. The permissions can change after we unlock the vnode;
199 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
200 error = VOP_ACCESS(ump->um_devvp, VREAD | VWRITE,
201 curthread->td_ucred, curthread);
202 VOP_UNLOCK(ump->um_devvp, 0);
206 if (mac_mount_check_stat(curthread->td_ucred, mp) != 0)
210 if ((error = vfs_write_suspend(mp, VS_SKIP_UNMOUNT)) != 0)
213 ump->um_writesuspended = 1;
219 ffs_susp_dtor(void *data)
222 struct ufsmount *ump;
226 sx_xlock(&ffs_susp_lock);
228 mp = (struct mount *)data;
232 if (ffs_susp_suspended(mp) == 0) {
233 sx_xunlock(&ffs_susp_lock);
237 KASSERT((mp->mnt_kern_flag & MNTK_SUSPEND) != 0,
238 ("MNTK_SUSPEND not set"));
240 error = ffs_reload(mp, curthread, FFSR_FORCE | FFSR_UNSUSPEND);
242 panic("failed to unsuspend writes on %s", fs->fs_fsmnt);
245 * XXX: The status is kept per-process; the vfs_write_resume() routine
246 * asserts that the resuming thread is the same one that called
247 * vfs_write_suspend(). The cdevpriv data, however, is attached
248 * to the file descriptor, e.g. is inherited during fork. Thus,
249 * it's possible that the resuming process will be different from
250 * the one that started the suspension.
252 * Work around by fooling the check in vfs_write_resume().
254 mp->mnt_susp_owner = curthread;
256 vfs_write_resume(mp, 0);
258 ump->um_writesuspended = 0;
260 sx_xunlock(&ffs_susp_lock);
264 ffs_susp_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
272 * No suspend inside the jail. Allowing it would require making
273 * sure that e.g. the devfs ruleset for that jail permits access
276 if (jailed(td->td_ucred))
279 sx_xlock(&ffs_susp_lock);
283 fsidp = (fsid_t *)addr;
284 mp = vfs_getvfs(fsidp);
289 error = vfs_busy(mp, 0);
293 error = ffs_susp_suspend(mp);
298 error = devfs_set_cdevpriv(mp, ffs_susp_dtor);
299 KASSERT(error == 0, ("devfs_set_cdevpriv failed"));
302 error = devfs_get_cdevpriv((void **)&mp);
306 * This calls ffs_susp_dtor, which in turn unsuspends the fs.
307 * The dtor expects to be called without lock held, because
308 * sometimes it's called from here, and sometimes due to the
309 * file being closed or process exiting.
311 sx_xunlock(&ffs_susp_lock);
312 devfs_clear_cdevpriv();
319 sx_xunlock(&ffs_susp_lock);
325 ffs_susp_initialize(void)
328 sx_init(&ffs_susp_lock, "ffs_susp");
329 ffs_susp_dev = make_dev(&ffs_susp_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
334 ffs_susp_uninitialize(void)
337 destroy_dev(ffs_susp_dev);
338 sx_destroy(&ffs_susp_lock);