xref: /illumos-gate/usr/src/uts/common/syscall/fcntl.c (revision d0f40dc6a997c84bacf5f9ba83d57a95495c399b)
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 /* ONC_PLUS EXTRACT START */
23 /*
24  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
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 /* 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 		/*
428 		 * Test for not-a-regular-file (and returning EINVAL)
429 		 * before testing for open-for-writing (and returning EBADF).
430 		 * This is relied upon by posix_fallocate() in libc.
431 		 */
432 		if (vp->v_type != VREG) {
433 			error = EINVAL;
434 			break;
435 		}
436 
437 		if ((flag & FWRITE) == 0) {
438 			error = EBADF;
439 			break;
440 		}
441 
442 		if (datamodel != DATAMODEL_ILP32 &&
443 		    (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
444 			error = EINVAL;
445 			break;
446 		}
447 
448 #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
449 		if (datamodel == DATAMODEL_ILP32 &&
450 		    (cmd == F_ALLOCSP || cmd == F_FREESP)) {
451 			struct flock32 sbf32;
452 			/*
453 			 * For compatibility we overlay an SVR3 flock on an SVR4
454 			 * flock.  This works because the input field offsets
455 			 * in "struct flock" were preserved.
456 			 */
457 			if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
458 				error = EFAULT;
459 				break;
460 			} else {
461 				bf.l_type = sbf32.l_type;
462 				bf.l_whence = sbf32.l_whence;
463 				bf.l_start = (off64_t)sbf32.l_start;
464 				bf.l_len = (off64_t)sbf32.l_len;
465 				bf.l_sysid = sbf32.l_sysid;
466 				bf.l_pid = sbf32.l_pid;
467 			}
468 		}
469 #endif /* _ILP32 || _SYSCALL32_IMPL */
470 
471 #if defined(_LP64)
472 		if (datamodel == DATAMODEL_LP64 &&
473 		    (cmd == F_ALLOCSP || cmd == F_FREESP)) {
474 			if (copyin((void *)arg, &bf, sizeof (bf))) {
475 				error = EFAULT;
476 				break;
477 			}
478 		}
479 #endif /* defined(_LP64) */
480 
481 #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
482 		if (datamodel == DATAMODEL_ILP32 &&
483 		    (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
484 			if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
485 				error = EFAULT;
486 				break;
487 			} else {
488 				/*
489 				 * Note that the size of flock64 is different in
490 				 * the ILP32 and LP64 models, due to the l_pad
491 				 * field. We do not want to assume that the
492 				 * flock64 structure is laid out the same in
493 				 * ILP32 and LP64 environments, so we will
494 				 * copy in the ILP32 version of flock64
495 				 * explicitly and copy it to the native
496 				 * flock64 structure.
497 				 */
498 				bf.l_type = (short)bf64_32.l_type;
499 				bf.l_whence = (short)bf64_32.l_whence;
500 				bf.l_start = bf64_32.l_start;
501 				bf.l_len = bf64_32.l_len;
502 				bf.l_sysid = (int)bf64_32.l_sysid;
503 				bf.l_pid = (pid_t)bf64_32.l_pid;
504 			}
505 		}
506 #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
507 
508 		if (cmd == F_ALLOCSP || cmd == F_FREESP)
509 			error = flock_check(vp, &bf, offset, maxoffset);
510 		else if (cmd == F_ALLOCSP64 || cmd == F_FREESP64)
511 			error = flock_check(vp, &bf, offset, MAXOFFSET_T);
512 		if (error)
513 			break;
514 
515 		if (vp->v_type == VREG && bf.l_len == 0 &&
516 		    bf.l_start > OFFSET_MAX(fp)) {
517 			error = EFBIG;
518 			break;
519 		}
520 
521 		/*
522 		 * Make sure that there are no conflicting non-blocking
523 		 * mandatory locks in the region being manipulated. If
524 		 * there are such locks then return EACCES.
525 		 */
526 		if ((error = flock_get_start(vp, &bf, offset, &start)) != 0)
527 			break;
528 
529 		if (nbl_need_check(vp)) {
530 			u_offset_t	begin;
531 			ssize_t		length;
532 
533 			nbl_start_crit(vp, RW_READER);
534 			in_crit = 1;
535 			vattr.va_mask = AT_SIZE;
536 			if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
537 			    != 0)
538 				break;
539 			begin = start > vattr.va_size ? vattr.va_size : start;
540 			length = vattr.va_size > start ? vattr.va_size - start :
541 			    start - vattr.va_size;
542 			if (nbl_conflict(vp, NBL_WRITE, begin, length, 0,
543 			    NULL)) {
544 				error = EACCES;
545 				break;
546 			}
547 		}
548 
549 		if (cmd == F_ALLOCSP64)
550 			cmd = F_ALLOCSP;
551 		else if (cmd == F_FREESP64)
552 			cmd = F_FREESP;
553 
554 		error = VOP_SPACE(vp, cmd, &bf, flag, offset, fp->f_cred, NULL);
555 
556 		break;
557 
558 #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
559 /* ONC_PLUS EXTRACT START */
560 	case F_GETLK64:
561 	case F_SETLK64:
562 	case F_SETLKW64:
563 	case F_SETLK64_NBMAND:
564 		/*
565 		 * Large Files: Here we set cmd as *LK and send it to
566 		 * lower layers. *LK64 is only for the user land.
567 		 * Most of the comments described above for F_SETLK
568 		 * applies here too.
569 		 * Large File support is only needed for ILP32 apps!
570 		 */
571 		if (datamodel != DATAMODEL_ILP32) {
572 			error = EINVAL;
573 			break;
574 		}
575 
576 		if (cmd == F_GETLK64)
577 			cmd = F_GETLK;
578 		else if (cmd == F_SETLK64)
579 			cmd = F_SETLK;
580 		else if (cmd == F_SETLKW64)
581 			cmd = F_SETLKW;
582 		else if (cmd == F_SETLK64_NBMAND)
583 			cmd = F_SETLK_NBMAND;
584 
585 		/*
586 		 * Note that the size of flock64 is different in the ILP32
587 		 * and LP64 models, due to the sucking l_pad field.
588 		 * We do not want to assume that the flock64 structure is
589 		 * laid out in the same in ILP32 and LP64 environments, so
590 		 * we will copy in the ILP32 version of flock64 explicitly
591 		 * and copy it to the native flock64 structure.
592 		 */
593 
594 		if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
595 			error = EFAULT;
596 			break;
597 		}
598 
599 		bf.l_type = (short)bf64_32.l_type;
600 		bf.l_whence = (short)bf64_32.l_whence;
601 		bf.l_start = bf64_32.l_start;
602 		bf.l_len = bf64_32.l_len;
603 		bf.l_sysid = (int)bf64_32.l_sysid;
604 		bf.l_pid = (pid_t)bf64_32.l_pid;
605 
606 		if ((error = flock_check(vp, &bf, offset, MAXOFFSET_T)) != 0)
607 			break;
608 
609 		if ((error = VOP_FRLOCK(vp, cmd, &bf, flag, offset,
610 		    NULL, fp->f_cred, NULL)) != 0)
611 			break;
612 
613 		if ((cmd == F_GETLK) && bf.l_type == F_UNLCK) {
614 			if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
615 			    sizeof (bf.l_type)))
616 				error = EFAULT;
617 			break;
618 		}
619 
620 		if (cmd == F_GETLK) {
621 			int i;
622 
623 			/*
624 			 * We do not want to assume that the flock64 structure
625 			 * is laid out in the same in ILP32 and LP64
626 			 * environments, so we will copy out the ILP32 version
627 			 * of flock64 explicitly after copying the native
628 			 * flock64 structure to it.
629 			 */
630 			for (i = 0; i < 4; i++)
631 				bf64_32.l_pad[i] = 0;
632 			bf64_32.l_type = (int16_t)bf.l_type;
633 			bf64_32.l_whence = (int16_t)bf.l_whence;
634 			bf64_32.l_start = bf.l_start;
635 			bf64_32.l_len = bf.l_len;
636 			bf64_32.l_sysid = (int32_t)bf.l_sysid;
637 			bf64_32.l_pid = (pid32_t)bf.l_pid;
638 			if (copyout(&bf64_32, (void *)arg, sizeof (bf64_32)))
639 				error = EFAULT;
640 		}
641 		break;
642 /* ONC_PLUS EXTRACT END */
643 #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
644 
645 /* ONC_PLUS EXTRACT START */
646 	case F_SHARE:
647 	case F_SHARE_NBMAND:
648 	case F_UNSHARE:
649 
650 		/*
651 		 * Copy in input fields only.
652 		 */
653 		if (copyin((void *)arg, &fsh, sizeof (fsh))) {
654 			error = EFAULT;
655 			break;
656 		}
657 
658 		/*
659 		 * Local share reservations always have this simple form
660 		 */
661 		shr.s_access = fsh.f_access;
662 		shr.s_deny = fsh.f_deny;
663 		shr.s_sysid = 0;
664 		shr.s_pid = ttoproc(curthread)->p_pid;
665 		shr_own.sl_pid = shr.s_pid;
666 		shr_own.sl_id = fsh.f_id;
667 		shr.s_own_len = sizeof (shr_own);
668 		shr.s_owner = (caddr_t)&shr_own;
669 		error = VOP_SHRLOCK(vp, cmd, &shr, flag, fp->f_cred, NULL);
670 /* ONC_PLUS EXTRACT END */
671 		break;
672 
673 	default:
674 		error = EINVAL;
675 		break;
676 	}
677 
678 	if (in_crit)
679 		nbl_end_crit(vp);
680 
681 done:
682 	releasef(fdes);
683 out:
684 	if (error)
685 		return (set_errno(error));
686 	return (retval);
687 }
688 
689 /* ONC_PLUS EXTRACT START */
690 int
691 flock_check(vnode_t *vp, flock64_t *flp, offset_t offset, offset_t max)
692 {
693 	struct vattr	vattr;
694 	int	error;
695 	u_offset_t start, end;
696 
697 	/*
698 	 * Determine the starting point of the request
699 	 */
700 	switch (flp->l_whence) {
701 	case 0:		/* SEEK_SET */
702 		start = (u_offset_t)flp->l_start;
703 		if (start > max)
704 			return (EINVAL);
705 		break;
706 	case 1:		/* SEEK_CUR */
707 		if (flp->l_start > (max - offset))
708 			return (EOVERFLOW);
709 		start = (u_offset_t)(flp->l_start + offset);
710 		if (start > max)
711 			return (EINVAL);
712 		break;
713 	case 2:		/* SEEK_END */
714 		vattr.va_mask = AT_SIZE;
715 		if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
716 			return (error);
717 		if (flp->l_start > (max - (offset_t)vattr.va_size))
718 			return (EOVERFLOW);
719 		start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
720 		if (start > max)
721 			return (EINVAL);
722 		break;
723 	default:
724 		return (EINVAL);
725 	}
726 
727 	/*
728 	 * Determine the range covered by the request.
729 	 */
730 	if (flp->l_len == 0)
731 		end = MAXEND;
732 	else if ((offset_t)flp->l_len > 0) {
733 		if (flp->l_len > (max - start + 1))
734 			return (EOVERFLOW);
735 		end = (u_offset_t)(start + (flp->l_len - 1));
736 		ASSERT(end <= max);
737 	} else {
738 		/*
739 		 * Negative length; why do we even allow this ?
740 		 * Because this allows easy specification of
741 		 * the last n bytes of the file.
742 		 */
743 		end = start;
744 		start += (u_offset_t)flp->l_len;
745 		(start)++;
746 		if (start > max)
747 			return (EINVAL);
748 		ASSERT(end <= max);
749 	}
750 	ASSERT(start <= max);
751 	if (flp->l_type == F_UNLCK && flp->l_len > 0 &&
752 	    end == (offset_t)max) {
753 		flp->l_len = 0;
754 	}
755 	if (start  > end)
756 		return (EINVAL);
757 	return (0);
758 }
759 
760 static int
761 flock_get_start(vnode_t *vp, flock64_t *flp, offset_t offset, u_offset_t *start)
762 {
763 	struct vattr	vattr;
764 	int	error;
765 
766 	/*
767 	 * Determine the starting point of the request. Assume that it is
768 	 * a valid starting point.
769 	 */
770 	switch (flp->l_whence) {
771 	case 0:		/* SEEK_SET */
772 		*start = (u_offset_t)flp->l_start;
773 		break;
774 	case 1:		/* SEEK_CUR */
775 		*start = (u_offset_t)(flp->l_start + offset);
776 		break;
777 	case 2:		/* SEEK_END */
778 		vattr.va_mask = AT_SIZE;
779 		if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
780 			return (error);
781 		*start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
782 		break;
783 	default:
784 		return (EINVAL);
785 	}
786 
787 	return (0);
788 }
789 
790 /*
791  * Take rctl action when the requested file descriptor is too big.
792  */
793 static void
794 fd_too_big(proc_t *p)
795 {
796 	mutex_enter(&p->p_lock);
797 	(void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
798 	    p->p_rctls, p, RCA_SAFE);
799 	mutex_exit(&p->p_lock);
800 }
801 /* ONC_PLUS EXTRACT END */
802