2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/ioccom.h>
38 #include <sys/mount.h>
39 #include <sys/vnode.h>
44 #include <security/mac/mac_framework.h>
46 #include <ufs/ufs/extattr.h>
47 #include <ufs/ufs/quota.h>
48 #include <ufs/ufs/ufsmount.h>
49 #include <ufs/ufs/inode.h>
51 #include <ufs/ffs/fs.h>
52 #include <ufs/ffs/ffs_extern.h>
54 static d_open_t ffs_susp_open;
55 static d_write_t ffs_susp_rdwr;
56 static d_ioctl_t ffs_susp_ioctl;
58 static struct cdevsw ffs_susp_cdevsw = {
59 .d_version = D_VERSION,
60 .d_open = ffs_susp_open,
61 .d_read = ffs_susp_rdwr,
62 .d_write = ffs_susp_rdwr,
63 .d_ioctl = ffs_susp_ioctl,
67 static struct cdev *ffs_susp_dev;
68 static struct sx ffs_susp_lock;
71 ffs_susp_suspended(struct mount *mp)
75 sx_assert(&ffs_susp_lock, SA_LOCKED);
78 if (ump->um_writesuspended)
84 ffs_susp_open(struct cdev *dev __unused, int flags __unused,
85 int fmt __unused, struct thread *td __unused)
92 ffs_susp_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
104 sx_slock(&ffs_susp_lock);
106 error = devfs_get_cdevpriv((void **)&mp);
108 sx_sunlock(&ffs_susp_lock);
113 devvp = ump->um_devvp;
116 if (ffs_susp_suspended(mp) == 0) {
117 sx_sunlock(&ffs_susp_lock);
121 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
122 ("neither UIO_READ or UIO_WRITE"));
123 KASSERT(uio->uio_segflg == UIO_USERSPACE,
124 ("uio->uio_segflg != UIO_USERSPACE"));
126 cnt = uio->uio_resid;
128 for (i = 0; i < uio->uio_iovcnt; i++) {
129 while (uio->uio_iov[i].iov_len) {
130 base = uio->uio_iov[i].iov_base;
131 len = uio->uio_iov[i].iov_len;
132 if (len > fs->fs_bsize)
134 if (fragoff(fs, uio->uio_offset) != 0 ||
135 fragoff(fs, len) != 0) {
139 error = bread(devvp, btodb(uio->uio_offset), len,
143 if (uio->uio_rw == UIO_WRITE) {
144 error = copyin(base, bp->b_data, len);
146 bp->b_flags |= B_INVAL | B_NOCACHE;
154 error = copyout(bp->b_data, base, len);
159 uio->uio_iov[i].iov_base =
160 (char *)uio->uio_iov[i].iov_base + len;
161 uio->uio_iov[i].iov_len -= len;
162 uio->uio_resid -= len;
163 uio->uio_offset += len;
168 sx_sunlock(&ffs_susp_lock);
170 if (uio->uio_resid < cnt)
177 ffs_susp_suspend(struct mount *mp)
179 struct ufsmount *ump;
182 sx_assert(&ffs_susp_lock, SA_XLOCKED);
184 if (!ffs_own_mount(mp))
186 if (ffs_susp_suspended(mp))
192 * Make sure the calling thread is permitted to access the mounted
193 * device. The permissions can change after we unlock the vnode;
196 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
197 error = VOP_ACCESS(ump->um_devvp, VREAD | VWRITE,
198 curthread->td_ucred, curthread);
199 VOP_UNLOCK(ump->um_devvp, 0);
203 if (mac_mount_check_stat(curthread->td_ucred, mp) != 0)
207 if ((error = vfs_write_suspend(mp, VS_SKIP_UNMOUNT)) != 0)
210 ump->um_writesuspended = 1;
216 ffs_susp_dtor(void *data)
219 struct ufsmount *ump;
223 sx_xlock(&ffs_susp_lock);
225 mp = (struct mount *)data;
229 if (ffs_susp_suspended(mp) == 0) {
230 sx_xunlock(&ffs_susp_lock);
234 KASSERT((mp->mnt_kern_flag & MNTK_SUSPEND) != 0,
235 ("MNTK_SUSPEND not set"));
237 error = ffs_reload(mp, curthread, 1);
239 panic("failed to unsuspend writes on %s", fs->fs_fsmnt);
242 * XXX: The status is kept per-process; the vfs_write_resume() routine
243 * asserts that the resuming thread is the same one that called
244 * vfs_write_suspend(). The cdevpriv data, however, is attached
245 * to the file descriptor, e.g. is inherited during fork. Thus,
246 * it's possible that the resuming process will be different from
247 * the one that started the suspension.
249 * Work around by fooling the check in vfs_write_resume().
251 mp->mnt_susp_owner = curthread;
253 vfs_write_resume(mp, 0);
255 ump->um_writesuspended = 0;
257 sx_xunlock(&ffs_susp_lock);
261 ffs_susp_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
269 * No suspend inside the jail. Allowing it would require making
270 * sure that e.g. the devfs ruleset for that jail permits access
273 if (jailed(td->td_ucred))
276 sx_xlock(&ffs_susp_lock);
280 fsidp = (fsid_t *)addr;
281 mp = vfs_getvfs(fsidp);
286 error = vfs_busy(mp, 0);
290 error = ffs_susp_suspend(mp);
295 error = devfs_set_cdevpriv(mp, ffs_susp_dtor);
296 KASSERT(error == 0, ("devfs_set_cdevpriv failed"));
299 error = devfs_get_cdevpriv((void **)&mp);
303 * This calls ffs_susp_dtor, which in turn unsuspends the fs.
304 * The dtor expects to be called without lock held, because
305 * sometimes it's called from here, and sometimes due to the
306 * file being closed or process exiting.
308 sx_xunlock(&ffs_susp_lock);
309 devfs_clear_cdevpriv();
316 sx_xunlock(&ffs_susp_lock);
322 ffs_susp_initialize(void)
325 sx_init(&ffs_susp_lock, "ffs_susp");
326 ffs_susp_dev = make_dev(&ffs_susp_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
331 ffs_susp_uninitialize(void)
334 destroy_dev(ffs_susp_dev);
335 sx_destroy(&ffs_susp_lock);