xref: /illumos-gate/usr/src/uts/common/fs/ufs/ufs_vnops.c (revision 87bfe94c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1984, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2018 Joyent, Inc.
25  * Copyright (c) 2016 by Delphix. All rights reserved.
26  */
27 
28 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved	*/
30 
31 /*
32  * Portions of this source code were derived from Berkeley 4.3 BSD
33  * under license from the Regents of the University of California.
34  */
35 
36 #include <sys/types.h>
37 #include <sys/t_lock.h>
38 #include <sys/ksynch.h>
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/systm.h>
42 #include <sys/sysmacros.h>
43 #include <sys/resource.h>
44 #include <sys/signal.h>
45 #include <sys/cred.h>
46 #include <sys/user.h>
47 #include <sys/buf.h>
48 #include <sys/vfs.h>
49 #include <sys/vfs_opreg.h>
50 #include <sys/vnode.h>
51 #include <sys/proc.h>
52 #include <sys/disp.h>
53 #include <sys/file.h>
54 #include <sys/fcntl.h>
55 #include <sys/flock.h>
56 #include <sys/atomic.h>
57 #include <sys/kmem.h>
58 #include <sys/uio.h>
59 #include <sys/dnlc.h>
60 #include <sys/conf.h>
61 #include <sys/mman.h>
62 #include <sys/pathname.h>
63 #include <sys/debug.h>
64 #include <sys/vmsystm.h>
65 #include <sys/cmn_err.h>
66 #include <sys/filio.h>
67 #include <sys/policy.h>
68 
69 #include <sys/fs/ufs_fs.h>
70 #include <sys/fs/ufs_lockfs.h>
71 #include <sys/fs/ufs_filio.h>
72 #include <sys/fs/ufs_inode.h>
73 #include <sys/fs/ufs_fsdir.h>
74 #include <sys/fs/ufs_quota.h>
75 #include <sys/fs/ufs_log.h>
76 #include <sys/fs/ufs_snap.h>
77 #include <sys/fs/ufs_trans.h>
78 #include <sys/fs/ufs_panic.h>
79 #include <sys/fs/ufs_bio.h>
80 #include <sys/dirent.h>		/* must be AFTER <sys/fs/fsdir.h>! */
81 #include <sys/errno.h>
82 #include <sys/fssnap_if.h>
83 #include <sys/unistd.h>
84 #include <sys/sunddi.h>
85 
86 #include <sys/filio.h>		/* _FIOIO */
87 
88 #include <vm/hat.h>
89 #include <vm/page.h>
90 #include <vm/pvn.h>
91 #include <vm/as.h>
92 #include <vm/seg.h>
93 #include <vm/seg_map.h>
94 #include <vm/seg_vn.h>
95 #include <vm/seg_kmem.h>
96 #include <vm/rm.h>
97 #include <sys/swap.h>
98 
99 #include <fs/fs_subr.h>
100 
101 #include <sys/fs/decomp.h>
102 
103 static struct instats ins;
104 
105 static	int ufs_getpage_ra(struct vnode *, u_offset_t, struct seg *, caddr_t);
106 static	int ufs_getpage_miss(struct vnode *, u_offset_t, size_t, struct seg *,
107 		caddr_t, struct page **, size_t, enum seg_rw, int);
108 static	int ufs_open(struct vnode **, int, struct cred *, caller_context_t *);
109 static	int ufs_close(struct vnode *, int, int, offset_t, struct cred *,
110 		caller_context_t *);
111 static	int ufs_read(struct vnode *, struct uio *, int, struct cred *,
112 		struct caller_context *);
113 static	int ufs_write(struct vnode *, struct uio *, int, struct cred *,
114 		struct caller_context *);
115 static	int ufs_ioctl(struct vnode *, int, intptr_t, int, struct cred *,
116 		int *, caller_context_t *);
117 static	int ufs_getattr(struct vnode *, struct vattr *, int, struct cred *,
118 		caller_context_t *);
119 static	int ufs_setattr(struct vnode *, struct vattr *, int, struct cred *,
120 		caller_context_t *);
121 static	int ufs_access(struct vnode *, int, int, struct cred *,
122 		caller_context_t *);
123 static	int ufs_lookup(struct vnode *, char *, struct vnode **,
124 		struct pathname *, int, struct vnode *, struct cred *,
125 		caller_context_t *, int *, pathname_t *);
126 static	int ufs_create(struct vnode *, char *, struct vattr *, enum vcexcl,
127 		int, struct vnode **, struct cred *, int,
128 		caller_context_t *, vsecattr_t  *);
129 static	int ufs_remove(struct vnode *, char *, struct cred *,
130 		caller_context_t *, int);
131 static	int ufs_link(struct vnode *, struct vnode *, char *, struct cred *,
132 		caller_context_t *, int);
133 static	int ufs_rename(struct vnode *, char *, struct vnode *, char *,
134 		struct cred *, caller_context_t *, int);
135 static	int ufs_mkdir(struct vnode *, char *, struct vattr *, struct vnode **,
136 		struct cred *, caller_context_t *, int, vsecattr_t *);
137 static	int ufs_rmdir(struct vnode *, char *, struct vnode *, struct cred *,
138 		caller_context_t *, int);
139 static	int ufs_readdir(struct vnode *, struct uio *, struct cred *, int *,
140 		caller_context_t *, int);
141 static	int ufs_symlink(struct vnode *, char *, struct vattr *, char *,
142 		struct cred *, caller_context_t *, int);
143 static	int ufs_readlink(struct vnode *, struct uio *, struct cred *,
144 		caller_context_t *);
145 static	int ufs_fsync(struct vnode *, int, struct cred *, caller_context_t *);
146 static	void ufs_inactive(struct vnode *, struct cred *, caller_context_t *);
147 static	int ufs_fid(struct vnode *, struct fid *, caller_context_t *);
148 static	int ufs_rwlock(struct vnode *, int, caller_context_t *);
149 static	void ufs_rwunlock(struct vnode *, int, caller_context_t *);
150 static	int ufs_seek(struct vnode *, offset_t, offset_t *, caller_context_t *);
151 static	int ufs_frlock(struct vnode *, int, struct flock64 *, int, offset_t,
152 		struct flk_callback *, struct cred *,
153 		caller_context_t *);
154 static  int ufs_space(struct vnode *, int, struct flock64 *, int, offset_t,
155 		cred_t *, caller_context_t *);
156 static	int ufs_getpage(struct vnode *, offset_t, size_t, uint_t *,
157 		struct page **, size_t, struct seg *, caddr_t,
158 		enum seg_rw, struct cred *, caller_context_t *);
159 static	int ufs_putpage(struct vnode *, offset_t, size_t, int, struct cred *,
160 		caller_context_t *);
161 static	int ufs_putpages(struct vnode *, offset_t, size_t, int, struct cred *);
162 static	int ufs_map(struct vnode *, offset_t, struct as *, caddr_t *, size_t,
163 		uchar_t, uchar_t, uint_t, struct cred *, caller_context_t *);
164 static	int ufs_addmap(struct vnode *, offset_t, struct as *, caddr_t,  size_t,
165 		uchar_t, uchar_t, uint_t, struct cred *, caller_context_t *);
166 static	int ufs_delmap(struct vnode *, offset_t, struct as *, caddr_t,  size_t,
167 		uint_t, uint_t, uint_t, struct cred *, caller_context_t *);
168 static	int ufs_poll(vnode_t *, short, int, short *, struct pollhead **,
169 		caller_context_t *);
170 static	int ufs_dump(vnode_t *, caddr_t, offset_t, offset_t,
171     caller_context_t *);
172 static	int ufs_l_pathconf(struct vnode *, int, ulong_t *, struct cred *,
173 		caller_context_t *);
174 static	int ufs_pageio(struct vnode *, struct page *, u_offset_t, size_t, int,
175 		struct cred *, caller_context_t *);
176 static	int ufs_dumpctl(vnode_t *, int, offset_t *, caller_context_t *);
177 static	daddr32_t *save_dblks(struct inode *, struct ufsvfs *, daddr32_t *,
178 		daddr32_t *, int, int);
179 static	int ufs_getsecattr(struct vnode *, vsecattr_t *, int, struct cred *,
180 		caller_context_t *);
181 static	int ufs_setsecattr(struct vnode *, vsecattr_t *, int, struct cred *,
182 		caller_context_t *);
183 static	int ufs_priv_access(void *, int, struct cred *);
184 static	int ufs_eventlookup(struct vnode *, char *, struct cred *,
185     struct vnode **);
186 
187 /*
188  * For lockfs: ulockfs begin/end is now inlined in the ufs_xxx functions.
189  *
190  * XXX - ULOCKFS in fs_pathconf and ufs_ioctl is not inlined yet.
191  */
192 struct vnodeops *ufs_vnodeops;
193 
194 /* NOTE: "not blkd" below  means that the operation isn't blocked by lockfs */
195 const fs_operation_def_t ufs_vnodeops_template[] = {
196 	VOPNAME_OPEN,		{ .vop_open = ufs_open },	/* not blkd */
197 	VOPNAME_CLOSE,		{ .vop_close = ufs_close },	/* not blkd */
198 	VOPNAME_READ,		{ .vop_read = ufs_read },
199 	VOPNAME_WRITE,		{ .vop_write = ufs_write },
200 	VOPNAME_IOCTL,		{ .vop_ioctl = ufs_ioctl },
201 	VOPNAME_GETATTR,	{ .vop_getattr = ufs_getattr },
202 	VOPNAME_SETATTR,	{ .vop_setattr = ufs_setattr },
203 	VOPNAME_ACCESS,		{ .vop_access = ufs_access },
204 	VOPNAME_LOOKUP,		{ .vop_lookup = ufs_lookup },
205 	VOPNAME_CREATE,		{ .vop_create = ufs_create },
206 	VOPNAME_REMOVE,		{ .vop_remove = ufs_remove },
207 	VOPNAME_LINK,		{ .vop_link = ufs_link },
208 	VOPNAME_RENAME,		{ .vop_rename = ufs_rename },
209 	VOPNAME_MKDIR,		{ .vop_mkdir = ufs_mkdir },
210 	VOPNAME_RMDIR,		{ .vop_rmdir = ufs_rmdir },
211 	VOPNAME_READDIR,	{ .vop_readdir = ufs_readdir },
212 	VOPNAME_SYMLINK,	{ .vop_symlink = ufs_symlink },
213 	VOPNAME_READLINK,	{ .vop_readlink = ufs_readlink },
214 	VOPNAME_FSYNC,		{ .vop_fsync = ufs_fsync },
215 	VOPNAME_INACTIVE,	{ .vop_inactive = ufs_inactive }, /* not blkd */
216 	VOPNAME_FID,		{ .vop_fid = ufs_fid },
217 	VOPNAME_RWLOCK,		{ .vop_rwlock = ufs_rwlock },	/* not blkd */
218 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = ufs_rwunlock }, /* not blkd */
219 	VOPNAME_SEEK,		{ .vop_seek = ufs_seek },
220 	VOPNAME_FRLOCK,		{ .vop_frlock = ufs_frlock },
221 	VOPNAME_SPACE,		{ .vop_space = ufs_space },
222 	VOPNAME_GETPAGE,	{ .vop_getpage = ufs_getpage },
223 	VOPNAME_PUTPAGE,	{ .vop_putpage = ufs_putpage },
224 	VOPNAME_MAP,		{ .vop_map = ufs_map },
225 	VOPNAME_ADDMAP,		{ .vop_addmap = ufs_addmap },	/* not blkd */
226 	VOPNAME_DELMAP,		{ .vop_delmap = ufs_delmap },	/* not blkd */
227 	VOPNAME_POLL,		{ .vop_poll = ufs_poll },	/* not blkd */
228 	VOPNAME_DUMP,		{ .vop_dump = ufs_dump },
229 	VOPNAME_PATHCONF,	{ .vop_pathconf = ufs_l_pathconf },
230 	VOPNAME_PAGEIO,		{ .vop_pageio = ufs_pageio },
231 	VOPNAME_DUMPCTL,	{ .vop_dumpctl = ufs_dumpctl },
232 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = ufs_getsecattr },
233 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = ufs_setsecattr },
234 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
235 	NULL,			NULL
236 };
237 
238 #define	MAX_BACKFILE_COUNT	9999
239 
240 /*
241  * Created by ufs_dumpctl() to store a file's disk block info into memory.
242  * Used by ufs_dump() to dump data to disk directly.
243  */
244 struct dump {
245 	struct inode	*ip;		/* the file we contain */
246 	daddr_t		fsbs;		/* number of blocks stored */
247 	struct timeval32 time;		/* time stamp for the struct */
248 	daddr32_t	dblk[1];	/* place holder for block info */
249 };
250 
251 static struct dump *dump_info = NULL;
252 
253 /*
254  * Previously there was no special action required for ordinary files.
255  * (Devices are handled through the device file system.)
256  * Now we support Large Files and Large File API requires open to
257  * fail if file is large.
258  * We could take care to prevent data corruption
259  * by doing an atomic check of size and truncate if file is opened with
260  * FTRUNC flag set but traditionally this is being done by the vfs/vnode
261  * layers. So taking care of truncation here is a change in the existing
262  * semantics of VOP_OPEN and therefore we chose not to implement any thing
263  * here. The check for the size of the file > 2GB is being done at the
264  * vfs layer in routine vn_open().
265  */
266 
267 /* ARGSUSED */
268 static int
ufs_open(struct vnode ** vpp,int flag,struct cred * cr,caller_context_t * ct)269 ufs_open(struct vnode **vpp, int flag, struct cred *cr, caller_context_t *ct)
270 {
271 	return (0);
272 }
273 
274 /*ARGSUSED*/
275 static int
ufs_close(struct vnode * vp,int flag,int count,offset_t offset,struct cred * cr,caller_context_t * ct)276 ufs_close(struct vnode *vp, int flag, int count, offset_t offset,
277     struct cred *cr, caller_context_t *ct)
278 {
279 	cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
280 	cleanshares(vp, ttoproc(curthread)->p_pid);
281 
282 	/*
283 	 * Push partially filled cluster at last close.
284 	 * ``last close'' is approximated because the dnlc
285 	 * may have a hold on the vnode.
286 	 * Checking for VBAD here will also act as a forced umount check.
287 	 */
288 	if (vp->v_count <= 2 && vp->v_type != VBAD) {
289 		struct inode *ip = VTOI(vp);
290 		if (ip->i_delaylen) {
291 			ins.in_poc.value.ul++;
292 			(void) ufs_putpages(vp, ip->i_delayoff, ip->i_delaylen,
293 			    B_ASYNC | B_FREE, cr);
294 			ip->i_delaylen = 0;
295 		}
296 	}
297 
298 	return (0);
299 }
300 
301 /*ARGSUSED*/
302 static int
ufs_read(struct vnode * vp,struct uio * uiop,int ioflag,struct cred * cr,struct caller_context * ct)303 ufs_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cr,
304     struct caller_context *ct)
305 {
306 	struct inode *ip = VTOI(vp);
307 	struct ufsvfs *ufsvfsp;
308 	struct ulockfs *ulp = NULL;
309 	int error = 0;
310 	int intrans = 0;
311 
312 	ASSERT(RW_READ_HELD(&ip->i_rwlock));
313 
314 	/*
315 	 * Mandatory locking needs to be done before ufs_lockfs_begin()
316 	 * and TRANS_BEGIN_SYNC() calls since mandatory locks can sleep.
317 	 */
318 	if (MANDLOCK(vp, ip->i_mode)) {
319 		/*
320 		 * ufs_getattr ends up being called by chklock
321 		 */
322 		error = chklock(vp, FREAD, uiop->uio_loffset,
323 		    uiop->uio_resid, uiop->uio_fmode, ct);
324 		if (error)
325 			goto out;
326 	}
327 
328 	ufsvfsp = ip->i_ufsvfs;
329 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READ_MASK);
330 	if (error)
331 		goto out;
332 
333 	/*
334 	 * In the case that a directory is opened for reading as a file
335 	 * (eg "cat .") with the  O_RSYNC, O_SYNC and O_DSYNC flags set.
336 	 * The locking order had to be changed to avoid a deadlock with
337 	 * an update taking place on that directory at the same time.
338 	 */
339 	if ((ip->i_mode & IFMT) == IFDIR) {
340 
341 		rw_enter(&ip->i_contents, RW_READER);
342 		error = rdip(ip, uiop, ioflag, cr);
343 		rw_exit(&ip->i_contents);
344 
345 		if (error) {
346 			if (ulp)
347 				ufs_lockfs_end(ulp);
348 			goto out;
349 		}
350 
351 		if (ulp && (ioflag & FRSYNC) && (ioflag & (FSYNC | FDSYNC)) &&
352 		    TRANS_ISTRANS(ufsvfsp)) {
353 			rw_exit(&ip->i_rwlock);
354 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_READ_SYNC, TOP_READ_SIZE,
355 			    error);
356 			ASSERT(!error);
357 			TRANS_END_SYNC(ufsvfsp, error, TOP_READ_SYNC,
358 			    TOP_READ_SIZE);
359 			rw_enter(&ip->i_rwlock, RW_READER);
360 		}
361 	} else {
362 		/*
363 		 * Only transact reads to files opened for sync-read and
364 		 * sync-write on a file system that is not write locked.
365 		 *
366 		 * The ``not write locked'' check prevents problems with
367 		 * enabling/disabling logging on a busy file system.  E.g.,
368 		 * logging exists at the beginning of the read but does not
369 		 * at the end.
370 		 *
371 		 */
372 		if (ulp && (ioflag & FRSYNC) && (ioflag & (FSYNC | FDSYNC)) &&
373 		    TRANS_ISTRANS(ufsvfsp)) {
374 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_READ_SYNC, TOP_READ_SIZE,
375 			    error);
376 			ASSERT(!error);
377 			intrans = 1;
378 		}
379 
380 		rw_enter(&ip->i_contents, RW_READER);
381 		error = rdip(ip, uiop, ioflag, cr);
382 		rw_exit(&ip->i_contents);
383 
384 		if (intrans) {
385 			TRANS_END_SYNC(ufsvfsp, error, TOP_READ_SYNC,
386 			    TOP_READ_SIZE);
387 		}
388 	}
389 
390 	if (ulp) {
391 		ufs_lockfs_end(ulp);
392 	}
393 out:
394 
395 	return (error);
396 }
397 
398 extern	int	ufs_HW;		/* high water mark */
399 extern	int	ufs_LW;		/* low water mark */
400 int	ufs_WRITES = 1;		/* XXX - enable/disable */
401 int	ufs_throttles = 0;	/* throttling count */
402 int	ufs_allow_shared_writes = 1;	/* directio shared writes */
403 
404 static int
ufs_check_rewrite(struct inode * ip,struct uio * uiop,int ioflag)405 ufs_check_rewrite(struct inode *ip, struct uio *uiop, int ioflag)
406 {
407 	int	shared_write;
408 
409 	/*
410 	 * If the FDSYNC flag is set then ignore the global
411 	 * ufs_allow_shared_writes in this case.
412 	 */
413 	shared_write = (ioflag & FDSYNC) | ufs_allow_shared_writes;
414 
415 	/*
416 	 * Filter to determine if this request is suitable as a
417 	 * concurrent rewrite. This write must not allocate blocks
418 	 * by extending the file or filling in holes. No use trying
419 	 * through FSYNC descriptors as the inode will be synchronously
420 	 * updated after the write. The uio structure has not yet been
421 	 * checked for sanity, so assume nothing.
422 	 */
423 	return (((ip->i_mode & IFMT) == IFREG) && !(ioflag & FAPPEND) &&
424 	    (uiop->uio_loffset >= (offset_t)0) &&
425 	    (uiop->uio_loffset < ip->i_size) && (uiop->uio_resid > 0) &&
426 	    ((ip->i_size - uiop->uio_loffset) >= uiop->uio_resid) &&
427 	    !(ioflag & FSYNC) && !bmap_has_holes(ip) &&
428 	    shared_write);
429 }
430 
431 /*ARGSUSED*/
432 static int
ufs_write(struct vnode * vp,struct uio * uiop,int ioflag,cred_t * cr,caller_context_t * ct)433 ufs_write(struct vnode *vp, struct uio *uiop, int ioflag, cred_t *cr,
434     caller_context_t *ct)
435 {
436 	struct inode *ip = VTOI(vp);
437 	struct ufsvfs *ufsvfsp;
438 	struct ulockfs *ulp;
439 	int retry = 1;
440 	int error, resv, resid = 0;
441 	int directio_status;
442 	int exclusive;
443 	int rewriteflg;
444 	long start_resid = uiop->uio_resid;
445 
446 	ASSERT(RW_LOCK_HELD(&ip->i_rwlock));
447 
448 retry_mandlock:
449 	/*
450 	 * Mandatory locking needs to be done before ufs_lockfs_begin()
451 	 * and TRANS_BEGIN_[A]SYNC() calls since mandatory locks can sleep.
452 	 * Check for forced unmounts normally done in ufs_lockfs_begin().
453 	 */
454 	if ((ufsvfsp = ip->i_ufsvfs) == NULL) {
455 		error = EIO;
456 		goto out;
457 	}
458 	if (MANDLOCK(vp, ip->i_mode)) {
459 
460 		ASSERT(RW_WRITE_HELD(&ip->i_rwlock));
461 
462 		/*
463 		 * ufs_getattr ends up being called by chklock
464 		 */
465 		error = chklock(vp, FWRITE, uiop->uio_loffset,
466 		    uiop->uio_resid, uiop->uio_fmode, ct);
467 		if (error)
468 			goto out;
469 	}
470 
471 	/* i_rwlock can change in chklock */
472 	exclusive = rw_write_held(&ip->i_rwlock);
473 	rewriteflg = ufs_check_rewrite(ip, uiop, ioflag);
474 
475 	/*
476 	 * Check for fast-path special case of directio re-writes.
477 	 */
478 	if ((ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) &&
479 	    !exclusive && rewriteflg) {
480 
481 		error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_WRITE_MASK);
482 		if (error)
483 			goto out;
484 
485 		rw_enter(&ip->i_contents, RW_READER);
486 		error = ufs_directio_write(ip, uiop, ioflag, 1, cr,
487 		    &directio_status);
488 		if (directio_status == DIRECTIO_SUCCESS) {
489 			uint_t i_flag_save;
490 
491 			if (start_resid != uiop->uio_resid)
492 				error = 0;
493 			/*
494 			 * Special treatment of access times for re-writes.
495 			 * If IMOD is not already set, then convert it
496 			 * to IMODACC for this operation. This defers
497 			 * entering a delta into the log until the inode
498 			 * is flushed. This mimics what is done for read
499 			 * operations and inode access time.
500 			 */
501 			mutex_enter(&ip->i_tlock);
502 			i_flag_save = ip->i_flag;
503 			ip->i_flag |= IUPD | ICHG;
504 			ip->i_seq++;
505 			ITIMES_NOLOCK(ip);
506 			if ((i_flag_save & IMOD) == 0) {
507 				ip->i_flag &= ~IMOD;
508 				ip->i_flag |= IMODACC;
509 			}
510 			mutex_exit(&ip->i_tlock);
511 			rw_exit(&ip->i_contents);
512 			if (ulp)
513 				ufs_lockfs_end(ulp);
514 			goto out;
515 		}
516 		rw_exit(&ip->i_contents);
517 		if (ulp)
518 			ufs_lockfs_end(ulp);
519 	}
520 
521 	if (!exclusive && !rw_tryupgrade(&ip->i_rwlock)) {
522 		rw_exit(&ip->i_rwlock);
523 		rw_enter(&ip->i_rwlock, RW_WRITER);
524 		/*
525 		 * Mandatory locking could have been enabled
526 		 * after dropping the i_rwlock.
527 		 */
528 		if (MANDLOCK(vp, ip->i_mode))
529 			goto retry_mandlock;
530 	}
531 
532 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_WRITE_MASK);
533 	if (error)
534 		goto out;
535 
536 	/*
537 	 * Amount of log space needed for this write
538 	 */
539 	if (!rewriteflg || !(ioflag & FDSYNC))
540 		TRANS_WRITE_RESV(ip, uiop, ulp, &resv, &resid);
541 
542 	/*
543 	 * Throttle writes.
544 	 */
545 	if (ufs_WRITES && (ip->i_writes > ufs_HW)) {
546 		mutex_enter(&ip->i_tlock);
547 		while (ip->i_writes > ufs_HW) {
548 			ufs_throttles++;
549 			cv_wait(&ip->i_wrcv, &ip->i_tlock);
550 		}
551 		mutex_exit(&ip->i_tlock);
552 	}
553 
554 	/*
555 	 * Enter Transaction
556 	 *
557 	 * If the write is a rewrite there is no need to open a transaction
558 	 * if the FDSYNC flag is set and not the FSYNC.  In this case just
559 	 * set the IMODACC flag to modify do the update at a later time
560 	 * thus avoiding the overhead of the logging transaction that is
561 	 * not required.
562 	 */
563 	if (ioflag & (FSYNC|FDSYNC)) {
564 		if (ulp) {
565 			if (rewriteflg) {
566 				uint_t i_flag_save;
567 
568 				rw_enter(&ip->i_contents, RW_READER);
569 				mutex_enter(&ip->i_tlock);
570 				i_flag_save = ip->i_flag;
571 				ip->i_flag |= IUPD | ICHG;
572 				ip->i_seq++;
573 				ITIMES_NOLOCK(ip);
574 				if ((i_flag_save & IMOD) == 0) {
575 					ip->i_flag &= ~IMOD;
576 					ip->i_flag |= IMODACC;
577 				}
578 				mutex_exit(&ip->i_tlock);
579 				rw_exit(&ip->i_contents);
580 			} else {
581 				int terr = 0;
582 				TRANS_BEGIN_SYNC(ufsvfsp, TOP_WRITE_SYNC, resv,
583 				    terr);
584 				ASSERT(!terr);
585 			}
586 		}
587 	} else {
588 		if (ulp)
589 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_WRITE, resv);
590 	}
591 
592 	/*
593 	 * Write the file
594 	 */
595 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
596 	rw_enter(&ip->i_contents, RW_WRITER);
597 	if ((ioflag & FAPPEND) != 0 && (ip->i_mode & IFMT) == IFREG) {
598 		/*
599 		 * In append mode start at end of file.
600 		 */
601 		uiop->uio_loffset = ip->i_size;
602 	}
603 
604 	/*
605 	 * Mild optimisation, don't call ufs_trans_write() unless we have to
606 	 * Also, suppress file system full messages if we will retry.
607 	 */
608 	if (retry)
609 		ip->i_flag |= IQUIET;
610 	if (resid) {
611 		TRANS_WRITE(ip, uiop, ioflag, error, ulp, cr, resv, resid);
612 	} else {
613 		error = wrip(ip, uiop, ioflag, cr);
614 	}
615 	ip->i_flag &= ~IQUIET;
616 
617 	rw_exit(&ip->i_contents);
618 	rw_exit(&ufsvfsp->vfs_dqrwlock);
619 
620 	/*
621 	 * Leave Transaction
622 	 */
623 	if (ulp) {
624 		if (ioflag & (FSYNC|FDSYNC)) {
625 			if (!rewriteflg) {
626 				int terr = 0;
627 
628 				TRANS_END_SYNC(ufsvfsp, terr, TOP_WRITE_SYNC,
629 				    resv);
630 				if (error == 0)
631 					error = terr;
632 			}
633 		} else {
634 			TRANS_END_ASYNC(ufsvfsp, TOP_WRITE, resv);
635 		}
636 		ufs_lockfs_end(ulp);
637 	}
638 out:
639 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
640 		/*
641 		 * Any blocks tied up in pending deletes?
642 		 */
643 		ufs_delete_drain_wait(ufsvfsp, 1);
644 		retry = 0;
645 		goto retry_mandlock;
646 	}
647 
648 	if (error == ENOSPC && (start_resid != uiop->uio_resid))
649 		error = 0;
650 
651 	return (error);
652 }
653 
654 /*
655  * Don't cache write blocks to files with the sticky bit set.
656  * Used to keep swap files from blowing the page cache on a server.
657  */
658 int stickyhack = 1;
659 
660 /*
661  * wrip does the real work of write requests for ufs.
662  */
663 int
wrip(struct inode * ip,struct uio * uio,int ioflag,struct cred * cr)664 wrip(struct inode *ip, struct uio *uio, int ioflag, struct cred *cr)
665 {
666 	rlim64_t limit = uio->uio_llimit;
667 	u_offset_t off;
668 	u_offset_t old_i_size;
669 	struct fs *fs;
670 	struct vnode *vp;
671 	struct ufsvfs *ufsvfsp;
672 	caddr_t base;
673 	long start_resid = uio->uio_resid;	/* save starting resid */
674 	long premove_resid;			/* resid before uiomove() */
675 	uint_t flags;
676 	int newpage;
677 	int iupdat_flag, directio_status;
678 	int n, on, mapon;
679 	int error, pagecreate;
680 	int do_dqrwlock;		/* drop/reacquire vfs_dqrwlock */
681 	int32_t	iblocks;
682 	int	new_iblocks;
683 
684 	/*
685 	 * ip->i_size is incremented before the uiomove
686 	 * is done on a write.  If the move fails (bad user
687 	 * address) reset ip->i_size.
688 	 * The better way would be to increment ip->i_size
689 	 * only if the uiomove succeeds.
690 	 */
691 	int i_size_changed = 0;
692 	o_mode_t type;
693 	int i_seq_needed = 0;
694 
695 	vp = ITOV(ip);
696 
697 	/*
698 	 * check for forced unmount - should not happen as
699 	 * the request passed the lockfs checks.
700 	 */
701 	if ((ufsvfsp = ip->i_ufsvfs) == NULL)
702 		return (EIO);
703 
704 	fs = ip->i_fs;
705 
706 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
707 
708 	/* check for valid filetype */
709 	type = ip->i_mode & IFMT;
710 	if ((type != IFREG) && (type != IFDIR) && (type != IFATTRDIR) &&
711 	    (type != IFLNK) && (type != IFSHAD)) {
712 		return (EIO);
713 	}
714 
715 	/*
716 	 * the actual limit of UFS file size
717 	 * is UFS_MAXOFFSET_T
718 	 */
719 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
720 		limit = MAXOFFSET_T;
721 
722 	if (uio->uio_loffset >= limit) {
723 		proc_t *p = ttoproc(curthread);
724 
725 		mutex_enter(&p->p_lock);
726 		(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
727 		    p, RCA_UNSAFE_SIGINFO);
728 		mutex_exit(&p->p_lock);
729 		return (EFBIG);
730 	}
731 
732 	/*
733 	 * if largefiles are disallowed, the limit is
734 	 * the pre-largefiles value of 2GB
735 	 */
736 	if (ufsvfsp->vfs_lfflags & UFS_LARGEFILES)
737 		limit = MIN(UFS_MAXOFFSET_T, limit);
738 	else
739 		limit = MIN(MAXOFF32_T, limit);
740 
741 	if (uio->uio_loffset < (offset_t)0) {
742 		return (EINVAL);
743 	}
744 	if (uio->uio_resid == 0) {
745 		return (0);
746 	}
747 
748 	if (uio->uio_loffset >= limit)
749 		return (EFBIG);
750 
751 	ip->i_flag |= INOACC;	/* don't update ref time in getpage */
752 
753 	if (ioflag & (FSYNC|FDSYNC)) {
754 		ip->i_flag |= ISYNC;
755 		iupdat_flag = 1;
756 	}
757 	/*
758 	 * Try to go direct
759 	 */
760 	if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) {
761 		uio->uio_llimit = limit;
762 		error = ufs_directio_write(ip, uio, ioflag, 0, cr,
763 		    &directio_status);
764 		/*
765 		 * If ufs_directio wrote to the file or set the flags,
766 		 * we need to update i_seq, but it may be deferred.
767 		 */
768 		if (start_resid != uio->uio_resid ||
769 		    (ip->i_flag & (ICHG|IUPD))) {
770 			i_seq_needed = 1;
771 			ip->i_flag |= ISEQ;
772 		}
773 		if (directio_status == DIRECTIO_SUCCESS)
774 			goto out;
775 	}
776 
777 	/*
778 	 * Behavior with respect to dropping/reacquiring vfs_dqrwlock:
779 	 *
780 	 * o shadow inodes: vfs_dqrwlock is not held at all
781 	 * o quota updates: vfs_dqrwlock is read or write held
782 	 * o other updates: vfs_dqrwlock is read held
783 	 *
784 	 * The first case is the only one where we do not hold
785 	 * vfs_dqrwlock at all while entering wrip().
786 	 * We must make sure not to downgrade/drop vfs_dqrwlock if we
787 	 * have it as writer, i.e. if we are updating the quota inode.
788 	 * There is no potential deadlock scenario in this case as
789 	 * ufs_getpage() takes care of this and avoids reacquiring
790 	 * vfs_dqrwlock in that case.
791 	 *
792 	 * This check is done here since the above conditions do not change
793 	 * and we possibly loop below, so save a few cycles.
794 	 */
795 	if ((type == IFSHAD) ||
796 	    (rw_owner(&ufsvfsp->vfs_dqrwlock) == curthread)) {
797 		do_dqrwlock = 0;
798 	} else {
799 		do_dqrwlock = 1;
800 	}
801 
802 	/*
803 	 * Large Files: We cast MAXBMASK to offset_t
804 	 * inorder to mask out the higher bits. Since offset_t
805 	 * is a signed value, the high order bit set in MAXBMASK
806 	 * value makes it do the right thing by having all bits 1
807 	 * in the higher word. May be removed for _SOLARIS64_.
808 	 */
809 
810 	fs = ip->i_fs;
811 	do {
812 		u_offset_t uoff = uio->uio_loffset;
813 		off = uoff & (offset_t)MAXBMASK;
814 		mapon = (int)(uoff & (offset_t)MAXBOFFSET);
815 		on = (int)blkoff(fs, uoff);
816 		n = (int)MIN(fs->fs_bsize - on, uio->uio_resid);
817 		new_iblocks = 1;
818 
819 		if (type == IFREG && uoff + n >= limit) {
820 			if (uoff >= limit) {
821 				error = EFBIG;
822 				goto out;
823 			}
824 			/*
825 			 * since uoff + n >= limit,
826 			 * therefore n >= limit - uoff, and n is an int
827 			 * so it is safe to cast it to an int
828 			 */
829 			n = (int)(limit - (rlim64_t)uoff);
830 		}
831 		if (uoff + n > ip->i_size) {
832 			/*
833 			 * We are extending the length of the file.
834 			 * bmap is used so that we are sure that
835 			 * if we need to allocate new blocks, that it
836 			 * is done here before we up the file size.
837 			 */
838 			error = bmap_write(ip, uoff, (int)(on + n),
839 			    mapon == 0, NULL, cr);
840 			/*
841 			 * bmap_write never drops i_contents so if
842 			 * the flags are set it changed the file.
843 			 */
844 			if (ip->i_flag & (ICHG|IUPD)) {
845 				i_seq_needed = 1;
846 				ip->i_flag |= ISEQ;
847 			}
848 			if (error)
849 				break;
850 			/*
851 			 * There is a window of vulnerability here.
852 			 * The sequence of operations: allocate file
853 			 * system blocks, uiomove the data into pages,
854 			 * and then update the size of the file in the
855 			 * inode, must happen atomically.  However, due
856 			 * to current locking constraints, this can not
857 			 * be done.
858 			 */
859 			ASSERT(ip->i_writer == NULL);
860 			ip->i_writer = curthread;
861 			i_size_changed = 1;
862 			/*
863 			 * If we are writing from the beginning of
864 			 * the mapping, we can just create the
865 			 * pages without having to read them.
866 			 */
867 			pagecreate = (mapon == 0);
868 		} else if (n == MAXBSIZE) {
869 			/*
870 			 * Going to do a whole mappings worth,
871 			 * so we can just create the pages w/o
872 			 * having to read them in.  But before
873 			 * we do that, we need to make sure any
874 			 * needed blocks are allocated first.
875 			 */
876 			iblocks = ip->i_blocks;
877 			error = bmap_write(ip, uoff, (int)(on + n),
878 			    BI_ALLOC_ONLY, NULL, cr);
879 			/*
880 			 * bmap_write never drops i_contents so if
881 			 * the flags are set it changed the file.
882 			 */
883 			if (ip->i_flag & (ICHG|IUPD)) {
884 				i_seq_needed = 1;
885 				ip->i_flag |= ISEQ;
886 			}
887 			if (error)
888 				break;
889 			pagecreate = 1;
890 			/*
891 			 * check if the new created page needed the
892 			 * allocation of new disk blocks.
893 			 */
894 			if (iblocks == ip->i_blocks)
895 				new_iblocks = 0; /* no new blocks allocated */
896 		} else {
897 			pagecreate = 0;
898 			/*
899 			 * In sync mode flush the indirect blocks which
900 			 * may have been allocated and not written on
901 			 * disk. In above cases bmap_write will allocate
902 			 * in sync mode.
903 			 */
904 			if (ioflag & (FSYNC|FDSYNC)) {
905 				error = ufs_indirblk_sync(ip, uoff);
906 				if (error)
907 					break;
908 			}
909 		}
910 
911 		/*
912 		 * At this point we can enter ufs_getpage() in one
913 		 * of two ways:
914 		 * 1) segmap_getmapflt() calls ufs_getpage() when the
915 		 *    forcefault parameter is true (pagecreate == 0)
916 		 * 2) uiomove() causes a page fault.
917 		 *
918 		 * We have to drop the contents lock to prevent the VM
919 		 * system from trying to reacquire it in ufs_getpage()
920 		 * should the uiomove cause a pagefault.
921 		 *
922 		 * We have to drop the reader vfs_dqrwlock here as well.
923 		 */
924 		rw_exit(&ip->i_contents);
925 		if (do_dqrwlock) {
926 			ASSERT(RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock));
927 			ASSERT(!(RW_WRITE_HELD(&ufsvfsp->vfs_dqrwlock)));
928 			rw_exit(&ufsvfsp->vfs_dqrwlock);
929 		}
930 
931 		newpage = 0;
932 		premove_resid = uio->uio_resid;
933 
934 		/*
935 		 * Touch the page and fault it in if it is not in core
936 		 * before segmap_getmapflt or vpm_data_copy can lock it.
937 		 * This is to avoid the deadlock if the buffer is mapped
938 		 * to the same file through mmap which we want to write.
939 		 */
940 		uio_prefaultpages((long)n, uio);
941 
942 		if (vpm_enable) {
943 			/*
944 			 * Copy data. If new pages are created, part of
945 			 * the page that is not written will be initizliazed
946 			 * with zeros.
947 			 */
948 			error = vpm_data_copy(vp, (off + mapon), (uint_t)n,
949 			    uio, !pagecreate, &newpage, 0, S_WRITE);
950 		} else {
951 
952 			base = segmap_getmapflt(segkmap, vp, (off + mapon),
953 			    (uint_t)n, !pagecreate, S_WRITE);
954 
955 			/*
956 			 * segmap_pagecreate() returns 1 if it calls
957 			 * page_create_va() to allocate any pages.
958 			 */
959 
960 			if (pagecreate)
961 				newpage = segmap_pagecreate(segkmap, base,
962 				    (size_t)n, 0);
963 
964 			error = uiomove(base + mapon, (long)n, UIO_WRITE, uio);
965 		}
966 
967 		/*
968 		 * If "newpage" is set, then a new page was created and it
969 		 * does not contain valid data, so it needs to be initialized
970 		 * at this point.
971 		 * Otherwise the page contains old data, which was overwritten
972 		 * partially or as a whole in uiomove.
973 		 * If there is only one iovec structure within uio, then
974 		 * on error uiomove will not be able to update uio->uio_loffset
975 		 * and we would zero the whole page here!
976 		 *
977 		 * If uiomove fails because of an error, the old valid data
978 		 * is kept instead of filling the rest of the page with zero's.
979 		 */
980 		if (!vpm_enable && newpage &&
981 		    uio->uio_loffset < roundup(off + mapon + n, PAGESIZE)) {
982 			/*
983 			 * We created pages w/o initializing them completely,
984 			 * thus we need to zero the part that wasn't set up.
985 			 * This happens on most EOF write cases and if
986 			 * we had some sort of error during the uiomove.
987 			 */
988 			int nzero, nmoved;
989 
990 			nmoved = (int)(uio->uio_loffset - (off + mapon));
991 			ASSERT(nmoved >= 0 && nmoved <= n);
992 			nzero = roundup(on + n, PAGESIZE) - nmoved;
993 			ASSERT(nzero > 0 && mapon + nmoved + nzero <= MAXBSIZE);
994 			(void) kzero(base + mapon + nmoved, (uint_t)nzero);
995 		}
996 
997 		/*
998 		 * Unlock the pages allocated by page_create_va()
999 		 * in segmap_pagecreate()
1000 		 */
1001 		if (!vpm_enable && newpage)
1002 			segmap_pageunlock(segkmap, base, (size_t)n, S_WRITE);
1003 
1004 		/*
1005 		 * If the size of the file changed, then update the
1006 		 * size field in the inode now.  This can't be done
1007 		 * before the call to segmap_pageunlock or there is
1008 		 * a potential deadlock with callers to ufs_putpage().
1009 		 * They will be holding i_contents and trying to lock
1010 		 * a page, while this thread is holding a page locked
1011 		 * and trying to acquire i_contents.
1012 		 */
1013 		if (i_size_changed) {
1014 			rw_enter(&ip->i_contents, RW_WRITER);
1015 			old_i_size = ip->i_size;
1016 			UFS_SET_ISIZE(uoff + n, ip);
1017 			TRANS_INODE(ufsvfsp, ip);
1018 			/*
1019 			 * file has grown larger than 2GB. Set flag
1020 			 * in superblock to indicate this, if it
1021 			 * is not already set.
1022 			 */
1023 			if ((ip->i_size > MAXOFF32_T) &&
1024 			    !(fs->fs_flags & FSLARGEFILES)) {
1025 				ASSERT(ufsvfsp->vfs_lfflags & UFS_LARGEFILES);
1026 				mutex_enter(&ufsvfsp->vfs_lock);
1027 				fs->fs_flags |= FSLARGEFILES;
1028 				ufs_sbwrite(ufsvfsp);
1029 				mutex_exit(&ufsvfsp->vfs_lock);
1030 			}
1031 			mutex_enter(&ip->i_tlock);
1032 			ip->i_writer = NULL;
1033 			cv_broadcast(&ip->i_wrcv);
1034 			mutex_exit(&ip->i_tlock);
1035 			rw_exit(&ip->i_contents);
1036 		}
1037 
1038 		if (error) {
1039 			/*
1040 			 * If we failed on a write, we may have already
1041 			 * allocated file blocks as well as pages.  It's
1042 			 * hard to undo the block allocation, but we must
1043 			 * be sure to invalidate any pages that may have
1044 			 * been allocated.
1045 			 *
1046 			 * If the page was created without initialization
1047 			 * then we must check if it should be possible
1048 			 * to destroy the new page and to keep the old data
1049 			 * on the disk.
1050 			 *
1051 			 * It is possible to destroy the page without
1052 			 * having to write back its contents only when
1053 			 * - the size of the file keeps unchanged
1054 			 * - bmap_write() did not allocate new disk blocks
1055 			 *   it is possible to create big files using "seek" and
1056 			 *   write to the end of the file. A "write" to a
1057 			 *   position before the end of the file would not
1058 			 *   change the size of the file but it would allocate
1059 			 *   new disk blocks.
1060 			 * - uiomove intended to overwrite the whole page.
1061 			 * - a new page was created (newpage == 1).
1062 			 */
1063 
1064 			if (i_size_changed == 0 && new_iblocks == 0 &&
1065 			    newpage) {
1066 
1067 				/* unwind what uiomove eventually last did */
1068 				uio->uio_resid = premove_resid;
1069 
1070 				/*
1071 				 * destroy the page, do not write ambiguous
1072 				 * data to the disk.
1073 				 */
1074 				flags = SM_DESTROY;
1075 			} else {
1076 				/*
1077 				 * write the page back to the disk, if dirty,
1078 				 * and remove the page from the cache.
1079 				 */
1080 				flags = SM_INVAL;
1081 			}
1082 
1083 			if (vpm_enable) {
1084 				/*
1085 				 *  Flush pages.
1086 				 */
1087 				(void) vpm_sync_pages(vp, off, n, flags);
1088 			} else {
1089 				(void) segmap_release(segkmap, base, flags);
1090 			}
1091 		} else {
1092 			flags = 0;
1093 			/*
1094 			 * Force write back for synchronous write cases.
1095 			 */
1096 			if ((ioflag & (FSYNC|FDSYNC)) || type == IFDIR) {
1097 				/*
1098 				 * If the sticky bit is set but the
1099 				 * execute bit is not set, we do a
1100 				 * synchronous write back and free
1101 				 * the page when done.  We set up swap
1102 				 * files to be handled this way to
1103 				 * prevent servers from keeping around
1104 				 * the client's swap pages too long.
1105 				 * XXX - there ought to be a better way.
1106 				 */
1107 				if (IS_SWAPVP(vp)) {
1108 					flags = SM_WRITE | SM_FREE |
1109 					    SM_DONTNEED;
1110 					iupdat_flag = 0;
1111 				} else {
1112 					flags = SM_WRITE;
1113 				}
1114 			} else if (n + on == MAXBSIZE || IS_SWAPVP(vp)) {
1115 				/*
1116 				 * Have written a whole block.
1117 				 * Start an asynchronous write and
1118 				 * mark the buffer to indicate that
1119 				 * it won't be needed again soon.
1120 				 */
1121 				flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
1122 			}
1123 			if (vpm_enable) {
1124 				/*
1125 				 * Flush pages.
1126 				 */
1127 				error = vpm_sync_pages(vp, off, n, flags);
1128 			} else {
1129 				error = segmap_release(segkmap, base, flags);
1130 			}
1131 			/*
1132 			 * If the operation failed and is synchronous,
1133 			 * then we need to unwind what uiomove() last
1134 			 * did so we can potentially return an error to
1135 			 * the caller.  If this write operation was
1136 			 * done in two pieces and the first succeeded,
1137 			 * then we won't return an error for the second
1138 			 * piece that failed.  However, we only want to
1139 			 * return a resid value that reflects what was
1140 			 * really done.
1141 			 *
1142 			 * Failures for non-synchronous operations can
1143 			 * be ignored since the page subsystem will
1144 			 * retry the operation until it succeeds or the
1145 			 * file system is unmounted.
1146 			 */
1147 			if (error) {
1148 				if ((ioflag & (FSYNC | FDSYNC)) ||
1149 				    type == IFDIR) {
1150 					uio->uio_resid = premove_resid;
1151 				} else {
1152 					error = 0;
1153 				}
1154 			}
1155 		}
1156 
1157 		/*
1158 		 * Re-acquire contents lock.
1159 		 * If it was dropped, reacquire reader vfs_dqrwlock as well.
1160 		 */
1161 		if (do_dqrwlock)
1162 			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
1163 		rw_enter(&ip->i_contents, RW_WRITER);
1164 
1165 		/*
1166 		 * If the uiomove() failed or if a synchronous
1167 		 * page push failed, fix up i_size.
1168 		 */
1169 		if (error) {
1170 			if (i_size_changed) {
1171 				/*
1172 				 * The uiomove failed, and we
1173 				 * allocated blocks,so get rid
1174 				 * of them.
1175 				 */
1176 				(void) ufs_itrunc(ip, old_i_size, 0, cr);
1177 			}
1178 		} else {
1179 			/*
1180 			 * XXX - Can this be out of the loop?
1181 			 */
1182 			ip->i_flag |= IUPD | ICHG;
1183 			/*
1184 			 * Only do one increase of i_seq for multiple
1185 			 * pieces.  Because we drop locks, record
1186 			 * the fact that we changed the timestamp and
1187 			 * are deferring the increase in case another thread
1188 			 * pushes our timestamp update.
1189 			 */
1190 			i_seq_needed = 1;
1191 			ip->i_flag |= ISEQ;
1192 			if (i_size_changed)
1193 				ip->i_flag |= IATTCHG;
1194 			if ((ip->i_mode & (IEXEC | (IEXEC >> 3) |
1195 			    (IEXEC >> 6))) != 0 &&
1196 			    (ip->i_mode & (ISUID | ISGID)) != 0 &&
1197 			    secpolicy_vnode_setid_retain(cr,
1198 			    (ip->i_mode & ISUID) != 0 && ip->i_uid == 0) != 0) {
1199 				/*
1200 				 * Clear Set-UID & Set-GID bits on
1201 				 * successful write if not privileged
1202 				 * and at least one of the execute bits
1203 				 * is set.  If we always clear Set-GID,
1204 				 * mandatory file and record locking is
1205 				 * unuseable.
1206 				 */
1207 				ip->i_mode &= ~(ISUID | ISGID);
1208 			}
1209 		}
1210 		/*
1211 		 * In the case the FDSYNC flag is set and this is a
1212 		 * "rewrite" we won't log a delta.
1213 		 * The FSYNC flag overrides all cases.
1214 		 */
1215 		if (!ufs_check_rewrite(ip, uio, ioflag) || !(ioflag & FDSYNC)) {
1216 			TRANS_INODE(ufsvfsp, ip);
1217 		}
1218 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
1219 
1220 out:
1221 	/*
1222 	 * Make sure i_seq is increased at least once per write
1223 	 */
1224 	if (i_seq_needed) {
1225 		ip->i_seq++;
1226 		ip->i_flag &= ~ISEQ;	/* no longer deferred */
1227 	}
1228 
1229 	/*
1230 	 * Inode is updated according to this table -
1231 	 *
1232 	 *   FSYNC	  FDSYNC(posix.4)
1233 	 *   --------------------------
1234 	 *   always@	  IATTCHG|IBDWRITE
1235 	 *
1236 	 * @ -	If we are doing synchronous write the only time we should
1237 	 *	not be sync'ing the ip here is if we have the stickyhack
1238 	 *	activated, the file is marked with the sticky bit and
1239 	 *	no exec bit, the file length has not been changed and
1240 	 *	no new blocks have been allocated during this write.
1241 	 */
1242 
1243 	if ((ip->i_flag & ISYNC) != 0) {
1244 		/*
1245 		 * we have eliminated nosync
1246 		 */
1247 		if ((ip->i_flag & (IATTCHG|IBDWRITE)) ||
1248 		    ((ioflag & FSYNC) && iupdat_flag)) {
1249 			ufs_iupdat(ip, 1);
1250 		}
1251 	}
1252 
1253 	/*
1254 	 * If we've already done a partial-write, terminate
1255 	 * the write but return no error unless the error is ENOSPC
1256 	 * because the caller can detect this and free resources and
1257 	 * try again.
1258 	 */
1259 	if ((start_resid != uio->uio_resid) && (error != ENOSPC))
1260 		error = 0;
1261 
1262 	ip->i_flag &= ~(INOACC | ISYNC);
1263 	ITIMES_NOLOCK(ip);
1264 	return (error);
1265 }
1266 
1267 /*
1268  * rdip does the real work of read requests for ufs.
1269  */
1270 int
rdip(struct inode * ip,struct uio * uio,int ioflag,cred_t * cr)1271 rdip(struct inode *ip, struct uio *uio, int ioflag, cred_t *cr)
1272 {
1273 	u_offset_t off;
1274 	caddr_t base;
1275 	struct fs *fs;
1276 	struct ufsvfs *ufsvfsp;
1277 	struct vnode *vp;
1278 	long oresid = uio->uio_resid;
1279 	u_offset_t n, on, mapon;
1280 	int error = 0;
1281 	int doupdate = 1;
1282 	uint_t flags;
1283 	int directio_status;
1284 	krw_t rwtype;
1285 	o_mode_t type;
1286 
1287 	vp = ITOV(ip);
1288 
1289 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
1290 
1291 	ufsvfsp = ip->i_ufsvfs;
1292 
1293 	if (ufsvfsp == NULL)
1294 		return (EIO);
1295 
1296 	fs = ufsvfsp->vfs_fs;
1297 
1298 	/* check for valid filetype */
1299 	type = ip->i_mode & IFMT;
1300 	if ((type != IFREG) && (type != IFDIR) && (type != IFATTRDIR) &&
1301 	    (type != IFLNK) && (type != IFSHAD)) {
1302 		return (EIO);
1303 	}
1304 
1305 	if (uio->uio_loffset > UFS_MAXOFFSET_T) {
1306 		error = 0;
1307 		goto out;
1308 	}
1309 	if (uio->uio_loffset < (offset_t)0) {
1310 		return (EINVAL);
1311 	}
1312 	if (uio->uio_resid == 0) {
1313 		return (0);
1314 	}
1315 
1316 	if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) && (fs->fs_ronly == 0) &&
1317 	    (!ufsvfsp->vfs_noatime)) {
1318 		mutex_enter(&ip->i_tlock);
1319 		ip->i_flag |= IACC;
1320 		mutex_exit(&ip->i_tlock);
1321 	}
1322 	/*
1323 	 * Try to go direct
1324 	 */
1325 	if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) {
1326 		error = ufs_directio_read(ip, uio, cr, &directio_status);
1327 		if (directio_status == DIRECTIO_SUCCESS)
1328 			goto out;
1329 	}
1330 
1331 	rwtype = (rw_write_held(&ip->i_contents)?RW_WRITER:RW_READER);
1332 
1333 	do {
1334 		offset_t diff;
1335 		u_offset_t uoff = uio->uio_loffset;
1336 		off = uoff & (offset_t)MAXBMASK;
1337 		mapon = (u_offset_t)(uoff & (offset_t)MAXBOFFSET);
1338 		on = (u_offset_t)blkoff(fs, uoff);
1339 		n = MIN((u_offset_t)fs->fs_bsize - on,
1340 		    (u_offset_t)uio->uio_resid);
1341 
1342 		diff = ip->i_size - uoff;
1343 
1344 		if (diff <= (offset_t)0) {
1345 			error = 0;
1346 			goto out;
1347 		}
1348 		if (diff < (offset_t)n)
1349 			n = (int)diff;
1350 
1351 		/*
1352 		 * At this point we can enter ufs_getpage() in one of two
1353 		 * ways:
1354 		 * 1) segmap_getmapflt() calls ufs_getpage() when the
1355 		 *    forcefault parameter is true (value of 1 is passed)
1356 		 * 2) uiomove() causes a page fault.
1357 		 *
1358 		 * We cannot hold onto an i_contents reader lock without
1359 		 * risking deadlock in ufs_getpage() so drop a reader lock.
1360 		 * The ufs_getpage() dolock logic already allows for a
1361 		 * thread holding i_contents as writer to work properly
1362 		 * so we keep a writer lock.
1363 		 */
1364 		if (rwtype == RW_READER)
1365 			rw_exit(&ip->i_contents);
1366 
1367 		if (vpm_enable) {
1368 			/*
1369 			 * Copy data.
1370 			 */
1371 			error = vpm_data_copy(vp, (off + mapon), (uint_t)n,
1372 			    uio, 1, NULL, 0, S_READ);
1373 		} else {
1374 			base = segmap_getmapflt(segkmap, vp, (off + mapon),
1375 			    (uint_t)n, 1, S_READ);
1376 			error = uiomove(base + mapon, (long)n, UIO_READ, uio);
1377 		}
1378 
1379 		flags = 0;
1380 		if (!error) {
1381 			/*
1382 			 * In POSIX SYNC (FSYNC and FDSYNC) read mode,
1383 			 * we want to make sure that the page which has
1384 			 * been read, is written on disk if it is dirty.
1385 			 * And corresponding indirect blocks should also
1386 			 * be flushed out.
1387 			 */
1388 			if ((ioflag & FRSYNC) && (ioflag & (FSYNC|FDSYNC))) {
1389 				flags |= SM_WRITE;
1390 			}
1391 			if (vpm_enable) {
1392 				error = vpm_sync_pages(vp, off, n, flags);
1393 			} else {
1394 				error = segmap_release(segkmap, base, flags);
1395 			}
1396 		} else {
1397 			if (vpm_enable) {
1398 				(void) vpm_sync_pages(vp, off, n, flags);
1399 			} else {
1400 				(void) segmap_release(segkmap, base, flags);
1401 			}
1402 		}
1403 
1404 		if (rwtype == RW_READER)
1405 			rw_enter(&ip->i_contents, rwtype);
1406 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
1407 out:
1408 	/*
1409 	 * Inode is updated according to this table if FRSYNC is set.
1410 	 *
1411 	 *   FSYNC	  FDSYNC(posix.4)
1412 	 *   --------------------------
1413 	 *   always	  IATTCHG|IBDWRITE
1414 	 */
1415 	/*
1416 	 * The inode is not updated if we're logging and the inode is a
1417 	 * directory with FRSYNC, FSYNC and FDSYNC flags set.
1418 	 */
1419 	if (ioflag & FRSYNC) {
1420 		if (TRANS_ISTRANS(ufsvfsp) && ((ip->i_mode & IFMT) == IFDIR)) {
1421 			doupdate = 0;
1422 		}
1423 		if (doupdate) {
1424 			if ((ioflag & FSYNC) ||
1425 			    ((ioflag & FDSYNC) &&
1426 			    (ip->i_flag & (IATTCHG|IBDWRITE)))) {
1427 				ufs_iupdat(ip, 1);
1428 			}
1429 		}
1430 	}
1431 	/*
1432 	 * If we've already done a partial read, terminate
1433 	 * the read but return no error.
1434 	 */
1435 	if (oresid != uio->uio_resid)
1436 		error = 0;
1437 	ITIMES(ip);
1438 
1439 	return (error);
1440 }
1441 
1442 /* ARGSUSED */
1443 static int
ufs_ioctl(struct vnode * vp,int cmd,intptr_t arg,int flag,struct cred * cr,int * rvalp,caller_context_t * ct)1444 ufs_ioctl(
1445 	struct vnode	*vp,
1446 	int		cmd,
1447 	intptr_t	arg,
1448 	int		flag,
1449 	struct cred	*cr,
1450 	int		*rvalp,
1451 	caller_context_t *ct)
1452 {
1453 	struct lockfs	lockfs, lockfs_out;
1454 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
1455 	char		*comment, *original_comment;
1456 	struct fs	*fs;
1457 	struct ulockfs	*ulp;
1458 	offset_t	off;
1459 	extern int	maxphys;
1460 	int		error;
1461 	int		issync;
1462 	int		trans_size;
1463 
1464 
1465 	/*
1466 	 * forcibly unmounted
1467 	 */
1468 	if (ufsvfsp == NULL || vp->v_vfsp == NULL ||
1469 	    vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)
1470 		return (EIO);
1471 	fs = ufsvfsp->vfs_fs;
1472 
1473 	if (cmd == Q_QUOTACTL) {
1474 		error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_QUOTA_MASK);
1475 		if (error)
1476 			return (error);
1477 
1478 		if (ulp) {
1479 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_QUOTA,
1480 			    TOP_SETQUOTA_SIZE(fs));
1481 		}
1482 
1483 		error = quotactl(vp, arg, flag, cr);
1484 
1485 		if (ulp) {
1486 			TRANS_END_ASYNC(ufsvfsp, TOP_QUOTA,
1487 			    TOP_SETQUOTA_SIZE(fs));
1488 			ufs_lockfs_end(ulp);
1489 		}
1490 		return (error);
1491 	}
1492 
1493 	switch (cmd) {
1494 		case _FIOLFS:
1495 			/*
1496 			 * file system locking
1497 			 */
1498 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1499 				return (EPERM);
1500 
1501 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
1502 				if (copyin((caddr_t)arg, &lockfs,
1503 				    sizeof (struct lockfs)))
1504 					return (EFAULT);
1505 			}
1506 #ifdef _SYSCALL32_IMPL
1507 			else {
1508 				struct lockfs32	lockfs32;
1509 				/* Translate ILP32 lockfs to LP64 lockfs */
1510 				if (copyin((caddr_t)arg, &lockfs32,
1511 				    sizeof (struct lockfs32)))
1512 					return (EFAULT);
1513 				lockfs.lf_lock = (ulong_t)lockfs32.lf_lock;
1514 				lockfs.lf_flags = (ulong_t)lockfs32.lf_flags;
1515 				lockfs.lf_key = (ulong_t)lockfs32.lf_key;
1516 				lockfs.lf_comlen = (ulong_t)lockfs32.lf_comlen;
1517 				lockfs.lf_comment =
1518 				    (caddr_t)(uintptr_t)lockfs32.lf_comment;
1519 			}
1520 #endif /* _SYSCALL32_IMPL */
1521 
1522 			if (lockfs.lf_comlen) {
1523 				if (lockfs.lf_comlen > LOCKFS_MAXCOMMENTLEN)
1524 					return (ENAMETOOLONG);
1525 				comment =
1526 				    kmem_alloc(lockfs.lf_comlen, KM_SLEEP);
1527 				if (copyin(lockfs.lf_comment, comment,
1528 				    lockfs.lf_comlen)) {
1529 					kmem_free(comment, lockfs.lf_comlen);
1530 					return (EFAULT);
1531 				}
1532 				original_comment = lockfs.lf_comment;
1533 				lockfs.lf_comment = comment;
1534 			}
1535 			if ((error = ufs_fiolfs(vp, &lockfs, 0)) == 0) {
1536 				lockfs.lf_comment = original_comment;
1537 
1538 				if ((flag & DATAMODEL_MASK) ==
1539 				    DATAMODEL_NATIVE) {
1540 					(void) copyout(&lockfs, (caddr_t)arg,
1541 					    sizeof (struct lockfs));
1542 				}
1543 #ifdef _SYSCALL32_IMPL
1544 				else {
1545 					struct lockfs32	lockfs32;
1546 					/* Translate LP64 to ILP32 lockfs */
1547 					lockfs32.lf_lock =
1548 					    (uint32_t)lockfs.lf_lock;
1549 					lockfs32.lf_flags =
1550 					    (uint32_t)lockfs.lf_flags;
1551 					lockfs32.lf_key =
1552 					    (uint32_t)lockfs.lf_key;
1553 					lockfs32.lf_comlen =
1554 					    (uint32_t)lockfs.lf_comlen;
1555 					lockfs32.lf_comment =
1556 					    (uint32_t)(uintptr_t)
1557 					    lockfs.lf_comment;
1558 					(void) copyout(&lockfs32, (caddr_t)arg,
1559 					    sizeof (struct lockfs32));
1560 				}
1561 #endif /* _SYSCALL32_IMPL */
1562 
1563 			} else {
1564 				if (lockfs.lf_comlen)
1565 					kmem_free(comment, lockfs.lf_comlen);
1566 			}
1567 			return (error);
1568 
1569 		case _FIOLFSS:
1570 			/*
1571 			 * get file system locking status
1572 			 */
1573 
1574 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
1575 				if (copyin((caddr_t)arg, &lockfs,
1576 				    sizeof (struct lockfs)))
1577 					return (EFAULT);
1578 			}
1579 #ifdef _SYSCALL32_IMPL
1580 			else {
1581 				struct lockfs32	lockfs32;
1582 				/* Translate ILP32 lockfs to LP64 lockfs */
1583 				if (copyin((caddr_t)arg, &lockfs32,
1584 				    sizeof (struct lockfs32)))
1585 					return (EFAULT);
1586 				lockfs.lf_lock = (ulong_t)lockfs32.lf_lock;
1587 				lockfs.lf_flags = (ulong_t)lockfs32.lf_flags;
1588 				lockfs.lf_key = (ulong_t)lockfs32.lf_key;
1589 				lockfs.lf_comlen = (ulong_t)lockfs32.lf_comlen;
1590 				lockfs.lf_comment =
1591 				    (caddr_t)(uintptr_t)lockfs32.lf_comment;
1592 			}
1593 #endif /* _SYSCALL32_IMPL */
1594 
1595 			if (error =  ufs_fiolfss(vp, &lockfs_out))
1596 				return (error);
1597 			lockfs.lf_lock = lockfs_out.lf_lock;
1598 			lockfs.lf_key = lockfs_out.lf_key;
1599 			lockfs.lf_flags = lockfs_out.lf_flags;
1600 			lockfs.lf_comlen = MIN(lockfs.lf_comlen,
1601 			    lockfs_out.lf_comlen);
1602 
1603 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
1604 				if (copyout(&lockfs, (caddr_t)arg,
1605 				    sizeof (struct lockfs)))
1606 					return (EFAULT);
1607 			}
1608 #ifdef _SYSCALL32_IMPL
1609 			else {
1610 				/* Translate LP64 to ILP32 lockfs */
1611 				struct lockfs32	lockfs32;
1612 				lockfs32.lf_lock = (uint32_t)lockfs.lf_lock;
1613 				lockfs32.lf_flags = (uint32_t)lockfs.lf_flags;
1614 				lockfs32.lf_key = (uint32_t)lockfs.lf_key;
1615 				lockfs32.lf_comlen = (uint32_t)lockfs.lf_comlen;
1616 				lockfs32.lf_comment =
1617 				    (uint32_t)(uintptr_t)lockfs.lf_comment;
1618 				if (copyout(&lockfs32, (caddr_t)arg,
1619 				    sizeof (struct lockfs32)))
1620 					return (EFAULT);
1621 			}
1622 #endif /* _SYSCALL32_IMPL */
1623 
1624 			if (lockfs.lf_comlen &&
1625 			    lockfs.lf_comment && lockfs_out.lf_comment)
1626 				if (copyout(lockfs_out.lf_comment,
1627 				    lockfs.lf_comment, lockfs.lf_comlen))
1628 					return (EFAULT);
1629 			return (0);
1630 
1631 		case _FIOSATIME:
1632 			/*
1633 			 * set access time
1634 			 */
1635 
1636 			/*
1637 			 * if mounted w/o atime, return quietly.
1638 			 * I briefly thought about returning ENOSYS, but
1639 			 * figured that most apps would consider this fatal
1640 			 * but the idea is to make this as seamless as poss.
1641 			 */
1642 			if (ufsvfsp->vfs_noatime)
1643 				return (0);
1644 
1645 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
1646 			    ULOCKFS_SETATTR_MASK);
1647 			if (error)
1648 				return (error);
1649 
1650 			if (ulp) {
1651 				trans_size = (int)TOP_SETATTR_SIZE(VTOI(vp));
1652 				TRANS_BEGIN_CSYNC(ufsvfsp, issync,
1653 				    TOP_SETATTR, trans_size);
1654 			}
1655 
1656 			error = ufs_fiosatime(vp, (struct timeval *)arg,
1657 			    flag, cr);
1658 
1659 			if (ulp) {
1660 				TRANS_END_CSYNC(ufsvfsp, error, issync,
1661 				    TOP_SETATTR, trans_size);
1662 				ufs_lockfs_end(ulp);
1663 			}
1664 			return (error);
1665 
1666 		case _FIOSDIO:
1667 			/*
1668 			 * set delayed-io
1669 			 */
1670 			return (ufs_fiosdio(vp, (uint_t *)arg, flag, cr));
1671 
1672 		case _FIOGDIO:
1673 			/*
1674 			 * get delayed-io
1675 			 */
1676 			return (ufs_fiogdio(vp, (uint_t *)arg, flag, cr));
1677 
1678 		case _FIOIO:
1679 			/*
1680 			 * inode open
1681 			 */
1682 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
1683 			    ULOCKFS_VGET_MASK);
1684 			if (error)
1685 				return (error);
1686 
1687 			error = ufs_fioio(vp, (struct fioio *)arg, flag, cr);
1688 
1689 			if (ulp) {
1690 				ufs_lockfs_end(ulp);
1691 			}
1692 			return (error);
1693 
1694 		case _FIOFFS:
1695 			/*
1696 			 * file system flush (push w/invalidate)
1697 			 */
1698 			if ((caddr_t)arg != NULL)
1699 				return (EINVAL);
1700 			return (ufs_fioffs(vp, NULL, cr));
1701 
1702 		case _FIOISBUSY:
1703 			/*
1704 			 * Contract-private interface for Legato
1705 			 * Purge this vnode from the DNLC and decide
1706 			 * if this vnode is busy (*arg == 1) or not
1707 			 * (*arg == 0)
1708 			 */
1709 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1710 				return (EPERM);
1711 			error = ufs_fioisbusy(vp, (int *)arg, cr);
1712 			return (error);
1713 
1714 		case _FIODIRECTIO:
1715 			return (ufs_fiodirectio(vp, (int)arg, cr));
1716 
1717 		case _FIOTUNE:
1718 			/*
1719 			 * Tune the file system (aka setting fs attributes)
1720 			 */
1721 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
1722 			    ULOCKFS_SETATTR_MASK);
1723 			if (error)
1724 				return (error);
1725 
1726 			error = ufs_fiotune(vp, (struct fiotune *)arg, cr);
1727 
1728 			if (ulp)
1729 				ufs_lockfs_end(ulp);
1730 			return (error);
1731 
1732 		case _FIOLOGENABLE:
1733 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1734 				return (EPERM);
1735 			return (ufs_fiologenable(vp, (void *)arg, cr, flag));
1736 
1737 		case _FIOLOGDISABLE:
1738 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1739 				return (EPERM);
1740 			return (ufs_fiologdisable(vp, (void *)arg, cr, flag));
1741 
1742 		case _FIOISLOG:
1743 			return (ufs_fioislog(vp, (void *)arg, cr, flag));
1744 
1745 		case _FIOSNAPSHOTCREATE_MULTI:
1746 		{
1747 			struct fiosnapcreate_multi	fc, *fcp;
1748 			size_t	fcm_size;
1749 
1750 			if (copyin((void *)arg, &fc, sizeof (fc)))
1751 				return (EFAULT);
1752 			if (fc.backfilecount > MAX_BACKFILE_COUNT)
1753 				return (EINVAL);
1754 			fcm_size = sizeof (struct fiosnapcreate_multi) +
1755 			    (fc.backfilecount - 1) * sizeof (int);
1756 			fcp = (struct fiosnapcreate_multi *)
1757 			    kmem_alloc(fcm_size, KM_SLEEP);
1758 			if (copyin((void *)arg, fcp, fcm_size)) {
1759 				kmem_free(fcp, fcm_size);
1760 				return (EFAULT);
1761 			}
1762 			error = ufs_snap_create(vp, fcp, cr);
1763 			/*
1764 			 * Do copyout even if there is an error because
1765 			 * the details of error is stored in fcp.
1766 			 */
1767 			if (copyout(fcp, (void *)arg, fcm_size))
1768 				error = EFAULT;
1769 			kmem_free(fcp, fcm_size);
1770 			return (error);
1771 		}
1772 
1773 		case _FIOSNAPSHOTDELETE:
1774 		{
1775 			struct fiosnapdelete	fc;
1776 
1777 			if (copyin((void *)arg, &fc, sizeof (fc)))
1778 				return (EFAULT);
1779 			error = ufs_snap_delete(vp, &fc, cr);
1780 			if (!error && copyout(&fc, (void *)arg, sizeof (fc)))
1781 				error = EFAULT;
1782 			return (error);
1783 		}
1784 
1785 		case _FIOGETSUPERBLOCK:
1786 			if (copyout(fs, (void *)arg, SBSIZE))
1787 				return (EFAULT);
1788 			return (0);
1789 
1790 		case _FIOGETMAXPHYS:
1791 			if (copyout(&maxphys, (void *)arg, sizeof (maxphys)))
1792 				return (EFAULT);
1793 			return (0);
1794 
1795 		/*
1796 		 * The following 3 ioctls are for TSufs support
1797 		 * although could potentially be used elsewhere
1798 		 */
1799 		case _FIO_SET_LUFS_DEBUG:
1800 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1801 				return (EPERM);
1802 			lufs_debug = (uint32_t)arg;
1803 			return (0);
1804 
1805 		case _FIO_SET_LUFS_ERROR:
1806 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1807 				return (EPERM);
1808 			TRANS_SETERROR(ufsvfsp);
1809 			return (0);
1810 
1811 		case _FIO_GET_TOP_STATS:
1812 		{
1813 			fio_lufs_stats_t *ls;
1814 			ml_unit_t *ul = ufsvfsp->vfs_log;
1815 
1816 			ls = kmem_zalloc(sizeof (*ls), KM_SLEEP);
1817 			ls->ls_debug = ul->un_debug; /* return debug value */
1818 			/* Copy stucture if statistics are being kept */
1819 			if (ul->un_logmap->mtm_tops) {
1820 				ls->ls_topstats = *(ul->un_logmap->mtm_tops);
1821 			}
1822 			error = 0;
1823 			if (copyout(ls, (void *)arg, sizeof (*ls)))
1824 				error = EFAULT;
1825 			kmem_free(ls, sizeof (*ls));
1826 			return (error);
1827 		}
1828 
1829 		case _FIO_SEEK_DATA:
1830 		case _FIO_SEEK_HOLE:
1831 			if (ddi_copyin((void *)arg, &off, sizeof (off), flag))
1832 				return (EFAULT);
1833 			/* offset paramater is in/out */
1834 			error = ufs_fio_holey(vp, cmd, &off);
1835 			if (error)
1836 				return (error);
1837 			if (ddi_copyout(&off, (void *)arg, sizeof (off), flag))
1838 				return (EFAULT);
1839 			return (0);
1840 
1841 		case _FIO_COMPRESSED:
1842 		{
1843 			/*
1844 			 * This is a project private ufs ioctl() to mark
1845 			 * the inode as that belonging to a compressed
1846 			 * file. This is used to mark individual
1847 			 * compressed files in a miniroot archive.
1848 			 * The files compressed in this manner are
1849 			 * automatically decompressed by the dcfs filesystem
1850 			 * (via an interception in ufs_lookup - see decompvp())
1851 			 * which is layered on top of ufs on a system running
1852 			 * from the archive. See uts/common/fs/dcfs for details.
1853 			 * This ioctl only marks the file as compressed - the
1854 			 * actual compression is done by fiocompress (a
1855 			 * userland utility) which invokes this ioctl().
1856 			 */
1857 			struct inode *ip = VTOI(vp);
1858 
1859 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
1860 			    ULOCKFS_SETATTR_MASK);
1861 			if (error)
1862 				return (error);
1863 
1864 			if (ulp) {
1865 				TRANS_BEGIN_ASYNC(ufsvfsp, TOP_IUPDAT,
1866 				    TOP_IUPDAT_SIZE(ip));
1867 			}
1868 
1869 			error = ufs_mark_compressed(vp);
1870 
1871 			if (ulp) {
1872 				TRANS_END_ASYNC(ufsvfsp, TOP_IUPDAT,
1873 				    TOP_IUPDAT_SIZE(ip));
1874 				ufs_lockfs_end(ulp);
1875 			}
1876 
1877 			return (error);
1878 
1879 		}
1880 
1881 		default:
1882 			return (ENOTTY);
1883 	}
1884 }
1885 
1886 
1887 /* ARGSUSED */
1888 static int
ufs_getattr(struct vnode * vp,struct vattr * vap,int flags,struct cred * cr,caller_context_t * ct)1889 ufs_getattr(struct vnode *vp, struct vattr *vap, int flags,
1890     struct cred *cr, caller_context_t *ct)
1891 {
1892 	struct inode *ip = VTOI(vp);
1893 	struct ufsvfs *ufsvfsp;
1894 	int err;
1895 
1896 	if (vap->va_mask == AT_SIZE) {
1897 		/*
1898 		 * for performance, if only the size is requested don't bother
1899 		 * with anything else.
1900 		 */
1901 		UFS_GET_ISIZE(&vap->va_size, ip);
1902 		return (0);
1903 	}
1904 
1905 	/*
1906 	 * inlined lockfs checks
1907 	 */
1908 	ufsvfsp = ip->i_ufsvfs;
1909 	if ((ufsvfsp == NULL) || ULOCKFS_IS_HLOCK(&ufsvfsp->vfs_ulockfs)) {
1910 		err = EIO;
1911 		goto out;
1912 	}
1913 
1914 	rw_enter(&ip->i_contents, RW_READER);
1915 	/*
1916 	 * Return all the attributes.  This should be refined so
1917 	 * that it only returns what's asked for.
1918 	 */
1919 
1920 	/*
1921 	 * Copy from inode table.
1922 	 */
1923 	vap->va_type = vp->v_type;
1924 	vap->va_mode = ip->i_mode & MODEMASK;
1925 	/*
1926 	 * If there is an ACL and there is a mask entry, then do the
1927 	 * extra work that completes the equivalent of an acltomode(3)
1928 	 * call.  According to POSIX P1003.1e, the acl mask should be
1929 	 * returned in the group permissions field.
1930 	 *
1931 	 * - start with the original permission and mode bits (from above)
1932 	 * - clear the group owner bits
1933 	 * - add in the mask bits.
1934 	 */
1935 	if (ip->i_ufs_acl && ip->i_ufs_acl->aclass.acl_ismask) {
1936 		vap->va_mode &= ~((VREAD | VWRITE | VEXEC) >> 3);
1937 		vap->va_mode |=
1938 		    (ip->i_ufs_acl->aclass.acl_maskbits & PERMMASK) << 3;
1939 	}
1940 	vap->va_uid = ip->i_uid;
1941 	vap->va_gid = ip->i_gid;
1942 	vap->va_fsid = ip->i_dev;
1943 	vap->va_nodeid = (ino64_t)ip->i_number;
1944 	vap->va_nlink = ip->i_nlink;
1945 	vap->va_size = ip->i_size;
1946 	if (vp->v_type == VCHR || vp->v_type == VBLK)
1947 		vap->va_rdev = ip->i_rdev;
1948 	else
1949 		vap->va_rdev = 0;	/* not a b/c spec. */
1950 	mutex_enter(&ip->i_tlock);
1951 	ITIMES_NOLOCK(ip);	/* mark correct time in inode */
1952 	vap->va_seq = ip->i_seq;
1953 	vap->va_atime.tv_sec = (time_t)ip->i_atime.tv_sec;
1954 	vap->va_atime.tv_nsec = ip->i_atime.tv_usec*1000;
1955 	vap->va_mtime.tv_sec = (time_t)ip->i_mtime.tv_sec;
1956 	vap->va_mtime.tv_nsec = ip->i_mtime.tv_usec*1000;
1957 	vap->va_ctime.tv_sec = (time_t)ip->i_ctime.tv_sec;
1958 	vap->va_ctime.tv_nsec = ip->i_ctime.tv_usec*1000;
1959 	mutex_exit(&ip->i_tlock);
1960 
1961 	switch (ip->i_mode & IFMT) {
1962 
1963 	case IFBLK:
1964 		vap->va_blksize = MAXBSIZE;		/* was BLKDEV_IOSIZE */
1965 		break;
1966 
1967 	case IFCHR:
1968 		vap->va_blksize = MAXBSIZE;
1969 		break;
1970 
1971 	default:
1972 		vap->va_blksize = ip->i_fs->fs_bsize;
1973 		break;
1974 	}
1975 	vap->va_nblocks = (fsblkcnt64_t)ip->i_blocks;
1976 	rw_exit(&ip->i_contents);
1977 	err = 0;
1978 
1979 out:
1980 	return (err);
1981 }
1982 
1983 /*
1984  * Special wrapper to provide a callback for secpolicy_vnode_setattr().
1985  * The i_contents lock is already held by the caller and we need to
1986  * declare the inode as 'void *' argument.
1987  */
1988 static int
ufs_priv_access(void * vip,int mode,struct cred * cr)1989 ufs_priv_access(void *vip, int mode, struct cred *cr)
1990 {
1991 	struct inode *ip = vip;
1992 
1993 	return (ufs_iaccess(ip, mode, cr, 0));
1994 }
1995 
1996 /*ARGSUSED4*/
1997 static int
ufs_setattr(struct vnode * vp,struct vattr * vap,int flags,struct cred * cr,caller_context_t * ct)1998 ufs_setattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr,
1999     caller_context_t *ct)
2000 {
2001 	struct inode *ip = VTOI(vp);
2002 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
2003 	struct fs *fs;
2004 	struct ulockfs *ulp;
2005 	char *errmsg1;
2006 	char *errmsg2;
2007 	long blocks;
2008 	long int mask = vap->va_mask;
2009 	size_t len1, len2;
2010 	int issync;
2011 	int trans_size;
2012 	int dotrans;
2013 	int dorwlock;
2014 	int error;
2015 	int owner_change;
2016 	int dodqlock;
2017 	timestruc_t now;
2018 	vattr_t oldva;
2019 	int retry = 1;
2020 	int indeadlock;
2021 
2022 	/*
2023 	 * Cannot set these attributes.
2024 	 */
2025 	if ((mask & AT_NOSET) || (mask & AT_XVATTR))
2026 		return (EINVAL);
2027 
2028 	/*
2029 	 * check for forced unmount
2030 	 */
2031 	if (ufsvfsp == NULL)
2032 		return (EIO);
2033 
2034 	fs = ufsvfsp->vfs_fs;
2035 	if (fs->fs_ronly != 0)
2036 		return (EROFS);
2037 
2038 again:
2039 	errmsg1 = NULL;
2040 	errmsg2 = NULL;
2041 	dotrans = 0;
2042 	dorwlock = 0;
2043 	dodqlock = 0;
2044 
2045 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SETATTR_MASK);
2046 	if (error)
2047 		goto out;
2048 
2049 	/*
2050 	 * Acquire i_rwlock before TRANS_BEGIN_CSYNC() if this is a file.
2051 	 * This follows the protocol for read()/write().
2052 	 */
2053 	if (vp->v_type != VDIR) {
2054 		/*
2055 		 * ufs_tryirwlock uses rw_tryenter and checks for SLOCK to
2056 		 * avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
2057 		 * possible, retries the operation.
2058 		 */
2059 		ufs_tryirwlock(&ip->i_rwlock, RW_WRITER, retry_file);
2060 		if (indeadlock) {
2061 			if (ulp)
2062 				ufs_lockfs_end(ulp);
2063 			goto again;
2064 		}
2065 		dorwlock = 1;
2066 	}
2067 
2068 	/*
2069 	 * Truncate file.  Must have write permission and not be a directory.
2070 	 */
2071 	if (mask & AT_SIZE) {
2072 		rw_enter(&ip->i_contents, RW_WRITER);
2073 		if (vp->v_type == VDIR) {
2074 			error = EISDIR;
2075 			goto update_inode;
2076 		}
2077 		if (error = ufs_iaccess(ip, IWRITE, cr, 0))
2078 			goto update_inode;
2079 
2080 		rw_exit(&ip->i_contents);
2081 		error = TRANS_ITRUNC(ip, vap->va_size, 0, cr);
2082 		if (error) {
2083 			rw_enter(&ip->i_contents, RW_WRITER);
2084 			goto update_inode;
2085 		}
2086 
2087 		if (error == 0 && vap->va_size)
2088 			vnevent_truncate(vp, ct);
2089 	}
2090 
2091 	if (ulp) {
2092 		trans_size = (int)TOP_SETATTR_SIZE(ip);
2093 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_SETATTR, trans_size);
2094 		++dotrans;
2095 	}
2096 
2097 	/*
2098 	 * Acquire i_rwlock after TRANS_BEGIN_CSYNC() if this is a directory.
2099 	 * This follows the protocol established by
2100 	 * ufs_link/create/remove/rename/mkdir/rmdir/symlink.
2101 	 */
2102 	if (vp->v_type == VDIR) {
2103 		ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_SETATTR,
2104 		    retry_dir);
2105 		if (indeadlock)
2106 			goto again;
2107 		dorwlock = 1;
2108 	}
2109 
2110 	/*
2111 	 * Grab quota lock if we are changing the file's owner.
2112 	 */
2113 	if (mask & AT_UID) {
2114 		rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
2115 		dodqlock = 1;
2116 	}
2117 	rw_enter(&ip->i_contents, RW_WRITER);
2118 
2119 	oldva.va_mode = ip->i_mode;
2120 	oldva.va_uid = ip->i_uid;
2121 	oldva.va_gid = ip->i_gid;
2122 
2123 	vap->va_mask &= ~AT_SIZE;
2124 
2125 	error = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2126 	    ufs_priv_access, ip);
2127 	if (error)
2128 		goto update_inode;
2129 
2130 	mask = vap->va_mask;
2131 
2132 	/*
2133 	 * Change file access modes.
2134 	 */
2135 	if (mask & AT_MODE) {
2136 		ip->i_mode = (ip->i_mode & IFMT) | (vap->va_mode & ~IFMT);
2137 		TRANS_INODE(ufsvfsp, ip);
2138 		ip->i_flag |= ICHG;
2139 		if (stickyhack) {
2140 			mutex_enter(&vp->v_lock);
2141 			if ((ip->i_mode & (ISVTX | IEXEC | IFDIR)) == ISVTX)
2142 				vp->v_flag |= VSWAPLIKE;
2143 			else
2144 				vp->v_flag &= ~VSWAPLIKE;
2145 			mutex_exit(&vp->v_lock);
2146 		}
2147 	}
2148 	if (mask & (AT_UID|AT_GID)) {
2149 		if (mask & AT_UID) {
2150 			/*
2151 			 * Don't change ownership of the quota inode.
2152 			 */
2153 			if (ufsvfsp->vfs_qinod == ip) {
2154 				ASSERT(ufsvfsp->vfs_qflags & MQ_ENABLED);
2155 				error = EINVAL;
2156 				goto update_inode;
2157 			}
2158 
2159 			/*
2160 			 * No real ownership change.
2161 			 */
2162 			if (ip->i_uid == vap->va_uid) {
2163 				blocks = 0;
2164 				owner_change = 0;
2165 			}
2166 			/*
2167 			 * Remove the blocks and the file, from the old user's
2168 			 * quota.
2169 			 */
2170 			else {
2171 				blocks = ip->i_blocks;
2172 				owner_change = 1;
2173 
2174 				(void) chkdq(ip, -blocks, /* force */ 1, cr,
2175 				    (char **)NULL, (size_t *)NULL);
2176 				(void) chkiq(ufsvfsp, /* change */ -1, ip,
2177 				    (uid_t)ip->i_uid, /* force */ 1, cr,
2178 				    (char **)NULL, (size_t *)NULL);
2179 				dqrele(ip->i_dquot);
2180 			}
2181 
2182 			ip->i_uid = vap->va_uid;
2183 
2184 			/*
2185 			 * There is a real ownership change.
2186 			 */
2187 			if (owner_change) {
2188 				/*
2189 				 * Add the blocks and the file to the new
2190 				 * user's quota.
2191 				 */
2192 				ip->i_dquot = getinoquota(ip);
2193 				(void) chkdq(ip, blocks, /* force */ 1, cr,
2194 				    &errmsg1, &len1);
2195 				(void) chkiq(ufsvfsp, /* change */ 1,
2196 				    (struct inode *)NULL, (uid_t)ip->i_uid,
2197 				    /* force */ 1, cr, &errmsg2, &len2);
2198 			}
2199 		}
2200 		if (mask & AT_GID) {
2201 			ip->i_gid = vap->va_gid;
2202 		}
2203 		TRANS_INODE(ufsvfsp, ip);
2204 		ip->i_flag |= ICHG;
2205 	}
2206 	/*
2207 	 * Change file access or modified times.
2208 	 */
2209 	if (mask & (AT_ATIME|AT_MTIME)) {
2210 		/* Check that the time value is within ufs range */
2211 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2212 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2213 			error = EOVERFLOW;
2214 			goto update_inode;
2215 		}
2216 
2217 		/*
2218 		 * if the "noaccess" mount option is set and only atime
2219 		 * update is requested, do nothing. No error is returned.
2220 		 */
2221 		if ((ufsvfsp->vfs_noatime) &&
2222 		    ((mask & (AT_ATIME|AT_MTIME)) == AT_ATIME))
2223 			goto skip_atime;
2224 
2225 		if (mask & AT_ATIME) {
2226 			ip->i_atime.tv_sec = vap->va_atime.tv_sec;
2227 			ip->i_atime.tv_usec = vap->va_atime.tv_nsec / 1000;
2228 			ip->i_flag &= ~IACC;
2229 		}
2230 		if (mask & AT_MTIME) {
2231 			ip->i_mtime.tv_sec = vap->va_mtime.tv_sec;
2232 			ip->i_mtime.tv_usec = vap->va_mtime.tv_nsec / 1000;
2233 			gethrestime(&now);
2234 			if (now.tv_sec > TIME32_MAX) {
2235 				/*
2236 				 * In 2038, ctime sticks forever..
2237 				 */
2238 				ip->i_ctime.tv_sec = TIME32_MAX;
2239 				ip->i_ctime.tv_usec = 0;
2240 			} else {
2241 				ip->i_ctime.tv_sec = now.tv_sec;
2242 				ip->i_ctime.tv_usec = now.tv_nsec / 1000;
2243 			}
2244 			ip->i_flag &= ~(IUPD|ICHG);
2245 			ip->i_flag |= IMODTIME;
2246 		}
2247 		TRANS_INODE(ufsvfsp, ip);
2248 		ip->i_flag |= IMOD;
2249 	}
2250 
2251 skip_atime:
2252 	/*
2253 	 * The presence of a shadow inode may indicate an ACL, but does
2254 	 * not imply an ACL.  Future FSD types should be handled here too
2255 	 * and check for the presence of the attribute-specific data
2256 	 * before referencing it.
2257 	 */
2258 	if (ip->i_shadow) {
2259 		/*
2260 		 * XXX if ufs_iupdat is changed to sandbagged write fix
2261 		 * ufs_acl_setattr to push ip to keep acls consistent
2262 		 *
2263 		 * Suppress out of inodes messages if we will retry.
2264 		 */
2265 		if (retry)
2266 			ip->i_flag |= IQUIET;
2267 		error = ufs_acl_setattr(ip, vap, cr);
2268 		ip->i_flag &= ~IQUIET;
2269 	}
2270 
2271 update_inode:
2272 	/*
2273 	 * Setattr always increases the sequence number
2274 	 */
2275 	ip->i_seq++;
2276 
2277 	/*
2278 	 * if nfsd and not logging; push synchronously
2279 	 */
2280 	if ((curthread->t_flag & T_DONTPEND) && !TRANS_ISTRANS(ufsvfsp)) {
2281 		ufs_iupdat(ip, 1);
2282 	} else {
2283 		ITIMES_NOLOCK(ip);
2284 	}
2285 
2286 	rw_exit(&ip->i_contents);
2287 	if (dodqlock) {
2288 		rw_exit(&ufsvfsp->vfs_dqrwlock);
2289 	}
2290 	if (dorwlock)
2291 		rw_exit(&ip->i_rwlock);
2292 
2293 	if (ulp) {
2294 		if (dotrans) {
2295 			int terr = 0;
2296 			TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_SETATTR,
2297 			    trans_size);
2298 			if (error == 0)
2299 				error = terr;
2300 		}
2301 		ufs_lockfs_end(ulp);
2302 	}
2303 out:
2304 	/*
2305 	 * If out of inodes or blocks, see if we can free something
2306 	 * up from the delete queue.
2307 	 */
2308 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
2309 		ufs_delete_drain_wait(ufsvfsp, 1);
2310 		retry = 0;
2311 		if (errmsg1 != NULL)
2312 			kmem_free(errmsg1, len1);
2313 		if (errmsg2 != NULL)
2314 			kmem_free(errmsg2, len2);
2315 		goto again;
2316 	}
2317 	if (errmsg1 != NULL) {
2318 		uprintf(errmsg1);
2319 		kmem_free(errmsg1, len1);
2320 	}
2321 	if (errmsg2 != NULL) {
2322 		uprintf(errmsg2);
2323 		kmem_free(errmsg2, len2);
2324 	}
2325 	return (error);
2326 }
2327 
2328 /*ARGSUSED*/
2329 static int
ufs_access(struct vnode * vp,int mode,int flags,struct cred * cr,caller_context_t * ct)2330 ufs_access(struct vnode *vp, int mode, int flags, struct cred *cr,
2331     caller_context_t *ct)
2332 {
2333 	struct inode *ip = VTOI(vp);
2334 
2335 	if (ip->i_ufsvfs == NULL)
2336 		return (EIO);
2337 
2338 	/*
2339 	 * The ufs_iaccess function wants to be called with
2340 	 * mode bits expressed as "ufs specific" bits.
2341 	 * I.e., VWRITE|VREAD|VEXEC do not make sense to
2342 	 * ufs_iaccess() but IWRITE|IREAD|IEXEC do.
2343 	 * But since they're the same we just pass the vnode mode
2344 	 * bit but just verify that assumption at compile time.
2345 	 */
2346 #if IWRITE != VWRITE || IREAD != VREAD || IEXEC != VEXEC
2347 #error "ufs_access needs to map Vmodes to Imodes"
2348 #endif
2349 	return (ufs_iaccess(ip, mode, cr, 1));
2350 }
2351 
2352 /* ARGSUSED */
2353 static int
ufs_readlink(struct vnode * vp,struct uio * uiop,struct cred * cr,caller_context_t * ct)2354 ufs_readlink(struct vnode *vp, struct uio *uiop, struct cred *cr,
2355     caller_context_t *ct)
2356 {
2357 	struct inode *ip = VTOI(vp);
2358 	struct ufsvfs *ufsvfsp;
2359 	struct ulockfs *ulp;
2360 	int error;
2361 	int fastsymlink;
2362 
2363 	if (vp->v_type != VLNK) {
2364 		error = EINVAL;
2365 		goto nolockout;
2366 	}
2367 
2368 	/*
2369 	 * If the symbolic link is empty there is nothing to read.
2370 	 * Fast-track these empty symbolic links
2371 	 */
2372 	if (ip->i_size == 0) {
2373 		error = 0;
2374 		goto nolockout;
2375 	}
2376 
2377 	ufsvfsp = ip->i_ufsvfs;
2378 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READLINK_MASK);
2379 	if (error)
2380 		goto nolockout;
2381 	/*
2382 	 * The ip->i_rwlock protects the data blocks used for FASTSYMLINK
2383 	 */
2384 again:
2385 	fastsymlink = 0;
2386 	if (ip->i_flag & IFASTSYMLNK) {
2387 		rw_enter(&ip->i_rwlock, RW_READER);
2388 		rw_enter(&ip->i_contents, RW_READER);
2389 		if (ip->i_flag & IFASTSYMLNK) {
2390 			if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) &&
2391 			    (ip->i_fs->fs_ronly == 0) &&
2392 			    (!ufsvfsp->vfs_noatime)) {
2393 				mutex_enter(&ip->i_tlock);
2394 				ip->i_flag |= IACC;
2395 				mutex_exit(&ip->i_tlock);
2396 			}
2397 			error = uiomove((caddr_t)&ip->i_db[1],
2398 			    MIN(ip->i_size, uiop->uio_resid),
2399 			    UIO_READ, uiop);
2400 			ITIMES(ip);
2401 			++fastsymlink;
2402 		}
2403 		rw_exit(&ip->i_contents);
2404 		rw_exit(&ip->i_rwlock);
2405 	}
2406 	if (!fastsymlink) {
2407 		ssize_t size;	/* number of bytes read  */
2408 		caddr_t basep;	/* pointer to input data */
2409 		ino_t ino;
2410 		long  igen;
2411 		struct uio tuio;	/* temp uio struct */
2412 		struct uio *tuiop;
2413 		iovec_t tiov;		/* temp iovec struct */
2414 		char kbuf[FSL_SIZE];	/* buffer to hold fast symlink */
2415 		int tflag = 0;		/* flag to indicate temp vars used */
2416 
2417 		ino = ip->i_number;
2418 		igen = ip->i_gen;
2419 		size = uiop->uio_resid;
2420 		basep = uiop->uio_iov->iov_base;
2421 		tuiop = uiop;
2422 
2423 		rw_enter(&ip->i_rwlock, RW_WRITER);
2424 		rw_enter(&ip->i_contents, RW_WRITER);
2425 		if (ip->i_flag & IFASTSYMLNK) {
2426 			rw_exit(&ip->i_contents);
2427 			rw_exit(&ip->i_rwlock);
2428 			goto again;
2429 		}
2430 
2431 		/* can this be a fast symlink and is it a user buffer? */
2432 		if (ip->i_size <= FSL_SIZE &&
2433 		    (uiop->uio_segflg == UIO_USERSPACE ||
2434 		    uiop->uio_segflg == UIO_USERISPACE)) {
2435 
2436 			bzero(&tuio, sizeof (struct uio));
2437 			/*
2438 			 * setup a kernel buffer to read link into.  this
2439 			 * is to fix a race condition where the user buffer
2440 			 * got corrupted before copying it into the inode.
2441 			 */
2442 			size = ip->i_size;
2443 			tiov.iov_len = size;
2444 			tiov.iov_base = kbuf;
2445 			tuio.uio_iov = &tiov;
2446 			tuio.uio_iovcnt = 1;
2447 			tuio.uio_offset = uiop->uio_offset;
2448 			tuio.uio_segflg = UIO_SYSSPACE;
2449 			tuio.uio_fmode = uiop->uio_fmode;
2450 			tuio.uio_extflg = uiop->uio_extflg;
2451 			tuio.uio_limit = uiop->uio_limit;
2452 			tuio.uio_resid = size;
2453 
2454 			basep = tuio.uio_iov->iov_base;
2455 			tuiop = &tuio;
2456 			tflag = 1;
2457 		}
2458 
2459 		error = rdip(ip, tuiop, 0, cr);
2460 		if (!(error == 0 && ip->i_number == ino && ip->i_gen == igen)) {
2461 			rw_exit(&ip->i_contents);
2462 			rw_exit(&ip->i_rwlock);
2463 			goto out;
2464 		}
2465 
2466 		if (tflag == 0)
2467 			size -= uiop->uio_resid;
2468 
2469 		if ((tflag == 0 && ip->i_size <= FSL_SIZE &&
2470 		    ip->i_size == size) || (tflag == 1 &&
2471 		    tuio.uio_resid == 0)) {
2472 			error = kcopy(basep, &ip->i_db[1], ip->i_size);
2473 			if (error == 0) {
2474 				ip->i_flag |= IFASTSYMLNK;
2475 				/*
2476 				 * free page
2477 				 */
2478 				(void) VOP_PUTPAGE(ITOV(ip),
2479 				    (offset_t)0, PAGESIZE,
2480 				    (B_DONTNEED | B_FREE | B_FORCE | B_ASYNC),
2481 				    cr, ct);
2482 			} else {
2483 				int i;
2484 				/* error, clear garbage left behind */
2485 				for (i = 1; i < NDADDR; i++)
2486 					ip->i_db[i] = 0;
2487 				for (i = 0; i < NIADDR; i++)
2488 					ip->i_ib[i] = 0;
2489 			}
2490 		}
2491 		if (tflag == 1) {
2492 			/* now, copy it into the user buffer */
2493 			error = uiomove((caddr_t)kbuf,
2494 			    MIN(size, uiop->uio_resid),
2495 			    UIO_READ, uiop);
2496 		}
2497 		rw_exit(&ip->i_contents);
2498 		rw_exit(&ip->i_rwlock);
2499 	}
2500 out:
2501 	if (ulp) {
2502 		ufs_lockfs_end(ulp);
2503 	}
2504 nolockout:
2505 	return (error);
2506 }
2507 
2508 /* ARGSUSED */
2509 static int
ufs_fsync(struct vnode * vp,int syncflag,struct cred * cr,caller_context_t * ct)2510 ufs_fsync(struct vnode *vp, int syncflag, struct cred *cr, caller_context_t *ct)
2511 {
2512 	struct inode *ip = VTOI(vp);
2513 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
2514 	struct ulockfs *ulp;
2515 	int error;
2516 
2517 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_FSYNC_MASK);
2518 	if (error)
2519 		return (error);
2520 
2521 	if (TRANS_ISTRANS(ufsvfsp)) {
2522 		/*
2523 		 * First push out any data pages
2524 		 */
2525 		if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2526 		    (vp->v_type != VCHR) && !(IS_SWAPVP(vp))) {
2527 			error = VOP_PUTPAGE(vp, (offset_t)0, (size_t)0,
2528 			    0, CRED(), ct);
2529 			if (error)
2530 				goto out;
2531 		}
2532 
2533 		/*
2534 		 * Delta any delayed inode times updates
2535 		 * and push inode to log.
2536 		 * All other inode deltas will have already been delta'd
2537 		 * and will be pushed during the commit.
2538 		 */
2539 		if (!(syncflag & FDSYNC) &&
2540 		    ((ip->i_flag & (IMOD|IMODACC)) == IMODACC)) {
2541 			if (ulp) {
2542 				TRANS_BEGIN_ASYNC(ufsvfsp, TOP_FSYNC,
2543 				    TOP_SYNCIP_SIZE);
2544 			}
2545 			rw_enter(&ip->i_contents, RW_READER);
2546 			mutex_enter(&ip->i_tlock);
2547 			ip->i_flag &= ~IMODTIME;
2548 			mutex_exit(&ip->i_tlock);
2549 			ufs_iupdat(ip, I_SYNC);
2550 			rw_exit(&ip->i_contents);
2551 			if (ulp) {
2552 				TRANS_END_ASYNC(ufsvfsp, TOP_FSYNC,
2553 				    TOP_SYNCIP_SIZE);
2554 			}
2555 		}
2556 
2557 		/*
2558 		 * Commit the Moby transaction
2559 		 *
2560 		 * Deltas have already been made so we just need to
2561 		 * commit them with a synchronous transaction.
2562 		 * TRANS_BEGIN_SYNC() will return an error
2563 		 * if there are no deltas to commit, for an
2564 		 * empty transaction.
2565 		 */
2566 		if (ulp) {
2567 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_FSYNC, TOP_COMMIT_SIZE,
2568 			    error);
2569 			if (error) {
2570 				error = 0; /* commit wasn't needed */
2571 				goto out;
2572 			}
2573 			TRANS_END_SYNC(ufsvfsp, error, TOP_FSYNC,
2574 			    TOP_COMMIT_SIZE);
2575 		}
2576 	} else {	/* not logging */
2577 		if (!(IS_SWAPVP(vp)))
2578 			if (syncflag & FNODSYNC) {
2579 				/* Just update the inode only */
2580 				TRANS_IUPDAT(ip, 1);
2581 				error = 0;
2582 			} else if (syncflag & FDSYNC)
2583 				/* Do data-synchronous writes */
2584 				error = TRANS_SYNCIP(ip, 0, I_DSYNC, TOP_FSYNC);
2585 			else
2586 				/* Do synchronous writes */
2587 				error = TRANS_SYNCIP(ip, 0, I_SYNC, TOP_FSYNC);
2588 
2589 		rw_enter(&ip->i_contents, RW_WRITER);
2590 		if (!error)
2591 			error = ufs_sync_indir(ip);
2592 		rw_exit(&ip->i_contents);
2593 	}
2594 out:
2595 	if (ulp) {
2596 		ufs_lockfs_end(ulp);
2597 	}
2598 	return (error);
2599 }
2600 
2601 /*ARGSUSED*/
2602 static void
ufs_inactive(struct vnode * vp,struct cred * cr,caller_context_t * ct)2603 ufs_inactive(struct vnode *vp, struct cred *cr, caller_context_t *ct)
2604 {
2605 	ufs_iinactive(VTOI(vp));
2606 }
2607 
2608 /*
2609  * Unix file system operations having to do with directory manipulation.
2610  */
2611 int ufs_lookup_idle_count = 2;	/* Number of inodes to idle each time */
2612 /* ARGSUSED */
2613 static int
ufs_lookup(struct vnode * dvp,char * nm,struct vnode ** vpp,struct pathname * pnp,int flags,struct vnode * rdir,struct cred * cr,caller_context_t * ct,int * direntflags,pathname_t * realpnp)2614 ufs_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
2615     struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cr,
2616     caller_context_t *ct, int *direntflags, pathname_t *realpnp)
2617 {
2618 	struct inode *ip;
2619 	struct inode *sip;
2620 	struct inode *xip;
2621 	struct ufsvfs *ufsvfsp;
2622 	struct ulockfs *ulp;
2623 	struct vnode *vp;
2624 	int error;
2625 
2626 	/*
2627 	 * Check flags for type of lookup (regular file or attribute file)
2628 	 */
2629 
2630 	ip = VTOI(dvp);
2631 
2632 	if (flags & LOOKUP_XATTR) {
2633 
2634 		/*
2635 		 * If not mounted with XATTR support then return EINVAL
2636 		 */
2637 
2638 		if (!(ip->i_ufsvfs->vfs_vfs->vfs_flag & VFS_XATTR))
2639 			return (EINVAL);
2640 		/*
2641 		 * We don't allow recursive attributes...
2642 		 * Maybe someday we will.
2643 		 */
2644 		if ((ip->i_cflags & IXATTR)) {
2645 			return (EINVAL);
2646 		}
2647 
2648 		if ((vp = dnlc_lookup(dvp, XATTR_DIR_NAME)) == NULL) {
2649 			error = ufs_xattr_getattrdir(dvp, &sip, flags, cr);
2650 			if (error) {
2651 				*vpp = NULL;
2652 				goto out;
2653 			}
2654 
2655 			vp = ITOV(sip);
2656 			dnlc_update(dvp, XATTR_DIR_NAME, vp);
2657 		}
2658 
2659 		/*
2660 		 * Check accessibility of directory.
2661 		 */
2662 		if (vp == DNLC_NO_VNODE) {
2663 			VN_RELE(vp);
2664 			error = ENOENT;
2665 			goto out;
2666 		}
2667 		if ((error = ufs_iaccess(VTOI(vp), IEXEC, cr, 1)) != 0) {
2668 			VN_RELE(vp);
2669 			goto out;
2670 		}
2671 
2672 		*vpp = vp;
2673 		return (0);
2674 	}
2675 
2676 	/*
2677 	 * Check for a null component, which we should treat as
2678 	 * looking at dvp from within it's parent, so we don't
2679 	 * need a call to ufs_iaccess(), as it has already been
2680 	 * done.
2681 	 */
2682 	if (nm[0] == 0) {
2683 		VN_HOLD(dvp);
2684 		error = 0;
2685 		*vpp = dvp;
2686 		goto out;
2687 	}
2688 
2689 	/*
2690 	 * Check for "." ie itself. this is a quick check and
2691 	 * avoids adding "." into the dnlc (which have been seen
2692 	 * to occupy >10% of the cache).
2693 	 */
2694 	if ((nm[0] == '.') && (nm[1] == 0)) {
2695 		/*
2696 		 * Don't return without checking accessibility
2697 		 * of the directory. We only need the lock if
2698 		 * we are going to return it.
2699 		 */
2700 		if ((error = ufs_iaccess(ip, IEXEC, cr, 1)) == 0) {
2701 			VN_HOLD(dvp);
2702 			*vpp = dvp;
2703 		}
2704 		goto out;
2705 	}
2706 
2707 	/*
2708 	 * Fast path: Check the directory name lookup cache.
2709 	 */
2710 	if (vp = dnlc_lookup(dvp, nm)) {
2711 		/*
2712 		 * Check accessibility of directory.
2713 		 */
2714 		if ((error = ufs_iaccess(ip, IEXEC, cr, 1)) != 0) {
2715 			VN_RELE(vp);
2716 			goto out;
2717 		}
2718 		if (vp == DNLC_NO_VNODE) {
2719 			VN_RELE(vp);
2720 			error = ENOENT;
2721 			goto out;
2722 		}
2723 		xip = VTOI(vp);
2724 		ulp = NULL;
2725 		goto fastpath;
2726 	}
2727 
2728 	/*
2729 	 * Keep the idle queue from getting too long by
2730 	 * idling two inodes before attempting to allocate another.
2731 	 *    This operation must be performed before entering
2732 	 *    lockfs or a transaction.
2733 	 */
2734 	if (ufs_idle_q.uq_ne > ufs_idle_q.uq_hiwat)
2735 		if ((curthread->t_flag & T_DONTBLOCK) == 0) {
2736 			ins.in_lidles.value.ul += ufs_lookup_idle_count;
2737 			ufs_idle_some(ufs_lookup_idle_count);
2738 		}
2739 
2740 retry_lookup:
2741 	/*
2742 	 * Check accessibility of directory.
2743 	 */
2744 	if (error = ufs_diraccess(ip, IEXEC, cr))
2745 		goto out;
2746 
2747 	ufsvfsp = ip->i_ufsvfs;
2748 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LOOKUP_MASK);
2749 	if (error)
2750 		goto out;
2751 
2752 	error = ufs_dirlook(ip, nm, &xip, cr, 1, 0);
2753 
2754 fastpath:
2755 	if (error == 0) {
2756 		ip = xip;
2757 		*vpp = ITOV(ip);
2758 
2759 		/*
2760 		 * If vnode is a device return special vnode instead.
2761 		 */
2762 		if (IS_DEVVP(*vpp)) {
2763 			struct vnode *newvp;
2764 
2765 			newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type,
2766 			    cr);
2767 			VN_RELE(*vpp);
2768 			if (newvp == NULL)
2769 				error = ENOSYS;
2770 			else
2771 				*vpp = newvp;
2772 		} else if (ip->i_cflags & ICOMPRESS) {
2773 			struct vnode *newvp;
2774 
2775 			/*
2776 			 * Compressed file, substitute dcfs vnode
2777 			 */
2778 			newvp = decompvp(*vpp, cr, ct);
2779 			VN_RELE(*vpp);
2780 			if (newvp == NULL)
2781 				error = ENOSYS;
2782 			else
2783 				*vpp = newvp;
2784 		}
2785 	}
2786 	if (ulp) {
2787 		ufs_lockfs_end(ulp);
2788 	}
2789 
2790 	if (error == EAGAIN)
2791 		goto retry_lookup;
2792 
2793 out:
2794 	return (error);
2795 }
2796 
2797 /*ARGSUSED*/
2798 static int
ufs_create(struct vnode * dvp,char * name,struct vattr * vap,enum vcexcl excl,int mode,struct vnode ** vpp,struct cred * cr,int flag,caller_context_t * ct,vsecattr_t * vsecp)2799 ufs_create(struct vnode *dvp, char *name, struct vattr *vap, enum vcexcl excl,
2800     int mode, struct vnode **vpp, struct cred *cr, int flag,
2801     caller_context_t *ct, vsecattr_t *vsecp)
2802 {
2803 	struct inode *ip;
2804 	struct inode *xip;
2805 	struct inode *dip;
2806 	struct vnode *xvp;
2807 	struct ufsvfs *ufsvfsp;
2808 	struct ulockfs *ulp;
2809 	int error;
2810 	int issync;
2811 	int truncflag;
2812 	int trans_size;
2813 	int noentry;
2814 	int defer_dip_seq_update = 0;	/* need to defer update of dip->i_seq */
2815 	int retry = 1;
2816 	int indeadlock;
2817 
2818 again:
2819 	ip = VTOI(dvp);
2820 	ufsvfsp = ip->i_ufsvfs;
2821 	truncflag = 0;
2822 
2823 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_CREATE_MASK);
2824 	if (error)
2825 		goto out;
2826 
2827 	if (ulp) {
2828 		trans_size = (int)TOP_CREATE_SIZE(ip);
2829 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_CREATE, trans_size);
2830 	}
2831 
2832 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr) != 0)
2833 		vap->va_mode &= ~VSVTX;
2834 
2835 	if (*name == '\0') {
2836 		/*
2837 		 * Null component name refers to the directory itself.
2838 		 */
2839 		VN_HOLD(dvp);
2840 		/*
2841 		 * Even though this is an error case, we need to grab the
2842 		 * quota lock since the error handling code below is common.
2843 		 */
2844 		rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
2845 		rw_enter(&ip->i_contents, RW_WRITER);
2846 		error = EEXIST;
2847 	} else {
2848 		xip = NULL;
2849 		noentry = 0;
2850 		/*
2851 		 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
2852 		 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
2853 		 * possible, retries the operation.
2854 		 */
2855 		ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_CREATE,
2856 		    retry_dir);
2857 		if (indeadlock)
2858 			goto again;
2859 
2860 		xvp = dnlc_lookup(dvp, name);
2861 		if (xvp == DNLC_NO_VNODE) {
2862 			noentry = 1;
2863 			VN_RELE(xvp);
2864 			xvp = NULL;
2865 		}
2866 		if (xvp) {
2867 			rw_exit(&ip->i_rwlock);
2868 			if (error = ufs_iaccess(ip, IEXEC, cr, 1)) {
2869 				VN_RELE(xvp);
2870 			} else {
2871 				error = EEXIST;
2872 				xip = VTOI(xvp);
2873 			}
2874 		} else {
2875 			/*
2876 			 * Suppress file system full message if we will retry
2877 			 */
2878 			error = ufs_direnter_cm(ip, name, DE_CREATE,
2879 			    vap, &xip, cr, (noentry | (retry ? IQUIET : 0)));
2880 			if (error == EAGAIN) {
2881 				if (ulp) {
2882 					TRANS_END_CSYNC(ufsvfsp, error, issync,
2883 					    TOP_CREATE, trans_size);
2884 					ufs_lockfs_end(ulp);
2885 				}
2886 				goto again;
2887 			}
2888 			rw_exit(&ip->i_rwlock);
2889 		}
2890 		ip = xip;
2891 		if (ip != NULL) {
2892 			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
2893 			rw_enter(&ip->i_contents, RW_WRITER);
2894 		}
2895 	}
2896 
2897 	/*
2898 	 * If the file already exists and this is a non-exclusive create,
2899 	 * check permissions and allow access for non-directories.
2900 	 * Read-only create of an existing directory is also allowed.
2901 	 * We fail an exclusive create of anything which already exists.
2902 	 */
2903 	if (error == EEXIST) {
2904 		dip = VTOI(dvp);
2905 		if (excl == NONEXCL) {
2906 			if ((((ip->i_mode & IFMT) == IFDIR) ||
2907 			    ((ip->i_mode & IFMT) == IFATTRDIR)) &&
2908 			    (mode & IWRITE))
2909 				error = EISDIR;
2910 			else if (mode)
2911 				error = ufs_iaccess(ip, mode, cr, 0);
2912 			else
2913 				error = 0;
2914 		}
2915 		if (error) {
2916 			rw_exit(&ip->i_contents);
2917 			rw_exit(&ufsvfsp->vfs_dqrwlock);
2918 			VN_RELE(ITOV(ip));
2919 			goto unlock;
2920 		}
2921 		/*
2922 		 * If the error EEXIST was set, then i_seq can not
2923 		 * have been updated. The sequence number interface
2924 		 * is defined such that a non-error VOP_CREATE must
2925 		 * increase the dir va_seq it by at least one. If we
2926 		 * have cleared the error, increase i_seq. Note that
2927 		 * we are increasing the dir i_seq and in rare cases
2928 		 * ip may actually be from the dvp, so we already have
2929 		 * the locks and it will not be subject to truncation.
2930 		 * In case we have to update i_seq of the parent
2931 		 * directory dip, we have to defer it till we have
2932 		 * released our locks on ip due to lock ordering requirements.
2933 		 */
2934 		if (ip != dip)
2935 			defer_dip_seq_update = 1;
2936 		else
2937 			ip->i_seq++;
2938 
2939 		if (((ip->i_mode & IFMT) == IFREG) &&
2940 		    (vap->va_mask & AT_SIZE) && vap->va_size == 0) {
2941 			/*
2942 			 * Truncate regular files, if requested by caller.
2943 			 * Grab i_rwlock to make sure no one else is
2944 			 * currently writing to the file (we promised
2945 			 * bmap we would do this).
2946 			 * Must get the locks in the correct order.
2947 			 */
2948 			if (ip->i_size == 0) {
2949 				ip->i_flag |= ICHG | IUPD;
2950 				ip->i_seq++;
2951 				TRANS_INODE(ufsvfsp, ip);
2952 			} else {
2953 				/*
2954 				 * Large Files: Why this check here?
2955 				 * Though we do it in vn_create() we really
2956 				 * want to guarantee that we do not destroy
2957 				 * Large file data by atomically checking
2958 				 * the size while holding the contents
2959 				 * lock.
2960 				 */
2961 				if (flag && !(flag & FOFFMAX) &&
2962 				    ((ip->i_mode & IFMT) == IFREG) &&
2963 				    (ip->i_size > (offset_t)MAXOFF32_T)) {
2964 					rw_exit(&ip->i_contents);
2965 					rw_exit(&ufsvfsp->vfs_dqrwlock);
2966 					error = EOVERFLOW;
2967 					goto unlock;
2968 				}
2969 				if (TRANS_ISTRANS(ufsvfsp))
2970 					truncflag++;
2971 				else {
2972 					rw_exit(&ip->i_contents);
2973 					rw_exit(&ufsvfsp->vfs_dqrwlock);
2974 					ufs_tryirwlock_trans(&ip->i_rwlock,
2975 					    RW_WRITER, TOP_CREATE,
2976 					    retry_file);
2977 					if (indeadlock) {
2978 						VN_RELE(ITOV(ip));
2979 						goto again;
2980 					}
2981 					rw_enter(&ufsvfsp->vfs_dqrwlock,
2982 					    RW_READER);
2983 					rw_enter(&ip->i_contents, RW_WRITER);
2984 					(void) ufs_itrunc(ip, (u_offset_t)0, 0,
2985 					    cr);
2986 					rw_exit(&ip->i_rwlock);
2987 				}
2988 
2989 			}
2990 			if (error == 0) {
2991 				vnevent_create(ITOV(ip), ct);
2992 			}
2993 		}
2994 	}
2995 
2996 	if (error) {
2997 		if (ip != NULL) {
2998 			rw_exit(&ufsvfsp->vfs_dqrwlock);
2999 			rw_exit(&ip->i_contents);
3000 		}
3001 		goto unlock;
3002 	}
3003 
3004 	*vpp = ITOV(ip);
3005 	ITIMES(ip);
3006 	rw_exit(&ip->i_contents);
3007 	rw_exit(&ufsvfsp->vfs_dqrwlock);
3008 
3009 	/*
3010 	 * If vnode is a device return special vnode instead.
3011 	 */
3012 	if (!error && IS_DEVVP(*vpp)) {
3013 		struct vnode *newvp;
3014 
3015 		newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
3016 		VN_RELE(*vpp);
3017 		if (newvp == NULL) {
3018 			error = ENOSYS;
3019 			goto unlock;
3020 		}
3021 		truncflag = 0;
3022 		*vpp = newvp;
3023 	}
3024 unlock:
3025 
3026 	/*
3027 	 * Do the deferred update of the parent directory's sequence
3028 	 * number now.
3029 	 */
3030 	if (defer_dip_seq_update == 1) {
3031 		rw_enter(&dip->i_contents, RW_READER);
3032 		mutex_enter(&dip->i_tlock);
3033 		dip->i_seq++;
3034 		mutex_exit(&dip->i_tlock);
3035 		rw_exit(&dip->i_contents);
3036 	}
3037 
3038 	if (ulp) {
3039 		int terr = 0;
3040 
3041 		TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_CREATE,
3042 		    trans_size);
3043 
3044 		/*
3045 		 * If we haven't had a more interesting failure
3046 		 * already, then anything that might've happened
3047 		 * here should be reported.
3048 		 */
3049 		if (error == 0)
3050 			error = terr;
3051 	}
3052 
3053 	if (!error && truncflag) {
3054 		ufs_tryirwlock(&ip->i_rwlock, RW_WRITER, retry_trunc);
3055 		if (indeadlock) {
3056 			if (ulp)
3057 				ufs_lockfs_end(ulp);
3058 			VN_RELE(ITOV(ip));
3059 			goto again;
3060 		}
3061 		(void) TRANS_ITRUNC(ip, (u_offset_t)0, 0, cr);
3062 		rw_exit(&ip->i_rwlock);
3063 	}
3064 
3065 	if (ulp)
3066 		ufs_lockfs_end(ulp);
3067 
3068 	/*
3069 	 * If no inodes available, try to free one up out of the
3070 	 * pending delete queue.
3071 	 */
3072 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
3073 		ufs_delete_drain_wait(ufsvfsp, 1);
3074 		retry = 0;
3075 		goto again;
3076 	}
3077 
3078 out:
3079 	return (error);
3080 }
3081 
3082 extern int ufs_idle_max;
3083 /*ARGSUSED*/
3084 static int
ufs_remove(struct vnode * vp,char * nm,struct cred * cr,caller_context_t * ct,int flags)3085 ufs_remove(struct vnode *vp, char *nm, struct cred *cr, caller_context_t *ct,
3086     int flags)
3087 {
3088 	struct inode *ip = VTOI(vp);
3089 	struct ufsvfs *ufsvfsp	= ip->i_ufsvfs;
3090 	struct ulockfs *ulp;
3091 	vnode_t *rmvp = NULL;	/* Vnode corresponding to name being removed */
3092 	int indeadlock;
3093 	int error;
3094 	int issync;
3095 	int trans_size;
3096 
3097 	/*
3098 	 * don't let the delete queue get too long
3099 	 */
3100 	if (ufsvfsp == NULL) {
3101 		error = EIO;
3102 		goto out;
3103 	}
3104 	if (ufsvfsp->vfs_delete.uq_ne > ufs_idle_max)
3105 		ufs_delete_drain(vp->v_vfsp, 1, 1);
3106 
3107 	error = ufs_eventlookup(vp, nm, cr, &rmvp);
3108 	if (rmvp != NULL) {
3109 		/* Only send the event if there were no errors */
3110 		if (error == 0)
3111 			vnevent_remove(rmvp, vp, nm, ct);
3112 		VN_RELE(rmvp);
3113 	}
3114 
3115 retry_remove:
3116 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_REMOVE_MASK);
3117 	if (error)
3118 		goto out;
3119 
3120 	if (ulp)
3121 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_REMOVE,
3122 		    trans_size = (int)TOP_REMOVE_SIZE(VTOI(vp)));
3123 
3124 	/*
3125 	 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
3126 	 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
3127 	 * possible, retries the operation.
3128 	 */
3129 	ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_REMOVE, retry);
3130 	if (indeadlock)
3131 		goto retry_remove;
3132 	error = ufs_dirremove(ip, nm, (struct inode *)0, (struct vnode *)0,
3133 	    DR_REMOVE, cr);
3134 	rw_exit(&ip->i_rwlock);
3135 
3136 	if (ulp) {
3137 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_REMOVE, trans_size);
3138 		ufs_lockfs_end(ulp);
3139 	}
3140 
3141 out:
3142 	return (error);
3143 }
3144 
3145 /*
3146  * Link a file or a directory.  Only privileged processes are allowed to
3147  * make links to directories.
3148  */
3149 /*ARGSUSED*/
3150 static int
ufs_link(struct vnode * tdvp,struct vnode * svp,char * tnm,struct cred * cr,caller_context_t * ct,int flags)3151 ufs_link(struct vnode *tdvp, struct vnode *svp, char *tnm, struct cred *cr,
3152     caller_context_t *ct, int flags)
3153 {
3154 	struct inode *sip;
3155 	struct inode *tdp = VTOI(tdvp);
3156 	struct ufsvfs *ufsvfsp = tdp->i_ufsvfs;
3157 	struct ulockfs *ulp;
3158 	struct vnode *realvp;
3159 	int error;
3160 	int issync;
3161 	int trans_size;
3162 	int isdev;
3163 	int indeadlock;
3164 
3165 retry_link:
3166 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LINK_MASK);
3167 	if (error)
3168 		goto out;
3169 
3170 	if (ulp)
3171 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_LINK,
3172 		    trans_size = (int)TOP_LINK_SIZE(VTOI(tdvp)));
3173 
3174 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3175 		svp = realvp;
3176 
3177 	/*
3178 	 * Make sure link for extended attributes is valid
3179 	 * We only support hard linking of attr in ATTRDIR to ATTRDIR
3180 	 *
3181 	 * Make certain we don't attempt to look at a device node as
3182 	 * a ufs inode.
3183 	 */
3184 
3185 	isdev = IS_DEVVP(svp);
3186 	if (((isdev == 0) && ((VTOI(svp)->i_cflags & IXATTR) == 0) &&
3187 	    ((tdp->i_mode & IFMT) == IFATTRDIR)) ||
3188 	    ((isdev == 0) && (VTOI(svp)->i_cflags & IXATTR) &&
3189 	    ((tdp->i_mode & IFMT) == IFDIR))) {
3190 		error = EINVAL;
3191 		goto unlock;
3192 	}
3193 
3194 	sip = VTOI(svp);
3195 	if ((svp->v_type == VDIR &&
3196 	    secpolicy_fs_linkdir(cr, ufsvfsp->vfs_vfs) != 0) ||
3197 	    (sip->i_uid != crgetuid(cr) && secpolicy_basic_link(cr) != 0)) {
3198 		error = EPERM;
3199 		goto unlock;
3200 	}
3201 
3202 	/*
3203 	 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
3204 	 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
3205 	 * possible, retries the operation.
3206 	 */
3207 	ufs_tryirwlock_trans(&tdp->i_rwlock, RW_WRITER, TOP_LINK, retry);
3208 	if (indeadlock)
3209 		goto retry_link;
3210 	error = ufs_direnter_lr(tdp, tnm, DE_LINK, (struct inode *)0,
3211 	    sip, cr);
3212 	rw_exit(&tdp->i_rwlock);
3213 
3214 unlock:
3215 	if (ulp) {
3216 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_LINK, trans_size);
3217 		ufs_lockfs_end(ulp);
3218 	}
3219 
3220 	if (!error) {
3221 		vnevent_link(svp, ct);
3222 	}
3223 out:
3224 	return (error);
3225 }
3226 
3227 uint64_t ufs_rename_retry_cnt;
3228 uint64_t ufs_rename_upgrade_retry_cnt;
3229 uint64_t ufs_rename_dircheck_retry_cnt;
3230 clock_t	 ufs_rename_backoff_delay = 1;
3231 
3232 /*
3233  * Rename a file or directory.
3234  * We are given the vnode and entry string of the source and the
3235  * vnode and entry string of the place we want to move the source
3236  * to (the target). The essential operation is:
3237  *	unlink(target);
3238  *	link(source, target);
3239  *	unlink(source);
3240  * but "atomically".  Can't do full commit without saving state in
3241  * the inode on disk, which isn't feasible at this time.  Best we
3242  * can do is always guarantee that the TARGET exists.
3243  */
3244 
3245 /*ARGSUSED*/
3246 static int
ufs_rename(struct vnode * sdvp,char * snm,struct vnode * tdvp,char * tnm,struct cred * cr,caller_context_t * ct,int flags)3247 ufs_rename(struct vnode *sdvp, char *snm, struct vnode *tdvp, char *tnm,
3248     struct cred *cr, caller_context_t *ct, int flags)
3249 {
3250 	struct inode *sip = NULL;	/* source inode */
3251 	struct inode *ip = NULL;	/* check inode */
3252 	struct inode *sdp;		/* old (source) parent inode */
3253 	struct inode *tdp;		/* new (target) parent inode */
3254 	struct vnode *svp = NULL;	/* source vnode */
3255 	struct vnode *tvp = NULL;	/* target vnode, if it exists */
3256 	struct vnode *realvp;
3257 	struct ufsvfs *ufsvfsp;
3258 	struct ulockfs *ulp = NULL;
3259 	struct ufs_slot slot;
3260 	timestruc_t now;
3261 	int error;
3262 	int issync;
3263 	int trans_size;
3264 	krwlock_t *first_lock;
3265 	krwlock_t *second_lock;
3266 	krwlock_t *reverse_lock;
3267 	int serr, terr;
3268 
3269 	sdp = VTOI(sdvp);
3270 	slot.fbp = NULL;
3271 	ufsvfsp = sdp->i_ufsvfs;
3272 
3273 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3274 		tdvp = realvp;
3275 
3276 	/* Must do this before taking locks in case of DNLC miss */
3277 	terr = ufs_eventlookup(tdvp, tnm, cr, &tvp);
3278 	serr = ufs_eventlookup(sdvp, snm, cr, &svp);
3279 
3280 	if ((serr == 0) && ((terr == 0) || (terr == ENOENT))) {
3281 		if (tvp != NULL)
3282 			vnevent_pre_rename_dest(tvp, tdvp, tnm, ct);
3283 
3284 		/*
3285 		 * Notify the target directory of the rename event
3286 		 * if source and target directories are not the same.
3287 		 */
3288 		if (sdvp != tdvp)
3289 			vnevent_pre_rename_dest_dir(tdvp, svp, tnm, ct);
3290 
3291 		if (svp != NULL)
3292 			vnevent_pre_rename_src(svp, sdvp, snm, ct);
3293 	}
3294 
3295 	if (svp != NULL)
3296 		VN_RELE(svp);
3297 
3298 retry_rename:
3299 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_RENAME_MASK);
3300 	if (error)
3301 		goto unlock;
3302 
3303 	if (ulp)
3304 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_RENAME,
3305 		    trans_size = (int)TOP_RENAME_SIZE(sdp));
3306 
3307 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3308 		tdvp = realvp;
3309 
3310 	tdp = VTOI(tdvp);
3311 
3312 	/*
3313 	 * We only allow renaming of attributes from ATTRDIR to ATTRDIR.
3314 	 */
3315 	if ((tdp->i_mode & IFMT) != (sdp->i_mode & IFMT)) {
3316 		error = EINVAL;
3317 		goto unlock;
3318 	}
3319 
3320 	/*
3321 	 * Check accessibility of directory.
3322 	 */
3323 	if (error = ufs_diraccess(sdp, IEXEC, cr))
3324 		goto unlock;
3325 
3326 	/*
3327 	 * Look up inode of file we're supposed to rename.
3328 	 */
3329 	gethrestime(&now);
3330 	if (error = ufs_dirlook(sdp, snm, &sip, cr, 0, 0)) {
3331 		if (error == EAGAIN) {
3332 			if (ulp) {
3333 				TRANS_END_CSYNC(ufsvfsp, error, issync,
3334 				    TOP_RENAME, trans_size);
3335 				ufs_lockfs_end(ulp);
3336 			}
3337 			goto retry_rename;
3338 		}
3339 
3340 		goto unlock;
3341 	}
3342 
3343 	/*
3344 	 * Lock both the source and target directories (they may be
3345 	 * the same) to provide the atomicity semantics that was
3346 	 * previously provided by the per file system vfs_rename_lock
3347 	 *
3348 	 * with vfs_rename_lock removed to allow simultaneous renames
3349 	 * within a file system, ufs_dircheckpath can deadlock while
3350 	 * traversing back to ensure that source is not a parent directory
3351 	 * of target parent directory. This is because we get into
3352 	 * ufs_dircheckpath with the sdp and tdp locks held as RW_WRITER.
3353 	 * If the tdp and sdp of the simultaneous renames happen to be
3354 	 * in the path of each other, it can lead to a deadlock. This
3355 	 * can be avoided by getting the locks as RW_READER here and then
3356 	 * upgrading to RW_WRITER after completing the ufs_dircheckpath.
3357 	 *
3358 	 * We hold the target directory's i_rwlock after calling
3359 	 * ufs_lockfs_begin but in many other operations (like ufs_readdir)
3360 	 * VOP_RWLOCK is explicitly called by the filesystem independent code
3361 	 * before calling the file system operation. In these cases the order
3362 	 * is reversed (i.e i_rwlock is taken first and then ufs_lockfs_begin
3363 	 * is called). This is fine as long as ufs_lockfs_begin acts as a VOP
3364 	 * counter but with ufs_quiesce setting the SLOCK bit this becomes a
3365 	 * synchronizing object which might lead to a deadlock. So we use
3366 	 * rw_tryenter instead of rw_enter. If we fail to get this lock and
3367 	 * find that SLOCK bit is set, we call ufs_lockfs_end and restart the
3368 	 * operation.
3369 	 */
3370 retry:
3371 	first_lock = &tdp->i_rwlock;
3372 	second_lock = &sdp->i_rwlock;
3373 retry_firstlock:
3374 	if (!rw_tryenter(first_lock, RW_READER)) {
3375 		/*
3376 		 * We didn't get the lock. Check if the SLOCK is set in the
3377 		 * ufsvfs. If yes, we might be in a deadlock. Safer to give up
3378 		 * and wait for SLOCK to be cleared.
3379 		 */
3380 
3381 		if (ulp && ULOCKFS_IS_SLOCK(ulp)) {
3382 			TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME,
3383 			    trans_size);
3384 			ufs_lockfs_end(ulp);
3385 			goto retry_rename;
3386 
3387 		} else {
3388 			/*
3389 			 * SLOCK isn't set so this is a genuine synchronization
3390 			 * case. Let's try again after giving them a breather.
3391 			 */
3392 			delay(RETRY_LOCK_DELAY);
3393 			goto  retry_firstlock;
3394 		}
3395 	}
3396 	/*
3397 	 * Need to check if the tdp and sdp are same !!!
3398 	 */
3399 	if ((tdp != sdp) && (!rw_tryenter(second_lock, RW_READER))) {
3400 		/*
3401 		 * We didn't get the lock. Check if the SLOCK is set in the
3402 		 * ufsvfs. If yes, we might be in a deadlock. Safer to give up
3403 		 * and wait for SLOCK to be cleared.
3404 		 */
3405 
3406 		rw_exit(first_lock);
3407 		if (ulp && ULOCKFS_IS_SLOCK(ulp)) {
3408 			TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME,
3409 			    trans_size);
3410 			ufs_lockfs_end(ulp);
3411 			goto retry_rename;
3412 
3413 		} else {
3414 			/*
3415 			 * So we couldn't get the second level peer lock *and*
3416 			 * the SLOCK bit isn't set. Too bad we can be
3417 			 * contentding with someone wanting these locks otherway
3418 			 * round. Reverse the locks in case there is a heavy
3419 			 * contention for the second level lock.
3420 			 */
3421 			reverse_lock = first_lock;
3422 			first_lock = second_lock;
3423 			second_lock = reverse_lock;
3424 			ufs_rename_retry_cnt++;
3425 			goto  retry_firstlock;
3426 		}
3427 	}
3428 
3429 	if (sip == tdp) {
3430 		error = EINVAL;
3431 		goto errout;
3432 	}
3433 	/*
3434 	 * Make sure we can delete the source entry.  This requires
3435 	 * write permission on the containing directory.
3436 	 * Check for sticky directories.
3437 	 */
3438 	rw_enter(&sdp->i_contents, RW_READER);
3439 	rw_enter(&sip->i_contents, RW_READER);
3440 	if ((error = ufs_iaccess(sdp, IWRITE, cr, 0)) != 0 ||
3441 	    (error = ufs_sticky_remove_access(sdp, sip, cr)) != 0) {
3442 		rw_exit(&sip->i_contents);
3443 		rw_exit(&sdp->i_contents);
3444 		goto errout;
3445 	}
3446 
3447 	/*
3448 	 * If this is a rename of a directory and the parent is
3449 	 * different (".." must be changed), then the source
3450 	 * directory must not be in the directory hierarchy
3451 	 * above the target, as this would orphan everything
3452 	 * below the source directory.  Also the user must have
3453 	 * write permission in the source so as to be able to
3454 	 * change "..".
3455 	 */
3456 	if ((((sip->i_mode & IFMT) == IFDIR) ||
3457 	    ((sip->i_mode & IFMT) == IFATTRDIR)) && sdp != tdp) {
3458 		ino_t	inum;
3459 
3460 		if (error = ufs_iaccess(sip, IWRITE, cr, 0)) {
3461 			rw_exit(&sip->i_contents);
3462 			rw_exit(&sdp->i_contents);
3463 			goto errout;
3464 		}
3465 		inum = sip->i_number;
3466 		rw_exit(&sip->i_contents);
3467 		rw_exit(&sdp->i_contents);
3468 		if ((error = ufs_dircheckpath(inum, tdp, sdp,