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 + /*
227 + * Assume we succeed to duplicate the file descriptor
228 + * and associate the pid to the vnode.
229 + */
230 + if (fp->f_vnode != NULL) {
231 + (void) VOP_IOCTL(fp->f_vnode, F_ASSOCI_PID,
232 + (intptr_t)p->p_pidp->pid_id, FKIOCTL,
233 + kcred, NULL, NULL);
234 + }
235 +
218 236 if ((error = closeandsetf(iarg, fp)) == 0) {
219 237 if (cmd == F_DUP2FD_CLOEXEC) {
220 238 f_setfd(iarg, FD_CLOEXEC);
221 239 }
222 240 retval = iarg;
223 241 } else {
224 242 mutex_enter(&fp->f_tlock);
225 243 if (fp->f_count > 1) {
226 244 fp->f_count--;
227 245 mutex_exit(&fp->f_tlock);
246 + /*
247 + * Failed to duplicate fdes,
248 + * disassociate the pid from the vnode.
249 + */
250 + if (fp->f_vnode != NULL) {
251 + (void) VOP_IOCTL(fp->f_vnode,
252 + F_DASSOC_PID,
253 + (intptr_t)p->p_pidp->pid_id,
254 + FKIOCTL, kcred, NULL, NULL);
255 + }
256 +
228 257 } else {
229 258 mutex_exit(&fp->f_tlock);
230 259 (void) closef(fp);
231 260 }
232 261 }
233 262 goto out;
234 263 }
235 264 goto done;
236 265
237 266 case F_SETFL:
238 267 vp = fp->f_vnode;
239 268 flag = fp->f_flag;
240 269 if ((iarg & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY))
241 270 iarg &= ~FNDELAY;
242 271 if ((error = VOP_SETFL(vp, flag, iarg, fp->f_cred, NULL)) ==
243 272 0) {
244 273 iarg &= FMASK;
245 274 mutex_enter(&fp->f_tlock);
246 275 fp->f_flag &= ~FMASK | (FREAD|FWRITE);
247 276 fp->f_flag |= (iarg - FOPEN) & ~(FREAD|FWRITE);
248 277 mutex_exit(&fp->f_tlock);
249 278 }
250 279 retval = 0;
251 280 goto done;
252 281 }
253 282
254 283 /*
255 284 * Finally, deal with the expensive cases.
256 285 */
257 286 retval = 0;
258 287 in_crit = 0;
259 288 maxoffset = MAXOFF_T;
260 289 datamodel = DATAMODEL_NATIVE;
261 290 #if defined(_SYSCALL32_IMPL)
262 291 if ((datamodel = get_udatamodel()) == DATAMODEL_ILP32)
263 292 maxoffset = MAXOFF32_T;
264 293 #endif
265 294
266 295 vp = fp->f_vnode;
267 296 flag = fp->f_flag;
268 297 offset = fp->f_offset;
269 298
270 299 switch (cmd) {
271 300 /*
272 301 * The file system and vnode layers understand and implement
273 302 * locking with flock64 structures. So here once we pass through
274 303 * the test for compatibility as defined by LFS API, (for F_SETLK,
275 304 * F_SETLKW, F_GETLK, F_GETLKW, F_OFD_GETLK, F_OFD_SETLK, F_OFD_SETLKW,
276 305 * F_FREESP) we transform the flock structure to a flock64 structure
277 306 * and send it to the lower layers. Similarly in case of GETLK and
278 307 * OFD_GETLK the returned flock64 structure is transformed to a flock
279 308 * structure if everything fits in nicely, otherwise we return
280 309 * EOVERFLOW.
281 310 */
282 311
283 312 case F_GETLK:
284 313 case F_O_GETLK:
285 314 case F_SETLK:
286 315 case F_SETLKW:
287 316 case F_SETLK_NBMAND:
288 317 case F_OFD_GETLK:
289 318 case F_OFD_SETLK:
290 319 case F_OFD_SETLKW:
291 320 case F_FLOCK:
292 321 case F_FLOCKW:
293 322
294 323 /*
295 324 * Copy in input fields only.
296 325 */
297 326
298 327 if (cmd == F_O_GETLK) {
299 328 if (datamodel != DATAMODEL_ILP32) {
300 329 error = EINVAL;
301 330 break;
302 331 }
303 332
304 333 if (copyin((void *)arg, &obf, sizeof (obf))) {
305 334 error = EFAULT;
306 335 break;
307 336 }
308 337 bf.l_type = obf.l_type;
309 338 bf.l_whence = obf.l_whence;
310 339 bf.l_start = (off64_t)obf.l_start;
311 340 bf.l_len = (off64_t)obf.l_len;
312 341 bf.l_sysid = (int)obf.l_sysid;
313 342 bf.l_pid = obf.l_pid;
314 343 } else if (datamodel == DATAMODEL_NATIVE) {
315 344 if (copyin((void *)arg, &sbf, sizeof (sbf))) {
316 345 error = EFAULT;
317 346 break;
318 347 }
319 348 /*
320 349 * XXX In an LP64 kernel with an LP64 application
321 350 * there's no need to do a structure copy here
322 351 * struct flock == struct flock64. However,
323 352 * we did it this way to avoid more conditional
324 353 * compilation.
325 354 */
326 355 bf.l_type = sbf.l_type;
327 356 bf.l_whence = sbf.l_whence;
328 357 bf.l_start = (off64_t)sbf.l_start;
329 358 bf.l_len = (off64_t)sbf.l_len;
330 359 bf.l_sysid = sbf.l_sysid;
331 360 bf.l_pid = sbf.l_pid;
332 361 }
333 362 #if defined(_SYSCALL32_IMPL)
334 363 else {
335 364 struct flock32 sbf32;
336 365 if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
337 366 error = EFAULT;
338 367 break;
339 368 }
340 369 bf.l_type = sbf32.l_type;
341 370 bf.l_whence = sbf32.l_whence;
342 371 bf.l_start = (off64_t)sbf32.l_start;
343 372 bf.l_len = (off64_t)sbf32.l_len;
344 373 bf.l_sysid = sbf32.l_sysid;
345 374 bf.l_pid = sbf32.l_pid;
346 375 }
347 376 #endif /* _SYSCALL32_IMPL */
348 377
349 378 /*
350 379 * 64-bit support: check for overflow for 32-bit lock ops
351 380 */
352 381 if ((error = flock_check(vp, &bf, offset, maxoffset)) != 0)
353 382 break;
354 383
355 384 if (cmd == F_FLOCK || cmd == F_FLOCKW) {
356 385 /* FLOCK* locking is always over the entire file. */
357 386 if (bf.l_whence != 0 || bf.l_start != 0 ||
358 387 bf.l_len != 0) {
359 388 error = EINVAL;
360 389 break;
361 390 }
362 391 if (bf.l_type < F_RDLCK || bf.l_type > F_UNLCK) {
363 392 error = EINVAL;
364 393 break;
365 394 }
366 395 }
367 396
368 397 if (cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
369 398 /*
370 399 * TBD OFD-style locking is currently limited to
371 400 * covering the entire file.
372 401 */
373 402 if (bf.l_whence != 0 || bf.l_start != 0 ||
374 403 bf.l_len != 0) {
375 404 error = EINVAL;
376 405 break;
377 406 }
378 407 }
379 408
380 409 /*
381 410 * Not all of the filesystems understand F_O_GETLK, and
382 411 * there's no need for them to know. Map it to F_GETLK.
383 412 *
384 413 * The *_frlock functions in the various file systems basically
385 414 * do some validation and then funnel everything through the
386 415 * fs_frlock function. For OFD-style locks fs_frlock will do
387 416 * nothing so that once control returns here we can call the
388 417 * ofdlock function with the correct fp. For OFD-style locks
389 418 * the unsupported remote file systems, such as NFS, detect and
390 419 * reject the OFD-style cmd argument.
391 420 */
392 421 if ((error = VOP_FRLOCK(vp, (cmd == F_O_GETLK) ? F_GETLK : cmd,
393 422 &bf, flag, offset, NULL, fp->f_cred, NULL)) != 0)
394 423 break;
395 424
396 425 if (cmd == F_FLOCK || cmd == F_FLOCKW || cmd == F_OFD_GETLK ||
397 426 cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
398 427 /*
399 428 * This is an OFD-style lock so we need to handle it
400 429 * here. Because OFD-style locks are associated with
401 430 * the file_t we didn't have enough info down the
402 431 * VOP_FRLOCK path immediately above.
403 432 */
404 433 if ((error = ofdlock(fp, cmd, &bf, flag, offset)) != 0)
405 434 break;
406 435 }
407 436
408 437 /*
409 438 * If command is GETLK and no lock is found, only
410 439 * the type field is changed.
411 440 */
412 441 if ((cmd == F_O_GETLK || cmd == F_GETLK ||
413 442 cmd == F_OFD_GETLK) && bf.l_type == F_UNLCK) {
414 443 /* l_type always first entry, always a short */
415 444 if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
416 445 sizeof (bf.l_type)))
417 446 error = EFAULT;
418 447 break;
419 448 }
420 449
421 450 if (cmd == F_O_GETLK) {
422 451 /*
423 452 * Return an SVR3 flock structure to the user.
424 453 */
425 454 obf.l_type = (int16_t)bf.l_type;
426 455 obf.l_whence = (int16_t)bf.l_whence;
427 456 obf.l_start = (int32_t)bf.l_start;
428 457 obf.l_len = (int32_t)bf.l_len;
429 458 if (bf.l_sysid > SHRT_MAX || bf.l_pid > SHRT_MAX) {
430 459 /*
431 460 * One or both values for the above fields
432 461 * is too large to store in an SVR3 flock
433 462 * structure.
434 463 */
435 464 error = EOVERFLOW;
436 465 break;
437 466 }
438 467 obf.l_sysid = (int16_t)bf.l_sysid;
439 468 obf.l_pid = (int16_t)bf.l_pid;
440 469 if (copyout(&obf, (void *)arg, sizeof (obf)))
441 470 error = EFAULT;
442 471 } else if (cmd == F_GETLK || cmd == F_OFD_GETLK) {
443 472 /*
444 473 * Copy out SVR4 flock.
445 474 */
446 475 int i;
447 476
448 477 if (bf.l_start > maxoffset || bf.l_len > maxoffset) {
449 478 error = EOVERFLOW;
450 479 break;
451 480 }
452 481
453 482 if (datamodel == DATAMODEL_NATIVE) {
454 483 for (i = 0; i < 4; i++)
455 484 sbf.l_pad[i] = 0;
456 485 /*
457 486 * XXX In an LP64 kernel with an LP64
458 487 * application there's no need to do a
459 488 * structure copy here as currently
460 489 * struct flock == struct flock64.
461 490 * We did it this way to avoid more
462 491 * conditional compilation.
463 492 */
464 493 sbf.l_type = bf.l_type;
465 494 sbf.l_whence = bf.l_whence;
466 495 sbf.l_start = (off_t)bf.l_start;
467 496 sbf.l_len = (off_t)bf.l_len;
468 497 sbf.l_sysid = bf.l_sysid;
469 498 sbf.l_pid = bf.l_pid;
470 499 if (copyout(&sbf, (void *)arg, sizeof (sbf)))
471 500 error = EFAULT;
472 501 }
473 502 #if defined(_SYSCALL32_IMPL)
474 503 else {
475 504 struct flock32 sbf32;
476 505 if (bf.l_start > MAXOFF32_T ||
477 506 bf.l_len > MAXOFF32_T) {
478 507 error = EOVERFLOW;
479 508 break;
480 509 }
481 510 for (i = 0; i < 4; i++)
482 511 sbf32.l_pad[i] = 0;
483 512 sbf32.l_type = (int16_t)bf.l_type;
484 513 sbf32.l_whence = (int16_t)bf.l_whence;
485 514 sbf32.l_start = (off32_t)bf.l_start;
486 515 sbf32.l_len = (off32_t)bf.l_len;
487 516 sbf32.l_sysid = (int32_t)bf.l_sysid;
488 517 sbf32.l_pid = (pid32_t)bf.l_pid;
489 518 if (copyout(&sbf32,
490 519 (void *)arg, sizeof (sbf32)))
491 520 error = EFAULT;
492 521 }
493 522 #endif
494 523 }
495 524 break;
496 525
497 526 case F_CHKFL:
498 527 /*
499 528 * This is for internal use only, to allow the vnode layer
500 529 * to validate a flags setting before applying it. User
501 530 * programs can't issue it.
502 531 */
503 532 error = EINVAL;
504 533 break;
505 534
506 535 case F_ALLOCSP:
507 536 case F_FREESP:
508 537 case F_ALLOCSP64:
509 538 case F_FREESP64:
510 539 /*
511 540 * Test for not-a-regular-file (and returning EINVAL)
512 541 * before testing for open-for-writing (and returning EBADF).
513 542 * This is relied upon by posix_fallocate() in libc.
514 543 */
515 544 if (vp->v_type != VREG) {
516 545 error = EINVAL;
517 546 break;
518 547 }
519 548
520 549 if ((flag & FWRITE) == 0) {
521 550 error = EBADF;
522 551 break;
523 552 }
524 553
525 554 if (datamodel != DATAMODEL_ILP32 &&
526 555 (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
527 556 error = EINVAL;
528 557 break;
529 558 }
530 559
531 560 #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
532 561 if (datamodel == DATAMODEL_ILP32 &&
533 562 (cmd == F_ALLOCSP || cmd == F_FREESP)) {
534 563 struct flock32 sbf32;
535 564 /*
536 565 * For compatibility we overlay an SVR3 flock on an SVR4
537 566 * flock. This works because the input field offsets
538 567 * in "struct flock" were preserved.
539 568 */
540 569 if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
541 570 error = EFAULT;
542 571 break;
543 572 } else {
544 573 bf.l_type = sbf32.l_type;
545 574 bf.l_whence = sbf32.l_whence;
546 575 bf.l_start = (off64_t)sbf32.l_start;
547 576 bf.l_len = (off64_t)sbf32.l_len;
548 577 bf.l_sysid = sbf32.l_sysid;
549 578 bf.l_pid = sbf32.l_pid;
550 579 }
551 580 }
552 581 #endif /* _ILP32 || _SYSCALL32_IMPL */
553 582
554 583 #if defined(_LP64)
555 584 if (datamodel == DATAMODEL_LP64 &&
556 585 (cmd == F_ALLOCSP || cmd == F_FREESP)) {
557 586 if (copyin((void *)arg, &bf, sizeof (bf))) {
558 587 error = EFAULT;
559 588 break;
560 589 }
561 590 }
562 591 #endif /* defined(_LP64) */
563 592
564 593 #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
565 594 if (datamodel == DATAMODEL_ILP32 &&
566 595 (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
567 596 if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
568 597 error = EFAULT;
569 598 break;
570 599 } else {
571 600 /*
572 601 * Note that the size of flock64 is different in
573 602 * the ILP32 and LP64 models, due to the l_pad
574 603 * field. We do not want to assume that the
575 604 * flock64 structure is laid out the same in
576 605 * ILP32 and LP64 environments, so we will
577 606 * copy in the ILP32 version of flock64
578 607 * explicitly and copy it to the native
579 608 * flock64 structure.
580 609 */
581 610 bf.l_type = (short)bf64_32.l_type;
582 611 bf.l_whence = (short)bf64_32.l_whence;
583 612 bf.l_start = bf64_32.l_start;
584 613 bf.l_len = bf64_32.l_len;
585 614 bf.l_sysid = (int)bf64_32.l_sysid;
586 615 bf.l_pid = (pid_t)bf64_32.l_pid;
587 616 }
588 617 }
589 618 #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
590 619
591 620 if (cmd == F_ALLOCSP || cmd == F_FREESP)
592 621 error = flock_check(vp, &bf, offset, maxoffset);
593 622 else if (cmd == F_ALLOCSP64 || cmd == F_FREESP64)
594 623 error = flock_check(vp, &bf, offset, MAXOFFSET_T);
595 624 if (error)
596 625 break;
597 626
598 627 if (vp->v_type == VREG && bf.l_len == 0 &&
599 628 bf.l_start > OFFSET_MAX(fp)) {
600 629 error = EFBIG;
601 630 break;
602 631 }
603 632
604 633 /*
605 634 * Make sure that there are no conflicting non-blocking
606 635 * mandatory locks in the region being manipulated. If
607 636 * there are such locks then return EACCES.
608 637 */
609 638 if ((error = flock_get_start(vp, &bf, offset, &start)) != 0)
610 639 break;
611 640
612 641 if (nbl_need_check(vp)) {
613 642 u_offset_t begin;
614 643 ssize_t length;
615 644
616 645 nbl_start_crit(vp, RW_READER);
617 646 in_crit = 1;
618 647 vattr.va_mask = AT_SIZE;
619 648 if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
620 649 != 0)
621 650 break;
622 651 begin = start > vattr.va_size ? vattr.va_size : start;
623 652 length = vattr.va_size > start ? vattr.va_size - start :
624 653 start - vattr.va_size;
625 654 if (nbl_conflict(vp, NBL_WRITE, begin, length, 0,
626 655 NULL)) {
627 656 error = EACCES;
628 657 break;
629 658 }
630 659 }
631 660
632 661 if (cmd == F_ALLOCSP64)
633 662 cmd = F_ALLOCSP;
634 663 else if (cmd == F_FREESP64)
635 664 cmd = F_FREESP;
636 665
637 666 error = VOP_SPACE(vp, cmd, &bf, flag, offset, fp->f_cred, NULL);
638 667
639 668 break;
640 669
641 670 #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
642 671 case F_GETLK64:
643 672 case F_SETLK64:
644 673 case F_SETLKW64:
645 674 case F_SETLK64_NBMAND:
646 675 case F_OFD_GETLK64:
647 676 case F_OFD_SETLK64:
648 677 case F_OFD_SETLKW64:
649 678 case F_FLOCK64:
650 679 case F_FLOCKW64:
651 680 /*
652 681 * Large Files: Here we set cmd as *LK and send it to
653 682 * lower layers. *LK64 is only for the user land.
654 683 * Most of the comments described above for F_SETLK
655 684 * applies here too.
656 685 * Large File support is only needed for ILP32 apps!
657 686 */
658 687 if (datamodel != DATAMODEL_ILP32) {
659 688 error = EINVAL;
660 689 break;
661 690 }
662 691
663 692 if (cmd == F_GETLK64)
664 693 cmd = F_GETLK;
665 694 else if (cmd == F_SETLK64)
666 695 cmd = F_SETLK;
667 696 else if (cmd == F_SETLKW64)
668 697 cmd = F_SETLKW;
669 698 else if (cmd == F_SETLK64_NBMAND)
670 699 cmd = F_SETLK_NBMAND;
671 700 else if (cmd == F_OFD_GETLK64)
672 701 cmd = F_OFD_GETLK;
673 702 else if (cmd == F_OFD_SETLK64)
674 703 cmd = F_OFD_SETLK;
675 704 else if (cmd == F_OFD_SETLKW64)
676 705 cmd = F_OFD_SETLKW;
677 706 else if (cmd == F_FLOCK64)
678 707 cmd = F_FLOCK;
679 708 else if (cmd == F_FLOCKW64)
680 709 cmd = F_FLOCKW;
681 710
682 711 /*
683 712 * Note that the size of flock64 is different in the ILP32
684 713 * and LP64 models, due to the sucking l_pad field.
685 714 * We do not want to assume that the flock64 structure is
686 715 * laid out in the same in ILP32 and LP64 environments, so
687 716 * we will copy in the ILP32 version of flock64 explicitly
688 717 * and copy it to the native flock64 structure.
689 718 */
690 719
691 720 if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
692 721 error = EFAULT;
693 722 break;
694 723 }
695 724
696 725 bf.l_type = (short)bf64_32.l_type;
697 726 bf.l_whence = (short)bf64_32.l_whence;
698 727 bf.l_start = bf64_32.l_start;
699 728 bf.l_len = bf64_32.l_len;
700 729 bf.l_sysid = (int)bf64_32.l_sysid;
701 730 bf.l_pid = (pid_t)bf64_32.l_pid;
702 731
703 732 if ((error = flock_check(vp, &bf, offset, MAXOFFSET_T)) != 0)
704 733 break;
705 734
706 735 if (cmd == F_FLOCK || cmd == F_FLOCKW) {
707 736 /* FLOCK* locking is always over the entire file. */
708 737 if (bf.l_whence != 0 || bf.l_start != 0 ||
709 738 bf.l_len != 0) {
710 739 error = EINVAL;
711 740 break;
712 741 }
713 742 if (bf.l_type < F_RDLCK || bf.l_type > F_UNLCK) {
714 743 error = EINVAL;
715 744 break;
716 745 }
717 746 }
718 747
719 748 if (cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
720 749 /*
721 750 * TBD OFD-style locking is currently limited to
722 751 * covering the entire file.
723 752 */
724 753 if (bf.l_whence != 0 || bf.l_start != 0 ||
725 754 bf.l_len != 0) {
726 755 error = EINVAL;
727 756 break;
728 757 }
729 758 }
730 759
731 760 /*
732 761 * The *_frlock functions in the various file systems basically
733 762 * do some validation and then funnel everything through the
734 763 * fs_frlock function. For OFD-style locks fs_frlock will do
735 764 * nothing so that once control returns here we can call the
736 765 * ofdlock function with the correct fp. For OFD-style locks
737 766 * the unsupported remote file systems, such as NFS, detect and
738 767 * reject the OFD-style cmd argument.
739 768 */
740 769 if ((error = VOP_FRLOCK(vp, cmd, &bf, flag, offset,
741 770 NULL, fp->f_cred, NULL)) != 0)
742 771 break;
743 772
744 773 if (cmd == F_FLOCK || cmd == F_FLOCKW || cmd == F_OFD_GETLK ||
745 774 cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
746 775 /*
747 776 * This is an OFD-style lock so we need to handle it
748 777 * here. Because OFD-style locks are associated with
749 778 * the file_t we didn't have enough info down the
750 779 * VOP_FRLOCK path immediately above.
751 780 */
752 781 if ((error = ofdlock(fp, cmd, &bf, flag, offset)) != 0)
753 782 break;
754 783 }
755 784
756 785 if ((cmd == F_GETLK || cmd == F_OFD_GETLK) &&
757 786 bf.l_type == F_UNLCK) {
758 787 if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
759 788 sizeof (bf.l_type)))
760 789 error = EFAULT;
761 790 break;
762 791 }
763 792
764 793 if (cmd == F_GETLK || cmd == F_OFD_GETLK) {
765 794 int i;
766 795
767 796 /*
768 797 * We do not want to assume that the flock64 structure
769 798 * is laid out in the same in ILP32 and LP64
770 799 * environments, so we will copy out the ILP32 version
771 800 * of flock64 explicitly after copying the native
772 801 * flock64 structure to it.
773 802 */
774 803 for (i = 0; i < 4; i++)
775 804 bf64_32.l_pad[i] = 0;
776 805 bf64_32.l_type = (int16_t)bf.l_type;
777 806 bf64_32.l_whence = (int16_t)bf.l_whence;
778 807 bf64_32.l_start = bf.l_start;
779 808 bf64_32.l_len = bf.l_len;
780 809 bf64_32.l_sysid = (int32_t)bf.l_sysid;
781 810 bf64_32.l_pid = (pid32_t)bf.l_pid;
782 811 if (copyout(&bf64_32, (void *)arg, sizeof (bf64_32)))
783 812 error = EFAULT;
784 813 }
785 814 break;
786 815 #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
787 816
788 817 case F_SHARE:
789 818 case F_SHARE_NBMAND:
790 819 case F_UNSHARE:
791 820
792 821 /*
793 822 * Copy in input fields only.
794 823 */
795 824 if (copyin((void *)arg, &fsh, sizeof (fsh))) {
796 825 error = EFAULT;
797 826 break;
798 827 }
799 828
800 829 /*
801 830 * Local share reservations always have this simple form
802 831 */
803 832 shr.s_access = fsh.f_access;
804 833 shr.s_deny = fsh.f_deny;
805 834 shr.s_sysid = 0;
806 835 shr.s_pid = ttoproc(curthread)->p_pid;
807 836 shr_own.sl_pid = shr.s_pid;
808 837 shr_own.sl_id = fsh.f_id;
809 838 shr.s_own_len = sizeof (shr_own);
810 839 shr.s_owner = (caddr_t)&shr_own;
811 840 error = VOP_SHRLOCK(vp, cmd, &shr, flag, fp->f_cred, NULL);
812 841 break;
813 842
814 843 default:
815 844 error = EINVAL;
816 845 break;
817 846 }
818 847
819 848 if (in_crit)
820 849 nbl_end_crit(vp);
821 850
822 851 done:
823 852 releasef(fdes);
824 853 out:
825 854 if (error)
826 855 return (set_errno(error));
827 856 return (retval);
828 857 }
829 858
830 859 int
831 860 flock_check(vnode_t *vp, flock64_t *flp, offset_t offset, offset_t max)
832 861 {
833 862 struct vattr vattr;
834 863 int error;
835 864 u_offset_t start, end;
836 865
837 866 /*
838 867 * Determine the starting point of the request
839 868 */
840 869 switch (flp->l_whence) {
841 870 case 0: /* SEEK_SET */
842 871 start = (u_offset_t)flp->l_start;
843 872 if (start > max)
844 873 return (EINVAL);
845 874 break;
846 875 case 1: /* SEEK_CUR */
847 876 if (flp->l_start > (max - offset))
848 877 return (EOVERFLOW);
849 878 start = (u_offset_t)(flp->l_start + offset);
850 879 if (start > max)
851 880 return (EINVAL);
852 881 break;
853 882 case 2: /* SEEK_END */
854 883 vattr.va_mask = AT_SIZE;
855 884 if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
856 885 return (error);
857 886 if (flp->l_start > (max - (offset_t)vattr.va_size))
858 887 return (EOVERFLOW);
859 888 start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
860 889 if (start > max)
861 890 return (EINVAL);
862 891 break;
863 892 default:
864 893 return (EINVAL);
865 894 }
866 895
867 896 /*
868 897 * Determine the range covered by the request.
869 898 */
870 899 if (flp->l_len == 0)
871 900 end = MAXEND;
872 901 else if ((offset_t)flp->l_len > 0) {
873 902 if (flp->l_len > (max - start + 1))
874 903 return (EOVERFLOW);
875 904 end = (u_offset_t)(start + (flp->l_len - 1));
876 905 ASSERT(end <= max);
877 906 } else {
878 907 /*
879 908 * Negative length; why do we even allow this ?
880 909 * Because this allows easy specification of
881 910 * the last n bytes of the file.
882 911 */
883 912 end = start;
884 913 start += (u_offset_t)flp->l_len;
885 914 (start)++;
886 915 if (start > max)
887 916 return (EINVAL);
888 917 ASSERT(end <= max);
889 918 }
890 919 ASSERT(start <= max);
891 920 if (flp->l_type == F_UNLCK && flp->l_len > 0 &&
892 921 end == (offset_t)max) {
893 922 flp->l_len = 0;
894 923 }
895 924 if (start > end)
896 925 return (EINVAL);
897 926 return (0);
898 927 }
899 928
900 929 static int
901 930 flock_get_start(vnode_t *vp, flock64_t *flp, offset_t offset, u_offset_t *start)
902 931 {
903 932 struct vattr vattr;
904 933 int error;
905 934
906 935 /*
907 936 * Determine the starting point of the request. Assume that it is
908 937 * a valid starting point.
909 938 */
910 939 switch (flp->l_whence) {
911 940 case 0: /* SEEK_SET */
912 941 *start = (u_offset_t)flp->l_start;
913 942 break;
914 943 case 1: /* SEEK_CUR */
915 944 *start = (u_offset_t)(flp->l_start + offset);
916 945 break;
917 946 case 2: /* SEEK_END */
918 947 vattr.va_mask = AT_SIZE;
919 948 if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
920 949 return (error);
921 950 *start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
922 951 break;
923 952 default:
924 953 return (EINVAL);
925 954 }
926 955
927 956 return (0);
928 957 }
929 958
930 959 /*
931 960 * Take rctl action when the requested file descriptor is too big.
932 961 */
933 962 static void
934 963 fd_too_big(proc_t *p)
935 964 {
936 965 mutex_enter(&p->p_lock);
937 966 (void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
938 967 p->p_rctls, p, RCA_SAFE);
939 968 mutex_exit(&p->p_lock);
940 969 }
↓ open down ↓ |
703 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX