uts: make emu10k non-verbose
[unleashed.git] / kernel / syscall / fcntl.c
blobfe7b7767223798a326eeca3553815272cad789a8
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved.
25 * Copyright 2015, Joyent, Inc.
28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 * Portions of this source code were derived from Berkeley 4.3 BSD
33 * under license from the Regents of the University of California.
37 #include <sys/param.h>
38 #include <sys/isa_defs.h>
39 #include <sys/types.h>
40 #include <sys/sysmacros.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/fcntl.h>
44 #include <sys/flock.h>
45 #include <sys/vnode.h>
46 #include <sys/file.h>
47 #include <sys/mode.h>
48 #include <sys/proc.h>
49 #include <sys/filio.h>
50 #include <sys/share.h>
51 #include <sys/debug.h>
52 #include <sys/rctl.h>
53 #include <sys/nbmlock.h>
55 #include <sys/cmn_err.h>
57 static int flock_check(vnode_t *, flock64_t *, offset_t, offset_t);
58 static int flock_get_start(vnode_t *, flock64_t *, offset_t, uoff_t *);
59 static void fd_too_big(proc_t *);
62 * File control.
64 int
65 fcntl(int fdes, int cmd, intptr_t arg)
67 int iarg;
68 int error = 0;
69 int retval;
70 proc_t *p;
71 file_t *fp;
72 vnode_t *vp;
73 uoff_t offset;
74 uoff_t start;
75 struct vattr vattr;
76 int in_crit;
77 int flag;
78 struct flock sbf;
79 struct flock64 bf;
80 struct o_flock obf;
81 struct flock64_32 bf64_32;
82 struct fshare fsh;
83 struct shrlock shr;
84 struct shr_locowner shr_own;
85 offset_t maxoffset;
86 model_t datamodel;
87 int fdres;
89 #if defined(_ILP32) && !defined(lint) && defined(_SYSCALL32)
90 ASSERT(sizeof (struct flock) == sizeof (struct flock32));
91 ASSERT(sizeof (struct flock64) == sizeof (struct flock64_32));
92 #endif
93 #if defined(_LP64) && !defined(lint) && defined(_SYSCALL32)
94 ASSERT(sizeof (struct flock) == sizeof (struct flock64_64));
95 ASSERT(sizeof (struct flock64) == sizeof (struct flock64_64));
96 #endif
99 * First, for speed, deal with the subset of cases
100 * that do not require getf() / releasef().
102 switch (cmd) {
103 case F_GETFD:
104 if ((error = f_getfd_error(fdes, &flag)) == 0)
105 retval = flag;
106 goto out;
108 case F_SETFD:
109 error = f_setfd_error(fdes, (int)arg);
110 retval = 0;
111 goto out;
113 case F_GETFL:
114 if ((error = f_getfl(fdes, &flag)) == 0) {
115 retval = OFLAGS(flag) & (FCNTLFLAGS | O_ASYNC |
116 O_ACCMODE);
118 goto out;
120 case F_GETXFL:
121 if ((error = f_getfl(fdes, &flag)) == 0) {
122 retval = OFLAGS(flag);
124 goto out;
126 case F_BADFD:
127 if ((error = f_badfd(fdes, &fdres, (int)arg)) == 0)
128 retval = fdres;
129 goto out;
133 * Second, for speed, deal with the subset of cases that
134 * require getf() / releasef() but do not require copyin.
136 if ((fp = getf(fdes)) == NULL) {
137 error = EBADF;
138 goto out;
140 iarg = (int)arg;
142 switch (cmd) {
143 case F_DUPFD:
144 case F_DUPFD_CLOEXEC:
145 p = curproc;
146 if ((uint_t)iarg >= p->p_fno_ctl) {
147 if (iarg >= 0)
148 fd_too_big(p);
149 error = EINVAL;
150 goto done;
153 * We need to increment the f_count reference counter
154 * before allocating a new file descriptor.
155 * Doing it other way round opens a window for race condition
156 * with closeandsetf() on the target file descriptor which can
157 * close the file still referenced by the original
158 * file descriptor.
160 mutex_enter(&fp->f_tlock);
161 fp->f_count++;
162 mutex_exit(&fp->f_tlock);
163 if ((retval = ufalloc_file(iarg, fp)) == -1) {
165 * New file descriptor can't be allocated.
166 * Revert the reference count.
168 mutex_enter(&fp->f_tlock);
169 fp->f_count--;
170 mutex_exit(&fp->f_tlock);
171 error = EMFILE;
172 } else {
173 if (cmd == F_DUPFD_CLOEXEC) {
174 f_setfd(retval, FD_CLOEXEC);
177 goto done;
179 case F_DUP2FD_CLOEXEC:
180 if (fdes == iarg) {
181 error = EINVAL;
182 goto done;
185 /*FALLTHROUGH*/
187 case F_DUP2FD:
188 p = curproc;
189 if (fdes == iarg) {
190 retval = iarg;
191 } else if ((uint_t)iarg >= p->p_fno_ctl) {
192 if (iarg >= 0)
193 fd_too_big(p);
194 error = EBADF;
195 } else {
197 * We can't hold our getf(fdes) across the call to
198 * closeandsetf() because it creates a window for
199 * deadlock: if one thread is doing dup2(a, b) while
200 * another is doing dup2(b, a), each one will block
201 * waiting for the other to call releasef(). The
202 * solution is to increment the file reference count
203 * (which we have to do anyway), then releasef(fdes),
204 * then closeandsetf(). Incrementing f_count ensures
205 * that fp won't disappear after we call releasef().
206 * When closeandsetf() fails, we try avoid calling
207 * closef() because of all the side effects.
209 mutex_enter(&fp->f_tlock);
210 fp->f_count++;
211 mutex_exit(&fp->f_tlock);
212 releasef(fdes);
213 if ((error = closeandsetf(iarg, fp)) == 0) {
214 if (cmd == F_DUP2FD_CLOEXEC) {
215 f_setfd(iarg, FD_CLOEXEC);
217 retval = iarg;
218 } else {
219 mutex_enter(&fp->f_tlock);
220 if (fp->f_count > 1) {
221 fp->f_count--;
222 mutex_exit(&fp->f_tlock);
223 } else {
224 mutex_exit(&fp->f_tlock);
225 (void) closef(fp);
228 goto out;
230 goto done;
232 case F_SETFL:
233 vp = fp->f_vnode;
234 flag = fp->f_flag;
235 iarg = FFLAGS(iarg);
236 if ((iarg & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY))
237 iarg &= ~FNDELAY;
238 if ((error = fop_setfl(vp, flag, iarg, fp->f_cred, NULL)) ==
239 0) {
240 iarg &= FCNTLFLAGS;
241 mutex_enter(&fp->f_tlock);
242 fp->f_flag &= ~FCNTLFLAGS;
243 fp->f_flag |= iarg;
244 mutex_exit(&fp->f_tlock);
246 retval = 0;
247 goto done;
251 * Finally, deal with the expensive cases.
253 retval = 0;
254 in_crit = 0;
255 maxoffset = MAXOFF_T;
256 datamodel = DATAMODEL_NATIVE;
257 #if defined(_SYSCALL32_IMPL)
258 if ((datamodel = get_udatamodel()) == DATAMODEL_ILP32)
259 maxoffset = MAXOFF32_T;
260 #endif
262 vp = fp->f_vnode;
263 flag = fp->f_flag;
264 offset = fp->f_offset;
266 switch (cmd) {
268 * The file system and vnode layers understand and implement
269 * locking with flock64 structures. So here once we pass through
270 * the test for compatibility as defined by LFS API, (for F_SETLK,
271 * F_SETLKW, F_GETLK, F_GETLKW, F_OFD_GETLK, F_OFD_SETLK, F_OFD_SETLKW,
272 * F_FREESP) we transform the flock structure to a flock64 structure
273 * and send it to the lower layers. Similarly in case of GETLK and
274 * OFD_GETLK the returned flock64 structure is transformed to a flock
275 * structure if everything fits in nicely, otherwise we return
276 * EOVERFLOW.
279 case F_GETLK:
280 case F_O_GETLK:
281 case F_SETLK:
282 case F_SETLKW:
283 case F_SETLK_NBMAND:
284 case F_OFD_GETLK:
285 case F_OFD_SETLK:
286 case F_OFD_SETLKW:
287 case F_FLOCK:
288 case F_FLOCKW:
291 * Copy in input fields only.
294 if (cmd == F_O_GETLK) {
295 if (datamodel != DATAMODEL_ILP32) {
296 error = EINVAL;
297 break;
300 if (copyin((void *)arg, &obf, sizeof (obf))) {
301 error = EFAULT;
302 break;
304 bf.l_type = obf.l_type;
305 bf.l_whence = obf.l_whence;
306 bf.l_start = (off64_t)obf.l_start;
307 bf.l_len = (off64_t)obf.l_len;
308 bf.l_sysid = (int)obf.l_sysid;
309 bf.l_pid = obf.l_pid;
310 } else if (datamodel == DATAMODEL_NATIVE) {
311 if (copyin((void *)arg, &sbf, sizeof (sbf))) {
312 error = EFAULT;
313 break;
316 * XXX In an LP64 kernel with an LP64 application
317 * there's no need to do a structure copy here
318 * struct flock == struct flock64. However,
319 * we did it this way to avoid more conditional
320 * compilation.
322 bf.l_type = sbf.l_type;
323 bf.l_whence = sbf.l_whence;
324 bf.l_start = (off64_t)sbf.l_start;
325 bf.l_len = (off64_t)sbf.l_len;
326 bf.l_sysid = sbf.l_sysid;
327 bf.l_pid = sbf.l_pid;
329 #if defined(_SYSCALL32_IMPL)
330 else {
331 struct flock32 sbf32;
332 if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
333 error = EFAULT;
334 break;
336 bf.l_type = sbf32.l_type;
337 bf.l_whence = sbf32.l_whence;
338 bf.l_start = (off64_t)sbf32.l_start;
339 bf.l_len = (off64_t)sbf32.l_len;
340 bf.l_sysid = sbf32.l_sysid;
341 bf.l_pid = sbf32.l_pid;
343 #endif /* _SYSCALL32_IMPL */
346 * 64-bit support: check for overflow for 32-bit lock ops
348 if ((error = flock_check(vp, &bf, offset, maxoffset)) != 0)
349 break;
351 if (cmd == F_FLOCK || cmd == F_FLOCKW) {
352 /* FLOCK* locking is always over the entire file. */
353 if (bf.l_whence != 0 || bf.l_start != 0 ||
354 bf.l_len != 0) {
355 error = EINVAL;
356 break;
358 if (bf.l_type < F_RDLCK || bf.l_type > F_UNLCK) {
359 error = EINVAL;
360 break;
364 if (cmd == F_OFD_GETLK || cmd == F_OFD_SETLK ||
365 cmd == F_OFD_SETLKW) {
367 * TBD OFD-style locking is currently limited to
368 * covering the entire file.
370 if (bf.l_whence != 0 || bf.l_start != 0 ||
371 bf.l_len != 0) {
372 error = EINVAL;
373 break;
378 * Not all of the filesystems understand F_O_GETLK, and
379 * there's no need for them to know. Map it to F_GETLK.
381 * The *_frlock functions in the various file systems basically
382 * do some validation and then funnel everything through the
383 * fs_frlock function. For OFD-style locks fs_frlock will do
384 * nothing so that once control returns here we can call the
385 * ofdlock function with the correct fp. For OFD-style locks
386 * the unsupported remote file systems, such as NFS, detect and
387 * reject the OFD-style cmd argument.
389 if ((error = fop_frlock(vp, (cmd == F_O_GETLK) ? F_GETLK : cmd,
390 &bf, flag, offset, NULL, fp->f_cred, NULL)) != 0)
391 break;
393 if (cmd == F_FLOCK || cmd == F_FLOCKW || cmd == F_OFD_GETLK ||
394 cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
396 * This is an OFD-style lock so we need to handle it
397 * here. Because OFD-style locks are associated with
398 * the file_t we didn't have enough info down the
399 * fop_frlock path immediately above.
401 if ((error = ofdlock(fp, cmd, &bf, flag, offset)) != 0)
402 break;
406 * If command is GETLK and no lock is found, only
407 * the type field is changed.
409 if ((cmd == F_O_GETLK || cmd == F_GETLK ||
410 cmd == F_OFD_GETLK) && bf.l_type == F_UNLCK) {
411 /* l_type always first entry, always a short */
412 if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
413 sizeof (bf.l_type)))
414 error = EFAULT;
415 break;
418 if (cmd == F_O_GETLK) {
420 * Return an SVR3 flock structure to the user.
422 obf.l_type = (int16_t)bf.l_type;
423 obf.l_whence = (int16_t)bf.l_whence;
424 obf.l_start = (int32_t)bf.l_start;
425 obf.l_len = (int32_t)bf.l_len;
426 if (bf.l_sysid > SHRT_MAX || bf.l_pid > SHRT_MAX) {
428 * One or both values for the above fields
429 * is too large to store in an SVR3 flock
430 * structure.
432 error = EOVERFLOW;
433 break;
435 obf.l_sysid = (int16_t)bf.l_sysid;
436 obf.l_pid = (int16_t)bf.l_pid;
437 if (copyout(&obf, (void *)arg, sizeof (obf)))
438 error = EFAULT;
439 } else if (cmd == F_GETLK || cmd == F_OFD_GETLK) {
441 * Copy out SVR4 flock.
443 int i;
445 if (bf.l_start > maxoffset || bf.l_len > maxoffset) {
446 error = EOVERFLOW;
447 break;
450 if (datamodel == DATAMODEL_NATIVE) {
451 for (i = 0; i < 4; i++)
452 sbf.l_pad[i] = 0;
454 * XXX In an LP64 kernel with an LP64
455 * application there's no need to do a
456 * structure copy here as currently
457 * struct flock == struct flock64.
458 * We did it this way to avoid more
459 * conditional compilation.
461 sbf.l_type = bf.l_type;
462 sbf.l_whence = bf.l_whence;
463 sbf.l_start = (off_t)bf.l_start;
464 sbf.l_len = (off_t)bf.l_len;
465 sbf.l_sysid = bf.l_sysid;
466 sbf.l_pid = bf.l_pid;
467 if (copyout(&sbf, (void *)arg, sizeof (sbf)))
468 error = EFAULT;
470 #if defined(_SYSCALL32_IMPL)
471 else {
472 struct flock32 sbf32;
473 if (bf.l_start > MAXOFF32_T ||
474 bf.l_len > MAXOFF32_T) {
475 error = EOVERFLOW;
476 break;
478 for (i = 0; i < 4; i++)
479 sbf32.l_pad[i] = 0;
480 sbf32.l_type = (int16_t)bf.l_type;
481 sbf32.l_whence = (int16_t)bf.l_whence;
482 sbf32.l_start = (off32_t)bf.l_start;
483 sbf32.l_len = (off32_t)bf.l_len;
484 sbf32.l_sysid = (int32_t)bf.l_sysid;
485 sbf32.l_pid = (pid32_t)bf.l_pid;
486 if (copyout(&sbf32,
487 (void *)arg, sizeof (sbf32)))
488 error = EFAULT;
490 #endif
492 break;
494 case F_CHKFL:
496 * This is for internal use only, to allow the vnode layer
497 * to validate a flags setting before applying it. User
498 * programs can't issue it.
500 error = EINVAL;
501 break;
503 case F_ALLOCSP:
504 case F_FREESP:
505 case F_ALLOCSP64:
506 case F_FREESP64:
508 * Test for not-a-regular-file (and returning EINVAL)
509 * before testing for open-for-writing (and returning EBADF).
510 * This is relied upon by posix_fallocate() in libc.
512 if (vp->v_type != VREG) {
513 error = EINVAL;
514 break;
517 if ((flag & FWRITE) == 0) {
518 error = EBADF;
519 break;
522 if (datamodel != DATAMODEL_ILP32 &&
523 (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
524 error = EINVAL;
525 break;
528 #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
529 if (datamodel == DATAMODEL_ILP32 &&
530 (cmd == F_ALLOCSP || cmd == F_FREESP)) {
531 struct flock32 sbf32;
533 * For compatibility we overlay an SVR3 flock on an SVR4
534 * flock. This works because the input field offsets
535 * in "struct flock" were preserved.
537 if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
538 error = EFAULT;
539 break;
540 } else {
541 bf.l_type = sbf32.l_type;
542 bf.l_whence = sbf32.l_whence;
543 bf.l_start = (off64_t)sbf32.l_start;
544 bf.l_len = (off64_t)sbf32.l_len;
545 bf.l_sysid = sbf32.l_sysid;
546 bf.l_pid = sbf32.l_pid;
549 #endif /* _ILP32 || _SYSCALL32_IMPL */
551 #if defined(_LP64)
552 if (datamodel == DATAMODEL_LP64 &&
553 (cmd == F_ALLOCSP || cmd == F_FREESP)) {
554 if (copyin((void *)arg, &bf, sizeof (bf))) {
555 error = EFAULT;
556 break;
559 #endif /* defined(_LP64) */
561 #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
562 if (datamodel == DATAMODEL_ILP32 &&
563 (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
564 if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
565 error = EFAULT;
566 break;
567 } else {
569 * Note that the size of flock64 is different in
570 * the ILP32 and LP64 models, due to the l_pad
571 * field. We do not want to assume that the
572 * flock64 structure is laid out the same in
573 * ILP32 and LP64 environments, so we will
574 * copy in the ILP32 version of flock64
575 * explicitly and copy it to the native
576 * flock64 structure.
578 bf.l_type = (short)bf64_32.l_type;
579 bf.l_whence = (short)bf64_32.l_whence;
580 bf.l_start = bf64_32.l_start;
581 bf.l_len = bf64_32.l_len;
582 bf.l_sysid = (int)bf64_32.l_sysid;
583 bf.l_pid = (pid_t)bf64_32.l_pid;
586 #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
588 if (cmd == F_ALLOCSP || cmd == F_FREESP)
589 error = flock_check(vp, &bf, offset, maxoffset);
590 else if (cmd == F_ALLOCSP64 || cmd == F_FREESP64)
591 error = flock_check(vp, &bf, offset, MAXOFFSET_T);
592 if (error)
593 break;
595 if (vp->v_type == VREG && bf.l_len == 0 &&
596 bf.l_start > OFFSET_MAX(fp)) {
597 error = EFBIG;
598 break;
602 * Make sure that there are no conflicting non-blocking
603 * mandatory locks in the region being manipulated. If
604 * there are such locks then return EACCES.
606 if ((error = flock_get_start(vp, &bf, offset, &start)) != 0)
607 break;
609 if (nbl_need_check(vp)) {
610 uoff_t begin;
611 ssize_t length;
613 nbl_start_crit(vp, RW_READER);
614 in_crit = 1;
615 vattr.va_mask = AT_SIZE;
616 if ((error = fop_getattr(vp, &vattr, 0, CRED(), NULL))
617 != 0)
618 break;
619 begin = start > vattr.va_size ? vattr.va_size : start;
620 length = vattr.va_size > start ? vattr.va_size - start :
621 start - vattr.va_size;
622 if (nbl_conflict(vp, NBL_WRITE, begin, length, 0,
623 NULL)) {
624 error = EACCES;
625 break;
629 if (cmd == F_ALLOCSP64)
630 cmd = F_ALLOCSP;
631 else if (cmd == F_FREESP64)
632 cmd = F_FREESP;
634 error = fop_space(vp, cmd, &bf, flag, offset, fp->f_cred, NULL);
636 break;
638 #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
639 case F_GETLK64:
640 case F_SETLK64:
641 case F_SETLKW64:
642 case F_SETLK64_NBMAND:
643 case F_OFD_GETLK64:
644 case F_OFD_SETLK64:
645 case F_OFD_SETLKW64:
646 case F_FLOCK64:
647 case F_FLOCKW64:
649 * Large Files: Here we set cmd as *LK and send it to
650 * lower layers. *LK64 is only for the user land.
651 * Most of the comments described above for F_SETLK
652 * applies here too.
653 * Large File support is only needed for ILP32 apps!
655 if (datamodel != DATAMODEL_ILP32) {
656 error = EINVAL;
657 break;
660 if (cmd == F_GETLK64)
661 cmd = F_GETLK;
662 else if (cmd == F_SETLK64)
663 cmd = F_SETLK;
664 else if (cmd == F_SETLKW64)
665 cmd = F_SETLKW;
666 else if (cmd == F_SETLK64_NBMAND)
667 cmd = F_SETLK_NBMAND;
668 else if (cmd == F_OFD_GETLK64)
669 cmd = F_OFD_GETLK;
670 else if (cmd == F_OFD_SETLK64)
671 cmd = F_OFD_SETLK;
672 else if (cmd == F_OFD_SETLKW64)
673 cmd = F_OFD_SETLKW;
674 else if (cmd == F_FLOCK64)
675 cmd = F_FLOCK;
676 else if (cmd == F_FLOCKW64)
677 cmd = F_FLOCKW;
680 * Note that the size of flock64 is different in the ILP32
681 * and LP64 models, due to the sucking l_pad field.
682 * We do not want to assume that the flock64 structure is
683 * laid out in the same in ILP32 and LP64 environments, so
684 * we will copy in the ILP32 version of flock64 explicitly
685 * and copy it to the native flock64 structure.
688 if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
689 error = EFAULT;
690 break;
693 bf.l_type = (short)bf64_32.l_type;
694 bf.l_whence = (short)bf64_32.l_whence;
695 bf.l_start = bf64_32.l_start;
696 bf.l_len = bf64_32.l_len;
697 bf.l_sysid = (int)bf64_32.l_sysid;
698 bf.l_pid = (pid_t)bf64_32.l_pid;
700 if ((error = flock_check(vp, &bf, offset, MAXOFFSET_T)) != 0)
701 break;
703 if (cmd == F_FLOCK || cmd == F_FLOCKW) {
704 /* FLOCK* locking is always over the entire file. */
705 if (bf.l_whence != 0 || bf.l_start != 0 ||
706 bf.l_len != 0) {
707 error = EINVAL;
708 break;
710 if (bf.l_type < F_RDLCK || bf.l_type > F_UNLCK) {
711 error = EINVAL;
712 break;
716 if (cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
718 * TBD OFD-style locking is currently limited to
719 * covering the entire file.
721 if (bf.l_whence != 0 || bf.l_start != 0 ||
722 bf.l_len != 0) {
723 error = EINVAL;
724 break;
729 * The *_frlock functions in the various file systems basically
730 * do some validation and then funnel everything through the
731 * fs_frlock function. For OFD-style locks fs_frlock will do
732 * nothing so that once control returns here we can call the
733 * ofdlock function with the correct fp. For OFD-style locks
734 * the unsupported remote file systems, such as NFS, detect and
735 * reject the OFD-style cmd argument.
737 if ((error = fop_frlock(vp, cmd, &bf, flag, offset,
738 NULL, fp->f_cred, NULL)) != 0)
739 break;
741 if (cmd == F_FLOCK || cmd == F_FLOCKW || cmd == F_OFD_GETLK ||
742 cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
744 * This is an OFD-style lock so we need to handle it
745 * here. Because OFD-style locks are associated with
746 * the file_t we didn't have enough info down the
747 * fop_frlock path immediately above.
749 if ((error = ofdlock(fp, cmd, &bf, flag, offset)) != 0)
750 break;
753 if ((cmd == F_GETLK || cmd == F_OFD_GETLK) &&
754 bf.l_type == F_UNLCK) {
755 if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
756 sizeof (bf.l_type)))
757 error = EFAULT;
758 break;
761 if (cmd == F_GETLK || cmd == F_OFD_GETLK) {
762 int i;
765 * We do not want to assume that the flock64 structure
766 * is laid out in the same in ILP32 and LP64
767 * environments, so we will copy out the ILP32 version
768 * of flock64 explicitly after copying the native
769 * flock64 structure to it.
771 for (i = 0; i < 4; i++)
772 bf64_32.l_pad[i] = 0;
773 bf64_32.l_type = (int16_t)bf.l_type;
774 bf64_32.l_whence = (int16_t)bf.l_whence;
775 bf64_32.l_start = bf.l_start;
776 bf64_32.l_len = bf.l_len;
777 bf64_32.l_sysid = (int32_t)bf.l_sysid;
778 bf64_32.l_pid = (pid32_t)bf.l_pid;
779 if (copyout(&bf64_32, (void *)arg, sizeof (bf64_32)))
780 error = EFAULT;
782 break;
783 #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
785 case F_SHARE:
786 case F_SHARE_NBMAND:
787 case F_UNSHARE:
790 * Copy in input fields only.
792 if (copyin((void *)arg, &fsh, sizeof (fsh))) {
793 error = EFAULT;
794 break;
798 * Local share reservations always have this simple form
800 shr.s_access = fsh.f_access;
801 shr.s_deny = fsh.f_deny;
802 shr.s_sysid = 0;
803 shr.s_pid = ttoproc(curthread)->p_pid;
804 shr_own.sl_pid = shr.s_pid;
805 shr_own.sl_id = fsh.f_id;
806 shr.s_own_len = sizeof (shr_own);
807 shr.s_owner = (caddr_t)&shr_own;
808 error = fop_shrlock(vp, cmd, &shr, flag, fp->f_cred, NULL);
809 break;
811 default:
812 error = EINVAL;
813 break;
816 if (in_crit)
817 nbl_end_crit(vp);
819 done:
820 releasef(fdes);
821 out:
822 if (error)
823 return (set_errno(error));
824 return (retval);
828 flock_check(vnode_t *vp, flock64_t *flp, offset_t offset, offset_t max)
830 struct vattr vattr;
831 int error;
832 uoff_t start, end;
835 * Determine the starting point of the request
837 switch (flp->l_whence) {
838 case 0: /* SEEK_SET */
839 start = (uoff_t)flp->l_start;
840 if (start > max)
841 return (EINVAL);
842 break;
843 case 1: /* SEEK_CUR */
844 if (flp->l_start > (max - offset))
845 return (EOVERFLOW);
846 start = (uoff_t)(flp->l_start + offset);
847 if (start > max)
848 return (EINVAL);
849 break;
850 case 2: /* SEEK_END */
851 vattr.va_mask = AT_SIZE;
852 if (error = fop_getattr(vp, &vattr, 0, CRED(), NULL))
853 return (error);
854 if (flp->l_start > (max - (offset_t)vattr.va_size))
855 return (EOVERFLOW);
856 start = (uoff_t)(flp->l_start + (offset_t)vattr.va_size);
857 if (start > max)
858 return (EINVAL);
859 break;
860 default:
861 return (EINVAL);
865 * Determine the range covered by the request.
867 if (flp->l_len == 0)
868 end = MAXEND;
869 else if ((offset_t)flp->l_len > 0) {
870 if (flp->l_len > (max - start + 1))
871 return (EOVERFLOW);
872 end = (uoff_t)(start + (flp->l_len - 1));
873 ASSERT(end <= max);
874 } else {
876 * Negative length; why do we even allow this ?
877 * Because this allows easy specification of
878 * the last n bytes of the file.
880 end = start;
881 start += (uoff_t)flp->l_len;
882 (start)++;
883 if (start > max)
884 return (EINVAL);
885 ASSERT(end <= max);
887 ASSERT(start <= max);
888 if (flp->l_type == F_UNLCK && flp->l_len > 0 &&
889 end == (offset_t)max) {
890 flp->l_len = 0;
892 if (start > end)
893 return (EINVAL);
894 return (0);
897 static int
898 flock_get_start(vnode_t *vp, flock64_t *flp, offset_t offset, uoff_t *start)
900 struct vattr vattr;
901 int error;
904 * Determine the starting point of the request. Assume that it is
905 * a valid starting point.
907 switch (flp->l_whence) {
908 case 0: /* SEEK_SET */
909 *start = (uoff_t)flp->l_start;
910 break;
911 case 1: /* SEEK_CUR */
912 *start = (uoff_t)(flp->l_start + offset);
913 break;
914 case 2: /* SEEK_END */
915 vattr.va_mask = AT_SIZE;
916 if (error = fop_getattr(vp, &vattr, 0, CRED(), NULL))
917 return (error);
918 *start = (uoff_t)(flp->l_start + (offset_t)vattr.va_size);
919 break;
920 default:
921 return (EINVAL);
924 return (0);
928 * Take rctl action when the requested file descriptor is too big.
930 static void
931 fd_too_big(proc_t *p)
933 mutex_enter(&p->p_lock);
934 (void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
935 p->p_rctls, p, RCA_SAFE);
936 mutex_exit(&p->p_lock);