]> git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_vnops.c
1d1e6ec0e1ddb5e815a4031a0b73db2a12ce5f67
[apple/xnu.git] / bsd / ufs / ufs / ufs_vnops.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1982, 1986, 1989, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
61 */
62
63 #include <rev_endian_fs.h>
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/namei.h>
67 #include <sys/resourcevar.h>
68 #include <sys/kernel.h>
69 #include <sys/file.h>
70 #include <sys/stat.h>
71 #include <sys/buf.h>
72 #include <sys/proc.h>
73 #include <sys/conf.h>
74 #include <sys/mount.h>
75 #include <sys/vnode.h>
76 #include <sys/malloc.h>
77 #include <sys/dirent.h>
78 #include <sys/fcntl.h>
79 #include <sys/ubc.h>
80 #include <sys/quota.h>
81
82 #include <kern/thread.h>
83 #include <sys/vm.h>
84
85 #include <miscfs/specfs/specdev.h>
86
87 #include <ufs/ufs/lockf.h>
88 #include <ufs/ufs/quota.h>
89 #include <ufs/ufs/inode.h>
90 #include <ufs/ufs/dir.h>
91 #include <ufs/ufs/ufsmount.h>
92 #include <ufs/ufs/ufs_extern.h>
93
94 #if REV_ENDIAN_FS
95 #include <ufs/ufs/ufs_byte_order.h>
96 #include <architecture/byte_order.h>
97 #endif /* REV_ENDIAN_FS */
98
99 static int ufs_chmod __P((struct vnode *, int, struct ucred *, struct proc *));
100 static int ufs_chown
101 __P((struct vnode *, uid_t, gid_t, struct ucred *, struct proc *));
102
103 union _qcvt {
104 int64_t qcvt;
105 int32_t val[2];
106 };
107 #define SETHIGH(q, h) { \
108 union _qcvt tmp; \
109 tmp.qcvt = (q); \
110 tmp.val[_QUAD_HIGHWORD] = (h); \
111 (q) = tmp.qcvt; \
112 }
113 #define SETLOW(q, l) { \
114 union _qcvt tmp; \
115 tmp.qcvt = (q); \
116 tmp.val[_QUAD_LOWWORD] = (l); \
117 (q) = tmp.qcvt; \
118 }
119
120 /*
121 * Create a regular file
122 */
123 int
124 ufs_create(ap)
125 struct vop_create_args /* {
126 struct vnode *a_dvp;
127 struct vnode **a_vpp;
128 struct componentname *a_cnp;
129 struct vattr *a_vap;
130 } */ *ap;
131 {
132 int error;
133
134 if (error =
135 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
136 ap->a_dvp, ap->a_vpp, ap->a_cnp))
137 return (error);
138 return (0);
139 }
140
141 /*
142 * Mknod vnode call
143 */
144 /* ARGSUSED */
145 int
146 ufs_mknod(ap)
147 struct vop_mknod_args /* {
148 struct vnode *a_dvp;
149 struct vnode **a_vpp;
150 struct componentname *a_cnp;
151 struct vattr *a_vap;
152 } */ *ap;
153 {
154 struct vattr *vap = ap->a_vap;
155 struct vnode **vpp = ap->a_vpp;
156 struct inode *ip;
157 int error;
158
159 if (error =
160 ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
161 ap->a_dvp, vpp, ap->a_cnp))
162 return (error);
163 ip = VTOI(*vpp);
164 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
165 if (vap->va_rdev != VNOVAL) {
166 /*
167 * Want to be able to use this to make badblock
168 * inodes, so don't truncate the dev number.
169 */
170 ip->i_rdev = vap->va_rdev;
171 }
172 /*
173 * Remove inode so that it will be reloaded by VFS_VGET and
174 * checked to see if it is an alias of an existing entry in
175 * the inode cache.
176 */
177 vput(*vpp);
178 (*vpp)->v_type = VNON;
179 vgone(*vpp);
180 *vpp = 0;
181 return (0);
182 }
183
184 /*
185 * Open called.
186 *
187 * Nothing to do.
188 */
189 /* ARGSUSED */
190 int
191 ufs_open(ap)
192 struct vop_open_args /* {
193 struct vnode *a_vp;
194 int a_mode;
195 struct ucred *a_cred;
196 struct proc *a_p;
197 } */ *ap;
198 {
199
200 /*
201 * Files marked append-only must be opened for appending.
202 */
203 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
204 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
205 return (EPERM);
206 return (0);
207 }
208
209 /*
210 * Close called.
211 *
212 * Update the times on the inode.
213 */
214 /* ARGSUSED */
215 int
216 ufs_close(ap)
217 struct vop_close_args /* {
218 struct vnode *a_vp;
219 int a_fflag;
220 struct ucred *a_cred;
221 struct proc *a_p;
222 } */ *ap;
223 {
224 register struct vnode *vp = ap->a_vp;
225 register struct inode *ip = VTOI(vp);
226
227 simple_lock(&vp->v_interlock);
228 if ((!UBCISVALID(vp) && vp->v_usecount > 1)
229 || (UBCISVALID(vp) && ubc_isinuse(vp, 1)))
230 ITIMES(ip, &time, &time);
231 simple_unlock(&vp->v_interlock);
232
233 if (!VOP_ISLOCKED(vp)) {
234 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, ap->a_p);
235
236 cluster_push(vp);
237
238 VOP_UNLOCK(vp, 0, ap->a_p);
239 }
240 return (0);
241 }
242
243 int
244 ufs_access(ap)
245 struct vop_access_args /* {
246 struct vnode *a_vp;
247 int a_mode;
248 struct ucred *a_cred;
249 struct proc *a_p;
250 } */ *ap;
251 {
252 struct vnode *vp = ap->a_vp;
253 struct inode *ip = VTOI(vp);
254 struct ucred *cred = ap->a_cred;
255 mode_t mask, mode = ap->a_mode;
256 register gid_t *gp;
257 int i, error;
258
259 /*
260 * Disallow write attempts on read-only file systems;
261 * unless the file is a socket, fifo, or a block or
262 * character device resident on the file system.
263 */
264 if (mode & VWRITE) {
265 switch (vp->v_type) {
266 case VDIR:
267 case VLNK:
268 case VREG:
269 if (vp->v_mount->mnt_flag & MNT_RDONLY)
270 return (EROFS);
271 #if QUOTA
272 if (error = getinoquota(ip))
273 return (error);
274 #endif
275 break;
276 }
277 }
278
279 /* If immutable bit set, nobody gets to write it. */
280 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
281 return (EPERM);
282
283 /* Otherwise, user id 0 always gets access. */
284 if (cred->cr_uid == 0)
285 return (0);
286
287 mask = 0;
288
289 /* Otherwise, check the owner. */
290 if (cred->cr_uid == ip->i_uid) {
291 if (mode & VEXEC)
292 mask |= S_IXUSR;
293 if (mode & VREAD)
294 mask |= S_IRUSR;
295 if (mode & VWRITE)
296 mask |= S_IWUSR;
297 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
298 }
299
300 /* Otherwise, check the groups. */
301 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
302 if (ip->i_gid == *gp) {
303 if (mode & VEXEC)
304 mask |= S_IXGRP;
305 if (mode & VREAD)
306 mask |= S_IRGRP;
307 if (mode & VWRITE)
308 mask |= S_IWGRP;
309 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
310 }
311
312 /* Otherwise, check everyone else. */
313 if (mode & VEXEC)
314 mask |= S_IXOTH;
315 if (mode & VREAD)
316 mask |= S_IROTH;
317 if (mode & VWRITE)
318 mask |= S_IWOTH;
319 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
320 }
321
322 /* ARGSUSED */
323 int
324 ufs_getattr(ap)
325 struct vop_getattr_args /* {
326 struct vnode *a_vp;
327 struct vattr *a_vap;
328 struct ucred *a_cred;
329 struct proc *a_p;
330 } */ *ap;
331 {
332 register struct vnode *vp = ap->a_vp;
333 register struct inode *ip = VTOI(vp);
334 register struct vattr *vap = ap->a_vap;
335 int devBlockSize=0;
336
337 ITIMES(ip, &time, &time);
338 /*
339 * Copy from inode table
340 */
341 vap->va_fsid = ip->i_dev;
342 vap->va_fileid = ip->i_number;
343 vap->va_mode = ip->i_mode & ~IFMT;
344 vap->va_nlink = ip->i_nlink;
345 vap->va_uid = ip->i_uid;
346 vap->va_gid = ip->i_gid;
347 vap->va_rdev = (dev_t)ip->i_rdev;
348 vap->va_size = ip->i_din.di_size;
349 vap->va_atime.tv_sec = ip->i_atime;
350 vap->va_atime.tv_nsec = ip->i_atimensec;
351 vap->va_mtime.tv_sec = ip->i_mtime;
352 vap->va_mtime.tv_nsec = ip->i_mtimensec;
353 vap->va_ctime.tv_sec = ip->i_ctime;
354 vap->va_ctime.tv_nsec = ip->i_ctimensec;
355 vap->va_flags = ip->i_flags;
356 vap->va_gen = ip->i_gen;
357 /* this doesn't belong here */
358 if (vp->v_type == VBLK)
359 vap->va_blocksize = BLKDEV_IOSIZE;
360 else if (vp->v_type == VCHR)
361 vap->va_blocksize = MAXPHYSIO;
362 else
363 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
364 VOP_DEVBLOCKSIZE(ip->i_devvp, &devBlockSize);
365 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks, devBlockSize);
366 vap->va_type = vp->v_type;
367 vap->va_filerev = ip->i_modrev;
368 return (0);
369 }
370
371 /*
372 * Set attribute vnode op. called from several syscalls
373 */
374 int
375 ufs_setattr(ap)
376 struct vop_setattr_args /* {
377 struct vnode *a_vp;
378 struct vattr *a_vap;
379 struct ucred *a_cred;
380 struct proc *a_p;
381 } */ *ap;
382 {
383 struct vattr *vap = ap->a_vap;
384 struct vnode *vp = ap->a_vp;
385 struct inode *ip = VTOI(vp);
386 struct ucred *cred = ap->a_cred;
387 struct proc *p = ap->a_p;
388 struct timeval atimeval, mtimeval;
389 int error;
390
391 /*
392 * Check for unsettable attributes.
393 */
394 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
395 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
396 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
397 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
398 return (EINVAL);
399 }
400 if (vap->va_flags != VNOVAL) {
401 if (vp->v_mount->mnt_flag & MNT_RDONLY)
402 return (EROFS);
403 if (cred->cr_uid != ip->i_uid &&
404 (error = suser(cred, &p->p_acflag)))
405 return (error);
406 if (cred->cr_uid == 0) {
407 if ((ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) &&
408 securelevel > 0)
409 return (EPERM);
410 ip->i_flags = vap->va_flags;
411 } else {
412 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND) ||
413 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
414 return (EPERM);
415 ip->i_flags &= SF_SETTABLE;
416 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
417 }
418 ip->i_flag |= IN_CHANGE;
419 if (vap->va_flags & (IMMUTABLE | APPEND))
420 return (0);
421 }
422 if (ip->i_flags & (IMMUTABLE | APPEND))
423 return (EPERM);
424 /*
425 * Go through the fields and update iff not VNOVAL.
426 */
427 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
428 if (vp->v_mount->mnt_flag & MNT_RDONLY)
429 return (EROFS);
430 if (error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, p))
431 return (error);
432 }
433 if (vap->va_size != VNOVAL) {
434 /*
435 * Disallow write attempts on read-only file systems;
436 * unless the file is a socket, fifo, or a block or
437 * character device resident on the file system.
438 */
439 switch (vp->v_type) {
440 case VDIR:
441 return (EISDIR);
442 case VLNK:
443 case VREG:
444 if (vp->v_mount->mnt_flag & MNT_RDONLY)
445 return (EROFS);
446 break;
447 }
448 if (error = VOP_TRUNCATE(vp, vap->va_size, 0, cred, p))
449 return (error);
450 }
451 ip = VTOI(vp);
452 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
453 if (vp->v_mount->mnt_flag & MNT_RDONLY)
454 return (EROFS);
455 if (cred->cr_uid != ip->i_uid &&
456 (error = suser(cred, &p->p_acflag)) &&
457 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
458 (error = VOP_ACCESS(vp, VWRITE, cred, p))))
459 return (error);
460 if (vap->va_atime.tv_sec != VNOVAL)
461 ip->i_flag |= IN_ACCESS;
462 if (vap->va_mtime.tv_sec != VNOVAL)
463 ip->i_flag |= IN_CHANGE | IN_UPDATE;
464 atimeval.tv_sec = vap->va_atime.tv_sec;
465 atimeval.tv_usec = vap->va_atime.tv_nsec / 1000;
466 mtimeval.tv_sec = vap->va_mtime.tv_sec;
467 mtimeval.tv_usec = vap->va_mtime.tv_nsec / 1000;
468 if (error = VOP_UPDATE(vp, &atimeval, &mtimeval, 1))
469 return (error);
470 }
471 error = 0;
472 if (vap->va_mode != (mode_t)VNOVAL) {
473 if (vp->v_mount->mnt_flag & MNT_RDONLY)
474 return (EROFS);
475 error = ufs_chmod(vp, (int)vap->va_mode, cred, p);
476 }
477 return (error);
478 }
479
480 /*
481 * Change the mode on a file.
482 * Inode must be locked before calling.
483 */
484 static int
485 ufs_chmod(vp, mode, cred, p)
486 register struct vnode *vp;
487 register int mode;
488 register struct ucred *cred;
489 struct proc *p;
490 {
491 register struct inode *ip = VTOI(vp);
492 int error;
493
494 if (cred->cr_uid != ip->i_uid &&
495 (error = suser(cred, &p->p_acflag)))
496 return (error);
497 if (cred->cr_uid) {
498 if (vp->v_type != VDIR && (mode & S_ISTXT))
499 return (EFTYPE);
500 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
501 return (EPERM);
502 }
503 ip->i_mode &= ~ALLPERMS;
504 ip->i_mode |= (mode & ALLPERMS);
505 ip->i_flag |= IN_CHANGE;
506 return (0);
507 }
508
509 /*
510 * Perform chown operation on inode ip;
511 * inode must be locked prior to call.
512 */
513 static int
514 ufs_chown(vp, uid, gid, cred, p)
515 register struct vnode *vp;
516 uid_t uid;
517 gid_t gid;
518 struct ucred *cred;
519 struct proc *p;
520 {
521 register struct inode *ip = VTOI(vp);
522 uid_t ouid;
523 gid_t ogid;
524 int error = 0;
525 #if QUOTA
526 register int i;
527 int64_t change; /* in bytes */
528 int devBlockSize=0;
529 #endif /* QUOTA */
530
531 if (uid == (uid_t)VNOVAL)
532 uid = ip->i_uid;
533 if (gid == (gid_t)VNOVAL)
534 gid = ip->i_gid;
535 /*
536 * If we don't own the file, are trying to change the owner
537 * of the file, or are not a member of the target group,
538 * the caller must be superuser or the call fails.
539 */
540 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
541 (gid != ip->i_gid && !groupmember((gid_t)gid, cred))) &&
542 (error = suser(cred, &p->p_acflag)))
543 return (error);
544 ogid = ip->i_gid;
545 ouid = ip->i_uid;
546 #if QUOTA
547 if (error = getinoquota(ip))
548 return (error);
549 if (ouid == uid) {
550 dqrele(vp, ip->i_dquot[USRQUOTA]);
551 ip->i_dquot[USRQUOTA] = NODQUOT;
552 }
553 if (ogid == gid) {
554 dqrele(vp, ip->i_dquot[GRPQUOTA]);
555 ip->i_dquot[GRPQUOTA] = NODQUOT;
556 }
557 VOP_DEVBLOCKSIZE(ip->i_devvp, &devBlockSize);
558 change = dbtob((int64_t)ip->i_blocks, devBlockSize);
559 (void) chkdq(ip, -change, cred, CHOWN);
560 (void) chkiq(ip, -1, cred, CHOWN);
561 for (i = 0; i < MAXQUOTAS; i++) {
562 dqrele(vp, ip->i_dquot[i]);
563 ip->i_dquot[i] = NODQUOT;
564 }
565 #endif
566 ip->i_gid = gid;
567 ip->i_uid = uid;
568 #if QUOTA
569 if ((error = getinoquota(ip)) == 0) {
570 if (ouid == uid) {
571 dqrele(vp, ip->i_dquot[USRQUOTA]);
572 ip->i_dquot[USRQUOTA] = NODQUOT;
573 }
574 if (ogid == gid) {
575 dqrele(vp, ip->i_dquot[GRPQUOTA]);
576 ip->i_dquot[GRPQUOTA] = NODQUOT;
577 }
578 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
579 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
580 goto good;
581 else
582 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
583 }
584 for (i = 0; i < MAXQUOTAS; i++) {
585 dqrele(vp, ip->i_dquot[i]);
586 ip->i_dquot[i] = NODQUOT;
587 }
588 }
589 ip->i_gid = ogid;
590 ip->i_uid = ouid;
591 if (getinoquota(ip) == 0) {
592 if (ouid == uid) {
593 dqrele(vp, ip->i_dquot[USRQUOTA]);
594 ip->i_dquot[USRQUOTA] = NODQUOT;
595 }
596 if (ogid == gid) {
597 dqrele(vp, ip->i_dquot[GRPQUOTA]);
598 ip->i_dquot[GRPQUOTA] = NODQUOT;
599 }
600 (void) chkdq(ip, change, cred, FORCE|CHOWN);
601 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
602 (void) getinoquota(ip);
603 }
604 return (error);
605 good:
606 if (getinoquota(ip))
607 panic("chown: lost quota");
608 #endif /* QUOTA */
609 if (ouid != uid || ogid != gid)
610 ip->i_flag |= IN_CHANGE;
611 if (ouid != uid && cred->cr_uid != 0)
612 ip->i_mode &= ~ISUID;
613 if (ogid != gid && cred->cr_uid != 0)
614 ip->i_mode &= ~ISGID;
615 return (0);
616 }
617
618 /* ARGSUSED */
619 int
620 ufs_ioctl(ap)
621 struct vop_ioctl_args /* {
622 struct vnode *a_vp;
623 int a_command;
624 caddr_t a_data;
625 int a_fflag;
626 struct ucred *a_cred;
627 struct proc *a_p;
628 } */ *ap;
629 {
630
631 switch (ap->a_command) {
632
633 case 1:
634 { register struct inode *ip;
635 register struct vnode *vp;
636 register struct fs *fs;
637 register struct radvisory *ra;
638 int devBlockSize = 0;
639 int error;
640
641 vp = ap->a_vp;
642
643 VOP_LEASE(vp, ap->a_p, ap->a_cred, LEASE_READ);
644 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, ap->a_p);
645
646 ra = (struct radvisory *)(ap->a_data);
647 ip = VTOI(vp);
648 fs = ip->i_fs;
649
650 if ((u_int64_t)ra->ra_offset >= ip->i_size) {
651 VOP_UNLOCK(vp, 0, ap->a_p);
652 return (EFBIG);
653 }
654 VOP_DEVBLOCKSIZE(ip->i_devvp, &devBlockSize);
655
656 error = advisory_read(vp, ip->i_size, ra->ra_offset, ra->ra_count, devBlockSize);
657 VOP_UNLOCK(vp, 0, ap->a_p);
658 return (error);
659 }
660 default:
661 return (ENOTTY);
662 }
663 }
664
665 /* ARGSUSED */
666 int
667 ufs_select(ap)
668 struct vop_select_args /* {
669 struct vnode *a_vp;
670 int a_which;
671 int a_fflags;
672 struct ucred *a_cred;
673 void *a_wql;
674 struct proc *a_p;
675 } */ *ap;
676 {
677
678 /*
679 * We should really check to see if I/O is possible.
680 */
681 return (1);
682 }
683
684 /*
685 * Mmap a file
686 *
687 * NB Currently unsupported.
688 */
689 /* ARGSUSED */
690 int
691 ufs_mmap(ap)
692 struct vop_mmap_args /* {
693 struct vnode *a_vp;
694 int a_fflags;
695 struct ucred *a_cred;
696 struct proc *a_p;
697 } */ *ap;
698 {
699
700 return (EINVAL);
701 }
702
703 /*
704 * Seek on a file
705 *
706 * Nothing to do, so just return.
707 */
708 /* ARGSUSED */
709 int
710 ufs_seek(ap)
711 struct vop_seek_args /* {
712 struct vnode *a_vp;
713 off_t a_oldoff;
714 off_t a_newoff;
715 struct ucred *a_cred;
716 } */ *ap;
717 {
718
719 return (0);
720 }
721
722 int
723 ufs_remove(ap)
724 struct vop_remove_args /* {
725 struct vnode *a_dvp;
726 struct vnode *a_vp;
727 struct componentname *a_cnp;
728 } */ *ap;
729 {
730 struct inode *ip;
731 struct vnode *vp = ap->a_vp;
732 struct vnode *dvp = ap->a_dvp;
733 int error;
734
735 ip = VTOI(vp);
736 if ((ip->i_flags & (IMMUTABLE | APPEND)) ||
737 (VTOI(dvp)->i_flags & APPEND)) {
738 error = EPERM;
739 goto out;
740 }
741
742 if (ap->a_cnp->cn_flags & NODELETEBUSY) {
743 /* Caller requested Carbon delete semantics */
744 if ((!UBCISVALID(vp) && vp->v_usecount > 1)
745 || (UBCISVALID(vp) && ubc_isinuse(vp, 1))) {
746 error = EBUSY;
747 goto out;
748 }
749 }
750
751 if ((error = ufs_dirremove(dvp, ap->a_cnp)) == 0) {
752 ip->i_nlink--;
753 ip->i_flag |= IN_CHANGE;
754 }
755
756 if (dvp != vp)
757 VOP_UNLOCK(vp, 0, ap->a_cnp->cn_proc);
758
759 (void) ubc_uncache(vp);
760
761 vrele(vp);
762 vput(dvp);
763
764 return (error);
765
766 out:
767 if (dvp == vp)
768 vrele(vp);
769 else
770 vput(vp);
771 vput(dvp);
772 return (error);
773 }
774
775 /*
776 * link vnode call
777 */
778 int
779 ufs_link(ap)
780 struct vop_link_args /* {
781 struct vnode *a_vp;
782 struct vnode *a_tdvp;
783 struct componentname *a_cnp;
784 } */ *ap;
785 {
786 struct vnode *vp = ap->a_vp;
787 struct vnode *tdvp = ap->a_tdvp;
788 struct componentname *cnp = ap->a_cnp;
789 struct proc *p = cnp->cn_proc;
790 struct inode *ip;
791 struct timeval tv;
792 int error;
793
794 #if DIAGNOSTIC
795 if ((cnp->cn_flags & HASBUF) == 0)
796 panic("ufs_link: no name");
797 #endif
798 if (tdvp->v_mount != vp->v_mount) {
799 VOP_ABORTOP(tdvp, cnp);
800 error = EXDEV;
801 goto out2;
802 }
803 if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE, p))) {
804 VOP_ABORTOP(tdvp, cnp);
805 goto out2;
806 }
807 ip = VTOI(vp);
808 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
809 VOP_ABORTOP(tdvp, cnp);
810 error = EMLINK;
811 goto out1;
812 }
813 if (ip->i_flags & (IMMUTABLE | APPEND)) {
814 VOP_ABORTOP(tdvp, cnp);
815 error = EPERM;
816 goto out1;
817 }
818 ip->i_nlink++;
819 ip->i_flag |= IN_CHANGE;
820 tv = time;
821 error = VOP_UPDATE(vp, &tv, &tv, 1);
822 if (!error)
823 error = ufs_direnter(ip, tdvp, cnp);
824 if (error) {
825 ip->i_nlink--;
826 ip->i_flag |= IN_CHANGE;
827 }
828 FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
829 out1:
830 if (tdvp != vp)
831 VOP_UNLOCK(vp, 0, p);
832 out2:
833 vput(tdvp);
834 return (error);
835 }
836
837 /*
838 * whiteout vnode call
839 */
840 int
841 ufs_whiteout(ap)
842 struct vop_whiteout_args /* {
843 struct vnode *a_dvp;
844 struct componentname *a_cnp;
845 int a_flags;
846 } */ *ap;
847 {
848 struct vnode *dvp = ap->a_dvp;
849 struct componentname *cnp = ap->a_cnp;
850 struct direct newdir;
851 int error;
852
853 switch (ap->a_flags) {
854 case LOOKUP:
855 /* 4.4 format directories support whiteout operations */
856 if (dvp->v_mount->mnt_maxsymlinklen > 0)
857 return (0);
858 return (EOPNOTSUPP);
859
860 case CREATE:
861 /* create a new directory whiteout */
862 #if DIAGNOSTIC
863 if ((cnp->cn_flags & SAVENAME) == 0)
864 panic("ufs_whiteout: missing name");
865 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
866 panic("ufs_whiteout: old format filesystem");
867 #endif
868
869 newdir.d_ino = WINO;
870 newdir.d_namlen = cnp->cn_namelen;
871 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
872 newdir.d_type = DT_WHT;
873 error = ufs_direnter2(dvp, &newdir, cnp->cn_cred, cnp->cn_proc);
874 break;
875
876 case DELETE:
877 /* remove an existing directory whiteout */
878 #if DIAGNOSTIC
879 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
880 panic("ufs_whiteout: old format filesystem");
881 #endif
882
883 cnp->cn_flags &= ~DOWHITEOUT;
884 error = ufs_dirremove(dvp, cnp);
885 break;
886 }
887 if (cnp->cn_flags & HASBUF) {
888 FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
889 cnp->cn_flags &= ~HASBUF;
890 }
891 return (error);
892 }
893
894
895 /*
896 * Rename system call.
897 * rename("foo", "bar");
898 * is essentially
899 * unlink("bar");
900 * link("foo", "bar");
901 * unlink("foo");
902 * but ``atomically''. Can't do full commit without saving state in the
903 * inode on disk which isn't feasible at this time. Best we can do is
904 * always guarantee the target exists.
905 *
906 * Basic algorithm is:
907 *
908 * 1) Bump link count on source while we're linking it to the
909 * target. This also ensure the inode won't be deleted out
910 * from underneath us while we work (it may be truncated by
911 * a concurrent `trunc' or `open' for creation).
912 * 2) Link source to destination. If destination already exists,
913 * delete it first.
914 * 3) Unlink source reference to inode if still around. If a
915 * directory was moved and the parent of the destination
916 * is different from the source, patch the ".." entry in the
917 * directory.
918 */
919 int
920 ufs_rename(ap)
921 struct vop_rename_args /* {
922 struct vnode *a_fdvp;
923 struct vnode *a_fvp;
924 struct componentname *a_fcnp;
925 struct vnode *a_tdvp;
926 struct vnode *a_tvp;
927 struct componentname *a_tcnp;
928 } */ *ap;
929 {
930 struct vnode *tvp = ap->a_tvp;
931 register struct vnode *tdvp = ap->a_tdvp;
932 struct vnode *fvp = ap->a_fvp;
933 struct vnode *fdvp = ap->a_fdvp;
934 struct componentname *tcnp = ap->a_tcnp;
935 struct componentname *fcnp = ap->a_fcnp;
936 struct proc *p = fcnp->cn_proc;
937 struct inode *ip, *xp, *dp;
938 struct dirtemplate dirbuf;
939 struct timeval tv;
940 int doingdirectory = 0, oldparent = 0, newparent = 0;
941 int error = 0;
942 u_char namlen;
943
944 #if DIAGNOSTIC
945 if ((tcnp->cn_flags & HASBUF) == 0 ||
946 (fcnp->cn_flags & HASBUF) == 0)
947 panic("ufs_rename: no name");
948 #endif
949 /*
950 * Check for cross-device rename.
951 */
952 if ((fvp->v_mount != tdvp->v_mount) ||
953 (tvp && (fvp->v_mount != tvp->v_mount))) {
954 error = EXDEV;
955 abortit:
956 VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */
957 if (tdvp == tvp)
958 vrele(tdvp);
959 else
960 vput(tdvp);
961 if (tvp)
962 vput(tvp);
963 VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */
964 vrele(fdvp);
965 vrele(fvp);
966 return (error);
967 }
968
969 /*
970 * Check if just deleting a link name.
971 */
972 if (tvp && ((VTOI(tvp)->i_flags & (IMMUTABLE | APPEND)) ||
973 (VTOI(tdvp)->i_flags & APPEND))) {
974 error = EPERM;
975 goto abortit;
976 }
977 if (fvp == tvp) {
978 if (fvp->v_type == VDIR) {
979 error = EINVAL;
980 goto abortit;
981 }
982
983 /* Release destination completely. */
984 VOP_ABORTOP(tdvp, tcnp);
985 vput(tdvp);
986 vput(tvp);
987
988 /* Delete source. */
989 vrele(fdvp);
990 vrele(fvp);
991 fcnp->cn_flags &= ~MODMASK;
992 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
993 if ((fcnp->cn_flags & SAVESTART) == 0)
994 panic("ufs_rename: lost from startdir");
995 fcnp->cn_nameiop = DELETE;
996 (void) relookup(fdvp, &fvp, fcnp);
997 return (VOP_REMOVE(fdvp, fvp, fcnp));
998 }
999 if (error = vn_lock(fvp, LK_EXCLUSIVE, p))
1000 goto abortit;
1001 dp = VTOI(fdvp);
1002 ip = VTOI(fvp);
1003 if ((ip->i_flags & (IMMUTABLE | APPEND)) || (dp->i_flags & APPEND)) {
1004 VOP_UNLOCK(fvp, 0, p);
1005 error = EPERM;
1006 goto abortit;
1007 }
1008 if ((ip->i_mode & IFMT) == IFDIR) {
1009 /*
1010 * Avoid ".", "..", and aliases of "." for obvious reasons.
1011 */
1012 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1013 dp == ip || (fcnp->cn_flags&ISDOTDOT) ||
1014 (ip->i_flag & IN_RENAME)) {
1015 VOP_UNLOCK(fvp, 0, p);
1016 error = EINVAL;
1017 goto abortit;
1018 }
1019 ip->i_flag |= IN_RENAME;
1020 oldparent = dp->i_number;
1021 doingdirectory++;
1022 }
1023 vrele(fdvp);
1024
1025 /*
1026 * When the target exists, both the directory
1027 * and target vnodes are returned locked.
1028 */
1029 dp = VTOI(tdvp);
1030 xp = NULL;
1031 if (tvp)
1032 xp = VTOI(tvp);
1033
1034 /*
1035 * 1) Bump link count while we're moving stuff
1036 * around. If we crash somewhere before
1037 * completing our work, the link count
1038 * may be wrong, but correctable.
1039 */
1040 ip->i_nlink++;
1041 ip->i_flag |= IN_CHANGE;
1042 tv = time;
1043 if (error = VOP_UPDATE(fvp, &tv, &tv, 1)) {
1044 VOP_UNLOCK(fvp, 0, p);
1045 goto bad;
1046 }
1047
1048 /*
1049 * If ".." must be changed (ie the directory gets a new
1050 * parent) then the source directory must not be in the
1051 * directory heirarchy above the target, as this would
1052 * orphan everything below the source directory. Also
1053 * the user must have write permission in the source so
1054 * as to be able to change "..". We must repeat the call
1055 * to namei, as the parent directory is unlocked by the
1056 * call to checkpath().
1057 */
1058 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
1059 VOP_UNLOCK(fvp, 0, p);
1060 if (oldparent != dp->i_number)
1061 newparent = dp->i_number;
1062 if (doingdirectory && newparent) {
1063 if (error) /* write access check above */
1064 goto bad;
1065 if (xp != NULL)
1066 vput(tvp);
1067 if (error = ufs_checkpath(ip, dp, tcnp->cn_cred))
1068 goto out;
1069 if ((tcnp->cn_flags & SAVESTART) == 0)
1070 panic("ufs_rename: lost to startdir");
1071 if (error = relookup(tdvp, &tvp, tcnp))
1072 goto out;
1073 dp = VTOI(tdvp);
1074 xp = NULL;
1075 if (tvp)
1076 xp = VTOI(tvp);
1077 }
1078 /*
1079 * 2) If target doesn't exist, link the target
1080 * to the source and unlink the source.
1081 * Otherwise, rewrite the target directory
1082 * entry to reference the source inode and
1083 * expunge the original entry's existence.
1084 */
1085 if (xp == NULL) {
1086 if (dp->i_dev != ip->i_dev)
1087 panic("rename: EXDEV");
1088 /*
1089 * Account for ".." in new directory.
1090 * When source and destination have the same
1091 * parent we don't fool with the link count.
1092 */
1093 if (doingdirectory && newparent) {
1094 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1095 error = EMLINK;
1096 goto bad;
1097 }
1098 dp->i_nlink++;
1099 dp->i_flag |= IN_CHANGE;
1100 if (error = VOP_UPDATE(tdvp, &tv, &tv, 1))
1101 goto bad;
1102 }
1103 if (error = ufs_direnter(ip, tdvp, tcnp)) {
1104 if (doingdirectory && newparent) {
1105 dp->i_nlink--;
1106 dp->i_flag |= IN_CHANGE;
1107 (void)VOP_UPDATE(tdvp, &tv, &tv, 1);
1108 }
1109 goto bad;
1110 }
1111 vput(tdvp);
1112 } else {
1113 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1114 panic("rename: EXDEV");
1115 /*
1116 * Short circuit rename(foo, foo).
1117 */
1118 if (xp->i_number == ip->i_number)
1119 panic("rename: same file");
1120 /*
1121 * If the parent directory is "sticky", then the user must
1122 * own the parent directory, or the destination of the rename,
1123 * otherwise the destination may not be changed (except by
1124 * root). This implements append-only directories.
1125 */
1126 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1127 tcnp->cn_cred->cr_uid != dp->i_uid &&
1128 xp->i_uid != tcnp->cn_cred->cr_uid) {
1129 error = EPERM;
1130 goto bad;
1131 }
1132 /*
1133 * Target must be empty if a directory and have no links
1134 * to it. Also, ensure source and target are compatible
1135 * (both directories, or both not directories).
1136 */
1137 if ((xp->i_mode&IFMT) == IFDIR) {
1138 if (!ufs_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
1139 xp->i_nlink > 2) {
1140 error = ENOTEMPTY;
1141 goto bad;
1142 }
1143 if (!doingdirectory) {
1144 error = ENOTDIR;
1145 goto bad;
1146 }
1147 cache_purge(tdvp);
1148 } else if (doingdirectory) {
1149 error = EISDIR;
1150 goto bad;
1151 }
1152 if (error = ufs_dirrewrite(dp, ip, tcnp))
1153 goto bad;
1154 /*
1155 * If the target directory is in the same
1156 * directory as the source directory,
1157 * decrement the link count on the parent
1158 * of the target directory.
1159 */
1160 if (doingdirectory && !newparent) {
1161 dp->i_nlink--;
1162 dp->i_flag |= IN_CHANGE;
1163 }
1164 vput(tdvp);
1165 /*
1166 * Adjust the link count of the target to
1167 * reflect the dirrewrite above. If this is
1168 * a directory it is empty and there are
1169 * no links to it, so we can squash the inode and
1170 * any space associated with it. We disallowed
1171 * renaming over top of a directory with links to
1172 * it above, as the remaining link would point to
1173 * a directory without "." or ".." entries.
1174 */
1175 xp->i_nlink--;
1176 if (doingdirectory) {
1177 if (--xp->i_nlink != 0)
1178 panic("rename: linked directory");
1179 error = VOP_TRUNCATE(tvp, (off_t)0, IO_SYNC,
1180 tcnp->cn_cred, tcnp->cn_proc);
1181 }
1182 xp->i_flag |= IN_CHANGE;
1183 vput(tvp);
1184 xp = NULL;
1185 }
1186
1187 /*
1188 * 3) Unlink the source.
1189 */
1190 fcnp->cn_flags &= ~MODMASK;
1191 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1192 if ((fcnp->cn_flags & SAVESTART) == 0)
1193 panic("ufs_rename: lost from startdir");
1194 (void) relookup(fdvp, &fvp, fcnp);
1195 if (fvp != NULL) {
1196 xp = VTOI(fvp);
1197 dp = VTOI(fdvp);
1198 } else {
1199 /*
1200 * From name has disappeared.
1201 */
1202 if (doingdirectory)
1203 panic("rename: lost dir entry");
1204 vrele(ap->a_fvp);
1205 return (0);
1206 }
1207 /*
1208 * Ensure that the directory entry still exists and has not
1209 * changed while the new name has been entered. If the source is
1210 * a file then the entry may have been unlinked or renamed. In
1211 * either case there is no further work to be done. If the source
1212 * is a directory then it cannot have been rmdir'ed; its link
1213 * count of three would cause a rmdir to fail with ENOTEMPTY.
1214 * The IRENAME flag ensures that it cannot be moved by another
1215 * rename.
1216 */
1217 if (xp != ip) {
1218 if (doingdirectory)
1219 panic("rename: lost dir entry");
1220 } else {
1221 /*
1222 * If the source is a directory with a
1223 * new parent, the link count of the old
1224 * parent directory must be decremented
1225 * and ".." set to point to the new parent.
1226 */
1227 if (doingdirectory && newparent) {
1228 dp->i_nlink--;
1229 dp->i_flag |= IN_CHANGE;
1230 error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1231 sizeof (struct dirtemplate), (off_t)0,
1232 UIO_SYSSPACE, IO_NODELOCKED,
1233 tcnp->cn_cred, (int *)0, (struct proc *)0);
1234 if (error == 0) {
1235 # if (BYTE_ORDER == LITTLE_ENDIAN)
1236 if (fvp->v_mount->mnt_maxsymlinklen <= 0)
1237 namlen = dirbuf.dotdot_type;
1238 else
1239 namlen = dirbuf.dotdot_namlen;
1240 # else
1241 namlen = dirbuf.dotdot_namlen;
1242 # endif
1243 if (namlen != 2 ||
1244 dirbuf.dotdot_name[0] != '.' ||
1245 dirbuf.dotdot_name[1] != '.') {
1246 ufs_dirbad(xp, (doff_t)12,
1247 "rename: mangled dir");
1248 } else {
1249 dirbuf.dotdot_ino = newparent;
1250 (void) vn_rdwr(UIO_WRITE, fvp,
1251 (caddr_t)&dirbuf,
1252 sizeof (struct dirtemplate),
1253 (off_t)0, UIO_SYSSPACE,
1254 IO_NODELOCKED|IO_SYNC,
1255 tcnp->cn_cred, (int *)0,
1256 (struct proc *)0);
1257 cache_purge(fdvp);
1258 }
1259 }
1260 }
1261 error = ufs_dirremove(fdvp, fcnp);
1262 if (!error) {
1263 xp->i_nlink--;
1264 xp->i_flag |= IN_CHANGE;
1265 }
1266 xp->i_flag &= ~IN_RENAME;
1267 }
1268 if (dp)
1269 vput(fdvp);
1270 if (xp)
1271 vput(fvp);
1272 vrele(ap->a_fvp);
1273 return (error);
1274
1275 bad:
1276 if (xp)
1277 vput(ITOV(xp));
1278 vput(ITOV(dp));
1279 out:
1280 if (doingdirectory)
1281 ip->i_flag &= ~IN_RENAME;
1282 if (vn_lock(fvp, LK_EXCLUSIVE, p) == 0) {
1283 ip->i_nlink--;
1284 ip->i_flag |= IN_CHANGE;
1285 vput(fvp);
1286 } else
1287 vrele(fvp);
1288 return (error);
1289 }
1290
1291 /*
1292 * A virgin directory (no blushing please).
1293 */
1294 static struct dirtemplate mastertemplate = {
1295 0, 12, DT_DIR, 1, ".",
1296 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
1297 };
1298 static struct odirtemplate omastertemplate = {
1299 0, 12, 1, ".",
1300 0, DIRBLKSIZ - 12, 2, ".."
1301 };
1302
1303 /*
1304 * Mkdir system call
1305 */
1306 int
1307 ufs_mkdir(ap)
1308 struct vop_mkdir_args /* {
1309 struct vnode *a_dvp;
1310 struct vnode **a_vpp;
1311 struct componentname *a_cnp;
1312 struct vattr *a_vap;
1313 } */ *ap;
1314 {
1315 register struct vnode *dvp = ap->a_dvp;
1316 register struct vattr *vap = ap->a_vap;
1317 register struct componentname *cnp = ap->a_cnp;
1318 register struct inode *ip, *dp;
1319 struct vnode *tvp;
1320 struct dirtemplate dirtemplate, *dtp;
1321 struct timeval tv;
1322 int error, dmode;
1323
1324 #if DIAGNOSTIC
1325 if ((cnp->cn_flags & HASBUF) == 0)
1326 panic("ufs_mkdir: no name");
1327 #endif
1328 dp = VTOI(dvp);
1329 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1330 error = EMLINK;
1331 goto out;
1332 }
1333 dmode = vap->va_mode & 0777;
1334 dmode |= IFDIR;
1335 /*
1336 * Must simulate part of ufs_makeinode here to acquire the inode,
1337 * but not have it entered in the parent directory. The entry is
1338 * made later after writing "." and ".." entries.
1339 */
1340 if (error = VOP_VALLOC(dvp, dmode, cnp->cn_cred, &tvp))
1341 goto out;
1342 ip = VTOI(tvp);
1343 ip->i_uid = cnp->cn_cred->cr_uid;
1344 ip->i_gid = dp->i_gid;
1345 #if QUOTA
1346 if ((error = getinoquota(ip)) ||
1347 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1348 _FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
1349 VOP_VFREE(tvp, ip->i_number, dmode);
1350 vput(tvp);
1351 vput(dvp);
1352 return (error);
1353 }
1354 #endif
1355 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1356 ip->i_mode = dmode;
1357 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1358 ip->i_nlink = 2;
1359 if (cnp->cn_flags & ISWHITEOUT)
1360 ip->i_flags |= UF_OPAQUE;
1361 tv = time;
1362 error = VOP_UPDATE(tvp, &tv, &tv, 1);
1363
1364 /*
1365 * Bump link count in parent directory
1366 * to reflect work done below. Should
1367 * be done before reference is created
1368 * so reparation is possible if we crash.
1369 */
1370 dp->i_nlink++;
1371 dp->i_flag |= IN_CHANGE;
1372 if (error = VOP_UPDATE(dvp, &tv, &tv, 1))
1373 goto bad;
1374
1375 /* Initialize directory with "." and ".." from static template. */
1376 if (dvp->v_mount->mnt_maxsymlinklen > 0)
1377 dtp = &mastertemplate;
1378 else
1379 dtp = (struct dirtemplate *)&omastertemplate;
1380 dirtemplate = *dtp;
1381 dirtemplate.dot_ino = ip->i_number;
1382 dirtemplate.dotdot_ino = dp->i_number;
1383 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1384 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
1385 IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (int *)0, (struct proc *)0);
1386 if (error) {
1387 dp->i_nlink--;
1388 dp->i_flag |= IN_CHANGE;
1389 goto bad;
1390 }
1391 if (DIRBLKSIZ > VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1392 panic("ufs_mkdir: blksize"); /* XXX should grow with balloc() */
1393 else {
1394 ip->i_size = DIRBLKSIZ;
1395 ip->i_flag |= IN_CHANGE;
1396 }
1397
1398 /* Directory set up, now install it's entry in the parent directory. */
1399 if (error = ufs_direnter(ip, dvp, cnp)) {
1400 dp->i_nlink--;
1401 dp->i_flag |= IN_CHANGE;
1402 }
1403 bad:
1404 /*
1405 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1406 * for us because we set the link count to 0.
1407 */
1408 if (error) {
1409 ip->i_nlink = 0;
1410 ip->i_flag |= IN_CHANGE;
1411 vput(tvp);
1412 } else
1413 *ap->a_vpp = tvp;
1414 out:
1415 FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
1416 vput(dvp);
1417 return (error);
1418 }
1419
1420 /*
1421 * Rmdir system call.
1422 */
1423 int
1424 ufs_rmdir(ap)
1425 struct vop_rmdir_args /* {
1426 struct vnode *a_dvp;
1427 struct vnode *a_vp;
1428 struct componentname *a_cnp;
1429 } */ *ap;
1430 {
1431 struct vnode *vp = ap->a_vp;
1432 struct vnode *dvp = ap->a_dvp;
1433 struct componentname *cnp = ap->a_cnp;
1434 struct inode *ip, *dp;
1435 int error;
1436
1437 ip = VTOI(vp);
1438 dp = VTOI(dvp);
1439 /*
1440 * No rmdir "." please.
1441 */
1442 if (dp == ip) {
1443 vrele(dvp);
1444 vput(vp);
1445 return (EINVAL);
1446 }
1447 /*
1448 * Verify the directory is empty (and valid).
1449 * (Rmdir ".." won't be valid since
1450 * ".." will contain a reference to
1451 * the current directory and thus be
1452 * non-empty.)
1453 */
1454 error = 0;
1455 if (ip->i_nlink != 2 ||
1456 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1457 error = ENOTEMPTY;
1458 goto out;
1459 }
1460 if ((dp->i_flags & APPEND) || (ip->i_flags & (IMMUTABLE | APPEND))) {
1461 error = EPERM;
1462 goto out;
1463 }
1464 /*
1465 * Delete reference to directory before purging
1466 * inode. If we crash in between, the directory
1467 * will be reattached to lost+found,
1468 */
1469 if (error = ufs_dirremove(dvp, cnp))
1470 goto out;
1471 dp->i_nlink--;
1472 dp->i_flag |= IN_CHANGE;
1473 cache_purge(dvp);
1474 vput(dvp);
1475 dvp = NULL;
1476 /*
1477 * Truncate inode. The only stuff left
1478 * in the directory is "." and "..". The
1479 * "." reference is inconsequential since
1480 * we're quashing it. The ".." reference
1481 * has already been adjusted above. We've
1482 * removed the "." reference and the reference
1483 * in the parent directory, but there may be
1484 * other hard links so decrement by 2 and
1485 * worry about them later.
1486 */
1487 ip->i_nlink -= 2;
1488 error = VOP_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1489 cnp->cn_proc);
1490 cache_purge(ITOV(ip));
1491 out:
1492 if (dvp)
1493 vput(dvp);
1494 vput(vp);
1495 return (error);
1496 }
1497
1498 /*
1499 * symlink -- make a symbolic link
1500 */
1501 int
1502 ufs_symlink(ap)
1503 struct vop_symlink_args /* {
1504 struct vnode *a_dvp;
1505 struct vnode **a_vpp;
1506 struct componentname *a_cnp;
1507 struct vattr *a_vap;
1508 char *a_target;
1509 } */ *ap;
1510 {
1511 register struct vnode *vp, **vpp = ap->a_vpp;
1512 register struct inode *ip;
1513 int len, error;
1514
1515 if (error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1516 vpp, ap->a_cnp))
1517 return (error);
1518 vp = *vpp;
1519 len = strlen(ap->a_target);
1520 if (len < vp->v_mount->mnt_maxsymlinklen) {
1521 ip = VTOI(vp);
1522 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1523 ip->i_size = len;
1524 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1525 } else
1526 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1527 UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, (int *)0,
1528 (struct proc *)0);
1529 vput(vp);
1530 return (error);
1531 }
1532
1533 /*
1534 * Vnode op for reading directories.
1535 *
1536 * The routine below assumes that the on-disk format of a directory
1537 * is the same as that defined by <sys/dirent.h>. If the on-disk
1538 * format changes, then it will be necessary to do a conversion
1539 * from the on-disk format that read returns to the format defined
1540 * by <sys/dirent.h>.
1541 */
1542 int
1543 ufs_readdir(ap)
1544 struct vop_readdir_args /* {
1545 struct vnode *a_vp;
1546 struct uio *a_uio;
1547 struct ucred *a_cred;
1548 int *a_eofflag;
1549 int *ncookies;
1550 u_long **a_cookies;
1551 } */ *ap;
1552 {
1553 register struct uio *uio = ap->a_uio;
1554 int error;
1555 size_t count, lost;
1556 off_t off = uio->uio_offset;
1557
1558 count = uio->uio_resid;
1559 /* Make sure we don't return partial entries. */
1560 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1561 if (count <= 0)
1562 return (EINVAL);
1563 lost = uio->uio_resid - count;
1564 uio->uio_resid = count;
1565 uio->uio_iov->iov_len = count;
1566 # if (BYTE_ORDER == LITTLE_ENDIAN)
1567 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1568 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1569 } else {
1570 struct dirent *dp, *edp;
1571 struct uio auio;
1572 struct iovec aiov;
1573 caddr_t dirbuf;
1574 int readcnt;
1575 u_char tmp;
1576
1577 auio = *uio;
1578 auio.uio_iov = &aiov;
1579 auio.uio_iovcnt = 1;
1580 auio.uio_segflg = UIO_SYSSPACE;
1581 aiov.iov_len = count;
1582 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1583 aiov.iov_base = dirbuf;
1584 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1585 if (error == 0) {
1586 readcnt = count - auio.uio_resid;
1587 edp = (struct dirent *)&dirbuf[readcnt];
1588 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1589 tmp = dp->d_namlen;
1590 dp->d_namlen = dp->d_type;
1591 dp->d_type = tmp;
1592 if (dp->d_reclen > 0) {
1593 dp = (struct dirent *)
1594 ((char *)dp + dp->d_reclen);
1595 } else {
1596 error = EIO;
1597 break;
1598 }
1599 }
1600 if (dp >= edp)
1601 error = uiomove(dirbuf, readcnt, uio);
1602 }
1603 FREE(dirbuf, M_TEMP);
1604 }
1605 # else
1606 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1607 # endif
1608 if (!error && ap->a_ncookies != NULL) {
1609 struct dirent* dpStart;
1610 struct dirent* dpEnd;
1611 struct dirent* dp;
1612 int ncookies;
1613 u_long *cookies;
1614 u_long *cookiep;
1615
1616 /*
1617 * Only the NFS server uses cookies, and it loads the
1618 * directory block into system space, so we can just look at
1619 * it directly.
1620 */
1621 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1622 panic("ufs_readdir: unexpected uio from NFS server");
1623 dpStart = (struct dirent *)
1624 (uio->uio_iov->iov_base - (uio->uio_offset - off));
1625 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1626 for (dp = dpStart, ncookies = 0;
1627 dp < dpEnd && dp->d_reclen != 0;
1628 dp = (struct dirent *)((caddr_t)dp + dp->d_reclen))
1629 ncookies++;
1630 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1631 M_WAITOK);
1632 for (dp = dpStart, cookiep = cookies;
1633 dp < dpEnd;
1634 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1635 off += dp->d_reclen;
1636 *cookiep++ = (u_long) off;
1637 }
1638 *ap->a_ncookies = ncookies;
1639 *ap->a_cookies = cookies;
1640 }
1641 uio->uio_resid += lost;
1642 if (ap->a_eofflag)
1643 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1644 return (error);
1645 }
1646
1647 /*
1648 * Return target name of a symbolic link
1649 */
1650 int
1651 ufs_readlink(ap)
1652 struct vop_readlink_args /* {
1653 struct vnode *a_vp;
1654 struct uio *a_uio;
1655 struct ucred *a_cred;
1656 } */ *ap;
1657 {
1658 register struct vnode *vp = ap->a_vp;
1659 register struct inode *ip = VTOI(vp);
1660 int isize;
1661
1662 isize = ip->i_size;
1663 if (isize < vp->v_mount->mnt_maxsymlinklen) {
1664 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1665 return (0);
1666 }
1667 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1668 }
1669
1670 /*
1671 * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
1672 * done. If a buffer has been saved in anticipation of a CREATE, delete it.
1673 */
1674 /* ARGSUSED */
1675 int
1676 ufs_abortop(ap)
1677 struct vop_abortop_args /* {
1678 struct vnode *a_dvp;
1679 struct componentname *a_cnp;
1680 } */ *ap;
1681 {
1682 if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
1683 FREE_ZONE(ap->a_cnp->cn_pnbuf, ap->a_cnp->cn_pnlen, M_NAMEI);
1684 return (0);
1685 }
1686
1687 /*
1688 * Lock an inode. If its already locked, set the WANT bit and sleep.
1689 */
1690 int
1691 ufs_lock(ap)
1692 struct vop_lock_args /* {
1693 struct vnode *a_vp;
1694 int a_flags;
1695 struct proc *a_p;
1696 } */ *ap;
1697 {
1698 struct vnode *vp = ap->a_vp;
1699
1700 if (VTOI(vp) == (struct inode *)NULL)
1701 panic("inode in vnode is null\n");
1702 return (lockmgr(&VTOI(vp)->i_lock, ap->a_flags, &vp->v_interlock,
1703 ap->a_p));
1704 }
1705
1706 /*
1707 * Unlock an inode.
1708 */
1709 int
1710 ufs_unlock(ap)
1711 struct vop_unlock_args /* {
1712 struct vnode *a_vp;
1713 int a_flags;
1714 struct proc *a_p;
1715 } */ *ap;
1716 {
1717 struct vnode *vp = ap->a_vp;
1718
1719 return (lockmgr(&VTOI(vp)->i_lock, ap->a_flags | LK_RELEASE,
1720 &vp->v_interlock, ap->a_p));
1721 }
1722
1723 /*
1724 * Check for a locked inode.
1725 */
1726 int
1727 ufs_islocked(ap)
1728 struct vop_islocked_args /* {
1729 struct vnode *a_vp;
1730 } */ *ap;
1731 {
1732
1733 return (lockstatus(&VTOI(ap->a_vp)->i_lock));
1734 }
1735
1736 /*
1737 * Calculate the logical to physical mapping if not done already,
1738 * then call the device strategy routine.
1739 */
1740 int
1741 ufs_strategy(ap)
1742 struct vop_strategy_args /* {
1743 struct buf *a_bp;
1744 } */ *ap;
1745 {
1746 register struct buf *bp = ap->a_bp;
1747 register struct vnode *vp = bp->b_vp;
1748 register struct inode *ip;
1749 int error;
1750
1751 ip = VTOI(vp);
1752 if ( !(bp->b_flags & B_VECTORLIST)) {
1753 if (vp->v_type == VBLK || vp->v_type == VCHR)
1754 panic("ufs_strategy: spec");
1755
1756
1757 if (bp->b_flags & B_PAGELIST) {
1758 /*
1759 * if we have a page list associated with this bp,
1760 * then go through cluste_bp since it knows how to
1761 * deal with a page request that might span non-contiguous
1762 * physical blocks on the disk...
1763 */
1764 #if 1
1765 if (bp->b_blkno == bp->b_lblkno) {
1766 if (error = VOP_BMAP(vp, bp->b_lblkno, NULL,
1767 &bp->b_blkno, NULL)) {
1768 bp->b_error = error;
1769 bp->b_flags |= B_ERROR;
1770 biodone(bp);
1771 return (error);
1772 }
1773 }
1774 #endif /* 1 */
1775 error = cluster_bp(bp);
1776 vp = ip->i_devvp;
1777 bp->b_dev = vp->v_rdev;
1778
1779 return (error);
1780 }
1781
1782 if (bp->b_blkno == bp->b_lblkno) {
1783 if (error =
1784 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL)) {
1785 bp->b_error = error;
1786 bp->b_flags |= B_ERROR;
1787 biodone(bp);
1788 return (error);
1789 }
1790 if ((long)bp->b_blkno == -1)
1791 clrbuf(bp);
1792 }
1793 if ((long)bp->b_blkno == -1) {
1794 biodone(bp);
1795 return (0);
1796 }
1797
1798 }
1799
1800 vp = ip->i_devvp;
1801 bp->b_dev = vp->v_rdev;
1802 VOCALL (vp->v_op, VOFFSET(vop_strategy), ap);
1803 return (0);
1804 }
1805
1806 /*
1807 * Print out the contents of an inode.
1808 */
1809 int
1810 ufs_print(ap)
1811 struct vop_print_args /* {
1812 struct vnode *a_vp;
1813 } */ *ap;
1814 {
1815 register struct vnode *vp = ap->a_vp;
1816 register struct inode *ip = VTOI(vp);
1817
1818 printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number,
1819 major(ip->i_dev), minor(ip->i_dev));
1820 #if FIFO
1821 if (vp->v_type == VFIFO)
1822 fifo_printinfo(vp);
1823 #endif /* FIFO */
1824 lockmgr_printinfo(&ip->i_lock);
1825 printf("\n");
1826 return (0);
1827 }
1828
1829 /*
1830 * Read wrapper for special devices.
1831 */
1832 int
1833 ufsspec_read(ap)
1834 struct vop_read_args /* {
1835 struct vnode *a_vp;
1836 struct uio *a_uio;
1837 int a_ioflag;
1838 struct ucred *a_cred;
1839 } */ *ap;
1840 {
1841
1842 /*
1843 * Set access flag.
1844 */
1845 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1846 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap));
1847 }
1848
1849 /*
1850 * Write wrapper for special devices.
1851 */
1852 int
1853 ufsspec_write(ap)
1854 struct vop_write_args /* {
1855 struct vnode *a_vp;
1856 struct uio *a_uio;
1857 int a_ioflag;
1858 struct ucred *a_cred;
1859 } */ *ap;
1860 {
1861
1862 /*
1863 * Set update and change flags.
1864 */
1865 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1866 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap));
1867 }
1868
1869 /*
1870 * Close wrapper for special devices.
1871 *
1872 * Update the times on the inode then do device close.
1873 */
1874 int
1875 ufsspec_close(ap)
1876 struct vop_close_args /* {
1877 struct vnode *a_vp;
1878 int a_fflag;
1879 struct ucred *a_cred;
1880 struct proc *a_p;
1881 } */ *ap;
1882 {
1883 struct vnode *vp = ap->a_vp;
1884 struct inode *ip = VTOI(vp);
1885
1886 simple_lock(&vp->v_interlock);
1887 if (ap->a_vp->v_usecount > 1)
1888 ITIMES(ip, &time, &time);
1889 simple_unlock(&vp->v_interlock);
1890 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
1891 }
1892
1893 #if FIFO
1894 /*
1895 * Read wrapper for fifo's
1896 */
1897 int
1898 ufsfifo_read(ap)
1899 struct vop_read_args /* {
1900 struct vnode *a_vp;
1901 struct uio *a_uio;
1902 int a_ioflag;
1903 struct ucred *a_cred;
1904 } */ *ap;
1905 {
1906 extern int (**fifo_vnodeop_p)(void *);
1907
1908 /*
1909 * Set access flag.
1910 */
1911 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1912 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap));
1913 }
1914
1915 /*
1916 * Write wrapper for fifo's.
1917 */
1918 int
1919 ufsfifo_write(ap)
1920 struct vop_write_args /* {
1921 struct vnode *a_vp;
1922 struct uio *a_uio;
1923 int a_ioflag;
1924 struct ucred *a_cred;
1925 } */ *ap;
1926 {
1927 extern int (**fifo_vnodeop_p)(void *);
1928
1929 /*
1930 * Set update and change flags.
1931 */
1932 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1933 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap));
1934 }
1935
1936 /*
1937 * Close wrapper for fifo's.
1938 *
1939 * Update the times on the inode then do device close.
1940 */
1941 ufsfifo_close(ap)
1942 struct vop_close_args /* {
1943 struct vnode *a_vp;
1944 int a_fflag;
1945 struct ucred *a_cred;
1946 struct proc *a_p;
1947 } */ *ap;
1948 {
1949 extern int (**fifo_vnodeop_p)(void *);
1950 struct vnode *vp = ap->a_vp;
1951 struct inode *ip = VTOI(vp);
1952
1953 simple_lock(&vp->v_interlock);
1954 if (ap->a_vp->v_usecount > 1)
1955 ITIMES(ip, &time, &time);
1956 simple_unlock(&vp->v_interlock);
1957 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
1958 }
1959 #endif /* FIFO */
1960
1961 /*
1962 * Return POSIX pathconf information applicable to ufs filesystems.
1963 */
1964 ufs_pathconf(ap)
1965 struct vop_pathconf_args /* {
1966 struct vnode *a_vp;
1967 int a_name;
1968 int *a_retval;
1969 } */ *ap;
1970 {
1971
1972 switch (ap->a_name) {
1973 case _PC_LINK_MAX:
1974 *ap->a_retval = LINK_MAX;
1975 return (0);
1976 case _PC_NAME_MAX:
1977 *ap->a_retval = NAME_MAX;
1978 return (0);
1979 case _PC_PATH_MAX:
1980 *ap->a_retval = PATH_MAX;
1981 return (0);
1982 case _PC_PIPE_BUF:
1983 *ap->a_retval = PIPE_BUF;
1984 return (0);
1985 case _PC_CHOWN_RESTRICTED:
1986 *ap->a_retval = 1;
1987 return (0);
1988 case _PC_NO_TRUNC:
1989 *ap->a_retval = 1;
1990 return (0);
1991 default:
1992 return (EINVAL);
1993 }
1994 /* NOTREACHED */
1995 }
1996
1997 /*
1998 * Advisory record locking support
1999 */
2000 int
2001 ufs_advlock(ap)
2002 struct vop_advlock_args /* {
2003 struct vnode *a_vp;
2004 caddr_t a_id;
2005 int a_op;
2006 struct flock *a_fl;
2007 int a_flags;
2008 } */ *ap;
2009 {
2010 register struct inode *ip = VTOI(ap->a_vp);
2011 register struct flock *fl = ap->a_fl;
2012 register struct lockf *lock;
2013 off_t start, end;
2014 int error;
2015
2016 /*
2017 * Avoid the common case of unlocking when inode has no locks.
2018 */
2019 if (ip->i_lockf == (struct lockf *)0) {
2020 if (ap->a_op != F_SETLK) {
2021 fl->l_type = F_UNLCK;
2022 return (0);
2023 }
2024 }
2025 /*
2026 * Convert the flock structure into a start and end.
2027 */
2028 switch (fl->l_whence) {
2029
2030 case SEEK_SET:
2031 case SEEK_CUR:
2032 /*
2033 * Caller is responsible for adding any necessary offset
2034 * when SEEK_CUR is used.
2035 */
2036 start = fl->l_start;
2037 break;
2038
2039 case SEEK_END:
2040 start = ip->i_size + fl->l_start;
2041 break;
2042
2043 default:
2044 return (EINVAL);
2045 }
2046 if (start < 0)
2047 return (EINVAL);
2048 if (fl->l_len == 0)
2049 end = -1;
2050 else
2051 end = start + fl->l_len - 1;
2052 /*
2053 * Create the lockf structure
2054 */
2055 MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
2056 lock->lf_start = start;
2057 lock->lf_end = end;
2058 lock->lf_id = ap->a_id;
2059 lock->lf_inode = ip;
2060 lock->lf_type = fl->l_type;
2061 lock->lf_next = (struct lockf *)0;
2062 TAILQ_INIT(&lock->lf_blkhd);
2063 lock->lf_flags = ap->a_flags;
2064 /*
2065 * Do the requested operation.
2066 */
2067 switch(ap->a_op) {
2068 case F_SETLK:
2069 return (lf_setlock(lock));
2070
2071 case F_UNLCK:
2072 error = lf_clearlock(lock);
2073 FREE(lock, M_LOCKF);
2074 return (error);
2075
2076 case F_GETLK:
2077 error = lf_getlock(lock, fl);
2078 FREE(lock, M_LOCKF);
2079 return (error);
2080
2081 default:
2082 _FREE(lock, M_LOCKF);
2083 return (EINVAL);
2084 }
2085 /* NOTREACHED */
2086 }
2087
2088 /*
2089 * Initialize the vnode associated with a new inode, handle aliased
2090 * vnodes.
2091 */
2092 int
2093 ufs_vinit(mntp, specops, fifoops, vpp)
2094 struct mount *mntp;
2095 int (**specops)();
2096 int (**fifoops)();
2097 struct vnode **vpp;
2098 {
2099 struct proc *p = current_proc(); /* XXX */
2100 struct inode *ip;
2101 struct vnode *vp, *nvp;
2102
2103 vp = *vpp;
2104 ip = VTOI(vp);
2105 switch(vp->v_type = IFTOVT(ip->i_mode)) {
2106 case VCHR:
2107 case VBLK:
2108 vp->v_op = specops;
2109 if (nvp = checkalias(vp, ip->i_rdev, mntp)) {
2110 /*
2111 * Discard unneeded vnode, but save its inode.
2112 * Note that the lock is carried over in the inode
2113 * to the replacement vnode.
2114 */
2115 nvp->v_data = vp->v_data;
2116 vp->v_data = NULL;
2117 vp->v_op = spec_vnodeop_p;
2118 vrele(vp);
2119 vgone(vp);
2120 /*
2121 * Reinitialize aliased inode.
2122 */
2123 vp = nvp;
2124 ip->i_vnode = vp;
2125 }
2126 break;
2127 case VFIFO:
2128 #if FIFO
2129 vp->v_op = fifoops;
2130 break;
2131 #else
2132 return (EOPNOTSUPP);
2133 #endif
2134 case VREG:
2135 #if 0
2136 ubc_info_init(vp);
2137 #endif /* 0 */
2138 break;
2139 default:
2140 break;
2141 }
2142 if (ip->i_number == ROOTINO)
2143 vp->v_flag |= VROOT;
2144 /*
2145 * Initialize modrev times
2146 */
2147 SETHIGH(ip->i_modrev, time.tv_sec);
2148 SETLOW(ip->i_modrev, time.tv_usec * 4294);
2149 *vpp = vp;
2150 return (0);
2151 }
2152
2153 /*
2154 * Allocate a new inode.
2155 */
2156 int
2157 ufs_makeinode(mode, dvp, vpp, cnp)
2158 int mode;
2159 struct vnode *dvp;
2160 struct vnode **vpp;
2161 struct componentname *cnp;
2162 {
2163 register struct inode *ip, *pdir;
2164 struct timeval tv;
2165 struct vnode *tvp;
2166 int error;
2167
2168 pdir = VTOI(dvp);
2169 #if DIAGNOSTIC
2170 if ((cnp->cn_flags & HASBUF) == 0)
2171 panic("ufs_makeinode: no name");
2172 #endif
2173 *vpp = NULL;
2174 if ((mode & IFMT) == 0)
2175 mode |= IFREG;
2176
2177 if (error = VOP_VALLOC(dvp, mode, cnp->cn_cred, &tvp)) {
2178 _FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
2179 vput(dvp);
2180 return (error);
2181 }
2182 ip = VTOI(tvp);
2183 ip->i_gid = pdir->i_gid;
2184 if ((mode & IFMT) == IFLNK)
2185 ip->i_uid = pdir->i_uid;
2186 else
2187 ip->i_uid = cnp->cn_cred->cr_uid;
2188 #if QUOTA
2189 if ((error = getinoquota(ip)) ||
2190 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2191 _FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
2192 VOP_VFREE(tvp, ip->i_number, mode);
2193 vput(tvp);
2194 vput(dvp);
2195 return (error);
2196 }
2197 #endif
2198 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2199 ip->i_mode = mode;
2200 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2201 ip->i_nlink = 1;
2202 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2203 suser(cnp->cn_cred, NULL))
2204 ip->i_mode &= ~ISGID;
2205
2206 if (cnp->cn_flags & ISWHITEOUT)
2207 ip->i_flags |= UF_OPAQUE;
2208
2209 /*
2210 * initialize UBC before calling VOP_UPDATE and ufs_direnter
2211 * Not doing so introduces probelms in handling error from
2212 * those calls.
2213 * It results in a "vget: stolen ubc_info" panic due to attempt
2214 * to shutdown uninitialized UBC.
2215 */
2216 if (UBCINFOMISSING(tvp) || UBCINFORECLAIMED(tvp))
2217 ubc_info_init(tvp);
2218
2219 /*
2220 * Make sure inode goes to disk before directory entry.
2221 */
2222 tv = time;
2223 if (error = VOP_UPDATE(tvp, &tv, &tv, 1))
2224 goto bad;
2225 if (error = ufs_direnter(ip, dvp, cnp))
2226 goto bad;
2227 if ((cnp->cn_flags & SAVESTART) == 0)
2228 FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
2229 vput(dvp);
2230
2231 *vpp = tvp;
2232 return (0);
2233
2234 bad:
2235 /*
2236 * Write error occurred trying to update the inode
2237 * or the directory so must deallocate the inode.
2238 */
2239 _FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
2240 vput(dvp);
2241 ip->i_nlink = 0;
2242 ip->i_flag |= IN_CHANGE;
2243 vput(tvp);
2244 return (error);
2245 }
2246