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