ulfs_vnops.c revision 1.53 1 /* $NetBSD: ulfs_vnops.c,v 1.53 2020/05/16 18:31:53 christos Exp $ */
2 /* from NetBSD: ufs_vnops.c,v 1.232 2016/05/19 18:32:03 riastradh 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.53 2020/05/16 18:31:53 christos 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
94 #include <miscfs/specfs/specdev.h>
95 #include <miscfs/fifofs/fifo.h>
96 #include <miscfs/genfs/genfs.h>
97
98 #include <ufs/lfs/lfs_extern.h>
99 #include <ufs/lfs/lfs.h>
100 #include <ufs/lfs/lfs_accessors.h>
101
102 #include <ufs/lfs/ulfs_inode.h>
103 #include <ufs/lfs/ulfsmount.h>
104 #include <ufs/lfs/ulfs_bswap.h>
105 #include <ufs/lfs/ulfs_extern.h>
106 #ifdef LFS_DIRHASH
107 #include <ufs/lfs/ulfs_dirhash.h>
108 #endif
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 * Open called.
118 *
119 * Nothing to do.
120 */
121 /* ARGSUSED */
122 int
123 ulfs_open(void *v)
124 {
125 struct vop_open_args /* {
126 struct vnode *a_vp;
127 int a_mode;
128 kauth_cred_t a_cred;
129 } */ *ap = v;
130
131 KASSERT(VOP_ISLOCKED(ap->a_vp) == LK_EXCLUSIVE);
132
133 /*
134 * Files marked append-only must be opened for appending.
135 */
136 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
137 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
138 return (EPERM);
139 return (0);
140 }
141
142 static int
143 ulfs_check_possible(struct vnode *vp, struct inode *ip, accmode_t accmode,
144 kauth_cred_t cred)
145 {
146 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
147 int error;
148 #endif
149
150 /*
151 * Disallow write attempts on read-only file systems;
152 * unless the file is a socket, fifo, or a block or
153 * character device resident on the file system.
154 */
155 if (accmode & VWRITE) {
156 switch (vp->v_type) {
157 case VDIR:
158 case VLNK:
159 case VREG:
160 if (vp->v_mount->mnt_flag & MNT_RDONLY)
161 return (EROFS);
162 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
163 error = lfs_chkdq(ip, 0, cred, 0);
164 if (error != 0)
165 return error;
166 #endif
167 break;
168 case VBAD:
169 case VBLK:
170 case VCHR:
171 case VSOCK:
172 case VFIFO:
173 case VNON:
174 default:
175 break;
176 }
177 }
178
179 /* If it is a snapshot, nobody gets access to it. */
180 if ((ip->i_flags & SF_SNAPSHOT))
181 return (EPERM);
182 /* If immutable bit set, nobody gets to write it. */
183 if ((accmode & VWRITE) && (ip->i_flags & IMMUTABLE))
184 return (EPERM);
185
186 return 0;
187 }
188
189 static int
190 ulfs_check_permitted(struct vnode *vp, struct inode *ip, accmode_t accmode,
191 kauth_cred_t cred)
192 {
193
194 return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(accmode,
195 vp->v_type, ip->i_mode & ALLPERMS), vp, NULL, genfs_can_access(
196 vp, cred, ip->i_uid, ip->i_gid, ip->i_mode & ALLPERMS,
197 NULL, accmode));
198 }
199
200 int
201 ulfs_access(void *v)
202 {
203 struct vop_access_args /* {
204 struct vnode *a_vp;
205 accmode_t a_accmode;
206 kauth_cred_t a_cred;
207 } */ *ap = v;
208 struct vnode *vp;
209 struct inode *ip;
210 accmode_t accmode;
211 int error;
212
213 vp = ap->a_vp;
214 accmode = ap->a_accmode;
215
216 KASSERT(VOP_ISLOCKED(vp));
217
218 ip = VTOI(vp);
219
220 error = ulfs_check_possible(vp, ip, accmode, ap->a_cred);
221 if (error)
222 return error;
223
224 error = ulfs_check_permitted(vp, ip, accmode, ap->a_cred);
225
226 return error;
227 }
228
229 /*
230 * Set attribute vnode op. called from several syscalls
231 */
232 int
233 ulfs_setattr(void *v)
234 {
235 struct vop_setattr_args /* {
236 struct vnode *a_vp;
237 struct vattr *a_vap;
238 kauth_cred_t a_cred;
239 } */ *ap = v;
240 struct vattr *vap;
241 struct vnode *vp;
242 struct inode *ip;
243 struct lfs *fs;
244 kauth_cred_t cred;
245 struct lwp *l;
246 int error;
247 kauth_action_t action;
248 bool changing_sysflags;
249
250 vap = ap->a_vap;
251 vp = ap->a_vp;
252 cred = ap->a_cred;
253 l = curlwp;
254 action = KAUTH_VNODE_WRITE_FLAGS;
255 changing_sysflags = false;
256
257 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
258
259 ip = VTOI(vp);
260 fs = ip->i_lfs;
261
262 /*
263 * Check for unsettable attributes.
264 */
265 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
266 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
267 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
268 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
269 return (EINVAL);
270 }
271
272 if (vap->va_flags != VNOVAL) {
273 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
274 error = EROFS;
275 goto out;
276 }
277
278 /* Snapshot flag cannot be set or cleared */
279 if ((vap->va_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) !=
280 (ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))) {
281 error = EPERM;
282 goto out;
283 }
284
285 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
286 action |= KAUTH_VNODE_HAS_SYSFLAGS;
287 }
288
289 if ((vap->va_flags & SF_SETTABLE) !=
290 (ip->i_flags & SF_SETTABLE)) {
291 action |= KAUTH_VNODE_WRITE_SYSFLAGS;
292 changing_sysflags = true;
293 }
294
295 error = kauth_authorize_vnode(cred, action, vp, NULL,
296 genfs_can_chflags(vp, cred, ip->i_uid,
297 changing_sysflags));
298 if (error)
299 goto out;
300
301 if (changing_sysflags) {
302 ip->i_flags = vap->va_flags;
303 DIP_ASSIGN(ip, flags, ip->i_flags);
304 } else {
305 ip->i_flags &= SF_SETTABLE;
306 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
307 DIP_ASSIGN(ip, flags, ip->i_flags);
308 }
309 ip->i_state |= IN_CHANGE;
310 if (vap->va_flags & (IMMUTABLE | APPEND)) {
311 error = 0;
312 goto out;
313 }
314 }
315 if (ip->i_flags & (IMMUTABLE | APPEND)) {
316 error = EPERM;
317 goto out;
318 }
319 /*
320 * Go through the fields and update iff not VNOVAL.
321 */
322 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
323 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
324 error = EROFS;
325 goto out;
326 }
327 error = ulfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
328 if (error)
329 goto out;
330 }
331 if (vap->va_size != VNOVAL) {
332 /*
333 * Disallow write attempts on read-only file systems;
334 * unless the file is a socket, fifo, or a block or
335 * character device resident on the file system.
336 */
337 switch (vp->v_type) {
338 case VDIR:
339 error = EISDIR;
340 goto out;
341 case VCHR:
342 case VBLK:
343 case VFIFO:
344 break;
345 case VREG:
346 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
347 error = EROFS;
348 goto out;
349 }
350 if ((ip->i_flags & SF_SNAPSHOT) != 0) {
351 error = EPERM;
352 goto out;
353 }
354 error = lfs_truncate(vp, vap->va_size, 0, cred);
355 if (error)
356 goto out;
357 break;
358 default:
359 error = EOPNOTSUPP;
360 goto out;
361 }
362 }
363 ip = VTOI(vp);
364 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
365 vap->va_birthtime.tv_sec != VNOVAL) {
366 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
367 error = EROFS;
368 goto out;
369 }
370 if ((ip->i_flags & SF_SNAPSHOT) != 0) {
371 error = EPERM;
372 goto out;
373 }
374 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
375 NULL, genfs_can_chtimes(vp, cred, ip->i_uid,
376 vap->va_vaflags));
377 if (error)
378 goto out;
379 if (vap->va_atime.tv_sec != VNOVAL)
380 if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
381 ip->i_state |= IN_ACCESS;
382 if (vap->va_mtime.tv_sec != VNOVAL) {
383 ip->i_state |= IN_CHANGE | IN_UPDATE;
384 if (vp->v_mount->mnt_flag & MNT_RELATIME)
385 ip->i_state |= IN_ACCESS;
386 }
387 if (vap->va_birthtime.tv_sec != VNOVAL) {
388 lfs_dino_setbirthtime(fs, ip->i_din,
389 &vap->va_birthtime);
390 }
391 error = lfs_update(vp, &vap->va_atime, &vap->va_mtime, 0);
392 if (error)
393 goto out;
394 }
395 error = 0;
396 if (vap->va_mode != (mode_t)VNOVAL) {
397 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
398 error = EROFS;
399 goto out;
400 }
401 if ((ip->i_flags & SF_SNAPSHOT) != 0 &&
402 (vap->va_mode & (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP |
403 S_IXOTH | S_IWOTH))) {
404 error = EPERM;
405 goto out;
406 }
407 error = ulfs_chmod(vp, (int)vap->va_mode, cred, l);
408 }
409 VN_KNOTE(vp, NOTE_ATTRIB);
410 out:
411 return (error);
412 }
413
414 /*
415 * Change the mode on a file.
416 * Inode must be locked before calling.
417 */
418 static int
419 ulfs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct lwp *l)
420 {
421 struct inode *ip;
422 int error;
423
424 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
425
426 ip = VTOI(vp);
427
428 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
429 NULL, genfs_can_chmod(vp, cred, ip->i_uid, ip->i_gid, mode));
430 if (error)
431 return (error);
432
433 ip->i_mode &= ~ALLPERMS;
434 ip->i_mode |= (mode & ALLPERMS);
435 ip->i_state |= IN_CHANGE;
436 DIP_ASSIGN(ip, mode, ip->i_mode);
437 return (0);
438 }
439
440 /*
441 * Perform chown operation on inode ip;
442 * inode must be locked prior to call.
443 */
444 static int
445 ulfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
446 struct lwp *l)
447 {
448 struct inode *ip;
449 int error = 0;
450 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
451 uid_t ouid;
452 gid_t ogid;
453 int64_t change;
454 #endif
455
456 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
457
458 ip = VTOI(vp);
459 error = 0;
460
461 if (uid == (uid_t)VNOVAL)
462 uid = ip->i_uid;
463 if (gid == (gid_t)VNOVAL)
464 gid = ip->i_gid;
465
466 error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
467 NULL, genfs_can_chown(vp, cred, ip->i_uid, ip->i_gid, uid, gid));
468 if (error)
469 return (error);
470
471 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
472 ogid = ip->i_gid;
473 ouid = ip->i_uid;
474 change = DIP(ip, blocks);
475 (void) lfs_chkdq(ip, -change, cred, 0);
476 (void) lfs_chkiq(ip, -1, cred, 0);
477 #endif
478 ip->i_gid = gid;
479 DIP_ASSIGN(ip, gid, gid);
480 ip->i_uid = uid;
481 DIP_ASSIGN(ip, uid, uid);
482 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
483 if ((error = lfs_chkdq(ip, change, cred, 0)) == 0) {
484 if ((error = lfs_chkiq(ip, 1, cred, 0)) == 0)
485 goto good;
486 else
487 (void) lfs_chkdq(ip, -change, cred, FORCE);
488 }
489 ip->i_gid = ogid;
490 DIP_ASSIGN(ip, gid, ogid);
491 ip->i_uid = ouid;
492 DIP_ASSIGN(ip, uid, ouid);
493 (void) lfs_chkdq(ip, change, cred, FORCE);
494 (void) lfs_chkiq(ip, 1, cred, FORCE);
495 return (error);
496 good:
497 #endif /* LFS_QUOTA || LFS_QUOTA2 */
498 ip->i_state |= IN_CHANGE;
499 return (0);
500 }
501
502 int
503 ulfs_remove(void *v)
504 {
505 struct vop_remove_v2_args /* {
506 struct vnode *a_dvp;
507 struct vnode *a_vp;
508 struct componentname *a_cnp;
509 } */ *ap = v;
510 struct vnode *vp, *dvp;
511 struct inode *ip;
512 int error;
513 struct ulfs_lookup_results *ulr;
514
515 dvp = ap->a_dvp;
516 vp = ap->a_vp;
517
518 KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
519 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
520 KASSERT(dvp->v_mount == vp->v_mount);
521
522 ip = VTOI(vp);
523
524 /* XXX should handle this material another way */
525 ulr = &VTOI(dvp)->i_crap;
526 ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
527
528 if (vp->v_type == VDIR || (ip->i_flags & (IMMUTABLE | APPEND)) ||
529 (VTOI(dvp)->i_flags & APPEND))
530 error = EPERM;
531 else {
532 error = ulfs_dirremove(dvp, ulr,
533 ip, ap->a_cnp->cn_flags, 0);
534 }
535 VN_KNOTE(vp, NOTE_DELETE);
536 VN_KNOTE(dvp, NOTE_WRITE);
537 if (dvp == vp)
538 vrele(vp);
539 else
540 vput(vp);
541 return (error);
542 }
543
544 /*
545 * ulfs_link: create hard link.
546 */
547 int
548 ulfs_link(void *v)
549 {
550 struct vop_link_v2_args /* {
551 struct vnode *a_dvp;
552 struct vnode *a_vp;
553 struct componentname *a_cnp;
554 } */ *ap = v;
555 struct vnode *dvp = ap->a_dvp;
556 struct vnode *vp = ap->a_vp;
557 struct componentname *cnp = ap->a_cnp;
558 struct inode *ip;
559 int error;
560 struct ulfs_lookup_results *ulr;
561
562 KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
563 KASSERT(dvp != vp);
564 KASSERT(vp->v_type != VDIR);
565
566 /* XXX should handle this material another way */
567 ulr = &VTOI(dvp)->i_crap;
568 ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
569
570 error = vn_lock(vp, LK_EXCLUSIVE);
571 if (error) {
572 VOP_ABORTOP(dvp, cnp);
573 goto out2;
574 }
575 if (vp->v_mount != dvp->v_mount) {
576 error = ENOENT;
577 VOP_ABORTOP(dvp, cnp);
578 goto out2;
579 }
580 ip = VTOI(vp);
581 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
582 VOP_ABORTOP(dvp, cnp);
583 error = EMLINK;
584 goto out1;
585 }
586 if (ip->i_flags & (IMMUTABLE | APPEND)) {
587 VOP_ABORTOP(dvp, cnp);
588 error = EPERM;
589 goto out1;
590 }
591 ip->i_nlink++;
592 DIP_ASSIGN(ip, nlink, ip->i_nlink);
593 ip->i_state |= IN_CHANGE;
594 error = lfs_update(vp, NULL, NULL, UPDATE_DIROP);
595 if (!error) {
596 error = ulfs_direnter(dvp, ulr, vp,
597 cnp, ip->i_number, LFS_IFTODT(ip->i_mode), NULL);
598 }
599 if (error) {
600 ip->i_nlink--;
601 DIP_ASSIGN(ip, nlink, ip->i_nlink);
602 ip->i_state |= IN_CHANGE;
603 }
604 out1:
605 VOP_UNLOCK(vp);
606 out2:
607 VN_KNOTE(vp, NOTE_LINK);
608 VN_KNOTE(dvp, NOTE_WRITE);
609 return (error);
610 }
611
612 /*
613 * whiteout vnode call
614 */
615 int
616 ulfs_whiteout(void *v)
617 {
618 struct vop_whiteout_args /* {
619 struct vnode *a_dvp;
620 struct componentname *a_cnp;
621 int a_flags;
622 } */ *ap = v;
623 struct vnode *dvp = ap->a_dvp;
624 struct componentname *cnp = ap->a_cnp;
625 int error;
626 struct ulfsmount *ump = VFSTOULFS(dvp->v_mount);
627 struct lfs *fs = ump->um_lfs;
628 struct ulfs_lookup_results *ulr;
629
630 KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
631
632 /* XXX should handle this material another way */
633 ulr = &VTOI(dvp)->i_crap;
634 ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
635
636 error = 0;
637 switch (ap->a_flags) {
638 case LOOKUP:
639 /* 4.4 format directories support whiteout operations */
640 if (fs->um_maxsymlinklen > 0)
641 return (0);
642 return (EOPNOTSUPP);
643
644 case CREATE:
645 /* create a new directory whiteout */
646 KASSERTMSG((fs->um_maxsymlinklen > 0),
647 "ulfs_whiteout: old format filesystem");
648
649 error = ulfs_direnter(dvp, ulr, NULL,
650 cnp, ULFS_WINO, LFS_DT_WHT, NULL);
651 break;
652
653 case DELETE:
654 /* remove an existing directory whiteout */
655 KASSERTMSG((fs->um_maxsymlinklen > 0),
656 "ulfs_whiteout: old format filesystem");
657
658 cnp->cn_flags &= ~DOWHITEOUT;
659 error = ulfs_dirremove(dvp, ulr, NULL, cnp->cn_flags, 0);
660 break;
661 default:
662 panic("ulfs_whiteout: unknown op");
663 /* NOTREACHED */
664 }
665 return (error);
666 }
667
668 int
669 ulfs_rmdir(void *v)
670 {
671 struct vop_rmdir_v2_args /* {
672 struct vnode *a_dvp;
673 struct vnode *a_vp;
674 struct componentname *a_cnp;
675 } */ *ap = v;
676 struct vnode *vp, *dvp;
677 struct componentname *cnp;
678 struct inode *ip, *dp;
679 int error;
680 struct ulfs_lookup_results *ulr;
681
682 dvp = ap->a_dvp;
683 vp = ap->a_vp;
684 cnp = ap->a_cnp;
685
686 KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
687 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
688
689 dp = VTOI(dvp);
690 ip = VTOI(vp);
691
692 /* XXX should handle this material another way */
693 ulr = &dp->i_crap;
694 ULFS_CHECK_CRAPCOUNTER(dp);
695
696 /*
697 * No rmdir "." or of mounted directories please.
698 */
699 if (dp == ip || vp->v_mountedhere != NULL) {
700 if (dp == ip)
701 vrele(vp);
702 else
703 vput(vp);
704 return (EINVAL);
705 }
706
707 /*
708 * Do not remove a directory that is in the process of being renamed.
709 * Verify that the directory is empty (and valid). (Rmdir ".." won't
710 * be valid since ".." will contain a reference to the current
711 * directory and thus be non-empty.)
712 */
713 error = 0;
714 if (ip->i_nlink != 2 ||
715 !ulfs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
716 error = ENOTEMPTY;
717 goto out;
718 }
719 if ((dp->i_flags & APPEND) ||
720 (ip->i_flags & (IMMUTABLE | APPEND))) {
721 error = EPERM;
722 goto out;
723 }
724 /*
725 * Delete reference to directory before purging
726 * inode. If we crash in between, the directory
727 * will be reattached to lost+found,
728 */
729 error = ulfs_dirremove(dvp, ulr, ip, cnp->cn_flags, 1);
730 if (error) {
731 goto out;
732 }
733 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
734 cache_purge(dvp);
735 /*
736 * Truncate inode. The only stuff left in the directory is "." and
737 * "..". The "." reference is inconsequential since we're quashing
738 * it.
739 */
740 dp->i_nlink--;
741 DIP_ASSIGN(dp, nlink, dp->i_nlink);
742 dp->i_state |= IN_CHANGE;
743 ip->i_nlink--;
744 DIP_ASSIGN(ip, nlink, ip->i_nlink);
745 ip->i_state |= IN_CHANGE;
746 error = lfs_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
747 cache_purge(vp);
748 #ifdef LFS_DIRHASH
749 if (ip->i_dirhash != NULL)
750 ulfsdirhash_free(ip);
751 #endif
752 out:
753 VN_KNOTE(vp, NOTE_DELETE);
754 vput(vp);
755 return (error);
756 }
757
758 /*
759 * Vnode op for reading directories.
760 *
761 * This routine handles converting from the on-disk directory format
762 * "struct lfs_direct" to the in-memory format "struct dirent" as well as
763 * byte swapping the entries if necessary.
764 */
765 int
766 ulfs_readdir(void *v)
767 {
768 struct vop_readdir_args /* {
769 struct vnode *a_vp;
770 struct uio *a_uio;
771 kauth_cred_t a_cred;
772 int *a_eofflag;
773 off_t **a_cookies;
774 int *a_ncookies;
775 } */ *ap = v;
776
777 /* vnode and fs */
778 struct vnode *vp = ap->a_vp;
779 struct ulfsmount *ump = VFSTOULFS(vp->v_mount);
780 struct lfs *fs = ump->um_lfs;
781 /* caller's buffer */
782 struct uio *calleruio = ap->a_uio;
783 off_t startoffset, endoffset;
784 size_t callerbytes;
785 off_t curoffset;
786 /* dirent production buffer */
787 char *direntbuf;
788 size_t direntbufmax;
789 struct dirent *dirent, *stopdirent;
790 /* output cookies array */
791 off_t *cookies;
792 size_t numcookies, maxcookies;
793 /* disk buffer */
794 off_t physstart, physend;
795 size_t skipstart, dropend;
796 char *rawbuf;
797 size_t rawbufmax, rawbytes;
798 struct uio rawuio;
799 struct iovec rawiov;
800 LFS_DIRHEADER *rawdp, *stoprawdp;
801 /* general */
802 int error;
803
804 KASSERT(VOP_ISLOCKED(vp));
805
806 /* figure out where we want to read */
807 callerbytes = calleruio->uio_resid;
808 startoffset = calleruio->uio_offset;
809 endoffset = startoffset + callerbytes;
810
811 if (callerbytes < _DIRENT_MINSIZE(dirent)) {
812 /* no room for even one struct dirent */
813 return EINVAL;
814 }
815
816 /* round start and end down to block boundaries */
817 physstart = startoffset & ~(off_t)(fs->um_dirblksiz - 1);
818 physend = endoffset & ~(off_t)(fs->um_dirblksiz - 1);
819 skipstart = startoffset - physstart;
820 dropend = endoffset - physend;
821
822 if (callerbytes - dropend < LFS_DIRECTSIZ(fs, 0)) {
823 /* no room for even one dirheader + name */
824 return EINVAL;
825 }
826
827 /* how much to actually read */
828 rawbufmax = callerbytes + skipstart - dropend;
829
830 /* read it */
831 rawbuf = kmem_alloc(rawbufmax, KM_SLEEP);
832 rawiov.iov_base = rawbuf;
833 rawiov.iov_len = rawbufmax;
834 rawuio.uio_iov = &rawiov;
835 rawuio.uio_iovcnt = 1;
836 rawuio.uio_offset = physstart;
837 rawuio.uio_resid = rawbufmax;
838 UIO_SETUP_SYSSPACE(&rawuio);
839 rawuio.uio_rw = UIO_READ;
840 error = VOP_READ(vp, &rawuio, 0, ap->a_cred);
841 if (error != 0) {
842 kmem_free(rawbuf, rawbufmax);
843 return error;
844 }
845 rawbytes = rawbufmax - rawuio.uio_resid;
846
847 /* the raw entries to iterate over */
848 rawdp = (LFS_DIRHEADER *)(void *)rawbuf;
849 stoprawdp = (LFS_DIRHEADER *)(void *)&rawbuf[rawbytes];
850
851 /* allocate space to produce dirents into */
852 direntbufmax = callerbytes;
853 direntbuf = kmem_alloc(direntbufmax, KM_SLEEP);
854
855 /* the dirents to iterate over */
856 dirent = (struct dirent *)(void *)direntbuf;
857 stopdirent = (struct dirent *)(void *)&direntbuf[direntbufmax];
858
859 /* the output "cookies" (seek positions of directory entries) */
860 if (ap->a_cookies) {
861 numcookies = 0;
862 maxcookies = rawbytes / LFS_DIRECTSIZ(fs, 1);
863 cookies = malloc(maxcookies * sizeof(*cookies),
864 M_TEMP, M_WAITOK);
865 } else {
866 /* XXX: GCC */
867 maxcookies = 0;
868 cookies = NULL;
869 }
870
871 /* now produce the dirents */
872 curoffset = calleruio->uio_offset;
873 while (rawdp < stoprawdp) {
874 if (skipstart > 0) {
875 /* drain skipstart */
876 if (lfs_dir_getreclen(fs, rawdp) <= skipstart) {
877 skipstart -= lfs_dir_getreclen(fs, rawdp);
878 rawdp = LFS_NEXTDIR(fs, rawdp);
879 continue;
880 }
881 /* caller's start position wasn't on an entry */
882 error = EINVAL;
883 goto out;
884 }
885 if (lfs_dir_getreclen(fs, rawdp) == 0) {
886 struct dirent *save = dirent;
887 dirent->d_reclen = _DIRENT_MINSIZE(dirent);
888 dirent = _DIRENT_NEXT(dirent);
889 save->d_reclen = 0;
890 rawdp = stoprawdp;
891 break;
892 }
893
894 /* copy the header */
895 dirent->d_type = lfs_dir_gettype(fs, rawdp);
896 dirent->d_namlen = lfs_dir_getnamlen(fs, rawdp);
897 dirent->d_reclen = _DIRENT_RECLEN(dirent, dirent->d_namlen);
898
899 /* stop if there isn't room for the name AND another header */
900 if ((char *)(void *)dirent + dirent->d_reclen +
901 _DIRENT_MINSIZE(dirent) > (char *)(void *)stopdirent)
902 break;
903
904 /* copy the name (and inode (XXX: why after the test?)) */
905 dirent->d_fileno = lfs_dir_getino(fs, rawdp);
906 (void)memcpy(dirent->d_name, lfs_dir_nameptr(fs, rawdp),
907 dirent->d_namlen);
908 memset(&dirent->d_name[dirent->d_namlen], 0,
909 dirent->d_reclen - _DIRENT_NAMEOFF(dirent)
910 - dirent->d_namlen);
911
912 /* onward */
913 curoffset += lfs_dir_getreclen(fs, rawdp);
914 if (ap->a_cookies) {
915 KASSERT(numcookies < maxcookies);
916 cookies[numcookies++] = curoffset;
917 }
918 dirent = _DIRENT_NEXT(dirent);
919 rawdp = LFS_NEXTDIR(fs, rawdp);
920 }
921
922 /* transfer the dirents to the caller's buffer */
923 callerbytes = ((char *)(void *)dirent - direntbuf);
924 error = uiomove(direntbuf, callerbytes, calleruio);
925
926 out:
927 calleruio->uio_offset = curoffset;
928 if (ap->a_cookies) {
929 if (error) {
930 free(cookies, M_TEMP);
931 *ap->a_cookies = NULL;
932 *ap->a_ncookies = 0;
933 } else {
934 *ap->a_cookies = cookies;
935 *ap->a_ncookies = numcookies;
936 }
937 }
938 kmem_free(direntbuf, direntbufmax);
939 kmem_free(rawbuf, rawbufmax);
940 *ap->a_eofflag = VTOI(vp)->i_size <= calleruio->uio_offset;
941 return error;
942 }
943
944 /*
945 * Return target name of a symbolic link
946 */
947 int
948 ulfs_readlink(void *v)
949 {
950 struct vop_readlink_args /* {
951 struct vnode *a_vp;
952 struct uio *a_uio;
953 kauth_cred_t a_cred;
954 } */ *ap = v;
955 struct vnode *vp = ap->a_vp;
956 struct inode *ip = VTOI(vp);
957 struct ulfsmount *ump = VFSTOULFS(vp->v_mount);
958 struct lfs *fs = ump->um_lfs;
959 int isize;
960
961 KASSERT(VOP_ISLOCKED(vp));
962
963 /*
964 * The test against um_maxsymlinklen is off by one; it should
965 * theoretically be <=, not <. However, it cannot be changed
966 * as that would break compatibility with existing fs images.
967 */
968
969 isize = ip->i_size;
970 if (isize < fs->um_maxsymlinklen ||
971 (fs->um_maxsymlinklen == 0 && DIP(ip, blocks) == 0)) {
972 uiomove((char *)SHORTLINK(ip), isize, ap->a_uio);
973 return (0);
974 }
975 return (lfs_bufrd(vp, ap->a_uio, 0, ap->a_cred));
976 }
977
978 /*
979 * Print out the contents of an inode.
980 */
981 int
982 ulfs_print(void *v)
983 {
984 struct vop_print_args /* {
985 struct vnode *a_vp;
986 } */ *ap = v;
987 struct vnode *vp;
988 struct inode *ip;
989
990 vp = ap->a_vp;
991 ip = VTOI(vp);
992 printf("tag VT_ULFS, ino %llu, on dev %llu, %llu",
993 (unsigned long long)ip->i_number,
994 (unsigned long long)major(ip->i_dev),
995 (unsigned long long)minor(ip->i_dev));
996 printf(" flags 0x%x, nlink %d\n",
997 ip->i_state, ip->i_nlink);
998 printf("\tmode 0%o, owner %d, group %d, size %qd",
999 ip->i_mode, ip->i_uid, ip->i_gid,
1000 (long long)ip->i_size);
1001 if (vp->v_type == VFIFO)
1002 VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
1003 printf("\n");
1004 return (0);
1005 }
1006
1007 /*
1008 * Read wrapper for special devices.
1009 */
1010 int
1011 ulfsspec_read(void *v)
1012 {
1013 struct vop_read_args /* {
1014 struct vnode *a_vp;
1015 struct uio *a_uio;
1016 int a_ioflag;
1017 kauth_cred_t a_cred;
1018 } */ *ap = v;
1019
1020 KASSERT(VOP_ISLOCKED(ap->a_vp));
1021
1022 /*
1023 * Set access flag.
1024 */
1025 if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)
1026 VTOI(ap->a_vp)->i_state |= IN_ACCESS;
1027 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap));
1028 }
1029
1030 /*
1031 * Write wrapper for special devices.
1032 */
1033 int
1034 ulfsspec_write(void *v)
1035 {
1036 struct vop_write_args /* {
1037 struct vnode *a_vp;
1038 struct uio *a_uio;
1039 int a_ioflag;
1040 kauth_cred_t a_cred;
1041 } */ *ap = v;
1042
1043 KASSERT(VOP_ISLOCKED(ap->a_vp) == LK_EXCLUSIVE);
1044
1045 /*
1046 * Set update and change flags.
1047 */
1048 if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)
1049 VTOI(ap->a_vp)->i_state |= IN_MODIFY;
1050 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap));
1051 }
1052
1053 /*
1054 * Read wrapper for fifo's
1055 */
1056 int
1057 ulfsfifo_read(void *v)
1058 {
1059 struct vop_read_args /* {
1060 struct vnode *a_vp;
1061 struct uio *a_uio;
1062 int a_ioflag;
1063 kauth_cred_t a_cred;
1064 } */ *ap = v;
1065
1066 KASSERT(VOP_ISLOCKED(ap->a_vp));
1067
1068 /*
1069 * Set access flag.
1070 */
1071 VTOI(ap->a_vp)->i_state |= IN_ACCESS;
1072 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap));
1073 }
1074
1075 /*
1076 * Write wrapper for fifo's.
1077 */
1078 int
1079 ulfsfifo_write(void *v)
1080 {
1081 struct vop_write_args /* {
1082 struct vnode *a_vp;
1083 struct uio *a_uio;
1084 int a_ioflag;
1085 kauth_cred_t a_cred;
1086 } */ *ap = v;
1087
1088 KASSERT(VOP_ISLOCKED(ap->a_vp) == LK_EXCLUSIVE);
1089
1090 /*
1091 * Set update and change flags.
1092 */
1093 VTOI(ap->a_vp)->i_state |= IN_MODIFY;
1094 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap));
1095 }
1096
1097 /*
1098 * Return POSIX pathconf information applicable to ulfs filesystems.
1099 */
1100 int
1101 ulfs_pathconf(void *v)
1102 {
1103 struct vop_pathconf_args /* {
1104 struct vnode *a_vp;
1105 int a_name;
1106 register_t *a_retval;
1107 } */ *ap = v;
1108
1109 switch (ap->a_name) {
1110 case _PC_LINK_MAX:
1111 *ap->a_retval = LINK_MAX;
1112 return (0);
1113 case _PC_NAME_MAX:
1114 *ap->a_retval = LFS_MAXNAMLEN;
1115 return (0);
1116 case _PC_PATH_MAX:
1117 *ap->a_retval = PATH_MAX;
1118 return (0);
1119 case _PC_PIPE_BUF:
1120 *ap->a_retval = PIPE_BUF;
1121 return (0);
1122 case _PC_CHOWN_RESTRICTED:
1123 *ap->a_retval = 1;
1124 return (0);
1125 case _PC_NO_TRUNC:
1126 *ap->a_retval = 1;
1127 return (0);
1128 case _PC_SYNC_IO:
1129 *ap->a_retval = 1;
1130 return (0);
1131 case _PC_FILESIZEBITS:
1132 *ap->a_retval = 42;
1133 return (0);
1134 case _PC_SYMLINK_MAX:
1135 *ap->a_retval = MAXPATHLEN;
1136 return (0);
1137 case _PC_2_SYMLINKS:
1138 *ap->a_retval = 1;
1139 return (0);
1140 default:
1141 return (EINVAL);
1142 }
1143 /* NOTREACHED */
1144 }
1145
1146 /*
1147 * Advisory record locking support
1148 */
1149 int
1150 ulfs_advlock(void *v)
1151 {
1152 struct vop_advlock_args /* {
1153 struct vnode *a_vp;
1154 void * a_id;
1155 int a_op;
1156 struct flock *a_fl;
1157 int a_flags;
1158 } */ *ap = v;
1159 struct inode *ip;
1160
1161 ip = VTOI(ap->a_vp);
1162 return lf_advlock(ap, &ip->i_lockf, ip->i_size);
1163 }
1164
1165 /*
1166 * Initialize the vnode associated with a new inode, handle aliased
1167 * vnodes.
1168 */
1169 void
1170 ulfs_vinit(struct mount *mntp, int (**specops)(void *), int (**fifoops)(void *),
1171 struct vnode **vpp)
1172 {
1173 struct timeval tv;
1174 struct inode *ip;
1175 struct vnode *vp;
1176 dev_t rdev;
1177 struct ulfsmount *ump;
1178
1179 vp = *vpp;
1180 ip = VTOI(vp);
1181 switch(vp->v_type = IFTOVT(ip->i_mode)) {
1182 case VCHR:
1183 case VBLK:
1184 vp->v_op = specops;
1185 ump = ip->i_ump;
1186 // XXX clean this up
1187 if (ump->um_fstype == ULFS1)
1188 rdev = (dev_t)ulfs_rw32(ip->i_din->u_32.di_rdev,
1189 ULFS_MPNEEDSWAP(ump->um_lfs));
1190 else
1191 rdev = (dev_t)ulfs_rw64(ip->i_din->u_64.di_rdev,
1192 ULFS_MPNEEDSWAP(ump->um_lfs));
1193 spec_node_init(vp, rdev);
1194 break;
1195 case VFIFO:
1196 vp->v_op = fifoops;
1197 break;
1198 case VNON:
1199 case VBAD:
1200 case VSOCK:
1201 case VLNK:
1202 case VDIR:
1203 case VREG:
1204 break;
1205 }
1206 if (ip->i_number == ULFS_ROOTINO)
1207 vp->v_vflag |= VV_ROOT;
1208 /*
1209 * Initialize modrev times
1210 */
1211 getmicrouptime(&tv);
1212 ip->i_modrev = (uint64_t)(uint)tv.tv_sec << 32
1213 | tv.tv_usec * 4294u;
1214 *vpp = vp;
1215 }
1216
1217 /*
1218 * Allocate len bytes at offset off.
1219 */
1220 int
1221 ulfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
1222 kauth_cred_t cred)
1223 {
1224 struct inode *ip = VTOI(vp);
1225 int error, delta, bshift, bsize;
1226 UVMHIST_FUNC("ulfs_gop_alloc"); UVMHIST_CALLED(ubchist);
1227
1228 KASSERT(genfs_node_wrlocked(vp));
1229
1230 error = 0;
1231 bshift = vp->v_mount->mnt_fs_bshift;
1232 bsize = 1 << bshift;
1233
1234 delta = off & (bsize - 1);
1235 off -= delta;
1236 len += delta;
1237
1238 while (len > 0) {
1239 bsize = MIN(bsize, len);
1240
1241 error = lfs_balloc(vp, off, bsize, cred, flags, NULL);
1242 if (error) {
1243 goto out;
1244 }
1245
1246 /*
1247 * increase file size now, lfs_balloc() requires that
1248 * EOF be up-to-date before each call.
1249 */
1250
1251 if (ip->i_size < off + bsize) {
1252 UVMHIST_LOG(ubchist, "vp %#jx old 0x%jx new 0x%jx",
1253 (uintptr_t)vp, ip->i_size, off + bsize, 0);
1254 ip->i_size = off + bsize;
1255 DIP_ASSIGN(ip, size, ip->i_size);
1256 }
1257
1258 off += bsize;
1259 len -= bsize;
1260 }
1261
1262 out:
1263 return error;
1264 }
1265
1266 void
1267 ulfs_gop_markupdate(struct vnode *vp, int flags)
1268 {
1269 u_int32_t mask = 0;
1270
1271 if ((flags & GOP_UPDATE_ACCESSED) != 0) {
1272 mask = IN_ACCESS;
1273 }
1274 if ((flags & GOP_UPDATE_MODIFIED) != 0) {
1275 if (vp->v_type == VREG) {
1276 mask |= IN_CHANGE | IN_UPDATE;
1277 } else {
1278 mask |= IN_MODIFY;
1279 }
1280 }
1281 if (mask) {
1282 struct inode *ip = VTOI(vp);
1283
1284 ip->i_state |= mask;
1285 }
1286 }
1287
1288 int
1289 ulfs_bufio(enum uio_rw rw, struct vnode *vp, void *buf, size_t len, off_t off,
1290 int ioflg, kauth_cred_t cred, size_t *aresid, struct lwp *l)
1291 {
1292 struct iovec iov;
1293 struct uio uio;
1294 int error;
1295
1296 KASSERT(ISSET(ioflg, IO_NODELOCKED));
1297 KASSERT(VOP_ISLOCKED(vp));
1298 KASSERT(rw != UIO_WRITE || VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
1299
1300 iov.iov_base = buf;
1301 iov.iov_len = len;
1302 uio.uio_iov = &iov;
1303 uio.uio_iovcnt = 1;
1304 uio.uio_resid = len;
1305 uio.uio_offset = off;
1306 uio.uio_rw = rw;
1307 UIO_SETUP_SYSSPACE(&uio);
1308
1309 switch (rw) {
1310 case UIO_READ:
1311 error = lfs_bufrd(vp, &uio, ioflg, cred);
1312 break;
1313 case UIO_WRITE:
1314 error = lfs_bufwr(vp, &uio, ioflg, cred);
1315 break;
1316 default:
1317 panic("invalid uio rw: %d", (int)rw);
1318 }
1319
1320 if (aresid)
1321 *aresid = uio.uio_resid;
1322 else if (uio.uio_resid && error == 0)
1323 error = EIO;
1324
1325 KASSERT(VOP_ISLOCKED(vp));
1326 KASSERT(rw != UIO_WRITE || VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
1327 return error;
1328 }
1329