Print this page
XXXX adding PID information to netstat output
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/syscall/fcntl.c
+++ new/usr/src/uts/common/syscall/fcntl.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved.
25 25 * Copyright 2015, Joyent, Inc.
26 26 */
27 27
28 28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 29 /* All Rights Reserved */
30 30
31 31 /*
32 32 * Portions of this source code were derived from Berkeley 4.3 BSD
33 33 * under license from the Regents of the University of California.
34 34 */
35 35
36 36
37 37 #include <sys/param.h>
38 38 #include <sys/isa_defs.h>
39 39 #include <sys/types.h>
40 40 #include <sys/sysmacros.h>
41 41 #include <sys/systm.h>
42 42 #include <sys/errno.h>
43 43 #include <sys/fcntl.h>
44 44 #include <sys/flock.h>
45 45 #include <sys/vnode.h>
46 46 #include <sys/file.h>
47 47 #include <sys/mode.h>
48 48 #include <sys/proc.h>
49 49 #include <sys/filio.h>
50 50 #include <sys/share.h>
51 51 #include <sys/debug.h>
52 52 #include <sys/rctl.h>
53 53 #include <sys/nbmlock.h>
54 54
55 55 #include <sys/cmn_err.h>
56 56
57 57 static int flock_check(vnode_t *, flock64_t *, offset_t, offset_t);
58 58 static int flock_get_start(vnode_t *, flock64_t *, offset_t, u_offset_t *);
59 59 static void fd_too_big(proc_t *);
60 60
61 61 /*
62 62 * File control.
63 63 */
64 64 int
65 65 fcntl(int fdes, int cmd, intptr_t arg)
66 66 {
67 67 int iarg;
68 68 int error = 0;
69 69 int retval;
70 70 proc_t *p;
71 71 file_t *fp;
72 72 vnode_t *vp;
73 73 u_offset_t offset;
74 74 u_offset_t start;
75 75 struct vattr vattr;
76 76 int in_crit;
77 77 int flag;
78 78 struct flock sbf;
79 79 struct flock64 bf;
80 80 struct o_flock obf;
81 81 struct flock64_32 bf64_32;
82 82 struct fshare fsh;
83 83 struct shrlock shr;
84 84 struct shr_locowner shr_own;
85 85 offset_t maxoffset;
86 86 model_t datamodel;
87 87 int fdres;
88 88
89 89 #if defined(_ILP32) && !defined(lint) && defined(_SYSCALL32)
90 90 ASSERT(sizeof (struct flock) == sizeof (struct flock32));
91 91 ASSERT(sizeof (struct flock64) == sizeof (struct flock64_32));
92 92 #endif
93 93 #if defined(_LP64) && !defined(lint) && defined(_SYSCALL32)
94 94 ASSERT(sizeof (struct flock) == sizeof (struct flock64_64));
95 95 ASSERT(sizeof (struct flock64) == sizeof (struct flock64_64));
96 96 #endif
97 97
98 98 /*
99 99 * First, for speed, deal with the subset of cases
100 100 * that do not require getf() / releasef().
101 101 */
102 102 switch (cmd) {
103 103 case F_GETFD:
104 104 if ((error = f_getfd_error(fdes, &flag)) == 0)
105 105 retval = flag;
106 106 goto out;
107 107
108 108 case F_SETFD:
109 109 error = f_setfd_error(fdes, (int)arg);
110 110 retval = 0;
111 111 goto out;
112 112
113 113 case F_GETFL:
114 114 if ((error = f_getfl(fdes, &flag)) == 0) {
115 115 retval = (flag & (FMASK | FASYNC));
116 116 if ((flag & (FSEARCH | FEXEC)) == 0)
117 117 retval += FOPEN;
118 118 else
119 119 retval |= (flag & (FSEARCH | FEXEC));
120 120 }
121 121 goto out;
122 122
123 123 case F_GETXFL:
124 124 if ((error = f_getfl(fdes, &flag)) == 0) {
125 125 retval = flag;
126 126 if ((flag & (FSEARCH | FEXEC)) == 0)
127 127 retval += FOPEN;
128 128 }
129 129 goto out;
130 130
131 131 case F_BADFD:
132 132 if ((error = f_badfd(fdes, &fdres, (int)arg)) == 0)
133 133 retval = fdres;
134 134 goto out;
135 135 }
136 136
137 137 /*
138 138 * Second, for speed, deal with the subset of cases that
139 139 * require getf() / releasef() but do not require copyin.
140 140 */
141 141 if ((fp = getf(fdes)) == NULL) {
142 142 error = EBADF;
143 143 goto out;
144 144 }
145 145 iarg = (int)arg;
146 146
147 147 switch (cmd) {
148 148 case F_DUPFD:
149 149 case F_DUPFD_CLOEXEC:
150 150 p = curproc;
151 151 if ((uint_t)iarg >= p->p_fno_ctl) {
152 152 if (iarg >= 0)
153 153 fd_too_big(p);
154 154 error = EINVAL;
155 155 goto done;
156 156 }
157 157 /*
158 158 * We need to increment the f_count reference counter
159 159 * before allocating a new file descriptor.
160 160 * Doing it other way round opens a window for race condition
161 161 * with closeandsetf() on the target file descriptor which can
162 162 * close the file still referenced by the original
163 163 * file descriptor.
164 164 */
165 165 mutex_enter(&fp->f_tlock);
166 166 fp->f_count++;
167 167 mutex_exit(&fp->f_tlock);
168 168 if ((retval = ufalloc_file(iarg, fp)) == -1) {
169 169 /*
170 170 * New file descriptor can't be allocated.
171 171 * Revert the reference count.
↓ open down ↓ |
171 lines elided |
↑ open up ↑ |
172 172 */
173 173 mutex_enter(&fp->f_tlock);
174 174 fp->f_count--;
175 175 mutex_exit(&fp->f_tlock);
176 176 error = EMFILE;
177 177 } else {
178 178 if (cmd == F_DUPFD_CLOEXEC) {
179 179 f_setfd(retval, FD_CLOEXEC);
180 180 }
181 181 }
182 +
183 + if (error == 0 && fp->f_vnode != NULL) {
184 + (void) VOP_IOCTL(fp->f_vnode, F_ASSOCI_PID,
185 + (intptr_t)p->p_pidp->pid_id, FKIOCTL, kcred,
186 + NULL, NULL);
187 + }
188 +
182 189 goto done;
183 190
184 191 case F_DUP2FD_CLOEXEC:
185 192 if (fdes == iarg) {
186 193 error = EINVAL;
187 194 goto done;
188 195 }
189 196
190 197 /*FALLTHROUGH*/
191 198
192 199 case F_DUP2FD:
193 200 p = curproc;
194 201 if (fdes == iarg) {
195 202 retval = iarg;
196 203 } else if ((uint_t)iarg >= p->p_fno_ctl) {
197 204 if (iarg >= 0)
198 205 fd_too_big(p);
199 206 error = EBADF;
200 207 } else {
201 208 /*
202 209 * We can't hold our getf(fdes) across the call to
203 210 * closeandsetf() because it creates a window for
204 211 * deadlock: if one thread is doing dup2(a, b) while
205 212 * another is doing dup2(b, a), each one will block
206 213 * waiting for the other to call releasef(). The
207 214 * solution is to increment the file reference count
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
208 215 * (which we have to do anyway), then releasef(fdes),
209 216 * then closeandsetf(). Incrementing f_count ensures
210 217 * that fp won't disappear after we call releasef().
211 218 * When closeandsetf() fails, we try avoid calling
212 219 * closef() because of all the side effects.
213 220 */
214 221 mutex_enter(&fp->f_tlock);
215 222 fp->f_count++;
216 223 mutex_exit(&fp->f_tlock);
217 224 releasef(fdes);
225 +
226 + /* assume we have forked successfully */
227 + if (fp->f_vnode != NULL) {
228 + (void) VOP_IOCTL(fp->f_vnode, F_ASSOCI_PID,
229 + (intptr_t)p->p_pidp->pid_id, FKIOCTL,
230 + kcred, NULL, NULL);
231 + }
232 +
218 233 if ((error = closeandsetf(iarg, fp)) == 0) {
219 234 if (cmd == F_DUP2FD_CLOEXEC) {
220 235 f_setfd(iarg, FD_CLOEXEC);
221 236 }
222 237 retval = iarg;
223 238 } else {
224 239 mutex_enter(&fp->f_tlock);
225 240 if (fp->f_count > 1) {
226 241 fp->f_count--;
227 242 mutex_exit(&fp->f_tlock);
243 + if (fp->f_vnode != NULL) {
244 + (void) VOP_IOCTL(fp->f_vnode,
245 + F_DASSOC_PID,
246 + (intptr_t)p->p_pidp->pid_id,
247 + FKIOCTL, kcred, NULL, NULL);
248 + }
249 +
228 250 } else {
229 251 mutex_exit(&fp->f_tlock);
230 252 (void) closef(fp);
231 253 }
232 254 }
233 255 goto out;
234 256 }
235 257 goto done;
236 258
237 259 case F_SETFL:
238 260 vp = fp->f_vnode;
239 261 flag = fp->f_flag;
240 262 if ((iarg & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY))
241 263 iarg &= ~FNDELAY;
242 264 if ((error = VOP_SETFL(vp, flag, iarg, fp->f_cred, NULL)) ==
243 265 0) {
244 266 iarg &= FMASK;
245 267 mutex_enter(&fp->f_tlock);
246 268 fp->f_flag &= ~FMASK | (FREAD|FWRITE);
247 269 fp->f_flag |= (iarg - FOPEN) & ~(FREAD|FWRITE);
248 270 mutex_exit(&fp->f_tlock);
249 271 }
250 272 retval = 0;
251 273 goto done;
252 274 }
253 275
254 276 /*
255 277 * Finally, deal with the expensive cases.
256 278 */
257 279 retval = 0;
258 280 in_crit = 0;
259 281 maxoffset = MAXOFF_T;
260 282 datamodel = DATAMODEL_NATIVE;
261 283 #if defined(_SYSCALL32_IMPL)
262 284 if ((datamodel = get_udatamodel()) == DATAMODEL_ILP32)
263 285 maxoffset = MAXOFF32_T;
264 286 #endif
265 287
266 288 vp = fp->f_vnode;
267 289 flag = fp->f_flag;
268 290 offset = fp->f_offset;
269 291
270 292 switch (cmd) {
271 293 /*
272 294 * The file system and vnode layers understand and implement
273 295 * locking with flock64 structures. So here once we pass through
274 296 * the test for compatibility as defined by LFS API, (for F_SETLK,
275 297 * F_SETLKW, F_GETLK, F_GETLKW, F_OFD_GETLK, F_OFD_SETLK, F_OFD_SETLKW,
276 298 * F_FREESP) we transform the flock structure to a flock64 structure
277 299 * and send it to the lower layers. Similarly in case of GETLK and
278 300 * OFD_GETLK the returned flock64 structure is transformed to a flock
279 301 * structure if everything fits in nicely, otherwise we return
280 302 * EOVERFLOW.
281 303 */
282 304
283 305 case F_GETLK:
284 306 case F_O_GETLK:
285 307 case F_SETLK:
286 308 case F_SETLKW:
287 309 case F_SETLK_NBMAND:
288 310 case F_OFD_GETLK:
289 311 case F_OFD_SETLK:
290 312 case F_OFD_SETLKW:
291 313 case F_FLOCK:
292 314 case F_FLOCKW:
293 315
294 316 /*
295 317 * Copy in input fields only.
296 318 */
297 319
298 320 if (cmd == F_O_GETLK) {
299 321 if (datamodel != DATAMODEL_ILP32) {
300 322 error = EINVAL;
301 323 break;
302 324 }
303 325
304 326 if (copyin((void *)arg, &obf, sizeof (obf))) {
305 327 error = EFAULT;
306 328 break;
307 329 }
308 330 bf.l_type = obf.l_type;
309 331 bf.l_whence = obf.l_whence;
310 332 bf.l_start = (off64_t)obf.l_start;
311 333 bf.l_len = (off64_t)obf.l_len;
312 334 bf.l_sysid = (int)obf.l_sysid;
313 335 bf.l_pid = obf.l_pid;
314 336 } else if (datamodel == DATAMODEL_NATIVE) {
315 337 if (copyin((void *)arg, &sbf, sizeof (sbf))) {
316 338 error = EFAULT;
317 339 break;
318 340 }
319 341 /*
320 342 * XXX In an LP64 kernel with an LP64 application
321 343 * there's no need to do a structure copy here
322 344 * struct flock == struct flock64. However,
323 345 * we did it this way to avoid more conditional
324 346 * compilation.
325 347 */
326 348 bf.l_type = sbf.l_type;
327 349 bf.l_whence = sbf.l_whence;
328 350 bf.l_start = (off64_t)sbf.l_start;
329 351 bf.l_len = (off64_t)sbf.l_len;
330 352 bf.l_sysid = sbf.l_sysid;
331 353 bf.l_pid = sbf.l_pid;
332 354 }
333 355 #if defined(_SYSCALL32_IMPL)
334 356 else {
335 357 struct flock32 sbf32;
336 358 if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
337 359 error = EFAULT;
338 360 break;
339 361 }
340 362 bf.l_type = sbf32.l_type;
341 363 bf.l_whence = sbf32.l_whence;
342 364 bf.l_start = (off64_t)sbf32.l_start;
343 365 bf.l_len = (off64_t)sbf32.l_len;
344 366 bf.l_sysid = sbf32.l_sysid;
345 367 bf.l_pid = sbf32.l_pid;
346 368 }
347 369 #endif /* _SYSCALL32_IMPL */
348 370
349 371 /*
350 372 * 64-bit support: check for overflow for 32-bit lock ops
351 373 */
352 374 if ((error = flock_check(vp, &bf, offset, maxoffset)) != 0)
353 375 break;
354 376
355 377 if (cmd == F_FLOCK || cmd == F_FLOCKW) {
356 378 /* FLOCK* locking is always over the entire file. */
357 379 if (bf.l_whence != 0 || bf.l_start != 0 ||
358 380 bf.l_len != 0) {
359 381 error = EINVAL;
360 382 break;
361 383 }
362 384 if (bf.l_type < F_RDLCK || bf.l_type > F_UNLCK) {
363 385 error = EINVAL;
364 386 break;
365 387 }
366 388 }
367 389
368 390 if (cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
369 391 /*
370 392 * TBD OFD-style locking is currently limited to
371 393 * covering the entire file.
372 394 */
373 395 if (bf.l_whence != 0 || bf.l_start != 0 ||
374 396 bf.l_len != 0) {
375 397 error = EINVAL;
376 398 break;
377 399 }
378 400 }
379 401
380 402 /*
381 403 * Not all of the filesystems understand F_O_GETLK, and
382 404 * there's no need for them to know. Map it to F_GETLK.
383 405 *
384 406 * The *_frlock functions in the various file systems basically
385 407 * do some validation and then funnel everything through the
386 408 * fs_frlock function. For OFD-style locks fs_frlock will do
387 409 * nothing so that once control returns here we can call the
388 410 * ofdlock function with the correct fp. For OFD-style locks
389 411 * the unsupported remote file systems, such as NFS, detect and
390 412 * reject the OFD-style cmd argument.
391 413 */
392 414 if ((error = VOP_FRLOCK(vp, (cmd == F_O_GETLK) ? F_GETLK : cmd,
393 415 &bf, flag, offset, NULL, fp->f_cred, NULL)) != 0)
394 416 break;
395 417
396 418 if (cmd == F_FLOCK || cmd == F_FLOCKW || cmd == F_OFD_GETLK ||
397 419 cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
398 420 /*
399 421 * This is an OFD-style lock so we need to handle it
400 422 * here. Because OFD-style locks are associated with
401 423 * the file_t we didn't have enough info down the
402 424 * VOP_FRLOCK path immediately above.
403 425 */
404 426 if ((error = ofdlock(fp, cmd, &bf, flag, offset)) != 0)
405 427 break;
406 428 }
407 429
408 430 /*
409 431 * If command is GETLK and no lock is found, only
410 432 * the type field is changed.
411 433 */
412 434 if ((cmd == F_O_GETLK || cmd == F_GETLK ||
413 435 cmd == F_OFD_GETLK) && bf.l_type == F_UNLCK) {
414 436 /* l_type always first entry, always a short */
415 437 if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
416 438 sizeof (bf.l_type)))
417 439 error = EFAULT;
418 440 break;
419 441 }
420 442
421 443 if (cmd == F_O_GETLK) {
422 444 /*
423 445 * Return an SVR3 flock structure to the user.
424 446 */
425 447 obf.l_type = (int16_t)bf.l_type;
426 448 obf.l_whence = (int16_t)bf.l_whence;
427 449 obf.l_start = (int32_t)bf.l_start;
428 450 obf.l_len = (int32_t)bf.l_len;
429 451 if (bf.l_sysid > SHRT_MAX || bf.l_pid > SHRT_MAX) {
430 452 /*
431 453 * One or both values for the above fields
432 454 * is too large to store in an SVR3 flock
433 455 * structure.
434 456 */
435 457 error = EOVERFLOW;
436 458 break;
437 459 }
438 460 obf.l_sysid = (int16_t)bf.l_sysid;
439 461 obf.l_pid = (int16_t)bf.l_pid;
440 462 if (copyout(&obf, (void *)arg, sizeof (obf)))
441 463 error = EFAULT;
442 464 } else if (cmd == F_GETLK || cmd == F_OFD_GETLK) {
443 465 /*
444 466 * Copy out SVR4 flock.
445 467 */
446 468 int i;
447 469
448 470 if (bf.l_start > maxoffset || bf.l_len > maxoffset) {
449 471 error = EOVERFLOW;
450 472 break;
451 473 }
452 474
453 475 if (datamodel == DATAMODEL_NATIVE) {
454 476 for (i = 0; i < 4; i++)
455 477 sbf.l_pad[i] = 0;
456 478 /*
457 479 * XXX In an LP64 kernel with an LP64
458 480 * application there's no need to do a
459 481 * structure copy here as currently
460 482 * struct flock == struct flock64.
461 483 * We did it this way to avoid more
462 484 * conditional compilation.
463 485 */
464 486 sbf.l_type = bf.l_type;
465 487 sbf.l_whence = bf.l_whence;
466 488 sbf.l_start = (off_t)bf.l_start;
467 489 sbf.l_len = (off_t)bf.l_len;
468 490 sbf.l_sysid = bf.l_sysid;
469 491 sbf.l_pid = bf.l_pid;
470 492 if (copyout(&sbf, (void *)arg, sizeof (sbf)))
471 493 error = EFAULT;
472 494 }
473 495 #if defined(_SYSCALL32_IMPL)
474 496 else {
475 497 struct flock32 sbf32;
476 498 if (bf.l_start > MAXOFF32_T ||
477 499 bf.l_len > MAXOFF32_T) {
478 500 error = EOVERFLOW;
479 501 break;
480 502 }
481 503 for (i = 0; i < 4; i++)
482 504 sbf32.l_pad[i] = 0;
483 505 sbf32.l_type = (int16_t)bf.l_type;
484 506 sbf32.l_whence = (int16_t)bf.l_whence;
485 507 sbf32.l_start = (off32_t)bf.l_start;
486 508 sbf32.l_len = (off32_t)bf.l_len;
487 509 sbf32.l_sysid = (int32_t)bf.l_sysid;
488 510 sbf32.l_pid = (pid32_t)bf.l_pid;
489 511 if (copyout(&sbf32,
490 512 (void *)arg, sizeof (sbf32)))
491 513 error = EFAULT;
492 514 }
493 515 #endif
494 516 }
495 517 break;
496 518
497 519 case F_CHKFL:
498 520 /*
499 521 * This is for internal use only, to allow the vnode layer
500 522 * to validate a flags setting before applying it. User
501 523 * programs can't issue it.
502 524 */
503 525 error = EINVAL;
504 526 break;
505 527
506 528 case F_ALLOCSP:
507 529 case F_FREESP:
508 530 case F_ALLOCSP64:
509 531 case F_FREESP64:
510 532 /*
511 533 * Test for not-a-regular-file (and returning EINVAL)
512 534 * before testing for open-for-writing (and returning EBADF).
513 535 * This is relied upon by posix_fallocate() in libc.
514 536 */
515 537 if (vp->v_type != VREG) {
516 538 error = EINVAL;
517 539 break;
518 540 }
519 541
520 542 if ((flag & FWRITE) == 0) {
521 543 error = EBADF;
522 544 break;
523 545 }
524 546
525 547 if (datamodel != DATAMODEL_ILP32 &&
526 548 (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
527 549 error = EINVAL;
528 550 break;
529 551 }
530 552
531 553 #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
532 554 if (datamodel == DATAMODEL_ILP32 &&
533 555 (cmd == F_ALLOCSP || cmd == F_FREESP)) {
534 556 struct flock32 sbf32;
535 557 /*
536 558 * For compatibility we overlay an SVR3 flock on an SVR4
537 559 * flock. This works because the input field offsets
538 560 * in "struct flock" were preserved.
539 561 */
540 562 if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
541 563 error = EFAULT;
542 564 break;
543 565 } else {
544 566 bf.l_type = sbf32.l_type;
545 567 bf.l_whence = sbf32.l_whence;
546 568 bf.l_start = (off64_t)sbf32.l_start;
547 569 bf.l_len = (off64_t)sbf32.l_len;
548 570 bf.l_sysid = sbf32.l_sysid;
549 571 bf.l_pid = sbf32.l_pid;
550 572 }
551 573 }
552 574 #endif /* _ILP32 || _SYSCALL32_IMPL */
553 575
554 576 #if defined(_LP64)
555 577 if (datamodel == DATAMODEL_LP64 &&
556 578 (cmd == F_ALLOCSP || cmd == F_FREESP)) {
557 579 if (copyin((void *)arg, &bf, sizeof (bf))) {
558 580 error = EFAULT;
559 581 break;
560 582 }
561 583 }
562 584 #endif /* defined(_LP64) */
563 585
564 586 #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
565 587 if (datamodel == DATAMODEL_ILP32 &&
566 588 (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
567 589 if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
568 590 error = EFAULT;
569 591 break;
570 592 } else {
571 593 /*
572 594 * Note that the size of flock64 is different in
573 595 * the ILP32 and LP64 models, due to the l_pad
574 596 * field. We do not want to assume that the
575 597 * flock64 structure is laid out the same in
576 598 * ILP32 and LP64 environments, so we will
577 599 * copy in the ILP32 version of flock64
578 600 * explicitly and copy it to the native
579 601 * flock64 structure.
580 602 */
581 603 bf.l_type = (short)bf64_32.l_type;
582 604 bf.l_whence = (short)bf64_32.l_whence;
583 605 bf.l_start = bf64_32.l_start;
584 606 bf.l_len = bf64_32.l_len;
585 607 bf.l_sysid = (int)bf64_32.l_sysid;
586 608 bf.l_pid = (pid_t)bf64_32.l_pid;
587 609 }
588 610 }
589 611 #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
590 612
591 613 if (cmd == F_ALLOCSP || cmd == F_FREESP)
592 614 error = flock_check(vp, &bf, offset, maxoffset);
593 615 else if (cmd == F_ALLOCSP64 || cmd == F_FREESP64)
594 616 error = flock_check(vp, &bf, offset, MAXOFFSET_T);
595 617 if (error)
596 618 break;
597 619
598 620 if (vp->v_type == VREG && bf.l_len == 0 &&
599 621 bf.l_start > OFFSET_MAX(fp)) {
600 622 error = EFBIG;
601 623 break;
602 624 }
603 625
604 626 /*
605 627 * Make sure that there are no conflicting non-blocking
606 628 * mandatory locks in the region being manipulated. If
607 629 * there are such locks then return EACCES.
608 630 */
609 631 if ((error = flock_get_start(vp, &bf, offset, &start)) != 0)
610 632 break;
611 633
612 634 if (nbl_need_check(vp)) {
613 635 u_offset_t begin;
614 636 ssize_t length;
615 637
616 638 nbl_start_crit(vp, RW_READER);
617 639 in_crit = 1;
618 640 vattr.va_mask = AT_SIZE;
619 641 if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
620 642 != 0)
621 643 break;
622 644 begin = start > vattr.va_size ? vattr.va_size : start;
623 645 length = vattr.va_size > start ? vattr.va_size - start :
624 646 start - vattr.va_size;
625 647 if (nbl_conflict(vp, NBL_WRITE, begin, length, 0,
626 648 NULL)) {
627 649 error = EACCES;
628 650 break;
629 651 }
630 652 }
631 653
632 654 if (cmd == F_ALLOCSP64)
633 655 cmd = F_ALLOCSP;
634 656 else if (cmd == F_FREESP64)
635 657 cmd = F_FREESP;
636 658
637 659 error = VOP_SPACE(vp, cmd, &bf, flag, offset, fp->f_cred, NULL);
638 660
639 661 break;
640 662
641 663 #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
642 664 case F_GETLK64:
643 665 case F_SETLK64:
644 666 case F_SETLKW64:
645 667 case F_SETLK64_NBMAND:
646 668 case F_OFD_GETLK64:
647 669 case F_OFD_SETLK64:
648 670 case F_OFD_SETLKW64:
649 671 case F_FLOCK64:
650 672 case F_FLOCKW64:
651 673 /*
652 674 * Large Files: Here we set cmd as *LK and send it to
653 675 * lower layers. *LK64 is only for the user land.
654 676 * Most of the comments described above for F_SETLK
655 677 * applies here too.
656 678 * Large File support is only needed for ILP32 apps!
657 679 */
658 680 if (datamodel != DATAMODEL_ILP32) {
659 681 error = EINVAL;
660 682 break;
661 683 }
662 684
663 685 if (cmd == F_GETLK64)
664 686 cmd = F_GETLK;
665 687 else if (cmd == F_SETLK64)
666 688 cmd = F_SETLK;
667 689 else if (cmd == F_SETLKW64)
668 690 cmd = F_SETLKW;
669 691 else if (cmd == F_SETLK64_NBMAND)
670 692 cmd = F_SETLK_NBMAND;
671 693 else if (cmd == F_OFD_GETLK64)
672 694 cmd = F_OFD_GETLK;
673 695 else if (cmd == F_OFD_SETLK64)
674 696 cmd = F_OFD_SETLK;
675 697 else if (cmd == F_OFD_SETLKW64)
676 698 cmd = F_OFD_SETLKW;
677 699 else if (cmd == F_FLOCK64)
678 700 cmd = F_FLOCK;
679 701 else if (cmd == F_FLOCKW64)
680 702 cmd = F_FLOCKW;
681 703
682 704 /*
683 705 * Note that the size of flock64 is different in the ILP32
684 706 * and LP64 models, due to the sucking l_pad field.
685 707 * We do not want to assume that the flock64 structure is
686 708 * laid out in the same in ILP32 and LP64 environments, so
687 709 * we will copy in the ILP32 version of flock64 explicitly
688 710 * and copy it to the native flock64 structure.
689 711 */
690 712
691 713 if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
692 714 error = EFAULT;
693 715 break;
694 716 }
695 717
696 718 bf.l_type = (short)bf64_32.l_type;
697 719 bf.l_whence = (short)bf64_32.l_whence;
698 720 bf.l_start = bf64_32.l_start;
699 721 bf.l_len = bf64_32.l_len;
700 722 bf.l_sysid = (int)bf64_32.l_sysid;
701 723 bf.l_pid = (pid_t)bf64_32.l_pid;
702 724
703 725 if ((error = flock_check(vp, &bf, offset, MAXOFFSET_T)) != 0)
704 726 break;
705 727
706 728 if (cmd == F_FLOCK || cmd == F_FLOCKW) {
707 729 /* FLOCK* locking is always over the entire file. */
708 730 if (bf.l_whence != 0 || bf.l_start != 0 ||
709 731 bf.l_len != 0) {
710 732 error = EINVAL;
711 733 break;
712 734 }
713 735 if (bf.l_type < F_RDLCK || bf.l_type > F_UNLCK) {
714 736 error = EINVAL;
715 737 break;
716 738 }
717 739 }
718 740
719 741 if (cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
720 742 /*
721 743 * TBD OFD-style locking is currently limited to
722 744 * covering the entire file.
723 745 */
724 746 if (bf.l_whence != 0 || bf.l_start != 0 ||
725 747 bf.l_len != 0) {
726 748 error = EINVAL;
727 749 break;
728 750 }
729 751 }
730 752
731 753 /*
732 754 * The *_frlock functions in the various file systems basically
733 755 * do some validation and then funnel everything through the
734 756 * fs_frlock function. For OFD-style locks fs_frlock will do
735 757 * nothing so that once control returns here we can call the
736 758 * ofdlock function with the correct fp. For OFD-style locks
737 759 * the unsupported remote file systems, such as NFS, detect and
738 760 * reject the OFD-style cmd argument.
739 761 */
740 762 if ((error = VOP_FRLOCK(vp, cmd, &bf, flag, offset,
741 763 NULL, fp->f_cred, NULL)) != 0)
742 764 break;
743 765
744 766 if (cmd == F_FLOCK || cmd == F_FLOCKW || cmd == F_OFD_GETLK ||
745 767 cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
746 768 /*
747 769 * This is an OFD-style lock so we need to handle it
748 770 * here. Because OFD-style locks are associated with
749 771 * the file_t we didn't have enough info down the
750 772 * VOP_FRLOCK path immediately above.
751 773 */
752 774 if ((error = ofdlock(fp, cmd, &bf, flag, offset)) != 0)
753 775 break;
754 776 }
755 777
756 778 if ((cmd == F_GETLK || cmd == F_OFD_GETLK) &&
757 779 bf.l_type == F_UNLCK) {
758 780 if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
759 781 sizeof (bf.l_type)))
760 782 error = EFAULT;
761 783 break;
762 784 }
763 785
764 786 if (cmd == F_GETLK || cmd == F_OFD_GETLK) {
765 787 int i;
766 788
767 789 /*
768 790 * We do not want to assume that the flock64 structure
769 791 * is laid out in the same in ILP32 and LP64
770 792 * environments, so we will copy out the ILP32 version
771 793 * of flock64 explicitly after copying the native
772 794 * flock64 structure to it.
773 795 */
774 796 for (i = 0; i < 4; i++)
775 797 bf64_32.l_pad[i] = 0;
776 798 bf64_32.l_type = (int16_t)bf.l_type;
777 799 bf64_32.l_whence = (int16_t)bf.l_whence;
778 800 bf64_32.l_start = bf.l_start;
779 801 bf64_32.l_len = bf.l_len;
780 802 bf64_32.l_sysid = (int32_t)bf.l_sysid;
781 803 bf64_32.l_pid = (pid32_t)bf.l_pid;
782 804 if (copyout(&bf64_32, (void *)arg, sizeof (bf64_32)))
783 805 error = EFAULT;
784 806 }
785 807 break;
786 808 #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
787 809
788 810 case F_SHARE:
789 811 case F_SHARE_NBMAND:
790 812 case F_UNSHARE:
791 813
792 814 /*
793 815 * Copy in input fields only.
794 816 */
795 817 if (copyin((void *)arg, &fsh, sizeof (fsh))) {
796 818 error = EFAULT;
797 819 break;
798 820 }
799 821
800 822 /*
801 823 * Local share reservations always have this simple form
802 824 */
803 825 shr.s_access = fsh.f_access;
804 826 shr.s_deny = fsh.f_deny;
805 827 shr.s_sysid = 0;
806 828 shr.s_pid = ttoproc(curthread)->p_pid;
807 829 shr_own.sl_pid = shr.s_pid;
808 830 shr_own.sl_id = fsh.f_id;
809 831 shr.s_own_len = sizeof (shr_own);
810 832 shr.s_owner = (caddr_t)&shr_own;
811 833 error = VOP_SHRLOCK(vp, cmd, &shr, flag, fp->f_cred, NULL);
812 834 break;
813 835
814 836 default:
815 837 error = EINVAL;
816 838 break;
817 839 }
818 840
819 841 if (in_crit)
820 842 nbl_end_crit(vp);
821 843
822 844 done:
823 845 releasef(fdes);
824 846 out:
825 847 if (error)
826 848 return (set_errno(error));
827 849 return (retval);
828 850 }
829 851
830 852 int
831 853 flock_check(vnode_t *vp, flock64_t *flp, offset_t offset, offset_t max)
832 854 {
833 855 struct vattr vattr;
834 856 int error;
835 857 u_offset_t start, end;
836 858
837 859 /*
838 860 * Determine the starting point of the request
839 861 */
840 862 switch (flp->l_whence) {
841 863 case 0: /* SEEK_SET */
842 864 start = (u_offset_t)flp->l_start;
843 865 if (start > max)
844 866 return (EINVAL);
845 867 break;
846 868 case 1: /* SEEK_CUR */
847 869 if (flp->l_start > (max - offset))
848 870 return (EOVERFLOW);
849 871 start = (u_offset_t)(flp->l_start + offset);
850 872 if (start > max)
851 873 return (EINVAL);
852 874 break;
853 875 case 2: /* SEEK_END */
854 876 vattr.va_mask = AT_SIZE;
855 877 if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
856 878 return (error);
857 879 if (flp->l_start > (max - (offset_t)vattr.va_size))
858 880 return (EOVERFLOW);
859 881 start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
860 882 if (start > max)
861 883 return (EINVAL);
862 884 break;
863 885 default:
864 886 return (EINVAL);
865 887 }
866 888
867 889 /*
868 890 * Determine the range covered by the request.
869 891 */
870 892 if (flp->l_len == 0)
871 893 end = MAXEND;
872 894 else if ((offset_t)flp->l_len > 0) {
873 895 if (flp->l_len > (max - start + 1))
874 896 return (EOVERFLOW);
875 897 end = (u_offset_t)(start + (flp->l_len - 1));
876 898 ASSERT(end <= max);
877 899 } else {
878 900 /*
879 901 * Negative length; why do we even allow this ?
880 902 * Because this allows easy specification of
881 903 * the last n bytes of the file.
882 904 */
883 905 end = start;
884 906 start += (u_offset_t)flp->l_len;
885 907 (start)++;
886 908 if (start > max)
887 909 return (EINVAL);
888 910 ASSERT(end <= max);
889 911 }
890 912 ASSERT(start <= max);
891 913 if (flp->l_type == F_UNLCK && flp->l_len > 0 &&
892 914 end == (offset_t)max) {
893 915 flp->l_len = 0;
894 916 }
895 917 if (start > end)
896 918 return (EINVAL);
897 919 return (0);
898 920 }
899 921
900 922 static int
901 923 flock_get_start(vnode_t *vp, flock64_t *flp, offset_t offset, u_offset_t *start)
902 924 {
903 925 struct vattr vattr;
904 926 int error;
905 927
906 928 /*
907 929 * Determine the starting point of the request. Assume that it is
908 930 * a valid starting point.
909 931 */
910 932 switch (flp->l_whence) {
911 933 case 0: /* SEEK_SET */
912 934 *start = (u_offset_t)flp->l_start;
913 935 break;
914 936 case 1: /* SEEK_CUR */
915 937 *start = (u_offset_t)(flp->l_start + offset);
916 938 break;
917 939 case 2: /* SEEK_END */
918 940 vattr.va_mask = AT_SIZE;
919 941 if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
920 942 return (error);
921 943 *start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
922 944 break;
923 945 default:
924 946 return (EINVAL);
925 947 }
926 948
927 949 return (0);
928 950 }
929 951
930 952 /*
931 953 * Take rctl action when the requested file descriptor is too big.
932 954 */
933 955 static void
934 956 fd_too_big(proc_t *p)
935 957 {
936 958 mutex_enter(&p->p_lock);
937 959 (void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
938 960 p->p_rctls, p, RCA_SAFE);
939 961 mutex_exit(&p->p_lock);
940 962 }
↓ open down ↓ |
703 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX