xref: /illumos-gate/usr/src/uts/common/fs/udfs/udf_vnops.c (revision 06042525)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5aa59c4cbSrsb  * Common Development and Distribution License (the "License").
6aa59c4cbSrsb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
213b862e9aSRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23b39a0235SMilan Cermak  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
2672102e74SBryan Cantrill /*
2754207fd2SJerry Jelinek  * Copyright 2015, Joyent, Inc.
2872102e74SBryan Cantrill  */
2972102e74SBryan Cantrill 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/time.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #include <sys/resource.h>
377c478bd9Sstevel@tonic-gate #include <sys/signal.h>
387c478bd9Sstevel@tonic-gate #include <sys/cred.h>
397c478bd9Sstevel@tonic-gate #include <sys/user.h>
407c478bd9Sstevel@tonic-gate #include <sys/buf.h>
417c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
42aa59c4cbSrsb #include <sys/vfs_opreg.h>
437c478bd9Sstevel@tonic-gate #include <sys/stat.h>
447c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
457c478bd9Sstevel@tonic-gate #include <sys/mode.h>
467c478bd9Sstevel@tonic-gate #include <sys/proc.h>
477c478bd9Sstevel@tonic-gate #include <sys/disp.h>
487c478bd9Sstevel@tonic-gate #include <sys/file.h>
497c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
507c478bd9Sstevel@tonic-gate #include <sys/flock.h>
517c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
527c478bd9Sstevel@tonic-gate #include <sys/uio.h>
537c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
547c478bd9Sstevel@tonic-gate #include <sys/conf.h>
557c478bd9Sstevel@tonic-gate #include <sys/errno.h>
567c478bd9Sstevel@tonic-gate #include <sys/mman.h>
577c478bd9Sstevel@tonic-gate #include <sys/fbuf.h>
587c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
597c478bd9Sstevel@tonic-gate #include <sys/debug.h>
607c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
617c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
627c478bd9Sstevel@tonic-gate #include <sys/dirent.h>
637c478bd9Sstevel@tonic-gate #include <sys/errno.h>
647c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
657c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
667c478bd9Sstevel@tonic-gate #include <sys/mount.h>
677c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
687c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
697c478bd9Sstevel@tonic-gate #include <sys/policy.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #include <vm/hat.h>
727c478bd9Sstevel@tonic-gate #include <vm/page.h>
737c478bd9Sstevel@tonic-gate #include <vm/pvn.h>
747c478bd9Sstevel@tonic-gate #include <vm/as.h>
757c478bd9Sstevel@tonic-gate #include <vm/seg.h>
767c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
777c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
787c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
797c478bd9Sstevel@tonic-gate #include <vm/rm.h>
807c478bd9Sstevel@tonic-gate #include <vm/page.h>
817c478bd9Sstevel@tonic-gate #include <sys/swap.h>
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #include <sys/fs/udf_volume.h>
867c478bd9Sstevel@tonic-gate #include <sys/fs/udf_inode.h>
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static int32_t udf_open(struct vnode **,
89da6c28aaSamw 	int32_t, struct cred *, caller_context_t *);
907c478bd9Sstevel@tonic-gate static int32_t udf_close(struct vnode *,
91da6c28aaSamw 	int32_t, int32_t, offset_t, struct cred *, caller_context_t *);
927c478bd9Sstevel@tonic-gate static int32_t udf_read(struct vnode *,
93da6c28aaSamw 	struct uio *, int32_t, struct cred *, caller_context_t *);
947c478bd9Sstevel@tonic-gate static int32_t udf_write(struct vnode *,
95da6c28aaSamw 	struct uio *, int32_t, struct cred *, caller_context_t *);
967c478bd9Sstevel@tonic-gate static int32_t udf_ioctl(struct vnode *,
97da6c28aaSamw 	int32_t, intptr_t, int32_t, struct cred *, int32_t *,
98da6c28aaSamw 	caller_context_t *);
997c478bd9Sstevel@tonic-gate static int32_t udf_getattr(struct vnode *,
100da6c28aaSamw 	struct vattr *, int32_t, struct cred *, caller_context_t *);
1017c478bd9Sstevel@tonic-gate static int32_t udf_setattr(struct vnode *,
1027c478bd9Sstevel@tonic-gate 	struct vattr *, int32_t, struct cred *, caller_context_t *);
1037c478bd9Sstevel@tonic-gate static int32_t udf_access(struct vnode *,
104da6c28aaSamw 	int32_t, int32_t, struct cred *, caller_context_t *);
1057c478bd9Sstevel@tonic-gate static int32_t udf_lookup(struct vnode *,
1067c478bd9Sstevel@tonic-gate 	char *, struct vnode **, struct pathname *,
107da6c28aaSamw 	int32_t, struct vnode *, struct cred *,
108da6c28aaSamw 	caller_context_t *, int *, pathname_t *);
1097c478bd9Sstevel@tonic-gate static int32_t udf_create(struct vnode *,
1107c478bd9Sstevel@tonic-gate 	char *, struct vattr *, enum vcexcl,
111da6c28aaSamw 	int32_t, struct vnode **, struct cred *, int32_t,
112da6c28aaSamw 	caller_context_t *, vsecattr_t *);
1137c478bd9Sstevel@tonic-gate static int32_t udf_remove(struct vnode *,
114da6c28aaSamw 	char *, struct cred *, caller_context_t *, int);
1157c478bd9Sstevel@tonic-gate static int32_t udf_link(struct vnode *,
116da6c28aaSamw 	struct vnode *, char *, struct cred *, caller_context_t *, int);
1177c478bd9Sstevel@tonic-gate static int32_t udf_rename(struct vnode *,
118da6c28aaSamw 	char *, struct vnode *, char *, struct cred *, caller_context_t *, int);
1197c478bd9Sstevel@tonic-gate static int32_t udf_mkdir(struct vnode *,
120da6c28aaSamw 	char *, struct vattr *, struct vnode **, struct cred *,
121da6c28aaSamw 	caller_context_t *, int, vsecattr_t *);
1227c478bd9Sstevel@tonic-gate static int32_t udf_rmdir(struct vnode *,
123da6c28aaSamw 	char *, struct vnode *, struct cred *, caller_context_t *, int);
1247c478bd9Sstevel@tonic-gate static int32_t udf_readdir(struct vnode *,
125da6c28aaSamw 	struct uio *, struct cred *, int32_t *, caller_context_t *, int);
1267c478bd9Sstevel@tonic-gate static int32_t udf_symlink(struct vnode *,
127da6c28aaSamw 	char *, struct vattr *, char *, struct cred *, caller_context_t *, int);
1287c478bd9Sstevel@tonic-gate static int32_t udf_readlink(struct vnode *,
129da6c28aaSamw 	struct uio *, struct cred *, caller_context_t *);
1307c478bd9Sstevel@tonic-gate static int32_t udf_fsync(struct vnode *,
131da6c28aaSamw 	int32_t, struct cred *, caller_context_t *);
1327c478bd9Sstevel@tonic-gate static void udf_inactive(struct vnode *,
133da6c28aaSamw 	struct cred *, caller_context_t *);
134da6c28aaSamw static int32_t udf_fid(struct vnode *, struct fid *, caller_context_t *);
1357c478bd9Sstevel@tonic-gate static int udf_rwlock(struct vnode *, int32_t, caller_context_t *);
1367c478bd9Sstevel@tonic-gate static void udf_rwunlock(struct vnode *, int32_t, caller_context_t *);
137da6c28aaSamw static int32_t udf_seek(struct vnode *, offset_t, offset_t *,
138da6c28aaSamw 	caller_context_t *);
1397c478bd9Sstevel@tonic-gate static int32_t udf_frlock(struct vnode *, int32_t,
140da6c28aaSamw 	struct flock64 *, int32_t, offset_t, struct flk_callback *, cred_t *,
141da6c28aaSamw 	caller_context_t *);
1427c478bd9Sstevel@tonic-gate static int32_t udf_space(struct vnode *, int32_t,
1437c478bd9Sstevel@tonic-gate 	struct flock64 *, int32_t, offset_t, cred_t *, caller_context_t *);
1447c478bd9Sstevel@tonic-gate static int32_t udf_getpage(struct vnode *, offset_t,
1457c478bd9Sstevel@tonic-gate 	size_t, uint32_t *, struct page **, size_t,
146da6c28aaSamw 	struct seg *, caddr_t, enum seg_rw, struct cred *, caller_context_t *);
1477c478bd9Sstevel@tonic-gate static int32_t udf_putpage(struct vnode *, offset_t,
148da6c28aaSamw 	size_t, int32_t, struct cred *, caller_context_t *);
1497c478bd9Sstevel@tonic-gate static int32_t udf_map(struct vnode *, offset_t, struct as *,
150da6c28aaSamw 	caddr_t *, size_t, uint8_t, uint8_t, uint32_t, struct cred *,
151da6c28aaSamw 	caller_context_t *);
1527c478bd9Sstevel@tonic-gate static int32_t udf_addmap(struct vnode *, offset_t, struct as *,
153da6c28aaSamw 	caddr_t, size_t, uint8_t, uint8_t, uint32_t, struct cred *,
154da6c28aaSamw 	caller_context_t *);
1557c478bd9Sstevel@tonic-gate static int32_t udf_delmap(struct vnode *, offset_t, struct as *,
156da6c28aaSamw 	caddr_t, size_t, uint32_t, uint32_t, uint32_t, struct cred *,
157da6c28aaSamw 	caller_context_t *);
1587c478bd9Sstevel@tonic-gate static int32_t udf_l_pathconf(struct vnode *, int32_t,
159da6c28aaSamw 	ulong_t *, struct cred *, caller_context_t *);
1607c478bd9Sstevel@tonic-gate static int32_t udf_pageio(struct vnode *, struct page *,
161da6c28aaSamw 	u_offset_t, size_t, int32_t, struct cred *, caller_context_t *);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate int32_t ud_getpage_miss(struct vnode *, u_offset_t,
1647c478bd9Sstevel@tonic-gate 	size_t, struct seg *, caddr_t, page_t *pl[],
1657c478bd9Sstevel@tonic-gate 	size_t, enum seg_rw, int32_t);
1667c478bd9Sstevel@tonic-gate void ud_getpage_ra(struct vnode *, u_offset_t, struct seg *, caddr_t);
1677c478bd9Sstevel@tonic-gate int32_t ud_putpages(struct vnode *, offset_t, size_t, int32_t, struct cred *);
1687c478bd9Sstevel@tonic-gate int32_t ud_page_fill(struct ud_inode *, page_t *,
1697c478bd9Sstevel@tonic-gate 	u_offset_t, uint32_t, u_offset_t *);
1707c478bd9Sstevel@tonic-gate int32_t ud_iodone(struct buf *);
1717c478bd9Sstevel@tonic-gate int32_t ud_rdip(struct ud_inode *, struct uio *, int32_t, cred_t *);
1727c478bd9Sstevel@tonic-gate int32_t ud_wrip(struct ud_inode *, struct uio *, int32_t, cred_t *);
1737c478bd9Sstevel@tonic-gate int32_t ud_multi_strat(struct ud_inode *, page_t *, struct buf *, u_offset_t);
1747c478bd9Sstevel@tonic-gate int32_t ud_slave_done(struct buf *);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * Structures to control multiple IO operations to get or put pages
1787c478bd9Sstevel@tonic-gate  * that are backed by discontiguous blocks. The master struct is
1797c478bd9Sstevel@tonic-gate  * a dummy that holds the original bp from pageio_setup. The
1807c478bd9Sstevel@tonic-gate  * slave struct holds the working bp's to do the actual IO. Once
1817c478bd9Sstevel@tonic-gate  * all the slave IOs complete. The master is processed as if a single
1827c478bd9Sstevel@tonic-gate  * IO op has completed.
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate uint32_t master_index = 0;
1857c478bd9Sstevel@tonic-gate typedef struct mio_master {
1867c478bd9Sstevel@tonic-gate 	kmutex_t	mm_mutex;	/* protect the fields below */
1877c478bd9Sstevel@tonic-gate 	int32_t		mm_size;
1887c478bd9Sstevel@tonic-gate 	buf_t		*mm_bp;		/* original bp */
1897c478bd9Sstevel@tonic-gate 	int32_t		mm_resid;	/* bytes remaining to transfer */
1907c478bd9Sstevel@tonic-gate 	int32_t		mm_error;	/* accumulated error from slaves */
1917c478bd9Sstevel@tonic-gate 	int32_t		mm_index;	/* XXX debugging */
1927c478bd9Sstevel@tonic-gate } mio_master_t;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate typedef struct mio_slave {
1957c478bd9Sstevel@tonic-gate 	buf_t		ms_buf;		/* working buffer for this IO chunk */
1967c478bd9Sstevel@tonic-gate 	mio_master_t	*ms_ptr;	/* pointer to master */
1977c478bd9Sstevel@tonic-gate } mio_slave_t;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate struct vnodeops *udf_vnodeops;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate const fs_operation_def_t udf_vnodeops_template[] = {
202aa59c4cbSrsb 	VOPNAME_OPEN,		{ .vop_open = udf_open },
203aa59c4cbSrsb 	VOPNAME_CLOSE,		{ .vop_close = udf_close },
204aa59c4cbSrsb 	VOPNAME_READ,		{ .vop_read = udf_read },
205aa59c4cbSrsb 	VOPNAME_WRITE,		{ .vop_write = udf_write },
206aa59c4cbSrsb 	VOPNAME_IOCTL,		{ .vop_ioctl = udf_ioctl },
207aa59c4cbSrsb 	VOPNAME_GETATTR,	{ .vop_getattr = udf_getattr },
208aa59c4cbSrsb 	VOPNAME_SETATTR,	{ .vop_setattr = udf_setattr },
209aa59c4cbSrsb 	VOPNAME_ACCESS,		{ .vop_access = udf_access },
210aa59c4cbSrsb 	VOPNAME_LOOKUP,		{ .vop_lookup = udf_lookup },
211aa59c4cbSrsb 	VOPNAME_CREATE,		{ .vop_create = udf_create },
212aa59c4cbSrsb 	VOPNAME_REMOVE,		{ .vop_remove = udf_remove },
213aa59c4cbSrsb 	VOPNAME_LINK,		{ .vop_link = udf_link },
214aa59c4cbSrsb 	VOPNAME_RENAME,		{ .vop_rename = udf_rename },
215aa59c4cbSrsb 	VOPNAME_MKDIR,		{ .vop_mkdir = udf_mkdir },
216aa59c4cbSrsb 	VOPNAME_RMDIR,		{ .vop_rmdir = udf_rmdir },
217aa59c4cbSrsb 	VOPNAME_READDIR,	{ .vop_readdir = udf_readdir },
218aa59c4cbSrsb 	VOPNAME_SYMLINK,	{ .vop_symlink = udf_symlink },
219aa59c4cbSrsb 	VOPNAME_READLINK,	{ .vop_readlink = udf_readlink },
220aa59c4cbSrsb 	VOPNAME_FSYNC,		{ .vop_fsync = udf_fsync },
221aa59c4cbSrsb 	VOPNAME_INACTIVE,	{ .vop_inactive = udf_inactive },
222aa59c4cbSrsb 	VOPNAME_FID,		{ .vop_fid = udf_fid },
223aa59c4cbSrsb 	VOPNAME_RWLOCK,		{ .vop_rwlock = udf_rwlock },
224aa59c4cbSrsb 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = udf_rwunlock },
225aa59c4cbSrsb 	VOPNAME_SEEK,		{ .vop_seek = udf_seek },
226aa59c4cbSrsb 	VOPNAME_FRLOCK,		{ .vop_frlock = udf_frlock },
227aa59c4cbSrsb 	VOPNAME_SPACE,		{ .vop_space = udf_space },
228aa59c4cbSrsb 	VOPNAME_GETPAGE,	{ .vop_getpage = udf_getpage },
229aa59c4cbSrsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = udf_putpage },
230aa59c4cbSrsb 	VOPNAME_MAP,		{ .vop_map = udf_map },
231aa59c4cbSrsb 	VOPNAME_ADDMAP,		{ .vop_addmap = udf_addmap },
232aa59c4cbSrsb 	VOPNAME_DELMAP,		{ .vop_delmap = udf_delmap },
233aa59c4cbSrsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = udf_l_pathconf },
234aa59c4cbSrsb 	VOPNAME_PAGEIO,		{ .vop_pageio = udf_pageio },
235aa59c4cbSrsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
236aa59c4cbSrsb 	NULL,			NULL
2377c478bd9Sstevel@tonic-gate };
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /* ARGSUSED */
2407c478bd9Sstevel@tonic-gate static int32_t
udf_open(struct vnode ** vpp,int32_t flag,struct cred * cr,caller_context_t * ct)241da6c28aaSamw udf_open(
242da6c28aaSamw 	struct vnode **vpp,
243da6c28aaSamw 	int32_t flag,
244da6c28aaSamw 	struct cred *cr,
245da6c28aaSamw 	caller_context_t *ct)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	ud_printf("udf_open\n");
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	return (0);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /* ARGSUSED */
2537c478bd9Sstevel@tonic-gate static int32_t
udf_close(struct vnode * vp,int32_t flag,int32_t count,offset_t offset,struct cred * cr,caller_context_t * ct)254da6c28aaSamw udf_close(
255da6c28aaSamw 	struct vnode *vp,
256da6c28aaSamw 	int32_t flag,
257da6c28aaSamw 	int32_t count,
258da6c28aaSamw 	offset_t offset,
259da6c28aaSamw 	struct cred *cr,
260da6c28aaSamw 	caller_context_t *ct)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	ud_printf("udf_close\n");
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	ITIMES(ip);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
2697c478bd9Sstevel@tonic-gate 	cleanshares(vp, ttoproc(curthread)->p_pid);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * Push partially filled cluster at last close.
2737c478bd9Sstevel@tonic-gate 	 * ``last close'' is approximated because the dnlc
2747c478bd9Sstevel@tonic-gate 	 * may have a hold on the vnode.
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	if (vp->v_count <= 2 && vp->v_type != VBAD) {
2777c478bd9Sstevel@tonic-gate 		struct ud_inode *ip = VTOI(vp);
2787c478bd9Sstevel@tonic-gate 		if (ip->i_delaylen) {
2797c478bd9Sstevel@tonic-gate 			(void) ud_putpages(vp, ip->i_delayoff, ip->i_delaylen,
2806f5f1c63SDonghai Qiao 			    B_ASYNC | B_FREE, cr);
2817c478bd9Sstevel@tonic-gate 			ip->i_delaylen = 0;
2827c478bd9Sstevel@tonic-gate 		}
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	return (0);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
288da6c28aaSamw /* ARGSUSED */
2897c478bd9Sstevel@tonic-gate static int32_t
udf_read(struct vnode * vp,struct uio * uiop,int32_t ioflag,struct cred * cr,caller_context_t * ct)290da6c28aaSamw udf_read(
291da6c28aaSamw 	struct vnode *vp,
292da6c28aaSamw 	struct uio *uiop,
293da6c28aaSamw 	int32_t ioflag,
294da6c28aaSamw 	struct cred *cr,
295da6c28aaSamw 	caller_context_t *ct)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
2987c478bd9Sstevel@tonic-gate 	int32_t error;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	ud_printf("udf_read\n");
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
3037c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_READER);
3047c478bd9Sstevel@tonic-gate #endif
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&ip->i_rwlock));
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_char)) {
3097c478bd9Sstevel@tonic-gate 		/*
3107c478bd9Sstevel@tonic-gate 		 * udf_getattr ends up being called by chklock
3117c478bd9Sstevel@tonic-gate 		 */
3127c478bd9Sstevel@tonic-gate 		error = chklock(vp, FREAD, uiop->uio_loffset,
3136f5f1c63SDonghai Qiao 		    uiop->uio_resid, uiop->uio_fmode, ct);
3147c478bd9Sstevel@tonic-gate 		if (error) {
3157c478bd9Sstevel@tonic-gate 			goto end;
3167c478bd9Sstevel@tonic-gate 		}
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
3207c478bd9Sstevel@tonic-gate 	error = ud_rdip(ip, uiop, ioflag, cr);
3217c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate end:
3247c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
3257c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
3267c478bd9Sstevel@tonic-gate #endif
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	return (error);
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate int32_t ud_WRITES = 1;
3337c478bd9Sstevel@tonic-gate int32_t ud_HW = 96 * 1024;
3347c478bd9Sstevel@tonic-gate int32_t ud_LW = 64 * 1024;
3357c478bd9Sstevel@tonic-gate int32_t ud_throttles = 0;
3367c478bd9Sstevel@tonic-gate 
337da6c28aaSamw /* ARGSUSED */
3387c478bd9Sstevel@tonic-gate static int32_t
udf_write(struct vnode * vp,struct uio * uiop,int32_t ioflag,struct cred * cr,caller_context_t * ct)339da6c28aaSamw udf_write(
340da6c28aaSamw 	struct vnode *vp,
341da6c28aaSamw 	struct uio *uiop,
342da6c28aaSamw 	int32_t ioflag,
343da6c28aaSamw 	struct cred *cr,
344da6c28aaSamw 	caller_context_t *ct)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
3477c478bd9Sstevel@tonic-gate 	int32_t error = 0;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	ud_printf("udf_write\n");
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
3527c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_WRITER);
3537c478bd9Sstevel@tonic-gate #endif
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_rwlock));
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_char)) {
3587c478bd9Sstevel@tonic-gate 		/*
3597c478bd9Sstevel@tonic-gate 		 * ud_getattr ends up being called by chklock
3607c478bd9Sstevel@tonic-gate 		 */
3617c478bd9Sstevel@tonic-gate 		error = chklock(vp, FWRITE, uiop->uio_loffset,
3626f5f1c63SDonghai Qiao 		    uiop->uio_resid, uiop->uio_fmode, ct);
3637c478bd9Sstevel@tonic-gate 		if (error) {
3647c478bd9Sstevel@tonic-gate 			goto end;
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * Throttle writes.
3697c478bd9Sstevel@tonic-gate 	 */
3707c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
3717c478bd9Sstevel@tonic-gate 	if (ud_WRITES && (ip->i_writes > ud_HW)) {
3727c478bd9Sstevel@tonic-gate 		while (ip->i_writes > ud_HW) {
3737c478bd9Sstevel@tonic-gate 			ud_throttles++;
3747c478bd9Sstevel@tonic-gate 			cv_wait(&ip->i_wrcv, &ip->i_tlock);
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/*
3807c478bd9Sstevel@tonic-gate 	 * Write to the file
3817c478bd9Sstevel@tonic-gate 	 */
3827c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
3837c478bd9Sstevel@tonic-gate 	if ((ioflag & FAPPEND) != 0 && (ip->i_type == VREG)) {
3847c478bd9Sstevel@tonic-gate 		/*
3857c478bd9Sstevel@tonic-gate 		 * In append mode start at end of file.
3867c478bd9Sstevel@tonic-gate 		 */
3877c478bd9Sstevel@tonic-gate 		uiop->uio_loffset = ip->i_size;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 	error = ud_wrip(ip, uiop, ioflag, cr);
3907c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate end:
3937c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
3947c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
3957c478bd9Sstevel@tonic-gate #endif
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	return (error);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /* ARGSUSED */
4017c478bd9Sstevel@tonic-gate static int32_t
udf_ioctl(struct vnode * vp,int32_t cmd,intptr_t arg,int32_t flag,struct cred * cr,int32_t * rvalp,caller_context_t * ct)402da6c28aaSamw udf_ioctl(
403da6c28aaSamw 	struct vnode *vp,
404da6c28aaSamw 	int32_t cmd,
405da6c28aaSamw 	intptr_t arg,
406da6c28aaSamw 	int32_t flag,
407da6c28aaSamw 	struct cred *cr,
408da6c28aaSamw 	int32_t *rvalp,
409da6c28aaSamw 	caller_context_t *ct)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	return (ENOTTY);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /* ARGSUSED */
4157c478bd9Sstevel@tonic-gate static int32_t
udf_getattr(struct vnode * vp,struct vattr * vap,int32_t flags,struct cred * cr,caller_context_t * ct)416da6c28aaSamw udf_getattr(
417da6c28aaSamw 	struct vnode *vp,
418da6c28aaSamw 	struct vattr *vap,
419da6c28aaSamw 	int32_t flags,
420da6c28aaSamw 	struct cred *cr,
421da6c28aaSamw 	caller_context_t *ct)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	ud_printf("udf_getattr\n");
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (vap->va_mask == AT_SIZE) {
4287c478bd9Sstevel@tonic-gate 		/*
4297c478bd9Sstevel@tonic-gate 		 * for performance, if only the size is requested don't bother
4307c478bd9Sstevel@tonic-gate 		 * with anything else.
4317c478bd9Sstevel@tonic-gate 		 */
4327c478bd9Sstevel@tonic-gate 		vap->va_size = ip->i_size;
4337c478bd9Sstevel@tonic-gate 		return (0);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	vap->va_type = vp->v_type;
4397c478bd9Sstevel@tonic-gate 	vap->va_mode = UD2VA_PERM(ip->i_perm) | ip->i_char;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	vap->va_uid = ip->i_uid;
4427c478bd9Sstevel@tonic-gate 	vap->va_gid = ip->i_gid;
4437c478bd9Sstevel@tonic-gate 	vap->va_fsid = ip->i_dev;
4447c478bd9Sstevel@tonic-gate 	vap->va_nodeid = ip->i_icb_lbano;
4457c478bd9Sstevel@tonic-gate 	vap->va_nlink = ip->i_nlink;
4467c478bd9Sstevel@tonic-gate 	vap->va_size = ip->i_size;
4477c478bd9Sstevel@tonic-gate 	vap->va_seq = ip->i_seq;
4487c478bd9Sstevel@tonic-gate 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
4497c478bd9Sstevel@tonic-gate 		vap->va_rdev = ip->i_rdev;
4507c478bd9Sstevel@tonic-gate 	} else {
4517c478bd9Sstevel@tonic-gate 		vap->va_rdev = 0;
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
4557c478bd9Sstevel@tonic-gate 	ITIMES_NOLOCK(ip);	/* mark correct time in inode */
4567c478bd9Sstevel@tonic-gate 	vap->va_atime.tv_sec = (time_t)ip->i_atime.tv_sec;
4577c478bd9Sstevel@tonic-gate 	vap->va_atime.tv_nsec = ip->i_atime.tv_nsec;
4587c478bd9Sstevel@tonic-gate 	vap->va_mtime.tv_sec = (time_t)ip->i_mtime.tv_sec;
4597c478bd9Sstevel@tonic-gate 	vap->va_mtime.tv_nsec = ip->i_mtime.tv_nsec;
4607c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_sec = (time_t)ip->i_ctime.tv_sec;
4617c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_nsec = ip->i_ctime.tv_nsec;
4627c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	switch (ip->i_type) {
4657c478bd9Sstevel@tonic-gate 		case VBLK:
4667c478bd9Sstevel@tonic-gate 			vap->va_blksize = MAXBSIZE;
4677c478bd9Sstevel@tonic-gate 			break;
4687c478bd9Sstevel@tonic-gate 		case VCHR:
4697c478bd9Sstevel@tonic-gate 			vap->va_blksize = MAXBSIZE;
4707c478bd9Sstevel@tonic-gate 			break;
4717c478bd9Sstevel@tonic-gate 		default:
4727c478bd9Sstevel@tonic-gate 			vap->va_blksize = ip->i_udf->udf_lbsize;
4737c478bd9Sstevel@tonic-gate 			break;
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 	vap->va_nblocks = ip->i_lbr << ip->i_udf->udf_l2d_shift;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	return (0);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate static int
ud_iaccess_vmode(void * ip,int mode,struct cred * cr)4837c478bd9Sstevel@tonic-gate ud_iaccess_vmode(void *ip, int mode, struct cred *cr)
4847c478bd9Sstevel@tonic-gate {
485b39a0235SMilan Cermak 	return (ud_iaccess(ip, UD_UPERM2DPERM(mode), cr, 0));
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate /*ARGSUSED4*/
4897c478bd9Sstevel@tonic-gate static int32_t
udf_setattr(struct vnode * vp,struct vattr * vap,int32_t flags,struct cred * cr,caller_context_t * ct)4907c478bd9Sstevel@tonic-gate udf_setattr(
4917c478bd9Sstevel@tonic-gate 	struct vnode *vp,
4927c478bd9Sstevel@tonic-gate 	struct vattr *vap,
4937c478bd9Sstevel@tonic-gate 	int32_t flags,
4947c478bd9Sstevel@tonic-gate 	struct cred *cr,
4957c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	int32_t error = 0;
4987c478bd9Sstevel@tonic-gate 	uint32_t mask = vap->va_mask;
4997c478bd9Sstevel@tonic-gate 	struct ud_inode *ip;
5007c478bd9Sstevel@tonic-gate 	timestruc_t now;
5017c478bd9Sstevel@tonic-gate 	struct vattr ovap;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	ud_printf("udf_setattr\n");
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	ip = VTOI(vp);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	/*
5087c478bd9Sstevel@tonic-gate 	 * not updates allowed to 4096 files
5097c478bd9Sstevel@tonic-gate 	 */
5107c478bd9Sstevel@tonic-gate 	if (ip->i_astrat == STRAT_TYPE4096) {
5117c478bd9Sstevel@tonic-gate 		return (EINVAL);
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	/*
5157c478bd9Sstevel@tonic-gate 	 * Cannot set these attributes
5167c478bd9Sstevel@tonic-gate 	 */
5177c478bd9Sstevel@tonic-gate 	if (mask & AT_NOSET) {
5187c478bd9Sstevel@tonic-gate 		return (EINVAL);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_WRITER);
5227c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	ovap.va_uid = ip->i_uid;
5257c478bd9Sstevel@tonic-gate 	ovap.va_mode = UD2VA_PERM(ip->i_perm) | ip->i_char;
5267c478bd9Sstevel@tonic-gate 	error = secpolicy_vnode_setattr(cr, vp, vap, &ovap, flags,
5276f5f1c63SDonghai Qiao 	    ud_iaccess_vmode, ip);
5287c478bd9Sstevel@tonic-gate 	if (error)
5297c478bd9Sstevel@tonic-gate 		goto update_inode;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	mask = vap->va_mask;
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * Change file access modes.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	if (mask & AT_MODE) {
5367c478bd9Sstevel@tonic-gate 		ip->i_perm = VA2UD_PERM(vap->va_mode);
5377c478bd9Sstevel@tonic-gate 		ip->i_char = vap->va_mode & (VSUID | VSGID | VSVTX);
5387c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
5397c478bd9Sstevel@tonic-gate 		ip->i_flag |= ICHG;
5407c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 	if (mask & (AT_UID|AT_GID)) {
5437c478bd9Sstevel@tonic-gate 		if (mask & AT_UID) {
5447c478bd9Sstevel@tonic-gate 			ip->i_uid = vap->va_uid;
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 		if (mask & AT_GID) {
5477c478bd9Sstevel@tonic-gate 			ip->i_gid = vap->va_gid;
5487c478bd9Sstevel@tonic-gate 		}
5497c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
5507c478bd9Sstevel@tonic-gate 		ip->i_flag |= ICHG;
5517c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	/*
5547c478bd9Sstevel@tonic-gate 	 * Truncate file.  Must have write permission and not be a directory.
5557c478bd9Sstevel@tonic-gate 	 */
5567c478bd9Sstevel@tonic-gate 	if (mask & AT_SIZE) {
5577c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR) {
5587c478bd9Sstevel@tonic-gate 			error = EISDIR;
5597c478bd9Sstevel@tonic-gate 			goto update_inode;
5607c478bd9Sstevel@tonic-gate 		}
561b39a0235SMilan Cermak 		if (error = ud_iaccess(ip, IWRITE, cr, 0)) {
5627c478bd9Sstevel@tonic-gate 			goto update_inode;
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 		if (vap->va_size > MAXOFFSET_T) {
5657c478bd9Sstevel@tonic-gate 			error = EFBIG;
5667c478bd9Sstevel@tonic-gate 			goto update_inode;
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate 		if (error = ud_itrunc(ip, vap->va_size, 0, cr)) {
5697c478bd9Sstevel@tonic-gate 			goto update_inode;
5707c478bd9Sstevel@tonic-gate 		}
57172102e74SBryan Cantrill 
57272102e74SBryan Cantrill 		if (vap->va_size == 0)
57372102e74SBryan Cantrill 			vnevent_truncate(vp, ct);
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 	/*
5767c478bd9Sstevel@tonic-gate 	 * Change file access or modified times.
5777c478bd9Sstevel@tonic-gate 	 */
5787c478bd9Sstevel@tonic-gate 	if (mask & (AT_ATIME|AT_MTIME)) {
5797c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
5807c478bd9Sstevel@tonic-gate 		if (mask & AT_ATIME) {
5817c478bd9Sstevel@tonic-gate 			ip->i_atime.tv_sec = vap->va_atime.tv_sec;
5827c478bd9Sstevel@tonic-gate 			ip->i_atime.tv_nsec = vap->va_atime.tv_nsec;
5837c478bd9Sstevel@tonic-gate 			ip->i_flag &= ~IACC;
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 		if (mask & AT_MTIME) {
5867c478bd9Sstevel@tonic-gate 			ip->i_mtime.tv_sec = vap->va_mtime.tv_sec;
5877c478bd9Sstevel@tonic-gate 			ip->i_mtime.tv_nsec = vap->va_mtime.tv_nsec;
5887c478bd9Sstevel@tonic-gate 			gethrestime(&now);
5897c478bd9Sstevel@tonic-gate 			ip->i_ctime.tv_sec = now.tv_sec;
5907c478bd9Sstevel@tonic-gate 			ip->i_ctime.tv_nsec = now.tv_nsec;
5917c478bd9Sstevel@tonic-gate 			ip->i_flag &= ~(IUPD|ICHG);
5927c478bd9Sstevel@tonic-gate 			ip->i_flag |= IMODTIME;
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 		ip->i_flag |= IMOD;
5957c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate update_inode:
5997c478bd9Sstevel@tonic-gate 	if (curthread->t_flag & T_DONTPEND) {
6007c478bd9Sstevel@tonic-gate 		ud_iupdat(ip, 1);
6017c478bd9Sstevel@tonic-gate 	} else {
6027c478bd9Sstevel@tonic-gate 		ITIMES_NOLOCK(ip);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
6057c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	return (error);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /* ARGSUSED */
6117c478bd9Sstevel@tonic-gate static int32_t
udf_access(struct vnode * vp,int32_t mode,int32_t flags,struct cred * cr,caller_context_t * ct)612da6c28aaSamw udf_access(
613da6c28aaSamw 	struct vnode *vp,
614da6c28aaSamw 	int32_t mode,
615da6c28aaSamw 	int32_t flags,
616da6c28aaSamw 	struct cred *cr,
617da6c28aaSamw 	caller_context_t *ct)
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	ud_printf("udf_access\n");
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	if (ip->i_udf == NULL) {
6247c478bd9Sstevel@tonic-gate 		return (EIO);
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 
627b39a0235SMilan Cermak 	return (ud_iaccess(ip, UD_UPERM2DPERM(mode), cr, 1));
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate int32_t udfs_stickyhack = 1;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate /* ARGSUSED */
6337c478bd9Sstevel@tonic-gate static int32_t
udf_lookup(struct vnode * dvp,char * nm,struct vnode ** vpp,struct pathname * pnp,int32_t flags,struct vnode * rdir,struct cred * cr,caller_context_t * ct,int * direntflags,pathname_t * realpnp)634da6c28aaSamw udf_lookup(
635da6c28aaSamw 	struct vnode *dvp,
636da6c28aaSamw 	char *nm,
637da6c28aaSamw 	struct vnode **vpp,
638da6c28aaSamw 	struct pathname *pnp,
639da6c28aaSamw 	int32_t flags,
640da6c28aaSamw 	struct vnode *rdir,
641da6c28aaSamw 	struct cred *cr,
642da6c28aaSamw 	caller_context_t *ct,
643da6c28aaSamw 	int *direntflags,
644da6c28aaSamw 	pathname_t *realpnp)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	int32_t error;
6477c478bd9Sstevel@tonic-gate 	struct vnode *vp;
6487c478bd9Sstevel@tonic-gate 	struct ud_inode *ip, *xip;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	ud_printf("udf_lookup\n");
6517c478bd9Sstevel@tonic-gate 	/*
6527c478bd9Sstevel@tonic-gate 	 * Null component name is a synonym for directory being searched.
6537c478bd9Sstevel@tonic-gate 	 */
6547c478bd9Sstevel@tonic-gate 	if (*nm == '\0') {
6557c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
6567c478bd9Sstevel@tonic-gate 		*vpp = dvp;
6577c478bd9Sstevel@tonic-gate 		error = 0;
6587c478bd9Sstevel@tonic-gate 		goto out;
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	/*
6627c478bd9Sstevel@tonic-gate 	 * Fast path: Check the directory name lookup cache.
6637c478bd9Sstevel@tonic-gate 	 */
6647c478bd9Sstevel@tonic-gate 	ip = VTOI(dvp);
6657c478bd9Sstevel@tonic-gate 	if (vp = dnlc_lookup(dvp, nm)) {
6667c478bd9Sstevel@tonic-gate 		/*
6677c478bd9Sstevel@tonic-gate 		 * Check accessibility of directory.
6687c478bd9Sstevel@tonic-gate 		 */
669b39a0235SMilan Cermak 		if ((error = ud_iaccess(ip, IEXEC, cr, 1)) != 0) {
6707c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
6717c478bd9Sstevel@tonic-gate 		}
6727c478bd9Sstevel@tonic-gate 		xip = VTOI(vp);
6737c478bd9Sstevel@tonic-gate 	} else {
6747c478bd9Sstevel@tonic-gate 		error = ud_dirlook(ip, nm, &xip, cr, 1);
6757c478bd9Sstevel@tonic-gate 		ITIMES(ip);
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	if (error == 0) {
6797c478bd9Sstevel@tonic-gate 		ip = xip;
6807c478bd9Sstevel@tonic-gate 		*vpp = ITOV(ip);
6817c478bd9Sstevel@tonic-gate 		if ((ip->i_type != VDIR) &&
6826f5f1c63SDonghai Qiao 		    (ip->i_char & ISVTX) &&
6836f5f1c63SDonghai Qiao 		    ((ip->i_perm & IEXEC) == 0) &&
6846f5f1c63SDonghai Qiao 		    udfs_stickyhack) {
6857c478bd9Sstevel@tonic-gate 			mutex_enter(&(*vpp)->v_lock);
6867c478bd9Sstevel@tonic-gate 			(*vpp)->v_flag |= VISSWAP;
6877c478bd9Sstevel@tonic-gate 			mutex_exit(&(*vpp)->v_lock);
6887c478bd9Sstevel@tonic-gate 		}
6897c478bd9Sstevel@tonic-gate 		ITIMES(ip);
6907c478bd9Sstevel@tonic-gate 		/*
6917c478bd9Sstevel@tonic-gate 		 * If vnode is a device return special vnode instead.
6927c478bd9Sstevel@tonic-gate 		 */
6937c478bd9Sstevel@tonic-gate 		if (IS_DEVVP(*vpp)) {
6947c478bd9Sstevel@tonic-gate 			struct vnode *newvp;
6957c478bd9Sstevel@tonic-gate 			newvp = specvp(*vpp, (*vpp)->v_rdev,
6966f5f1c63SDonghai Qiao 			    (*vpp)->v_type, cr);
6977c478bd9Sstevel@tonic-gate 			VN_RELE(*vpp);
6987c478bd9Sstevel@tonic-gate 			if (newvp == NULL) {
6997c478bd9Sstevel@tonic-gate 				error = ENOSYS;
7007c478bd9Sstevel@tonic-gate 			} else {
7017c478bd9Sstevel@tonic-gate 				*vpp = newvp;
7027c478bd9Sstevel@tonic-gate 			}
7037c478bd9Sstevel@tonic-gate 		}
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate out:
7067c478bd9Sstevel@tonic-gate 	return (error);
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate /* ARGSUSED */
7107c478bd9Sstevel@tonic-gate static int32_t
udf_create(struct vnode * dvp,char * name,struct vattr * vap,enum vcexcl excl,int32_t mode,struct vnode ** vpp,struct cred * cr,int32_t flag,caller_context_t * ct,vsecattr_t * vsecp)711da6c28aaSamw udf_create(
712da6c28aaSamw 	struct vnode *dvp,
713da6c28aaSamw 	char *name,
714da6c28aaSamw 	struct vattr *vap,
715da6c28aaSamw 	enum vcexcl excl,
716da6c28aaSamw 	int32_t mode,
717da6c28aaSamw 	struct vnode **vpp,
718da6c28aaSamw 	struct cred *cr,
719da6c28aaSamw 	int32_t flag,
720da6c28aaSamw 	caller_context_t *ct,
721da6c28aaSamw 	vsecattr_t *vsecp)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	int32_t error;
7247c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(dvp), *xip;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	ud_printf("udf_create\n");
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr) != 0)
7297c478bd9Sstevel@tonic-gate 		vap->va_mode &= ~VSVTX;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	if (*name == '\0') {
7327c478bd9Sstevel@tonic-gate 		/*
7337c478bd9Sstevel@tonic-gate 		 * Null component name refers to the directory itself.
7347c478bd9Sstevel@tonic-gate 		 */
7357c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
7367c478bd9Sstevel@tonic-gate 		ITIMES(ip);
7377c478bd9Sstevel@tonic-gate 		error = EEXIST;
7387c478bd9Sstevel@tonic-gate 	} else {
7397c478bd9Sstevel@tonic-gate 		xip = NULL;
7407c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
7417c478bd9Sstevel@tonic-gate 		error = ud_direnter(ip, name, DE_CREATE,
7426f5f1c63SDonghai Qiao 		    (struct ud_inode *)0, (struct ud_inode *)0,
7436f5f1c63SDonghai Qiao 		    vap, &xip, cr, ct);
7447c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
7457c478bd9Sstevel@tonic-gate 		ITIMES(ip);
7467c478bd9Sstevel@tonic-gate 		ip = xip;
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
7497c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
7507c478bd9Sstevel@tonic-gate #else
7517c478bd9Sstevel@tonic-gate 	if (ip != NULL) {
7527c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate #endif
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	/*
7577c478bd9Sstevel@tonic-gate 	 * If the file already exists and this is a non-exclusive create,
7587c478bd9Sstevel@tonic-gate 	 * check permissions and allow access for non-directories.
7597c478bd9Sstevel@tonic-gate 	 * Read-only create of an existing directory is also allowed.
7607c478bd9Sstevel@tonic-gate 	 * We fail an exclusive create of anything which already exists.
7617c478bd9Sstevel@tonic-gate 	 */
7627c478bd9Sstevel@tonic-gate 	if (error == EEXIST) {
7637c478bd9Sstevel@tonic-gate 		if (excl == NONEXCL) {
7647c478bd9Sstevel@tonic-gate 			if ((ip->i_type == VDIR) && (mode & VWRITE)) {
7657c478bd9Sstevel@tonic-gate 				error = EISDIR;
7667c478bd9Sstevel@tonic-gate 			} else if (mode) {
7677c478bd9Sstevel@tonic-gate 				error = ud_iaccess(ip,
768b39a0235SMilan Cermak 				    UD_UPERM2DPERM(mode), cr, 0);
7697c478bd9Sstevel@tonic-gate 			} else {
7707c478bd9Sstevel@tonic-gate 				error = 0;
7717c478bd9Sstevel@tonic-gate 			}
7727c478bd9Sstevel@tonic-gate 		}
7737c478bd9Sstevel@tonic-gate 		if (error) {
7747c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
7757c478bd9Sstevel@tonic-gate 			VN_RELE(ITOV(ip));
7767c478bd9Sstevel@tonic-gate 			goto out;
7777c478bd9Sstevel@tonic-gate 		} else if ((ip->i_type == VREG) &&
7786f5f1c63SDonghai Qiao 		    (vap->va_mask & AT_SIZE) && vap->va_size == 0) {
7797c478bd9Sstevel@tonic-gate 			/*
7807c478bd9Sstevel@tonic-gate 			 * Truncate regular files, if requested by caller.
7817c478bd9Sstevel@tonic-gate 			 * Grab i_rwlock to make sure no one else is
7827c478bd9Sstevel@tonic-gate 			 * currently writing to the file (we promised
7837c478bd9Sstevel@tonic-gate 			 * bmap we would do this).
7847c478bd9Sstevel@tonic-gate 			 * Must get the locks in the correct order.
7857c478bd9Sstevel@tonic-gate 			 */
7867c478bd9Sstevel@tonic-gate 			if (ip->i_size == 0) {
7877c478bd9Sstevel@tonic-gate 				ip->i_flag |= ICHG | IUPD;
7887c478bd9Sstevel@tonic-gate 			} else {
7897c478bd9Sstevel@tonic-gate 				rw_exit(&ip->i_contents);
7907c478bd9Sstevel@tonic-gate 				rw_enter(&ip->i_rwlock, RW_WRITER);
7917c478bd9Sstevel@tonic-gate 				rw_enter(&ip->i_contents, RW_WRITER);
7927c478bd9Sstevel@tonic-gate 				(void) ud_itrunc(ip, 0, 0, cr);
7937c478bd9Sstevel@tonic-gate 				rw_exit(&ip->i_rwlock);
7947c478bd9Sstevel@tonic-gate 			}
795da6c28aaSamw 			vnevent_create(ITOV(ip), ct);
7967c478bd9Sstevel@tonic-gate 		}
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	if (error == 0) {
8007c478bd9Sstevel@tonic-gate 		*vpp = ITOV(ip);
8017c478bd9Sstevel@tonic-gate 		ITIMES(ip);
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
8047c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
8057c478bd9Sstevel@tonic-gate #else
8067c478bd9Sstevel@tonic-gate 	if (ip != NULL) {
8077c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate #endif
8107c478bd9Sstevel@tonic-gate 	if (error) {
8117c478bd9Sstevel@tonic-gate 		goto out;
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/*
8157c478bd9Sstevel@tonic-gate 	 * If vnode is a device return special vnode instead.
8167c478bd9Sstevel@tonic-gate 	 */
8177c478bd9Sstevel@tonic-gate 	if (!error && IS_DEVVP(*vpp)) {
8187c478bd9Sstevel@tonic-gate 		struct vnode *newvp;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 		newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
8217c478bd9Sstevel@tonic-gate 		VN_RELE(*vpp);
8227c478bd9Sstevel@tonic-gate 		if (newvp == NULL) {
8237c478bd9Sstevel@tonic-gate 			error = ENOSYS;
8247c478bd9Sstevel@tonic-gate 			goto out;
8257c478bd9Sstevel@tonic-gate 		}
8267c478bd9Sstevel@tonic-gate 		*vpp = newvp;
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate out:
8297c478bd9Sstevel@tonic-gate 	return (error);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
832da6c28aaSamw /* ARGSUSED */
8337c478bd9Sstevel@tonic-gate static int32_t
udf_remove(struct vnode * vp,char * nm,struct cred * cr,caller_context_t * ct,int flags)834da6c28aaSamw udf_remove(
835da6c28aaSamw 	struct vnode *vp,
836da6c28aaSamw 	char *nm,
837da6c28aaSamw 	struct cred *cr,
838da6c28aaSamw 	caller_context_t *ct,
839da6c28aaSamw 	int flags)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	int32_t error;
8427c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	ud_printf("udf_remove\n");
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_WRITER);
8477c478bd9Sstevel@tonic-gate 	error = ud_dirremove(ip, nm,
8486f5f1c63SDonghai Qiao 	    (struct ud_inode *)0, (struct vnode *)0, DR_REMOVE, cr, ct);
8497c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
8507c478bd9Sstevel@tonic-gate 	ITIMES(ip);
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	return (error);
8537c478bd9Sstevel@tonic-gate }
8547c478bd9Sstevel@tonic-gate 
855da6c28aaSamw /* ARGSUSED */
8567c478bd9Sstevel@tonic-gate static int32_t
udf_link(struct vnode * tdvp,struct vnode * svp,char * tnm,struct cred * cr,caller_context_t * ct,int flags)857da6c28aaSamw udf_link(
858da6c28aaSamw 	struct vnode *tdvp,
859da6c28aaSamw 	struct vnode *svp,
860da6c28aaSamw 	char *tnm,
861da6c28aaSamw 	struct cred *cr,
862da6c28aaSamw 	caller_context_t *ct,
863da6c28aaSamw 	int flags)
8647c478bd9Sstevel@tonic-gate {
8657c478bd9Sstevel@tonic-gate 	int32_t error;
8667c478bd9Sstevel@tonic-gate 	struct vnode *realvp;
8677c478bd9Sstevel@tonic-gate 	struct ud_inode *sip;
8687c478bd9Sstevel@tonic-gate 	struct ud_inode *tdp;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	ud_printf("udf_link\n");
871da6c28aaSamw 	if (VOP_REALVP(svp, &realvp, ct) == 0) {
8727c478bd9Sstevel@tonic-gate 		svp = realvp;
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	/*
8767c478bd9Sstevel@tonic-gate 	 * Do not allow links to directories
8777c478bd9Sstevel@tonic-gate 	 */
8787c478bd9Sstevel@tonic-gate 	if (svp->v_type == VDIR) {
8797c478bd9Sstevel@tonic-gate 		return (EPERM);
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	sip = VTOI(svp);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	if (sip->i_uid != crgetuid(cr) && secpolicy_basic_link(cr) != 0)
8857c478bd9Sstevel@tonic-gate 		return (EPERM);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	tdp = VTOI(tdvp);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	rw_enter(&tdp->i_rwlock, RW_WRITER);
8907c478bd9Sstevel@tonic-gate 	error = ud_direnter(tdp, tnm, DE_LINK, (struct ud_inode *)0,
8916f5f1c63SDonghai Qiao 	    sip, (struct vattr *)0, (struct ud_inode **)0, cr, ct);
8927c478bd9Sstevel@tonic-gate 	rw_exit(&tdp->i_rwlock);
8937c478bd9Sstevel@tonic-gate 	ITIMES(sip);
8947c478bd9Sstevel@tonic-gate 	ITIMES(tdp);
8957c478bd9Sstevel@tonic-gate 
896df2381bfSpraks 	if (error == 0) {
897da6c28aaSamw 		vnevent_link(svp, ct);
898df2381bfSpraks 	}
899df2381bfSpraks 
9007c478bd9Sstevel@tonic-gate 	return (error);
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate /* ARGSUSED */
9047c478bd9Sstevel@tonic-gate static int32_t
udf_rename(struct vnode * sdvp,char * snm,struct vnode * tdvp,char * tnm,struct cred * cr,caller_context_t * ct,int flags)905da6c28aaSamw udf_rename(
906da6c28aaSamw 	struct vnode *sdvp,
907da6c28aaSamw 	char *snm,
908da6c28aaSamw 	struct vnode *tdvp,
909da6c28aaSamw 	char *tnm,
910da6c28aaSamw 	struct cred *cr,
911da6c28aaSamw 	caller_context_t *ct,
912da6c28aaSamw 	int flags)
9137c478bd9Sstevel@tonic-gate {
9147c478bd9Sstevel@tonic-gate 	int32_t error = 0;
9157c478bd9Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
9167c478bd9Sstevel@tonic-gate 	struct ud_inode *sip;		/* source inode */
91754207fd2SJerry Jelinek 	struct ud_inode *tip;		/* target inode */
9187c478bd9Sstevel@tonic-gate 	struct ud_inode *sdp, *tdp;	/* source and target parent inode */
9197c478bd9Sstevel@tonic-gate 	struct vnode *realvp;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	ud_printf("udf_rename\n");
9227c478bd9Sstevel@tonic-gate 
923da6c28aaSamw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0) {
9247c478bd9Sstevel@tonic-gate 		tdvp = realvp;
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	sdp = VTOI(sdvp);
9287c478bd9Sstevel@tonic-gate 	tdp = VTOI(tdvp);
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	udf_vfsp = sdp->i_udf;
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	mutex_enter(&udf_vfsp->udf_rename_lck);
9337c478bd9Sstevel@tonic-gate 	/*
9347c478bd9Sstevel@tonic-gate 	 * Look up inode of file we're supposed to rename.
9357c478bd9Sstevel@tonic-gate 	 */
9367c478bd9Sstevel@tonic-gate 	if (error = ud_dirlook(sdp, snm, &sip, cr, 0)) {
9377c478bd9Sstevel@tonic-gate 		mutex_exit(&udf_vfsp->udf_rename_lck);
9387c478bd9Sstevel@tonic-gate 		return (error);
9397c478bd9Sstevel@tonic-gate 	}
9407c478bd9Sstevel@tonic-gate 	/*
9417c478bd9Sstevel@tonic-gate 	 * be sure this is not a directory with another file system mounted
9427c478bd9Sstevel@tonic-gate 	 * over it.  If it is just give up the locks, and return with
9437c478bd9Sstevel@tonic-gate 	 * EBUSY
9447c478bd9Sstevel@tonic-gate 	 */
9457c478bd9Sstevel@tonic-gate 	if (vn_mountedvfs(ITOV(sip)) != NULL) {
9467c478bd9Sstevel@tonic-gate 		error = EBUSY;
9477c478bd9Sstevel@tonic-gate 		goto errout;
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 	/*
9507c478bd9Sstevel@tonic-gate 	 * Make sure we can delete the source entry.  This requires
9517c478bd9Sstevel@tonic-gate 	 * write permission on the containing directory.  If that
9527c478bd9Sstevel@tonic-gate 	 * directory is "sticky" it further requires (except for
9537c478bd9Sstevel@tonic-gate 	 * privileged users) that the user own the directory or the
9547c478bd9Sstevel@tonic-gate 	 * source entry, or else have permission to write the source
9557c478bd9Sstevel@tonic-gate 	 * entry.
9567c478bd9Sstevel@tonic-gate 	 */
9577c478bd9Sstevel@tonic-gate 	rw_enter(&sdp->i_contents, RW_READER);
9587c478bd9Sstevel@tonic-gate 	rw_enter(&sip->i_contents, RW_READER);
959b39a0235SMilan Cermak 	if ((error = ud_iaccess(sdp, IWRITE, cr, 0)) != 0 ||
9607c478bd9Sstevel@tonic-gate 	    (error = ud_sticky_remove_access(sdp, sip, cr)) != 0) {
9617c478bd9Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
9627c478bd9Sstevel@tonic-gate 		rw_exit(&sdp->i_contents);
9637c478bd9Sstevel@tonic-gate 		ITIMES(sip);
9647c478bd9Sstevel@tonic-gate 		goto errout;
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	/*
9687c478bd9Sstevel@tonic-gate 	 * Check for renaming '.' or '..' or alias of '.'
9697c478bd9Sstevel@tonic-gate 	 */
9707c478bd9Sstevel@tonic-gate 	if ((strcmp(snm, ".") == 0) ||
9716f5f1c63SDonghai Qiao 	    (strcmp(snm, "..") == 0) ||
9726f5f1c63SDonghai Qiao 	    (sdp == sip)) {
9737c478bd9Sstevel@tonic-gate 		error = EINVAL;
9747c478bd9Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
9757c478bd9Sstevel@tonic-gate 		rw_exit(&sdp->i_contents);
9767c478bd9Sstevel@tonic-gate 		goto errout;
9777c478bd9Sstevel@tonic-gate 	}
97854207fd2SJerry Jelinek 
9797c478bd9Sstevel@tonic-gate 	rw_exit(&sip->i_contents);
9807c478bd9Sstevel@tonic-gate 	rw_exit(&sdp->i_contents);
9817c478bd9Sstevel@tonic-gate 
98254207fd2SJerry Jelinek 	if (ud_dirlook(tdp, tnm, &tip, cr, 0) == 0) {
98354207fd2SJerry Jelinek 		vnevent_pre_rename_dest(ITOV(tip), tdvp, tnm, ct);
98454207fd2SJerry Jelinek 		VN_RELE(ITOV(tip));
98554207fd2SJerry Jelinek 	}
98654207fd2SJerry Jelinek 
98754207fd2SJerry Jelinek 	/* Notify the target dir. if not the same as the source dir. */
98854207fd2SJerry Jelinek 	if (sdvp != tdvp)
98954207fd2SJerry Jelinek 		vnevent_pre_rename_dest_dir(tdvp, ITOV(sip), tnm, ct);
99054207fd2SJerry Jelinek 
99154207fd2SJerry Jelinek 	vnevent_pre_rename_src(ITOV(sip), sdvp, snm, ct);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	/*
9947c478bd9Sstevel@tonic-gate 	 * Link source to the target.
9957c478bd9Sstevel@tonic-gate 	 */
9967c478bd9Sstevel@tonic-gate 	rw_enter(&tdp->i_rwlock, RW_WRITER);
9977c478bd9Sstevel@tonic-gate 	if (error = ud_direnter(tdp, tnm, DE_RENAME, sdp, sip,
998da6c28aaSamw 	    (struct vattr *)0, (struct ud_inode **)0, cr, ct)) {
9997c478bd9Sstevel@tonic-gate 		/*
10007c478bd9Sstevel@tonic-gate 		 * ESAME isn't really an error; it indicates that the
10017c478bd9Sstevel@tonic-gate 		 * operation should not be done because the source and target
10027c478bd9Sstevel@tonic-gate 		 * are the same file, but that no error should be reported.
10037c478bd9Sstevel@tonic-gate 		 */
10047c478bd9Sstevel@tonic-gate 		if (error == ESAME) {
10057c478bd9Sstevel@tonic-gate 			error = 0;
10067c478bd9Sstevel@tonic-gate 		}
10077c478bd9Sstevel@tonic-gate 		rw_exit(&tdp->i_rwlock);
10087c478bd9Sstevel@tonic-gate 		goto errout;
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate 	rw_exit(&tdp->i_rwlock);
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	rw_enter(&sdp->i_rwlock, RW_WRITER);
10137c478bd9Sstevel@tonic-gate 	/*
10147c478bd9Sstevel@tonic-gate 	 * Unlink the source.
10157c478bd9Sstevel@tonic-gate 	 * Remove the source entry.  ud_dirremove() checks that the entry
10167c478bd9Sstevel@tonic-gate 	 * still reflects sip, and returns an error if it doesn't.
10177c478bd9Sstevel@tonic-gate 	 * If the entry has changed just forget about it.  Release
10187c478bd9Sstevel@tonic-gate 	 * the source inode.
10197c478bd9Sstevel@tonic-gate 	 */
10207c478bd9Sstevel@tonic-gate 	if ((error = ud_dirremove(sdp, snm, sip, (struct vnode *)0,
1021da6c28aaSamw 	    DR_RENAME, cr, ct)) == ENOENT) {
10227c478bd9Sstevel@tonic-gate 		error = 0;
10237c478bd9Sstevel@tonic-gate 	}
10247c478bd9Sstevel@tonic-gate 	rw_exit(&sdp->i_rwlock);
102554207fd2SJerry Jelinek 
102654207fd2SJerry Jelinek 	if (error == 0) {
102754207fd2SJerry Jelinek 		vnevent_rename_src(ITOV(sip), sdvp, snm, ct);
102854207fd2SJerry Jelinek 		/*
102954207fd2SJerry Jelinek 		 * vnevent_rename_dest and vnevent_rename_dest_dir are called
103054207fd2SJerry Jelinek 		 * in ud_direnter().
103154207fd2SJerry Jelinek 		 */
103254207fd2SJerry Jelinek 	}
103354207fd2SJerry Jelinek 
10347c478bd9Sstevel@tonic-gate errout:
10357c478bd9Sstevel@tonic-gate 	ITIMES(sdp);
10367c478bd9Sstevel@tonic-gate 	ITIMES(tdp);
10377c478bd9Sstevel@tonic-gate 	VN_RELE(ITOV(sip));
10387c478bd9Sstevel@tonic-gate 	mutex_exit(&udf_vfsp->udf_rename_lck);
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	return (error);
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate 
1043da6c28aaSamw /* ARGSUSED */
10447c478bd9Sstevel@tonic-gate static int32_t
udf_mkdir(struct vnode * dvp,char * dirname,struct vattr * vap,struct vnode ** vpp,struct cred * cr,caller_context_t * ct,int flags,vsecattr_t * vsecp)1045da6c28aaSamw udf_mkdir(
1046da6c28aaSamw 	struct vnode *dvp,
1047da6c28aaSamw 	char *dirname,
1048da6c28aaSamw 	struct vattr *vap,
1049da6c28aaSamw 	struct vnode **vpp,
1050da6c28aaSamw 	struct cred *cr,
1051da6c28aaSamw 	caller_context_t *ct,
1052da6c28aaSamw 	int flags,
1053da6c28aaSamw 	vsecattr_t *vsecp)
10547c478bd9Sstevel@tonic-gate {
10557c478bd9Sstevel@tonic-gate 	int32_t error;
10567c478bd9Sstevel@tonic-gate 	struct ud_inode *ip;
10577c478bd9Sstevel@tonic-gate 	struct ud_inode *xip;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	ud_printf("udf_mkdir\n");
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	ip = VTOI(dvp);
10647c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_WRITER);
10657c478bd9Sstevel@tonic-gate 	error = ud_direnter(ip, dirname, DE_MKDIR,
10666f5f1c63SDonghai Qiao 	    (struct ud_inode *)0, (struct ud_inode *)0, vap, &xip, cr, ct);
10677c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
10687c478bd9Sstevel@tonic-gate 	ITIMES(ip);
10697c478bd9Sstevel@tonic-gate 	if (error == 0) {
10707c478bd9Sstevel@tonic-gate 		ip = xip;
10717c478bd9Sstevel@tonic-gate 		*vpp = ITOV(ip);
10727c478bd9Sstevel@tonic-gate 		ITIMES(ip);
10737c478bd9Sstevel@tonic-gate 	} else if (error == EEXIST) {
10747c478bd9Sstevel@tonic-gate 		ITIMES(xip);
10757c478bd9Sstevel@tonic-gate 		VN_RELE(ITOV(xip));
10767c478bd9Sstevel@tonic-gate 	}
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	return (error);
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate 
1081da6c28aaSamw /* ARGSUSED */
10827c478bd9Sstevel@tonic-gate static int32_t
udf_rmdir(struct vnode * vp,char * nm,struct vnode * cdir,struct cred * cr,caller_context_t * ct,int flags)1083da6c28aaSamw udf_rmdir(
1084da6c28aaSamw 	struct vnode *vp,
1085da6c28aaSamw 	char *nm,
1086da6c28aaSamw 	struct vnode *cdir,
1087da6c28aaSamw 	struct cred *cr,
1088da6c28aaSamw 	caller_context_t *ct,
1089da6c28aaSamw 	int flags)
10907c478bd9Sstevel@tonic-gate {
10917c478bd9Sstevel@tonic-gate 	int32_t error;
10927c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	ud_printf("udf_rmdir\n");
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_WRITER);
1097da6c28aaSamw 	error = ud_dirremove(ip, nm, (struct ud_inode *)0, cdir, DR_RMDIR,
10986f5f1c63SDonghai Qiao 	    cr, ct);
10997c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
11007c478bd9Sstevel@tonic-gate 	ITIMES(ip);
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	return (error);
11037c478bd9Sstevel@tonic-gate }
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate /* ARGSUSED */
11067c478bd9Sstevel@tonic-gate static int32_t
udf_readdir(struct vnode * vp,struct uio * uiop,struct cred * cr,int32_t * eofp,caller_context_t * ct,int flags)1107da6c28aaSamw udf_readdir(
1108da6c28aaSamw 	struct vnode *vp,
1109da6c28aaSamw 	struct uio *uiop,
1110da6c28aaSamw 	struct cred *cr,
1111da6c28aaSamw 	int32_t *eofp,
1112da6c28aaSamw 	caller_context_t *ct,
1113da6c28aaSamw 	int flags)
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 	struct ud_inode *ip;
11167c478bd9Sstevel@tonic-gate 	struct dirent64 *nd;
11177c478bd9Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
11187c478bd9Sstevel@tonic-gate 	int32_t error = 0, len, outcount = 0;
11197c478bd9Sstevel@tonic-gate 	uint32_t dirsiz, offset;
11207c478bd9Sstevel@tonic-gate 	uint32_t bufsize, ndlen, dummy;
11217c478bd9Sstevel@tonic-gate 	caddr_t outbuf;
11227c478bd9Sstevel@tonic-gate 	caddr_t outb, end_outb;
11237c478bd9Sstevel@tonic-gate 	struct iovec *iovp;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	uint8_t *dname;
11267c478bd9Sstevel@tonic-gate 	int32_t length;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	uint8_t *buf = NULL;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	struct fbuf *fbp = NULL;
11317c478bd9Sstevel@tonic-gate 	struct file_id *fid;
11327c478bd9Sstevel@tonic-gate 	uint8_t *name;
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	ud_printf("udf_readdir\n");
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	ip = VTOI(vp);
11387c478bd9Sstevel@tonic-gate 	udf_vfsp = ip->i_udf;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	dirsiz = ip->i_size;
11417c478bd9Sstevel@tonic-gate 	if ((uiop->uio_offset >= dirsiz) ||
11426f5f1c63SDonghai Qiao 	    (ip->i_nlink <= 0)) {
11437c478bd9Sstevel@tonic-gate 		if (eofp) {
11447c478bd9Sstevel@tonic-gate 			*eofp = 1;
11457c478bd9Sstevel@tonic-gate 		}
11467c478bd9Sstevel@tonic-gate 		return (0);
11477c478bd9Sstevel@tonic-gate 	}
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	offset = uiop->uio_offset;
11507c478bd9Sstevel@tonic-gate 	iovp = uiop->uio_iov;
11517c478bd9Sstevel@tonic-gate 	bufsize = iovp->iov_len;
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	outb = outbuf = (char *)kmem_alloc((uint32_t)bufsize, KM_SLEEP);
11547c478bd9Sstevel@tonic-gate 	end_outb = outb + bufsize;
11557c478bd9Sstevel@tonic-gate 	nd = (struct dirent64 *)outbuf;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	dname = (uint8_t *)kmem_zalloc(1024, KM_SLEEP);
11587c478bd9Sstevel@tonic-gate 	buf = (uint8_t *)kmem_zalloc(udf_vfsp->udf_lbsize, KM_SLEEP);
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	if (offset == 0) {
11617c478bd9Sstevel@tonic-gate 		len = DIRENT64_RECLEN(1);
11627c478bd9Sstevel@tonic-gate 		if (((caddr_t)nd + len) >= end_outb) {
11637c478bd9Sstevel@tonic-gate 			error = EINVAL;
11647c478bd9Sstevel@tonic-gate 			goto end;
11657c478bd9Sstevel@tonic-gate 		}
11667c478bd9Sstevel@tonic-gate 		nd->d_ino = ip->i_icb_lbano;
11677c478bd9Sstevel@tonic-gate 		nd->d_reclen = (uint16_t)len;
11687c478bd9Sstevel@tonic-gate 		nd->d_off = 0x10;
11697c478bd9Sstevel@tonic-gate 		nd->d_name[0] = '.';
11707c478bd9Sstevel@tonic-gate 		bzero(&nd->d_name[1], DIRENT64_NAMELEN(len) - 1);
11717c478bd9Sstevel@tonic-gate 		nd = (struct dirent64 *)((char *)nd + nd->d_reclen);
11727c478bd9Sstevel@tonic-gate 		outcount++;
11737c478bd9Sstevel@tonic-gate 	} else if (offset == 0x10) {
11747c478bd9Sstevel@tonic-gate 		offset = 0;
11757c478bd9Sstevel@tonic-gate 	}
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	while (offset < dirsiz) {
11787c478bd9Sstevel@tonic-gate 		error = ud_get_next_fid(ip, &fbp,
11796f5f1c63SDonghai Qiao 		    offset, &fid, &name, buf);
11807c478bd9Sstevel@tonic-gate 		if (error != 0) {
11817c478bd9Sstevel@tonic-gate 			break;
11827c478bd9Sstevel@tonic-gate 		}
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 		if ((fid->fid_flags & FID_DELETED) == 0) {
11857c478bd9Sstevel@tonic-gate 			if (fid->fid_flags & FID_PARENT) {
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 				len = DIRENT64_RECLEN(2);
11887c478bd9Sstevel@tonic-gate 				if (((caddr_t)nd + len) >= end_outb) {
11897c478bd9Sstevel@tonic-gate 					error = EINVAL;
11907c478bd9Sstevel@tonic-gate 					break;
11917c478bd9Sstevel@tonic-gate 				}
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 				nd->d_ino = ip->i_icb_lbano;
11947c478bd9Sstevel@tonic-gate 				nd->d_reclen = (uint16_t)len;
11957c478bd9Sstevel@tonic-gate 				nd->d_off = offset + FID_LEN(fid);
11967c478bd9Sstevel@tonic-gate 				nd->d_name[0] = '.';
11977c478bd9Sstevel@tonic-gate 				nd->d_name[1] = '.';
11987c478bd9Sstevel@tonic-gate 				bzero(&nd->d_name[2],
11997c478bd9Sstevel@tonic-gate 				    DIRENT64_NAMELEN(len) - 2);
12007c478bd9Sstevel@tonic-gate 				nd = (struct dirent64 *)
12016f5f1c63SDonghai Qiao 				    ((char *)nd + nd->d_reclen);
12027c478bd9Sstevel@tonic-gate 			} else {
12037c478bd9Sstevel@tonic-gate 				if ((error = ud_uncompress(fid->fid_idlen,
12046f5f1c63SDonghai Qiao 				    &length, name, dname)) != 0) {
12057c478bd9Sstevel@tonic-gate 					break;
12067c478bd9Sstevel@tonic-gate 				}
12077c478bd9Sstevel@tonic-gate 				if (length == 0) {
12087c478bd9Sstevel@tonic-gate 					offset += FID_LEN(fid);
12097c478bd9Sstevel@tonic-gate 					continue;
12107c478bd9Sstevel@tonic-gate 				}
12117c478bd9Sstevel@tonic-gate 				len = DIRENT64_RECLEN(length);
12127c478bd9Sstevel@tonic-gate 				if (((caddr_t)nd + len) >= end_outb) {
12137c478bd9Sstevel@tonic-gate 					if (!outcount) {
12147c478bd9Sstevel@tonic-gate 						error = EINVAL;
12157c478bd9Sstevel@tonic-gate 					}
12167c478bd9Sstevel@tonic-gate 					break;
12177c478bd9Sstevel@tonic-gate 				}
12187c478bd9Sstevel@tonic-gate 				(void) strncpy(nd->d_name,
12197c478bd9Sstevel@tonic-gate 				    (caddr_t)dname, length);
12207c478bd9Sstevel@tonic-gate 				bzero(&nd->d_name[length],
12217c478bd9Sstevel@tonic-gate 				    DIRENT64_NAMELEN(len) - length);
12227c478bd9Sstevel@tonic-gate 				nd->d_ino = ud_xlate_to_daddr(udf_vfsp,
12236f5f1c63SDonghai Qiao 				    SWAP_16(fid->fid_icb.lad_ext_prn),
12246f5f1c63SDonghai Qiao 				    SWAP_32(fid->fid_icb.lad_ext_loc), 1,
12256f5f1c63SDonghai Qiao 				    &dummy);
12267c478bd9Sstevel@tonic-gate 				nd->d_reclen = (uint16_t)len;
12277c478bd9Sstevel@tonic-gate 				nd->d_off = offset + FID_LEN(fid);
12287c478bd9Sstevel@tonic-gate 				nd = (struct dirent64 *)
12296f5f1c63SDonghai Qiao 				    ((char *)nd + nd->d_reclen);
12307c478bd9Sstevel@tonic-gate 			}
12317c478bd9Sstevel@tonic-gate 			outcount++;
12327c478bd9Sstevel@tonic-gate 		}
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 		offset += FID_LEN(fid);
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate end:
12387c478bd9Sstevel@tonic-gate 	if (fbp != NULL) {
12397c478bd9Sstevel@tonic-gate 		fbrelse(fbp, S_OTHER);
12407c478bd9Sstevel@tonic-gate 	}
12417c478bd9Sstevel@tonic-gate 	ndlen = ((char *)nd - outbuf);
12427c478bd9Sstevel@tonic-gate 	/*
12437c478bd9Sstevel@tonic-gate 	 * In case of error do not call uiomove.
12447c478bd9Sstevel@tonic-gate 	 * Return the error to the caller.
12457c478bd9Sstevel@tonic-gate 	 */
12467c478bd9Sstevel@tonic-gate 	if ((error == 0) && (ndlen != 0)) {
12477c478bd9Sstevel@tonic-gate 		error = uiomove(outbuf, (long)ndlen, UIO_READ, uiop);
12487c478bd9Sstevel@tonic-gate 		uiop->uio_offset = offset;
12497c478bd9Sstevel@tonic-gate 	}
12507c478bd9Sstevel@tonic-gate 	kmem_free((caddr_t)buf, udf_vfsp->udf_lbsize);
12517c478bd9Sstevel@tonic-gate 	kmem_free((caddr_t)dname, 1024);
12527c478bd9Sstevel@tonic-gate 	kmem_free(outbuf, (uint32_t)bufsize);
12537c478bd9Sstevel@tonic-gate 	if (eofp && error == 0) {
12547c478bd9Sstevel@tonic-gate 		*eofp = (uiop->uio_offset >= dirsiz);
12557c478bd9Sstevel@tonic-gate 	}
12567c478bd9Sstevel@tonic-gate 	return (error);
12577c478bd9Sstevel@tonic-gate }
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate /* ARGSUSED */
12607c478bd9Sstevel@tonic-gate static int32_t
udf_symlink(struct vnode * dvp,char * linkname,struct vattr * vap,char * target,struct cred * cr,caller_context_t * ct,int flags)1261da6c28aaSamw udf_symlink(
1262da6c28aaSamw 	struct vnode *dvp,
1263da6c28aaSamw 	char *linkname,
1264da6c28aaSamw 	struct vattr *vap,
1265da6c28aaSamw 	char *target,
1266da6c28aaSamw 	struct cred *cr,
1267da6c28aaSamw 	caller_context_t *ct,
1268da6c28aaSamw 	int flags)
12697c478bd9Sstevel@tonic-gate {
12707c478bd9Sstevel@tonic-gate 	int32_t error = 0, outlen;
12717c478bd9Sstevel@tonic-gate 	uint32_t ioflag = 0;
12727c478bd9Sstevel@tonic-gate 	struct ud_inode *ip, *dip = VTOI(dvp);
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	struct path_comp *pc;
12757c478bd9Sstevel@tonic-gate 	int8_t *dname = NULL, *uname = NULL, *sp;
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	ud_printf("udf_symlink\n");
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	ip = (struct ud_inode *)0;
12807c478bd9Sstevel@tonic-gate 	vap->va_type = VLNK;
12817c478bd9Sstevel@tonic-gate 	vap->va_rdev = 0;
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	rw_enter(&dip->i_rwlock, RW_WRITER);
12847c478bd9Sstevel@tonic-gate 	error = ud_direnter(dip, linkname, DE_CREATE,
12856f5f1c63SDonghai Qiao 	    (struct ud_inode *)0, (struct ud_inode *)0, vap, &ip, cr, ct);
12867c478bd9Sstevel@tonic-gate 	rw_exit(&dip->i_rwlock);
12877c478bd9Sstevel@tonic-gate 	if (error == 0) {
12887c478bd9Sstevel@tonic-gate 		dname = kmem_zalloc(1024, KM_SLEEP);
12897c478bd9Sstevel@tonic-gate 		uname = kmem_zalloc(PAGESIZE, KM_SLEEP);
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 		pc = (struct path_comp *)uname;
12927c478bd9Sstevel@tonic-gate 		/*
12937c478bd9Sstevel@tonic-gate 		 * If the first character in target is "/"
12947c478bd9Sstevel@tonic-gate 		 * then skip it and create entry for it
12957c478bd9Sstevel@tonic-gate 		 */
12967c478bd9Sstevel@tonic-gate 		if (*target == '/') {
12977c478bd9Sstevel@tonic-gate 			pc->pc_type = 2;
12987c478bd9Sstevel@tonic-gate 			pc->pc_len = 0;
12997c478bd9Sstevel@tonic-gate 			pc = (struct path_comp *)(((char *)pc) + 4);
13007c478bd9Sstevel@tonic-gate 			while (*target == '/') {
13017c478bd9Sstevel@tonic-gate 				target++;
13027c478bd9Sstevel@tonic-gate 			}
13037c478bd9Sstevel@tonic-gate 		}
13047c478bd9Sstevel@tonic-gate 
1305*06042525SToomas Soome 		while (*target != '\0') {
13067c478bd9Sstevel@tonic-gate 			sp = target;
13077c478bd9Sstevel@tonic-gate 			while ((*target != '/') && (*target != '\0')) {
13087c478bd9Sstevel@tonic-gate 				target ++;
13097c478bd9Sstevel@tonic-gate 			}
13107c478bd9Sstevel@tonic-gate 			/*
13117c478bd9Sstevel@tonic-gate 			 * We got the next component of the
13127c478bd9Sstevel@tonic-gate 			 * path name. Create path_comp of
13137c478bd9Sstevel@tonic-gate 			 * appropriate type
13147c478bd9Sstevel@tonic-gate 			 */
13157c478bd9Sstevel@tonic-gate 			if (((target - sp) == 1) && (*sp == '.')) {
13167c478bd9Sstevel@tonic-gate 				/*
13177c478bd9Sstevel@tonic-gate 				 * Dot entry.
13187c478bd9Sstevel@tonic-gate 				 */
13197c478bd9Sstevel@tonic-gate 				pc->pc_type = 4;
13207c478bd9Sstevel@tonic-gate 				pc = (struct path_comp *)(((char *)pc) + 4);
13217c478bd9Sstevel@tonic-gate 			} else if (((target - sp) == 2) &&
13226f5f1c63SDonghai Qiao 			    (*sp == '.') && ((*(sp + 1)) == '.')) {
13237c478bd9Sstevel@tonic-gate 				/*
13247c478bd9Sstevel@tonic-gate 				 * DotDot entry.
13257c478bd9Sstevel@tonic-gate 				 */
13267c478bd9Sstevel@tonic-gate 				pc->pc_type = 3;
13277c478bd9Sstevel@tonic-gate 				pc = (struct path_comp *)(((char *)pc) + 4);
13287c478bd9Sstevel@tonic-gate 			} else {
13297c478bd9Sstevel@tonic-gate 				/*
13307c478bd9Sstevel@tonic-gate 				 * convert the user given name
13317c478bd9Sstevel@tonic-gate 				 * into appropriate form to be put
13327c478bd9Sstevel@tonic-gate 				 * on the media
13337c478bd9Sstevel@tonic-gate 				 */
13347c478bd9Sstevel@tonic-gate 				outlen = 1024;	/* set to size of dname */
13357c478bd9Sstevel@tonic-gate 				if (error = ud_compress(target - sp, &outlen,
13366f5f1c63SDonghai Qiao 				    (uint8_t *)sp, (uint8_t *)dname)) {
13377c478bd9Sstevel@tonic-gate 					break;
13387c478bd9Sstevel@tonic-gate 				}
13397c478bd9Sstevel@tonic-gate 				pc->pc_type = 5;
13407c478bd9Sstevel@tonic-gate 				/* LINTED */
13417c478bd9Sstevel@tonic-gate 				pc->pc_len = outlen;
13427c478bd9Sstevel@tonic-gate 				dname[outlen] = '\0';
13437c478bd9Sstevel@tonic-gate 				(void) strcpy((char *)pc->pc_id, dname);
13447c478bd9Sstevel@tonic-gate 				pc = (struct path_comp *)
13456f5f1c63SDonghai Qiao 				    (((char *)pc) + 4 + outlen);
13467c478bd9Sstevel@tonic-gate 			}
13477c478bd9Sstevel@tonic-gate 			while (*target == '/') {
13487c478bd9Sstevel@tonic-gate 				target++;
13497c478bd9Sstevel@tonic-gate 			}
1350*06042525SToomas Soome 			if (*target == '\0') {
13517c478bd9Sstevel@tonic-gate 				break;
13527c478bd9Sstevel@tonic-gate 			}
13537c478bd9Sstevel@tonic-gate 		}
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
13567c478bd9Sstevel@tonic-gate 		if (error == 0) {
13577c478bd9Sstevel@tonic-gate 			ioflag = FWRITE;
13587c478bd9Sstevel@tonic-gate 			if (curthread->t_flag & T_DONTPEND) {
13597c478bd9Sstevel@tonic-gate 				ioflag |= FDSYNC;
13607c478bd9Sstevel@tonic-gate 			}
13617c478bd9Sstevel@tonic-gate 			error = ud_rdwri(UIO_WRITE, ioflag, ip,
13626f5f1c63SDonghai Qiao 			    uname, ((int8_t *)pc) - uname,
13636f5f1c63SDonghai Qiao 			    (offset_t)0, UIO_SYSSPACE, (int32_t *)0, cr);
13647c478bd9Sstevel@tonic-gate 		}
13657c478bd9Sstevel@tonic-gate 		if (error) {
13667c478bd9Sstevel@tonic-gate 			ud_idrop(ip);
13677c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
13687c478bd9Sstevel@tonic-gate 			rw_enter(&dip->i_rwlock, RW_WRITER);
13697c478bd9Sstevel@tonic-gate 			(void) ud_dirremove(dip, linkname, (struct ud_inode *)0,
13706f5f1c63SDonghai Qiao 			    (struct vnode *)0, DR_REMOVE, cr, ct);
13717c478bd9Sstevel@tonic-gate 			rw_exit(&dip->i_rwlock);
13727c478bd9Sstevel@tonic-gate 			goto update_inode;
13737c478bd9Sstevel@tonic-gate 		}
13747c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
13757c478bd9Sstevel@tonic-gate 	}
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	if ((error == 0) || (error == EEXIST)) {
13787c478bd9Sstevel@tonic-gate 		VN_RELE(ITOV(ip));
13797c478bd9Sstevel@tonic-gate 	}
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate update_inode:
13827c478bd9Sstevel@tonic-gate 	ITIMES(VTOI(dvp));
13837c478bd9Sstevel@tonic-gate 	if (uname != NULL) {
13847c478bd9Sstevel@tonic-gate 		kmem_free(uname, PAGESIZE);
13857c478bd9Sstevel@tonic-gate 	}
13867c478bd9Sstevel@tonic-gate 	if (dname != NULL) {
13877c478bd9Sstevel@tonic-gate 		kmem_free(dname, 1024);
13887c478bd9Sstevel@tonic-gate 	}
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	return (error);
13917c478bd9Sstevel@tonic-gate }
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate /* ARGSUSED */
13947c478bd9Sstevel@tonic-gate static int32_t
udf_readlink(struct vnode * vp,struct uio * uiop,struct cred * cr,caller_context_t * ct)1395da6c28aaSamw udf_readlink(
1396da6c28aaSamw 	struct vnode *vp,
1397da6c28aaSamw 	struct uio *uiop,
1398da6c28aaSamw 	struct cred *cr,
1399da6c28aaSamw 	caller_context_t *ct)
14007c478bd9Sstevel@tonic-gate {
14017c478bd9Sstevel@tonic-gate 	int32_t error = 0, off, id_len, size, len;
14027c478bd9Sstevel@tonic-gate 	int8_t *dname = NULL, *uname = NULL;
14037c478bd9Sstevel@tonic-gate 	struct ud_inode *ip;
14047c478bd9Sstevel@tonic-gate 	struct fbuf *fbp = NULL;
14057c478bd9Sstevel@tonic-gate 	struct path_comp *pc;
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	ud_printf("udf_readlink\n");
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	if (vp->v_type != VLNK) {
14107c478bd9Sstevel@tonic-gate 		return (EINVAL);
14117c478bd9Sstevel@tonic-gate 	}
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	ip = VTOI(vp);
14147c478bd9Sstevel@tonic-gate 	size = ip->i_size;
14157c478bd9Sstevel@tonic-gate 	if (size > PAGESIZE) {
14167c478bd9Sstevel@tonic-gate 		return (EIO);
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	if (size == 0) {
14207c478bd9Sstevel@tonic-gate 		return (0);
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	dname = kmem_zalloc(1024, KM_SLEEP);
14247c478bd9Sstevel@tonic-gate 	uname = kmem_zalloc(PAGESIZE, KM_SLEEP);
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	if ((error = fbread(vp, 0, size, S_READ, &fbp)) != 0) {
14297c478bd9Sstevel@tonic-gate 		goto end;
14307c478bd9Sstevel@tonic-gate 	}
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	off = 0;
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	while (off < size) {
14357c478bd9Sstevel@tonic-gate 		pc = (struct path_comp *)(fbp->fb_addr + off);
14367c478bd9Sstevel@tonic-gate 		switch (pc->pc_type) {
14377c478bd9Sstevel@tonic-gate 			case 1 :
14387c478bd9Sstevel@tonic-gate 				(void) strcpy(uname, ip->i_udf->udf_fsmnt);
14397c478bd9Sstevel@tonic-gate 				(void) strcat(uname, "/");
14407c478bd9Sstevel@tonic-gate 				break;
14417c478bd9Sstevel@tonic-gate 			case 2 :
14427c478bd9Sstevel@tonic-gate 				if (pc->pc_len != 0) {
14437c478bd9Sstevel@tonic-gate 					goto end;
14447c478bd9Sstevel@tonic-gate 				}
14457c478bd9Sstevel@tonic-gate 				uname[0] = '/';
14467c478bd9Sstevel@tonic-gate 				uname[1] = '\0';
14477c478bd9Sstevel@tonic-gate 				break;
14487c478bd9Sstevel@tonic-gate 			case 3 :
14497c478bd9Sstevel@tonic-gate 				(void) strcat(uname, "../");
14507c478bd9Sstevel@tonic-gate 				break;
14517c478bd9Sstevel@tonic-gate 			case 4 :
14527c478bd9Sstevel@tonic-gate 				(void) strcat(uname, "./");
14537c478bd9Sstevel@tonic-gate 				break;
14547c478bd9Sstevel@tonic-gate 			case 5 :
14557c478bd9Sstevel@tonic-gate 				if ((error = ud_uncompress(pc->pc_len, &id_len,
14566f5f1c63SDonghai Qiao 				    pc->pc_id, (uint8_t *)dname)) != 0) {
14577c478bd9Sstevel@tonic-gate 					break;
14587c478bd9Sstevel@tonic-gate 				}
14597c478bd9Sstevel@tonic-gate 				dname[id_len] = '\0';
14607c478bd9Sstevel@tonic-gate 				(void) strcat(uname, dname);
14617c478bd9Sstevel@tonic-gate 				(void) strcat(uname, "/");
14627c478bd9Sstevel@tonic-gate 				break;
14637c478bd9Sstevel@tonic-gate 			default :
14647c478bd9Sstevel@tonic-gate 				error = EINVAL;
14657c478bd9Sstevel@tonic-gate 				goto end;
14667c478bd9Sstevel@tonic-gate 		}
14677c478bd9Sstevel@tonic-gate 		off += 4 + pc->pc_len;
14687c478bd9Sstevel@tonic-gate 	}
14697c478bd9Sstevel@tonic-gate 	len = strlen(uname) - 1;
14707c478bd9Sstevel@tonic-gate 	if (uname[len] == '/') {
14717c478bd9Sstevel@tonic-gate 		if (len == 0) {
14727c478bd9Sstevel@tonic-gate 			/*
14737c478bd9Sstevel@tonic-gate 			 * special case link to /
14747c478bd9Sstevel@tonic-gate 			 */
14757c478bd9Sstevel@tonic-gate 			len = 1;
14767c478bd9Sstevel@tonic-gate 		} else {
14777c478bd9Sstevel@tonic-gate 			uname[len] = '\0';
14787c478bd9Sstevel@tonic-gate 		}
14797c478bd9Sstevel@tonic-gate 	}
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	error = uiomove(uname, len, UIO_READ, uiop);
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	ITIMES(ip);
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate end:
14867c478bd9Sstevel@tonic-gate 	if (fbp != NULL) {
14877c478bd9Sstevel@tonic-gate 		fbrelse(fbp, S_OTHER);
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
14907c478bd9Sstevel@tonic-gate 	if (uname != NULL) {
14917c478bd9Sstevel@tonic-gate 		kmem_free(uname, PAGESIZE);
14927c478bd9Sstevel@tonic-gate 	}
14937c478bd9Sstevel@tonic-gate 	if (dname != NULL) {
14947c478bd9Sstevel@tonic-gate 		kmem_free(dname, 1024);
14957c478bd9Sstevel@tonic-gate 	}
14967c478bd9Sstevel@tonic-gate 	return (error);
14977c478bd9Sstevel@tonic-gate }
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate /* ARGSUSED */
15007c478bd9Sstevel@tonic-gate static int32_t
udf_fsync(struct vnode * vp,int32_t syncflag,struct cred * cr,caller_context_t * ct)1501da6c28aaSamw udf_fsync(
1502da6c28aaSamw 	struct vnode *vp,
1503da6c28aaSamw 	int32_t syncflag,
1504da6c28aaSamw 	struct cred *cr,
1505da6c28aaSamw 	caller_context_t *ct)
15067c478bd9Sstevel@tonic-gate {
15077c478bd9Sstevel@tonic-gate 	int32_t error = 0;
15087c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	ud_printf("udf_fsync\n");
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
15137c478bd9Sstevel@tonic-gate 	if (!(IS_SWAPVP(vp))) {
15147c478bd9Sstevel@tonic-gate 		error = ud_syncip(ip, 0, I_SYNC); /* Do synchronous writes */
15157c478bd9Sstevel@tonic-gate 	}
15167c478bd9Sstevel@tonic-gate 	if (error == 0) {
15177c478bd9Sstevel@tonic-gate 		error = ud_sync_indir(ip);
15187c478bd9Sstevel@tonic-gate 	}
15197c478bd9Sstevel@tonic-gate 	ITIMES(ip);		/* XXX: is this necessary ??? */
15207c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	return (error);
15237c478bd9Sstevel@tonic-gate }
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate /* ARGSUSED */
15267c478bd9Sstevel@tonic-gate static void
udf_inactive(struct vnode * vp,struct cred * cr,caller_context_t * ct)1527da6c28aaSamw udf_inactive(struct vnode *vp, struct cred *cr, caller_context_t *ct)
15287c478bd9Sstevel@tonic-gate {
15297c478bd9Sstevel@tonic-gate 	ud_printf("udf_iinactive\n");
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 	ud_iinactive(VTOI(vp), cr);
15327c478bd9Sstevel@tonic-gate }
15337c478bd9Sstevel@tonic-gate 
1534da6c28aaSamw /* ARGSUSED */
15357c478bd9Sstevel@tonic-gate static int32_t
udf_fid(struct vnode * vp,struct fid * fidp,caller_context_t * ct)1536da6c28aaSamw udf_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
15377c478bd9Sstevel@tonic-gate {
15387c478bd9Sstevel@tonic-gate 	struct udf_fid *udfidp;
15397c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	ud_printf("udf_fid\n");
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	if (fidp->fid_len < (sizeof (struct udf_fid) - sizeof (uint16_t))) {
15447c478bd9Sstevel@tonic-gate 		fidp->fid_len = sizeof (struct udf_fid) - sizeof (uint16_t);
15457c478bd9Sstevel@tonic-gate 		return (ENOSPC);
15467c478bd9Sstevel@tonic-gate 	}
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	udfidp = (struct udf_fid *)fidp;
15497c478bd9Sstevel@tonic-gate 	bzero((char *)udfidp, sizeof (struct udf_fid));
15507c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
15517c478bd9Sstevel@tonic-gate 	udfidp->udfid_len = sizeof (struct udf_fid) - sizeof (uint16_t);
15527c478bd9Sstevel@tonic-gate 	udfidp->udfid_uinq_lo = ip->i_uniqid & 0xffffffff;
15537c478bd9Sstevel@tonic-gate 	udfidp->udfid_prn = ip->i_icb_prn;
15547c478bd9Sstevel@tonic-gate 	udfidp->udfid_icb_lbn = ip->i_icb_block;
15557c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	return (0);
15587c478bd9Sstevel@tonic-gate }
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
15617c478bd9Sstevel@tonic-gate static int
udf_rwlock(struct vnode * vp,int32_t write_lock,caller_context_t * ctp)15627c478bd9Sstevel@tonic-gate udf_rwlock(struct vnode *vp, int32_t write_lock, caller_context_t *ctp)
15637c478bd9Sstevel@tonic-gate {
15647c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	ud_printf("udf_rwlock\n");
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	if (write_lock) {
15697c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
15707c478bd9Sstevel@tonic-gate 	} else {
15717c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_READER);
15727c478bd9Sstevel@tonic-gate 	}
15737c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
15747c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
15757c478bd9Sstevel@tonic-gate #endif
15767c478bd9Sstevel@tonic-gate 	return (write_lock);
15777c478bd9Sstevel@tonic-gate }
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate /* ARGSUSED */
15807c478bd9Sstevel@tonic-gate static void
udf_rwunlock(struct vnode * vp,int32_t write_lock,caller_context_t * ctp)15817c478bd9Sstevel@tonic-gate udf_rwunlock(struct vnode *vp, int32_t write_lock, caller_context_t *ctp)
15827c478bd9Sstevel@tonic-gate {
15837c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 	ud_printf("udf_rwunlock\n");
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
15887c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_WRITER);
15897c478bd9Sstevel@tonic-gate #endif
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate }
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate /* ARGSUSED */
15967c478bd9Sstevel@tonic-gate static int32_t
udf_seek(struct vnode * vp,offset_t ooff,offset_t * noffp,caller_context_t * ct)1597da6c28aaSamw udf_seek(struct vnode *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
15987c478bd9Sstevel@tonic-gate {
15997c478bd9Sstevel@tonic-gate 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
16007c478bd9Sstevel@tonic-gate }
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate static int32_t
udf_frlock(struct vnode * vp,int32_t cmd,struct flock64 * bfp,int32_t flag,offset_t offset,struct flk_callback * flk_cbp,cred_t * cr,caller_context_t * ct)1603da6c28aaSamw udf_frlock(
1604da6c28aaSamw 	struct vnode *vp,
1605da6c28aaSamw 	int32_t cmd,
1606da6c28aaSamw 	struct flock64 *bfp,
1607da6c28aaSamw 	int32_t flag,
1608da6c28aaSamw 	offset_t offset,
1609da6c28aaSamw 	struct flk_callback *flk_cbp,
1610da6c28aaSamw 	cred_t *cr,
1611da6c28aaSamw 	caller_context_t *ct)
16127c478bd9Sstevel@tonic-gate {
16137c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	ud_printf("udf_frlock\n");
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	/*
16187c478bd9Sstevel@tonic-gate 	 * If file is being mapped, disallow frlock.
16197c478bd9Sstevel@tonic-gate 	 * XXX I am not holding tlock while checking i_mapcnt because the
16207c478bd9Sstevel@tonic-gate 	 * current locking strategy drops all locks before calling fs_frlock.
16217c478bd9Sstevel@tonic-gate 	 * So, mapcnt could change before we enter fs_frlock making is
16227c478bd9Sstevel@tonic-gate 	 * meaningless to have held tlock in the first place.
16237c478bd9Sstevel@tonic-gate 	 */
16247c478bd9Sstevel@tonic-gate 	if ((ip->i_mapcnt > 0) &&
16256f5f1c63SDonghai Qiao 	    (MANDLOCK(vp, ip->i_char))) {
16267c478bd9Sstevel@tonic-gate 		return (EAGAIN);
16277c478bd9Sstevel@tonic-gate 	}
16287c478bd9Sstevel@tonic-gate 
1629da6c28aaSamw 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
16307c478bd9Sstevel@tonic-gate }
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate /*ARGSUSED6*/
16337c478bd9Sstevel@tonic-gate static int32_t
udf_space(struct vnode * vp,int32_t cmd,struct flock64 * bfp,int32_t flag,offset_t offset,cred_t * cr,caller_context_t * ct)16347c478bd9Sstevel@tonic-gate udf_space(
16357c478bd9Sstevel@tonic-gate 	struct vnode *vp,
16367c478bd9Sstevel@tonic-gate 	int32_t cmd,
16377c478bd9Sstevel@tonic-gate 	struct flock64 *bfp,
16387c478bd9Sstevel@tonic-gate 	int32_t flag,
16397c478bd9Sstevel@tonic-gate 	offset_t offset,
16407c478bd9Sstevel@tonic-gate 	cred_t *cr,
16417c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
16427c478bd9Sstevel@tonic-gate {
16437c478bd9Sstevel@tonic-gate 	int32_t error = 0;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	ud_printf("udf_space\n");
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	if (cmd != F_FREESP) {
16487c478bd9Sstevel@tonic-gate 		error =  EINVAL;
16497c478bd9Sstevel@tonic-gate 	} else if ((error = convoff(vp, bfp, 0, offset)) == 0) {
16507c478bd9Sstevel@tonic-gate 		error = ud_freesp(vp, bfp, flag, cr);
165172102e74SBryan Cantrill 
165272102e74SBryan Cantrill 		if (error == 0 && bfp->l_start == 0)
165372102e74SBryan Cantrill 			vnevent_truncate(vp, ct);
16547c478bd9Sstevel@tonic-gate 	}
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	return (error);
16577c478bd9Sstevel@tonic-gate }
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate /* ARGSUSED */
16607c478bd9Sstevel@tonic-gate static int32_t
udf_getpage(struct vnode * vp,offset_t off,size_t len,uint32_t * protp,struct page ** plarr,size_t plsz,struct seg * seg,caddr_t addr,enum seg_rw rw,struct cred * cr,caller_context_t * ct)1661da6c28aaSamw udf_getpage(
1662da6c28aaSamw 	struct vnode *vp,
1663da6c28aaSamw 	offset_t off,
1664da6c28aaSamw 	size_t len,
1665da6c28aaSamw 	uint32_t *protp,
1666da6c28aaSamw 	struct page **plarr,
1667da6c28aaSamw 	size_t plsz,
1668da6c28aaSamw 	struct seg *seg,
1669da6c28aaSamw 	caddr_t addr,
1670da6c28aaSamw 	enum seg_rw rw,
1671da6c28aaSamw 	struct cred *cr,
1672da6c28aaSamw 	caller_context_t *ct)
16737c478bd9Sstevel@tonic-gate {
16747c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
16757c478bd9Sstevel@tonic-gate 	int32_t error, has_holes, beyond_eof, seqmode, dolock;
16767c478bd9Sstevel@tonic-gate 	int32_t pgsize = PAGESIZE;
16777c478bd9Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp = ip->i_udf;
16787c478bd9Sstevel@tonic-gate 	page_t **pl;
16797c478bd9Sstevel@tonic-gate 	u_offset_t pgoff, eoff, uoff;
16807c478bd9Sstevel@tonic-gate 	krw_t rwtype;
16817c478bd9Sstevel@tonic-gate 	caddr_t pgaddr;
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	ud_printf("udf_getpage\n");
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 	uoff = (u_offset_t)off; /* type conversion */
16867c478bd9Sstevel@tonic-gate 	if (protp) {
16877c478bd9Sstevel@tonic-gate 		*protp = PROT_ALL;
16887c478bd9Sstevel@tonic-gate 	}
16897c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
16907c478bd9Sstevel@tonic-gate 		return (ENOSYS);
16917c478bd9Sstevel@tonic-gate 	}
16927c478bd9Sstevel@tonic-gate 	seqmode = ip->i_nextr == uoff && rw != S_CREATE;
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	rwtype = RW_READER;
16957c478bd9Sstevel@tonic-gate 	dolock = (rw_owner(&ip->i_contents) != curthread);
16967c478bd9Sstevel@tonic-gate retrylock:
16977c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
16987c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, rwtype);
16997c478bd9Sstevel@tonic-gate #else
17007c478bd9Sstevel@tonic-gate 	if (dolock) {
17017c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, rwtype);
17027c478bd9Sstevel@tonic-gate 	}
17037c478bd9Sstevel@tonic-gate #endif
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	/*
17067c478bd9Sstevel@tonic-gate 	 * We may be getting called as a side effect of a bmap using
17077c478bd9Sstevel@tonic-gate 	 * fbread() when the blocks might be being allocated and the
17087c478bd9Sstevel@tonic-gate 	 * size has not yet been up'ed.  In this case we want to be
17097c478bd9Sstevel@tonic-gate 	 * able to return zero pages if we get back UDF_HOLE from
17107c478bd9Sstevel@tonic-gate 	 * calling bmap for a non write case here.  We also might have
17117c478bd9Sstevel@tonic-gate 	 * to read some frags from the disk into a page if we are
17127c478bd9Sstevel@tonic-gate 	 * extending the number of frags for a given lbn in bmap().
17137c478bd9Sstevel@tonic-gate 	 */
17147c478bd9Sstevel@tonic-gate 	beyond_eof = uoff + len > ip->i_size + PAGEOFFSET;
17157c478bd9Sstevel@tonic-gate 	if (beyond_eof && seg != segkmap) {
17167c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
17177c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
17187c478bd9Sstevel@tonic-gate #else
17197c478bd9Sstevel@tonic-gate 		if (dolock) {
17207c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
17217c478bd9Sstevel@tonic-gate 		}
17227c478bd9Sstevel@tonic-gate #endif
17237c478bd9Sstevel@tonic-gate 		return (EFAULT);
17247c478bd9Sstevel@tonic-gate 	}
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	/*
17277c478bd9Sstevel@tonic-gate 	 * Must hold i_contents lock throughout the call to pvn_getpages
17287c478bd9Sstevel@tonic-gate 	 * since locked pages are returned from each call to ud_getapage.
17297c478bd9Sstevel@tonic-gate 	 * Must *not* return locked pages and then try for contents lock
17307c478bd9Sstevel@tonic-gate 	 * due to lock ordering requirements (inode > page)
17317c478bd9Sstevel@tonic-gate 	 */
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 	has_holes = ud_bmap_has_holes(ip);
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	if ((rw == S_WRITE || rw == S_CREATE) && (has_holes || beyond_eof)) {
17367c478bd9Sstevel@tonic-gate 		int32_t	blk_size, count;
17377c478bd9Sstevel@tonic-gate 		u_offset_t offset;
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 		/*
17407c478bd9Sstevel@tonic-gate 		 * We must acquire the RW_WRITER lock in order to
17417c478bd9Sstevel@tonic-gate 		 * call bmap_write().
17427c478bd9Sstevel@tonic-gate 		 */
17437c478bd9Sstevel@tonic-gate 		if (dolock && rwtype == RW_READER) {
17447c478bd9Sstevel@tonic-gate 			rwtype = RW_WRITER;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 			if (!rw_tryupgrade(&ip->i_contents)) {
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 				rw_exit(&ip->i_contents);
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 				goto retrylock;
17517c478bd9Sstevel@tonic-gate 			}
17527c478bd9Sstevel@tonic-gate 		}
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 		/*
17557c478bd9Sstevel@tonic-gate 		 * May be allocating disk blocks for holes here as
17567c478bd9Sstevel@tonic-gate 		 * a result of mmap faults. write(2) does the bmap_write
17577c478bd9Sstevel@tonic-gate 		 * in rdip/wrip, not here. We are not dealing with frags
17587c478bd9Sstevel@tonic-gate 		 * in this case.
17597c478bd9Sstevel@tonic-gate 		 */
17607c478bd9Sstevel@tonic-gate 		offset = uoff;
17617c478bd9Sstevel@tonic-gate 		while ((offset < uoff + len) &&
17626f5f1c63SDonghai Qiao 		    (offset < ip->i_size)) {
17637c478bd9Sstevel@tonic-gate 			/*
17647c478bd9Sstevel@tonic-gate 			 * the variable "bnp" is to simplify the expression for
17657c478bd9Sstevel@tonic-gate 			 * the compiler; * just passing in &bn to bmap_write
17667c478bd9Sstevel@tonic-gate 			 * causes a compiler "loop"
17677c478bd9Sstevel@tonic-gate 			 */
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 			blk_size = udf_vfsp->udf_lbsize;
17707c478bd9Sstevel@tonic-gate 			if ((offset + blk_size) > ip->i_size) {
17717c478bd9Sstevel@tonic-gate 				count = ip->i_size - offset;
17727c478bd9Sstevel@tonic-gate 			} else {
17737c478bd9Sstevel@tonic-gate 				count = blk_size;
17747c478bd9Sstevel@tonic-gate 			}
17757c478bd9Sstevel@tonic-gate 			error = ud_bmap_write(ip, offset, count, 0, cr);
17767c478bd9Sstevel@tonic-gate 			if (error) {
17777c478bd9Sstevel@tonic-gate 				goto update_inode;
17787c478bd9Sstevel@tonic-gate 			}
17797c478bd9Sstevel@tonic-gate 			offset += count; /* XXX - make this contig */
17807c478bd9Sstevel@tonic-gate 		}
17817c478bd9Sstevel@tonic-gate 	}
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 	/*
17847c478bd9Sstevel@tonic-gate 	 * Can be a reader from now on.
17857c478bd9Sstevel@tonic-gate 	 */
17867c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
17877c478bd9Sstevel@tonic-gate 	if (rwtype == RW_WRITER) {
17887c478bd9Sstevel@tonic-gate 		rw_downgrade(&ip->i_contents);
17897c478bd9Sstevel@tonic-gate 	}
17907c478bd9Sstevel@tonic-gate #else
17917c478bd9Sstevel@tonic-gate 	if (dolock && rwtype == RW_WRITER) {
17927c478bd9Sstevel@tonic-gate 		rw_downgrade(&ip->i_contents);
17937c478bd9Sstevel@tonic-gate 	}
17947c478bd9Sstevel@tonic-gate #endif
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	/*
17977c478bd9Sstevel@tonic-gate 	 * We remove PROT_WRITE in cases when the file has UDF holes
17987c478bd9Sstevel@tonic-gate 	 * because we don't  want to call bmap_read() to check each
17997c478bd9Sstevel@tonic-gate 	 * page if it is backed with a disk block.
18007c478bd9Sstevel@tonic-gate 	 */
18017c478bd9Sstevel@tonic-gate 	if (protp && has_holes && rw != S_WRITE && rw != S_CREATE) {
18027c478bd9Sstevel@tonic-gate 		*protp &= ~PROT_WRITE;
18037c478bd9Sstevel@tonic-gate 	}
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	error = 0;
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	/*
18087c478bd9Sstevel@tonic-gate 	 * The loop looks up pages in the range <off, off + len).
18097c478bd9Sstevel@tonic-gate 	 * For each page, we first check if we should initiate an asynchronous
18107c478bd9Sstevel@tonic-gate 	 * read ahead before we call page_lookup (we may sleep in page_lookup
18117c478bd9Sstevel@tonic-gate 	 * for a previously initiated disk read).
18127c478bd9Sstevel@tonic-gate 	 */
18137c478bd9Sstevel@tonic-gate 	eoff = (uoff + len);
18147c478bd9Sstevel@tonic-gate 	for (pgoff = uoff, pgaddr = addr, pl = plarr;
18156f5f1c63SDonghai Qiao 	    pgoff < eoff; /* empty */) {
18167c478bd9Sstevel@tonic-gate 		page_t	*pp;
18177c478bd9Sstevel@tonic-gate 		u_offset_t	nextrio;
18187c478bd9Sstevel@tonic-gate 		se_t	se;
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 		se = ((rw == S_CREATE) ? SE_EXCL : SE_SHARED);
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 		/*
18237c478bd9Sstevel@tonic-gate 		 * Handle async getpage (faultahead)
18247c478bd9Sstevel@tonic-gate 		 */
18257c478bd9Sstevel@tonic-gate 		if (plarr == NULL) {
18267c478bd9Sstevel@tonic-gate 			ip->i_nextrio = pgoff;
18277c478bd9Sstevel@tonic-gate 			ud_getpage_ra(vp, pgoff, seg, pgaddr);
18287c478bd9Sstevel@tonic-gate 			pgoff += pgsize;
18297c478bd9Sstevel@tonic-gate 			pgaddr += pgsize;
18307c478bd9Sstevel@tonic-gate 			continue;
18317c478bd9Sstevel@tonic-gate 		}
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 		/*
18347c478bd9Sstevel@tonic-gate 		 * Check if we should initiate read ahead of next cluster.
18357c478bd9Sstevel@tonic-gate 		 * We call page_exists only when we need to confirm that
18367c478bd9Sstevel@tonic-gate 		 * we have the current page before we initiate the read ahead.
18377c478bd9Sstevel@tonic-gate 		 */
18387c478bd9Sstevel@tonic-gate 		nextrio = ip->i_nextrio;
18397c478bd9Sstevel@tonic-gate 		if (seqmode &&
18407c478bd9Sstevel@tonic-gate 		    pgoff + RD_CLUSTSZ(ip) >= nextrio && pgoff <= nextrio &&
18417c478bd9Sstevel@tonic-gate 		    nextrio < ip->i_size && page_exists(vp, pgoff))
18427c478bd9Sstevel@tonic-gate 			ud_getpage_ra(vp, pgoff, seg, pgaddr);
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 		if ((pp = page_lookup(vp, pgoff, se)) != NULL) {
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 			/*
18477c478bd9Sstevel@tonic-gate 			 * We found the page in the page cache.
18487c478bd9Sstevel@tonic-gate 			 */
18497c478bd9Sstevel@tonic-gate 			*pl++ = pp;
18507c478bd9Sstevel@tonic-gate 			pgoff += pgsize;
18517c478bd9Sstevel@tonic-gate 			pgaddr += pgsize;
18527c478bd9Sstevel@tonic-gate 			len -= pgsize;
18537c478bd9Sstevel@tonic-gate 			plsz -= pgsize;
18547c478bd9Sstevel@tonic-gate 		} else  {
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 			/*
18577c478bd9Sstevel@tonic-gate 			 * We have to create the page, or read it from disk.
18587c478bd9Sstevel@tonic-gate 			 */
18597c478bd9Sstevel@tonic-gate 			if (error = ud_getpage_miss(vp, pgoff, len,
18606f5f1c63SDonghai Qiao 			    seg, pgaddr, pl, plsz, rw, seqmode)) {
18617c478bd9Sstevel@tonic-gate 				goto error_out;
18627c478bd9Sstevel@tonic-gate 			}
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 			while (*pl != NULL) {
18657c478bd9Sstevel@tonic-gate 				pl++;
18667c478bd9Sstevel@tonic-gate 				pgoff += pgsize;
18677c478bd9Sstevel@tonic-gate 				pgaddr += pgsize;
18687c478bd9Sstevel@tonic-gate 				len -= pgsize;
18697c478bd9Sstevel@tonic-gate 				plsz -= pgsize;
18707c478bd9Sstevel@tonic-gate 			}
18717c478bd9Sstevel@tonic-gate 		}
18727c478bd9Sstevel@tonic-gate 	}
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	/*
18757c478bd9Sstevel@tonic-gate 	 * Return pages up to plsz if they are in the page cache.
18767c478bd9Sstevel@tonic-gate 	 * We cannot return pages if there is a chance that they are
18777c478bd9Sstevel@tonic-gate 	 * backed with a UDF hole and rw is S_WRITE or S_CREATE.
18787c478bd9Sstevel@tonic-gate 	 */
18797c478bd9Sstevel@tonic-gate 	if (plarr && !(has_holes && (rw == S_WRITE || rw == S_CREATE))) {
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 		ASSERT((protp == NULL) ||
18826f5f1c63SDonghai Qiao 		    !(has_holes && (*protp & PROT_WRITE)));
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 		eoff = pgoff + plsz;
18857c478bd9Sstevel@tonic-gate 		while (pgoff < eoff) {
18867c478bd9Sstevel@tonic-gate 			page_t		*pp;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 			if ((pp = page_lookup_nowait(vp, pgoff,
18897c478bd9Sstevel@tonic-gate 			    SE_SHARED)) == NULL)
18907c478bd9Sstevel@tonic-gate 				break;
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate 			*pl++ = pp;
18937c478bd9Sstevel@tonic-gate 			pgoff += pgsize;
18947c478bd9Sstevel@tonic-gate 			plsz -= pgsize;
18957c478bd9Sstevel@tonic-gate 		}
18967c478bd9Sstevel@tonic-gate 	}
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 	if (plarr)
18997c478bd9Sstevel@tonic-gate 		*pl = NULL;			/* Terminate page list */
19007c478bd9Sstevel@tonic-gate 	ip->i_nextr = pgoff;
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate error_out:
19037c478bd9Sstevel@tonic-gate 	if (error && plarr) {
19047c478bd9Sstevel@tonic-gate 		/*
19057c478bd9Sstevel@tonic-gate 		 * Release any pages we have locked.
19067c478bd9Sstevel@tonic-gate 		 */
19077c478bd9Sstevel@tonic-gate 		while (pl > &plarr[0])
19087c478bd9Sstevel@tonic-gate 			page_unlock(*--pl);
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 		plarr[0] = NULL;
19117c478bd9Sstevel@tonic-gate 	}
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate update_inode:
19147c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
19157c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
19167c478bd9Sstevel@tonic-gate #else
19177c478bd9Sstevel@tonic-gate 	if (dolock) {
19187c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
19197c478bd9Sstevel@tonic-gate 	}
19207c478bd9Sstevel@tonic-gate #endif
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 	/*
19237c478bd9Sstevel@tonic-gate 	 * If the inode is not already marked for IACC (in rwip() for read)
19247c478bd9Sstevel@tonic-gate 	 * and the inode is not marked for no access time update (in rwip()
19257c478bd9Sstevel@tonic-gate 	 * for write) then update the inode access time and mod time now.
19267c478bd9Sstevel@tonic-gate 	 */
19277c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
19287c478bd9Sstevel@tonic-gate 	if ((ip->i_flag & (IACC | INOACC)) == 0) {
19297c478bd9Sstevel@tonic-gate 		if ((rw != S_OTHER) && (ip->i_type != VDIR)) {
19307c478bd9Sstevel@tonic-gate 			ip->i_flag |= IACC;
19317c478bd9Sstevel@tonic-gate 		}
19327c478bd9Sstevel@tonic-gate 		if (rw == S_WRITE) {
19337c478bd9Sstevel@tonic-gate 			ip->i_flag |= IUPD;
19347c478bd9Sstevel@tonic-gate 		}
19357c478bd9Sstevel@tonic-gate 		ITIMES_NOLOCK(ip);
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	return (error);
19407c478bd9Sstevel@tonic-gate }
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate int32_t ud_delay = 1;
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate /* ARGSUSED */
19457c478bd9Sstevel@tonic-gate static int32_t
udf_putpage(struct vnode * vp,offset_t off,size_t len,int32_t flags,struct cred * cr,caller_context_t * ct)1946da6c28aaSamw udf_putpage(
1947da6c28aaSamw 	struct vnode *vp,
1948da6c28aaSamw 	offset_t off,
1949da6c28aaSamw 	size_t len,
1950da6c28aaSamw 	int32_t flags,
1951da6c28aaSamw 	struct cred *cr,
1952da6c28aaSamw 	caller_context_t *ct)
19537c478bd9Sstevel@tonic-gate {
19547c478bd9Sstevel@tonic-gate 	struct ud_inode *ip;
19557c478bd9Sstevel@tonic-gate 	int32_t error = 0;
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	ud_printf("udf_putpage\n");
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	ip = VTOI(vp);
19607c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
19617c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
19627c478bd9Sstevel@tonic-gate #endif
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	if (vp->v_count == 0) {
19657c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "ud_putpage : bad v_count");
19667c478bd9Sstevel@tonic-gate 		error = EINVAL;
19677c478bd9Sstevel@tonic-gate 		goto out;
19687c478bd9Sstevel@tonic-gate 	}
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
19717c478bd9Sstevel@tonic-gate 		error = ENOSYS;
19727c478bd9Sstevel@tonic-gate 		goto out;
19737c478bd9Sstevel@tonic-gate 	}
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 	if (flags & B_ASYNC) {
19767c478bd9Sstevel@tonic-gate 		if (ud_delay && len &&
19777c478bd9Sstevel@tonic-gate 		    (flags & ~(B_ASYNC|B_DONTNEED|B_FREE)) == 0) {
19787c478bd9Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 			/*
19817c478bd9Sstevel@tonic-gate 			 * If nobody stalled, start a new cluster.
19827c478bd9Sstevel@tonic-gate 			 */
19837c478bd9Sstevel@tonic-gate 			if (ip->i_delaylen == 0) {
19847c478bd9Sstevel@tonic-gate 				ip->i_delayoff = off;
19857c478bd9Sstevel@tonic-gate 				ip->i_delaylen = len;
19867c478bd9Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
19877c478bd9Sstevel@tonic-gate 				goto out;
19887c478bd9Sstevel@tonic-gate 			}
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 			/*
19917c478bd9Sstevel@tonic-gate 			 * If we have a full cluster or they are not contig,
19927c478bd9Sstevel@tonic-gate 			 * then push last cluster and start over.
19937c478bd9Sstevel@tonic-gate 			 */
19947c478bd9Sstevel@tonic-gate 			if (ip->i_delaylen >= WR_CLUSTSZ(ip) ||
19957c478bd9Sstevel@tonic-gate 			    ip->i_delayoff + ip->i_delaylen != off) {
19967c478bd9Sstevel@tonic-gate 				u_offset_t doff;
19977c478bd9Sstevel@tonic-gate 				size_t dlen;
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 				doff = ip->i_delayoff;
20007c478bd9Sstevel@tonic-gate 				dlen = ip->i_delaylen;
20017c478bd9Sstevel@tonic-gate 				ip->i_delayoff = off;
20027c478bd9Sstevel@tonic-gate 				ip->i_delaylen = len;
20037c478bd9Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
20047c478bd9Sstevel@tonic-gate 				error = ud_putpages(vp, doff, dlen, flags, cr);
20057c478bd9Sstevel@tonic-gate 				/* LMXXX - flags are new val, not old */
20067c478bd9Sstevel@tonic-gate 				goto out;
20077c478bd9Sstevel@tonic-gate 			}
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 			/*
20107c478bd9Sstevel@tonic-gate 			 * There is something there, it's not full, and
20117c478bd9Sstevel@tonic-gate 			 * it is contig.
20127c478bd9Sstevel@tonic-gate 			 */
20137c478bd9Sstevel@tonic-gate 			ip->i_delaylen += len;
20147c478bd9Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
20157c478bd9Sstevel@tonic-gate 			goto out;
20167c478bd9Sstevel@tonic-gate 		}
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 		/*
20197c478bd9Sstevel@tonic-gate 		 * Must have weird flags or we are not clustering.
20207c478bd9Sstevel@tonic-gate 		 */
20217c478bd9Sstevel@tonic-gate 	}
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 	error = ud_putpages(vp, off, len, flags, cr);
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate out:
20267c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
20277c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
20287c478bd9Sstevel@tonic-gate #endif
20297c478bd9Sstevel@tonic-gate 	return (error);
20307c478bd9Sstevel@tonic-gate }
20317c478bd9Sstevel@tonic-gate 
2032da6c28aaSamw /* ARGSUSED */
20337c478bd9Sstevel@tonic-gate static int32_t
udf_map(struct vnode * vp,offset_t off,struct as * as,caddr_t * addrp,size_t len,uint8_t prot,uint8_t maxprot,uint32_t flags,struct cred * cr,caller_context_t * ct)2034da6c28aaSamw udf_map(
2035da6c28aaSamw 	struct vnode *vp,
2036da6c28aaSamw 	offset_t off,
2037da6c28aaSamw 	struct as *as,
2038da6c28aaSamw 	caddr_t *addrp,
2039da6c28aaSamw 	size_t len,
2040da6c28aaSamw 	uint8_t prot,
2041da6c28aaSamw 	uint8_t maxprot,
2042da6c28aaSamw 	uint32_t flags,
2043da6c28aaSamw 	struct cred *cr,
2044da6c28aaSamw 	caller_context_t *ct)
20457c478bd9Sstevel@tonic-gate {
20467c478bd9Sstevel@tonic-gate 	struct segvn_crargs vn_a;
20477c478bd9Sstevel@tonic-gate 	int32_t error = 0;
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	ud_printf("udf_map\n");
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
20527c478bd9Sstevel@tonic-gate 		error = ENOSYS;
20537c478bd9Sstevel@tonic-gate 		goto end;
20547c478bd9Sstevel@tonic-gate 	}
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate 	if ((off < (offset_t)0) ||
20576f5f1c63SDonghai Qiao 	    ((off + len) < (offset_t)0)) {
20587c478bd9Sstevel@tonic-gate 		error = EINVAL;
20597c478bd9Sstevel@tonic-gate 		goto end;
20607c478bd9Sstevel@tonic-gate 	}
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG) {
20637c478bd9Sstevel@tonic-gate 		error = ENODEV;
20647c478bd9Sstevel@tonic-gate 		goto end;
20657c478bd9Sstevel@tonic-gate 	}
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	/*
20687c478bd9Sstevel@tonic-gate 	 * If file is being locked, disallow mapping.
20697c478bd9Sstevel@tonic-gate 	 */
20707c478bd9Sstevel@tonic-gate 	if (vn_has_mandatory_locks(vp, VTOI(vp)->i_char)) {
20717c478bd9Sstevel@tonic-gate 		error = EAGAIN;
20727c478bd9Sstevel@tonic-gate 		goto end;
20737c478bd9Sstevel@tonic-gate 	}
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	as_rangelock(as);
207660946fe0Smec 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
207760946fe0Smec 	if (error != 0) {
207860946fe0Smec 		as_rangeunlock(as);
207960946fe0Smec 		goto end;
20807c478bd9Sstevel@tonic-gate 	}
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 	vn_a.vp = vp;
20837c478bd9Sstevel@tonic-gate 	vn_a.offset = off;
20847c478bd9Sstevel@tonic-gate 	vn_a.type = flags & MAP_TYPE;
20857c478bd9Sstevel@tonic-gate 	vn_a.prot = prot;
20867c478bd9Sstevel@tonic-gate 	vn_a.maxprot = maxprot;
20877c478bd9Sstevel@tonic-gate 	vn_a.cred = cr;
20887c478bd9Sstevel@tonic-gate 	vn_a.amp = NULL;
20897c478bd9Sstevel@tonic-gate 	vn_a.flags = flags & ~MAP_TYPE;
20907c478bd9Sstevel@tonic-gate 	vn_a.szc = 0;
20917c478bd9Sstevel@tonic-gate 	vn_a.lgrp_mem_policy_flags = 0;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	error = as_map(as, *addrp, len, segvn_create, (caddr_t)&vn_a);
20947c478bd9Sstevel@tonic-gate 	as_rangeunlock(as);
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate end:
20977c478bd9Sstevel@tonic-gate 	return (error);
20987c478bd9Sstevel@tonic-gate }
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate /* ARGSUSED */
21017c478bd9Sstevel@tonic-gate static int32_t
udf_addmap(struct vnode * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uint8_t prot,uint8_t maxprot,uint32_t flags,struct cred * cr,caller_context_t * ct)2102*06042525SToomas Soome udf_addmap(struct vnode *vp, offset_t off, struct as *as, caddr_t addr,
2103*06042525SToomas Soome     size_t len, uint8_t prot, uint8_t maxprot, uint32_t flags,
2104*06042525SToomas Soome     struct cred *cr, caller_context_t *ct)
21057c478bd9Sstevel@tonic-gate {
21067c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 	ud_printf("udf_addmap\n");
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
21117c478bd9Sstevel@tonic-gate 		return (ENOSYS);
21127c478bd9Sstevel@tonic-gate 	}
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
21157c478bd9Sstevel@tonic-gate 	ip->i_mapcnt += btopr(len);
21167c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 	return (0);
21197c478bd9Sstevel@tonic-gate }
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate /* ARGSUSED */
21227c478bd9Sstevel@tonic-gate static int32_t
udf_delmap(struct vnode * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uint32_t prot,uint32_t maxprot,uint32_t flags,struct cred * cr,caller_context_t * ct)2123*06042525SToomas Soome udf_delmap(struct vnode *vp, offset_t off, struct as *as, caddr_t addr,
2124*06042525SToomas Soome     size_t len, uint32_t prot, uint32_t maxprot, uint32_t flags,
2125*06042525SToomas Soome     struct cred *cr, caller_context_t *ct)
21267c478bd9Sstevel@tonic-gate {
21277c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 	ud_printf("udf_delmap\n");
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
21327c478bd9Sstevel@tonic-gate 		return (ENOSYS);
21337c478bd9Sstevel@tonic-gate 	}
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
2136*06042525SToomas Soome 	ip->i_mapcnt -= btopr(len);	/* Count released mappings */
21377c478bd9Sstevel@tonic-gate 	ASSERT(ip->i_mapcnt >= 0);
21387c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	return (0);
21417c478bd9Sstevel@tonic-gate }
21427c478bd9Sstevel@tonic-gate 
2143da6c28aaSamw /* ARGSUSED */
21447c478bd9Sstevel@tonic-gate static int32_t
udf_l_pathconf(struct vnode * vp,int32_t cmd,ulong_t * valp,struct cred * cr,caller_context_t * ct)2145*06042525SToomas Soome udf_l_pathconf(struct vnode *vp, int32_t cmd, ulong_t *valp, struct cred *cr,
2146*06042525SToomas Soome     caller_context_t *ct)
21477c478bd9Sstevel@tonic-gate {
21487c478bd9Sstevel@tonic-gate 	int32_t error = 0;
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	ud_printf("udf_l_pathconf\n");
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	if (cmd == _PC_FILESIZEBITS) {
21537c478bd9Sstevel@tonic-gate 		/*
21547c478bd9Sstevel@tonic-gate 		 * udf supports 64 bits as file size
21557c478bd9Sstevel@tonic-gate 		 * but there are several other restrictions
21567c478bd9Sstevel@tonic-gate 		 * it only supports 32-bit block numbers and
21577c478bd9Sstevel@tonic-gate 		 * daddr32_t is only and int32_t so taking these
21587c478bd9Sstevel@tonic-gate 		 * into account we can stay just as where ufs is
21597c478bd9Sstevel@tonic-gate 		 */
21607c478bd9Sstevel@tonic-gate 		*valp = 41;
21613b862e9aSRoger A. Faulkner 	} else if (cmd == _PC_TIMESTAMP_RESOLUTION) {
21623b862e9aSRoger A. Faulkner 		/* nanosecond timestamp resolution */
21633b862e9aSRoger A. Faulkner 		*valp = 1L;
21647c478bd9Sstevel@tonic-gate 	} else {
2165da6c28aaSamw 		error = fs_pathconf(vp, cmd, valp, cr, ct);
21667c478bd9Sstevel@tonic-gate 	}
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 	return (error);
21697c478bd9Sstevel@tonic-gate }
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate uint32_t ud_pageio_reads = 0, ud_pageio_writes = 0;
21727c478bd9Sstevel@tonic-gate #ifndef	__lint
21737c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("safe sharing", ud_pageio_reads))
21747c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("safe sharing", ud_pageio_writes))
21757c478bd9Sstevel@tonic-gate #endif
21767c478bd9Sstevel@tonic-gate /*
21777c478bd9Sstevel@tonic-gate  * Assumption is that there will not be a pageio request
21787c478bd9Sstevel@tonic-gate  * to a enbedded file
21797c478bd9Sstevel@tonic-gate  */
21807c478bd9Sstevel@tonic-gate /* ARGSUSED */
21817c478bd9Sstevel@tonic-gate static int32_t
udf_pageio(struct vnode * vp,struct page * pp,u_offset_t io_off,size_t io_len,int32_t flags,struct cred * cr,caller_context_t * ct)2182da6c28aaSamw udf_pageio(
2183da6c28aaSamw 	struct vnode *vp,
2184da6c28aaSamw 	struct page *pp,
2185da6c28aaSamw 	u_offset_t io_off,
2186da6c28aaSamw 	size_t io_len,
2187da6c28aaSamw 	int32_t flags,
2188da6c28aaSamw 	struct cred *cr,
2189da6c28aaSamw 	caller_context_t *ct)
21907c478bd9Sstevel@tonic-gate {
21917c478bd9Sstevel@tonic-gate 	daddr_t bn;
21927c478bd9Sstevel@tonic-gate 	struct buf *bp;
21937c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
21947c478bd9Sstevel@tonic-gate 	int32_t dolock, error = 0, contig, multi_io;
21957c478bd9Sstevel@tonic-gate 	size_t done_len = 0, cur_len = 0;
21967c478bd9Sstevel@tonic-gate 	page_t *npp = NULL, *opp = NULL, *cpp = pp;
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	if (pp == NULL) {
21997c478bd9Sstevel@tonic-gate 		return (EINVAL);
22007c478bd9Sstevel@tonic-gate 	}
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	dolock = (rw_owner(&ip->i_contents) != curthread);
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 	/*
22057c478bd9Sstevel@tonic-gate 	 * We need a better check.  Ideally, we would use another
22067c478bd9Sstevel@tonic-gate 	 * vnodeops so that hlocked and forcibly unmounted file
22077c478bd9Sstevel@tonic-gate 	 * systems would return EIO where appropriate and w/o the
22087c478bd9Sstevel@tonic-gate 	 * need for these checks.
22097c478bd9Sstevel@tonic-gate 	 */
22107c478bd9Sstevel@tonic-gate 	if (ip->i_udf == NULL) {
22117c478bd9Sstevel@tonic-gate 		return (EIO);
22127c478bd9Sstevel@tonic-gate 	}
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
22157c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
22167c478bd9Sstevel@tonic-gate #else
22177c478bd9Sstevel@tonic-gate 	if (dolock) {
22187c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
22197c478bd9Sstevel@tonic-gate 	}
22207c478bd9Sstevel@tonic-gate #endif
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 	/*
22237c478bd9Sstevel@tonic-gate 	 * Break the io request into chunks, one for each contiguous
22247c478bd9Sstevel@tonic-gate 	 * stretch of disk blocks in the target file.
22257c478bd9Sstevel@tonic-gate 	 */
22267c478bd9Sstevel@tonic-gate 	while (done_len < io_len) {
22277c478bd9Sstevel@tonic-gate 		ASSERT(cpp);
22287c478bd9Sstevel@tonic-gate 		bp = NULL;
22297c478bd9Sstevel@tonic-gate 		contig = 0;
22307c478bd9Sstevel@tonic-gate 		if (error = ud_bmap_read(ip, (u_offset_t)(io_off + done_len),
22316f5f1c63SDonghai Qiao 		    &bn, &contig)) {
22327c478bd9Sstevel@tonic-gate 			break;
22337c478bd9Sstevel@tonic-gate 		}
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 		if (bn == UDF_HOLE) {   /* No holey swapfiles */
22367c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "SWAP file has HOLES");
22377c478bd9Sstevel@tonic-gate 			error = EINVAL;
22387c478bd9Sstevel@tonic-gate 			break;
22397c478bd9Sstevel@tonic-gate 		}
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 		cur_len = MIN(io_len - done_len, contig);
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 		/*
22447c478bd9Sstevel@tonic-gate 		 * Check if more than one I/O is
22457c478bd9Sstevel@tonic-gate 		 * required to complete the given
22467c478bd9Sstevel@tonic-gate 		 * I/O operation
22477c478bd9Sstevel@tonic-gate 		 */
22487c478bd9Sstevel@tonic-gate 		if (ip->i_udf->udf_lbsize < PAGESIZE) {
22497c478bd9Sstevel@tonic-gate 			if (cur_len >= PAGESIZE) {
22507c478bd9Sstevel@tonic-gate 				multi_io = 0;
22517c478bd9Sstevel@tonic-gate 				cur_len &= PAGEMASK;
22527c478bd9Sstevel@tonic-gate 			} else {
22537c478bd9Sstevel@tonic-gate 				multi_io = 1;
22547c478bd9Sstevel@tonic-gate 				cur_len = MIN(io_len - done_len, PAGESIZE);
22557c478bd9Sstevel@tonic-gate 			}
22567c478bd9Sstevel@tonic-gate 		}
22577c478bd9Sstevel@tonic-gate 		page_list_break(&cpp, &npp, btop(cur_len));
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 		bp = pageio_setup(cpp, cur_len, ip->i_devvp, flags);
22607c478bd9Sstevel@tonic-gate 		ASSERT(bp != NULL);
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 		bp->b_edev = ip->i_dev;
22637c478bd9Sstevel@tonic-gate 		bp->b_dev = cmpdev(ip->i_dev);
22647c478bd9Sstevel@tonic-gate 		bp->b_blkno = bn;
22657c478bd9Sstevel@tonic-gate 		bp->b_un.b_addr = (caddr_t)0;
22667c478bd9Sstevel@tonic-gate 		bp->b_file = vp;
22677c478bd9Sstevel@tonic-gate 		bp->b_offset = (offset_t)(io_off + done_len);
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate /*
22707c478bd9Sstevel@tonic-gate  *		ub.ub_pageios.value.ul++;
22717c478bd9Sstevel@tonic-gate  */
22727c478bd9Sstevel@tonic-gate 		if (multi_io == 0) {
22737c478bd9Sstevel@tonic-gate 			(void) bdev_strategy(bp);
22747c478bd9Sstevel@tonic-gate 		} else {
22757c478bd9Sstevel@tonic-gate 			error = ud_multi_strat(ip, cpp, bp,
22766f5f1c63SDonghai Qiao 			    (u_offset_t)(io_off + done_len));
22777c478bd9Sstevel@tonic-gate 			if (error != 0) {
22787c478bd9Sstevel@tonic-gate 				pageio_done(bp);
22797c478bd9Sstevel@tonic-gate 				break;
22807c478bd9Sstevel@tonic-gate 			}
22817c478bd9Sstevel@tonic-gate 		}
22827c478bd9Sstevel@tonic-gate 		if (flags & B_READ) {
22837c478bd9Sstevel@tonic-gate 			ud_pageio_reads++;
22847c478bd9Sstevel@tonic-gate 		} else {
22857c478bd9Sstevel@tonic-gate 			ud_pageio_writes++;
22867c478bd9Sstevel@tonic-gate 		}
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate 		/*
22897c478bd9Sstevel@tonic-gate 		 * If the request is not B_ASYNC, wait for i/o to complete
22907c478bd9Sstevel@tonic-gate 		 * and re-assemble the page list to return to the caller.
22917c478bd9Sstevel@tonic-gate 		 * If it is B_ASYNC we leave the page list in pieces and
22927c478bd9Sstevel@tonic-gate 		 * cleanup() will dispose of them.
22937c478bd9Sstevel@tonic-gate 		 */
22947c478bd9Sstevel@tonic-gate 		if ((flags & B_ASYNC) == 0) {
22957c478bd9Sstevel@tonic-gate 			error = biowait(bp);
22967c478bd9Sstevel@tonic-gate 			pageio_done(bp);
22977c478bd9Sstevel@tonic-gate 			if (error) {
22987c478bd9Sstevel@tonic-gate 				break;
22997c478bd9Sstevel@tonic-gate 			}
23007c478bd9Sstevel@tonic-gate 			page_list_concat(&opp, &cpp);
23017c478bd9Sstevel@tonic-gate 		}
23027c478bd9Sstevel@tonic-gate 		cpp = npp;
23037c478bd9Sstevel@tonic-gate 		npp = NULL;
23047c478bd9Sstevel@tonic-gate 		done_len += cur_len;
23057c478bd9Sstevel@tonic-gate 	}
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	ASSERT(error || (cpp == NULL && npp == NULL && done_len == io_len));
23087c478bd9Sstevel@tonic-gate 	if (error) {
23097c478bd9Sstevel@tonic-gate 		if (flags & B_ASYNC) {
23107c478bd9Sstevel@tonic-gate 			/* Cleanup unprocessed parts of list */
23117c478bd9Sstevel@tonic-gate 			page_list_concat(&cpp, &npp);
23127c478bd9Sstevel@tonic-gate 			if (flags & B_READ) {
23137c478bd9Sstevel@tonic-gate 				pvn_read_done(cpp, B_ERROR);
23147c478bd9Sstevel@tonic-gate 			} else {
23157c478bd9Sstevel@tonic-gate 				pvn_write_done(cpp, B_ERROR);
23167c478bd9Sstevel@tonic-gate 			}
23177c478bd9Sstevel@tonic-gate 		} else {
23187c478bd9Sstevel@tonic-gate 			/* Re-assemble list and let caller clean up */
23197c478bd9Sstevel@tonic-gate 			page_list_concat(&opp, &cpp);
23207c478bd9Sstevel@tonic-gate 			page_list_concat(&opp, &npp);
23217c478bd9Sstevel@tonic-gate 		}
23227c478bd9Sstevel@tonic-gate 	}
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
23257c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
23267c478bd9Sstevel@tonic-gate #else
23277c478bd9Sstevel@tonic-gate 	if (dolock) {
23287c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
23297c478bd9Sstevel@tonic-gate 	}
23307c478bd9Sstevel@tonic-gate #endif
23317c478bd9Sstevel@tonic-gate 	return (error);
23327c478bd9Sstevel@tonic-gate }
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate /* -------------------- local functions --------------------------- */
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate int32_t
ud_rdwri(enum uio_rw rw,int32_t ioflag,struct ud_inode * ip,caddr_t base,int32_t len,offset_t offset,enum uio_seg seg,int32_t * aresid,struct cred * cr)2342*06042525SToomas Soome ud_rdwri(enum uio_rw rw, int32_t ioflag, struct ud_inode *ip, caddr_t base,
2343*06042525SToomas Soome     int32_t len, offset_t offset, enum uio_seg seg, int32_t *aresid,
2344*06042525SToomas Soome     struct cred *cr)
23457c478bd9Sstevel@tonic-gate {
23467c478bd9Sstevel@tonic-gate 	int32_t error;
23477c478bd9Sstevel@tonic-gate 	struct uio auio;
23487c478bd9Sstevel@tonic-gate 	struct iovec aiov;
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	ud_printf("ud_rdwri\n");
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&auio, sizeof (uio_t));
23537c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&aiov, sizeof (iovec_t));
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	aiov.iov_base = base;
23567c478bd9Sstevel@tonic-gate 	aiov.iov_len = len;
23577c478bd9Sstevel@tonic-gate 	auio.uio_iov = &aiov;
23587c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
23597c478bd9Sstevel@tonic-gate 	auio.uio_loffset = offset;
23607c478bd9Sstevel@tonic-gate 	auio.uio_segflg = (int16_t)seg;
23617c478bd9Sstevel@tonic-gate 	auio.uio_resid = len;
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 	if (rw == UIO_WRITE) {
23647c478bd9Sstevel@tonic-gate 		auio.uio_fmode = FWRITE;
23657c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_DEFAULT;
23667c478bd9Sstevel@tonic-gate 		auio.uio_llimit = curproc->p_fsz_ctl;
23677c478bd9Sstevel@tonic-gate 		error = ud_wrip(ip, &auio, ioflag, cr);
23687c478bd9Sstevel@tonic-gate 	} else {
23697c478bd9Sstevel@tonic-gate 		auio.uio_fmode = FREAD;
23707c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_CACHED;
23717c478bd9Sstevel@tonic-gate 		auio.uio_llimit = MAXOFFSET_T;
23727c478bd9Sstevel@tonic-gate 		error = ud_rdip(ip, &auio, ioflag, cr);
23737c478bd9Sstevel@tonic-gate 	}
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	if (aresid) {
23767c478bd9Sstevel@tonic-gate 		*aresid = auio.uio_resid;
23777c478bd9Sstevel@tonic-gate 	} else if (auio.uio_resid) {
23787c478bd9Sstevel@tonic-gate 		error = EIO;
23797c478bd9Sstevel@tonic-gate 	}
23807c478bd9Sstevel@tonic-gate 	return (error);
23817c478bd9Sstevel@tonic-gate }
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate /*
23847c478bd9Sstevel@tonic-gate  * Free behind hacks.  The pager is busted.
23857c478bd9Sstevel@tonic-gate  * XXX - need to pass the information down to writedone() in a flag like B_SEQ
23867c478bd9Sstevel@tonic-gate  * or B_FREE_IF_TIGHT_ON_MEMORY.
23877c478bd9Sstevel@tonic-gate  */
23887c478bd9Sstevel@tonic-gate int32_t ud_freebehind = 1;
23897c478bd9Sstevel@tonic-gate int32_t ud_smallfile = 32 * 1024;
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate /* ARGSUSED */
23927c478bd9Sstevel@tonic-gate int32_t
ud_getpage_miss(struct vnode * vp,u_offset_t off,size_t len,struct seg * seg,caddr_t addr,page_t * pl[],size_t plsz,enum seg_rw rw,int32_t seq)2393*06042525SToomas Soome ud_getpage_miss(struct vnode *vp, u_offset_t off, size_t len, struct seg *seg,
2394*06042525SToomas Soome     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw, int32_t seq)
23957c478bd9Sstevel@tonic-gate {
23967c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
23977c478bd9Sstevel@tonic-gate 	int32_t err = 0;
23987c478bd9Sstevel@tonic-gate 	size_t io_len;
23997c478bd9Sstevel@tonic-gate 	u_offset_t io_off;
24007c478bd9Sstevel@tonic-gate 	u_offset_t pgoff;
24017c478bd9Sstevel@tonic-gate 	page_t *pp;
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	pl[0] = NULL;
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 	/*
24067c478bd9Sstevel@tonic-gate 	 * Figure out whether the page can be created, or must be
24077c478bd9Sstevel@tonic-gate 	 * read from the disk
24087c478bd9Sstevel@tonic-gate 	 */
24097c478bd9Sstevel@tonic-gate 	if (rw == S_CREATE) {
24107c478bd9Sstevel@tonic-gate 		if ((pp = page_create_va(vp, off,
24116f5f1c63SDonghai Qiao 		    PAGESIZE, PG_WAIT, seg, addr)) == NULL) {
24127c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "ud_getpage_miss: page_create");
24137c478bd9Sstevel@tonic-gate 			return (EINVAL);
24147c478bd9Sstevel@tonic-gate 		}
24157c478bd9Sstevel@tonic-gate 		io_len = PAGESIZE;
24167c478bd9Sstevel@tonic-gate 	} else {
24177c478bd9Sstevel@tonic-gate 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
24186f5f1c63SDonghai Qiao 		    &io_len, off, PAGESIZE, 0);
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 		/*
24217c478bd9Sstevel@tonic-gate 		 * Some other thread has entered the page.
24227c478bd9Sstevel@tonic-gate 		 * ud_getpage will retry page_lookup.
24237c478bd9Sstevel@tonic-gate 		 */
24247c478bd9Sstevel@tonic-gate 		if (pp == NULL) {
24257c478bd9Sstevel@tonic-gate 			return (0);
24267c478bd9Sstevel@tonic-gate 		}
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate 		/*
24297c478bd9Sstevel@tonic-gate 		 * Fill the page with as much data as we can from the file.
24307c478bd9Sstevel@tonic-gate 		 */
24317c478bd9Sstevel@tonic-gate 		err = ud_page_fill(ip, pp, off, B_READ, &pgoff);
24327c478bd9Sstevel@tonic-gate 		if (err) {
24337c478bd9Sstevel@tonic-gate 			pvn_read_done(pp, B_ERROR);
24347c478bd9Sstevel@tonic-gate 			return (err);
24357c478bd9Sstevel@tonic-gate 		}
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 		/*
24387c478bd9Sstevel@tonic-gate 		 * XXX ??? ufs has io_len instead of pgoff below
24397c478bd9Sstevel@tonic-gate 		 */
24407c478bd9Sstevel@tonic-gate 		ip->i_nextrio = off + ((pgoff + PAGESIZE - 1) & PAGEMASK);
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 		/*
24437c478bd9Sstevel@tonic-gate 		 * If the file access is sequential, initiate read ahead
24447c478bd9Sstevel@tonic-gate 		 * of the next cluster.
24457c478bd9Sstevel@tonic-gate 		 */
24467c478bd9Sstevel@tonic-gate 		if (seq && ip->i_nextrio < ip->i_size) {
24477c478bd9Sstevel@tonic-gate 			ud_getpage_ra(vp, off, seg, addr);
24487c478bd9Sstevel@tonic-gate 		}
24497c478bd9Sstevel@tonic-gate 	}
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate outmiss:
24527c478bd9Sstevel@tonic-gate 	pvn_plist_init(pp, pl, plsz, (offset_t)off, io_len, rw);
24537c478bd9Sstevel@tonic-gate 	return (err);
24547c478bd9Sstevel@tonic-gate }
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate /* ARGSUSED */
24577c478bd9Sstevel@tonic-gate void
ud_getpage_ra(struct vnode * vp,u_offset_t off,struct seg * seg,caddr_t addr)2458*06042525SToomas Soome ud_getpage_ra(struct vnode *vp, u_offset_t off, struct seg *seg, caddr_t addr)
24597c478bd9Sstevel@tonic-gate {
24607c478bd9Sstevel@tonic-gate 	page_t *pp;
24617c478bd9Sstevel@tonic-gate 	size_t io_len;
24627c478bd9Sstevel@tonic-gate 	struct ud_inode *ip = VTOI(vp);
24637c478bd9Sstevel@tonic-gate 	u_offset_t io_off = ip->i_nextrio, pgoff;
24647c478bd9Sstevel@tonic-gate 	caddr_t addr2 = addr + (io_off - off);
24657c478bd9Sstevel@tonic-gate 	daddr_t bn;
24667c478bd9Sstevel@tonic-gate 	int32_t contig = 0;
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 	/*
24697c478bd9Sstevel@tonic-gate 	 * Is this test needed?
24707c478bd9Sstevel@tonic-gate 	 */
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate 	if (addr2 >= seg->s_base + seg->s_size) {
24737c478bd9Sstevel@tonic-gate 		return;
24747c478bd9Sstevel@tonic-gate 	}
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 	contig = 0;
24777c478bd9Sstevel@tonic-gate 	if (ud_bmap_read(ip, io_off, &bn, &contig) != 0 || bn == UDF_HOLE) {
24787c478bd9Sstevel@tonic-gate 		return;
24797c478bd9Sstevel@tonic-gate 	}
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	pp = pvn_read_kluster(vp, io_off, seg, addr2,
24826f5f1c63SDonghai Qiao 	    &io_off, &io_len, io_off, PAGESIZE, 1);
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 	/*
24857c478bd9Sstevel@tonic-gate 	 * Some other thread has entered the page.
24867c478bd9Sstevel@tonic-gate 	 * So no read head done here (ie we will have to and wait
24877c478bd9Sstevel@tonic-gate 	 * for the read when needed).
24887c478bd9Sstevel@tonic-gate 	 */
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	if (pp == NULL) {
24917c478bd9Sstevel@tonic-gate 		return;
24927c478bd9Sstevel@tonic-gate 	}
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 	(void) ud_page_fill(ip, pp, io_off, (B_READ|B_ASYNC), &pgoff);
24957c478bd9Sstevel@tonic-gate 	ip->i_nextrio =  io_off + ((pgoff + PAGESIZE - 1) & PAGEMASK);
24967c478bd9Sstevel@tonic-gate }
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate int
ud_page_fill(struct ud_inode * ip,page_t * pp,u_offset_t off,uint32_t bflgs,u_offset_t * pg_off)2499*06042525SToomas Soome ud_page_fill(struct ud_inode *ip, page_t *pp, u_offset_t off, uint32_t bflgs,
2500*06042525SToomas Soome     u_offset_t *pg_off)
25017c478bd9Sstevel@tonic-gate {
25027c478bd9Sstevel@tonic-gate 	daddr_t bn;
25037c478bd9Sstevel@tonic-gate 	struct buf *bp;
25047c478bd9Sstevel@tonic-gate 	caddr_t kaddr, caddr;
25057c478bd9Sstevel@tonic-gate 	int32_t error = 0, contig = 0, multi_io = 0;
25067c478bd9Sstevel@tonic-gate 	int32_t lbsize = ip->i_udf->udf_lbsize;
25077c478bd9Sstevel@tonic-gate 	int32_t lbmask = ip->i_udf->udf_lbmask;
25087c478bd9Sstevel@tonic-gate 	uint64_t isize;
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 	isize = (ip->i_size + lbmask) & (~lbmask);
25117c478bd9Sstevel@tonic-gate 	if (ip->i_desc_type == ICB_FLAG_ONE_AD) {
25127c478bd9Sstevel@tonic-gate 
25137c478bd9Sstevel@tonic-gate 		/*
25147c478bd9Sstevel@tonic-gate 		 * Embedded file read file_entry
25157c478bd9Sstevel@tonic-gate 		 * from buffer cache and copy the required
25167c478bd9Sstevel@tonic-gate 		 * portions
25177c478bd9Sstevel@tonic-gate 		 */
25187c478bd9Sstevel@tonic-gate 		bp = ud_bread(ip->i_dev,
25196f5f1c63SDonghai Qiao 		    ip->i_icb_lbano << ip->i_udf->udf_l2d_shift, lbsize);
25207c478bd9Sstevel@tonic-gate 		if ((bp->b_error == 0) &&
25216f5f1c63SDonghai Qiao 		    (bp->b_resid == 0)) {
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 			caddr = bp->b_un.b_addr + ip->i_data_off;
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate 			/*
25267c478bd9Sstevel@tonic-gate 			 * mapin to kvm
25277c478bd9Sstevel@tonic-gate 			 */
25287c478bd9Sstevel@tonic-gate 			kaddr = (caddr_t)ppmapin(pp,
25296f5f1c63SDonghai Qiao 			    PROT_READ | PROT_WRITE, (caddr_t)-1);
25307c478bd9Sstevel@tonic-gate 			(void) kcopy(caddr, kaddr, ip->i_size);
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate 			/*
25337c478bd9Sstevel@tonic-gate 			 * mapout of kvm
25347c478bd9Sstevel@tonic-gate 			 */
25357c478bd9Sstevel@tonic-gate 			ppmapout(kaddr);
25367c478bd9Sstevel@tonic-gate 		}
25377c478bd9Sstevel@tonic-gate 		brelse(bp);
25387c478bd9Sstevel@tonic-gate 		contig = ip->i_size;
25397c478bd9Sstevel@tonic-gate 	} else {
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 		/*
25427c478bd9Sstevel@tonic-gate 		 * Get the continuous size and block number
25437c478bd9Sstevel@tonic-gate 		 * at offset "off"
25447c478bd9Sstevel@tonic-gate 		 */
25457c478bd9Sstevel@tonic-gate 		if (error = ud_bmap_read(ip, off, &bn, &contig))
25467c478bd9Sstevel@tonic-gate 			goto out;
25477c478bd9Sstevel@tonic-gate 		contig = MIN(contig, PAGESIZE);
25487c478bd9Sstevel@tonic-gate 		contig = (contig + lbmask) & (~lbmask);
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate 		/*
25517c478bd9Sstevel@tonic-gate 		 * Zero part of the page which we are not
25527c478bd9Sstevel@tonic-gate 		 * going to read from the disk.
25537c478bd9Sstevel@tonic-gate 		 */
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate 		if (bn == UDF_HOLE) {
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate 			/*
25587c478bd9Sstevel@tonic-gate 			 * This is a HOLE. Just zero out
25597c478bd9Sstevel@tonic-gate 			 * the page
25607c478bd9Sstevel@tonic-gate 			 */
25617c478bd9Sstevel@tonic-gate 			if (((off + contig) == isize) ||
25626f5f1c63SDonghai Qiao 			    (contig == PAGESIZE)) {
25637c478bd9Sstevel@tonic-gate 				pagezero(pp->p_prev, 0, PAGESIZE);
25647c478bd9Sstevel@tonic-gate 				goto out;
25657c478bd9Sstevel@tonic-gate 			}
25667c478bd9Sstevel@tonic-gate 		}
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 		if (contig < PAGESIZE) {
25697c478bd9Sstevel@tonic-gate 			uint64_t count;
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate 			count = isize - off;
25727c478bd9Sstevel@tonic-gate 			if (contig != count) {
25737c478bd9Sstevel@tonic-gate 				multi_io = 1;
25747c478bd9Sstevel@tonic-gate 				contig = (int32_t)(MIN(count, PAGESIZE));
25757c478bd9Sstevel@tonic-gate 			} else {
25767c478bd9Sstevel@tonic-gate 				pagezero(pp->p_prev, contig, PAGESIZE - contig);
25777c478bd9Sstevel@tonic-gate 			}
25787c478bd9Sstevel@tonic-gate 		}
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 		/*
25817c478bd9Sstevel@tonic-gate 		 * Get a bp and initialize it
25827c478bd9Sstevel@tonic-gate 		 */
25837c478bd9Sstevel@tonic-gate 		bp = pageio_setup(pp, contig, ip->i_devvp, bflgs);
25847c478bd9Sstevel@tonic-gate 		ASSERT(bp != NULL);
25857c478bd9Sstevel@tonic-gate 
25867c478bd9Sstevel@tonic-gate 		bp->b_edev = ip->i_dev;
25877c478bd9Sstevel@tonic-gate 		bp->b_dev = cmpdev(ip->i_dev);
25887c478bd9Sstevel@tonic-gate 		bp->b_blkno = bn;
25897c478bd9Sstevel@tonic-gate 		bp->b_un.b_addr = 0;
25907c478bd9Sstevel@tonic-gate 		bp->b_file = ip->i_vnode;
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 		/*
25937c478bd9Sstevel@tonic-gate 		 * Start I/O
25947c478bd9Sstevel@tonic-gate 		 */
25957c478bd9Sstevel@tonic-gate 		if (multi_io == 0) {
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate 			/*
25987c478bd9Sstevel@tonic-gate 			 * Single I/O is sufficient for this page
25997c478bd9Sstevel@tonic-gate 			 */
26007c478bd9Sstevel@tonic-gate 			(void) bdev_strategy(bp);
26017c478bd9Sstevel@tonic-gate 		} else {
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 			/*
26047c478bd9Sstevel@tonic-gate 			 * We need to do the I/O in
26057c478bd9Sstevel@tonic-gate 			 * piece's
26067c478bd9Sstevel@tonic-gate 			 */
26077c478bd9Sstevel@tonic-gate 			error = ud_multi_strat(ip, pp, bp, off);
26087c478bd9Sstevel@tonic-gate 			if (error != 0) {
26097c478bd9Sstevel@tonic-gate 				goto out;
26107c478bd9Sstevel@tonic-gate 			}
26117c478bd9Sstevel@tonic-gate 		}
26127c478bd9Sstevel@tonic-gate 		if ((bflgs & B_ASYNC) == 0) {
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate 			/*
26157c478bd9Sstevel@tonic-gate 			 * Wait for i/o to complete.
26167c478bd9Sstevel@tonic-gate 			 */
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 			error = biowait(bp);
26197c478bd9Sstevel@tonic-gate 			pageio_done(bp);
26207c478bd9Sstevel@tonic-gate 			if (error) {
26217c478bd9Sstevel@tonic-gate 				goto out;
26227c478bd9Sstevel@tonic-gate 			}
26237c478bd9Sstevel@tonic-gate 		}
26247c478bd9Sstevel@tonic-gate 	}
26257c478bd9Sstevel@tonic-gate 	if ((off + contig) >= ip->i_size) {
26267c478bd9Sstevel@tonic-gate 		contig = ip->i_size - off;
26277c478bd9Sstevel@tonic-gate 	}
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate out:
26307c478bd9Sstevel@tonic-gate 	*pg_off = contig;
26317c478bd9Sstevel@tonic-gate 	return (error);
26327c478bd9Sstevel@tonic-gate }
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate int32_t
ud_putpages(struct vnode * vp,offset_t off,size_t len,int32_t flags,struct cred * cr)2635*06042525SToomas Soome ud_putpages(struct vnode *vp, offset_t off, size_t len, int32_t flags,
2636*06042525SToomas Soome     struct cred *cr)
26377c478bd9Sstevel@tonic-gate {
26387c478bd9Sstevel@tonic-gate 	struct ud_inode *ip;
26397c478bd9Sstevel@tonic-gate 	page_t *pp;
26407c478bd9Sstevel@tonic-gate 	u_offset_t io_off;
26417c478bd9Sstevel@tonic-gate 	size_t io_len;
26427c478bd9Sstevel@tonic-gate 	u_offset_t eoff;
26437c478bd9Sstevel@tonic-gate 	int32_t err = 0;
26447c478bd9Sstevel@tonic-gate 	int32_t dolock;
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate 	ud_printf("ud_putpages\n");
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate 	if (vp->v_count == 0) {
26497c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "ud_putpages: bad v_count");
26507c478bd9Sstevel@tonic-gate 		return (EINVAL);
26517c478bd9Sstevel@tonic-gate 	}
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 	ip = VTOI(vp);
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 	/*
26567c478bd9Sstevel@tonic-gate 	 * Acquire the readers/write inode lock before locking
26577c478bd9Sstevel@tonic-gate 	 * any pages in this inode.
26587c478bd9Sstevel@tonic-gate 	 * The inode lock is held during i/o.
26597c478bd9Sstevel@tonic-gate 	 */
26607c478bd9Sstevel@tonic-gate 	if (len == 0) {
26617c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
26627c478bd9Sstevel@tonic-gate 		ip->i_delayoff = ip->i_delaylen = 0;
26637c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
26647c478bd9Sstevel@tonic-gate 	}
26657c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
26667c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
26677c478bd9Sstevel@tonic-gate #else
26687c478bd9Sstevel@tonic-gate 	dolock = (rw_owner(&ip->i_contents) != curthread);
26697c478bd9Sstevel@tonic-gate 	if (dolock) {
26707c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
26717c478bd9Sstevel@tonic-gate 	}
26727c478bd9Sstevel@tonic-gate #endif
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 	if (!vn_has_cached_data(vp)) {
26757c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
26767c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
26777c478bd9Sstevel@tonic-gate #else
26787c478bd9Sstevel@tonic-gate 		if (dolock) {
26797c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
26807c478bd9Sstevel@tonic-gate 		}
26817c478bd9Sstevel@tonic-gate #endif
26827c478bd9Sstevel@tonic-gate 		return (0);
26837c478bd9Sstevel@tonic-gate 	}
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 	if (len == 0) {
26867c478bd9Sstevel@tonic-gate 		/*
26877c478bd9Sstevel@tonic-gate 		 * Search the entire vp list for pages >= off.
26887c478bd9Sstevel@tonic-gate 		 */
26897c478bd9Sstevel@tonic-gate 		err = pvn_vplist_dirty(vp, (u_offset_t)off, ud_putapage,
26906f5f1c63SDonghai Qiao 		    flags, cr);
26917c478bd9Sstevel@tonic-gate 	} else {
26927c478bd9Sstevel@tonic-gate 		/*
26937c478bd9Sstevel@tonic-gate 		 * Loop over all offsets in the range looking for
26947c478bd9Sstevel@tonic-gate 		 * pages to deal with.
26957c478bd9Sstevel@tonic-gate 		 */
26967c478bd9Sstevel@tonic-gate 		if ((eoff = blkroundup(ip->i_udf, ip->i_size)) != 0) {
26977c478bd9Sstevel@tonic-gate 			eoff = MIN(off + len, eoff);
26987c478bd9Sstevel@tonic-gate 		} else {
26997c478bd9Sstevel@tonic-gate 			eoff = off + len;
27007c478bd9Sstevel@tonic-gate 		}
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 		for (io_off = off; io_off < eoff; io_off += io_len) {
27037c478bd9Sstevel@tonic-gate 			/*
27047c478bd9Sstevel@tonic-gate 			 * If we are not invalidating, synchronously
27057c478bd9Sstevel@tonic-gate 			 * freeing or writing pages, use the routine
27067c478bd9Sstevel@tonic-gate 			 * page_lookup_nowait() to prevent reclaiming
27077c478bd9Sstevel@tonic-gate 			 * them from the free list.
27087c478bd9Sstevel@tonic-gate 			 */
27097c478bd9Sstevel@tonic-gate 			if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
27107c478bd9Sstevel@tonic-gate 				pp = page_lookup(vp, io_off,
27116f5f1c63SDonghai Qiao 				    (flags & (B_INVAL | B_FREE)) ?
27126f5f1c63SDonghai Qiao 				    SE_EXCL : SE_SHARED);
27137c478bd9Sstevel@tonic-gate 			} else {
27147c478bd9Sstevel@tonic-gate 				pp = page_lookup_nowait(vp, io_off,
27156f5f1c63SDonghai Qiao 				    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
27167c478bd9Sstevel@tonic-gate 			}
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 			if (pp == NULL || pvn_getdirty(pp, flags) == 0) {
27197c478bd9Sstevel@tonic-gate 				io_len = PAGESIZE;
27207c478bd9Sstevel@tonic-gate 			} else {
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate 				err = ud_putapage(vp, pp,
27236f5f1c63SDonghai Qiao 				    &io_off, &io_len, flags, cr);
27247c478bd9Sstevel@tonic-gate 				if (err != 0) {
27257c478bd9Sstevel@tonic-gate 					break;
27267c478bd9Sstevel@tonic-gate 				}
27277c478bd9Sstevel@tonic-gate 				/*
27287c478bd9Sstevel@tonic-gate 				 * "io_off" and "io_len" are returned as
27297c478bd9Sstevel@tonic-gate 				 * the range of pages we actually wrote.
27307c478bd9Sstevel@tonic-gate 				 * This allows us to skip ahead more quickly
27317c478bd9Sstevel@tonic-gate 				 * since several pages may've been dealt
27327c478bd9Sstevel@tonic-gate 				 * with by this iteration of the loop.
27337c478bd9Sstevel@tonic-gate 				 */
27347c478bd9Sstevel@tonic-gate 			}
27357c478bd9Sstevel@tonic-gate 		}
27367c478bd9Sstevel@tonic-gate 	}
27377c478bd9Sstevel@tonic-gate 	if (err == 0 && off == 0 && (len == 0 || len >= ip->i_size)) {
27387c478bd9Sstevel@tonic-gate 		/*
27397c478bd9Sstevel@tonic-gate 		 * We have just sync'ed back all the pages on
27407c478bd9Sstevel@tonic-gate 		 * the inode, turn off the IMODTIME flag.
27417c478bd9Sstevel@tonic-gate 		 */
27427c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
27437c478bd9Sstevel@tonic-gate 		ip->i_flag &= ~IMODTIME;
27447c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
27457c478bd9Sstevel@tonic-gate 	}
27467c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
27477c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
27487c478bd9Sstevel@tonic-gate #else
27497c478bd9Sstevel@tonic-gate 	if (dolock) {
27507c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
27517c478bd9Sstevel@tonic-gate 	}
27527c478bd9Sstevel@tonic-gate #endif
27537c478bd9Sstevel@tonic-gate 	return (err);
27547c478bd9Sstevel@tonic-gate }
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate /* ARGSUSED */
27577c478bd9Sstevel@tonic-gate int32_t
ud_putapage(struct vnode * vp,page_t * pp,u_offset_t * offp,size_t * lenp,int32_t flags,struct cred * cr)2758*06042525SToomas Soome ud_putapage(struct vnode *vp, page_t *pp, u_offset_t *offp,
2759*06042525SToomas Soome     size_t *lenp, int32_t flags, struct cred *cr)
27607c478bd9Sstevel@tonic-gate {
27617c478bd9Sstevel@tonic-gate 	daddr_t bn;
27627c478bd9Sstevel@tonic-gate 	size_t io_len;
27637c478bd9Sstevel@tonic-gate 	struct ud_inode *ip;
27647c478bd9Sstevel@tonic-gate 	int32_t error = 0, contig, multi_io = 0;
27657c478bd9Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
27667c478bd9Sstevel@tonic-gate 	u_offset_t off, io_off;
27677c478bd9Sstevel@tonic-gate 	caddr_t kaddr, caddr;
27687c478bd9Sstevel@tonic-gate 	struct buf *bp = NULL;
27697c478bd9Sstevel@tonic-gate 	int32_t lbmask;
27707c478bd9Sstevel@tonic-gate 	uint64_t isize;
2771ad9a7bd3SRichard Lowe 	uint16_t crc_len;
27727c478bd9Sstevel@tonic-gate 	struct file_entry *fe;
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	ud_printf("ud_putapage\n");
27757c478bd9Sstevel@tonic-gate 
27767c478bd9Sstevel@tonic-gate 	ip = VTOI(vp);
27777c478bd9Sstevel@tonic-gate 	ASSERT(ip);
27787c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
27797c478bd9Sstevel@tonic-gate 	lbmask = ip->i_udf->udf_lbmask;
27807c478bd9Sstevel@tonic-gate 	isize = (ip->i_size + lbmask) & (~lbmask);
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate 	udf_vfsp = ip->i_udf;
27837c478bd9Sstevel@tonic-gate 	ASSERT(udf_vfsp->udf_flags & UDF_FL_RW);
27847c478bd9Sstevel@tonic-gate 
27857c478bd9Sstevel@tonic-gate 	/*
27867c478bd9Sstevel@tonic-gate 	 * If the modified time on the inode has not already been
27877c478bd9Sstevel@tonic-gate 	 * set elsewhere (e.g. for write/setattr) we set the time now.
27887c478bd9Sstevel@tonic-gate 	 * This gives us approximate modified times for mmap'ed files
27897c478bd9Sstevel@tonic-gate 	 * which are modified via stores in the user address space.
27907c478bd9Sstevel@tonic-gate 	 */
27917c478bd9Sstevel@tonic-gate 	if (((ip->i_flag & IMODTIME) == 0) || (flags & B_FORCE)) {
27927c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
27937c478bd9Sstevel@tonic-gate 		ip->i_flag |= IUPD;
27947c478bd9Sstevel@tonic-gate 		ITIMES_NOLOCK(ip);
27957c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
27967c478bd9Sstevel@tonic-gate 	}
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	/*
28007c478bd9Sstevel@tonic-gate 	 * Align the request to a block boundry (for old file systems),
28017c478bd9Sstevel@tonic-gate 	 * and go ask bmap() how contiguous things are for this file.
28027c478bd9Sstevel@tonic-gate 	 */
28037c478bd9Sstevel@tonic-gate 	off = pp->p_offset & ~(offset_t)lbmask;
28047c478bd9Sstevel@tonic-gate 				/* block align it */
28057c478bd9Sstevel@tonic-gate 
28067c478bd9Sstevel@tonic-gate 
28077c478bd9Sstevel@tonic-gate 	if (ip->i_desc_type == ICB_FLAG_ONE_AD) {
28087c478bd9Sstevel@tonic-gate 		ASSERT(ip->i_size <= ip->i_max_emb);
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate 		pp = pvn_write_kluster(vp, pp, &io_off,
28116f5f1c63SDonghai Qiao 		    &io_len, off, PAGESIZE, flags);
28127c478bd9Sstevel@tonic-gate 		if (io_len == 0) {
28137c478bd9Sstevel@tonic-gate 			io_len = PAGESIZE;
28147c478bd9Sstevel@tonic-gate 		}
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate 		bp = ud_bread(ip->i_dev,
28176f5f1c63SDonghai Qiao 		    ip->i_icb_lbano << udf_vfsp->udf_l2d_shift,
28186f5f1c63SDonghai Qiao 		    udf_vfsp->udf_lbsize);
28197c478bd9Sstevel@tonic-gate 		fe = (struct file_entry *)bp->b_un.b_addr;
28207c478bd9Sstevel@tonic-gate 		if ((bp->b_flags & B_ERROR) ||
28217c478bd9Sstevel@tonic-gate 		    (ud_verify_tag_and_desc(&fe->fe_tag, UD_FILE_ENTRY,
28227c478bd9Sstevel@tonic-gate 		    ip->i_icb_block,
28237c478bd9Sstevel@tonic-gate 		    1, udf_vfsp->udf_lbsize) != 0)) {
28247c478bd9Sstevel@tonic-gate 			if (pp != NULL)
28257c478bd9Sstevel@tonic-gate 				pvn_write_done(pp, B_ERROR | B_WRITE | flags);
28267c478bd9Sstevel@tonic-gate 			if (bp->b_flags & B_ERROR) {
28277c478bd9Sstevel@tonic-gate 				error = EIO;
28287c478bd9Sstevel@tonic-gate 			} else {
28297c478bd9Sstevel@tonic-gate 				error = EINVAL;
28307c478bd9Sstevel@tonic-gate 			}
28317c478bd9Sstevel@tonic-gate 			brelse(bp);
28327c478bd9Sstevel@tonic-gate 			return (error);
28337c478bd9Sstevel@tonic-gate 		}
28347c478bd9Sstevel@tonic-gate 		if ((bp->b_error == 0) &&
28356f5f1c63SDonghai Qiao 		    (bp->b_resid == 0)) {
28367c478bd9Sstevel@tonic-gate 
28377c478bd9Sstevel@tonic-gate 			caddr = bp->b_un.b_addr + ip->i_data_off;
28387c478bd9Sstevel@tonic-gate 			kaddr = (caddr_t)ppmapin(pp,
28396f5f1c63SDonghai Qiao 			    PROT_READ | PROT_WRITE, (caddr_t)-1);
28407c478bd9Sstevel@tonic-gate 			(void) kcopy(kaddr, caddr, ip->i_size);
28417c478bd9Sstevel@tonic-gate 			ppmapout(kaddr);
28427c478bd9Sstevel@tonic-gate 		}
2843ad9a7bd3SRichard Lowe 		crc_len = offsetof(struct file_entry, fe_spec) +
28446f5f1c63SDonghai Qiao 		    SWAP_32(fe->fe_len_ear);
28457c478bd9Sstevel@tonic-gate 		crc_len += ip->i_size;
28467c478bd9Sstevel@tonic-gate 		ud_make_tag(ip->i_udf, &fe->fe_tag,
28476f5f1c63SDonghai Qiao 		    UD_FILE_ENTRY, ip->i_icb_block, crc_len);
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 		bwrite(bp);
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 		if (flags & B_ASYNC) {
28527c478bd9Sstevel@tonic-gate 			pvn_write_done(pp, flags);
28537c478bd9Sstevel@tonic-gate 		}
28547c478bd9Sstevel@tonic-gate 		contig = ip->i_size;
28557c478bd9Sstevel@tonic-gate 	} else {
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 		if (error = ud_bmap_read(ip, off, &bn, &contig)) {
28587c478bd9Sstevel@tonic-gate 			goto out;
28597c478bd9Sstevel@tonic-gate 		}
28607c478bd9Sstevel@tonic-gate 		contig = MIN(contig, PAGESIZE);
28617c478bd9Sstevel@tonic-gate 		contig = (contig + lbmask) & (~lbmask);
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate 		if (contig < PAGESIZE) {
28647c478bd9Sstevel@tonic-gate 			uint64_t count;
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 			count = isize - off;
28677c478bd9Sstevel@tonic-gate 			if (contig != count) {
28687c478bd9Sstevel@tonic-gate 				multi_io = 1;
28697c478bd9Sstevel@tonic-gate 				contig = (int32_t)(MIN(count, PAGESIZE));
28707c478bd9Sstevel@tonic-gate 			}
28717c478bd9Sstevel@tonic-gate 		}
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate 		if ((off + contig) > isize) {
28747c478bd9Sstevel@tonic-gate 			contig = isize - off;
28757c478bd9Sstevel@tonic-gate 		}
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 		if (contig > PAGESIZE) {
28787c478bd9Sstevel@tonic-gate 			if (contig & PAGEOFFSET) {
28797c478bd9Sstevel@tonic-gate 				contig &= PAGEMASK;
28807c478bd9Sstevel@tonic-gate 			}
28817c478bd9Sstevel@tonic-gate 		}
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate 		pp = pvn_write_kluster(vp, pp, &io_off,
28846f5f1c63SDonghai Qiao 		    &io_len, off, contig, flags);
28857c478bd9Sstevel@tonic-gate 		if (io_len == 0) {
28867c478bd9Sstevel@tonic-gate 			io_len = PAGESIZE;
28877c478bd9Sstevel@tonic-gate 		}
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 		bp = pageio_setup(pp, contig, ip->i_devvp, B_WRITE | flags);
28907c478bd9Sstevel@tonic-gate 		ASSERT(bp != NULL);
28917c478bd9Sstevel@tonic-gate 
28927c478bd9Sstevel@tonic-gate 		bp->b_edev = ip->i_dev;
28937c478bd9Sstevel@tonic-gate 		bp->b_dev = cmpdev(ip->i_dev);
28947c478bd9Sstevel@tonic-gate 		bp->b_blkno = bn;
28957c478bd9Sstevel@tonic-gate 		bp->b_un.b_addr = 0;
28967c478bd9Sstevel@tonic-gate 		bp->b_file = vp;
28977c478bd9Sstevel@tonic-gate 		bp->b_offset = (offset_t)off;
28987c478bd9Sstevel@tonic-gate 
28997c478bd9Sstevel@tonic-gate 
29007c478bd9Sstevel@tonic-gate 		/*
29017c478bd9Sstevel@tonic-gate 		 * write throttle
29027c478bd9Sstevel@tonic-gate 		 */
29037c478bd9Sstevel@tonic-gate 		ASSERT(bp->b_iodone == NULL);
29047c478bd9Sstevel@tonic-gate 		bp->b_iodone = ud_iodone;
29057c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
29067c478bd9Sstevel@tonic-gate 		ip->i_writes += bp->b_bcount;
29077c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 		if (multi_io == 0) {
29107c478bd9Sstevel@tonic-gate 
29117c478bd9Sstevel@tonic-gate 			(void) bdev_strategy(bp);
29127c478bd9Sstevel@tonic-gate 		} else {
29137c478bd9Sstevel@tonic-gate 			error = ud_multi_strat(ip, pp, bp, off);
29147c478bd9Sstevel@tonic-gate 			if (error != 0) {
29157c478bd9Sstevel@tonic-gate 				goto out;
29167c478bd9Sstevel@tonic-gate 			}
29177c478bd9Sstevel@tonic-gate 		}
29187c478bd9Sstevel@tonic-gate 
29197c478bd9Sstevel@tonic-gate 		if ((flags & B_ASYNC) == 0) {
29207c478bd9Sstevel@tonic-gate 			/*
29217c478bd9Sstevel@tonic-gate 			 * Wait for i/o to complete.
29227c478bd9Sstevel@tonic-gate 			 */
29237c478bd9Sstevel@tonic-gate 			error = biowait(bp);
29247c478bd9Sstevel@tonic-gate 			pageio_done(bp);
29257c478bd9Sstevel@tonic-gate 		}
29267c478bd9Sstevel@tonic-gate 	}
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 	if ((flags & B_ASYNC) == 0) {
29297c478bd9Sstevel@tonic-gate 		pvn_write_done(pp, ((error) ? B_ERROR : 0) | B_WRITE | flags);
29307c478bd9Sstevel@tonic-gate 	}
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate 	pp = NULL;
29337c478bd9Sstevel@tonic-gate 
29347c478bd9Sstevel@tonic-gate out:
29357c478bd9Sstevel@tonic-gate 	if (error != 0 && pp != NULL) {
29367c478bd9Sstevel@tonic-gate 		pvn_write_done(pp, B_ERROR | B_WRITE | flags);
29377c478bd9Sstevel@tonic-gate 	}
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate 	if (offp) {
29407c478bd9Sstevel@tonic-gate 		*offp = io_off;
29417c478bd9Sstevel@tonic-gate 	}
29427c478bd9Sstevel@tonic-gate 	if (lenp) {
29437c478bd9Sstevel@tonic-gate 		*lenp = io_len;
29447c478bd9Sstevel@tonic-gate 	}
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	return (error);
29477c478bd9Sstevel@tonic-gate }
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 
29507c478bd9Sstevel@tonic-gate int32_t
ud_iodone(struct buf * bp)29517c478bd9Sstevel@tonic-gate ud_iodone(struct buf *bp)
29527c478bd9Sstevel@tonic-gate {
29537c478bd9Sstevel@tonic-gate 	struct ud_inode *ip;
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 	ASSERT((bp->b_pages->p_vnode != NULL) && !(bp->b_flags & B_READ));
29567c478bd9Sstevel@tonic-gate 
29577c478bd9Sstevel@tonic-gate 	bp->b_iodone = NULL;
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate 	ip = VTOI(bp->b_pages->p_vnode);
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
29627c478bd9Sstevel@tonic-gate 	if (ip->i_writes >= ud_LW) {
29637c478bd9Sstevel@tonic-gate 		if ((ip->i_writes -= bp->b_bcount) <= ud_LW) {
29647c478bd9Sstevel@tonic-gate 			if (ud_WRITES) {
29657c478bd9Sstevel@tonic-gate 				cv_broadcast(&ip->i_wrcv); /* wake all up */
29667c478bd9Sstevel@tonic-gate 			}
29677c478bd9Sstevel@tonic-gate 		}
29687c478bd9Sstevel@tonic-gate 	} else {
29697c478bd9Sstevel@tonic-gate 		ip->i_writes -= bp->b_bcount;
29707c478bd9Sstevel@tonic-gate 	}
29717c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
29727c478bd9Sstevel@tonic-gate 	iodone(bp);
29737c478bd9Sstevel@tonic-gate 	return (0);
29747c478bd9Sstevel@tonic-gate }
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
29777c478bd9Sstevel@tonic-gate int32_t
ud_rdip(struct ud_inode * ip,struct uio * uio,int32_t ioflag,cred_t * cr)29787c478bd9Sstevel@tonic-gate ud_rdip(struct ud_inode *ip, struct uio *uio, int32_t ioflag, cred_t *cr)
29797c478bd9Sstevel@tonic-gate {
29807c478bd9Sstevel@tonic-gate 	struct vnode *vp;
29817c478bd9Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
29827c478bd9Sstevel@tonic-gate 	krw_t rwtype;
29837c478bd9Sstevel@tonic-gate 	caddr_t base;
29847c478bd9Sstevel@tonic-gate 	uint32_t flags;
29857c478bd9Sstevel@tonic-gate 	int32_t error, n, on, mapon, dofree;
29867c478bd9Sstevel@tonic-gate 	u_offset_t off;
29877c478bd9Sstevel@tonic-gate 	long oresid = uio->uio_resid;
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
29907c478bd9Sstevel@tonic-gate 	if ((ip->i_type != VREG) &&
29916f5f1c63SDonghai Qiao 	    (ip->i_type != VDIR) &&
29926f5f1c63SDonghai Qiao 	    (ip->i_type != VLNK)) {
29937c478bd9Sstevel@tonic-gate 		return (EIO);
29947c478bd9Sstevel@tonic-gate 	}
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	if (uio->uio_loffset > MAXOFFSET_T) {
29977c478bd9Sstevel@tonic-gate 		return (0);
29987c478bd9Sstevel@tonic-gate 	}
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 	if ((uio->uio_loffset < (offset_t)0) ||
30016f5f1c63SDonghai Qiao 	    ((uio->uio_loffset + uio->uio_resid) < 0)) {
30027c478bd9Sstevel@tonic-gate 		return (EINVAL);
30037c478bd9Sstevel@tonic-gate 	}
30047c478bd9Sstevel@tonic-gate 	if (uio->uio_resid == 0) {
30057c478bd9Sstevel@tonic-gate 		return (0);
30067c478bd9Sstevel@tonic-gate 	}
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate 	vp = ITOV(ip);
30097c478bd9Sstevel@tonic-gate 	udf_vfsp = ip->i_udf;
30107c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
30117c478bd9Sstevel@tonic-gate 	ip->i_flag |= IACC;
30127c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 	rwtype = (rw_write_held(&ip->i_contents)?RW_WRITER:RW_READER);
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 	do {
30177c478bd9Sstevel@tonic-gate 		offset_t diff;
30187c478bd9Sstevel@tonic-gate 		u_offset_t uoff = uio->uio_loffset;
30197c478bd9Sstevel@tonic-gate 		off = uoff & (offset_t)MAXBMASK;
30207c478bd9Sstevel@tonic-gate 		mapon = (int)(uoff & (offset_t)MAXBOFFSET);
30217c478bd9Sstevel@tonic-gate 		on = (int)blkoff(udf_vfsp, uoff);
30227c478bd9Sstevel@tonic-gate 		n = (int)MIN(udf_vfsp->udf_lbsize - on, uio->uio_resid);
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate 		diff = ip->i_size - uoff;
30257c478bd9Sstevel@tonic-gate 
30267c478bd9Sstevel@tonic-gate 		if (diff <= (offset_t)0) {
30277c478bd9Sstevel@tonic-gate 			error = 0;
30287c478bd9Sstevel@tonic-gate 			goto out;
30297c478bd9Sstevel@tonic-gate 		}
30307c478bd9Sstevel@tonic-gate 		if (diff < (offset_t)n) {
30317c478bd9Sstevel@tonic-gate 			n = (int)diff;
30327c478bd9Sstevel@tonic-gate 		}
30337c478bd9Sstevel@tonic-gate 		dofree = ud_freebehind &&
30346f5f1c63SDonghai Qiao 		    ip->i_nextr == (off & PAGEMASK) &&
30356f5f1c63SDonghai Qiao 		    off > ud_smallfile;
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate #ifndef	__lock_lint
30387c478bd9Sstevel@tonic-gate 		if (rwtype == RW_READER) {
30397c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
30407c478bd9Sstevel@tonic-gate 		}
30417c478bd9Sstevel@tonic-gate #endif
30427c478bd9Sstevel@tonic-gate 
30437c478bd9Sstevel@tonic-gate 		base = segmap_getmapflt(segkmap, vp, (off + mapon),
30446f5f1c63SDonghai Qiao 		    (uint32_t)n, 1, S_READ);
30457c478bd9Sstevel@tonic-gate 		error = uiomove(base + mapon, (long)n, UIO_READ, uio);
30467c478bd9Sstevel@tonic-gate 
30477c478bd9Sstevel@tonic-gate 		flags = 0;
30487c478bd9Sstevel@tonic-gate 		if (!error) {
30497c478bd9Sstevel@tonic-gate 			/*
30507c478bd9Sstevel@tonic-gate 			 * If read a whole block, or read to eof,
30517c478bd9Sstevel@tonic-gate 			 * won't need this buffer again soon.
30527c478bd9Sstevel@tonic-gate 			 */
30537c478bd9Sstevel@tonic-gate 			if (n + on == MAXBSIZE && ud_freebehind && dofree &&
30546f5f1c63SDonghai Qiao 			    freemem < lotsfree + pages_before_pager) {
30557c478bd9Sstevel@tonic-gate 				flags = SM_FREE | SM_DONTNEED |SM_ASYNC;
30567c478bd9Sstevel@tonic-gate 			}
30577c478bd9Sstevel@tonic-gate 			/*
30587c478bd9Sstevel@tonic-gate 			 * In POSIX SYNC (FSYNC and FDSYNC) read mode,
30597c478bd9Sstevel@tonic-gate 			 * we want to make sure that the page which has
30607c478bd9Sstevel@tonic-gate 			 * been read, is written on disk if it is dirty.
30617c478bd9Sstevel@tonic-gate 			 * And corresponding indirect blocks should also
30627c478bd9Sstevel@tonic-gate 			 * be flushed out.
30637c478bd9Sstevel@tonic-gate 			 */
30647c478bd9Sstevel@tonic-gate 			if ((ioflag & FRSYNC) && (ioflag & (FSYNC|FDSYNC))) {
30657c478bd9Sstevel@tonic-gate 				flags &= ~SM_ASYNC;
30667c478bd9Sstevel@tonic-gate 				flags |= SM_WRITE;
30677c478bd9Sstevel@tonic-gate 			}
30687c478bd9Sstevel@tonic-gate 			error = segmap_release(segkmap, base, flags);
30697c478bd9Sstevel@tonic-gate 		} else    {
30707c478bd9Sstevel@tonic-gate 			(void) segmap_release(segkmap, base, flags);
30717c478bd9Sstevel@tonic-gate 		}
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate #ifndef __lock_lint
30747c478bd9Sstevel@tonic-gate 		if (rwtype == RW_READER) {
30757c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, rwtype);
30767c478bd9Sstevel@tonic-gate 		}
30777c478bd9Sstevel@tonic-gate #endif
30787c478bd9Sstevel@tonic-gate 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
30797c478bd9Sstevel@tonic-gate out:
30807c478bd9Sstevel@tonic-gate 	/*
30817c478bd9Sstevel@tonic-gate 	 * Inode is updated according to this table if FRSYNC is set.
30827c478bd9Sstevel@tonic-gate 	 *
30837c478bd9Sstevel@tonic-gate 	 *	FSYNC	FDSYNC(posix.4)
30847c478bd9Sstevel@tonic-gate 	 *	--------------------------
30857c478bd9Sstevel@tonic-gate 	 *	always	IATTCHG|IBDWRITE
30867c478bd9Sstevel@tonic-gate 	 */
30877c478bd9Sstevel@tonic-gate 	if (ioflag & FRSYNC) {
30887c478bd9Sstevel@tonic-gate 		if ((ioflag & FSYNC) ||
30896f5f1c63SDonghai Qiao 		    ((ioflag & FDSYNC) &&
30906f5f1c63SDonghai Qiao 		    (ip->i_flag & (IATTCHG|IBDWRITE)))) {
30917c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
30927c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
30937c478bd9Sstevel@tonic-gate 		ud_iupdat(ip, 1);
30947c478bd9Sstevel@tonic-gate 		}
30957c478bd9Sstevel@tonic-gate 	}
30967c478bd9Sstevel@tonic-gate 	/*
30977c478bd9Sstevel@tonic-gate 	 * If we've already done a partial read, terminate
30987c478bd9Sstevel@tonic-gate 	 * the read but return no error.
30997c478bd9Sstevel@tonic-gate 	 */
31007c478bd9Sstevel@tonic-gate 	if (oresid != uio->uio_resid) {
31017c478bd9Sstevel@tonic-gate 		error = 0;
31027c478bd9Sstevel@tonic-gate 	}
31037c478bd9Sstevel@tonic-gate 	ITIMES(ip);
31047c478bd9Sstevel@tonic-gate 
31057c478bd9Sstevel@tonic-gate 	return (error);
31067c478bd9Sstevel@tonic-gate }
31077c478bd9Sstevel@tonic-gate 
31087c478bd9Sstevel@tonic-gate int32_t
ud_wrip(struct ud_inode * ip,struct uio * uio,int ioflag,struct cred * cr)31097c478bd9Sstevel@tonic-gate ud_wrip(struct ud_inode *ip, struct uio *uio, int ioflag, struct cred *cr)
31107c478bd9Sstevel@tonic-gate {
31117c478bd9Sstevel@tonic-gate 	caddr_t base;
31127c478bd9Sstevel@tonic-gate 	struct vnode *vp;
31137c478bd9Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
31147c478bd9Sstevel@tonic-gate 	uint32_t flags;
31157c478bd9Sstevel@tonic-gate 	int32_t error = 0, iupdat_flag, n, on, mapon, i_size_changed = 0;
31167c478bd9Sstevel@tonic-gate 	int32_t pagecreate, newpage;
31177c478bd9Sstevel@tonic-gate 	uint64_t old_i_size;
31187c478bd9Sstevel@tonic-gate 	u_offset_t off;
31197c478bd9Sstevel@tonic-gate 	long start_resid = uio->uio_resid, premove_resid;
31207c478bd9Sstevel@tonic-gate 	rlim64_t limit = uio->uio_limit;
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate 
31237c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
31247c478bd9Sstevel@tonic-gate 	if ((ip->i_type != VREG) &&
31256f5f1c63SDonghai Qiao 	    (ip->i_type != VDIR) &&
31266f5f1c63SDonghai Qiao 	    (ip->i_type != VLNK)) {
31277c478bd9Sstevel@tonic-gate 		return (EIO);
31287c478bd9Sstevel@tonic-gate 	}
31297c478bd9Sstevel@tonic-gate 
31307c478bd9Sstevel@tonic-gate 	if (uio->uio_loffset >= MAXOFFSET_T) {
31317c478bd9Sstevel@tonic-gate 		return (EFBIG);
31327c478bd9Sstevel@tonic-gate 	}
31337c478bd9Sstevel@tonic-gate 	/*
31347c478bd9Sstevel@tonic-gate 	 * see udf_l_pathconf
31357c478bd9Sstevel@tonic-gate 	 */
31367c478bd9Sstevel@tonic-gate 	if (limit > (((uint64_t)1 << 40) - 1)) {
31377c478bd9Sstevel@tonic-gate 		limit = ((uint64_t)1 << 40) - 1;
31387c478bd9Sstevel@tonic-gate 	}
31397c478bd9Sstevel@tonic-gate 	if (uio->uio_loffset >= limit) {
31407c478bd9Sstevel@tonic-gate 		proc_t *p = ttoproc(curthread);
31417c478bd9Sstevel@tonic-gate 
31427c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
31437c478bd9Sstevel@tonic-gate 		(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
31447c478bd9Sstevel@tonic-gate 		    p, RCA_UNSAFE_SIGINFO);
31457c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
31467c478bd9Sstevel@tonic-gate 		return (EFBIG);
31477c478bd9Sstevel@tonic-gate 	}
31487c478bd9Sstevel@tonic-gate 	if ((uio->uio_loffset < (offset_t)0) ||
31496f5f1c63SDonghai Qiao 	    ((uio->uio_loffset + uio->uio_resid) < 0)) {
31507c478bd9Sstevel@tonic-gate 		return (EINVAL);
31517c478bd9Sstevel@tonic-gate 	}
31527c478bd9Sstevel@tonic-gate 	if (uio->uio_resid == 0) {
31537c478bd9Sstevel@tonic-gate 		return (0);
31547c478bd9Sstevel@tonic-gate 	}
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
31577c478bd9Sstevel@tonic-gate 	ip->i_flag |= INOACC;
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate 	if (ioflag & (FSYNC | FDSYNC)) {
31607c478bd9Sstevel@tonic-gate 		ip->i_flag |= ISYNC;
31617c478bd9Sstevel@tonic-gate 		iupdat_flag = 1;
31627c478bd9Sstevel@tonic-gate 	}
31637c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate 	udf_vfsp = ip->i_udf;
31667c478bd9Sstevel@tonic-gate 	vp = ITOV(ip);
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate 	do {
31697c478bd9Sstevel@tonic-gate 		u_offset_t uoff = uio->uio_loffset;
31707c478bd9Sstevel@tonic-gate 		off = uoff & (offset_t)MAXBMASK;
31717c478bd9Sstevel@tonic-gate 		mapon = (int)(uoff & (offset_t)MAXBOFFSET);
31727c478bd9Sstevel@tonic-gate 		on = (int)blkoff(udf_vfsp, uoff);
31737c478bd9Sstevel@tonic-gate 		n = (int)MIN(udf_vfsp->udf_lbsize - on, uio->uio_resid);
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate 		if (ip->i_type == VREG && uoff + n >= limit) {
31767c478bd9Sstevel@tonic-gate 			if (uoff >= limit) {
31777c478bd9Sstevel@tonic-gate 				error = EFBIG;
31787c478bd9Sstevel@tonic-gate 				goto out;
31797c478bd9Sstevel@tonic-gate 			}
31807c478bd9Sstevel@tonic-gate 			n = (int)(limit - (rlim64_t)uoff);
31817c478bd9Sstevel@tonic-gate 		}
31827c478bd9Sstevel@tonic-gate 		if (uoff + n > ip->i_size) {
31837c478bd9Sstevel@tonic-gate 			/*
31847c478bd9Sstevel@tonic-gate 			 * We are extending the length of the file.
31857c478bd9Sstevel@tonic-gate 			 * bmap is used so that we are sure that
31867c478bd9Sstevel@tonic-gate 			 * if we need to allocate new blocks, that it
31877c478bd9Sstevel@tonic-gate 			 * is done here before we up the file size.
31887c478bd9Sstevel@tonic-gate 			 */
31897c478bd9Sstevel@tonic-gate 			error = ud_bmap_write(ip, uoff,
31906f5f1c63SDonghai Qiao 			    (int)(on + n), mapon == 0, cr);
31917c478bd9Sstevel@tonic-gate 			if (error) {
31927c478bd9Sstevel@tonic-gate 				break;
31937c478bd9Sstevel@tonic-gate 			}
31947c478bd9Sstevel@tonic-gate 			i_size_changed = 1;
31957c478bd9Sstevel@tonic-gate 			old_i_size = ip->i_size;
31967c478bd9Sstevel@tonic-gate 			ip->i_size = uoff + n;
31977c478bd9Sstevel@tonic-gate 			/*
31987c478bd9Sstevel@tonic-gate 			 * If we are writing from the beginning of
31997c478bd9Sstevel@tonic-gate 			 * the mapping, we can just create the
32007c478bd9Sstevel@tonic-gate 			 * pages without having to read them.
32017c478bd9Sstevel@tonic-gate 			 */
32027c478bd9Sstevel@tonic-gate 			pagecreate = (mapon == 0);
32037c478bd9Sstevel@tonic-gate 		} else if (n == MAXBSIZE) {
32047c478bd9Sstevel@tonic-gate 			/*
32057c478bd9Sstevel@tonic-gate 			 * Going to do a whole mappings worth,
32067c478bd9Sstevel@tonic-gate 			 * so we can just create the pages w/o
32077c478bd9Sstevel@tonic-gate 			 * having to read them in.  But before
32087c478bd9Sstevel@tonic-gate 			 * we do that, we need to make sure any
32097c478bd9Sstevel@tonic-gate 			 * needed blocks are allocated first.
32107c478bd9Sstevel@tonic-gate 			 */
32117c478bd9Sstevel@tonic-gate 			error = ud_bmap_write(ip, uoff,
32126f5f1c63SDonghai Qiao 			    (int)(on + n), 1, cr);
32137c478bd9Sstevel@tonic-gate 			if (error) {
32147c478bd9Sstevel@tonic-gate 				break;
32157c478bd9Sstevel@tonic-gate 			}
32167c478bd9Sstevel@tonic-gate 			pagecreate = 1;
32177c478bd9Sstevel@tonic-gate 		} else {
32187c478bd9Sstevel@tonic-gate 			pagecreate = 0;
32197c478bd9Sstevel@tonic-gate 		}
32207c478bd9Sstevel@tonic-gate 
32217c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
32227c478bd9Sstevel@tonic-gate 
32236f5f1c63SDonghai Qiao 		/*
32246f5f1c63SDonghai Qiao 		 * Touch the page and fault it in if it is not in
32256f5f1c63SDonghai Qiao 		 * core before segmap_getmapflt can lock it. This
32266f5f1c63SDonghai Qiao 		 * is to avoid the deadlock if the buffer is mapped
32276f5f1c63SDonghai Qiao 		 * to the same file through mmap which we want to
32286f5f1c63SDonghai Qiao 		 * write to.
32296f5f1c63SDonghai Qiao 		 */
32306f5f1c63SDonghai Qiao 		uio_prefaultpages((long)n, uio);
32316f5f1c63SDonghai Qiao 
32327c478bd9Sstevel@tonic-gate 		base = segmap_getmapflt(segkmap, vp, (off + mapon),
32336f5f1c63SDonghai Qiao 		    (uint32_t)n, !pagecreate, S_WRITE);
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate 		/*
32367c478bd9Sstevel@tonic-gate 		 * segmap_pagecreate() returns 1 if it calls
32377c478bd9Sstevel@tonic-gate 		 * page_create_va() to allocate any pages.
32387c478bd9Sstevel@tonic-gate 		 */
32397c478bd9Sstevel@tonic-gate 		newpage = 0;
32407c478bd9Sstevel@tonic-gate 		if (pagecreate) {
32417c478bd9Sstevel@tonic-gate 			newpage = segmap_pagecreate(segkmap, base,
32426f5f1c63SDonghai Qiao 			    (size_t)n, 0);
32437c478bd9Sstevel@tonic-gate 		}
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate 		premove_resid = uio->uio_resid;
32467c478bd9Sstevel@tonic-gate 		error = uiomove(base + mapon, (long)n, UIO_WRITE, uio);
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 		if (pagecreate &&
32496f5f1c63SDonghai Qiao 		    uio->uio_loffset < roundup(off + mapon + n, PAGESIZE)) {
32507c478bd9Sstevel@tonic-gate 			/*
32517c478bd9Sstevel@tonic-gate 			 * We created pages w/o initializing them completely,
32527c478bd9Sstevel@tonic-gate 			 * thus we need to zero the part that wasn't set up.
32537c478bd9Sstevel@tonic-gate 			 * This happens on most EOF write cases and if
32547c478bd9Sstevel@tonic-gate 			 * we had some sort of error during the uiomove.
32557c478bd9Sstevel@tonic-gate 			 */
32567c478bd9Sstevel@tonic-gate 			int nzero, nmoved;
32577c478bd9Sstevel@tonic-gate 
32587c478bd9Sstevel@tonic-gate 			nmoved = (int)(uio->uio_loffset - (off + mapon));
32597c478bd9Sstevel@tonic-gate 			ASSERT(nmoved >= 0 && nmoved <= n);
32607c478bd9Sstevel@tonic-gate 			nzero = roundup(on + n, PAGESIZE) - nmoved;
32617c478bd9Sstevel@tonic-gate 			ASSERT(nzero > 0 && mapon + nmoved + nzero <= MAXBSIZE);
32627c478bd9Sstevel@tonic-gate 			(void) kzero(base + mapon + nmoved, (uint32_t)nzero);
32637c478bd9Sstevel@tonic-gate 		}
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate 		/*
32667c478bd9Sstevel@tonic-gate 		 * Unlock the pages allocated by page_create_va()
32677c478bd9Sstevel@tonic-gate 		 * in segmap_pagecreate()
32687c478bd9Sstevel@tonic-gate 		 */
32697c478bd9Sstevel@tonic-gate 		if (newpage) {
32707c478bd9Sstevel@tonic-gate 			segmap_pageunlock(segkmap, base, (size_t)n, S_WRITE);
32717c478bd9Sstevel@tonic-gate 		}
32727c478bd9Sstevel@tonic-gate 
32737c478bd9Sstevel@tonic-gate 		if (error) {
32747c478bd9Sstevel@tonic-gate 			/*
32757c478bd9Sstevel@tonic-gate 			 * If we failed on a write, we may have already
32767c478bd9Sstevel@tonic-gate 			 * allocated file blocks as well as pages.  It's
32777c478bd9Sstevel@tonic-gate 			 * hard to undo the block allocation, but we must
32787c478bd9Sstevel@tonic-gate 			 * be sure to invalidate any pages that may have
32797c478bd9Sstevel@tonic-gate 			 * been allocated.
32807c478bd9Sstevel@tonic-gate 			 */
32817c478bd9Sstevel@tonic-gate 			(void) segmap_release(segkmap, base, SM_INVAL);
32827c478bd9Sstevel@tonic-gate 		} else {
32837c478bd9Sstevel@tonic-gate 			flags = 0;
32847c478bd9Sstevel@tonic-gate 			/*
32857c478bd9Sstevel@tonic-gate 			 * Force write back for synchronous write cases.
32867c478bd9Sstevel@tonic-gate 			 */
32877c478bd9Sstevel@tonic-gate 			if ((ioflag & (FSYNC|FDSYNC)) || ip->i_type == VDIR) {
32887c478bd9Sstevel@tonic-gate 				/*
32897c478bd9Sstevel@tonic-gate 				 * If the sticky bit is set but the
32907c478bd9Sstevel@tonic-gate 				 * execute bit is not set, we do a
32917c478bd9Sstevel@tonic-gate 				 * synchronous write back and free
32927c478bd9Sstevel@tonic-gate 				 * the page when done.  We set up swap
32937c478bd9Sstevel@tonic-gate 				 * files to be handled this way to
32947c478bd9Sstevel@tonic-gate 				 * prevent servers from keeping around
32957c478bd9Sstevel@tonic-gate 				 * the client's swap pages too long.
32967c478bd9Sstevel@tonic-gate 				 * XXX - there ought to be a better way.
32977c478bd9Sstevel@tonic-gate 				 */
32987c478bd9Sstevel@tonic-gate 				if (IS_SWAPVP(vp)) {
32997c478bd9Sstevel@tonic-gate 					flags = SM_WRITE | SM_FREE |
33006f5f1c63SDonghai Qiao 					    SM_DONTNEED;
33017c478bd9Sstevel@tonic-gate 					iupdat_flag = 0;
33027c478bd9Sstevel@tonic-gate 				} else {
33037c478bd9Sstevel@tonic-gate 					flags = SM_WRITE;
33047c478bd9Sstevel@tonic-gate 				}
33057c478bd9Sstevel@tonic-gate 			} else if (((mapon + n) == MAXBSIZE) ||
33066f5f1c63SDonghai Qiao 			    IS_SWAPVP(vp)) {
33077c478bd9Sstevel@tonic-gate 				/*
33087c478bd9Sstevel@tonic-gate 				 * Have written a whole block.
33097c478bd9Sstevel@tonic-gate 				 * Start an asynchronous write and
33107c478bd9Sstevel@tonic-gate 				 * mark the buffer to indicate that
33117c478bd9Sstevel@tonic-gate 				 * it won't be needed again soon.
33127c478bd9Sstevel@tonic-gate 				 */
33137c478bd9Sstevel@tonic-gate 				flags = SM_WRITE |SM_ASYNC | SM_DONTNEED;
33147c478bd9Sstevel@tonic-gate 			}
33157c478bd9Sstevel@tonic-gate 			error = segmap_release(segkmap, base, flags);
33167c478bd9Sstevel@tonic-gate 
33177c478bd9Sstevel@tonic-gate 			/*
33187c478bd9Sstevel@tonic-gate 			 * If the operation failed and is synchronous,
33197c478bd9Sstevel@tonic-gate 			 * then we need to unwind what uiomove() last
33207c478bd9Sstevel@tonic-gate 			 * did so we can potentially return an error to
33217c478bd9Sstevel@tonic-gate 			 * the caller.  If this write operation was
33227c478bd9Sstevel@tonic-gate 			 * done in two pieces and the first succeeded,
33237c478bd9Sstevel@tonic-gate 			 * then we won't return an error for the second
33247c478bd9Sstevel@tonic-gate 			 * piece that failed.  However, we only want to
33257c478bd9Sstevel@tonic-gate 			 * return a resid value that reflects what was
33267c478bd9Sstevel@tonic-gate 			 * really done.
33277c478bd9Sstevel@tonic-gate 			 *
33287c478bd9Sstevel@tonic-gate 			 * Failures for non-synchronous operations can
33297c478bd9Sstevel@tonic-gate 			 * be ignored since the page subsystem will
33307c478bd9Sstevel@tonic-gate 			 * retry the operation until it succeeds or the
33317c478bd9Sstevel@tonic-gate 			 * file system is unmounted.
33327c478bd9Sstevel@tonic-gate 			 */
33337c478bd9Sstevel@tonic-gate 			if (error) {
33347c478bd9Sstevel@tonic-gate 				if ((ioflag & (FSYNC | FDSYNC)) ||
33356f5f1c63SDonghai Qiao 				    ip->i_type == VDIR) {
33367c478bd9Sstevel@tonic-gate 					uio->uio_resid = premove_resid;
33377c478bd9Sstevel@tonic-gate 				} else {
33387c478bd9Sstevel@tonic-gate 					error = 0;
33397c478bd9Sstevel@tonic-gate 				}
33407c478bd9Sstevel@tonic-gate 			}
33417c478bd9Sstevel@tonic-gate 		}
33427c478bd9Sstevel@tonic-gate 
33437c478bd9Sstevel@tonic-gate 		/*
33447c478bd9Sstevel@tonic-gate 		 * Re-acquire contents lock.
33457c478bd9Sstevel@tonic-gate 		 */
33467c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
33477c478bd9Sstevel@tonic-gate 		/*
33487c478bd9Sstevel@tonic-gate 		 * If the uiomove() failed or if a synchronous
33497c478bd9Sstevel@tonic-gate 		 * page push failed, fix up i_size.
33507c478bd9Sstevel@tonic-gate 		 */
33517c478bd9Sstevel@tonic-gate 		if (error) {
33527c478bd9Sstevel@tonic-gate 			if (i_size_changed) {
33537c478bd9Sstevel@tonic-gate 				/*
33547c478bd9Sstevel@tonic-gate 				 * The uiomove failed, and we
33557c478bd9Sstevel@tonic-gate 				 * allocated blocks,so get rid
33567c478bd9Sstevel@tonic-gate 				 * of them.
33577c478bd9Sstevel@tonic-gate 				 */
33587c478bd9Sstevel@tonic-gate 				(void) ud_itrunc(ip, old_i_size, 0, cr);
33597c478bd9Sstevel@tonic-gate 			}
33607c478bd9Sstevel@tonic-gate 		} else {
33617c478bd9Sstevel@tonic-gate 			/*
33627c478bd9Sstevel@tonic-gate 			 * XXX - Can this be out of the loop?
33637c478bd9Sstevel@tonic-gate 			 */
33647c478bd9Sstevel@tonic-gate 			ip->i_flag |= IUPD | ICHG;
33657c478bd9Sstevel@tonic-gate 			if (i_size_changed) {
33667c478bd9Sstevel@tonic-gate 				ip->i_flag |= IATTCHG;
33677c478bd9Sstevel@tonic-gate 			}
33687c478bd9Sstevel@tonic-gate 			if ((ip->i_perm & (IEXEC | (IEXEC >> 5) |
33696f5f1c63SDonghai Qiao 			    (IEXEC >> 10))) != 0 &&
33707c478bd9Sstevel@tonic-gate 			    (ip->i_char & (ISUID | ISGID)) != 0 &&
33717c478bd9Sstevel@tonic-gate 			    secpolicy_vnode_setid_retain(cr,
33727c478bd9Sstevel@tonic-gate 			    (ip->i_char & ISUID) != 0 && ip->i_uid == 0) != 0) {
33737c478bd9Sstevel@tonic-gate 				/*
33747c478bd9Sstevel@tonic-gate 				 * Clear Set-UID & Set-GID bits on
33757c478bd9Sstevel@tonic-gate 				 * successful write if not privileged
33767c478bd9Sstevel@tonic-gate 				 * and at least one of the execute bits
33777c478bd9Sstevel@tonic-gate 				 * is set.  If we always clear Set-GID,
33787c478bd9Sstevel@tonic-gate 				 * mandatory file and record locking is
33797c478bd9Sstevel@tonic-gate 				 * unuseable.
33807c478bd9Sstevel@tonic-gate 				 */
33817c478bd9Sstevel@tonic-gate 				ip->i_char &= ~(ISUID | ISGID);
33827c478bd9Sstevel@tonic-gate 			}
33837c478bd9Sstevel@tonic-gate 		}
33847c478bd9Sstevel@tonic-gate 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate out:
33877c478bd9Sstevel@tonic-gate 	/*
33887c478bd9Sstevel@tonic-gate 	 * Inode is updated according to this table -
33897c478bd9Sstevel@tonic-gate 	 *
33907c478bd9Sstevel@tonic-gate 	 *	FSYNC	FDSYNC(posix.4)
33917c478bd9Sstevel@tonic-gate 	 *	--------------------------
33927c478bd9Sstevel@tonic-gate 	 *	always@	IATTCHG|IBDWRITE
33937c478bd9Sstevel@tonic-gate 	 *
33947c478bd9Sstevel@tonic-gate 	 * @ -  If we are doing synchronous write the only time we should
33957c478bd9Sstevel@tonic-gate 	 *	not be sync'ing the ip here is if we have the stickyhack
33967c478bd9Sstevel@tonic-gate 	 *	activated, the file is marked with the sticky bit and
33977c478bd9Sstevel@tonic-gate 	 *	no exec bit, the file length has not been changed and
33987c478bd9Sstevel@tonic-gate 	 *	no new blocks have been allocated during this write.
33997c478bd9Sstevel@tonic-gate 	 */
34007c478bd9Sstevel@tonic-gate 	if ((ip->i_flag & ISYNC) != 0) {
34017c478bd9Sstevel@tonic-gate 		/*
34027c478bd9Sstevel@tonic-gate 		 * we have eliminated nosync
34037c478bd9Sstevel@tonic-gate 		 */
34047c478bd9Sstevel@tonic-gate 		if ((ip->i_flag & (IATTCHG|IBDWRITE)) ||
34056f5f1c63SDonghai Qiao 		    ((ioflag & FSYNC) && iupdat_flag)) {
34067c478bd9Sstevel@tonic-gate 			ud_iupdat(ip, 1);
34077c478bd9Sstevel@tonic-gate 		}
34087c478bd9Sstevel@tonic-gate 	}
34097c478bd9Sstevel@tonic-gate 
34107c478bd9Sstevel@tonic-gate 	/*
34117c478bd9Sstevel@tonic-gate 	 * If we've already done a partial-write, terminate
34127c478bd9Sstevel@tonic-gate 	 * the write but return no error.
34137c478bd9Sstevel@tonic-gate 	 */
34147c478bd9Sstevel@tonic-gate 	if (start_resid != uio->uio_resid) {
34157c478bd9Sstevel@tonic-gate 		error = 0;
34167c478bd9Sstevel@tonic-gate 	}
34177c478bd9Sstevel@tonic-gate 	ip->i_flag &= ~(INOACC | ISYNC);
34187c478bd9Sstevel@tonic-gate 	ITIMES_NOLOCK(ip);
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate 	return (error);
34217c478bd9Sstevel@tonic-gate }
34227c478bd9Sstevel@tonic-gate 
34237c478bd9Sstevel@tonic-gate int32_t
ud_multi_strat(struct ud_inode * ip,page_t * pp,struct buf * bp,u_offset_t start)34247c478bd9Sstevel@tonic-gate ud_multi_strat(struct ud_inode *ip,
3425*06042525SToomas Soome     page_t *pp, struct buf *bp, u_offset_t start)
34267c478bd9Sstevel@tonic-gate {
34277c478bd9Sstevel@tonic-gate 	daddr_t bn;
34287c478bd9Sstevel@tonic-gate 	int32_t error = 0, io_count, contig, alloc_sz, i;
34297c478bd9Sstevel@tonic-gate 	uint32_t io_off;
34307c478bd9Sstevel@tonic-gate 	mio_master_t *mm = NULL;
34317c478bd9Sstevel@tonic-gate 	mio_slave_t *ms = NULL;
34327c478bd9Sstevel@tonic-gate 	struct buf *rbp;
34337c478bd9Sstevel@tonic-gate 
34347c478bd9Sstevel@tonic-gate 	ASSERT(!(start & PAGEOFFSET));
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	/*
34377c478bd9Sstevel@tonic-gate 	 * Figure out how many buffers to allocate
34387c478bd9Sstevel@tonic-gate 	 */
34397c478bd9Sstevel@tonic-gate 	io_count = 0;
34407c478bd9Sstevel@tonic-gate 	for (io_off = 0; io_off < bp->b_bcount; io_off += contig) {
34417c478bd9Sstevel@tonic-gate 		contig = 0;
34427c478bd9Sstevel@tonic-gate 		if (error = ud_bmap_read(ip, (u_offset_t)(start + io_off),
34436f5f1c63SDonghai Qiao 		    &bn, &contig)) {
34447c478bd9Sstevel@tonic-gate 			goto end;
34457c478bd9Sstevel@tonic-gate 		}
34467c478bd9Sstevel@tonic-gate 		if (contig == 0) {
34477c478bd9Sstevel@tonic-gate 			goto end;
34487c478bd9Sstevel@tonic-gate 		}
34497c478bd9Sstevel@tonic-gate 		contig = MIN(contig, PAGESIZE - io_off);
34507c478bd9Sstevel@tonic-gate 		if (bn != UDF_HOLE) {
34517c478bd9Sstevel@tonic-gate 			io_count ++;
34527c478bd9Sstevel@tonic-gate 		} else {
34537c478bd9Sstevel@tonic-gate 			/*
34547c478bd9Sstevel@tonic-gate 			 * HOLE
34557c478bd9Sstevel@tonic-gate 			 */
34567c478bd9Sstevel@tonic-gate 			if (bp->b_flags & B_READ) {
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate 				/*
34597c478bd9Sstevel@tonic-gate 				 * This is a hole and is read
34607c478bd9Sstevel@tonic-gate 				 * it should be filled with 0's
34617c478bd9Sstevel@tonic-gate 				 */
34627c478bd9Sstevel@tonic-gate 				pagezero(pp, io_off, contig);
34637c478bd9Sstevel@tonic-gate 			}
34647c478bd9Sstevel@tonic-gate 		}
34657c478bd9Sstevel@tonic-gate 	}
34667c478bd9Sstevel@tonic-gate 
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 	if (io_count != 0) {
34697c478bd9Sstevel@tonic-gate 
34707c478bd9Sstevel@tonic-gate 		/*
34717c478bd9Sstevel@tonic-gate 		 * Allocate memory for all the
34727c478bd9Sstevel@tonic-gate 		 * required number of buffers
34737c478bd9Sstevel@tonic-gate 		 */
34747c478bd9Sstevel@tonic-gate 		alloc_sz = sizeof (mio_master_t) +
34756f5f1c63SDonghai Qiao 		    (sizeof (mio_slave_t) * io_count);
34767c478bd9Sstevel@tonic-gate 		mm = (mio_master_t *)kmem_zalloc(alloc_sz, KM_SLEEP);
34777c478bd9Sstevel@tonic-gate 		if (mm == NULL) {
34787c478bd9Sstevel@tonic-gate 			error = ENOMEM;
34797c478bd9Sstevel@tonic-gate 			goto end;
34807c478bd9Sstevel@tonic-gate 		}
34817c478bd9Sstevel@tonic-gate 
34827c478bd9Sstevel@tonic-gate 		/*
34837c478bd9Sstevel@tonic-gate 		 * initialize master
34847c478bd9Sstevel@tonic-gate 		 */
34857c478bd9Sstevel@tonic-gate 		mutex_init(&mm->mm_mutex, NULL, MUTEX_DEFAULT, NULL);
34867c478bd9Sstevel@tonic-gate 		mm->mm_size = alloc_sz;
34877c478bd9Sstevel@tonic-gate 		mm->mm_bp = bp;
34887c478bd9Sstevel@tonic-gate 		mm->mm_resid = 0;
34897c478bd9Sstevel@tonic-gate 		mm->mm_error = 0;
34907c478bd9Sstevel@tonic-gate 		mm->mm_index = master_index++;
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate 		ms = (mio_slave_t *)(((caddr_t)mm) + sizeof (mio_master_t));
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate 		/*
34957c478bd9Sstevel@tonic-gate 		 * Initialize buffers
34967c478bd9Sstevel@tonic-gate 		 */
34977c478bd9Sstevel@tonic-gate 		io_count = 0;
34987c478bd9Sstevel@tonic-gate 		for (io_off = 0; io_off < bp->b_bcount; io_off += contig) {
34997c478bd9Sstevel@tonic-gate 			contig = 0;
35007c478bd9Sstevel@tonic-gate 			if (error = ud_bmap_read(ip,
35016f5f1c63SDonghai Qiao 			    (u_offset_t)(start + io_off),
35026f5f1c63SDonghai Qiao 			    &bn, &contig)) {
35037c478bd9Sstevel@tonic-gate 				goto end;
35047c478bd9Sstevel@tonic-gate 			}
35057c478bd9Sstevel@tonic-gate 			ASSERT(contig);
35067c478bd9Sstevel@tonic-gate 			if ((io_off + contig) > bp->b_bcount) {
35077c478bd9Sstevel@tonic-gate 				contig = bp->b_bcount - io_off;
35087c478bd9Sstevel@tonic-gate 			}
35097c478bd9Sstevel@tonic-gate 			if (bn != UDF_HOLE) {
35107c478bd9Sstevel@tonic-gate 				/*
35117c478bd9Sstevel@tonic-gate 				 * Clone the buffer
35127c478bd9Sstevel@tonic-gate 				 * and prepare to start I/O
35137c478bd9Sstevel@tonic-gate 				 */
35147c478bd9Sstevel@tonic-gate 				ms->ms_ptr = mm;
35157c478bd9Sstevel@tonic-gate 				bioinit(&ms->ms_buf);
35167c478bd9Sstevel@tonic-gate 				rbp = bioclone(bp, io_off, (size_t)contig,
35176f5f1c63SDonghai Qiao 				    bp->b_edev, bn, ud_slave_done,
35186f5f1c63SDonghai Qiao 				    &ms->ms_buf, KM_NOSLEEP);
35197c478bd9Sstevel@tonic-gate 				ASSERT(rbp == &ms->ms_buf);
35207c478bd9Sstevel@tonic-gate 				mm->mm_resid += contig;
35217c478bd9Sstevel@tonic-gate 				io_count++;
35227c478bd9Sstevel@tonic-gate 				ms ++;
35237c478bd9Sstevel@tonic-gate 			}
35247c478bd9Sstevel@tonic-gate 		}
35257c478bd9Sstevel@tonic-gate 
35267c478bd9Sstevel@tonic-gate 		/*
35277c478bd9Sstevel@tonic-gate 		 * Start I/O's
35287c478bd9Sstevel@tonic-gate 		 */
35297c478bd9Sstevel@tonic-gate 		ms = (mio_slave_t *)(((caddr_t)mm) + sizeof (mio_master_t));
35307c478bd9Sstevel@tonic-gate 		for (i = 0; i < io_count; i++) {
35317c478bd9Sstevel@tonic-gate 			(void) bdev_strategy(&ms->ms_buf);
35327c478bd9Sstevel@tonic-gate 			ms ++;
35337c478bd9Sstevel@tonic-gate 		}
35347c478bd9Sstevel@tonic-gate 	}
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate end:
35377c478bd9Sstevel@tonic-gate 	if (error != 0) {
35387c478bd9Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
35397c478bd9Sstevel@tonic-gate 		bp->b_error = error;
35407c478bd9Sstevel@tonic-gate 		if (mm != NULL) {
35417c478bd9Sstevel@tonic-gate 			mutex_destroy(&mm->mm_mutex);
35427c478bd9Sstevel@tonic-gate 			kmem_free(mm, mm->mm_size);
35437c478bd9Sstevel@tonic-gate 		}
35447c478bd9Sstevel@tonic-gate 	}
35457c478bd9Sstevel@tonic-gate 	return (error);
35467c478bd9Sstevel@tonic-gate }
35477c478bd9Sstevel@tonic-gate 
35487c478bd9Sstevel@tonic-gate int32_t
ud_slave_done(struct buf * bp)35497c478bd9Sstevel@tonic-gate ud_slave_done(struct buf *bp)
35507c478bd9Sstevel@tonic-gate {
35517c478bd9Sstevel@tonic-gate 	mio_master_t *mm;
35527c478bd9Sstevel@tonic-gate 	int32_t resid;
35537c478bd9Sstevel@tonic-gate 
35547c478bd9Sstevel@tonic-gate 	ASSERT(SEMA_HELD(&bp->b_sem));
35557c478bd9Sstevel@tonic-gate 	ASSERT((bp->b_flags & B_DONE) == 0);
35567c478bd9Sstevel@tonic-gate 
35577c478bd9Sstevel@tonic-gate 	mm = ((mio_slave_t *)bp)->ms_ptr;
35587c478bd9Sstevel@tonic-gate 
35597c478bd9Sstevel@tonic-gate 	/*
35607c478bd9Sstevel@tonic-gate 	 * Propagate error and byte count info from slave struct to
35617c478bd9Sstevel@tonic-gate 	 * the master struct
35627c478bd9Sstevel@tonic-gate 	 */
35637c478bd9Sstevel@tonic-gate 	mutex_enter(&mm->mm_mutex);
35647c478bd9Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
35657c478bd9Sstevel@tonic-gate 
35667c478bd9Sstevel@tonic-gate 		/*
35677c478bd9Sstevel@tonic-gate 		 * If multiple slave buffers get
35687c478bd9Sstevel@tonic-gate 		 * error we forget the old errors
35697c478bd9Sstevel@tonic-gate 		 * this is ok because we any way
35707c478bd9Sstevel@tonic-gate 		 * cannot return multiple errors
35717c478bd9Sstevel@tonic-gate 		 */
35727c478bd9Sstevel@tonic-gate 		mm->mm_error = bp->b_error;
35737c478bd9Sstevel@tonic-gate 	}
35747c478bd9Sstevel@tonic-gate 	mm->mm_resid -= bp->b_bcount;
35757c478bd9Sstevel@tonic-gate 	resid = mm->mm_resid;
35767c478bd9Sstevel@tonic-gate 	mutex_exit(&mm->mm_mutex);
35777c478bd9Sstevel@tonic-gate 
35787c478bd9Sstevel@tonic-gate 	/*
35797c478bd9Sstevel@tonic-gate 	 * free up the resources allocated to cloned buffers.
35807c478bd9Sstevel@tonic-gate 	 */
35817c478bd9Sstevel@tonic-gate 	bp_mapout(bp);
35827c478bd9Sstevel@tonic-gate 	biofini(bp);
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate 	if (resid == 0) {
35857c478bd9Sstevel@tonic-gate 
35867c478bd9Sstevel@tonic-gate 		/*
35877c478bd9Sstevel@tonic-gate 		 * This is the last I/O operation
35887c478bd9Sstevel@tonic-gate 		 * clean up and return the original buffer
35897c478bd9Sstevel@tonic-gate 		 */
35907c478bd9Sstevel@tonic-gate 		if (mm->mm_error) {
35917c478bd9Sstevel@tonic-gate 			mm->mm_bp->b_flags |= B_ERROR;
35927c478bd9Sstevel@tonic-gate 			mm->mm_bp->b_error = mm->mm_error;
35937c478bd9Sstevel@tonic-gate 		}
35947c478bd9Sstevel@tonic-gate 		biodone(mm->mm_bp);
35957c478bd9Sstevel@tonic-gate 		mutex_destroy(&mm->mm_mutex);
35967c478bd9Sstevel@tonic-gate 		kmem_free(mm, mm->mm_size);
35977c478bd9Sstevel@tonic-gate 	}
35987c478bd9Sstevel@tonic-gate 	return (0);
35997c478bd9Sstevel@tonic-gate }
3600