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