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