Commit | Line | Data |
---|---|---|
984263bc MD |
1 | /* |
2 | * Copyright (c) 1982, 1986, 1989, 1993, 1995 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * (c) UNIX System Laboratories, Inc. | |
5 | * All or some portions of this file are derived from material licensed | |
6 | * to the University of California by American Telephone and Telegraph | |
7 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
8 | * the permission of UNIX System Laboratories, Inc. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in the | |
17 | * documentation and/or other materials provided with the distribution. | |
dc71b7ab | 18 | * 3. Neither the name of the University nor the names of its contributors |
984263bc MD |
19 | * may be used to endorse or promote products derived from this software |
20 | * without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
32 | * SUCH DAMAGE. | |
33 | * | |
34 | * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95 | |
35 | * $FreeBSD: src/sys/ufs/ufs/ufs_vnops.c,v 1.131.2.8 2003/01/02 17:26:19 bde Exp $ | |
36 | */ | |
37 | ||
38 | #include "opt_quota.h" | |
39 | #include "opt_suiddir.h" | |
40 | #include "opt_ufs.h" | |
41 | ||
42 | #include <sys/param.h> | |
43 | #include <sys/systm.h> | |
13dd34d8 | 44 | #include <sys/uio.h> |
984263bc MD |
45 | #include <sys/kernel.h> |
46 | #include <sys/fcntl.h> | |
47 | #include <sys/stat.h> | |
48 | #include <sys/buf.h> | |
49 | #include <sys/proc.h> | |
2b3f93ea | 50 | #include <sys/caps.h> |
dadab5e9 | 51 | #include <sys/namei.h> |
984263bc MD |
52 | #include <sys/mount.h> |
53 | #include <sys/unistd.h> | |
54 | #include <sys/vnode.h> | |
55 | #include <sys/malloc.h> | |
56 | #include <sys/dirent.h> | |
57 | #include <sys/lockf.h> | |
58 | #include <sys/event.h> | |
59 | #include <sys/conf.h> | |
60 | ||
61 | #include <sys/file.h> /* XXX */ | |
21c8b4bd | 62 | #include <sys/jail.h> |
984263bc MD |
63 | |
64 | #include <vm/vm.h> | |
65 | #include <vm/vm_extern.h> | |
66 | ||
1f2de5d4 | 67 | #include <vfs/fifofs/fifo.h> |
984263bc | 68 | |
1f2de5d4 MD |
69 | #include "quota.h" |
70 | #include "inode.h" | |
71 | #include "dir.h" | |
72 | #include "ufsmount.h" | |
73 | #include "ufs_extern.h" | |
ac690a1d | 74 | #include "ffs_extern.h" |
b0d18f7d | 75 | #include "fs.h" |
984263bc | 76 | #ifdef UFS_DIRHASH |
1f2de5d4 | 77 | #include "dirhash.h" |
984263bc MD |
78 | #endif |
79 | ||
a6ee311a RG |
80 | static int ufs_access (struct vop_access_args *); |
81 | static int ufs_advlock (struct vop_advlock_args *); | |
87de5057 MD |
82 | static int ufs_chmod (struct vnode *, int, struct ucred *); |
83 | static int ufs_chown (struct vnode *, uid_t, gid_t, struct ucred *); | |
a6ee311a | 84 | static int ufs_close (struct vop_close_args *); |
e62afb5f | 85 | static int ufs_create (struct vop_old_create_args *); |
a6ee311a | 86 | static int ufs_getattr (struct vop_getattr_args *); |
e62afb5f | 87 | static int ufs_link (struct vop_old_link_args *); |
a6ee311a | 88 | static int ufs_makeinode (int mode, struct vnode *, struct vnode **, struct componentname *); |
349433c9 | 89 | static int ufs_markatime (struct vop_markatime_args *); |
a6ee311a | 90 | static int ufs_missingop (struct vop_generic_args *ap); |
e62afb5f MD |
91 | static int ufs_mkdir (struct vop_old_mkdir_args *); |
92 | static int ufs_mknod (struct vop_old_mknod_args *); | |
a6ee311a RG |
93 | static int ufs_print (struct vop_print_args *); |
94 | static int ufs_readdir (struct vop_readdir_args *); | |
95 | static int ufs_readlink (struct vop_readlink_args *); | |
e62afb5f MD |
96 | static int ufs_remove (struct vop_old_remove_args *); |
97 | static int ufs_rename (struct vop_old_rename_args *); | |
98 | static int ufs_rmdir (struct vop_old_rmdir_args *); | |
a6ee311a RG |
99 | static int ufs_setattr (struct vop_setattr_args *); |
100 | static int ufs_strategy (struct vop_strategy_args *); | |
e62afb5f MD |
101 | static int ufs_symlink (struct vop_old_symlink_args *); |
102 | static int ufs_whiteout (struct vop_old_whiteout_args *); | |
a6ee311a RG |
103 | static int ufsfifo_close (struct vop_close_args *); |
104 | static int ufsfifo_kqfilter (struct vop_kqfilter_args *); | |
105 | static int ufsfifo_read (struct vop_read_args *); | |
106 | static int ufsfifo_write (struct vop_write_args *); | |
a6ee311a RG |
107 | static int filt_ufsread (struct knote *kn, long hint); |
108 | static int filt_ufswrite (struct knote *kn, long hint); | |
109 | static int filt_ufsvnode (struct knote *kn, long hint); | |
110 | static void filt_ufsdetach (struct knote *kn); | |
111 | static int ufs_kqfilter (struct vop_kqfilter_args *ap); | |
984263bc | 112 | |
984263bc | 113 | #define VN_KNOTE(vp, b) \ |
5b22f1a7 | 114 | KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, (b)) |
984263bc | 115 | |
e088dc32 JS |
116 | #define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0) |
117 | ||
984263bc MD |
118 | /* |
119 | * A virgin directory (no blushing please). | |
120 | */ | |
121 | static struct dirtemplate mastertemplate = { | |
122 | 0, 12, DT_DIR, 1, ".", | |
123 | 0, DIRBLKSIZ - 12, DT_DIR, 2, ".." | |
124 | }; | |
125 | static struct odirtemplate omastertemplate = { | |
126 | 0, 12, 1, ".", | |
127 | 0, DIRBLKSIZ - 12, 2, ".." | |
128 | }; | |
129 | ||
130 | void | |
0973c589 | 131 | ufs_itimes(struct vnode *vp) |
984263bc MD |
132 | { |
133 | struct inode *ip; | |
134 | struct timespec ts; | |
135 | ||
136 | ip = VTOI(vp); | |
137 | if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0) | |
138 | return; | |
139 | if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp)) | |
140 | ip->i_flag |= IN_LAZYMOD; | |
141 | else | |
142 | ip->i_flag |= IN_MODIFIED; | |
723be147 | 143 | |
984263bc MD |
144 | if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { |
145 | vfs_timestamp(&ts); | |
146 | if (ip->i_flag & IN_ACCESS) { | |
712774a1 MD |
147 | ip->i_atime = (uint32_t)ts.tv_sec; |
148 | ip->i_atime_ext = ts.tv_sec >> 32; | |
984263bc MD |
149 | ip->i_atimensec = ts.tv_nsec; |
150 | } | |
723be147 | 151 | if (ip->i_flag & IN_CHANGE) { |
712774a1 MD |
152 | ip->i_ctime = (uint32_t)ts.tv_sec; |
153 | ip->i_ctime_ext = ts.tv_sec >> 32; | |
723be147 MD |
154 | ip->i_ctimensec = ts.tv_nsec; |
155 | } | |
984263bc | 156 | if (ip->i_flag & IN_UPDATE) { |
fa4a12c4 MD |
157 | if (ip->i_flag & IN_NOCOPYWRITE) { |
158 | if (vp->v_flag & VLASTWRITETS) { | |
712774a1 MD |
159 | ip->i_mtime = (uint32_t) |
160 | vp->v_lastwrite_ts.tv_sec; | |
161 | ip->i_mtime_ext = | |
162 | vp->v_lastwrite_ts.tv_sec >> 32; | |
fa4a12c4 MD |
163 | ip->i_mtimensec = |
164 | vp->v_lastwrite_ts.tv_nsec; | |
165 | } | |
166 | } else { | |
712774a1 MD |
167 | ip->i_mtime = (uint32_t)ts.tv_sec; |
168 | ip->i_mtime_ext = ts.tv_sec >> 32; | |
fa4a12c4 | 169 | ip->i_mtimensec = ts.tv_nsec; |
723be147 | 170 | } |
984263bc MD |
171 | ip->i_modrev++; |
172 | } | |
984263bc MD |
173 | } |
174 | ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE); | |
175 | } | |
176 | ||
177 | /* | |
178 | * Create a regular file | |
0973c589 CP |
179 | * |
180 | * ufs_create(struct vnode *a_dvp, struct vnode **a_vpp, | |
181 | * struct componentname *a_cnp, struct vattr *a_vap) | |
984263bc | 182 | */ |
0961aa92 | 183 | static |
984263bc | 184 | int |
e62afb5f | 185 | ufs_create(struct vop_old_create_args *ap) |
984263bc MD |
186 | { |
187 | int error; | |
188 | ||
189 | error = | |
190 | ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode), | |
191 | ap->a_dvp, ap->a_vpp, ap->a_cnp); | |
192 | if (error) | |
193 | return (error); | |
194 | VN_KNOTE(ap->a_dvp, NOTE_WRITE); | |
195 | return (0); | |
196 | } | |
197 | ||
198 | /* | |
199 | * Mknod vnode call | |
0973c589 CP |
200 | * |
201 | * ufs_mknod(struct vnode *a_dvp, struct vnode **a_vpp, | |
202 | * struct componentname *a_cnp, struct vattr *a_vap) | |
984263bc MD |
203 | */ |
204 | /* ARGSUSED */ | |
0961aa92 | 205 | static |
984263bc | 206 | int |
e62afb5f | 207 | ufs_mknod(struct vop_old_mknod_args *ap) |
984263bc MD |
208 | { |
209 | struct vattr *vap = ap->a_vap; | |
210 | struct vnode **vpp = ap->a_vpp; | |
211 | struct inode *ip; | |
212 | ino_t ino; | |
213 | int error; | |
214 | ||
0e9b9130 MD |
215 | /* |
216 | * UFS cannot represent the entire major/minor range supported by | |
217 | * the kernel. | |
218 | */ | |
219 | if (vap->va_rmajor != VNOVAL && | |
220 | makeudev(vap->va_rmajor, vap->va_rminor) == NOUDEV) { | |
221 | return(EINVAL); | |
222 | } | |
223 | ||
c0515afc MD |
224 | /* no special directory support */ |
225 | if (vap->va_type == VDIR) | |
226 | return(EINVAL); | |
227 | ||
984263bc MD |
228 | error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), |
229 | ap->a_dvp, vpp, ap->a_cnp); | |
230 | if (error) | |
231 | return (error); | |
232 | VN_KNOTE(ap->a_dvp, NOTE_WRITE); | |
233 | ip = VTOI(*vpp); | |
234 | ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; | |
0e9b9130 | 235 | if (vap->va_rmajor != VNOVAL) { |
984263bc MD |
236 | /* |
237 | * Want to be able to use this to make badblock | |
238 | * inodes, so don't truncate the dev number. | |
239 | */ | |
0e9b9130 | 240 | ip->i_rdev = makeudev(vap->va_rmajor, vap->va_rminor); |
984263bc MD |
241 | } |
242 | /* | |
243 | * Remove inode, then reload it through VFS_VGET so it is | |
244 | * checked to see if it is an alias of an existing entry in | |
245 | * the inode cache. | |
246 | */ | |
984263bc MD |
247 | (*vpp)->v_type = VNON; |
248 | ino = ip->i_number; /* Save this before vgone() invalidates ip. */ | |
3c37c940 | 249 | vgone_vxlocked(*vpp); |
5fd012e0 | 250 | vput(*vpp); |
b9b0a6d0 | 251 | error = VFS_VGET(ap->a_dvp->v_mount, NULL, ino, vpp); |
984263bc MD |
252 | if (error) { |
253 | *vpp = NULL; | |
254 | return (error); | |
255 | } | |
256 | return (0); | |
257 | } | |
258 | ||
984263bc MD |
259 | /* |
260 | * Close called. | |
261 | * | |
262 | * Update the times on the inode. | |
0973c589 | 263 | * |
b478fdce | 264 | * ufs_close(struct vnode *a_vp, int a_fflag) |
984263bc MD |
265 | */ |
266 | /* ARGSUSED */ | |
0961aa92 | 267 | static |
984263bc | 268 | int |
0973c589 | 269 | ufs_close(struct vop_close_args *ap) |
984263bc | 270 | { |
3ff2135f | 271 | struct vnode *vp = ap->a_vp; |
984263bc | 272 | |
ee173d09 | 273 | if (VREFCNT(vp) > 1) |
984263bc | 274 | ufs_itimes(vp); |
8ddc6004 | 275 | return (vop_stdclose(ap)); |
984263bc MD |
276 | } |
277 | ||
0973c589 | 278 | /* |
b478fdce | 279 | * ufs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred) |
0973c589 | 280 | */ |
0961aa92 | 281 | static |
984263bc | 282 | int |
0973c589 | 283 | ufs_access(struct vop_access_args *ap) |
984263bc MD |
284 | { |
285 | struct vnode *vp = ap->a_vp; | |
286 | struct inode *ip = VTOI(vp); | |
984263bc | 287 | int error; |
984263bc | 288 | |
795ecc74 NT |
289 | #ifdef QUOTA |
290 | if (ap->a_mode & VWRITE) { | |
984263bc MD |
291 | switch (vp->v_type) { |
292 | case VDIR: | |
293 | case VLNK: | |
294 | case VREG: | |
50e58362 | 295 | if ((error = ufs_getinoquota(ip)) != 0) |
984263bc | 296 | return (error); |
984263bc MD |
297 | break; |
298 | default: | |
299 | break; | |
300 | } | |
301 | } | |
3a907475 | 302 | #endif |
984263bc | 303 | |
795ecc74 NT |
304 | error = vop_helper_access(ap, ip->i_uid, ip->i_gid, ip->i_mode, 0); |
305 | return (error); | |
984263bc MD |
306 | } |
307 | ||
0973c589 | 308 | /* |
b478fdce | 309 | * ufs_getattr(struct vnode *a_vp, struct vattr *a_vap) |
0973c589 | 310 | */ |
984263bc | 311 | /* ARGSUSED */ |
0961aa92 | 312 | static |
984263bc | 313 | int |
0973c589 | 314 | ufs_getattr(struct vop_getattr_args *ap) |
984263bc | 315 | { |
3ff2135f RG |
316 | struct vnode *vp = ap->a_vp; |
317 | struct inode *ip = VTOI(vp); | |
318 | struct vattr *vap = ap->a_vap; | |
984263bc MD |
319 | |
320 | ufs_itimes(vp); | |
321 | /* | |
322 | * Copy from inode table | |
323 | */ | |
2ac7d105 | 324 | vap->va_fsid = devid_from_dev(ip->i_dev); |
984263bc MD |
325 | vap->va_fileid = ip->i_number; |
326 | vap->va_mode = ip->i_mode & ~IFMT; | |
327 | vap->va_nlink = VFSTOUFS(vp->v_mount)->um_i_effnlink_valid ? | |
328 | ip->i_effnlink : ip->i_nlink; | |
329 | vap->va_uid = ip->i_uid; | |
330 | vap->va_gid = ip->i_gid; | |
0e9b9130 MD |
331 | vap->va_rmajor = umajor(ip->i_rdev); |
332 | vap->va_rminor = uminor(ip->i_rdev); | |
984263bc | 333 | vap->va_size = ip->i_din.di_size; |
712774a1 MD |
334 | vap->va_atime.tv_sec = |
335 | (time_t)(ip->i_atime | ((uint64_t)ip->i_atime_ext << 32)); | |
984263bc | 336 | vap->va_atime.tv_nsec = ip->i_atimensec; |
712774a1 MD |
337 | vap->va_mtime.tv_sec = |
338 | (time_t)(ip->i_mtime | ((uint64_t)ip->i_mtime_ext << 32)); | |
984263bc | 339 | vap->va_mtime.tv_nsec = ip->i_mtimensec; |
712774a1 MD |
340 | vap->va_ctime.tv_sec = |
341 | (time_t)(ip->i_ctime | ((uint64_t)ip->i_ctime_ext << 32)); | |
984263bc MD |
342 | vap->va_ctime.tv_nsec = ip->i_ctimensec; |
343 | vap->va_flags = ip->i_flags; | |
344 | vap->va_gen = ip->i_gen; | |
345 | vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize; | |
346 | vap->va_bytes = dbtob((u_quad_t)ip->i_blocks); | |
347 | vap->va_type = IFTOVT(ip->i_mode); | |
348 | vap->va_filerev = ip->i_modrev; | |
349 | return (0); | |
350 | } | |
351 | ||
349433c9 MD |
352 | static |
353 | int | |
354 | ufs_markatime(struct vop_markatime_args *ap) | |
355 | { | |
356 | struct vnode *vp = ap->a_vp; | |
357 | struct inode *ip = VTOI(vp); | |
358 | ||
359 | if (vp->v_mount->mnt_flag & MNT_RDONLY) | |
360 | return (EROFS); | |
361 | if (vp->v_mount->mnt_flag & MNT_NOATIME) | |
362 | return (0); | |
363 | ip->i_flag |= IN_ACCESS; | |
364 | VN_KNOTE(vp, NOTE_ATTRIB); | |
365 | return (0); | |
366 | } | |
367 | ||
984263bc MD |
368 | /* |
369 | * Set attribute vnode op. called from several syscalls | |
0973c589 CP |
370 | * |
371 | * ufs_setattr(struct vnode *a_vp, struct vattr *a_vap, | |
b478fdce | 372 | * struct ucred *a_cred) |
984263bc | 373 | */ |
0961aa92 | 374 | static |
984263bc | 375 | int |
0973c589 | 376 | ufs_setattr(struct vop_setattr_args *ap) |
984263bc MD |
377 | { |
378 | struct vattr *vap = ap->a_vap; | |
379 | struct vnode *vp = ap->a_vp; | |
380 | struct inode *ip = VTOI(vp); | |
381 | struct ucred *cred = ap->a_cred; | |
984263bc MD |
382 | int error; |
383 | ||
384 | /* | |
385 | * Check for unsettable attributes. | |
386 | */ | |
387 | if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || | |
388 | (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || | |
0e9b9130 | 389 | (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) || |
984263bc MD |
390 | ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { |
391 | return (EINVAL); | |
392 | } | |
393 | if (vap->va_flags != VNOVAL) { | |
394 | if (vp->v_mount->mnt_flag & MNT_RDONLY) | |
395 | return (EROFS); | |
396 | if (cred->cr_uid != ip->i_uid && | |
2b3f93ea MD |
397 | (error = caps_priv_check(cred, SYSCAP_NOVFS_SETATTR))) |
398 | { | |
984263bc | 399 | return (error); |
2b3f93ea MD |
400 | } |
401 | ||
21c8b4bd MD |
402 | /* |
403 | * Note that a root chflags becomes a user chflags when | |
25e27214 | 404 | * we are jailed, unless the jail vfs_chflags sysctl |
21c8b4bd MD |
405 | * is set. |
406 | */ | |
25e27214 AHJ |
407 | if (cred->cr_uid == 0 && |
408 | (!jailed(cred) || | |
409 | PRISON_CAP_ISSET(cred->cr_prison->pr_caps, | |
410 | PRISON_CAP_VFS_CHFLAGS))) { | |
984263bc MD |
411 | if ((ip->i_flags |
412 | & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) && | |
413 | securelevel > 0) | |
414 | return (EPERM); | |
415 | ip->i_flags = vap->va_flags; | |
416 | } else { | |
417 | if (ip->i_flags | |
418 | & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) || | |
419 | (vap->va_flags & UF_SETTABLE) != vap->va_flags) | |
420 | return (EPERM); | |
421 | ip->i_flags &= SF_SETTABLE; | |
422 | ip->i_flags |= (vap->va_flags & UF_SETTABLE); | |
423 | } | |
424 | ip->i_flag |= IN_CHANGE; | |
425 | if (vap->va_flags & (IMMUTABLE | APPEND)) | |
426 | return (0); | |
427 | } | |
428 | if (ip->i_flags & (IMMUTABLE | APPEND)) | |
429 | return (EPERM); | |
430 | /* | |
431 | * Go through the fields and update iff not VNOVAL. | |
432 | */ | |
433 | if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { | |
434 | if (vp->v_mount->mnt_flag & MNT_RDONLY) | |
435 | return (EROFS); | |
87de5057 | 436 | if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred)) != 0) |
984263bc MD |
437 | return (error); |
438 | } | |
439 | if (vap->va_size != VNOVAL) { | |
440 | /* | |
f719c866 | 441 | * Disallow write attempts on read-only filesystems; |
984263bc | 442 | * unless the file is a socket, fifo, or a block or |
f719c866 | 443 | * character device resident on the filesystem. |
984263bc MD |
444 | */ |
445 | switch (vp->v_type) { | |
446 | case VDIR: | |
447 | return (EISDIR); | |
448 | case VLNK: | |
449 | case VREG: | |
450 | if (vp->v_mount->mnt_flag & MNT_RDONLY) | |
451 | return (EROFS); | |
452 | break; | |
453 | default: | |
454 | break; | |
455 | } | |
ac690a1d | 456 | if ((error = ffs_truncate(vp, vap->va_size, 0, cred)) != 0) |
984263bc MD |
457 | return (error); |
458 | } | |
459 | ip = VTOI(vp); | |
460 | if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { | |
461 | if (vp->v_mount->mnt_flag & MNT_RDONLY) | |
462 | return (EROFS); | |
463 | if (cred->cr_uid != ip->i_uid && | |
2b3f93ea | 464 | (error = caps_priv_check(cred, SYSCAP_NOVFS_SETATTR)) && |
984263bc | 465 | ((vap->va_vaflags & VA_UTIMES_NULL) == 0 || |
cb66845a | 466 | (error = VOP_EACCESS(vp, VWRITE, cred)))) |
2b3f93ea | 467 | { |
984263bc | 468 | return (error); |
2b3f93ea | 469 | } |
984263bc MD |
470 | if (vap->va_atime.tv_sec != VNOVAL) |
471 | ip->i_flag |= IN_ACCESS; | |
472 | if (vap->va_mtime.tv_sec != VNOVAL) | |
473 | ip->i_flag |= IN_CHANGE | IN_UPDATE; | |
474 | ufs_itimes(vp); | |
475 | if (vap->va_atime.tv_sec != VNOVAL) { | |
712774a1 MD |
476 | ip->i_atime = (uint32_t)vap->va_atime.tv_sec; |
477 | ip->i_atime_ext = vap->va_atime.tv_sec >> 32; | |
984263bc MD |
478 | ip->i_atimensec = vap->va_atime.tv_nsec; |
479 | } | |
480 | if (vap->va_mtime.tv_sec != VNOVAL) { | |
712774a1 MD |
481 | ip->i_mtime = (uint32_t)vap->va_mtime.tv_sec; |
482 | ip->i_mtime_ext = vap->va_mtime.tv_sec >> 32; | |
984263bc | 483 | ip->i_mtimensec = vap->va_mtime.tv_nsec; |
fa4a12c4 | 484 | vclrflags(vp, VLASTWRITETS); |
984263bc | 485 | } |
ac690a1d | 486 | error = ffs_update(vp, 0); |
984263bc MD |
487 | if (error) |
488 | return (error); | |
489 | } | |
490 | error = 0; | |
491 | if (vap->va_mode != (mode_t)VNOVAL) { | |
492 | if (vp->v_mount->mnt_flag & MNT_RDONLY) | |
493 | return (EROFS); | |
87de5057 | 494 | error = ufs_chmod(vp, (int)vap->va_mode, cred); |
984263bc MD |
495 | } |
496 | VN_KNOTE(vp, NOTE_ATTRIB); | |
497 | return (error); | |
498 | } | |
499 | ||
500 | /* | |
501 | * Change the mode on a file. | |
502 | * Inode must be locked before calling. | |
503 | */ | |
504 | static int | |
87de5057 | 505 | ufs_chmod(struct vnode *vp, int mode, struct ucred *cred) |
984263bc | 506 | { |
3ff2135f | 507 | struct inode *ip = VTOI(vp); |
984263bc | 508 | int error; |
cd65363e | 509 | mode_t cur_mode = ip->i_mode; |
984263bc | 510 | |
cd65363e MD |
511 | error = vop_helper_chmod(vp, mode, cred, ip->i_uid, ip->i_gid, |
512 | &cur_mode); | |
513 | if (error) | |
514 | return (error); | |
515 | #if 0 | |
984263bc | 516 | if (cred->cr_uid != ip->i_uid) { |
2b3f93ea | 517 | error = caps_priv_check(cred, SYSCAP_NOVFS_CHMOD); |
984263bc MD |
518 | if (error) |
519 | return (error); | |
520 | } | |
521 | if (cred->cr_uid) { | |
522 | if (vp->v_type != VDIR && (mode & S_ISTXT)) | |
523 | return (EFTYPE); | |
524 | if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) | |
525 | return (EPERM); | |
526 | } | |
cd65363e MD |
527 | #endif |
528 | ip->i_mode = cur_mode; | |
984263bc MD |
529 | ip->i_flag |= IN_CHANGE; |
530 | return (0); | |
531 | } | |
532 | ||
533 | /* | |
534 | * Perform chown operation on inode ip; | |
535 | * inode must be locked prior to call. | |
536 | */ | |
537 | static int | |
87de5057 | 538 | ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred) |
984263bc | 539 | { |
3ff2135f | 540 | struct inode *ip = VTOI(vp); |
984263bc MD |
541 | uid_t ouid; |
542 | gid_t ogid; | |
543 | int error = 0; | |
544 | #ifdef QUOTA | |
3ff2135f | 545 | int i; |
984263bc MD |
546 | long change; |
547 | #endif | |
548 | ||
549 | if (uid == (uid_t)VNOVAL) | |
550 | uid = ip->i_uid; | |
551 | if (gid == (gid_t)VNOVAL) | |
552 | gid = ip->i_gid; | |
553 | /* | |
554 | * If we don't own the file, are trying to change the owner | |
555 | * of the file, or are not a member of the target group, | |
556 | * the caller must be superuser or the call fails. | |
557 | */ | |
558 | if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid || | |
a10e3626 | 559 | (gid != ip->i_gid && !(cred->cr_gid == gid || |
ad23467e | 560 | groupmember(gid, cred)))) && |
2b3f93ea MD |
561 | (error = caps_priv_check(cred, SYSCAP_NOVFS_CHOWN))) |
562 | { | |
984263bc | 563 | return (error); |
2b3f93ea | 564 | } |
984263bc MD |
565 | ogid = ip->i_gid; |
566 | ouid = ip->i_uid; | |
567 | #ifdef QUOTA | |
50e58362 | 568 | if ((error = ufs_getinoquota(ip)) != 0) |
984263bc MD |
569 | return (error); |
570 | if (ouid == uid) { | |
50e58362 | 571 | ufs_dqrele(vp, ip->i_dquot[USRQUOTA]); |
984263bc MD |
572 | ip->i_dquot[USRQUOTA] = NODQUOT; |
573 | } | |
574 | if (ogid == gid) { | |
50e58362 | 575 | ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]); |
984263bc MD |
576 | ip->i_dquot[GRPQUOTA] = NODQUOT; |
577 | } | |
578 | change = ip->i_blocks; | |
50e58362 MD |
579 | (void) ufs_chkdq(ip, -change, cred, CHOWN); |
580 | (void) ufs_chkiq(ip, -1, cred, CHOWN); | |
984263bc | 581 | for (i = 0; i < MAXQUOTAS; i++) { |
50e58362 | 582 | ufs_dqrele(vp, ip->i_dquot[i]); |
984263bc MD |
583 | ip->i_dquot[i] = NODQUOT; |
584 | } | |
585 | #endif | |
586 | ip->i_gid = gid; | |
587 | ip->i_uid = uid; | |
588 | #ifdef QUOTA | |
50e58362 | 589 | if ((error = ufs_getinoquota(ip)) == 0) { |
984263bc | 590 | if (ouid == uid) { |
50e58362 | 591 | ufs_dqrele(vp, ip->i_dquot[USRQUOTA]); |
984263bc MD |
592 | ip->i_dquot[USRQUOTA] = NODQUOT; |
593 | } | |
594 | if (ogid == gid) { | |
50e58362 | 595 | ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]); |
984263bc MD |
596 | ip->i_dquot[GRPQUOTA] = NODQUOT; |
597 | } | |
50e58362 MD |
598 | if ((error = ufs_chkdq(ip, change, cred, CHOWN)) == 0) { |
599 | if ((error = ufs_chkiq(ip, 1, cred, CHOWN)) == 0) | |
984263bc MD |
600 | goto good; |
601 | else | |
50e58362 | 602 | (void)ufs_chkdq(ip, -change, cred, CHOWN|FORCE); |
984263bc MD |
603 | } |
604 | for (i = 0; i < MAXQUOTAS; i++) { | |
50e58362 | 605 | ufs_dqrele(vp, ip->i_dquot[i]); |
984263bc MD |
606 | ip->i_dquot[i] = NODQUOT; |
607 | } | |
608 | } | |
609 | ip->i_gid = ogid; | |
610 | ip->i_uid = ouid; | |
50e58362 | 611 | if (ufs_getinoquota(ip) == 0) { |
984263bc | 612 | if (ouid == uid) { |
50e58362 | 613 | ufs_dqrele(vp, ip->i_dquot[USRQUOTA]); |
984263bc MD |
614 | ip->i_dquot[USRQUOTA] = NODQUOT; |
615 | } | |
616 | if (ogid == gid) { | |
50e58362 | 617 | ufs_dqrele(vp, ip->i_dquot[GRPQUOTA]); |
984263bc MD |
618 | ip->i_dquot[GRPQUOTA] = NODQUOT; |
619 | } | |
50e58362 MD |
620 | (void) ufs_chkdq(ip, change, cred, FORCE|CHOWN); |
621 | (void) ufs_chkiq(ip, 1, cred, FORCE|CHOWN); | |
622 | (void) ufs_getinoquota(ip); | |
984263bc MD |
623 | } |
624 | return (error); | |
625 | good: | |
50e58362 | 626 | if (ufs_getinoquota(ip)) |
984263bc MD |
627 | panic("ufs_chown: lost quota"); |
628 | #endif /* QUOTA */ | |
629 | ip->i_flag |= IN_CHANGE; | |
630 | if (cred->cr_uid != 0 && (ouid != uid || ogid != gid)) | |
631 | ip->i_mode &= ~(ISUID | ISGID); | |
632 | return (0); | |
633 | } | |
634 | ||
0973c589 CP |
635 | /* |
636 | * ufs_remove(struct vnode *a_dvp, struct vnode *a_vp, | |
637 | * struct componentname *a_cnp) | |
638 | */ | |
0961aa92 | 639 | static |
984263bc | 640 | int |
e62afb5f | 641 | ufs_remove(struct vop_old_remove_args *ap) |
984263bc MD |
642 | { |
643 | struct inode *ip; | |
644 | struct vnode *vp = ap->a_vp; | |
645 | struct vnode *dvp = ap->a_dvp; | |
646 | int error; | |
647 | ||
648 | ip = VTOI(vp); | |
3a907475 | 649 | #if 0 /* handled by kernel now */ |
984263bc MD |
650 | if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) || |
651 | (VTOI(dvp)->i_flags & APPEND)) { | |
652 | error = EPERM; | |
653 | goto out; | |
654 | } | |
3a907475 | 655 | #endif |
984263bc MD |
656 | error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0); |
657 | VN_KNOTE(vp, NOTE_DELETE); | |
658 | VN_KNOTE(dvp, NOTE_WRITE); | |
3a907475 | 659 | #if 0 |
984263bc | 660 | out: |
3a907475 | 661 | #endif |
984263bc MD |
662 | return (error); |
663 | } | |
664 | ||
665 | /* | |
666 | * link vnode call | |
0973c589 CP |
667 | * |
668 | * ufs_link(struct vnode *a_tdvp, struct vnode *a_vp, | |
669 | * struct componentname *a_cnp) | |
984263bc | 670 | */ |
0961aa92 | 671 | static |
984263bc | 672 | int |
e62afb5f | 673 | ufs_link(struct vop_old_link_args *ap) |
984263bc MD |
674 | { |
675 | struct vnode *vp = ap->a_vp; | |
676 | struct vnode *tdvp = ap->a_tdvp; | |
677 | struct componentname *cnp = ap->a_cnp; | |
984263bc MD |
678 | struct inode *ip; |
679 | struct direct newdir; | |
680 | int error; | |
681 | ||
984263bc MD |
682 | if (tdvp->v_mount != vp->v_mount) { |
683 | error = EXDEV; | |
684 | goto out2; | |
685 | } | |
b458d1ab MD |
686 | if (tdvp != vp) { |
687 | error = vn_lock(vp, LK_EXCLUSIVE | LK_FAILRECLAIM); | |
688 | if (error) | |
689 | goto out2; | |
984263bc MD |
690 | } |
691 | ip = VTOI(vp); | |
692 | if ((nlink_t)ip->i_nlink >= LINK_MAX) { | |
693 | error = EMLINK; | |
694 | goto out1; | |
695 | } | |
3a907475 | 696 | #if 0 /* handled by kernel now, also DragonFly allows this */ |
984263bc MD |
697 | if (ip->i_flags & (IMMUTABLE | APPEND)) { |
698 | error = EPERM; | |
699 | goto out1; | |
700 | } | |
3a907475 | 701 | #endif |
984263bc MD |
702 | ip->i_effnlink++; |
703 | ip->i_nlink++; | |
704 | ip->i_flag |= IN_CHANGE; | |
705 | if (DOINGSOFTDEP(vp)) | |
706 | softdep_change_linkcnt(ip); | |
ac690a1d | 707 | error = ffs_update(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp))); |
984263bc MD |
708 | if (!error) { |
709 | ufs_makedirentry(ip, cnp, &newdir); | |
710 | error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL); | |
711 | } | |
712 | ||
713 | if (error) { | |
714 | ip->i_effnlink--; | |
715 | ip->i_nlink--; | |
716 | ip->i_flag |= IN_CHANGE; | |
717 | if (DOINGSOFTDEP(vp)) | |
718 | softdep_change_linkcnt(ip); | |
719 | } | |
720 | out1: | |
721 | if (tdvp != vp) | |
a11aaa81 | 722 | vn_unlock(vp); |
984263bc MD |
723 | out2: |
724 | VN_KNOTE(vp, NOTE_LINK); | |
725 | VN_KNOTE(tdvp, NOTE_WRITE); | |
726 | return (error); | |
727 | } | |
728 | ||
729 | /* | |
730 | * whiteout vnode call | |
0973c589 CP |
731 | * |
732 | * ufs_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags) | |
984263bc | 733 | */ |
0961aa92 | 734 | static |
984263bc | 735 | int |
e62afb5f | 736 | ufs_whiteout(struct vop_old_whiteout_args *ap) |
984263bc MD |
737 | { |
738 | struct vnode *dvp = ap->a_dvp; | |
739 | struct componentname *cnp = ap->a_cnp; | |
740 | struct direct newdir; | |
741 | int error = 0; | |
742 | ||
743 | switch (ap->a_flags) { | |
2b69e610 | 744 | case NAMEI_LOOKUP: |
984263bc MD |
745 | /* 4.4 format directories support whiteout operations */ |
746 | if (dvp->v_mount->mnt_maxsymlinklen > 0) | |
747 | return (0); | |
748 | return (EOPNOTSUPP); | |
749 | ||
2b69e610 | 750 | case NAMEI_CREATE: |
984263bc MD |
751 | /* create a new directory whiteout */ |
752 | #ifdef DIAGNOSTIC | |
984263bc MD |
753 | if (dvp->v_mount->mnt_maxsymlinklen <= 0) |
754 | panic("ufs_whiteout: old format filesystem"); | |
755 | #endif | |
756 | ||
c309c6d4 | 757 | newdir.d_ino = UFS_WINO; |
984263bc MD |
758 | newdir.d_namlen = cnp->cn_namelen; |
759 | bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1); | |
760 | newdir.d_type = DT_WHT; | |
761 | error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL); | |
762 | break; | |
763 | ||
2b69e610 | 764 | case NAMEI_DELETE: |
984263bc MD |
765 | /* remove an existing directory whiteout */ |
766 | #ifdef DIAGNOSTIC | |
767 | if (dvp->v_mount->mnt_maxsymlinklen <= 0) | |
768 | panic("ufs_whiteout: old format filesystem"); | |
769 | #endif | |
770 | ||
2b69e610 | 771 | cnp->cn_flags &= ~CNP_DOWHITEOUT; |
984263bc MD |
772 | error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0); |
773 | break; | |
774 | default: | |
775 | panic("ufs_whiteout: unknown op"); | |
776 | } | |
777 | return (error); | |
778 | } | |
779 | ||
780 | /* | |
781 | * Rename system call. | |
782 | * rename("foo", "bar"); | |
783 | * is essentially | |
784 | * unlink("bar"); | |
785 | * link("foo", "bar"); | |
786 | * unlink("foo"); | |
787 | * but ``atomically''. Can't do full commit without saving state in the | |
788 | * inode on disk which isn't feasible at this time. Best we can do is | |
789 | * always guarantee the target exists. | |
790 | * | |
791 | * Basic algorithm is: | |
792 | * | |
793 | * 1) Bump link count on source while we're linking it to the | |
794 | * target. This also ensure the inode won't be deleted out | |
795 | * from underneath us while we work (it may be truncated by | |
796 | * a concurrent `trunc' or `open' for creation). | |
797 | * 2) Link source to destination. If destination already exists, | |
798 | * delete it first. | |
799 | * 3) Unlink source reference to inode if still around. If a | |
800 | * directory was moved and the parent of the destination | |
801 | * is different from the source, patch the ".." entry in the | |
802 | * directory. | |
0973c589 CP |
803 | * |
804 | * ufs_rename(struct vnode *a_fdvp, struct vnode *a_fvp, | |
805 | * struct componentname *a_fcnp, struct vnode *a_tdvp, | |
806 | * struct vnode *a_tvp, struct componentname *a_tcnp) | |
984263bc | 807 | */ |
0961aa92 | 808 | static |
984263bc | 809 | int |
e62afb5f | 810 | ufs_rename(struct vop_old_rename_args *ap) |
984263bc MD |
811 | { |
812 | struct vnode *tvp = ap->a_tvp; | |
3ff2135f | 813 | struct vnode *tdvp = ap->a_tdvp; |
984263bc MD |
814 | struct vnode *fvp = ap->a_fvp; |
815 | struct vnode *fdvp = ap->a_fdvp; | |
816 | struct componentname *tcnp = ap->a_tcnp; | |
817 | struct componentname *fcnp = ap->a_fcnp; | |
984263bc MD |
818 | struct inode *ip, *xp, *dp; |
819 | struct direct newdir; | |
9ab06300 MD |
820 | ino_t oldparent = 0, newparent = 0; |
821 | int doingdirectory = 0; | |
984263bc MD |
822 | int error = 0, ioflag; |
823 | ||
984263bc MD |
824 | /* |
825 | * Check for cross-device rename. | |
826 | */ | |
827 | if ((fvp->v_mount != tdvp->v_mount) || | |
828 | (tvp && (fvp->v_mount != tvp->v_mount))) { | |
829 | error = EXDEV; | |
830 | abortit: | |
831 | if (tdvp == tvp) | |
832 | vrele(tdvp); | |
833 | else | |
834 | vput(tdvp); | |
835 | if (tvp) | |
836 | vput(tvp); | |
837 | vrele(fdvp); | |
838 | vrele(fvp); | |
839 | return (error); | |
840 | } | |
841 | ||
3a907475 | 842 | #if 0 /* handled by kernel now */ |
984263bc MD |
843 | if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) || |
844 | (VTOI(tdvp)->i_flags & APPEND))) { | |
845 | error = EPERM; | |
846 | goto abortit; | |
847 | } | |
3a907475 | 848 | #endif |
984263bc MD |
849 | |
850 | /* | |
851 | * Renaming a file to itself has no effect. The upper layers should | |
852 | * not call us in that case. Temporarily just warn if they do. | |
853 | */ | |
854 | if (fvp == tvp) { | |
086c1d7e | 855 | kprintf("ufs_rename: fvp == tvp (can't happen)\n"); |
984263bc MD |
856 | error = 0; |
857 | goto abortit; | |
858 | } | |
859 | ||
b458d1ab MD |
860 | error = vn_lock(fvp, LK_EXCLUSIVE | LK_FAILRECLAIM); |
861 | if (error) | |
984263bc | 862 | goto abortit; |
fad57d0e MD |
863 | |
864 | /* | |
865 | * Note: now that fvp is locked we have to be sure to unlock it before | |
866 | * using the 'abortit' target. | |
867 | */ | |
984263bc MD |
868 | dp = VTOI(fdvp); |
869 | ip = VTOI(fvp); | |
870 | if (ip->i_nlink >= LINK_MAX) { | |
a11aaa81 | 871 | vn_unlock(fvp); |
984263bc MD |
872 | error = EMLINK; |
873 | goto abortit; | |
874 | } | |
3a907475 | 875 | #if 0 /* handled by kernel now */ |
984263bc MD |
876 | if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) |
877 | || (dp->i_flags & APPEND)) { | |
a11aaa81 | 878 | vn_unlock(fvp); |
984263bc MD |
879 | error = EPERM; |
880 | goto abortit; | |
881 | } | |
3a907475 | 882 | #endif |
984263bc MD |
883 | if ((ip->i_mode & IFMT) == IFDIR) { |
884 | /* | |
885 | * Avoid ".", "..", and aliases of "." for obvious reasons. | |
886 | */ | |
887 | if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') || | |
2b69e610 | 888 | dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT || |
984263bc | 889 | (ip->i_flag & IN_RENAME)) { |
a11aaa81 | 890 | vn_unlock(fvp); |
984263bc MD |
891 | error = EINVAL; |
892 | goto abortit; | |
893 | } | |
894 | ip->i_flag |= IN_RENAME; | |
895 | oldparent = dp->i_number; | |
896 | doingdirectory = 1; | |
897 | } | |
898 | VN_KNOTE(fdvp, NOTE_WRITE); /* XXX right place? */ | |
984263bc MD |
899 | |
900 | /* | |
fad57d0e MD |
901 | * fvp still locked. ip->i_flag has IN_RENAME set if doingdirectory. |
902 | * Cleanup fvp requirements so we can unlock it. | |
903 | * | |
904 | * tvp and tdvp are locked. tvp may be NULL. Now that dp and xp | |
905 | * is setup we can use the 'bad' target if we unlock fvp. We cannot | |
906 | * use the abortit target anymore because of IN_RENAME. | |
984263bc MD |
907 | */ |
908 | dp = VTOI(tdvp); | |
984263bc MD |
909 | if (tvp) |
910 | xp = VTOI(tvp); | |
fad57d0e MD |
911 | else |
912 | xp = NULL; | |
984263bc MD |
913 | |
914 | /* | |
915 | * 1) Bump link count while we're moving stuff | |
916 | * around. If we crash somewhere before | |
917 | * completing our work, the link count | |
918 | * may be wrong, but correctable. | |
919 | */ | |
920 | ip->i_effnlink++; | |
921 | ip->i_nlink++; | |
922 | ip->i_flag |= IN_CHANGE; | |
923 | if (DOINGSOFTDEP(fvp)) | |
924 | softdep_change_linkcnt(ip); | |
ac690a1d | 925 | if ((error = ffs_update(fvp, !(DOINGSOFTDEP(fvp) | |
984263bc | 926 | DOINGASYNC(fvp)))) != 0) { |
a11aaa81 | 927 | vn_unlock(fvp); |
984263bc MD |
928 | goto bad; |
929 | } | |
930 | ||
931 | /* | |
932 | * If ".." must be changed (ie the directory gets a new | |
933 | * parent) then the source directory must not be in the | |
934 | * directory heirarchy above the target, as this would | |
935 | * orphan everything below the source directory. Also | |
936 | * the user must have write permission in the source so | |
937 | * as to be able to change "..". We must repeat the call | |
938 | * to namei, as the parent directory is unlocked by the | |
939 | * call to checkpath(). | |
940 | */ | |
cb66845a | 941 | error = VOP_EACCESS(fvp, VWRITE, tcnp->cn_cred); |
a11aaa81 | 942 | vn_unlock(fvp); |
fad57d0e MD |
943 | |
944 | /* | |
945 | * We are now back to where we were in that fvp, fdvp are unlocked | |
946 | * and tvp, tdvp are locked. tvp may be NULL. IN_RENAME may be | |
947 | * set. Only the bad target or, if we clean up tvp and tdvp, the | |
948 | * out target, may be used. | |
949 | */ | |
984263bc MD |
950 | if (oldparent != dp->i_number) |
951 | newparent = dp->i_number; | |
952 | if (doingdirectory && newparent) { | |
953 | if (error) /* write access check above */ | |
954 | goto bad; | |
fad57d0e MD |
955 | |
956 | /* | |
957 | * Once we start messing with tvp and tdvp we cannot use the | |
958 | * 'bad' target, only finish cleaning tdvp and tvp up and | |
959 | * use the 'out' target. | |
960 | * | |
961 | * This cleans up tvp. | |
962 | */ | |
963 | if (xp != NULL) { | |
984263bc | 964 | vput(tvp); |
fad57d0e MD |
965 | xp = NULL; |
966 | } | |
967 | ||
968 | /* | |
969 | * This is a real mess. ufs_checkpath vput's the target | |
970 | * directory so retain an extra ref and note that tdvp will | |
971 | * lose its lock on return. This leaves us with one good | |
972 | * ref after ufs_checkpath returns. | |
973 | */ | |
974 | vref(tdvp); | |
984263bc | 975 | error = ufs_checkpath(ip, dp, tcnp->cn_cred); |
fad57d0e MD |
976 | tcnp->cn_flags |= CNP_PDIRUNLOCK; |
977 | if (error) { | |
978 | vrele(tdvp); | |
984263bc | 979 | goto out; |
fad57d0e MD |
980 | } |
981 | ||
982 | /* | |
983 | * relookup no longer messes with tdvp's refs. tdvp must be | |
984 | * unlocked on entry and will be locked on a successful | |
985 | * return. | |
986 | */ | |
984263bc | 987 | error = relookup(tdvp, &tvp, tcnp); |
fad57d0e MD |
988 | if (error) { |
989 | if (tcnp->cn_flags & CNP_PDIRUNLOCK) | |
990 | vrele(tdvp); | |
991 | else | |
992 | vput(tdvp); | |
984263bc | 993 | goto out; |
fad57d0e MD |
994 | } |
995 | KKASSERT((tcnp->cn_flags & CNP_PDIRUNLOCK) == 0); | |
984263bc | 996 | dp = VTOI(tdvp); |
984263bc MD |
997 | if (tvp) |
998 | xp = VTOI(tvp); | |
999 | } | |
fad57d0e MD |
1000 | |
1001 | /* | |
1002 | * We are back to fvp, fdvp unlocked, tvp, tdvp locked. tvp may | |
1003 | * be NULL (xp will also be NULL in that case), and IN_RENAME will | |
1004 | * be set if doingdirectory. This means we can use the 'bad' target | |
1005 | * again. | |
1006 | */ | |
1007 | ||
984263bc MD |
1008 | /* |
1009 | * 2) If target doesn't exist, link the target | |
1010 | * to the source and unlink the source. | |
1011 | * Otherwise, rewrite the target directory | |
1012 | * entry to reference the source inode and | |
1013 | * expunge the original entry's existence. | |
1014 | */ | |
1015 | if (xp == NULL) { | |
1016 | if (dp->i_dev != ip->i_dev) | |
1017 | panic("ufs_rename: EXDEV"); | |
1018 | /* | |
1019 | * Account for ".." in new directory. | |
1020 | * When source and destination have the same | |
1021 | * parent we don't fool with the link count. | |
1022 | */ | |
1023 | if (doingdirectory && newparent) { | |
1024 | if ((nlink_t)dp->i_nlink >= LINK_MAX) { | |
1025 | error = EMLINK; | |
1026 | goto bad; | |
1027 | } | |
1028 | dp->i_effnlink++; | |
1029 | dp->i_nlink++; | |
1030 | dp->i_flag |= IN_CHANGE; | |
1031 | if (DOINGSOFTDEP(tdvp)) | |
1032 | softdep_change_linkcnt(dp); | |
ac690a1d | 1033 | error = ffs_update(tdvp, !(DOINGSOFTDEP(tdvp) | |
984263bc MD |
1034 | DOINGASYNC(tdvp))); |
1035 | if (error) | |
1036 | goto bad; | |
1037 | } | |
1038 | ufs_makedirentry(ip, tcnp, &newdir); | |
1039 | error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL); | |
1040 | if (error) { | |
1041 | if (doingdirectory && newparent) { | |
1042 | dp->i_effnlink--; | |
1043 | dp->i_nlink--; | |
1044 | dp->i_flag |= IN_CHANGE; | |
1045 | if (DOINGSOFTDEP(tdvp)) | |
1046 | softdep_change_linkcnt(dp); | |
ac690a1d | 1047 | (void)ffs_update(tdvp, 1); |
984263bc MD |
1048 | } |
1049 | goto bad; | |
1050 | } | |
1051 | VN_KNOTE(tdvp, NOTE_WRITE); | |
1052 | vput(tdvp); | |
1053 | } else { | |
1054 | if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) | |
1055 | panic("ufs_rename: EXDEV"); | |
1056 | /* | |
1057 | * Short circuit rename(foo, foo). | |
1058 | */ | |
1059 | if (xp->i_number == ip->i_number) | |
1060 | panic("ufs_rename: same file"); | |
1061 | /* | |
1062 | * If the parent directory is "sticky", then the user must | |
1063 | * own the parent directory, or the destination of the rename, | |
1064 | * otherwise the destination may not be changed (except by | |
1065 | * root). This implements append-only directories. | |
1066 | */ | |
1067 | if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 && | |
1068 | tcnp->cn_cred->cr_uid != dp->i_uid && | |
1069 | xp->i_uid != tcnp->cn_cred->cr_uid) { | |
1070 | error = EPERM; | |
1071 | goto bad; | |
1072 | } | |
1073 | /* | |
1074 | * Target must be empty if a directory and have no links | |
1075 | * to it. Also, ensure source and target are compatible | |
1076 | * (both directories, or both not directories). | |
8e005a45 MD |
1077 | * |
1078 | * Purge the file or directory being replaced from the | |
1079 | * nameccache. | |
984263bc MD |
1080 | */ |
1081 | if ((xp->i_mode&IFMT) == IFDIR) { | |
1082 | if ((xp->i_effnlink > 2) || | |
1083 | !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) { | |
1084 | error = ENOTEMPTY; | |
1085 | goto bad; | |
1086 | } | |
1087 | if (!doingdirectory) { | |
1088 | error = ENOTDIR; | |
1089 | goto bad; | |
1090 | } | |
fad57d0e | 1091 | /* cache_purge removed - handled by VFS compat layer */ |
8e005a45 | 1092 | } else if (doingdirectory == 0) { |
fad57d0e | 1093 | /* cache_purge removed - handled by VFS compat layer */ |
8e005a45 | 1094 | } else { |
984263bc MD |
1095 | error = EISDIR; |
1096 | goto bad; | |
1097 | } | |
9ab06300 MD |
1098 | /* |
1099 | * note: inode passed to ufs_dirrewrite() is 0 for a | |
1100 | * non-directory file rename, 1 for a directory rename | |
1101 | * in the same directory, and > 1 for an inode representing | |
1102 | * the new directory. | |
1103 | */ | |
984263bc MD |
1104 | error = ufs_dirrewrite(dp, xp, ip->i_number, |
1105 | IFTODT(ip->i_mode), | |
9ab06300 MD |
1106 | (doingdirectory && newparent) ? |
1107 | newparent : (ino_t)doingdirectory); | |
984263bc MD |
1108 | if (error) |
1109 | goto bad; | |
1110 | if (doingdirectory) { | |
1111 | if (!newparent) { | |
1112 | dp->i_effnlink--; | |
1113 | if (DOINGSOFTDEP(tdvp)) | |
1114 | softdep_change_linkcnt(dp); | |
1115 | } | |
1116 | xp->i_effnlink--; | |
1117 | if (DOINGSOFTDEP(tvp)) | |
1118 | softdep_change_linkcnt(xp); | |
1119 | } | |
1120 | if (doingdirectory && !DOINGSOFTDEP(tvp)) { | |
1121 | /* | |
1122 | * Truncate inode. The only stuff left in the directory | |
1123 | * is "." and "..". The "." reference is inconsequential | |
1124 | * since we are quashing it. We have removed the "." | |
1125 | * reference and the reference in the parent directory, | |
1126 | * but there may be other hard links. The soft | |
1127 | * dependency code will arrange to do these operations | |
1128 | * after the parent directory entry has been deleted on | |
1129 | * disk, so when running with that code we avoid doing | |
1130 | * them now. | |
1131 | */ | |
1132 | if (!newparent) { | |
1133 | dp->i_nlink--; | |
1134 | dp->i_flag |= IN_CHANGE; | |
1135 | } | |
1136 | xp->i_nlink--; | |
1137 | xp->i_flag |= IN_CHANGE; | |
1138 | ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC; | |
ac690a1d | 1139 | error = ffs_truncate(tvp, (off_t)0, ioflag, |
87de5057 MD |
1140 | tcnp->cn_cred); |
1141 | if (error) | |
984263bc MD |
1142 | goto bad; |
1143 | } | |
1144 | VN_KNOTE(tdvp, NOTE_WRITE); | |
1145 | vput(tdvp); | |
1146 | VN_KNOTE(tvp, NOTE_DELETE); | |
1147 | vput(tvp); | |
1148 | xp = NULL; | |
1149 | } | |
1150 | ||
fad57d0e MD |
1151 | /* |
1152 | * tvp and tdvp have been cleaned up. only fvp and fdvp (both | |
1153 | * unlocked) remain. We are about to overwrite fvp but we have to | |
1154 | * keep 'ip' intact so we cannot release the old fvp, which is still | |
1155 | * refd and accessible via ap->a_fvp. | |
1156 | * | |
1157 | * This means we cannot use either 'bad' or 'out' to cleanup any | |
1158 | * more. | |
1159 | */ | |
1160 | ||
984263bc MD |
1161 | /* |
1162 | * 3) Unlink the source. | |
1163 | */ | |
2b69e610 | 1164 | fcnp->cn_flags &= ~CNP_MODMASK; |
fad57d0e | 1165 | fcnp->cn_flags |= CNP_LOCKPARENT; |
984263bc | 1166 | error = relookup(fdvp, &fvp, fcnp); |
fad57d0e | 1167 | if (error || fvp == NULL) { |
984263bc | 1168 | /* |
fad57d0e MD |
1169 | * From name has disappeared. IN_RENAME will not be set if |
1170 | * we get past the panic so we don't have to clean it up. | |
984263bc MD |
1171 | */ |
1172 | if (doingdirectory) | |
1173 | panic("ufs_rename: lost dir entry"); | |
1174 | vrele(ap->a_fvp); | |
fad57d0e MD |
1175 | if (fcnp->cn_flags & CNP_PDIRUNLOCK) |
1176 | vrele(fdvp); | |
1177 | else | |
1178 | vput(fdvp); | |
1179 | return(0); | |
984263bc | 1180 | } |
fad57d0e MD |
1181 | KKASSERT((fcnp->cn_flags & CNP_PDIRUNLOCK) == 0); |
1182 | ||
1183 | /* | |
1184 | * fdvp and fvp are locked. | |
1185 | */ | |
1186 | xp = VTOI(fvp); | |
1187 | dp = VTOI(fdvp); | |
1188 | ||
984263bc MD |
1189 | /* |
1190 | * Ensure that the directory entry still exists and has not | |
1191 | * changed while the new name has been entered. If the source is | |
1192 | * a file then the entry may have been unlinked or renamed. In | |
1193 | * either case there is no further work to be done. If the source | |
1194 | * is a directory then it cannot have been rmdir'ed; the IN_RENAME | |
1195 | * flag ensures that it cannot be moved by another rename or removed | |
fad57d0e | 1196 | * by a rmdir. Cleanup IN_RENAME. |
984263bc MD |
1197 | */ |
1198 | if (xp != ip) { | |
1199 | if (doingdirectory) | |
1200 | panic("ufs_rename: lost dir entry"); | |
1201 | } else { | |
1202 | /* | |
1203 | * If the source is a directory with a | |
1204 | * new parent, the link count of the old | |
1205 | * parent directory must be decremented | |
1206 | * and ".." set to point to the new parent. | |
1207 | */ | |
1208 | if (doingdirectory && newparent) { | |
1209 | xp->i_offset = mastertemplate.dot_reclen; | |
1210 | ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0); | |
fad57d0e | 1211 | /* cache_purge removed - handled by VFS compat layer */ |
984263bc MD |
1212 | } |
1213 | error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0); | |
1214 | xp->i_flag &= ~IN_RENAME; | |
1215 | } | |
8e005a45 | 1216 | |
984263bc | 1217 | VN_KNOTE(fvp, NOTE_RENAME); |
fad57d0e MD |
1218 | vput(fdvp); |
1219 | vput(fvp); | |
984263bc MD |
1220 | vrele(ap->a_fvp); |
1221 | return (error); | |
1222 | ||
1223 | bad: | |
1224 | if (xp) | |
1225 | vput(ITOV(xp)); | |
1226 | vput(ITOV(dp)); | |
1227 | out: | |
1228 | if (doingdirectory) | |
1229 | ip->i_flag &= ~IN_RENAME; | |
b458d1ab | 1230 | if (vn_lock(fvp, LK_EXCLUSIVE | LK_FAILRECLAIM) == 0) { |
984263bc MD |
1231 | ip->i_effnlink--; |
1232 | ip->i_nlink--; | |
1233 | ip->i_flag |= IN_CHANGE; | |
1234 | ip->i_flag &= ~IN_RENAME; | |
1235 | if (DOINGSOFTDEP(fvp)) | |
1236 | softdep_change_linkcnt(ip); | |
1237 | vput(fvp); | |
fad57d0e | 1238 | } else { |
984263bc | 1239 | vrele(fvp); |
fad57d0e | 1240 | } |
984263bc MD |
1241 | return (error); |
1242 | } | |
1243 | ||
1244 | /* | |
1245 | * Mkdir system call | |
0973c589 CP |
1246 | * |
1247 | * ufs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp, | |
1248 | * struct componentname *a_cnp, struct vattr *a_vap) | |
984263bc | 1249 | */ |
0961aa92 | 1250 | static |
984263bc | 1251 | int |
e62afb5f | 1252 | ufs_mkdir(struct vop_old_mkdir_args *ap) |
984263bc | 1253 | { |
3ff2135f RG |
1254 | struct vnode *dvp = ap->a_dvp; |
1255 | struct vattr *vap = ap->a_vap; | |
1256 | struct componentname *cnp = ap->a_cnp; | |
1257 | struct inode *ip, *dp; | |
984263bc MD |
1258 | struct vnode *tvp; |
1259 | struct buf *bp; | |
1260 | struct dirtemplate dirtemplate, *dtp; | |
1261 | struct direct newdir; | |
1262 | int error, dmode; | |
1263 | long blkoff; | |
1264 | ||
984263bc MD |
1265 | dp = VTOI(dvp); |
1266 | if ((nlink_t)dp->i_nlink >= LINK_MAX) { | |
1267 | error = EMLINK; | |
1268 | goto out; | |
1269 | } | |
1270 | dmode = vap->va_mode & 0777; | |
1271 | dmode |= IFDIR; | |
1272 | /* | |
1273 | * Must simulate part of ufs_makeinode here to acquire the inode, | |
1274 | * but not have it entered in the parent directory. The entry is | |
1275 | * made later after writing "." and ".." entries. | |
1276 | */ | |
ac690a1d | 1277 | error = ffs_valloc(dvp, dmode, cnp->cn_cred, &tvp); |
984263bc MD |
1278 | if (error) |
1279 | goto out; | |
1280 | ip = VTOI(tvp); | |
1281 | ip->i_gid = dp->i_gid; | |
1282 | #ifdef SUIDDIR | |
1283 | { | |
1284 | #ifdef QUOTA | |
1285 | struct ucred ucred, *ucp; | |
1286 | ucp = cnp->cn_cred; | |
1287 | #endif | |
1288 | /* | |
1289 | * If we are hacking owners here, (only do this where told to) | |
1290 | * and we are not giving it TO root, (would subvert quotas) | |
1291 | * then go ahead and give it to the other user. | |
1292 | * The new directory also inherits the SUID bit. | |
1293 | * If user's UID and dir UID are the same, | |
1294 | * 'give it away' so that the SUID is still forced on. | |
1295 | */ | |
1296 | if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) && | |
1297 | (dp->i_mode & ISUID) && dp->i_uid) { | |
1298 | dmode |= ISUID; | |
1299 | ip->i_uid = dp->i_uid; | |
1300 | #ifdef QUOTA | |
1301 | if (dp->i_uid != cnp->cn_cred->cr_uid) { | |
1302 | /* | |
1303 | * Make sure the correct user gets charged | |
1304 | * for the space. | |
1305 | * Make a dummy credential for the victim. | |
1306 | * XXX This seems to never be accessed out of | |
1307 | * our context so a stack variable is ok. | |
1308 | */ | |
1309 | ucred.cr_ref = 1; | |
1310 | ucred.cr_uid = ip->i_uid; | |
1311 | ucred.cr_ngroups = 1; | |
1312 | ucred.cr_groups[0] = dp->i_gid; | |
1313 | ucp = &ucred; | |
1314 | } | |
1315 | #endif | |
1316 | } else | |
1317 | ip->i_uid = cnp->cn_cred->cr_uid; | |
1318 | #ifdef QUOTA | |
50e58362 MD |
1319 | if ((error = ufs_getinoquota(ip)) || |
1320 | (error = ufs_chkiq(ip, 1, ucp, 0))) { | |
ac690a1d | 1321 | ffs_vfree(tvp, ip->i_number, dmode); |
984263bc MD |
1322 | vput(tvp); |
1323 | return (error); | |
1324 | } | |
1325 | #endif | |
1326 | } | |
1327 | #else /* !SUIDDIR */ | |
1328 | ip->i_uid = cnp->cn_cred->cr_uid; | |
1329 | #ifdef QUOTA | |
50e58362 MD |
1330 | if ((error = ufs_getinoquota(ip)) || |
1331 | (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) { | |
ac690a1d | 1332 | ffs_vfree(tvp, ip->i_number, dmode); |
984263bc MD |
1333 | vput(tvp); |
1334 | return (error); | |
1335 | } | |
1336 | #endif | |
1337 | #endif /* !SUIDDIR */ | |
1338 | ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; | |
1339 | ip->i_mode = dmode; | |
1340 | tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */ | |
1341 | ip->i_effnlink = 2; | |
1342 | ip->i_nlink = 2; | |
1343 | if (DOINGSOFTDEP(tvp)) | |
1344 | softdep_change_linkcnt(ip); | |
2b69e610 | 1345 | if (cnp->cn_flags & CNP_ISWHITEOUT) |
984263bc MD |
1346 | ip->i_flags |= UF_OPAQUE; |
1347 | ||
1348 | /* | |
1349 | * Bump link count in parent directory to reflect work done below. | |
1350 | * Should be done before reference is created so cleanup is | |
1351 | * possible if we crash. | |
1352 | */ | |
1353 | dp->i_effnlink++; | |
1354 | dp->i_nlink++; | |
1355 | dp->i_flag |= IN_CHANGE; | |
1356 | if (DOINGSOFTDEP(dvp)) | |
1357 | softdep_change_linkcnt(dp); | |
ac690a1d | 1358 | error = ffs_update(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp))); |
984263bc MD |
1359 | if (error) |
1360 | goto bad; | |
1361 | ||
83a5effe MD |
1362 | /* |
1363 | * The vnode must have a VM object in order to issue buffer cache | |
1364 | * ops on it. | |
1365 | */ | |
b0d18f7d | 1366 | vinitvmio(tvp, DIRBLKSIZ, DIRBLKSIZ, -1); |
83a5effe | 1367 | |
984263bc MD |
1368 | /* |
1369 | * Initialize directory with "." and ".." from static template. | |
1370 | */ | |
83a5effe | 1371 | if (dvp->v_mount->mnt_maxsymlinklen > 0) |
984263bc MD |
1372 | dtp = &mastertemplate; |
1373 | else | |
1374 | dtp = (struct dirtemplate *)&omastertemplate; | |
1375 | dirtemplate = *dtp; | |
1376 | dirtemplate.dot_ino = ip->i_number; | |
1377 | dirtemplate.dotdot_ino = dp->i_number; | |
3bb7eedb | 1378 | nvnode_pager_setsize(tvp, DIRBLKSIZ, DIRBLKSIZ, -1); |
83a5effe MD |
1379 | error = VOP_BALLOC(tvp, 0LL, DIRBLKSIZ, cnp->cn_cred, B_CLRBUF, &bp); |
1380 | if (error) | |
984263bc MD |
1381 | goto bad; |
1382 | ip->i_size = DIRBLKSIZ; | |
1383 | ip->i_flag |= IN_CHANGE | IN_UPDATE; | |
984263bc MD |
1384 | bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate); |
1385 | if (DOINGSOFTDEP(tvp)) { | |
1386 | /* | |
1387 | * Ensure that the entire newly allocated block is a | |
1388 | * valid directory so that future growth within the | |
1389 | * block does not have to ensure that the block is | |
1390 | * written before the inode. | |
1391 | */ | |
1392 | blkoff = DIRBLKSIZ; | |
1393 | while (blkoff < bp->b_bcount) { | |
1394 | ((struct direct *) | |
1395 | (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ; | |
1396 | blkoff += DIRBLKSIZ; | |
1397 | } | |
1398 | } | |
ac690a1d | 1399 | if ((error = ffs_update(tvp, !(DOINGSOFTDEP(tvp) | |
984263bc | 1400 | DOINGASYNC(tvp)))) != 0) { |
62cfda27 | 1401 | bwrite(bp); |
984263bc MD |
1402 | goto bad; |
1403 | } | |
1404 | /* | |
1405 | * Directory set up, now install its entry in the parent directory. | |
1406 | * | |
1407 | * If we are not doing soft dependencies, then we must write out the | |
1408 | * buffer containing the new directory body before entering the new | |
1409 | * name in the parent. If we are doing soft dependencies, then the | |
1410 | * buffer containing the new directory body will be passed to and | |
1411 | * released in the soft dependency code after the code has attached | |
1412 | * an appropriate ordering dependency to the buffer which ensures that | |
1413 | * the buffer is written before the new name is written in the parent. | |
1414 | */ | |
1415 | if (DOINGASYNC(dvp)) | |
1416 | bdwrite(bp); | |
62cfda27 | 1417 | else if (!DOINGSOFTDEP(dvp) && (error = bwrite(bp)) != 0) |
984263bc MD |
1418 | goto bad; |
1419 | ufs_makedirentry(ip, cnp, &newdir); | |
1420 | error = ufs_direnter(dvp, tvp, &newdir, cnp, bp); | |
1421 | ||
1422 | bad: | |
1423 | if (error == 0) { | |
1424 | VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK); | |
1425 | *ap->a_vpp = tvp; | |
1426 | } else { | |
1427 | dp->i_effnlink--; | |
1428 | dp->i_nlink--; | |
1429 | dp->i_flag |= IN_CHANGE; | |
1430 | if (DOINGSOFTDEP(dvp)) | |
1431 | softdep_change_linkcnt(dp); | |
1432 | /* | |
1433 | * No need to do an explicit VOP_TRUNCATE here, vrele will | |
1434 | * do this for us because we set the link count to 0. | |
1435 | */ | |
1436 | ip->i_effnlink = 0; | |
1437 | ip->i_nlink = 0; | |
1438 | ip->i_flag |= IN_CHANGE; | |
1439 | if (DOINGSOFTDEP(tvp)) | |
1440 | softdep_change_linkcnt(ip); | |
1441 | vput(tvp); | |
1442 | } | |
1443 | out: | |
1444 | return (error); | |
1445 | } | |
1446 | ||
1447 | /* | |
1448 | * Rmdir system call. | |
0973c589 CP |
1449 | * |
1450 | * ufs_rmdir(struct vnode *a_dvp, struct vnode *a_vp, | |
1451 | * struct componentname *a_cnp) | |
984263bc | 1452 | */ |
0961aa92 | 1453 | static |
984263bc | 1454 | int |
e62afb5f | 1455 | ufs_rmdir(struct vop_old_rmdir_args *ap) |
984263bc MD |
1456 | { |
1457 | struct vnode *vp = ap->a_vp; | |
1458 | struct vnode *dvp = ap->a_dvp; | |
1459 | struct componentname *cnp = ap->a_cnp; | |
1460 | struct inode *ip, *dp; | |
1461 | int error, ioflag; | |
1462 | ||
1463 | ip = VTOI(vp); | |
1464 | dp = VTOI(dvp); | |
1465 | ||
1466 | /* | |
1467 | * Do not remove a directory that is in the process of being renamed. | |
1468 | * Verify the directory is empty (and valid). Rmdir ".." will not be | |
1469 | * valid since ".." will contain a reference to the current directory | |
1470 | * and thus be non-empty. Do not allow the removal of mounted on | |
1471 | * directories (this can happen when an NFS exported filesystem | |
1472 | * tries to remove a locally mounted on directory). | |
1473 | */ | |
1474 | error = 0; | |
1475 | if (ip->i_flag & IN_RENAME) { | |
1476 | error = EINVAL; | |
1477 | goto out; | |
1478 | } | |
1479 | if (ip->i_effnlink != 2 || | |
1480 | !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) { | |
1481 | error = ENOTEMPTY; | |
1482 | goto out; | |
1483 | } | |
3a907475 | 1484 | #if 0 /* handled by kernel now */ |
984263bc MD |
1485 | if ((dp->i_flags & APPEND) |
1486 | || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) { | |
1487 | error = EPERM; | |
1488 | goto out; | |
1489 | } | |
3a907475 | 1490 | #endif |
984263bc MD |
1491 | /* |
1492 | * Delete reference to directory before purging | |
1493 | * inode. If we crash in between, the directory | |
1494 | * will be reattached to lost+found, | |
1495 | */ | |
1496 | dp->i_effnlink--; | |
1497 | ip->i_effnlink--; | |
1498 | if (DOINGSOFTDEP(vp)) { | |
1499 | softdep_change_linkcnt(dp); | |
1500 | softdep_change_linkcnt(ip); | |
1501 | } | |
1502 | error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1); | |
1503 | if (error) { | |
1504 | dp->i_effnlink++; | |
1505 | ip->i_effnlink++; | |
1506 | if (DOINGSOFTDEP(vp)) { | |
1507 | softdep_change_linkcnt(dp); | |
1508 | softdep_change_linkcnt(ip); | |
1509 | } | |
1510 | goto out; | |
1511 | } | |
1512 | VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK); | |
984263bc MD |
1513 | /* |
1514 | * Truncate inode. The only stuff left in the directory is "." and | |
1515 | * "..". The "." reference is inconsequential since we are quashing | |
1516 | * it. The soft dependency code will arrange to do these operations | |
1517 | * after the parent directory entry has been deleted on disk, so | |
1518 | * when running with that code we avoid doing them now. | |
1519 | */ | |
1520 | if (!DOINGSOFTDEP(vp)) { | |
1521 | dp->i_nlink--; | |
1522 | dp->i_flag |= IN_CHANGE; | |
1523 | ip->i_nlink--; | |
1524 | ip->i_flag |= IN_CHANGE; | |
1525 | ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC; | |
ac690a1d | 1526 | error = ffs_truncate(vp, (off_t)0, ioflag, cnp->cn_cred); |
984263bc | 1527 | } |
fad57d0e | 1528 | /* cache_purge removed - handled by VFS compat layer */ |
984263bc MD |
1529 | #ifdef UFS_DIRHASH |
1530 | /* Kill any active hash; i_effnlink == 0, so it will not come back. */ | |
1531 | if (ip->i_dirhash != NULL) | |
1532 | ufsdirhash_free(ip); | |
1533 | #endif | |
1534 | out: | |
1535 | VN_KNOTE(vp, NOTE_DELETE); | |
1536 | return (error); | |
1537 | } | |
1538 | ||
1539 | /* | |
1540 | * symlink -- make a symbolic link | |
0973c589 CP |
1541 | * |
1542 | * ufs_symlink(struct vnode *a_dvp, struct vnode **a_vpp, | |
1543 | * struct componentname *a_cnp, struct vattr *a_vap, | |
1544 | * char *a_target) | |
984263bc | 1545 | */ |
0961aa92 | 1546 | static |
984263bc | 1547 | int |
e62afb5f | 1548 | ufs_symlink(struct vop_old_symlink_args *ap) |
984263bc | 1549 | { |
3ff2135f RG |
1550 | struct vnode *vp, **vpp = ap->a_vpp; |
1551 | struct inode *ip; | |
984263bc MD |
1552 | int len, error; |
1553 | ||
1554 | error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp, | |
83a5effe | 1555 | vpp, ap->a_cnp); |
984263bc MD |
1556 | if (error) |
1557 | return (error); | |
1558 | VN_KNOTE(ap->a_dvp, NOTE_WRITE); | |
1559 | vp = *vpp; | |
1560 | len = strlen(ap->a_target); | |
1561 | if (len < vp->v_mount->mnt_maxsymlinklen) { | |
1562 | ip = VTOI(vp); | |
1563 | bcopy(ap->a_target, (char *)ip->i_shortlink, len); | |
1564 | ip->i_size = len; | |
1565 | ip->i_flag |= IN_CHANGE | IN_UPDATE; | |
83a5effe MD |
1566 | } else { |
1567 | /* | |
1568 | * Make sure we have a VM object in order to use | |
1569 | * the buffer cache. | |
1570 | */ | |
1571 | if (vp->v_object == NULL) | |
b0d18f7d | 1572 | vinitvmio(vp, 0, PAGE_SIZE, -1); |
984263bc | 1573 | error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0, |
83a5effe | 1574 | UIO_SYSSPACE, IO_NODELOCKED, |
87de5057 | 1575 | ap->a_cnp->cn_cred, NULL); |
83a5effe | 1576 | } |
984263bc MD |
1577 | if (error) |
1578 | vput(vp); | |
1579 | return (error); | |
1580 | } | |
1581 | ||
1582 | /* | |
1583 | * Vnode op for reading directories. | |
1584 | * | |
0973c589 | 1585 | * ufs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred, |
84009d92 | 1586 | * int *a_eofflag, int *ncookies, off_t **a_cookies) |
984263bc | 1587 | */ |
0961aa92 | 1588 | static |
984263bc | 1589 | int |
0973c589 | 1590 | ufs_readdir(struct vop_readdir_args *ap) |
984263bc | 1591 | { |
3ff2135f | 1592 | struct uio *uio = ap->a_uio; |
7f8e5940 | 1593 | struct vnode *vp = ap->a_vp; |
fb0466c9 MD |
1594 | struct direct *dp; |
1595 | struct buf *bp; | |
1596 | int retval; | |
1597 | int error; | |
1598 | int offset; /* offset into buffer cache buffer */ | |
1599 | int eoffset; /* end of buffer clipped to file EOF */ | |
1600 | int pickup; /* pickup point */ | |
7f8e5940 | 1601 | int ncookies; |
fb0466c9 | 1602 | int cookie_index; |
84009d92 | 1603 | off_t *cookies; |
984263bc | 1604 | |
fb0466c9 MD |
1605 | if (uio->uio_offset < 0) |
1606 | return (EINVAL); | |
e088dc32 | 1607 | /* |
fb0466c9 MD |
1608 | * Guess the number of cookies needed. Make sure we compute at |
1609 | * least 1, and no more then a reasonable limit. | |
e088dc32 | 1610 | */ |
fb0466c9 MD |
1611 | if (ap->a_ncookies) { |
1612 | ncookies = uio->uio_resid / 16 + 1; | |
1613 | if (ncookies > 1024) | |
1614 | ncookies = 1024; | |
84009d92 | 1615 | cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK); |
fb0466c9 MD |
1616 | } else { |
1617 | ncookies = -1; /* force conditionals below */ | |
1618 | cookies = NULL; | |
1619 | } | |
1620 | cookie_index = 0; | |
1621 | ||
b458d1ab MD |
1622 | error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM); |
1623 | if (error) | |
885ecb13 MD |
1624 | return (error); |
1625 | ||
fb0466c9 MD |
1626 | /* |
1627 | * Past or at EOF | |
1628 | */ | |
1629 | if (uio->uio_offset >= VTOI(vp)->i_size) { | |
1630 | if (ap->a_eofflag) | |
1631 | *ap->a_eofflag = 1; | |
1632 | if (ap->a_ncookies) { | |
1633 | *ap->a_ncookies = cookie_index; | |
1634 | *ap->a_cookies = cookies; | |
1635 | } | |
885ecb13 | 1636 | goto done; |
fb0466c9 MD |
1637 | } |
1638 | ||
1639 | /* | |
1640 | * Loop until we run out of cookies, we run out of user buffer, | |
1641 | * or we hit the directory EOF. | |
1642 | * | |
1643 | * Always start scans at the beginning of the buffer, don't trust | |
1644 | * the offset supplied by userland. | |
1645 | */ | |
9ba4b517 | 1646 | while ((error = ffs_blkatoff_ra(vp, uio->uio_offset, NULL, &bp, 2)) == 0) { |
fb0466c9 MD |
1647 | pickup = (int)(uio->uio_offset - bp->b_loffset); |
1648 | offset = 0; | |
1649 | retval = 0; | |
1650 | if (bp->b_loffset + bp->b_bcount > VTOI(vp)->i_size) | |
1651 | eoffset = (int)(VTOI(vp)->i_size - bp->b_loffset); | |
1652 | else | |
1653 | eoffset = bp->b_bcount; | |
1654 | ||
1655 | while (offset < eoffset) { | |
1656 | dp = (struct direct *)(bp->b_data + offset); | |
1657 | if (dp->d_reclen <= 0 || (dp->d_reclen & 3) || | |
1658 | offset + dp->d_reclen > bp->b_bcount) { | |
82fd1984 JS |
1659 | error = EIO; |
1660 | break; | |
1661 | } | |
fb0466c9 MD |
1662 | if (offsetof(struct direct, d_name[dp->d_namlen]) > dp->d_reclen) { |
1663 | error = EIO; | |
1664 | break; | |
1665 | } | |
1666 | if (offset < pickup) { | |
1667 | offset += dp->d_reclen; | |
1668 | continue; | |
1669 | } | |
e088dc32 | 1670 | #if BYTE_ORDER == LITTLE_ENDIAN |
7f8e5940 | 1671 | if (OFSFMT(vp)) { |
82fd1984 JS |
1672 | retval = vop_write_dirent(&error, uio, |
1673 | dp->d_ino, dp->d_namlen, dp->d_type, | |
1674 | dp->d_name); | |
e088dc32 JS |
1675 | } else |
1676 | #endif | |
1677 | { | |
82fd1984 JS |
1678 | retval = vop_write_dirent(&error, uio, |
1679 | dp->d_ino, dp->d_type, dp->d_namlen, | |
1680 | dp->d_name); | |
e088dc32 | 1681 | } |
82fd1984 | 1682 | if (retval) |
e088dc32 | 1683 | break; |
84009d92 MD |
1684 | if (cookies) |
1685 | cookies[cookie_index] = bp->b_loffset + offset; | |
fb0466c9 MD |
1686 | ++cookie_index; |
1687 | offset += dp->d_reclen; | |
1688 | if (cookie_index == ncookies) | |
1689 | break; | |
1690 | } | |
1691 | ||
1692 | /* | |
1693 | * This will align the next loop to the beginning of the | |
1694 | * next block, and pickup will calculate to 0. | |
1695 | */ | |
1696 | uio->uio_offset = bp->b_loffset + offset; | |
1697 | brelse(bp); | |
1698 | ||
1699 | if (retval || error || cookie_index == ncookies || | |
1700 | uio->uio_offset >= VTOI(vp)->i_size) { | |
1701 | break; | |
984263bc | 1702 | } |
984263bc | 1703 | } |
984263bc | 1704 | if (ap->a_eofflag) |
7f8e5940 | 1705 | *ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset; |
fb0466c9 MD |
1706 | |
1707 | /* | |
1708 | * Report errors only if we didn't manage to read anything | |
1709 | */ | |
1710 | if (error && cookie_index == 0) { | |
1711 | if (cookies) { | |
efda3bd0 | 1712 | kfree(cookies, M_TEMP); |
fb0466c9 MD |
1713 | *ap->a_ncookies = 0; |
1714 | *ap->a_cookies = NULL; | |
1715 | } | |
1716 | } else { | |
1717 | error = 0; | |
1718 | if (cookies) { | |
1719 | *ap->a_ncookies = cookie_index; | |
1720 | *ap->a_cookies = cookies; | |
1721 | } | |
1722 | } | |
885ecb13 MD |
1723 | done: |
1724 | vn_unlock(vp); | |
e088dc32 | 1725 | return (error); |
984263bc MD |
1726 | } |
1727 | ||
1728 | /* | |
1729 | * Return target name of a symbolic link | |
0973c589 CP |
1730 | * |
1731 | * ufs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred) | |
984263bc | 1732 | */ |
0961aa92 | 1733 | static |
984263bc | 1734 | int |
0973c589 | 1735 | ufs_readlink(struct vop_readlink_args *ap) |
984263bc | 1736 | { |
3ff2135f RG |
1737 | struct vnode *vp = ap->a_vp; |
1738 | struct inode *ip = VTOI(vp); | |
984263bc MD |
1739 | int isize; |
1740 | ||
1741 | isize = ip->i_size; | |
1742 | if ((isize < vp->v_mount->mnt_maxsymlinklen) || | |
f719c866 | 1743 | (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */ |
984263bc MD |
1744 | uiomove((char *)ip->i_shortlink, isize, ap->a_uio); |
1745 | return (0); | |
1746 | } | |
83a5effe MD |
1747 | |
1748 | /* | |
1749 | * Perform the equivalent of an OPEN on vp so we can issue a | |
1750 | * VOP_READ. | |
1751 | */ | |
984263bc MD |
1752 | return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred)); |
1753 | } | |
1754 | ||
1755 | /* | |
1756 | * Calculate the logical to physical mapping if not done already, | |
1757 | * then call the device strategy routine. | |
1758 | * | |
1759 | * In order to be able to swap to a file, the VOP_BMAP operation may not | |
1760 | * deadlock on memory. See ufs_bmap() for details. | |
0973c589 | 1761 | * |
81b5c339 | 1762 | * ufs_strategy(struct vnode *a_vp, struct bio *a_bio) |
984263bc | 1763 | */ |
0961aa92 | 1764 | static |
984263bc | 1765 | int |
0973c589 | 1766 | ufs_strategy(struct vop_strategy_args *ap) |
984263bc | 1767 | { |
81b5c339 MD |
1768 | struct bio *bio = ap->a_bio; |
1769 | struct bio *nbio; | |
1770 | struct buf *bp = bio->bio_buf; | |
3ff2135f RG |
1771 | struct vnode *vp = ap->a_vp; |
1772 | struct inode *ip; | |
984263bc MD |
1773 | int error; |
1774 | ||
1775 | ip = VTOI(vp); | |
1776 | if (vp->v_type == VBLK || vp->v_type == VCHR) | |
1777 | panic("ufs_strategy: spec"); | |
81b5c339 | 1778 | nbio = push_bio(bio); |
54078292 | 1779 | if (nbio->bio_offset == NOOFFSET) { |
08daea96 | 1780 | error = VOP_BMAP(vp, bio->bio_offset, &nbio->bio_offset, |
e92ca23a | 1781 | NULL, NULL, bp->b_cmd); |
984263bc MD |
1782 | if (error) { |
1783 | bp->b_error = error; | |
1784 | bp->b_flags |= B_ERROR; | |
81b5c339 MD |
1785 | /* I/O was never started on nbio, must biodone(bio) */ |
1786 | biodone(bio); | |
984263bc MD |
1787 | return (error); |
1788 | } | |
54078292 | 1789 | if (nbio->bio_offset == NOOFFSET) |
984263bc MD |
1790 | vfs_bio_clrbuf(bp); |
1791 | } | |
54078292 | 1792 | if (nbio->bio_offset == NOOFFSET) { |
1f97d073 MD |
1793 | /* |
1794 | * We hit a hole in the file. The buffer has been zero-filled | |
1795 | * so just biodone() it. | |
1796 | */ | |
81b5c339 | 1797 | biodone(bio); |
1f97d073 MD |
1798 | } else { |
1799 | vn_strategy(ip->i_devvp, nbio); | |
984263bc | 1800 | } |
984263bc MD |
1801 | return (0); |
1802 | } | |
1803 | ||
1804 | /* | |
1805 | * Print out the contents of an inode. | |
0973c589 CP |
1806 | * |
1807 | * ufs_print(struct vnode *a_vp) | |
984263bc | 1808 | */ |
0961aa92 | 1809 | static |
984263bc | 1810 | int |
0973c589 | 1811 | ufs_print(struct vop_print_args *ap) |
984263bc | 1812 | { |
3ff2135f RG |
1813 | struct vnode *vp = ap->a_vp; |
1814 | struct inode *ip = VTOI(vp); | |
984263bc | 1815 | |
086c1d7e | 1816 | kprintf("tag VT_UFS, ino %lu, on dev %s (%d, %d)", |
984263bc MD |
1817 | (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev), |
1818 | minor(ip->i_dev)); | |
1819 | if (vp->v_type == VFIFO) | |
1820 | fifo_printinfo(vp); | |
3446c007 | 1821 | lockmgr_printinfo(&vp->v_lock); |
086c1d7e | 1822 | kprintf("\n"); |
984263bc MD |
1823 | return (0); |
1824 | } | |
1825 | ||
984263bc MD |
1826 | /* |
1827 | * Read wrapper for fifos. | |
0973c589 CP |
1828 | * |
1829 | * ufsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, | |
1830 | * struct ucred *a_cred) | |
984263bc | 1831 | */ |
0961aa92 | 1832 | static |
984263bc | 1833 | int |
0973c589 | 1834 | ufsfifo_read(struct vop_read_args *ap) |
984263bc MD |
1835 | { |
1836 | int error, resid; | |
1837 | struct inode *ip; | |
1838 | struct uio *uio; | |
1839 | ||
1840 | uio = ap->a_uio; | |
1841 | resid = uio->uio_resid; | |
66a1ddf5 | 1842 | error = VOCALL(&fifo_vnode_vops, &ap->a_head); |
984263bc MD |
1843 | ip = VTOI(ap->a_vp); |
1844 | if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL && | |
1845 | (uio->uio_resid != resid || (error == 0 && resid != 0))) | |
1846 | VTOI(ap->a_vp)->i_flag |= IN_ACCESS; | |
1847 | return (error); | |
1848 | } | |
1849 | ||
1850 | /* | |
1851 | * Write wrapper for fifos. | |
0973c589 CP |
1852 | * |
1853 | * ufsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, | |
1854 | * struct ucred *a_cred) | |
984263bc | 1855 | */ |
0961aa92 | 1856 | static |
984263bc | 1857 | int |
0973c589 | 1858 | ufsfifo_write(struct vop_write_args *ap) |
984263bc MD |
1859 | { |
1860 | int error, resid; | |
1861 | struct inode *ip; | |
1862 | struct uio *uio; | |
1863 | ||
1864 | uio = ap->a_uio; | |
1865 | resid = uio->uio_resid; | |
66a1ddf5 | 1866 | error = VOCALL(&fifo_vnode_vops, &ap->a_head); |
984263bc MD |
1867 | ip = VTOI(ap->a_vp); |
1868 | if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0))) | |
1869 | VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE; | |
1870 | return (error); | |
1871 | } | |
1872 | ||
1873 | /* | |
1874 | * Close wrapper for fifos. | |
1875 | * | |
1876 | * Update the times on the inode then do device close. | |
0973c589 | 1877 | * |
b478fdce | 1878 | * ufsfifo_close(struct vnode *a_vp, int a_fflag) |
984263bc | 1879 | */ |
0961aa92 | 1880 | static |
984263bc | 1881 | int |
0973c589 | 1882 | ufsfifo_close(struct vop_close_args *ap) |
984263bc MD |
1883 | { |
1884 | struct vnode *vp = ap->a_vp; | |
1885 | ||
ee173d09 | 1886 | if (VREFCNT(vp) > 1) |
984263bc | 1887 | ufs_itimes(vp); |
66a1ddf5 | 1888 | return (VOCALL(&fifo_vnode_vops, &ap->a_head)); |
984263bc MD |
1889 | } |
1890 | ||
1891 | /* | |
1892 | * Kqfilter wrapper for fifos. | |
1893 | * | |
1894 | * Fall through to ufs kqfilter routines if needed | |
1895 | */ | |
0961aa92 | 1896 | static |
984263bc | 1897 | int |
0973c589 | 1898 | ufsfifo_kqfilter(struct vop_kqfilter_args *ap) |
984263bc MD |
1899 | { |
1900 | int error; | |
1901 | ||
66a1ddf5 | 1902 | error = VOCALL(&fifo_vnode_vops, &ap->a_head); |
984263bc MD |
1903 | if (error) |
1904 | error = ufs_kqfilter(ap); | |
1905 | return (error); | |
1906 | } | |
1907 | ||
984263bc MD |
1908 | /* |
1909 | * Advisory record locking support | |
0973c589 CP |
1910 | * |
1911 | * ufs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl, | |
1912 | * int a_flags) | |
984263bc | 1913 | */ |
0961aa92 | 1914 | static |
984263bc | 1915 | int |
0973c589 | 1916 | ufs_advlock(struct vop_advlock_args *ap) |
984263bc | 1917 | { |
3ff2135f | 1918 | struct inode *ip = VTOI(ap->a_vp); |
984263bc MD |
1919 | |
1920 | return (lf_advlock(ap, &(ip->i_lockf), ip->i_size)); | |
1921 | } | |
1922 | ||
1923 | /* | |
1924 | * Initialize the vnode associated with a new inode, handle aliased | |
1925 | * vnodes. | |
3e72e117 MD |
1926 | * |
1927 | * Make sure directories have their VM object now rather then later, | |
1928 | * saving us from having to check on all the myrid directory VOPs | |
1929 | * that might be executed without a VOP_OPEN being performed. | |
984263bc MD |
1930 | */ |
1931 | int | |
0961aa92 | 1932 | ufs_vinit(struct mount *mntp, struct vnode **vpp) |
984263bc MD |
1933 | { |
1934 | struct inode *ip; | |
1935 | struct vnode *vp; | |
984263bc MD |
1936 | |
1937 | vp = *vpp; | |
1938 | ip = VTOI(vp); | |
57f7b636 | 1939 | |
1c843a13 MD |
1940 | vp->v_type = IFTOVT(ip->i_mode); |
1941 | ||
1942 | switch(vp->v_type) { | |
984263bc MD |
1943 | case VCHR: |
1944 | case VBLK: | |
6ddb7618 | 1945 | vp->v_ops = &mntp->mnt_vn_spec_ops; |
0e9b9130 | 1946 | addaliasu(vp, umajor(ip->i_rdev), uminor(ip->i_rdev)); |
984263bc MD |
1947 | break; |
1948 | case VFIFO: | |
6ddb7618 | 1949 | vp->v_ops = &mntp->mnt_vn_fifo_ops; |
984263bc | 1950 | break; |
3e72e117 | 1951 | case VDIR: |
1c843a13 | 1952 | case VREG: |
b0d18f7d MD |
1953 | vinitvmio(vp, ip->i_size, |
1954 | blkoffsize(ip->i_fs, ip, ip->i_size), | |
1955 | blkoff(ip->i_fs, ip->i_size)); | |
1c843a13 MD |
1956 | break; |
1957 | case VLNK: | |
b0d18f7d MD |
1958 | if (ip->i_size >= vp->v_mount->mnt_maxsymlinklen) { |
1959 | vinitvmio(vp, ip->i_size, | |
1960 | blkoffsize(ip->i_fs, ip, ip->i_size), | |
1961 | blkoff(ip->i_fs, ip->i_size)); | |
1962 | } | |
3e72e117 | 1963 | break; |
984263bc MD |
1964 | default: |
1965 | break; | |
1966 | ||
1967 | } | |
57f7b636 | 1968 | |
c309c6d4 | 1969 | if (ip->i_number == UFS_ROOTINO) |
2247fe02 | 1970 | vsetflags(vp, VROOT); |
984263bc MD |
1971 | /* |
1972 | * Initialize modrev times | |
1973 | */ | |
9d6d26c9 | 1974 | ip->i_modrev = init_va_filerev(); |
984263bc MD |
1975 | *vpp = vp; |
1976 | return (0); | |
1977 | } | |
1978 | ||
1979 | /* | |
1980 | * Allocate a new inode. | |
1981 | */ | |
0961aa92 | 1982 | static |
984263bc | 1983 | int |
0973c589 CP |
1984 | ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp, |
1985 | struct componentname *cnp) | |
984263bc | 1986 | { |
3ff2135f | 1987 | struct inode *ip, *pdir; |
984263bc MD |
1988 | struct direct newdir; |
1989 | struct vnode *tvp; | |
1990 | int error; | |
1991 | ||
1992 | pdir = VTOI(dvp); | |
984263bc MD |
1993 | *vpp = NULL; |
1994 | if ((mode & IFMT) == 0) | |
1995 | mode |= IFREG; | |
1996 | ||
ac690a1d | 1997 | error = ffs_valloc(dvp, mode, cnp->cn_cred, &tvp); |
984263bc MD |
1998 | if (error) |
1999 | return (error); | |
2000 | ip = VTOI(tvp); | |
33036abf | 2001 | ip->i_flags = pdir->i_flags & (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP); |
984263bc MD |
2002 | ip->i_gid = pdir->i_gid; |
2003 | #ifdef SUIDDIR | |
2004 | { | |
2005 | #ifdef QUOTA | |
2006 | struct ucred ucred, *ucp; | |
2007 | ucp = cnp->cn_cred; | |
2008 | #endif | |
2009 | /* | |
2010 | * If we are not the owner of the directory, | |
2011 | * and we are hacking owners here, (only do this where told to) | |
2012 | * and we are not giving it TO root, (would subvert quotas) | |
2013 | * then go ahead and give it to the other user. | |
2014 | * Note that this drops off the execute bits for security. | |
2015 | */ | |
2016 | if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) && | |
2017 | (pdir->i_mode & ISUID) && | |
2018 | (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) { | |
2019 | ip->i_uid = pdir->i_uid; | |
2020 | mode &= ~07111; | |
2021 | #ifdef QUOTA | |
2022 | /* | |
2023 | * Make sure the correct user gets charged | |
2024 | * for the space. | |
2025 | * Quickly knock up a dummy credential for the victim. | |
2026 | * XXX This seems to never be accessed out of our | |
2027 | * context so a stack variable is ok. | |
2028 | */ | |
2029 | ucred.cr_ref = 1; | |
2030 | ucred.cr_uid = ip->i_uid; | |
2031 | ucred.cr_ngroups = 1; | |
2032 | ucred.cr_groups[0] = pdir->i_gid; | |
2033 | ucp = &ucred; | |
2034 | #endif | |
2035 | } else | |
2036 | ip->i_uid = cnp->cn_cred->cr_uid; | |
2037 | ||
2038 | #ifdef QUOTA | |
50e58362 MD |
2039 | if ((error = ufs_getinoquota(ip)) || |
2040 | (error = ufs_chkiq(ip, 1, ucp, 0))) { | |
ac690a1d | 2041 | ffs_vfree(tvp, ip->i_number, mode); |
984263bc MD |
2042 | vput(tvp); |
2043 | return (error); | |
2044 | } | |
2045 | #endif | |
2046 | } | |
2047 | #else /* !SUIDDIR */ | |
2048 | ip->i_uid = cnp->cn_cred->cr_uid; | |
2049 | #ifdef QUOTA | |
50e58362 MD |
2050 | if ((error = ufs_getinoquota(ip)) || |
2051 | (error = ufs_chkiq(ip, 1, cnp->cn_cred, 0))) { | |
ac690a1d | 2052 | ffs_vfree(tvp, ip->i_number, mode); |
984263bc MD |
2053 | vput(tvp); |
2054 | return (error); | |
2055 | } | |
2056 | #endif | |
2057 | #endif /* !SUIDDIR */ | |
712774a1 | 2058 | ip->i_din.di_spare7E = 0; |
984263bc MD |
2059 | ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; |
2060 | ip->i_mode = mode; | |
2061 | tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */ | |
2062 | ip->i_effnlink = 1; | |
2063 | ip->i_nlink = 1; | |
2064 | if (DOINGSOFTDEP(tvp)) | |
2065 | softdep_change_linkcnt(ip); | |
2066 | if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) && | |
2b3f93ea MD |
2067 | caps_priv_check(cnp->cn_cred, SYSCAP_NOVFS_SETGID)) |
2068 | { | |
984263bc | 2069 | ip->i_mode &= ~ISGID; |
41c20dac | 2070 | } |
984263bc | 2071 | |
2b69e610 | 2072 | if (cnp->cn_flags & CNP_ISWHITEOUT) |
984263bc MD |
2073 | ip->i_flags |= UF_OPAQUE; |
2074 | ||
1c843a13 MD |
2075 | /* |
2076 | * Regular files and directories need VM objects. Softlinks do | |
2077 | * not (not immediately anyway). | |
2078 | */ | |
2079 | if (tvp->v_type == VREG || tvp->v_type == VDIR) | |
b0d18f7d | 2080 | vinitvmio(tvp, 0, PAGE_SIZE, -1); |
1c843a13 | 2081 | |
984263bc MD |
2082 | /* |
2083 | * Make sure inode goes to disk before directory entry. | |
2084 | */ | |
ac690a1d | 2085 | error = ffs_update(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp))); |
984263bc MD |
2086 | if (error) |
2087 | goto bad; | |
2088 | ufs_makedirentry(ip, cnp, &newdir); | |
2089 | error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL); | |
2090 | if (error) | |
2091 | goto bad; | |
2092 | *vpp = tvp; | |
2093 | return (0); | |
2094 | ||
2095 | bad: | |
2096 | /* | |
2097 | * Write error occurred trying to update the inode | |
2098 | * or the directory so must deallocate the inode. | |
2099 | */ | |
2100 | ip->i_effnlink = 0; | |
2101 | ip->i_nlink = 0; | |
2102 | ip->i_flag |= IN_CHANGE; | |
2103 | if (DOINGSOFTDEP(tvp)) | |
2104 | softdep_change_linkcnt(ip); | |
2105 | vput(tvp); | |
2106 | return (error); | |
2107 | } | |
2108 | ||
2109 | static int | |
0973c589 | 2110 | ufs_missingop(struct vop_generic_args *ap) |
984263bc | 2111 | { |
31bd717a | 2112 | panic("no vop function for %s in ufs child", ap->a_desc->sd_name); |
984263bc MD |
2113 | return (EOPNOTSUPP); |
2114 | } | |
2115 | ||
2116 | static struct filterops ufsread_filtops = | |
4c91dbc9 | 2117 | { FILTEROP_ISFD, NULL, filt_ufsdetach, filt_ufsread }; |
984263bc | 2118 | static struct filterops ufswrite_filtops = |
4c91dbc9 | 2119 | { FILTEROP_ISFD, NULL, filt_ufsdetach, filt_ufswrite }; |
984263bc | 2120 | static struct filterops ufsvnode_filtops = |
4c91dbc9 | 2121 | { FILTEROP_ISFD, NULL, filt_ufsdetach, filt_ufsvnode }; |
984263bc | 2122 | |
0973c589 CP |
2123 | /* |
2124 | * ufs_kqfilter(struct vnode *a_vp, struct knote *a_kn) | |
2125 | */ | |
984263bc | 2126 | static int |
0973c589 | 2127 | ufs_kqfilter(struct vop_kqfilter_args *ap) |
984263bc MD |
2128 | { |
2129 | struct vnode *vp = ap->a_vp; | |
2130 | struct knote *kn = ap->a_kn; | |
2131 | ||
2132 | switch (kn->kn_filter) { | |
2133 | case EVFILT_READ: | |
2134 | kn->kn_fop = &ufsread_filtops; | |
2135 | break; | |
2136 | case EVFILT_WRITE: | |
2137 | kn->kn_fop = &ufswrite_filtops; | |
2138 | break; | |
2139 | case EVFILT_VNODE: | |
2140 | kn->kn_fop = &ufsvnode_filtops; | |
2141 | break; | |
2142 | default: | |
b287d649 | 2143 | return (EOPNOTSUPP); |
984263bc MD |
2144 | } |
2145 | ||
2146 | kn->kn_hook = (caddr_t)vp; | |
2147 | ||
5b22f1a7 | 2148 | /* XXX: kq token actually protects the list */ |
3b998fa9 | 2149 | lwkt_gettoken(&vp->v_token); |
5b22f1a7 | 2150 | knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); |
3b998fa9 | 2151 | lwkt_reltoken(&vp->v_token); |
984263bc MD |
2152 | |
2153 | return (0); | |
2154 | } | |
2155 | ||
2156 | static void | |
2157 | filt_ufsdetach(struct knote *kn) | |
2158 | { | |
2159 | struct vnode *vp = (struct vnode *)kn->kn_hook; | |
2160 | ||
3b998fa9 | 2161 | lwkt_gettoken(&vp->v_token); |
5b22f1a7 | 2162 | knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); |
3b998fa9 | 2163 | lwkt_reltoken(&vp->v_token); |
984263bc MD |
2164 | } |
2165 | ||
2166 | /*ARGSUSED*/ | |
2167 | static int | |
2168 | filt_ufsread(struct knote *kn, long hint) | |
2169 | { | |
2170 | struct vnode *vp = (struct vnode *)kn->kn_hook; | |
2171 | struct inode *ip = VTOI(vp); | |
57b24f4e | 2172 | off_t off; |
984263bc MD |
2173 | |
2174 | /* | |
2175 | * filesystem is gone, so set the EOF flag and schedule | |
2176 | * the knote for deletion. | |
2177 | */ | |
2178 | if (hint == NOTE_REVOKE) { | |
3bcb6e5e | 2179 | kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); |
984263bc MD |
2180 | return (1); |
2181 | } | |
2182 | ||
57b24f4e MD |
2183 | off = ip->i_size - kn->kn_fp->f_offset; |
2184 | kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX; | |
2185 | if (kn->kn_sfflags & NOTE_OLDAPI) | |
2186 | return(1); | |
984263bc MD |
2187 | return (kn->kn_data != 0); |
2188 | } | |
2189 | ||
2190 | /*ARGSUSED*/ | |
2191 | static int | |
2192 | filt_ufswrite(struct knote *kn, long hint) | |
2193 | { | |
984263bc MD |
2194 | /* |
2195 | * filesystem is gone, so set the EOF flag and schedule | |
2196 | * the knote for deletion. | |
2197 | */ | |
2198 | if (hint == NOTE_REVOKE) | |
3bcb6e5e | 2199 | kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); |
984263bc MD |
2200 | |
2201 | kn->kn_data = 0; | |
2202 | return (1); | |
2203 | } | |
2204 | ||
2205 | static int | |
2206 | filt_ufsvnode(struct knote *kn, long hint) | |
2207 | { | |
984263bc MD |
2208 | if (kn->kn_sfflags & hint) |
2209 | kn->kn_fflags |= hint; | |
2210 | if (hint == NOTE_REVOKE) { | |
3bcb6e5e | 2211 | kn->kn_flags |= (EV_EOF | EV_NODATA); |
984263bc MD |
2212 | return (1); |
2213 | } | |
2214 | return (kn->kn_fflags != 0); | |
2215 | } | |
2216 | ||
2217 | /* Global vfs data structures for ufs. */ | |
66a1ddf5 MD |
2218 | static struct vop_ops ufs_vnode_vops = { |
2219 | .vop_default = vop_defaultop, | |
2220 | .vop_fsync = (void *)ufs_missingop, | |
2221 | .vop_read = (void *)ufs_missingop, | |
2222 | .vop_reallocblks = (void *)ufs_missingop, | |
2223 | .vop_write = (void *)ufs_missingop, | |
2224 | .vop_access = ufs_access, | |
2225 | .vop_advlock = ufs_advlock, | |
2226 | .vop_bmap = ufs_bmap, | |
2227 | .vop_old_lookup = ufs_lookup, | |
2228 | .vop_close = ufs_close, | |
2229 | .vop_old_create = ufs_create, | |
2230 | .vop_getattr = ufs_getattr, | |
2231 | .vop_inactive = ufs_inactive, | |
66a1ddf5 | 2232 | .vop_old_link = ufs_link, |
64950f31 MD |
2233 | .vop_old_mkdir = ufs_mkdir, |
2234 | .vop_old_mknod = ufs_mknod, | |
3a907475 | 2235 | .vop_open = vop_stdopen, |
64950f31 | 2236 | .vop_pathconf = vop_stdpathconf, |
66a1ddf5 MD |
2237 | .vop_kqfilter = ufs_kqfilter, |
2238 | .vop_print = ufs_print, | |
2239 | .vop_readdir = ufs_readdir, | |
2240 | .vop_readlink = ufs_readlink, | |
2241 | .vop_reclaim = ufs_reclaim, | |
2242 | .vop_old_remove = ufs_remove, | |
2243 | .vop_old_rename = ufs_rename, | |
2244 | .vop_old_rmdir = ufs_rmdir, | |
2245 | .vop_setattr = ufs_setattr, | |
349433c9 | 2246 | .vop_markatime = ufs_markatime, |
66a1ddf5 MD |
2247 | .vop_strategy = ufs_strategy, |
2248 | .vop_old_symlink = ufs_symlink, | |
66a1ddf5 | 2249 | .vop_old_whiteout = ufs_whiteout |
984263bc | 2250 | }; |
66a1ddf5 MD |
2251 | |
2252 | static struct vop_ops ufs_spec_vops = { | |
8be7edad | 2253 | .vop_default = vop_defaultop, |
66a1ddf5 MD |
2254 | .vop_fsync = (void *)ufs_missingop, |
2255 | .vop_access = ufs_access, | |
8be7edad MD |
2256 | .vop_close = ufs_close, |
2257 | .vop_getattr = ufs_getattr, | |
66a1ddf5 | 2258 | .vop_inactive = ufs_inactive, |
66a1ddf5 | 2259 | .vop_print = ufs_print, |
8be7edad | 2260 | .vop_read = vop_stdnoread, |
66a1ddf5 MD |
2261 | .vop_reclaim = ufs_reclaim, |
2262 | .vop_setattr = ufs_setattr, | |
349433c9 | 2263 | .vop_markatime = ufs_markatime, |
8be7edad | 2264 | .vop_write = vop_stdnowrite |
984263bc | 2265 | }; |
66a1ddf5 MD |
2266 | |
2267 | static struct vop_ops ufs_fifo_vops = { | |
2268 | .vop_default = fifo_vnoperate, | |
2269 | .vop_fsync = (void *)ufs_missingop, | |
2270 | .vop_access = ufs_access, | |
2271 | .vop_close = ufsfifo_close, | |
2272 | .vop_getattr = ufs_getattr, | |
2273 | .vop_inactive = ufs_inactive, | |
66a1ddf5 | 2274 | .vop_kqfilter = ufsfifo_kqfilter, |
66a1ddf5 MD |
2275 | .vop_print = ufs_print, |
2276 | .vop_read = ufsfifo_read, | |
2277 | .vop_reclaim = ufs_reclaim, | |
2278 | .vop_setattr = ufs_setattr, | |
349433c9 | 2279 | .vop_markatime = ufs_markatime, |
66a1ddf5 | 2280 | .vop_write = ufsfifo_write |
984263bc | 2281 | }; |
984263bc | 2282 | |
66a1ddf5 MD |
2283 | VNODEOP_SET(ufs_vnode_vops); |
2284 | VNODEOP_SET(ufs_spec_vops); | |
2285 | VNODEOP_SET(ufs_fifo_vops); | |
984263bc | 2286 | |
0973c589 | 2287 | /* |
31bd717a | 2288 | * ufs_vnoperate() |
0973c589 | 2289 | */ |
984263bc | 2290 | int |
0973c589 | 2291 | ufs_vnoperate(struct vop_generic_args *ap) |
984263bc | 2292 | { |
66a1ddf5 | 2293 | return (VOCALL(&ufs_vnode_vops, ap)); |
984263bc MD |
2294 | } |
2295 | ||
0973c589 | 2296 | /* |
31bd717a | 2297 | * ufs_vnoperatefifo() |
0973c589 | 2298 | */ |
984263bc | 2299 | int |
0973c589 | 2300 | ufs_vnoperatefifo(struct vop_generic_args *ap) |
984263bc | 2301 | { |
66a1ddf5 | 2302 | return (VOCALL(&ufs_fifo_vops, ap)); |
984263bc MD |
2303 | } |
2304 | ||
0973c589 | 2305 | /* |
31bd717a | 2306 | * ufs_vnoperatespec() |
0973c589 | 2307 | */ |
984263bc | 2308 | int |
0973c589 | 2309 | ufs_vnoperatespec(struct vop_generic_args *ap) |
984263bc | 2310 | { |
66a1ddf5 | 2311 | return (VOCALL(&ufs_spec_vops, ap)); |
984263bc | 2312 | } |