ulfs_vnops.c revision 1.13.4.1 1 /* $NetBSD: ulfs_vnops.c,v 1.13.4.1 2013/08/28 23:59:38 rmind Exp $ */
2 /* from NetBSD: ufs_vnops.c,v 1.213 2013/06/08 05:47:02 kardel Exp */
3
4 /*-
5 * Copyright (c) 2008 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Wasabi Systems, Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1989, 1993, 1995
35 * The Regents of the University of California. All rights reserved.
36 * (c) UNIX System Laboratories, Inc.
37 * All or some portions of this file are derived from material licensed
38 * to the University of California by American Telephone and Telegraph
39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 * the permission of UNIX System Laboratories, Inc.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)ufs_vnops.c 8.28 (Berkeley) 7/31/95
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: ulfs_vnops.c,v 1.13.4.1 2013/08/28 23:59:38 rmind Exp $");
71
72 #if defined(_KERNEL_OPT)
73 #include "opt_lfs.h"
74 #include "opt_quota.h"
75 #endif
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/namei.h>
80 #include <sys/resourcevar.h>
81 #include <sys/kernel.h>
82 #include <sys/file.h>
83 #include <sys/stat.h>
84 #include <sys/buf.h>
85 #include <sys/proc.h>
86 #include <sys/mount.h>
87 #include <sys/vnode.h>
88 #include <sys/kmem.h>
89 #include <sys/malloc.h>
90 #include <sys/dirent.h>
91 #include <sys/lockf.h>
92 #include <sys/kauth.h>
93 #include <sys/wapbl.h>
94 #include <sys/fstrans.h>
95
96 #include <miscfs/specfs/specdev.h>
97 #include <miscfs/fifofs/fifo.h>
98 #include <miscfs/genfs/genfs.h>
99
100 #include <ufs/lfs/ulfs_inode.h>
101 #include <ufs/lfs/ulfsmount.h>
102 #include <ufs/lfs/ulfs_bswap.h>
103 #include <ufs/lfs/ulfs_extern.h>
104 #ifdef LFS_DIRHASH
105 #include <ufs/lfs/ulfs_dirhash.h>
106 #endif
107 #include <ufs/lfs/lfs_extern.h>
108 #include <ufs/lfs/lfs.h>
109
110 #include <uvm/uvm.h>
111
112 static int ulfs_chmod(struct vnode *, int, kauth_cred_t, struct lwp *);
113 static int ulfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t,
114 struct lwp *);
115
116 /*
117 * A virgin directory (no blushing please).
118 */
119 static const struct lfs_dirtemplate mastertemplate = {
120 0, 12, LFS_DT_DIR, 1, ".",
121 0, LFS_DIRBLKSIZ - 12, LFS_DT_DIR, 2, ".."
122 };
123
124 /*
125 * Create a regular file
126 */
127 int
128 ulfs_create(void *v)
129 {
130 struct vop_create_args /* {
131 struct vnode *a_dvp;
132 struct vnode **a_vpp;
133 struct componentname *a_cnp;
134 struct vattr *a_vap;
135 } */ *ap = v;
136 int error;
137 struct vnode *dvp = ap->a_dvp;
138 struct ulfs_lookup_results *ulr;
139
140 /* XXX should handle this material another way */
141 ulr = &VTOI(dvp)->i_crap;
142 ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
143
144 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
145 error =
146 ulfs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
147 dvp, ulr, ap->a_vpp, ap->a_cnp);
148 if (error) {
149 fstrans_done(dvp->v_mount);
150 return (error);
151 }
152 fstrans_done(dvp->v_mount);
153 VN_KNOTE(dvp, NOTE_WRITE);
154 return (0);
155 }
156
157 /*
158 * Open called.
159 *
160 * Nothing to do.
161 */
162 /* ARGSUSED */
163 int
164 ulfs_open(void *v)
165 {
166 struct vop_open_args /* {
167 struct vnode *a_vp;
168 int a_mode;
169 kauth_cred_t a_cred;
170 } */ *ap = v;
171
172 /*
173 * Files marked append-only must be opened for appending.
174 */
175 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
176 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
177 return (EPERM);
178 return (0);
179 }
180
181 static int
182 ulfs_check_possible(struct vnode *vp, struct inode *ip, mode_t mode,
183 kauth_cred_t cred)
184 {
185 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
186 int error;
187 #endif
188
189 /*
190 * Disallow write attempts on read-only file systems;
191 * unless the file is a socket, fifo, or a block or
192 * character device resident on the file system.
193 */
194 if (mode & VWRITE) {
195 switch (vp->v_type) {
196 case VDIR:
197 case VLNK:
198 case VREG:
199 if (vp->v_mount->mnt_flag & MNT_RDONLY)
200 return (EROFS);
201 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
202 fstrans_start(vp->v_mount, FSTRANS_SHARED);
203 error = lfs_chkdq(ip, 0, cred, 0);
204 fstrans_done(vp->v_mount);
205 if (error != 0)
206 return error;
207 #endif
208 break;
209 case VBAD:
210 case VBLK:
211 case VCHR:
212 case VSOCK:
213 case VFIFO:
214 case VNON:
215 default:
216 break;
217 }
218 }
219
220 /* If it is a snapshot, nobody gets access to it. */
221 if ((ip->i_flags & SF_SNAPSHOT))
222 return (EPERM);
223 /* If immutable bit set, nobody gets to write it. */
224 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
225 return (EPERM);
226
227 return 0;
228 }
229
230 static int
231 ulfs_check_permitted(struct vnode *vp, struct inode *ip, mode_t mode,
232 kauth_cred_t cred)
233 {
234
235 return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode, vp->v_type,
236 ip->i_mode & ALLPERMS), vp, NULL, genfs_can_access(vp->v_type,
237 ip->i_mode & ALLPERMS, ip->i_uid, ip->i_gid, mode, cred));
238 }
239
240 int
241 ulfs_access(void *v)
242 {
243 struct vop_access_args /* {
244 struct vnode *a_vp;
245 int a_mode;
246 kauth_cred_t a_cred;
247 } */ *ap = v;
248 struct vnode *vp;
249 struct inode *ip;
250 mode_t mode;
251 int error;
252
253 vp = ap->a_vp;
254 ip = VTOI(vp);
255 mode = ap->a_mode;
256
257 error = ulfs_check_possible(vp, ip, mode, ap->a_cred);
258 if (error)
259 return error;
260
261 error = ulfs_check_permitted(vp, ip, mode, ap->a_cred);
262
263 return error;
264 }
265
266 /*
267 * Set attribute vnode op. called from several syscalls
268 */
269 int
270 ulfs_setattr(void *v)
271 {
272 struct vop_setattr_args /* {
273 struct vnode *a_vp;
274 struct vattr *a_vap;
275 kauth_cred_t a_cred;
276 } */ *ap = v;
277 struct vattr *vap;
278 struct vnode *vp;
279 struct inode *ip;
280 kauth_cred_t cred;
281 struct lwp *l;
282 int error;
283 kauth_action_t action;
284 bool changing_sysflags;
285
286 vap = ap->a_vap;
287 vp = ap->a_vp;
288 ip = VTOI(vp);
289 cred = ap->a_cred;
290 l = curlwp;
291 action = KAUTH_VNODE_WRITE_FLAGS;
292 changing_sysflags = false;
293
294 /*
295 * Check for unsettable attributes.
296 */
297 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
298 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
299 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
300 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
301 return (EINVAL);
302 }
303
304 fstrans_start(vp->v_mount, FSTRANS_SHARED);
305
306 if (vap->va_flags != VNOVAL) {
307 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
308 error = EROFS;
309 goto out;
310 }
311
312 /* Snapshot flag cannot be set or cleared */
313 if ((vap->va_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) !=
314 (ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))) {
315 error = EPERM;
316 goto out;
317 }
318
319 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
320 action |= KAUTH_VNODE_HAS_SYSFLAGS;
321 }
322
323 if ((vap->va_flags & SF_SETTABLE) != (ip->i_flags & SF_SETTABLE)) {
324 action |= KAUTH_VNODE_WRITE_SYSFLAGS;
325 changing_sysflags = true;
326 }
327
328 error = kauth_authorize_vnode(cred, action, vp, NULL,
329 genfs_can_chflags(cred, vp->v_type, ip->i_uid,
330 changing_sysflags));
331 if (error)
332 goto out;
333
334 if (changing_sysflags) {
335 ip->i_flags = vap->va_flags;
336 DIP_ASSIGN(ip, flags, ip->i_flags);
337 } else {
338 ip->i_flags &= SF_SETTABLE;
339 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
340 DIP_ASSIGN(ip, flags, ip->i_flags);
341 }
342 ip->i_flag |= IN_CHANGE;
343 if (vap->va_flags & (IMMUTABLE | APPEND)) {
344 error = 0;
345 goto out;
346 }
347 }
348 if (ip->i_flags & (IMMUTABLE | APPEND)) {
349 error = EPERM;
350 goto out;
351 }
352 /*
353 * Go through the fields and update iff not VNOVAL.
354 */
355 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
356 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
357 error = EROFS;
358 goto out;
359 }
360 error = ulfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
361 if (error)
362 goto out;
363 }
364 if (vap->va_size != VNOVAL) {
365 /*
366 * Disallow write attempts on read-only file systems;
367 * unless the file is a socket, fifo, or a block or
368 * character device resident on the file system.
369 */
370 switch (vp->v_type) {
371 case VDIR:
372 error = EISDIR;
373 goto out;
374 case VCHR:
375 case VBLK:
376 case VFIFO:
377 break;
378 case VREG:
379 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
380 error = EROFS;
381 goto out;
382 }
383 if ((ip->i_flags & SF_SNAPSHOT) != 0) {
384 error = EPERM;
385 goto out;
386 }
387 error = lfs_truncate(vp, vap->va_size, 0, cred);
388 if (error)
389 goto out;
390 break;
391 default:
392 error = EOPNOTSUPP;
393 goto out;
394 }
395 }
396 ip = VTOI(vp);
397 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
398 vap->va_birthtime.tv_sec != VNOVAL) {
399 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
400 error = EROFS;
401 goto out;
402 }
403 if ((ip->i_flags & SF_SNAPSHOT) != 0) {
404 error = EPERM;
405 goto out;
406 }
407 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
408 NULL, genfs_can_chtimes(vp, vap->va_vaflags, ip->i_uid, cred));
409 if (error)
410 goto out;
411 if (vap->va_atime.tv_sec != VNOVAL)
412 if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
413 ip->i_flag |= IN_ACCESS;
414 if (vap->va_mtime.tv_sec != VNOVAL) {
415 ip->i_flag |= IN_CHANGE | IN_UPDATE;
416 if (vp->v_mount->mnt_flag & MNT_RELATIME)
417 ip->i_flag |= IN_ACCESS;
418 }
419 if (vap->va_birthtime.tv_sec != VNOVAL &&
420 ip->i_ump->um_fstype == ULFS2) {
421 ip->i_ffs2_birthtime = vap->va_birthtime.tv_sec;
422 ip->i_ffs2_birthnsec = vap->va_birthtime.tv_nsec;
423 }
424 error = lfs_update(vp, &vap->va_atime, &vap->va_mtime, 0);
425 if (error)
426 goto out;
427 }
428 error = 0;
429 if (vap->va_mode != (mode_t)VNOVAL) {
430 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
431 error = EROFS;
432 goto out;
433 }
434 if ((ip->i_flags & SF_SNAPSHOT) != 0 &&
435 (vap->va_mode & (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP |
436 S_IXOTH | S_IWOTH))) {
437 error = EPERM;
438 goto out;
439 }
440 error = ulfs_chmod(vp, (int)vap->va_mode, cred, l);
441 }
442 VN_KNOTE(vp, NOTE_ATTRIB);
443 out:
444 fstrans_done(vp->v_mount);
445 return (error);
446 }
447
448 /*
449 * Change the mode on a file.
450 * Inode must be locked before calling.
451 */
452 static int
453 ulfs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct lwp *l)
454 {
455 struct inode *ip;
456 int error;
457
458 ip = VTOI(vp);
459
460 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
461 NULL, genfs_can_chmod(vp->v_type, cred, ip->i_uid, ip->i_gid, mode));
462 if (error)
463 return (error);
464
465 fstrans_start(vp->v_mount, FSTRANS_SHARED);
466 ip->i_mode &= ~ALLPERMS;
467 ip->i_mode |= (mode & ALLPERMS);
468 ip->i_flag |= IN_CHANGE;
469 DIP_ASSIGN(ip, mode, ip->i_mode);
470 fstrans_done(vp->v_mount);
471 return (0);
472 }
473
474 /*
475 * Perform chown operation on inode ip;
476 * inode must be locked prior to call.
477 */
478 static int
479 ulfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
480 struct lwp *l)
481 {
482 struct inode *ip;
483 int error = 0;
484 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
485 uid_t ouid;
486 gid_t ogid;
487 int64_t change;
488 #endif
489 ip = VTOI(vp);
490 error = 0;
491
492 if (uid == (uid_t)VNOVAL)
493 uid = ip->i_uid;
494 if (gid == (gid_t)VNOVAL)
495 gid = ip->i_gid;
496
497 error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
498 NULL, genfs_can_chown(cred, ip->i_uid, ip->i_gid, uid, gid));
499 if (error)
500 return (error);
501
502 fstrans_start(vp->v_mount, FSTRANS_SHARED);
503 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
504 ogid = ip->i_gid;
505 ouid = ip->i_uid;
506 change = DIP(ip, blocks);
507 (void) lfs_chkdq(ip, -change, cred, 0);
508 (void) lfs_chkiq(ip, -1, cred, 0);
509 #endif
510 ip->i_gid = gid;
511 DIP_ASSIGN(ip, gid, gid);
512 ip->i_uid = uid;
513 DIP_ASSIGN(ip, uid, uid);
514 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
515 if ((error = lfs_chkdq(ip, change, cred, 0)) == 0) {
516 if ((error = lfs_chkiq(ip, 1, cred, 0)) == 0)
517 goto good;
518 else
519 (void) lfs_chkdq(ip, -change, cred, FORCE);
520 }
521 ip->i_gid = ogid;
522 DIP_ASSIGN(ip, gid, ogid);
523 ip->i_uid = ouid;
524 DIP_ASSIGN(ip, uid, ouid);
525 (void) lfs_chkdq(ip, change, cred, FORCE);
526 (void) lfs_chkiq(ip, 1, cred, FORCE);
527 fstrans_done(vp->v_mount);
528 return (error);
529 good:
530 #endif /* LFS_QUOTA || LFS_QUOTA2 */
531 ip->i_flag |= IN_CHANGE;
532 fstrans_done(vp->v_mount);
533 return (0);
534 }
535
536 int
537 ulfs_remove(void *v)
538 {
539 struct vop_remove_args /* {
540 struct vnode *a_dvp;
541 struct vnode *a_vp;
542 struct componentname *a_cnp;
543 } */ *ap = v;
544 struct vnode *vp, *dvp;
545 struct inode *ip;
546 int error;
547 struct ulfs_lookup_results *ulr;
548
549 vp = ap->a_vp;
550 dvp = ap->a_dvp;
551 ip = VTOI(vp);
552
553 /* XXX should handle this material another way */
554 ulr = &VTOI(dvp)->i_crap;
555 ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
556
557 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
558 if (vp->v_type == VDIR || (ip->i_flags & (IMMUTABLE | APPEND)) ||
559 (VTOI(dvp)->i_flags & APPEND))
560 error = EPERM;
561 else {
562 error = ulfs_dirremove(dvp, ulr,
563 ip, ap->a_cnp->cn_flags, 0);
564 }
565 VN_KNOTE(vp, NOTE_DELETE);
566 VN_KNOTE(dvp, NOTE_WRITE);
567 if (dvp == vp)
568 vrele(vp);
569 else
570 vput(vp);
571 vput(dvp);
572 fstrans_done(dvp->v_mount);
573 return (error);
574 }
575
576 /*
577 * ulfs_link: create hard link.
578 */
579 int
580 ulfs_link(void *v)
581 {
582 struct vop_link_args /* {
583 struct vnode *a_dvp;
584 struct vnode *a_vp;
585 struct componentname *a_cnp;
586 } */ *ap = v;
587 struct vnode *dvp = ap->a_dvp;
588 struct vnode *vp = ap->a_vp;
589 struct componentname *cnp = ap->a_cnp;
590 struct inode *ip;
591 struct lfs_direct *newdir;
592 int error;
593 struct ulfs_lookup_results *ulr;
594
595 KASSERT(dvp != vp);
596 KASSERT(vp->v_type != VDIR);
597 KASSERT(dvp->v_mount == vp->v_mount);
598
599 /* XXX should handle this material another way */
600 ulr = &VTOI(dvp)->i_crap;
601 ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
602
603 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
604 error = vn_lock(vp, LK_EXCLUSIVE);
605 if (error) {
606 VOP_ABORTOP(dvp, cnp);
607 goto out2;
608 }
609 ip = VTOI(vp);
610 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
611 VOP_ABORTOP(dvp, cnp);
612 error = EMLINK;
613 goto out1;
614 }
615 if (ip->i_flags & (IMMUTABLE | APPEND)) {
616 VOP_ABORTOP(dvp, cnp);
617 error = EPERM;
618 goto out1;
619 }
620 ip->i_nlink++;
621 DIP_ASSIGN(ip, nlink, ip->i_nlink);
622 ip->i_flag |= IN_CHANGE;
623 error = lfs_update(vp, NULL, NULL, UPDATE_DIROP);
624 if (!error) {
625 newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
626 ulfs_makedirentry(ip, cnp, newdir);
627 error = ulfs_direnter(dvp, ulr, vp, newdir, cnp, NULL);
628 pool_cache_put(ulfs_direct_cache, newdir);
629 }
630 if (error) {
631 ip->i_nlink--;
632 DIP_ASSIGN(ip, nlink, ip->i_nlink);
633 ip->i_flag |= IN_CHANGE;
634 }
635 out1:
636 VOP_UNLOCK(vp);
637 out2:
638 VN_KNOTE(vp, NOTE_LINK);
639 VN_KNOTE(dvp, NOTE_WRITE);
640 vput(dvp);
641 fstrans_done(dvp->v_mount);
642 return (error);
643 }
644
645 /*
646 * whiteout vnode call
647 */
648 int
649 ulfs_whiteout(void *v)
650 {
651 struct vop_whiteout_args /* {
652 struct vnode *a_dvp;
653 struct componentname *a_cnp;
654 int a_flags;
655 } */ *ap = v;
656 struct vnode *dvp = ap->a_dvp;
657 struct componentname *cnp = ap->a_cnp;
658 struct lfs_direct *newdir;
659 int error;
660 struct ulfsmount *ump = VFSTOULFS(dvp->v_mount);
661 struct lfs *fs = ump->um_lfs;
662 struct ulfs_lookup_results *ulr;
663
664 /* XXX should handle this material another way */
665 ulr = &VTOI(dvp)->i_crap;
666 ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
667
668 error = 0;
669 switch (ap->a_flags) {
670 case LOOKUP:
671 /* 4.4 format directories support whiteout operations */
672 if (fs->um_maxsymlinklen > 0)
673 return (0);
674 return (EOPNOTSUPP);
675
676 case CREATE:
677 /* create a new directory whiteout */
678 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
679 #ifdef DIAGNOSTIC
680 if (fs->um_maxsymlinklen <= 0)
681 panic("ulfs_whiteout: old format filesystem");
682 #endif
683
684 newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
685 newdir->d_ino = ULFS_WINO;
686 newdir->d_namlen = cnp->cn_namelen;
687 memcpy(newdir->d_name, cnp->cn_nameptr,
688 (size_t)cnp->cn_namelen);
689 newdir->d_name[cnp->cn_namelen] = '\0';
690 newdir->d_type = LFS_DT_WHT;
691 error = ulfs_direnter(dvp, ulr, NULL, newdir, cnp, NULL);
692 pool_cache_put(ulfs_direct_cache, newdir);
693 break;
694
695 case DELETE:
696 /* remove an existing directory whiteout */
697 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
698 #ifdef DIAGNOSTIC
699 if (fs->um_maxsymlinklen <= 0)
700 panic("ulfs_whiteout: old format filesystem");
701 #endif
702
703 cnp->cn_flags &= ~DOWHITEOUT;
704 error = ulfs_dirremove(dvp, ulr, NULL, cnp->cn_flags, 0);
705 break;
706 default:
707 panic("ulfs_whiteout: unknown op");
708 /* NOTREACHED */
709 }
710 fstrans_done(dvp->v_mount);
711 return (error);
712 }
713
714 int
715 ulfs_mkdir(void *v)
716 {
717 struct vop_mkdir_args /* {
718 struct vnode *a_dvp;
719 struct vnode **a_vpp;
720 struct componentname *a_cnp;
721 struct vattr *a_vap;
722 } */ *ap = v;
723 struct vnode *dvp = ap->a_dvp, *tvp;
724 struct vattr *vap = ap->a_vap;
725 struct componentname *cnp = ap->a_cnp;
726 struct inode *ip, *dp = VTOI(dvp);
727 struct buf *bp;
728 struct lfs_dirtemplate dirtemplate;
729 struct lfs_direct *newdir;
730 int error, dmode;
731 struct ulfsmount *ump = dp->i_ump;
732 struct lfs *fs = ump->um_lfs;
733 int dirblksiz = fs->um_dirblksiz;
734 struct ulfs_lookup_results *ulr;
735
736 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
737
738 /* XXX should handle this material another way */
739 ulr = &dp->i_crap;
740 ULFS_CHECK_CRAPCOUNTER(dp);
741
742 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
743 error = EMLINK;
744 goto out;
745 }
746 dmode = vap->va_mode & ACCESSPERMS;
747 dmode |= LFS_IFDIR;
748 /*
749 * Must simulate part of ulfs_makeinode here to acquire the inode,
750 * but not have it entered in the parent directory. The entry is
751 * made later after writing "." and ".." entries.
752 */
753 if ((error = lfs_valloc(dvp, dmode, cnp->cn_cred, ap->a_vpp)) != 0)
754 goto out;
755
756 tvp = *ap->a_vpp;
757 ip = VTOI(tvp);
758
759 ip->i_uid = kauth_cred_geteuid(cnp->cn_cred);
760 DIP_ASSIGN(ip, uid, ip->i_uid);
761 ip->i_gid = dp->i_gid;
762 DIP_ASSIGN(ip, gid, ip->i_gid);
763 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
764 if ((error = lfs_chkiq(ip, 1, cnp->cn_cred, 0))) {
765 lfs_vfree(tvp, ip->i_number, dmode);
766 fstrans_done(dvp->v_mount);
767 vput(tvp);
768 vput(dvp);
769 return (error);
770 }
771 #endif
772 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
773 ip->i_mode = dmode;
774 DIP_ASSIGN(ip, mode, dmode);
775 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
776 ip->i_nlink = 2;
777 DIP_ASSIGN(ip, nlink, 2);
778 if (cnp->cn_flags & ISWHITEOUT) {
779 ip->i_flags |= UF_OPAQUE;
780 DIP_ASSIGN(ip, flags, ip->i_flags);
781 }
782
783 /*
784 * Bump link count in parent directory to reflect work done below.
785 * Should be done before reference is created so cleanup is
786 * possible if we crash.
787 */
788 dp->i_nlink++;
789 DIP_ASSIGN(dp, nlink, dp->i_nlink);
790 dp->i_flag |= IN_CHANGE;
791 if ((error = lfs_update(dvp, NULL, NULL, UPDATE_DIROP)) != 0)
792 goto bad;
793
794 /*
795 * Initialize directory with "." and ".." from static template.
796 */
797 dirtemplate = mastertemplate;
798 dirtemplate.dotdot_reclen = dirblksiz - dirtemplate.dot_reclen;
799 dirtemplate.dot_ino = ulfs_rw32(ip->i_number, ULFS_MPNEEDSWAP(fs));
800 dirtemplate.dotdot_ino = ulfs_rw32(dp->i_number, ULFS_MPNEEDSWAP(fs));
801 dirtemplate.dot_reclen = ulfs_rw16(dirtemplate.dot_reclen,
802 ULFS_MPNEEDSWAP(fs));
803 dirtemplate.dotdot_reclen = ulfs_rw16(dirtemplate.dotdot_reclen,
804 ULFS_MPNEEDSWAP(fs));
805 if (fs->um_maxsymlinklen <= 0) {
806 #if BYTE_ORDER == LITTLE_ENDIAN
807 if (ULFS_MPNEEDSWAP(fs) == 0)
808 #else
809 if (ULFS_MPNEEDSWAP(fs) != 0)
810 #endif
811 {
812 dirtemplate.dot_type = dirtemplate.dot_namlen;
813 dirtemplate.dotdot_type = dirtemplate.dotdot_namlen;
814 dirtemplate.dot_namlen = dirtemplate.dotdot_namlen = 0;
815 } else
816 dirtemplate.dot_type = dirtemplate.dotdot_type = 0;
817 }
818 if ((error = lfs_balloc(tvp, (off_t)0, dirblksiz, cnp->cn_cred,
819 B_CLRBUF, &bp)) != 0)
820 goto bad;
821 ip->i_size = dirblksiz;
822 DIP_ASSIGN(ip, size, dirblksiz);
823 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
824 uvm_vnp_setsize(tvp, ip->i_size);
825 memcpy((void *)bp->b_data, (void *)&dirtemplate, sizeof dirtemplate);
826
827 /*
828 * Directory set up, now install it's entry in the parent directory.
829 * We must write out the buffer containing the new directory body
830 * before entering the new name in the parent.
831 */
832 if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0)
833 goto bad;
834 if ((error = lfs_update(tvp, NULL, NULL, UPDATE_DIROP)) != 0) {
835 goto bad;
836 }
837 newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
838 ulfs_makedirentry(ip, cnp, newdir);
839 error = ulfs_direnter(dvp, ulr, tvp, newdir, cnp, bp);
840 pool_cache_put(ulfs_direct_cache, newdir);
841 bad:
842 if (error == 0) {
843 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
844 } else {
845 dp->i_nlink--;
846 DIP_ASSIGN(dp, nlink, dp->i_nlink);
847 dp->i_flag |= IN_CHANGE;
848 /*
849 * No need to do an explicit lfs_truncate here, vrele will
850 * do this for us because we set the link count to 0.
851 */
852 ip->i_nlink = 0;
853 DIP_ASSIGN(ip, nlink, 0);
854 ip->i_flag |= IN_CHANGE;
855 /* If IN_ADIROP, account for it */
856 lfs_unmark_vnode(tvp);
857 vput(tvp);
858 }
859 out:
860 fstrans_done(dvp->v_mount);
861 vput(dvp);
862 return (error);
863 }
864
865 int
866 ulfs_rmdir(void *v)
867 {
868 struct vop_rmdir_args /* {
869 struct vnode *a_dvp;
870 struct vnode *a_vp;
871 struct componentname *a_cnp;
872 } */ *ap = v;
873 struct vnode *vp, *dvp;
874 struct componentname *cnp;
875 struct inode *ip, *dp;
876 int error;
877 struct ulfs_lookup_results *ulr;
878
879 vp = ap->a_vp;
880 dvp = ap->a_dvp;
881 cnp = ap->a_cnp;
882 ip = VTOI(vp);
883 dp = VTOI(dvp);
884
885 /* XXX should handle this material another way */
886 ulr = &dp->i_crap;
887 ULFS_CHECK_CRAPCOUNTER(dp);
888
889 /*
890 * No rmdir "." or of mounted directories please.
891 */
892 if (dp == ip || vp->v_mountedhere != NULL) {
893 if (dp == ip)
894 vrele(dvp);
895 else
896 vput(dvp);
897 vput(vp);
898 return (EINVAL);
899 }
900
901 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
902
903 /*
904 * Do not remove a directory that is in the process of being renamed.
905 * Verify that the directory is empty (and valid). (Rmdir ".." won't
906 * be valid since ".." will contain a reference to the current
907 * directory and thus be non-empty.)
908 */
909 error = 0;
910 if (ip->i_nlink != 2 ||
911 !ulfs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
912 error = ENOTEMPTY;
913 goto out;
914 }
915 if ((dp->i_flags & APPEND) ||
916 (ip->i_flags & (IMMUTABLE | APPEND))) {
917 error = EPERM;
918 goto out;
919 }
920 /*
921 * Delete reference to directory before purging
922 * inode. If we crash in between, the directory
923 * will be reattached to lost+found,
924 */
925 error = ulfs_dirremove(dvp, ulr, ip, cnp->cn_flags, 1);
926 if (error) {
927 goto out;
928 }
929 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
930 cache_purge(dvp);
931 /*
932 * Truncate inode. The only stuff left in the directory is "." and
933 * "..". The "." reference is inconsequential since we're quashing
934 * it.
935 */
936 dp->i_nlink--;
937 DIP_ASSIGN(dp, nlink, dp->i_nlink);
938 dp->i_flag |= IN_CHANGE;
939 ip->i_nlink--;
940 DIP_ASSIGN(ip, nlink, ip->i_nlink);
941 ip->i_flag |= IN_CHANGE;
942 error = lfs_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
943 cache_purge(vp);
944 #ifdef LFS_DIRHASH
945 if (ip->i_dirhash != NULL)
946 ulfsdirhash_free(ip);
947 #endif
948 out:
949 VN_KNOTE(vp, NOTE_DELETE);
950 vput(vp);
951 fstrans_done(dvp->v_mount);
952 vput(dvp);
953 return (error);
954 }
955
956 /*
957 * symlink -- make a symbolic link
958 */
959 int
960 ulfs_symlink(void *v)
961 {
962 struct vop_symlink_args /* {
963 struct vnode *a_dvp;
964 struct vnode **a_vpp;
965 struct componentname *a_cnp;
966 struct vattr *a_vap;
967 char *a_target;
968 } */ *ap = v;
969 struct vnode *vp, **vpp;
970 struct inode *ip;
971 int len, error;
972 struct ulfs_lookup_results *ulr;
973
974 vpp = ap->a_vpp;
975
976 /* XXX should handle this material another way */
977 ulr = &VTOI(ap->a_dvp)->i_crap;
978 ULFS_CHECK_CRAPCOUNTER(VTOI(ap->a_dvp));
979
980 fstrans_start(ap->a_dvp->v_mount, FSTRANS_SHARED);
981 error = ulfs_makeinode(LFS_IFLNK | ap->a_vap->va_mode, ap->a_dvp, ulr,
982 vpp, ap->a_cnp);
983 if (error)
984 goto out;
985 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
986 vp = *vpp;
987 len = strlen(ap->a_target);
988 ip = VTOI(vp);
989 if (len < ip->i_lfs->um_maxsymlinklen) {
990 memcpy((char *)SHORTLINK(ip), ap->a_target, len);
991 ip->i_size = len;
992 DIP_ASSIGN(ip, size, len);
993 uvm_vnp_setsize(vp, ip->i_size);
994 ip->i_flag |= IN_CHANGE | IN_UPDATE;
995 if (vp->v_mount->mnt_flag & MNT_RELATIME)
996 ip->i_flag |= IN_ACCESS;
997 } else
998 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
999 UIO_SYSSPACE, IO_NODELOCKED | IO_JOURNALLOCKED,
1000 ap->a_cnp->cn_cred, NULL, NULL);
1001 if (error)
1002 vput(vp);
1003 out:
1004 fstrans_done(ap->a_dvp->v_mount);
1005 return (error);
1006 }
1007
1008 /*
1009 * Vnode op for reading directories.
1010 *
1011 * This routine handles converting from the on-disk directory format
1012 * "struct lfs_direct" to the in-memory format "struct dirent" as well as
1013 * byte swapping the entries if necessary.
1014 */
1015 int
1016 ulfs_readdir(void *v)
1017 {
1018 struct vop_readdir_args /* {
1019 struct vnode *a_vp;
1020 struct uio *a_uio;
1021 kauth_cred_t a_cred;
1022 int *a_eofflag;
1023 off_t **a_cookies;
1024 int *ncookies;
1025 } */ *ap = v;
1026 struct vnode *vp = ap->a_vp;
1027 struct lfs_direct *cdp, *ecdp;
1028 struct dirent *ndp;
1029 char *cdbuf, *ndbuf, *endp;
1030 struct uio auio, *uio;
1031 struct iovec aiov;
1032 int error;
1033 size_t count, ccount, rcount, cdbufsz, ndbufsz;
1034 off_t off, *ccp;
1035 off_t startoff;
1036 size_t skipbytes;
1037 struct ulfsmount *ump = VFSTOULFS(vp->v_mount);
1038 struct lfs *fs = ump->um_lfs;
1039 int nswap = ULFS_MPNEEDSWAP(fs);
1040 #if BYTE_ORDER == LITTLE_ENDIAN
1041 int needswap = fs->um_maxsymlinklen <= 0 && nswap == 0;
1042 #else
1043 int needswap = fs->um_maxsymlinklen <= 0 && nswap != 0;
1044 #endif
1045 uio = ap->a_uio;
1046 count = uio->uio_resid;
1047 rcount = count - ((uio->uio_offset + count) & (fs->um_dirblksiz - 1));
1048
1049 if (rcount < _DIRENT_MINSIZE(cdp) || count < _DIRENT_MINSIZE(ndp))
1050 return EINVAL;
1051
1052 startoff = uio->uio_offset & ~(fs->um_dirblksiz - 1);
1053 skipbytes = uio->uio_offset - startoff;
1054 rcount += skipbytes;
1055
1056 auio.uio_iov = &aiov;
1057 auio.uio_iovcnt = 1;
1058 auio.uio_offset = startoff;
1059 auio.uio_resid = rcount;
1060 UIO_SETUP_SYSSPACE(&auio);
1061 auio.uio_rw = UIO_READ;
1062 cdbufsz = rcount;
1063 cdbuf = kmem_alloc(cdbufsz, KM_SLEEP);
1064 aiov.iov_base = cdbuf;
1065 aiov.iov_len = rcount;
1066 error = VOP_READ(vp, &auio, 0, ap->a_cred);
1067 if (error != 0) {
1068 kmem_free(cdbuf, cdbufsz);
1069 return error;
1070 }
1071
1072 rcount -= auio.uio_resid;
1073
1074 cdp = (struct lfs_direct *)(void *)cdbuf;
1075 ecdp = (struct lfs_direct *)(void *)&cdbuf[rcount];
1076
1077 ndbufsz = count;
1078 ndbuf = kmem_alloc(ndbufsz, KM_SLEEP);
1079 ndp = (struct dirent *)(void *)ndbuf;
1080 endp = &ndbuf[count];
1081
1082 off = uio->uio_offset;
1083 if (ap->a_cookies) {
1084 ccount = rcount / _DIRENT_RECLEN(cdp, 1);
1085 ccp = *(ap->a_cookies) = malloc(ccount * sizeof(*ccp),
1086 M_TEMP, M_WAITOK);
1087 } else {
1088 /* XXX: GCC */
1089 ccount = 0;
1090 ccp = NULL;
1091 }
1092
1093 while (cdp < ecdp) {
1094 cdp->d_reclen = ulfs_rw16(cdp->d_reclen, nswap);
1095 if (skipbytes > 0) {
1096 if (cdp->d_reclen <= skipbytes) {
1097 skipbytes -= cdp->d_reclen;
1098 cdp = _DIRENT_NEXT(cdp);
1099 continue;
1100 }
1101 /*
1102 * invalid cookie.
1103 */
1104 error = EINVAL;
1105 goto out;
1106 }
1107 if (cdp->d_reclen == 0) {
1108 struct dirent *ondp = ndp;
1109 ndp->d_reclen = _DIRENT_MINSIZE(ndp);
1110 ndp = _DIRENT_NEXT(ndp);
1111 ondp->d_reclen = 0;
1112 cdp = ecdp;
1113 break;
1114 }
1115 if (needswap) {
1116 ndp->d_type = cdp->d_namlen;
1117 ndp->d_namlen = cdp->d_type;
1118 } else {
1119 ndp->d_type = cdp->d_type;
1120 ndp->d_namlen = cdp->d_namlen;
1121 }
1122 ndp->d_reclen = _DIRENT_RECLEN(ndp, ndp->d_namlen);
1123 if ((char *)(void *)ndp + ndp->d_reclen +
1124 _DIRENT_MINSIZE(ndp) > endp)
1125 break;
1126 ndp->d_fileno = ulfs_rw32(cdp->d_ino, nswap);
1127 (void)memcpy(ndp->d_name, cdp->d_name, ndp->d_namlen);
1128 memset(&ndp->d_name[ndp->d_namlen], 0,
1129 ndp->d_reclen - _DIRENT_NAMEOFF(ndp) - ndp->d_namlen);
1130 off += cdp->d_reclen;
1131 if (ap->a_cookies) {
1132 KASSERT(ccp - *(ap->a_cookies) < ccount);
1133 *(ccp++) = off;
1134 }
1135 ndp = _DIRENT_NEXT(ndp);
1136 cdp = _DIRENT_NEXT(cdp);
1137 }
1138
1139 count = ((char *)(void *)ndp - ndbuf);
1140 error = uiomove(ndbuf, count, uio);
1141 out:
1142 if (ap->a_cookies) {
1143 if (error) {
1144 free(*(ap->a_cookies), M_TEMP);
1145 *(ap->a_cookies) = NULL;
1146 *(ap->a_ncookies) = 0;
1147 } else {
1148 *ap->a_ncookies = ccp - *(ap->a_cookies);
1149 }
1150 }
1151 uio->uio_offset = off;
1152 kmem_free(ndbuf, ndbufsz);
1153 kmem_free(cdbuf, cdbufsz);
1154 *ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset;
1155 return error;
1156 }
1157
1158 /*
1159 * Return target name of a symbolic link
1160 */
1161 int
1162 ulfs_readlink(void *v)
1163 {
1164 struct vop_readlink_args /* {
1165 struct vnode *a_vp;
1166 struct uio *a_uio;
1167 kauth_cred_t a_cred;
1168 } */ *ap = v;
1169 struct vnode *vp = ap->a_vp;
1170 struct inode *ip = VTOI(vp);
1171 struct ulfsmount *ump = VFSTOULFS(vp->v_mount);
1172 struct lfs *fs = ump->um_lfs;
1173 int isize;
1174
1175 isize = ip->i_size;
1176 if (isize < fs->um_maxsymlinklen ||
1177 (fs->um_maxsymlinklen == 0 && DIP(ip, blocks) == 0)) {
1178 uiomove((char *)SHORTLINK(ip), isize, ap->a_uio);
1179 return (0);
1180 }
1181 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1182 }
1183
1184 /*
1185 * Print out the contents of an inode.
1186 */
1187 int
1188 ulfs_print(void *v)
1189 {
1190 struct vop_print_args /* {
1191 struct vnode *a_vp;
1192 } */ *ap = v;
1193 struct vnode *vp;
1194 struct inode *ip;
1195
1196 vp = ap->a_vp;
1197 ip = VTOI(vp);
1198 printf("tag VT_ULFS, ino %llu, on dev %llu, %llu",
1199 (unsigned long long)ip->i_number,
1200 (unsigned long long)major(ip->i_dev),
1201 (unsigned long long)minor(ip->i_dev));
1202 printf(" flags 0x%x, nlink %d\n",
1203 ip->i_flag, ip->i_nlink);
1204 printf("\tmode 0%o, owner %d, group %d, size %qd",
1205 ip->i_mode, ip->i_uid, ip->i_gid,
1206 (long long)ip->i_size);
1207 if (vp->v_type == VFIFO)
1208 VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
1209 printf("\n");
1210 return (0);
1211 }
1212
1213 /*
1214 * Read wrapper for special devices.
1215 */
1216 int
1217 ulfsspec_read(void *v)
1218 {
1219 struct vop_read_args /* {
1220 struct vnode *a_vp;
1221 struct uio *a_uio;
1222 int a_ioflag;
1223 kauth_cred_t a_cred;
1224 } */ *ap = v;
1225
1226 /*
1227 * Set access flag.
1228 */
1229 if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)
1230 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1231 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap));
1232 }
1233
1234 /*
1235 * Write wrapper for special devices.
1236 */
1237 int
1238 ulfsspec_write(void *v)
1239 {
1240 struct vop_write_args /* {
1241 struct vnode *a_vp;
1242 struct uio *a_uio;
1243 int a_ioflag;
1244 kauth_cred_t a_cred;
1245 } */ *ap = v;
1246
1247 /*
1248 * Set update and change flags.
1249 */
1250 if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)
1251 VTOI(ap->a_vp)->i_flag |= IN_MODIFY;
1252 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap));
1253 }
1254
1255 /*
1256 * Read wrapper for fifo's
1257 */
1258 int
1259 ulfsfifo_read(void *v)
1260 {
1261 struct vop_read_args /* {
1262 struct vnode *a_vp;
1263 struct uio *a_uio;
1264 int a_ioflag;
1265 kauth_cred_t a_cred;
1266 } */ *ap = v;
1267
1268 /*
1269 * Set access flag.
1270 */
1271 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1272 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap));
1273 }
1274
1275 /*
1276 * Write wrapper for fifo's.
1277 */
1278 int
1279 ulfsfifo_write(void *v)
1280 {
1281 struct vop_write_args /* {
1282 struct vnode *a_vp;
1283 struct uio *a_uio;
1284 int a_ioflag;
1285 kauth_cred_t a_cred;
1286 } */ *ap = v;
1287
1288 /*
1289 * Set update and change flags.
1290 */
1291 VTOI(ap->a_vp)->i_flag |= IN_MODIFY;
1292 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap));
1293 }
1294
1295 /*
1296 * Return POSIX pathconf information applicable to ulfs filesystems.
1297 */
1298 int
1299 ulfs_pathconf(void *v)
1300 {
1301 struct vop_pathconf_args /* {
1302 struct vnode *a_vp;
1303 int a_name;
1304 register_t *a_retval;
1305 } */ *ap = v;
1306
1307 switch (ap->a_name) {
1308 case _PC_LINK_MAX:
1309 *ap->a_retval = LINK_MAX;
1310 return (0);
1311 case _PC_NAME_MAX:
1312 *ap->a_retval = LFS_MAXNAMLEN;
1313 return (0);
1314 case _PC_PATH_MAX:
1315 *ap->a_retval = PATH_MAX;
1316 return (0);
1317 case _PC_PIPE_BUF:
1318 *ap->a_retval = PIPE_BUF;
1319 return (0);
1320 case _PC_CHOWN_RESTRICTED:
1321 *ap->a_retval = 1;
1322 return (0);
1323 case _PC_NO_TRUNC:
1324 *ap->a_retval = 1;
1325 return (0);
1326 case _PC_SYNC_IO:
1327 *ap->a_retval = 1;
1328 return (0);
1329 case _PC_FILESIZEBITS:
1330 *ap->a_retval = 42;
1331 return (0);
1332 case _PC_SYMLINK_MAX:
1333 *ap->a_retval = MAXPATHLEN;
1334 return (0);
1335 case _PC_2_SYMLINKS:
1336 *ap->a_retval = 1;
1337 return (0);
1338 default:
1339 return (EINVAL);
1340 }
1341 /* NOTREACHED */
1342 }
1343
1344 /*
1345 * Advisory record locking support
1346 */
1347 int
1348 ulfs_advlock(void *v)
1349 {
1350 struct vop_advlock_args /* {
1351 struct vnode *a_vp;
1352 void * a_id;
1353 int a_op;
1354 struct flock *a_fl;
1355 int a_flags;
1356 } */ *ap = v;
1357 struct inode *ip;
1358
1359 ip = VTOI(ap->a_vp);
1360 return lf_advlock(ap, &ip->i_lockf, ip->i_size);
1361 }
1362
1363 /*
1364 * Initialize the vnode associated with a new inode, handle aliased
1365 * vnodes.
1366 */
1367 void
1368 ulfs_vinit(struct mount *mntp, int (**specops)(void *), int (**fifoops)(void *),
1369 struct vnode **vpp)
1370 {
1371 struct timeval tv;
1372 struct inode *ip;
1373 struct vnode *vp;
1374 dev_t rdev;
1375 struct ulfsmount *ump;
1376
1377 vp = *vpp;
1378 ip = VTOI(vp);
1379 switch(vp->v_type = IFTOVT(ip->i_mode)) {
1380 case VCHR:
1381 case VBLK:
1382 vp->v_op = specops;
1383 ump = ip->i_ump;
1384 if (ump->um_fstype == ULFS1)
1385 rdev = (dev_t)ulfs_rw32(ip->i_ffs1_rdev,
1386 ULFS_MPNEEDSWAP(ump->um_lfs));
1387 else
1388 rdev = (dev_t)ulfs_rw64(ip->i_ffs2_rdev,
1389 ULFS_MPNEEDSWAP(ump->um_lfs));
1390 spec_node_init(vp, rdev);
1391 break;
1392 case VFIFO:
1393 vp->v_op = fifoops;
1394 break;
1395 case VNON:
1396 case VBAD:
1397 case VSOCK:
1398 case VLNK:
1399 case VDIR:
1400 case VREG:
1401 break;
1402 }
1403 if (ip->i_number == ULFS_ROOTINO)
1404 vp->v_vflag |= VV_ROOT;
1405 /*
1406 * Initialize modrev times
1407 */
1408 getmicrouptime(&tv);
1409 ip->i_modrev = (uint64_t)(uint)tv.tv_sec << 32
1410 | tv.tv_usec * 4294u;
1411 *vpp = vp;
1412 }
1413
1414 /*
1415 * Allocate a new inode.
1416 */
1417 int
1418 ulfs_makeinode(int mode, struct vnode *dvp, const struct ulfs_lookup_results *ulr,
1419 struct vnode **vpp, struct componentname *cnp)
1420 {
1421 struct inode *ip, *pdir;
1422 struct lfs_direct *newdir;
1423 struct vnode *tvp;
1424 int error;
1425
1426 pdir = VTOI(dvp);
1427
1428 if ((mode & LFS_IFMT) == 0)
1429 mode |= LFS_IFREG;
1430
1431 if ((error = lfs_valloc(dvp, mode, cnp->cn_cred, vpp)) != 0) {
1432 vput(dvp);
1433 return (error);
1434 }
1435 tvp = *vpp;
1436 ip = VTOI(tvp);
1437 ip->i_gid = pdir->i_gid;
1438 DIP_ASSIGN(ip, gid, ip->i_gid);
1439 ip->i_uid = kauth_cred_geteuid(cnp->cn_cred);
1440 DIP_ASSIGN(ip, uid, ip->i_uid);
1441 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
1442 if ((error = lfs_chkiq(ip, 1, cnp->cn_cred, 0))) {
1443 lfs_vfree(tvp, ip->i_number, mode);
1444 vput(tvp);
1445 vput(dvp);
1446 return (error);
1447 }
1448 #endif
1449 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1450 ip->i_mode = mode;
1451 DIP_ASSIGN(ip, mode, mode);
1452 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
1453 ip->i_nlink = 1;
1454 DIP_ASSIGN(ip, nlink, 1);
1455
1456 /* Authorize setting SGID if needed. */
1457 if (ip->i_mode & ISGID) {
1458 error = kauth_authorize_vnode(cnp->cn_cred, KAUTH_VNODE_WRITE_SECURITY,
1459 tvp, NULL, genfs_can_chmod(tvp->v_type, cnp->cn_cred, ip->i_uid,
1460 ip->i_gid, mode));
1461 if (error) {
1462 ip->i_mode &= ~ISGID;
1463 DIP_ASSIGN(ip, mode, ip->i_mode);
1464 }
1465 }
1466
1467 if (cnp->cn_flags & ISWHITEOUT) {
1468 ip->i_flags |= UF_OPAQUE;
1469 DIP_ASSIGN(ip, flags, ip->i_flags);
1470 }
1471
1472 /*
1473 * Make sure inode goes to disk before directory entry.
1474 */
1475 if ((error = lfs_update(tvp, NULL, NULL, UPDATE_DIROP)) != 0)
1476 goto bad;
1477 newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
1478 ulfs_makedirentry(ip, cnp, newdir);
1479 error = ulfs_direnter(dvp, ulr, tvp, newdir, cnp, NULL);
1480 pool_cache_put(ulfs_direct_cache, newdir);
1481 if (error)
1482 goto bad;
1483 vput(dvp);
1484 *vpp = tvp;
1485 return (0);
1486
1487 bad:
1488 /*
1489 * Write error occurred trying to update the inode
1490 * or the directory so must deallocate the inode.
1491 */
1492 ip->i_nlink = 0;
1493 DIP_ASSIGN(ip, nlink, 0);
1494 ip->i_flag |= IN_CHANGE;
1495 /* If IN_ADIROP, account for it */
1496 lfs_unmark_vnode(tvp);
1497 tvp->v_type = VNON; /* explodes later if VBLK */
1498 vput(tvp);
1499 vput(dvp);
1500 return (error);
1501 }
1502
1503 /*
1504 * Allocate len bytes at offset off.
1505 */
1506 int
1507 ulfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
1508 kauth_cred_t cred)
1509 {
1510 struct inode *ip = VTOI(vp);
1511 int error, delta, bshift, bsize;
1512 UVMHIST_FUNC("ulfs_gop_alloc"); UVMHIST_CALLED(ubchist);
1513
1514 error = 0;
1515 bshift = vp->v_mount->mnt_fs_bshift;
1516 bsize = 1 << bshift;
1517
1518 delta = off & (bsize - 1);
1519 off -= delta;
1520 len += delta;
1521
1522 while (len > 0) {
1523 bsize = MIN(bsize, len);
1524
1525 error = lfs_balloc(vp, off, bsize, cred, flags, NULL);
1526 if (error) {
1527 goto out;
1528 }
1529
1530 /*
1531 * increase file size now, lfs_balloc() requires that
1532 * EOF be up-to-date before each call.
1533 */
1534
1535 if (ip->i_size < off + bsize) {
1536 UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x",
1537 vp, ip->i_size, off + bsize, 0);
1538 ip->i_size = off + bsize;
1539 DIP_ASSIGN(ip, size, ip->i_size);
1540 }
1541
1542 off += bsize;
1543 len -= bsize;
1544 }
1545
1546 out:
1547 return error;
1548 }
1549
1550 void
1551 ulfs_gop_markupdate(struct vnode *vp, int flags)
1552 {
1553 u_int32_t mask = 0;
1554
1555 if ((flags & GOP_UPDATE_ACCESSED) != 0) {
1556 mask = IN_ACCESS;
1557 }
1558 if ((flags & GOP_UPDATE_MODIFIED) != 0) {
1559 if (vp->v_type == VREG) {
1560 mask |= IN_CHANGE | IN_UPDATE;
1561 } else {
1562 mask |= IN_MODIFY;
1563 }
1564 }
1565 if (mask) {
1566 struct inode *ip = VTOI(vp);
1567
1568 ip->i_flag |= mask;
1569 }
1570 }
1571