xref: /titanic_41/usr/src/uts/common/syscall/fcntl.c (revision 0cfdb6036e046270988a17ac442e4d717d426a44)
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 /* ONC_PLUS EXTRACT START */
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 /* ONC_PLUS EXTRACT END */
37 
38 #include <sys/param.h>
39 #include <sys/isa_defs.h>
40 #include <sys/types.h>
41 #include <sys/sysmacros.h>
42 #include <sys/systm.h>
43 #include <sys/errno.h>
44 #include <sys/fcntl.h>
45 /* ONC_PLUS EXTRACT START */
46 #include <sys/flock.h>
47 /* ONC_PLUS EXTRACT END */
48 #include <sys/vnode.h>
49 #include <sys/file.h>
50 #include <sys/mode.h>
51 #include <sys/proc.h>
52 #include <sys/filio.h>
53 #include <sys/share.h>
54 #include <sys/debug.h>
55 #include <sys/rctl.h>
56 #include <sys/nbmlock.h>
57 
58 #include <sys/cmn_err.h>
59 
60 /* ONC_PLUS EXTRACT START */
61 static int flock_check(vnode_t *, flock64_t *, offset_t, offset_t);
62 static int flock_get_start(vnode_t *, flock64_t *, offset_t, u_offset_t *);
63 static void fd_too_big(proc_t *);
64 
65 /*
66  * File control.
67  */
68 int
69 fcntl(int fdes, int cmd, intptr_t arg)
70 {
71 	int iarg;
72 	int error = 0;
73 	int retval;
74 	proc_t *p;
75 	file_t *fp;
76 	vnode_t *vp;
77 	u_offset_t offset;
78 	u_offset_t start;
79 	struct vattr vattr;
80 	int in_crit;
81 	int flag;
82 	struct flock sbf;
83 	struct flock64 bf;
84 	struct o_flock obf;
85 	struct flock64_32 bf64_32;
86 	struct fshare fsh;
87 	struct shrlock shr;
88 	struct shr_locowner shr_own;
89 	offset_t maxoffset;
90 	model_t datamodel;
91 	int fdres;
92 
93 #if defined(_ILP32) && !defined(lint) && defined(_SYSCALL32)
94 	ASSERT(sizeof (struct flock) == sizeof (struct flock32));
95 	ASSERT(sizeof (struct flock64) == sizeof (struct flock64_32));
96 #endif
97 #if defined(_LP64) && !defined(lint) && defined(_SYSCALL32)
98 	ASSERT(sizeof (struct flock) == sizeof (struct flock64_64));
99 	ASSERT(sizeof (struct flock64) == sizeof (struct flock64_64));
100 #endif
101 
102 	/*
103 	 * First, for speed, deal with the subset of cases
104 	 * that do not require getf() / releasef().
105 	 */
106 	switch (cmd) {
107 	case F_GETFD:
108 		if ((error = f_getfd_error(fdes, &flag)) == 0)
109 			retval = flag;
110 		goto out;
111 
112 	case F_SETFD:
113 		error = f_setfd_error(fdes, (int)arg);
114 		retval = 0;
115 		goto out;
116 
117 	case F_GETFL:
118 		if ((error = f_getfl(fdes, &flag)) == 0)
119 			retval = (flag & (FMASK | FASYNC)) + FOPEN;
120 		goto out;
121 
122 	case F_GETXFL:
123 		if ((error = f_getfl(fdes, &flag)) == 0)
124 			retval = flag + FOPEN;
125 		goto out;
126 
127 	case F_BADFD:
128 		if ((error = f_badfd(fdes, &fdres, (int)arg)) == 0)
129 			retval = fdres;
130 		goto out;
131 	}
132 
133 	/*
134 	 * Second, for speed, deal with the subset of cases that
135 	 * require getf() / releasef() but do not require copyin.
136 	 */
137 	if ((fp = getf(fdes)) == NULL) {
138 		error = EBADF;
139 		goto out;
140 	}
141 	iarg = (int)arg;
142 
143 	switch (cmd) {
144 /* ONC_PLUS EXTRACT END */
145 
146 	case F_DUPFD:
147 		p = curproc;
148 		if ((uint_t)iarg >= p->p_fno_ctl) {
149 			if (iarg >= 0)
150 				fd_too_big(p);
151 			error = EINVAL;
152 		} else if ((retval = ufalloc_file(iarg, fp)) == -1) {
153 			error = EMFILE;
154 		} else {
155 			mutex_enter(&fp->f_tlock);
156 			fp->f_count++;
157 			mutex_exit(&fp->f_tlock);
158 		}
159 		goto done;
160 
161 	case F_DUP2FD:
162 		p = curproc;
163 		if (fdes == iarg) {
164 			retval = iarg;
165 		} else if ((uint_t)iarg >= p->p_fno_ctl) {
166 			if (iarg >= 0)
167 				fd_too_big(p);
168 			error = EBADF;
169 		} else {
170 			/*
171 			 * We can't hold our getf(fdes) across the call to
172 			 * closeandsetf() because it creates a window for
173 			 * deadlock: if one thread is doing dup2(a, b) while
174 			 * another is doing dup2(b, a), each one will block
175 			 * waiting for the other to call releasef().  The
176 			 * solution is to increment the file reference count
177 			 * (which we have to do anyway), then releasef(fdes),
178 			 * then closeandsetf().  Incrementing f_count ensures
179 			 * that fp won't disappear after we call releasef().
180 			 * When closeandsetf() fails, we try avoid calling
181 			 * closef() because of all the side effects.
182 			 */
183 			mutex_enter(&fp->f_tlock);
184 			fp->f_count++;
185 			mutex_exit(&fp->f_tlock);
186 			releasef(fdes);
187 			if ((error = closeandsetf(iarg, fp)) == 0) {
188 				retval = iarg;
189 			} else {
190 				mutex_enter(&fp->f_tlock);
191 				if (fp->f_count > 1) {
192 					fp->f_count--;
193 					mutex_exit(&fp->f_tlock);
194 				} else {
195 					mutex_exit(&fp->f_tlock);
196 					(void) closef(fp);
197 				}
198 			}
199 			goto out;
200 		}
201 		goto done;
202 
203 	case F_SETFL:
204 		vp = fp->f_vnode;
205 		flag = fp->f_flag;
206 		if ((iarg & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY))
207 			iarg &= ~FNDELAY;
208 		if ((error = VOP_SETFL(vp, flag, iarg, fp->f_cred, NULL)) ==
209 		    0) {
210 			iarg &= FMASK;
211 			mutex_enter(&fp->f_tlock);
212 			fp->f_flag &= ~FMASK | (FREAD|FWRITE);
213 			fp->f_flag |= (iarg - FOPEN) & ~(FREAD|FWRITE);
214 			mutex_exit(&fp->f_tlock);
215 		}
216 		retval = 0;
217 		goto done;
218 	}
219 
220 	/*
221 	 * Finally, deal with the expensive cases.
222 	 */
223 	retval = 0;
224 	in_crit = 0;
225 	maxoffset = MAXOFF_T;
226 	datamodel = DATAMODEL_NATIVE;
227 #if defined(_SYSCALL32_IMPL)
228 	if ((datamodel = get_udatamodel()) == DATAMODEL_ILP32)
229 		maxoffset = MAXOFF32_T;
230 #endif
231 
232 	vp = fp->f_vnode;
233 	flag = fp->f_flag;
234 	offset = fp->f_offset;
235 
236 	switch (cmd) {
237 /* ONC_PLUS EXTRACT START */
238 	/*
239 	 * The file system and vnode layers understand and implement
240 	 * locking with flock64 structures. So here once we pass through
241 	 * the test for compatibility as defined by LFS API, (for F_SETLK,
242 	 * F_SETLKW, F_GETLK, F_GETLKW, F_FREESP) we transform
243 	 * the flock structure to a flock64 structure and send it to the
244 	 * lower layers. Similarly in case of GETLK the returned flock64
245 	 * structure is transformed to a flock structure if everything fits
246 	 * in nicely, otherwise we return EOVERFLOW.
247 	 */
248 
249 	case F_GETLK:
250 	case F_O_GETLK:
251 	case F_SETLK:
252 	case F_SETLKW:
253 	case F_SETLK_NBMAND:
254 
255 		/*
256 		 * Copy in input fields only.
257 		 */
258 
259 		if (cmd == F_O_GETLK) {
260 			if (datamodel != DATAMODEL_ILP32) {
261 				error = EINVAL;
262 				break;
263 			}
264 
265 			if (copyin((void *)arg, &obf, sizeof (obf))) {
266 				error = EFAULT;
267 				break;
268 			}
269 			bf.l_type = obf.l_type;
270 			bf.l_whence = obf.l_whence;
271 			bf.l_start = (off64_t)obf.l_start;
272 			bf.l_len = (off64_t)obf.l_len;
273 			bf.l_sysid = (int)obf.l_sysid;
274 			bf.l_pid = obf.l_pid;
275 		} else if (datamodel == DATAMODEL_NATIVE) {
276 			if (copyin((void *)arg, &sbf, sizeof (sbf))) {
277 				error = EFAULT;
278 				break;
279 			}
280 			/*
281 			 * XXX	In an LP64 kernel with an LP64 application
282 			 *	there's no need to do a structure copy here
283 			 *	struct flock == struct flock64. However,
284 			 *	we did it this way to avoid more conditional
285 			 *	compilation.
286 			 */
287 			bf.l_type = sbf.l_type;
288 			bf.l_whence = sbf.l_whence;
289 			bf.l_start = (off64_t)sbf.l_start;
290 			bf.l_len = (off64_t)sbf.l_len;
291 			bf.l_sysid = sbf.l_sysid;
292 			bf.l_pid = sbf.l_pid;
293 		}
294 #if defined(_SYSCALL32_IMPL)
295 		else {
296 			struct flock32 sbf32;
297 			if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
298 				error = EFAULT;
299 				break;
300 			}
301 			bf.l_type = sbf32.l_type;
302 			bf.l_whence = sbf32.l_whence;
303 			bf.l_start = (off64_t)sbf32.l_start;
304 			bf.l_len = (off64_t)sbf32.l_len;
305 			bf.l_sysid = sbf32.l_sysid;
306 			bf.l_pid = sbf32.l_pid;
307 		}
308 #endif /* _SYSCALL32_IMPL */
309 
310 		/*
311 		 * 64-bit support: check for overflow for 32-bit lock ops
312 		 */
313 		if ((error = flock_check(vp, &bf, offset, maxoffset)) != 0)
314 			break;
315 
316 		/*
317 		 * Not all of the filesystems understand F_O_GETLK, and
318 		 * there's no need for them to know.  Map it to F_GETLK.
319 		 */
320 		if ((error = VOP_FRLOCK(vp, (cmd == F_O_GETLK) ? F_GETLK : cmd,
321 		    &bf, flag, offset, NULL, fp->f_cred, NULL)) != 0)
322 			break;
323 
324 		/*
325 		 * If command is GETLK and no lock is found, only
326 		 * the type field is changed.
327 		 */
328 		if ((cmd == F_O_GETLK || cmd == F_GETLK) &&
329 		    bf.l_type == F_UNLCK) {
330 			/* l_type always first entry, always a short */
331 			if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
332 			    sizeof (bf.l_type)))
333 				error = EFAULT;
334 			break;
335 		}
336 
337 		if (cmd == F_O_GETLK) {
338 			/*
339 			 * Return an SVR3 flock structure to the user.
340 			 */
341 			obf.l_type = (int16_t)bf.l_type;
342 			obf.l_whence = (int16_t)bf.l_whence;
343 			obf.l_start = (int32_t)bf.l_start;
344 			obf.l_len = (int32_t)bf.l_len;
345 			if (bf.l_sysid > SHRT_MAX || bf.l_pid > SHRT_MAX) {
346 				/*
347 				 * One or both values for the above fields
348 				 * is too large to store in an SVR3 flock
349 				 * structure.
350 				 */
351 				error = EOVERFLOW;
352 				break;
353 			}
354 			obf.l_sysid = (int16_t)bf.l_sysid;
355 			obf.l_pid = (int16_t)bf.l_pid;
356 			if (copyout(&obf, (void *)arg, sizeof (obf)))
357 				error = EFAULT;
358 		} else if (cmd == F_GETLK) {
359 			/*
360 			 * Copy out SVR4 flock.
361 			 */
362 			int i;
363 
364 			if (bf.l_start > maxoffset || bf.l_len > maxoffset) {
365 				error = EOVERFLOW;
366 				break;
367 			}
368 
369 			if (datamodel == DATAMODEL_NATIVE) {
370 				for (i = 0; i < 4; i++)
371 					sbf.l_pad[i] = 0;
372 				/*
373 				 * XXX	In an LP64 kernel with an LP64
374 				 *	application there's no need to do a
375 				 *	structure copy here as currently
376 				 *	struct flock == struct flock64.
377 				 *	We did it this way to avoid more
378 				 *	conditional compilation.
379 				 */
380 				sbf.l_type = bf.l_type;
381 				sbf.l_whence = bf.l_whence;
382 				sbf.l_start = (off_t)bf.l_start;
383 				sbf.l_len = (off_t)bf.l_len;
384 				sbf.l_sysid = bf.l_sysid;
385 				sbf.l_pid = bf.l_pid;
386 				if (copyout(&sbf, (void *)arg, sizeof (sbf)))
387 					error = EFAULT;
388 			}
389 #if defined(_SYSCALL32_IMPL)
390 			else {
391 				struct flock32 sbf32;
392 				if (bf.l_start > MAXOFF32_T ||
393 				    bf.l_len > MAXOFF32_T) {
394 					error = EOVERFLOW;
395 					break;
396 				}
397 				for (i = 0; i < 4; i++)
398 					sbf32.l_pad[i] = 0;
399 				sbf32.l_type = (int16_t)bf.l_type;
400 				sbf32.l_whence = (int16_t)bf.l_whence;
401 				sbf32.l_start = (off32_t)bf.l_start;
402 				sbf32.l_len = (off32_t)bf.l_len;
403 				sbf32.l_sysid = (int32_t)bf.l_sysid;
404 				sbf32.l_pid = (pid32_t)bf.l_pid;
405 				if (copyout(&sbf32,
406 				    (void *)arg, sizeof (sbf32)))
407 					error = EFAULT;
408 			}
409 #endif
410 		}
411 		break;
412 /* ONC_PLUS EXTRACT END */
413 
414 	case F_CHKFL:
415 		/*
416 		 * This is for internal use only, to allow the vnode layer
417 		 * to validate a flags setting before applying it.  User
418 		 * programs can't issue it.
419 		 */
420 		error = EINVAL;
421 		break;
422 
423 	case F_ALLOCSP:
424 	case F_FREESP:
425 	case F_ALLOCSP64:
426 	case F_FREESP64:
427 		if ((flag & FWRITE) == 0) {
428 			error = EBADF;
429 			break;
430 		}
431 
432 		if (vp->v_type != VREG) {
433 			error = EINVAL;
434 			break;
435 		}
436 
437 		if (datamodel != DATAMODEL_ILP32 &&
438 		    (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
439 			error = EINVAL;
440 			break;
441 		}
442 
443 #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
444 		if (datamodel == DATAMODEL_ILP32 &&
445 		    (cmd == F_ALLOCSP || cmd == F_FREESP)) {
446 			struct flock32 sbf32;
447 			/*
448 			 * For compatibility we overlay an SVR3 flock on an SVR4
449 			 * flock.  This works because the input field offsets
450 			 * in "struct flock" were preserved.
451 			 */
452 			if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
453 				error = EFAULT;
454 				break;
455 			} else {
456 				bf.l_type = sbf32.l_type;
457 				bf.l_whence = sbf32.l_whence;
458 				bf.l_start = (off64_t)sbf32.l_start;
459 				bf.l_len = (off64_t)sbf32.l_len;
460 				bf.l_sysid = sbf32.l_sysid;
461 				bf.l_pid = sbf32.l_pid;
462 			}
463 		}
464 #endif /* _ILP32 || _SYSCALL32_IMPL */
465 
466 #if defined(_LP64)
467 		if (datamodel == DATAMODEL_LP64 &&
468 		    (cmd == F_ALLOCSP || cmd == F_FREESP)) {
469 			if (copyin((void *)arg, &bf, sizeof (bf))) {
470 				error = EFAULT;
471 				break;
472 			}
473 		}
474 #endif /* defined(_LP64) */
475 
476 #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
477 		if (datamodel == DATAMODEL_ILP32 &&
478 		    (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
479 			if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
480 				error = EFAULT;
481 				break;
482 			} else {
483 				/*
484 				 * Note that the size of flock64 is different in
485 				 * the ILP32 and LP64 models, due to the l_pad
486 				 * field. We do not want to assume that the
487 				 * flock64 structure is laid out the same in
488 				 * ILP32 and LP64 environments, so we will
489 				 * copy in the ILP32 version of flock64
490 				 * explicitly and copy it to the native
491 				 * flock64 structure.
492 				 */
493 				bf.l_type = (short)bf64_32.l_type;
494 				bf.l_whence = (short)bf64_32.l_whence;
495 				bf.l_start = bf64_32.l_start;
496 				bf.l_len = bf64_32.l_len;
497 				bf.l_sysid = (int)bf64_32.l_sysid;
498 				bf.l_pid = (pid_t)bf64_32.l_pid;
499 			}
500 		}
501 #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
502 
503 		if (cmd == F_ALLOCSP || cmd == F_FREESP)
504 			error = flock_check(vp, &bf, offset, maxoffset);
505 		else if (cmd == F_ALLOCSP64 || cmd == F_FREESP64)
506 			error = flock_check(vp, &bf, offset, MAXOFFSET_T);
507 		if (error)
508 			break;
509 
510 		if (vp->v_type == VREG && bf.l_len == 0 &&
511 		    bf.l_start > OFFSET_MAX(fp)) {
512 			error = EFBIG;
513 			break;
514 		}
515 
516 		/*
517 		 * Make sure that there are no conflicting non-blocking
518 		 * mandatory locks in the region being manipulated. If
519 		 * there are such locks then return EACCES.
520 		 */
521 		if ((error = flock_get_start(vp, &bf, offset, &start)) != 0)
522 			break;
523 
524 		if (nbl_need_check(vp)) {
525 			u_offset_t	begin;
526 			ssize_t		length;
527 
528 			nbl_start_crit(vp, RW_READER);
529 			in_crit = 1;
530 			vattr.va_mask = AT_SIZE;
531 			if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
532 			    != 0)
533 				break;
534 			begin = start > vattr.va_size ? vattr.va_size : start;
535 			length = vattr.va_size > start ? vattr.va_size - start :
536 				start - vattr.va_size;
537 			if (nbl_conflict(vp, NBL_WRITE, begin, length, 0,
538 			    NULL)) {
539 				error = EACCES;
540 				break;
541 			}
542 		}
543 
544 		if (cmd == F_ALLOCSP64)
545 			cmd = F_ALLOCSP;
546 		else if (cmd == F_FREESP64)
547 			cmd = F_FREESP;
548 
549 		error = VOP_SPACE(vp, cmd, &bf, flag, offset, fp->f_cred, NULL);
550 
551 		break;
552 
553 #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
554 /* ONC_PLUS EXTRACT START */
555 	case F_GETLK64:
556 	case F_SETLK64:
557 	case F_SETLKW64:
558 	case F_SETLK64_NBMAND:
559 		/*
560 		 * Large Files: Here we set cmd as *LK and send it to
561 		 * lower layers. *LK64 is only for the user land.
562 		 * Most of the comments described above for F_SETLK
563 		 * applies here too.
564 		 * Large File support is only needed for ILP32 apps!
565 		 */
566 		if (datamodel != DATAMODEL_ILP32) {
567 			error = EINVAL;
568 			break;
569 		}
570 
571 		if (cmd == F_GETLK64)
572 			cmd = F_GETLK;
573 		else if (cmd == F_SETLK64)
574 			cmd = F_SETLK;
575 		else if (cmd == F_SETLKW64)
576 			cmd = F_SETLKW;
577 		else if (cmd == F_SETLK64_NBMAND)
578 			cmd = F_SETLK_NBMAND;
579 
580 		/*
581 		 * Note that the size of flock64 is different in the ILP32
582 		 * and LP64 models, due to the sucking l_pad field.
583 		 * We do not want to assume that the flock64 structure is
584 		 * laid out in the same in ILP32 and LP64 environments, so
585 		 * we will copy in the ILP32 version of flock64 explicitly
586 		 * and copy it to the native flock64 structure.
587 		 */
588 
589 		if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
590 			error = EFAULT;
591 			break;
592 		}
593 
594 		bf.l_type = (short)bf64_32.l_type;
595 		bf.l_whence = (short)bf64_32.l_whence;
596 		bf.l_start = bf64_32.l_start;
597 		bf.l_len = bf64_32.l_len;
598 		bf.l_sysid = (int)bf64_32.l_sysid;
599 		bf.l_pid = (pid_t)bf64_32.l_pid;
600 
601 		if ((error = flock_check(vp, &bf, offset, MAXOFFSET_T)) != 0)
602 			break;
603 
604 		if ((error = VOP_FRLOCK(vp, cmd, &bf, flag, offset,
605 		    NULL, fp->f_cred, NULL)) != 0)
606 			break;
607 
608 		if ((cmd == F_GETLK) && bf.l_type == F_UNLCK) {
609 			if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
610 			    sizeof (bf.l_type)))
611 				error = EFAULT;
612 			break;
613 		}
614 
615 		if (cmd == F_GETLK) {
616 			int i;
617 
618 			/*
619 			 * We do not want to assume that the flock64 structure
620 			 * is laid out in the same in ILP32 and LP64
621 			 * environments, so we will copy out the ILP32 version
622 			 * of flock64 explicitly after copying the native
623 			 * flock64 structure to it.
624 			 */
625 			for (i = 0; i < 4; i++)
626 				bf64_32.l_pad[i] = 0;
627 			bf64_32.l_type = (int16_t)bf.l_type;
628 			bf64_32.l_whence = (int16_t)bf.l_whence;
629 			bf64_32.l_start = bf.l_start;
630 			bf64_32.l_len = bf.l_len;
631 			bf64_32.l_sysid = (int32_t)bf.l_sysid;
632 			bf64_32.l_pid = (pid32_t)bf.l_pid;
633 			if (copyout(&bf64_32, (void *)arg, sizeof (bf64_32)))
634 				error = EFAULT;
635 		}
636 		break;
637 /* ONC_PLUS EXTRACT END */
638 #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
639 
640 /* ONC_PLUS EXTRACT START */
641 	case F_SHARE:
642 	case F_SHARE_NBMAND:
643 	case F_UNSHARE:
644 
645 		/*
646 		 * Copy in input fields only.
647 		 */
648 		if (copyin((void *)arg, &fsh, sizeof (fsh))) {
649 			error = EFAULT;
650 			break;
651 		}
652 
653 		/*
654 		 * Local share reservations always have this simple form
655 		 */
656 		shr.s_access = fsh.f_access;
657 		shr.s_deny = fsh.f_deny;
658 		shr.s_sysid = 0;
659 		shr.s_pid = ttoproc(curthread)->p_pid;
660 		shr_own.sl_pid = shr.s_pid;
661 		shr_own.sl_id = fsh.f_id;
662 		shr.s_own_len = sizeof (shr_own);
663 		shr.s_owner = (caddr_t)&shr_own;
664 		error = VOP_SHRLOCK(vp, cmd, &shr, flag, fp->f_cred, NULL);
665 /* ONC_PLUS EXTRACT END */
666 		break;
667 
668 	default:
669 		error = EINVAL;
670 		break;
671 	}
672 
673 	if (in_crit)
674 		nbl_end_crit(vp);
675 
676 done:
677 	releasef(fdes);
678 out:
679 	if (error)
680 		return (set_errno(error));
681 	return (retval);
682 }
683 
684 int
685 dup(int fd)
686 {
687 	return (fcntl(fd, F_DUPFD, 0));
688 }
689 
690 /* ONC_PLUS EXTRACT START */
691 int
692 flock_check(vnode_t *vp, flock64_t *flp, offset_t offset, offset_t max)
693 {
694 	struct vattr	vattr;
695 	int	error;
696 	u_offset_t start, end;
697 
698 	/*
699 	 * Determine the starting point of the request
700 	 */
701 	switch (flp->l_whence) {
702 	case 0:		/* SEEK_SET */
703 		start = (u_offset_t)flp->l_start;
704 		if (start > max)
705 			return (EINVAL);
706 		break;
707 	case 1:		/* SEEK_CUR */
708 		if (flp->l_start > (max - offset))
709 			return (EOVERFLOW);
710 		start = (u_offset_t)(flp->l_start + offset);
711 		if (start > max)
712 			return (EINVAL);
713 		break;
714 	case 2:		/* SEEK_END */
715 		vattr.va_mask = AT_SIZE;
716 		if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
717 			return (error);
718 		if (flp->l_start > (max - (offset_t)vattr.va_size))
719 			return (EOVERFLOW);
720 		start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
721 		if (start > max)
722 			return (EINVAL);
723 		break;
724 	default:
725 		return (EINVAL);
726 	}
727 
728 	/*
729 	 * Determine the range covered by the request.
730 	 */
731 	if (flp->l_len == 0)
732 		end = MAXEND;
733 	else if ((offset_t)flp->l_len > 0) {
734 		if (flp->l_len > (max - start + 1))
735 			return (EOVERFLOW);
736 		end = (u_offset_t)(start + (flp->l_len - 1));
737 		ASSERT(end <= max);
738 	} else {
739 		/*
740 		 * Negative length; why do we even allow this ?
741 		 * Because this allows easy specification of
742 		 * the last n bytes of the file.
743 		 */
744 		end = start;
745 		start += (u_offset_t)flp->l_len;
746 		(start)++;
747 		if (start > max)
748 			return (EINVAL);
749 		ASSERT(end <= max);
750 	}
751 	ASSERT(start <= max);
752 	if (flp->l_type == F_UNLCK && flp->l_len > 0 &&
753 	    end == (offset_t)max) {
754 		flp->l_len = 0;
755 	}
756 	if (start  > end)
757 		return (EINVAL);
758 	return (0);
759 }
760 
761 static int
762 flock_get_start(vnode_t *vp, flock64_t *flp, offset_t offset, u_offset_t *start)
763 {
764 	struct vattr	vattr;
765 	int	error;
766 
767 	/*
768 	 * Determine the starting point of the request. Assume that it is
769 	 * a valid starting point.
770 	 */
771 	switch (flp->l_whence) {
772 	case 0:		/* SEEK_SET */
773 		*start = (u_offset_t)flp->l_start;
774 		break;
775 	case 1:		/* SEEK_CUR */
776 		*start = (u_offset_t)(flp->l_start + offset);
777 		break;
778 	case 2:		/* SEEK_END */
779 		vattr.va_mask = AT_SIZE;
780 		if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
781 			return (error);
782 		*start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
783 		break;
784 	default:
785 		return (EINVAL);
786 	}
787 
788 	return (0);
789 }
790 
791 /*
792  * Take rctl action when the requested file descriptor is too big.
793  */
794 static void
795 fd_too_big(proc_t *p)
796 {
797 	mutex_enter(&p->p_lock);
798 	(void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
799 	    p->p_rctls, p, RCA_SAFE);
800 	mutex_exit(&p->p_lock);
801 }
802 /* ONC_PLUS EXTRACT END */
803